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, ugcompfrmemcs.csproj, ugcompfrmem.mos, ugcompmemcs.cs, ugcompmemcs.csproj, 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 
               J.Farmer, Mar. 2021
********************************************************/


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();
    }
  }

}

ugcompfrmemcs.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" /> 
  </ItemGroup>
  

</Project>

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 



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, rev. May. 2021
********************************************************/


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();
      // Set Mosel work directory to folder containing our example files
      mosel.WorkDir = Directory.GetParent(System.Reflection.Assembly.GetExecutingAssembly().Location).FullName;

      // Compile the Mosel model to a MemoryStream
      MemoryStream bimStream = new MemoryStream();
      mosel.Compile("",mosel.WorkDir+"/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();
    }
  }

}

ugcompmemcs.csproj
<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>net5.0</TargetFramework>

    <IsPackable>false</IsPackable>
  </PropertyGroup>

  <ItemGroup>
    <Content Include="../burglar.dat" CopyToOutputDirectory="Always" />
    <Content Include="../burglar2.mos" CopyToOutputDirectory="Always" />
  </ItemGroup>
  
  <ItemGroup>
    <PackageReference Include="FICO.Xpress.XPRMdn" Version="5.6.1" /> 
  </ItemGroup>
  

</Project>

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-2021 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.