Initializing help system before first use

Compilation to/from memory


Type: Programming
Rating: 3 (intermediate)
Description:
  • ugcompfrmem.mos, ugcompfrmem.java: Compiling a model held in memory
  • ugcompmem.mos, ugcompmem.java: Compiling a model to memory (requires burglar2.mos, burglar.dat)
File(s): ugcompfrmem.java, ugcompfrmem.mos, ugcompmem.java, ugcompmem.mos, burglar2.mos
Data file(s): burglar.dat


ugcompfrmem.java
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugcompfrmem.java
   `````````````````````
   Compiling a model from memory.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2006
********************************************************/

import java.nio.*;
import com.dashoptimization.*;

public class ugcompfrmem
{
                // The source of the model as a string
 static final String 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";


 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  ByteBuffer mosfile;                   // Buffer to store source file

  mosel = new XPRM();                   // Initialize Mosel

                                        // Prepare file names for compilation:
                                        // Wrap source in a byte buffer
  mosfile=ByteBuffer.wrap(source_of_model.getBytes());
  mosel.bind("modelfile", mosfile);     // Associate Java object with a name 
                                        // in Mosel
                                        
  try
  {                                     // Compile model from memory
   mosel.compile("", "java:modelfile", "burglar.bim", "");
  }
  catch(XPRMCompileException e)
  {
   System.out.println(e.getMessage());
  }
  mosel.unbind("mosfile");              // Release memory
  mosfile=null;
                                      
  mod=mosel.loadModel("burglar.bim");   // Load BIM file
  mod.run();                            // Run the model
 }
}

ugcompfrmem.mos
(!******************************************************
   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 



ugcompmem.java
/*******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugcompmem.java
   ```````````````````
   Compiling a model to memory.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2006
********************************************************/

import java.nio.*;
import com.dashoptimization.*;

public class ugcompmem
{
 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  ByteBuffer bimfile;                   // Buffer to store BIM file

  mosel = new XPRM();                   // Initialize Mosel

                                        // Prepare file names for compilation
  bimfile=ByteBuffer.allocateDirect(2048);   // Create a 2K byte buffer
  mosel.bind("mybim", bimfile);         // Associate Java obj. with Mosel name
                                        
  try
  {                                     // Compile model to memory
   mosel.compile("", "burglar2.mos", "java:mybim", "");
  }
  catch(XPRMCompileException e)
  {
   System.out.println(e.getMessage());
  }
 
  bimfile.limit(bimfile.position());    // Mark end of data in the buffer
  bimfile.rewind();                     // Back to the beginning
  System.out.println("BIM file uses "+bimfile.limit()+" bytes of memory.");
                                     
  mod=mosel.loadModel("java:mybim");    // Load a BIM file from memory
  mosel.unbind("mybim");                // Release memory
  bimfile=null;

  mod.run();                            // Run the model
 }
}

ugcompmem.mos
(!******************************************************
   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 



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

© 2001-2025 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.