Generalized file handling in Mosel
char bimfile_name[64]; /* File name of BIM file */
XPRMinit(); /* Initialize Mosel */
/* Prepare file name for compilation using ’mem’ driver: */
/* "mem:base address/size[/actual size pointer]" */
bimfile_size=0;
sprintf(bimfile_name, "mem:%p/%d/%p",
bimfile, (int)sizeof(bimfile), &bimfile_size);
/* Compile model file to memory */
XPRMcompmod(NULL, "burglar2.mos", bimfile_name, "Knapsack example");
printf("BIM file uses %lu bytes of memory.\n", bimfile_size);
/* Load the BIM file from memory */
mod=XPRMloadmod(bimfile_name, NULL);
XPRMrunmod(mod, &result, NULL); /* Run the model */
return 0;
}
The only change to standard use of the Mosel library functions is the replacement of the
(physical) BIM file by an extended filename in function XPRMcompmod. The extended filename
indicates the driver we want to use (mem), an address where to store the BIM file, and the space
reserved for it. Notice that the size of the resulting BIM file is not known before compilation. In
this example we reserve roughly twice the size of the model file for the BIM file. However, there
is no guarantee that this is sufficient for other models. We therefore print out the size of the
generated file as a control.
We may take working in memory one step further, by including the source of the Mosel model
directly in the C source code. After compilation, we obtain a single all-in-one application file that
does not require or generate any auxiliary file for handling the model.
The source of the model is included in the C source file 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 = {’camera’, ’necklace’, ’vase’, ’picture’, ’tv’, ’video’, \n"
" ’chest’, ’brick’} ! Index set 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::([’camera’, ’necklace’, ’vase’, ’picture’, ’tv’, ’video’,\n"
" ’chest’, ’brick’])[15,100,90,60,40,15,10,1]\n"
"WEIGHT::([’camera’, ’necklace’, ’vase’, ’picture’, ’tv’, ’video’,\n"
" ’chest’, ’brick’])[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"
Exchange of information with embedded models Fair Isaac Corporation Confidential and Proprietary Information 17