/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcompfrmem.c
``````````````````
Compilation from memory.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2006, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
/**** The source of the model as an array of characters ****/
const char source_of_model[]=
"model Burglar\n"
"uses 'mmxprs'\n"
"declarations\n"
" WTMAX = 102 ! Maximum weight allowed\n"
" ITEMS = 1..8 ! Index range for items\n"
" VALUE: array(ITEMS) of real ! Value of items\n"
" WEIGHT: array(ITEMS) of real ! Weight of items\n"
" take: array(ITEMS) of mpvar ! 1 if we take item i; 0 otherwise\n"
"end-declarations\n"
"VALUE :: [15, 100, 90, 60, 40, 15, 10, 1]\n"
"WEIGHT:: [ 2, 20, 20, 30, 40, 30, 60, 10]\n"
"! Objective: maximize total value\n"
"MaxVal:= sum(i in ITEMS) VALUE(i)*take(i)\n"
"! Weight restriction\n"
"sum(i in ITEMS) WEIGHT(i)*take(i) <= WTMAX\n"
"! All variables are 0/1\n"
"forall(i in ITEMS) take(i) is_binary\n"
"maximize(MaxVal) ! Solve the problem\n"
"! Print out the solution\n"
"writeln(\"Solution:\\n Objective: \", getobjval)\n"
"forall(i in ITEMS) writeln(' take(', i, '): ', getsol(take(i)))\n"
"end-model";
/**** Main function ****/
int main()
{
XPRMmodel mod;
int result;
char mosfile_name[40]; /* File name of MOS file */
if(XPRMinit()) /* Initialize Mosel */
return 1;
/* Prepare file name for compilation using 'mem' driver: */
/* "mem:base_address/size[/actual_size_of_pointer]" */
sprintf(mosfile_name, "mem:%p/%d",
source_of_model, (int)sizeof(source_of_model));
/* Compile model file from memory */
if(XPRMcompmod(NULL, mosfile_name, "burglar.bim", "Knapsack example"))
return 2;
/* Load BIM file */
if((mod=XPRMloadmod("burglar.bim", NULL))==NULL)
return 3;
if(XPRMrunmod(mod, &result, NULL)) /* Run the model */
return 4;
/* Alternative to compile/load/run sequence: execute the model */
/* if(XPRMexecmod(NULL, mosfile_name, NULL, &result, &mod)) return 2; */
XPRMresetmod(mod);
return 0;
}
|
(!******************************************************
Mosel User Guide Example Problems
=================================
file ugcompfrmem.mos
````````````````````
Compilation from memory.
(c) 2014 Fair Isaac Corporation
author: S. Heipcke, Sep. 2014
*******************************************************!)
model "Compile burglar.mos from memory"
uses "mmjobs", "mmsystem"
!**** The source of the submodel as a multi-line string ****
public declarations
source_of_model=`SUBMODELSOURCE
model Burglar
uses 'mmxprs'
declarations
WTMAX = 102 ! Maximum weight allowed
ITEMS = 1..8 ! Index range 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
VALUE :: [15, 100, 90, 60, 40, 15, 10, 1]
WEIGHT:: [ 2, 20, 20, 30, 40, 30, 60, 10]
! 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 problem
! Print out the solution
writeln("Solution:\n Objective: ", getobjval)
forall(i in ITEMS) writeln(' take(', i, '): ', getsol(take(i)))
end-model
SUBMODELSOURCE`
end-declarations
!**** Main model ****
declarations
modBurg: Model
end-declarations
! Compile the model from memory
if compile("", "text:source_of_model", "tmp:burglar.bim")<>0 then
exit(1)
end-if
load(modBurg, "tmp:burglar.bim") ! Load the bim file
run(modBurg) ! Start model execution
wait ! Wait for model termination
dropnextevent ! Ignore termination event message
end-model
|
/*******************************************************
Mosel User Guide Example Problems
=================================
file ugcompmem.c
````````````````
Compilation to memory.
(c) 2008 Fair Isaac Corporation
author: S. Heipcke, 2006, rev. Feb. 2017
********************************************************/
#include <stdio.h>
#include "xprm_mc.h"
int main()
{
XPRMmodel mod;
int result;
char bimfile[2000]; /* Buffer to store BIM file */
size_t bimfile_size; /* Buffer to store actual size of BIM file */
char bimfile_name[64]; /* File name of BIM file */
if(XPRMinit()) /* Initialize Mosel */
return 1;
/* Prepare file name for compilation using 'mem' driver: */
/* "mem:base_address/size[/actual_size_of_pointer]" */
bimfile_size=0;
sprintf(bimfile_name, "mem:%p/%d/%p",
bimfile, (int)sizeof(bimfile), &bimfile_size);
/* Compile model file to memory */
if(XPRMcompmod(NULL, "burglar2.mos", bimfile_name, "Knapsack example"))
return 2;
printf("BIM file uses %lu bytes of memory.\n", (unsigned long)bimfile_size);
/* Load a BIM file from memory */
if((mod=XPRMloadmod(bimfile_name, NULL))==NULL)
return 3;
if(XPRMrunmod(mod, &result, NULL)) /* Run the model */
return 4;
XPRMresetmod(mod);
return 0;
}
|
(!******************************************************
Mosel User Guide Example Problems
=================================
file ugcompmem.mos
``````````````````
Compilation a model to memory.
(c) 2014 Fair Isaac Corporation
author: S. Heipcke, Sep. 2014
*******************************************************!)
model "Compile burglar.mos to memory"
uses "mmjobs", "mmsystem"
declarations
modBurg: Model
end-declarations
! Compile the model to memory
if compile("", "burglar2.mos", "mem:bimfile")<>0 then exit(1); end-if
load(modBurg, "mem:bimfile") ! Load the bim file
fdelete("mem:bimfile")
run(modBurg) ! Start model execution
wait ! Wait for model termination
dropnextevent ! Ignore termination event message
end-model
|
(!******************************************************
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
|