Initializing help system before first use

Burglar - In-memory data exchange between Mosel models


Type: Knapsack Problem
Rating: 3 (intermediate)
Description:
  • runburglar.mos, burglar.mos - compile/load/run another model
  • runburglario.mos, burglar2r.mos - data exchange between models in memory using bin format
  • runburglarior.mos, burglar2r.mos - data exchange between models in memory using raw format
  • runburglardistr.mos, burglar2m.mos - data exchange between models in memory on the local host, combining 'bin:', 'rmt:', 'shmem:'
  • runburglardistr2.mos, burglar2m.mos - data exchange between models in memory on the remote Mosel instance, combining 'bin:', 'rmt:', 'shmem:'
  • runburglarmem.mos - compiling a model source from memory
File(s): runburglar.mos, burglar.mos, runburglario.mos, runburglarior.mos, burglar2r.mos (submodel), runburglardistr.mos, runburglardistr2.mos, burglar2m.mos (submodel), runburglarmem.mos

runburglar.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file runburglar.mos
   ```````````````````
   Running a model from another Mosel model.
   Save bim file in Mosel's temporary directory.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2013
*******************************************************!)

model "Run model burglar"
 uses "mmjobs"

 declarations
  modBurg: Model
 end-declarations

                                   ! Compile the model, save bim in temp. dir.
 if compile("", "burglar.mos", "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 

burglar.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file burglar.mos
   ````````````````
   Small MIP problem.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Jan. 2001
*******************************************************!)

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

! Item:      1   2   3   4   5   6   7   8
  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 MIP-problem

! Print out the solution
 writeln("Solution:\n Objective: ", getobjval)
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))
end-model

runburglario.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file runburglario.mos
   `````````````````````
   Running a model from another Mosel model.
   Data exchange in memory using binary format.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2013
*******************************************************!)

model "Run model burglar IO"
 uses "mmjobs", "mmsystem"

 declarations
  modBurg: Model                    ! Submodel
  ISet,SSet: set of string          ! Index set for data arrays
  V,W: array(ISet) of real          ! Data arrays
  Solution: array(SSet) of real     ! Solution values
 end-declarations

 V:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [15, 100, 90, 60, 40, 15, 10,  1]
 W:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [ 2,  20, 20, 30, 40, 30, 60, 10]

                                    ! Compile the model, save bim in memory
 if compile("", "burglar2r.mos", "shmem:burglar.bim")<>0 then exit(1); end-if
 load(modBurg, "shmem:burglar.bim") ! Load the bim file
 fdelete("shmem:burglar.bim")       ! bim file is no longer needed
 
 setdefstream(modBurg, F_OUTPUT, "null:")   ! Disable output from submodel

! Save data in shared memory
 IODRV:="bin:shmem:burgdata"
 initializations to IODRV
  [V,W] as "bdat"
 end-initializations
 
                                    ! Start model execution, setting parameters 
 run(modBurg, "IODRV='" +IODRV + "',DATA='bdat',SOL='Solution'")   
 wait                               ! Wait for model termination
 dropnextevent                      ! Ignore termination event message

! Retrieve solution from shared memory
 initializations from IODRV
  Solution
 end-initializations

 forall(i in SSet)  writeln(" take(", i, "): ", Solution(i))

end-model 

runburglarior.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file runburglarior.mos
   ``````````````````````
   Running a model from another Mosel model.
   Data exchange in memory using binary format using 'raw'.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2013
*******************************************************!)

model "Run model burglar IO"
 uses "mmjobs", "mmsystem"

 declarations
  modBurg: Model                    ! Submodel
  ISet,SSet: set of string          ! Index set for data arrays
  V,W: array(ISet) of real          ! Data arrays
  Solution: array(SSet) of real     ! Solution values
 end-declarations

 V:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [15, 100, 90, 60, 40, 15, 10,  1]
 W:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [ 2,  20, 20, 30, 40, 30, 60, 10]

                                    ! Compile the model, save bim in memory
 if compile("", "burglar2r.mos", "shmem:burglar.bim")<>0 then exit(1); end-if
 load(modBurg, "shmem:burglar.bim") ! Load the bim file
 fdelete("shmem:burglar.bim")       ! bim file is no longer needed
 
 setdefstream(modBurg, F_OUTPUT, "null:")   ! Disable output from submodel

! Save data in shared memory
 IODRV:="raw:"
 initializations to IODRV
  [V,W] as "shmem:bdat"
 end-initializations
 
                                    ! Start model execution, setting parameters 
 run(modBurg, "IODRV='" +IODRV + "',DATA='shmem:bdat',SOL='shmem:Solution'")   
 wait                               ! Wait for model termination
 dropnextevent                      ! Ignore termination event message

! Retrieve solution from shared memory
 initializations from IODRV
  Solution as "shmem:Solution"
 end-initializations

 forall(i in SSet)  writeln(" take(", i, "): ", Solution(i))

end-model 

burglar2r.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2r.mos
   ``````````````````
   Use of I/O drivers for data handling.

   *** Not intended to be run standalone
       - run from iodrvraw2.c or runburglario*.mos ***
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2004, rev. Sep. 2018
*******************************************************!)

model Burglar2r
 uses 'mmxprs'

 parameters
  IODRV='raw:'
  DATA='BurgData'
  SOL='SolTake'
  WTMAX = 102                    ! Maximum weight allowed
 end-parameters

 declarations
  public ITEMS: set of string    ! Index set for items
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  SOLTAKE: array(ITEMS) of real  ! Solution values
 end-declarations

 initialisations from IODRV
  [VALUE,WEIGHT] as DATA
 end-initialisations

 declarations
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

! 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

! Solution output
 forall(i in ITEMS) SOLTAKE(i):= getsol(take(i))

 writeln("Solution:\n Objective: ", getobjval)
 writeln(SOLTAKE) 

 initialisations to IODRV
  SOLTAKE as SOL
 end-initialisations

end-model

runburglardistr.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file runburglardistr.mos
   `````````````````````
   Running a remote model from another Mosel model.
   Data exchange using binary format.
   Information saved in memory on the local host.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2013
*******************************************************!)

model "Run model burglar remotely IO"
 uses "mmjobs", "mmsystem"

 declarations
  modBurg: Model                    ! Submodel
  moselInst: Mosel                  ! Mosel instance
  ISet,SSet: set of string          ! Index set for data arrays
  V,W: array(ISet) of real ! Data arrays
  SolTake: array(SSet) of real      ! Solution values
 end-declarations

 V:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [15, 100, 90, 60, 40, 15, 10,  1]
 W:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [ 2,  20, 20, 30, 40, 30, 60, 10]

!!! Select the (remote) machines to be used:
!!! Use names, IP addresses, or empty string for the node running this model
 MOSINST:= ""
                                    ! Connect to a remote instance
 if connect(moselInst, MOSINST)<>0 then exit(1); end-if
                                    ! Compile the model remotely
 if compile(moselInst, "", "rmt:burglar2m.mos", "shmem:burglar.bim")<>0 then 
  exit(2); end-if
 load(moselInst, modBurg, "shmem:burglar.bim") ! Load the bim file
 fdelete("rmt:["+getnode(moselInst)+"]shmem:burglar.bim")   ! bim file is no longer needed
 
! setdefstream(modBurg, F_OUTPUT, "null:")     ! Disable output from submodel

! Save data in shared memory
 initializations to "bin:shmem:burgdata"
  [V, W] as "BurgData"
 end-initializations 
                                    ! Start model execution, setting parameters 
 run(modBurg, "DATA='bin:rmt:shmem:burgdata',SOL='bin:rmt:shmem:burgsol'")   
 wait                               ! Wait for model termination
 dropnextevent                      ! Ignore termination event message

! Retrieve solution from shared memory
 initializations from "bin:shmem:burgsol"
  SolTake
 end-initializations

 forall(i in SSet)  writeln(" take(", i, "): ", SolTake(i))

end-model 

runburglardistr2.mos
(!*******************************************************
   Mosel Example Problems 
   ======================

   file runburglardistr.mos
   `````````````````````
   Running a remote model from another Mosel model.
   Data exchange using binary format.
   Information saved in memory on the remote host.
       
   (c) 2013 Fair Isaac Corporation
       author: S. Heipcke, Apr. 2013
*******************************************************!)

model "Run model burglar remotely IO"
 uses "mmjobs", "mmsystem"

 declarations
  modBurg: Model                    ! Submodel
  moselInst: Mosel                  ! Mosel instance
  ISet,SSet: set of string          ! Index set for data arrays
  V,W: array(ISet) of real ! Data arrays
  SolTake: array(SSet) of real      ! Solution values
 end-declarations

 V:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [15, 100, 90, 60, 40, 15, 10,  1]
 W:: (["camera","necklace","vase","picture","tv","video","chest","brick"])
      [ 2,  20, 20, 30, 40, 30, 60, 10]

!!! Select the (remote) machines to be used:
!!! Use names, IP addresses, or empty string for the node running this model
 MOSINST:= ""
                                    ! Connect to a remote instance
 if connect(moselInst, MOSINST)<>0 then exit(1); end-if
 instnum:= getid(moselInst)
                                    ! Compile the model remotely
 if compile(moselInst, "", "rmt:burglar2m.mos", "shmem:burglar.bim")<>0 then 
  exit(2); end-if
 load(moselInst, modBurg, "shmem:burglar.bim") ! Load the bim file
 fdelete("rmt:["+getnode(moselInst)+"]shmem:burglar.bim")   ! bim file is no longer needed
 
! setdefstream(modBurg, F_OUTPUT, "null:")     ! Disable output from submodel

! Save data in shared memory
 initializations to "bin:rmt:["+instnum+"]shmem:burgdata"
  [V, W] as "BurgData"
 end-initializations 
                                    ! Start model execution, setting parameters 
 run(modBurg, "DATA='bin:shmem:burgdata',SOL='bin:shmem:burgsol'")   
 wait                               ! Wait for model termination
 dropnextevent                      ! Ignore termination event message

! Retrieve solution from shared memory
 initializations from "bin:rmt:["+instnum+"]shmem:burgsol"
  SolTake
 end-initializations

 forall(i in SSet)  writeln(" take(", i, "): ", SolTake(i))

end-model 

burglar2m.mos
(!******************************************************
   Mosel Example Problems
   ====================== 

   file burglar2m.mos
   ``````````````````
   Use of I/O drivers for data handling.

   *** Not intended to be run standalone - run from burgbindata.*  ***
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2004,rev. Apr. 2013
*******************************************************!)

model Burglar2r
 uses 'mmxprs'

 parameters
  DATA='bin:burgdatabin'
  SOL='bin:resdatabin'
  WTMAX = 102                    ! Maximum weight allowed
 end-parameters

 declarations
  ITEMS: set of string           ! Index set for items
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
  SOLTAKE: array(ITEMS) of real  ! Solution values
 end-declarations

 initialisations from DATA
  [VALUE, WEIGHT] as "BurgData"
 end-initialisations

 declarations
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

! 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

! Solution output
 forall(i in ITEMS) SOLTAKE(i):= getsol(take(i))

 writeln("Solution:\n Objective: ", getobjval)
 writeln(SOLTAKE) 

 initialisations to SOL
  SOLTAKE as "SolTake"
  evaluation of getobjval as "Objective"
 end-initialisations

end-model

runburglarmem.mos
(!******************************************************
   Mosel User Guide Example Problems
   ================================= 

   file runburglarmem.mos
   ``````````````````````
   Compilation from memory.
   
   (c) 2014 Fair Isaac Corporation
       author: S. Heipcke, Sep. 2014
*******************************************************!)

model "Compile burglar.mos from memory"
 uses "mmjobs", "mmsystem"
 
 parameters
   IDXCONFIG = 1         ! 1: range, 2: set of string
 end-parameters

!**** The source of the submodel as multi-line strings ****
 public declarations
  source_of_model: text
  model_header, data1, data2, math_model: text
 end-declarations 
  
 model_header:= 
`--------------- Model start (header and options) ----------------
 model Burglar
 uses 'mmxprs'
--------------- Model start (header and options) ----------------`


 data1:= 
`---------------- Data set using integer indices -----------------
 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
 end-declarations

 VALUE :: [15, 100, 90, 60, 40, 15, 10,  1]
 WEIGHT:: [ 2,  20, 20, 30, 40, 30, 60, 10]
---------------- Data set using integer indices -----------------`


 data2:= 
`---------------- Data set using string indices -----------------
 declarations
  WTMAX = 102                    ! Maximum weight allowed
  ITEMS: set of string           ! Index set for items
  VALUE: array(ITEMS) of real    ! Value of items
  WEIGHT: array(ITEMS) of real   ! Weight of items
 end-declarations

 VALUE :: (["camera", "necklace", "vase", "picture", "tv", "video", 
           "chest", "brick"])[15, 100, 90, 60, 40, 15, 10,  1]
 WEIGHT:: (["camera", "necklace", "vase", "picture", "tv", "video", 
           "chest", "brick"])[ 2,  20, 20, 30, 40, 30, 60, 10]
---------------- Data set using string indices -----------------`


 math_model:= 
`-------------- Definition of the mathematical model --------------
 declarations
  take: array(ITEMS) of mpvar    ! 1 if we take item i; 0 otherwise
 end-declarations

 ! 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
-------------- Definition of the mathematical model --------------`


!**** Compose the parts to form the model source ****
 source_of_model:= model_header +
   if(IDXCONFIG=1, data1, data2) + math_model
 

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