ugcomp.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcomp.c
`````````````
Compiling a model into a BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001
********************************************************/
#include <stdlib.h>
#include "xprm_mc.h"
int main()
{
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMcompmod(NULL, "burglar2.mos", NULL, "Knapsack example"))
return 2; /* Compile the model burglar2.mos,
output the file burglar2.bim */
return 0;
}
|
|
ugcomptmp.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcomptmp.c
````````````````
Compiling a model into a BIM file saved in Mosel's
temporary directory, then load and run it.
(c) 2013 Fair Isaac Corporation
author: S. Heipcke, Apr. 2013
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
XPRMmodel mod;
int result;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMcompmod(NULL, "burglar2.mos", "tmp:burglar2.bim", "Knapsack example"))
return 2; /* Compile the model burglar2.mos,
output the file burglar2.bim
in Mosel's temporary directory */
if((mod=XPRMloadmod("tmp:burglar2.bim",NULL))==NULL) /* Load the BIM file */
return 3;
if(XPRMrunmod(mod,&result,NULL)) /* Run the model */
return 4;
return 0;
}
|
|
ugexec.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugexec.c
`````````````
Execute (compile/load/run) a model.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
int result;
if(XPRMinit()) /* Initialize Mosel */
return 1;
/* Execute = compile/load/run a model */
if(XPRMexecmod(NULL,"burglar2.mos",NULL,&result,NULL))
return 2;
return 0;
}
|
|
ugrun.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugrun.c
````````````
Executing a BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001
********************************************************/
#include <stdio.h>
#include "xprm_rt.h"
int main()
{
XPRMmodel mod;
int result;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if((mod=XPRMloadmod("burglar2.bim",NULL))==NULL) /* Load a BIM file */
return 2;
if(XPRMrunmod(mod,&result,NULL)) /* Run the model */
return 3;
return 0;
}
|
|
ugdefstream.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugdefstream.c
``````````````````
Redirecting model output.
(c) 2013 Fair Isaac Corporation
author: S. Heipcke, Apr. 2013
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
int result;
if(XPRMinit()) /* Initialize Mosel */
return 1;
/* Uncomment one of the following: */
/* Disable model output */
/* XPRMsetdefstream(NULL, XPRM_F_WRITE, "null:"); */
/* Double model output (file+stdout) */
XPRMsetdefstream(NULL, XPRM_F_WRITE, "tee:burgres.txt&");
/* Execute = compile/load/run a model */
if(XPRMexecmod(NULL, "burglar2.mos", NULL, &result, NULL))
return 2;
printf("`burglar2' returned: %d\n", result);
return 0;
}
|
|
ugarray1.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugarray1.c
```````````````
Accessing modeling objects (sparse arrays).
Running the BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. Oct. 2010
********************************************************/
#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;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if((mod=XPRMloadmod("transport.bim",NULL))==NULL) /* Load a BIM file */
return 2;
if(XPRMrunmod(mod,&result,NULL)) /* Run the model */
return 3;
type=XPRMfindident(mod,"flow",&rvalue); /* Get the model object named 'flow' */
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 */
XPRMgetfirstarrtruentry(varr,indices); /* Get the first true index tuple */
do
{
printf("flow(");
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;
}
|
|
ugarray2.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugarray2.c
```````````````
Accessing modeling objects (sparse arrays).
Executing a model file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002, rev. Oct. 2010
********************************************************/
#include <stdio.h>
#include <stdlib.h>
#include "xprm_mc.h"
int main()
{
XPRMmodel mod;
XPRMalltypes rvalue;
XPRMarray varr;
XPRMset *sets;
int *indices, dim, result, type, i;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMexecmod(NULL,"transport.mos",NULL,&result,&mod))
return 2; /* Execute a model file */
type=XPRMfindident(mod,"flow",&rvalue); /* Get the model object named 'flow' */
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 */
XPRMgetfirstarrtruentry(varr,indices); /* Get the first true index tuple */
do
{
printf("flow(");
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;
}
|
|
ugcb.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcb.c
```````````
Retrieve model output via a callback.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2006, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
/**************************************/
/* Callback function to handle output */
/**************************************/
long XPRM_RTC cbmsg(XPRMmodel model, void *info, char *buf, unsigned long size)
{
/* Note: 'model' is NULL if the stream is used outside of an execution */
printf("Mosel: %.*s", (int)size, buf);
return 0;
}
int main()
{
int result;
char outfile_name[40]; /* File name of output stream */
if(XPRMinit()) /* Initialize Mosel */
return 1;
/* Using 'sysfd' driver: */
/* Redirect error stream to default output */
XPRMsetdefstream(NULL, XPRM_F_ERROR, "sysfd:1");
/* Prepare file name for output stream */
/* using 'cb' driver: */
/* "cb:function_pointer[/callback_data]" */
sprintf(outfile_name, "cb:%p", cbmsg);
/* Set default output stream to callback */
XPRMsetdefstream(NULL, XPRM_F_WRITE, outfile_name);
/* Execute = compile/load/run a model */
if(XPRMexecmod(NULL, "burglar2.mos", NULL, &result, NULL))
return 2;
return 0;
}
|
|
ugsol1.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugsol1.c
`````````````
Accessing modeling objects and solution information.
Running the BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001
********************************************************/
#include <stdio.h>
#include "xprm_rt.h"
int main()
{
XPRMmodel mod;
XPRMalltypes rvalue, itemname;
XPRMarray varr, darr;
XPRMmpvar x;
XPRMset set;
int indices[1], result, type;
double val;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if((mod=XPRMloadmod("burglar3.bim",NULL))==NULL) /* Load a BIM file */
return 2;
if(XPRMrunmod(mod,&result,NULL)) /* Run the model (includes
optimization) */
return 3;
if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)
return 4; /* Test whether a solution is found */
printf("Objective value: %g\n", XPRMgetobjval(mod));
/* Print the objective function value */
type=XPRMfindident(mod,"take",&rvalue); /* Get the model object 'take' */
if((XPRM_TYP(type)!=XPRM_TYP_MPVAR)|| /* Check the type: */
(XPRM_STR(type)!=XPRM_STR_ARR)) /* it must be an `mpvar' array */
return 5;
varr = rvalue.array;
type=XPRMfindident(mod,"VALUE",&rvalue); /* Get the model object 'VALUE' */
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;
type = XPRMfindident(mod,"ITEMS",&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 7;
set = rvalue.set;
XPRMgetfirstarrentry(varr, indices); /* Get the first entry of array varr
(we know that the array is dense
and has a single dimension) */
do
{
XPRMgetarrval(varr,indices,&x); /* Get a variable from varr */
XPRMgetarrval(darr,indices,&val); /* Get the corresponding value */
printf("take(%s): %g\t (item value: %g)\n", XPRMgetelsetval(set, indices[0],
&itemname)->string, XPRMgetvsol(mod,x), val);
/* Print the solution value */
} while(!XPRMgetnextarrentry(varr, indices)); /* Get the next index */
XPRMresetmod(mod);
return 0;
}
|
|
ugsol2.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugsol2.c
`````````````
Accessing modeling objects and solution information.
Executing a model file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
XPRMmodel mod;
XPRMalltypes rvalue, itemname;
XPRMarray varr, darr;
XPRMmpvar x;
XPRMset set;
int indices[1], result, type;
double val;
if(XPRMinit()) /* Initialize Mosel */
return 1;
if(XPRMexecmod(NULL,"burglar3.mos",NULL,&result,&mod))
return 2; /* Execute a model file */
if((XPRMgetprobstat(mod)&XPRM_PBRES)!=XPRM_PBOPT)
return 4; /* Test whether a solution is found */
printf("Objective value: %g\n", XPRMgetobjval(mod));
/* Print the objective function value */
type=XPRMfindident(mod,"take",&rvalue); /* Get the model object 'take' */
if((XPRM_TYP(type)!=XPRM_TYP_MPVAR)|| /* Check the type: */
(XPRM_STR(type)!=XPRM_STR_ARR)) /* it must be an `mpvar' array */
return 5;
varr = rvalue.array;
type=XPRMfindident(mod,"VALUE",&rvalue); /* Get the model object 'VALUE' */
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;
type = XPRMfindident(mod,"ITEMS",&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 7;
set = rvalue.set;
XPRMgetfirstarrentry(varr, indices); /* Get the first entry of array varr
(we know that the array is dense
and has a single dimension) */
do
{
XPRMgetarrval(varr,indices,&x); /* Get a variable from varr */
XPRMgetarrval(darr,indices,&val); /* Get the corresponding value */
printf("take(%s): %g\t (item value: %g)\n", XPRMgetelsetval(set, indices[0],
&itemname)->string, XPRMgetvsol(mod,x), val);
/* Print the solution value */
} while(!XPRMgetnextarrentry(varr, indices)); /* Get the next index */
XPRMresetmod(mod);
return 0;
}
|
|
ugparam1.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugparam1.c
```````````````
Passing parameters to a Mosel program.
Running the BIM file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001
********************************************************/
#include <stdio.h>
#include "xprm_rt.h"
int main()
{
XPRMmodel mod;
XPRMalltypes rvalue, setitem;
XPRMset set;
int result, type, i, size, first, last;
int LIM=500;
char params[128];
if(XPRMinit()) /* Initialize Mosel */
return 1;
sprintf(params, "LIMIT=%d", LIM);
if((mod=XPRMloadmod("prime2.bim", NULL))==NULL)
return 2; /* Load a BIM file */
if(XPRMrunmod(mod, &result, params)) /* Run the model */
return 3;
type=XPRMfindident(mod,"SPrime",&rvalue); /* Get the object 'SPrime' */
if((XPRM_TYP(type)!=XPRM_TYP_INT)|| /* Check the type: */
(XPRM_STR(type)!=XPRM_STR_SET)) /* it must be a set of integers */
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("Prime numbers from 2 to %d:\n", LIM);
for(i=first;i<=last;i++) /* Print all set elements */
printf(" %d,", XPRMgetelsetval(set,i,&setitem)->integer);
printf("\n");
}
return 0;
}
|
|
ugparam2.c |
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugparam2.c
```````````````
Passing parameters to a Mosel program.
Executing a model file.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2002
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
XPRMmodel mod;
XPRMalltypes rvalue, setitem;
XPRMset set;
int result, type, i, size, first, last;
int LIM=500;
char params[128];
if(XPRMinit()) /* Initialize Mosel */
return 1;
sprintf(params, "LIMIT=%d", LIM);
if(XPRMexecmod(NULL,"prime2.mos",params,&result,&mod))
return 2; /* Execute the model */
type=XPRMfindident(mod,"SPrime",&rvalue); /* Get the object 'SPrime' */
if((XPRM_TYP(type)!=XPRM_TYP_INT)|| /* Check the type: */
(XPRM_STR(type)!=XPRM_STR_SET)) /* it must be a set of integers */
return 3;
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("Prime numbers from 2 to %d:\n", LIM);
for(i=first;i<=last;i++) /* Print all set elements */
printf(" %d,",XPRMgetelsetval(set,i,&setitem)->integer);
printf("\n");
}
return 0;
}
|
|
burglar2.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar2.mos
`````````````````
Use of index sets.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. 2006
*******************************************************!)
model Burglar2
uses "mmxprs"
declarations
WTMAX = 102 ! Maximum weight allowed
ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"} ! Index set for items
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
initializations from 'burglar.dat'
VALUE WEIGHT
end-initializations
! Objective: maximize total value
MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)
! Weight restriction
sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX
! All variables are 0/1
forall(i in ITEMS) take(i) is_binary
maximize(MaxVal) ! Solve the MIP-problem
! Print out the solution
writeln("Solution:\n Objective: ", getobjval)
forall(i in ITEMS) writeln(" take(", i, "): ", getsol(take(i)))
end-model
|
|
transport.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file transport.mos
``````````````````
Using dynamic arrays.
(c) 2008 Fair Isaac Corporation
author: S.Heipcke, 2001, rev. June 2018
*******************************************************!)
model Transport
uses "mmxprs"
public declarations
REGION: set of string ! Set of customer regions
PLANT: set of string ! Set of plants
DEMAND: array(REGION) of real ! Demand at regions
PLANTCAP: array(PLANT) of real ! Production capacity at plants
PLANTCOST: array(PLANT) of real ! Unit production cost at plants
TRANSCAP: dynamic array(PLANT,REGION) of real
! Capacity on each route plant->region
DISTANCE: dynamic array(PLANT,REGION) of real
! Distance of each route plant->region
FUELCOST: real ! Fuel cost per unit distance
flow: dynamic array(PLANT,REGION) of mpvar ! Flow on each route
end-declarations
initializations from 'transprt.dat'
DEMAND
[PLANTCAP,PLANTCOST] as 'PLANTDATA'
[DISTANCE,TRANSCAP] as 'ROUTES'
FUELCOST
end-initializations
! Create the flow variables that exist
forall(p in PLANT, r in REGION | exists(TRANSCAP(p,r)) ) create(flow(p,r))
! Objective: minimize total cost
MinCost:= sum(p in PLANT, r in REGION | exists(flow(p,r)))
(FUELCOST * DISTANCE(p,r) + PLANTCOST(p)) * flow(p,r)
! Limits on plant capacity
forall(p in PLANT) sum(r in REGION) flow(p,r) <= PLANTCAP(p)
! Satisfy all demands
forall(r in REGION) sum(p in PLANT) flow(p,r) = DEMAND(r)
! Bounds on flows
forall(p in PLANT, r in REGION | exists(flow(p,r)))
flow(p,r) <= TRANSCAP(p,r)
minimize(MinCost) ! Solve the problem
end-model
|
|
prime2.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file prime2.mos
```````````````
Version of prime.mos without output printing.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. June 2018
*******************************************************!)
model Prime
parameters
LIMIT=100 ! Search for prime numbers in 2..LIMIT
end-parameters
public declarations
SNumbers: set of integer ! Set of numbers to be checked
SPrime: set of integer ! Set of prime numbers
end-declarations
SNumbers:={2..LIMIT}
n:=2
repeat
while (not(n in SNumbers)) n+=1
SPrime += {n} ! n is a prime number
i:=n
while (i<=LIMIT) do ! Remove n and all its multiples
SNumbers-= {i}
i+=n
end-do
until SNumbers={}
end-model
|
|
burglar3.mos |
(!******************************************************
Mosel User Guide Example Problems
=================================
file burglar3.mos
`````````````````
Same as burglar2.mos but no output printing.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2001, rev. June 2018
*******************************************************!)
model Burglar3
uses "mmxprs"
public declarations
WTMAX = 102 ! Maximum weight allowed
ITEMS = {"camera", "necklace", "vase", "picture", "tv", "video",
"chest", "brick"} ! Index set for items
VALUE: array(ITEMS) of real ! Value of items
WEIGHT: array(ITEMS) of real ! Weight of items
take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise
end-declarations
initializations from 'burglar.dat'
VALUE WEIGHT
end-initializations
! Objective: maximize total value
MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)
! Weight restriction
sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX
! All variables are 0/1
forall(i in ITEMS) take(i) is_binary
maximize(MaxVal) ! Solve the MIP-problem
end-model
|
|