Initializing help system before first use

Compilation to/from memory


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

ugcompfrmemcs.cs
/********************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugcompfrmem.cs
   ```````````````````
   Retrieve model output via callback-style functionality.
   
   (c) 2013 Fair Isaac Corporation
       author: S.Heipcke, Mar. 2013 
********************************************************/


using System;
using System.IO;
using Mosel;


namespace ugcompfrmem.cs {

  public class ugcompfrmem {
    /// <summary>
    /// String containing the model
    /// </summary>
    const string modelSource=
  "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"; 
    
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static void Main(string[] args) {
      // Initialize Mosel
      XPRM mosel = XPRM.Init();

      // Compile the Mosel model to a physical file
      FileStream file= new FileStream("burglar2.bim", FileMode.Create,  
        FileAccess.Write);
      mosel.Compile("", new StringReader(modelSource), file); 
      file.Close();

      // Load the Mosel model
      XPRMModel model = mosel.LoadModel("burglar2.bim");

// Alternative version: compile+load without writing the BIM
//      XPRMModel model = mosel.CompileAndLoad(new StringReader(modelSource));
          
      // Run the model
      model.Run();
    }
  }

}

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


!**** Master 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 



ugcompmemcs.cs
/********************************************************
   Mosel User Guide Example Problems
   ================================= 

   file ugcompmem.cs
   `````````````````
   Compiling a model to memory.
   
   (c) 2013 Fair Isaac Corporation
       author: S.Heipcke, Mar. 2013 
               J.Farmer, Jul. 2019
********************************************************/


using System;
using System.IO;
using Mosel;


namespace ugcompmem.cs {

  public class ugcompmem {
    /// <summary>
    /// Main entry point for the application
    /// </summary>
    [STAThread]
    static void Main(string[] args) {
      // Initialize Mosel
      XPRM mosel = XPRM.Init();

      // Compile the Mosel model to a MemoryStream
      MemoryStream bimStream = new MemoryStream();
      mosel.Compile("","burglar2.mos",bimStream);

      // Reset stream pointer to beginning so we can read the bytes we've just written
      bimStream.Seek(0,SeekOrigin.Begin);

      // Load the Mosel model
      XPRMModel model;
      mosel.Bind("bimblk", bimStream);
      try {
        model = mosel.LoadModel("dotnet:bimblk");
      } finally {
        mosel.Unbind("bimblk");
      }
      
      // Run the model
      model.Run();
    }
  }

}

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