Initializing help system before first use

Retrieving data from a Mosel model


Type: Programming
Rating: 3 (intermediate)
Description: mmexset.c: Using sets in Mosel (requires burglari.bim)
  • retrieve a set by its model name
  • get the set size
  • get first and last set element
  • get the name or index of a set element
mmexarr.c: Using arrays in Mosel (requires trans.bim)
  • compare index tuples
  • retrieve an array by its model name
  • get array size and number of dimensions
  • get indices of first and last array entries
  • get (value of) array entries
  • check whether an index tuple lies in the range of an array
mmexas.c: Using arrays with index sets (requires trans.bim)
  • get indexing sets of an array
  • get array type
  • enumerate array entries in usual and transposed order
  • enumerate true array entries
mmexlst.c: Using lists in Mosel (requires euler.mos and euler.dat)
  • retrieve a list by its model name
  • get the list size
  • enumerate the list elements
  • get value of list element
mmexrec.c: Using records in Mosel (requires burglar_rec.mos and burglar_rec.dat)
  • retrieve an array of records (user type) by its model name
  • retrieve the record field information (field name, type, and number)
  • enumerate the array of records
  • for each array entry (record) get the value of all its fields
mmexprob.c: Accessing problems and solution information with Mosel (requires blend2.bim)
  • export problem to a file (MPS or LP format)
  • get problem status
  • get objective function value
  • get primal/dual solution values, and constraint activity
Note that these examples require the provided mos files to be pre-compiled.
File(s): mmexset.c, mmexarr.c, mmexas.c, mmexlst.c, mmexrec.c, mmexprob.c
Data file(s): burglari.mos, trans.mos, euler.mos, euler.dat, burglar_rec.mos, burglar_rec.dat, blend2.mos


mmexset.c
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexset.c                                      */
/*  ``````````````                                      */
/*  Example for the use of the Mosel libraries          */
/*  (accessing sets in Mosel)                           */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2001                        */
/********************************************************/

#include <stdio.h>
#include "xprm_rt.h"

int main()
{
 XPRMmodel mod;
 XPRMalltypes rvalue;
 XPRMset set;
 int result,type,first,last,i,size;
 char *setname;
 XPRMalltypes itemname;

 i=XPRMinit();
 if((i!=0)&&(i!=32))                      /* Initialize Mosel */
  return 1;

 if((mod=XPRMloadmod("Models/burglari.bim",NULL))==NULL)  /* Load a BIM file */
  return 2;
 
 if(XPRMrunmod(mod,&result,NULL))         /* Run the model */
  return 3;

 setname="ITEMS";
 type = XPRMfindident(mod,setname,&rvalue);  /* Get the model object 'ITEMS' */
 if((XPRM_TYP(type)!=XPRM_TYP_STRING)||     /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_SET))         /* it must be a set of strings */
  return 4;

 set = rvalue.set;
  
 size = XPRMgetsetsize(set);             /* Get the size of the set */
 if(size>0)
 {
  first = XPRMgetfirstsetndx(set);       /* Get the number of the first index */
  last = XPRMgetlastsetndx(set);         /* Get the number of the last index */
  printf("Elements of set %s:\n",setname);
  for(i=first;i<=last;i++)              /* Print names of all set elements */
   printf(" %s,",XPRMgetelsetval(set,i,&itemname)->string);
  printf("\n");  
 }

 itemname.string="CD player";
 if(XPRMgetelsetndx(mod, set, &itemname)<0)
  printf("'%s' is not contained in '%s'.\n", itemname.string, setname); 
 
 return 0;
}


mmexarr.c
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexarr.c                                      */
/*  ``````````````                                      */
/*  Example for the use of the Mosel libraries          */
/*  (accessing arrays in Mosel)                         */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2001                        */
/********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "xprm_rt.h"

int main()
{
 XPRMmodel mod;
 XPRMalltypes rvalue;
 XPRMarray varr;
 XPRMmpvar lv;
 int *indices, dim, size, result, type, i;
 int ind1[] = {-1,0,6}, ind2[] = {-1,2,-5};

 i=XPRMinit();
 if((i!=0)&&(i!=32))                      /* Initialize Mosel */
  return 1;

 /**** Compare two given index tuples independent of any model ****/ 
 if(XPRMcmpindices(3, ind1, ind2)<0)      /* Compare two index tuples */
  printf("The tuple (%d,%d,%d) comes before (%d,%d,%d).\n", 
         ind1[0], ind1[1], ind1[2], ind2[0], ind2[1], ind2[2]);

 /**** Load and run a model ****/
 if((mod=XPRMloadmod("Models/trans.bim",NULL))==NULL)  /* Load a BIM file */
  return 2;
 
 if(XPRMrunmod(mod,&result,NULL))         /* Run the model */
  return 3;

 type=XPRMfindident(mod,"x",&rvalue);     /* Get the model object named 'x' */
 if((XPRM_TYP(type)!=XPRM_TYP_MPVAR)||    /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_ARR))       /* it must be an array of unknowns */
  return 4;
 varr=rvalue.array;
 
 size = XPRMgetarrsize(varr);             /* Get the array size */

 dim = XPRMgetarrdim(varr);               /* Get the number of dimensions of 
                                           the array */
 printf("\nArray size: %d true elements, %d dimensions\n", size, dim);
 
 indices = (int *)malloc(dim*sizeof(int));

 XPRMgetfirstarrentry(varr,indices);      /* Get the first index tuple */
 printf("First array entry: x(%d", indices[0]);
 for(i=1;i<dim;i++) printf(",%d", indices[i]);

 XPRMgetlastarrentry(varr,indices);       /* Get the last index tuple */
 printf(");  last array entry: x(%d", indices[0]);
 for(i=1;i<dim;i++) printf(",%d", indices[i]);
 printf(")\n");

 for(i=0;i<dim;i++) indices[i]=2;
 if(XPRMchkarrind(varr,indices)==0)     /* Check whether a tuple lies within */
 {                                      /* the range defined for the array   */
  printf("The tuple (%d", indices[0]);
  for(i=1;i<dim;i++) printf(",%d", indices[i]);
  printf(") lies within the range of the array");
  XPRMgetarrval(varr, indices, &lv);      /* Get the array entry corresponding
                                           to the index tuple: it may be NULL
                                           in the case of a sparse array! */
  if(lv==NULL) 
   printf("\nbut the array entry is not defined (sparse array!).\n");
  else printf(".\n");
 }

 free(indices);
 XPRMresetmod(mod);
 
 return 0;
}

mmexas.c
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexas.c                                       */
/*  `````````````                                       */
/*  Example for the use of the Mosel libraries          */
/*  (using arrays with index sets: different ways       */
/*   of enumerating arrays)                             */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2001                        */
/********************************************************/

#include <stdio.h>
#include <stdlib.h>
#include "xprm_rt.h"

int main()
{
 XPRMmodel mod;
 XPRMalltypes rvalue;
 XPRMarray varr;
 XPRMset *sets;
 int *indices, dim, result, type, i;

 i=XPRMinit();
 if((i!=0)&&(i!=32))                      /* Initialize Mosel */
  return 1;

 if((mod=XPRMloadmod("Models/trans.bim",NULL))==NULL)  /* Load a BIM file */
  return 2;
 
 if(XPRMrunmod(mod,&result,NULL))         /* Run the model */
  return 3;

 type=XPRMfindident(mod,"x",&rvalue);     /* Get the model object named 'x' */
 if((XPRM_TYP(type)!=XPRM_TYP_MPVAR)||      /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_ARR))         /* it must be an array of unknowns */
  return 4;
 varr=rvalue.array;
 
 dim = XPRMgetarrdim(varr);               /* Get the number of dimensions of 
                                           the array */
 indices = (int *)malloc(dim*sizeof(int));
 sets = (XPRMset *)malloc(dim*sizeof(XPRMset)); 

 XPRMgetarrsets(varr,sets);               /* Get the indexing sets  */
   
 printf("\n1. Logic entries:\n");
 XPRMgetfirstarrentry(varr,indices);         /* Get the first index tuple */
 do
 {
  printf("x(");
  for(i=0;i<dim-1;i++)
   printf("%s,", XPRMgetelsetval(sets[i],indices[i],&rvalue)->string);
  printf("%s), ",XPRMgetelsetval(sets[dim-1],indices[dim-1],&rvalue)->string);
 } while(!XPRMgetnextarrentry(varr,indices));  /* Get the next index tuple */ 

 type = XPRMgetarrtype(varr);                   /* Get the type of the array */

 if(XPRM_GRP(type)!=XPRM_ARR_DENSE)         /* There would be no difference to
                                           the first way of enumerating 
                                           in the case of a dense array */
 { 
  printf("\n\n2. True entries:\n");
  XPRMgetfirstarrtruentry(varr,indices);    /* Get the first true index tuple */
  do
  {
   printf("x(");
   for(i=0;i<dim-1;i++)
    printf("%s,",XPRMgetelsetval(sets[i],indices[i],&rvalue)->string);
   printf("%s), ",XPRMgetelsetval(sets[dim-1],indices[dim-1],&rvalue)->string);
  } while(!XPRMgetnextarrtruentry(varr,indices)); /* Get next true index tuple*/
 } 
 printf("\n");

 free(sets);
 free(indices);
 XPRMresetmod(mod);
 
 return 0;
}

mmexlst.c
/*******************************************************
   Mosel Library Examples
   ====================== 

   file mmexlst.c
   ``````````````
   Accessing modeling objects 
   (enumerating the elements of a list).
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2007
********************************************************/

#include <stdio.h>
#include "xprm_mc.h"

int main()
{
 XPRMmodel mod;
 XPRMalltypes value;
 mm_list lst;
 int i,result,type,ct;
 void *ref;

 i=XPRMinit();
 if((i!=0)&&(i!=32))       /* Initialize Mosel */
  return 1;
                           /* Disable output from Mosel (make model silent) */
 XPRMsetdefstream(NULL, XPRM_F_WRITE, "null:");  
                           /* Execute = compile/load/run a model */
 if(XPRMexecmod(NULL, "Models/euler.mos", NULL, &result, &mod))
  return 2;

                           /* Get the model object named 'TOUR' */
 type=XPRMfindident(mod, "TOUR", &value);      
                           /* Check the type: it must be a list of integers */
 if((XPRM_STR(type)!=XPRM_STR_LIST)||(XPRM_TYP(type)!=XPRM_TYP_INT))
   return 3; 

 lst=value.list;           /* Retrieve the list */
 ref=NULL; ct=0;
 printf("Tour: ");         /* Print out all list elements */
 while((ref=XPRMgetnextlistelt(lst, ref, &type, &value))!=NULL)
  if (++ct<XPRMgetlistsize(lst))  
   printf("%d -> ", value.integer);
  else
   printf("%d\n", value.integer); 

 return 0;
}


mmexrec.c
/*******************************************************
   Mosel Library Examples
   ====================== 

   file mmexrec.c
   ``````````````
   Accessing modeling objects 
   (enumerating an array of records and
    printing the value of each record field).
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2007
********************************************************/

#include <stdio.h>
#include "xprm_mc.h"

typedef struct {                      /* For storing record field info: */
 const char *name;                    /*   field name */
 int type,num;                        /*   field type and number */
} s_field; 

int main()
{
 XPRMmodel mod;
 XPRMalltypes value,rvalue,avalue,fvalue;
 XPRMarray arr;
 XPRMset set;
 s_field fields[2];
 int i,result,type,num,utype,first,indices[1];
 void *ref;

 i=XPRMinit();
 if((i!=0)&&(i!=32))                  /* Initialize Mosel */
  return 1;
                                      /* Execute = compile/load/run a model */
 if(XPRMexecmod(NULL, "Models/burglar_rec.mos", NULL, &result, &mod))
  return 2;

 type=XPRMfindident(mod,"I",&value);  /* Get the model object named 'I' */
 if(XPRM_STR(type)!=XPRM_STR_ARR)     /* Check the type: it must be an array */
   return 3;
 type=XPRM_TYP(type);                 /* Expand type of a user type: */
 XPRMgettypeprop(mod, type, XPRM_TPROP_EXP, (XPRMalltypes*)&utype);
 if(XPRM_STR(utype)!=XPRM_STR_REC)    /* the structure must be a record */
  return 4;
 
          /* Retrieve record field info (we know there are 2 fields) */
 for(ref=NULL,i=0;i<2;i++)
  ref=XPRMgetnextfield(mod, ref, type, &(fields[i].name), &(fields[i].type),
                       &(fields[i].num));

          /* Enumerate the array (we know it has a single dimension) */ 
 arr=value.array;   
 XPRMgetarrsets(arr, &set);           /* Get the indexing set */
 
 XPRMgetfirstarrentry(arr, indices);  /* Get the first index tuple */
 do
 {                                       /* Retrieve the array index */
  printf("I(%s):  \t", XPRMgetelsetval(set,indices[0],&rvalue)->string);
  XPRMgetarrval(arr, indices, &avalue);  /* Retrieve array entry (=record) */
                                         /* Contents of 1st record field */
  XPRMgetfieldval(mod, type, avalue.ref, fields[0].num, &fvalue);
  printf("%s=%g ", fields[0].name, fvalue.real);
                                         /* Contents of 2nd record field */
  XPRMgetfieldval(mod, type, avalue.ref, fields[1].num, &fvalue);
  printf("%s=%g\n", fields[1].name, fvalue.real);
 } while(!XPRMgetnextarrentry(arr, indices));   /* Get the next index tuple */ 
 
 return 0;
}

mmexprob.c
/********************************************************/
/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file mmexprob.c                                     */
/*  ```````````````                                     */
/*  Example for the use of the Mosel libraries          */
/*  (accessing problems and solution information)       */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: S. Heipcke, 2001                        */
/********************************************************/

#include <stdio.h>
#include "xprm_rt.h"

int main()
{
 XPRMmodel mod;
 XPRMalltypes rvalue;
 XPRMarray varr, darr;
 XPRMmpvar x;
 XPRMlinctr lgrade;
 double cost;
 int result, type;
 int indices[1],i;

 i=XPRMinit();
 if((i!=0)&&(i!=32))                                   /* Initialize Mosel */
  return 1;

 if((mod=XPRMloadmod("Models/blend2.bim",NULL))==NULL) /* Load a BIM file */
  return 2;
 
 if(XPRMrunmod(mod,&result,NULL))         /* Run the model (it includes 
                                             optimization) */
  return 3;

 XPRMexportprob(mod,"p","blend",NULL);    /* Export problem to a file in LP 
                                             format (maximization) */

 if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)
  return 5;                             /* Test whether a solution is found */

 printf("Objective value: %g\n", XPRMgetobjval(mod));
                                        /* Print the objective function value */

 type=XPRMfindident(mod,"x",&rvalue);     /* Get the model object named 'x' */
 if((XPRM_TYP(type)!=XPRM_TYP_MPVAR)||    /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_ARR))       /* it must be an array of unknowns */
  return 5;
 varr=rvalue.array;

 type=XPRMfindident(mod,"COST",&rvalue);  /* Get the model object 'COST' */
 if((XPRM_TYP(type)!=XPRM_TYP_REAL)||     /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_ARR))       /* it must be an array of reals */
  return 6;
 darr=rvalue.array;
   
 XPRMgetfirstarrentry(varr, indices);  /* Get the first entry of the array varr
                                          (we know that the array is dense 
                                          and has a single dimension) */
 do
 {
  XPRMgetarrval(varr,indices,&x);         /* Get the a variable from varr */
  XPRMgetarrval(darr,indices,&cost);      /* Get the corresponding cost value */
  printf("x(%d)=%g (COST: %g)\n",indices[0],
   XPRMgetvsol(mod,x), cost);            /* Print the solution value */
 } while(!XPRMgetnextarrentry(varr, indices));  /* Get the next index */
 
 type=XPRMfindident(mod,"LoGrade",&rvalue);/* Get the model object 'LoGrade' */
 if((XPRM_TYP(type)!=XPRM_TYP_LINCTR)||    /* Check the type: */
    (XPRM_STR(type)!=XPRM_STR_REF))        /* it must be a reference to a linear
                                              constraint */
  return 7;
 lgrade=rvalue.linctr;

 printf("LoGrade: activity=%g, dual=%g\n",
   XPRMgetact(mod,lgrade), XPRMgetdual(mod,lgrade));
                                      /* Print the activity and dual values */

 XPRMresetmod(mod);
 return 0;
}