Basic tasks
To work with a Mosel model, in the C language or with the command line interpreter, it always needs to be compiled, then loaded into Mosel and executed. In this section we show how to perform these basic tasks in C.
Compiling a model in C
The following example program shows how Mosel is initialized in C, and how a model file (extension .mos) is compiled into a binary model (BIM) file (extension .bim). To use the Mosel Model Compiler Library, we need to include the header file xprm_mc.h at the start of the C program.
For the sake of readability, in this program (file ugcomp.c), as for all others in this chapter, we only implement a rudimentary testing for errors.
#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 */ XPRMfinish(); /* Finish Mosel, clear everything */ return 0; }
The model burglar2.mos used here is the same as model burglari.mos in Section The burglar problem revisited, but reading the data from file.
With version 1.4 of Mosel it becomes possible to redirect the BIM file that is generated by the compilation. Instead of writing it out to a physical file it may, for instance, be kept in memory or be written out in compressed format. The interested reader is refered to the whitepaper Generalized file handling in Mosel.
Executing a model in C
The example in this section shows how a Mosel binary model file (BIM) can be executed in C. The BIM file can of course be generated within the same program where it is executed, but here we leave out this step. A BIM file is an executable version of a model, but it does not include any data that is read in by the model from external files. It is portable, that is, it may be executed on a different type of architecture than the one it has been generated on. A BIM file produced by the Mosel compiler first needs to be loaded into Mosel (function XPRMloadmod) and can then be run by a call to function XPRMrunmod. To use these functions, we need to include the header file xprm_rt.h at the beginning of our program (named ugrun.c).
#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; XPRMfinish(); /* Finish Mosel, clear everything */ return 0; }
The compile/load/run sequence may also be performed with a single function call to XPRMexecmod (in this case we need to include the header file xprm_mc.h):
#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; XPRMfinish(); /* Finish Mosel, clear everything */ return 0; }
Termination
All program examples in this manual only serve to execute Mosel models. The corresponding model and Mosel itself are terminated (unloaded from memory) with the end of the C program. However, for embedding the execution of a Mosel model into some larger application it may be desirable to free the space used by the model or the execution of Mosel before the end of the application program. To this aim Mosel provides the functions XPRMresetmod, XPRMunloadmod, and XPRMfinish.
The function XPRMresetmod frees some resources allocated to a model, in particular (solution) data held in memory or temporary files that may have been created during its execution. The model remains loaded for later re-use. With a call to XPRMunloadmod a model is unloaded and all related resources are freed.
Function XPRMfinish performs the unloading of all models, frees all memory used by Mosel, and also removes the temporary directory/files that have been created by Mosel.
© 2001-2019 Fair Isaac Corporation. All rights reserved. This documentation is the property of Fair Isaac Corporation (“FICO”). Receipt or possession of this documentation does not convey rights to disclose, reproduce, make derivative works, use, or allow others to use it except solely for internal evaluation purposes to determine whether to purchase a license to the software described in this documentation, or as otherwise set forth in a written software license agreement between you and FICO (or a FICO affiliate). Use of this documentation and the software described in it must conform strictly to the foregoing permitted uses, and no other use is permitted.