Initializing help system before first use

In-memory data exchange


Type: Embedding
Rating: 3
Description:
  • ugiocb.java: Exchanging data between model and host application. Callbacks for exchanging data: sparse data, string indices (requires burglar13.mos)
  • ugiodense.java: Exchanging data between model and host application. Dense data (requires burglar8.mos)
  • ugioscalar.java: Exchanging data between model and host application. Scalars (requires burglar11.mos)
  • ugiosparse.java: Exchanging data between model and host application. Sparse data, string indices (requires burglar9.mos)
  • ugstreamdense.java: Exchanging data between model and host application using Java streams. Dense data (requires burglar8s.mos)
  • ugstreamsparse.java: Exchanging data between model and host application using Java streams. Sparse data, string indices (requires burglar9s.mos)
File(s): ugiocb.java, ugiodense.java, ugioscalar.java, ugiosparse.java, ugstreamdense.java, ugstreamsparse.java, burglar13.mos, burglar11.mos, burglar8.mos, burglar8s.mos, burglar9.mos, burglar9s.mos

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

   file ugiocb.java
   ````````````````
   Exchanging data between model and host application.
   - Callbacks for exchanging data (sparse data, string indices) -
   
   (c) 2009 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2009
********************************************************/

import com.dashoptimization.*;

public class ugiocb
{                                   // Input data
 static final double[] vdata={15,100,90,60,40,15,10, 1};   // VALUE
 static final double[] wdata={ 2, 20,20,30,40,30,60,10};   // WEIGHT
 static final String[] ind={"camera", "necklace", "vase", "picture", 
     "tv", "video", "chest", "brick"};                     // Index names
 static final int datasize=8;

                        // Class to receive solution values
 public static class MySol
 {
  public String ind;                // index name
  public double val;                // solution value
 }
 static MySol[] solution;
 static int solsize;

                /*************************************************/
                /* A class to initialize model data via callback */
                /*************************************************/
 public static class modelInit implements XPRMInitializationFrom, XPRMInitializationTo
 {
  public boolean initializeFrom(XPRMInitializeContext ictx, String label, XPRMTyped type)
  {
   try
   {
    if(label.equals("DATA"))
    {
     ictx.sendControl(ictx.CONTROL_OPENLST);
     for(int i=0;i

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

   file ugiodense.java
   ````````````````````
   Exchanging data between model and host application.
   - Dense data -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2006
********************************************************/

import com.dashoptimization.*;

public class ugiodense
{                                     // Input data
 static final double[] vdata={15,100,90,60,40,15,10, 1};   // VALUE
 static final double[] wdata={ 2, 20,20,30,40,30,60,10};   // WEIGHT

                                      // Array to receive solution values
 static double[] solution = new double[8];  

 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;

  mosel = new XPRM();                 // Initialize Mosel

  mosel.compile("burglar8.mos");      // Compile & load the model
  mod = mosel.loadModel("burglar8.bim");

                        // Associate the Java objects with names in Mosel
  mosel.bind("vdat", vdata);
  mosel.bind("wdat", wdata);
  mosel.bind("sol", solution);
                        // File names are passed through execution parameters
  mod.execParams =
   "VDATA='noindex,vdat',WDATA='noindex,wdat',SOL='noindex,sol'";

  mod.run();                          // Run the model

  if(mod.getProblemStatus()!=mod.PB_OPTIMAL) 
   System.exit(1);                    // Stop if no solution found

                        // Display solution values obtained from the model
  System.out.println("Objective value: " + mod.getObjectiveValue());
  for(int i=0;i<8;i++)
   System.out.println(" take(" + (i+1) + "): " + solution[i]);

  mod.reset();                        // Reset the model
 }
}


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

   file ugioscalar.java
   ````````````````````
   Exchanging data between model and host application.
   - Scalars -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2008
********************************************************/

import com.dashoptimization.*;

public class ugioscalar
{
 public static class MyData            // Scalars for data in/output
 {
  public int wmax;
  public int numitem;
  public double objval;
 }
 
 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  MyData data=new MyData();

  data.wmax=100;

  mosel = new XPRM();                 // Initialize Mosel

  mosel.compile("burglar11.mos");     // Compile & load the model
  mod = mosel.loadModel("burglar11.bim");

                        // Associate the Java object with a name in Mosel
  mosel.bind("data", data);
                        // File names are passed through execution parameters
  mod.execParams =
   "WMAX='data(wmax)',NUM='data(numitem)',SOLVAL='data(objval)'";

  mod.run();                          // Run the model

  if(mod.getProblemStatus()!=mod.PB_OPTIMAL) 
   System.exit(1);                    // Stop if no solution found

                        // Display solution values obtained from the model
  System.out.println("Objective value: " + data.objval);
  System.out.println("Total number of items: " + data.numitem);

  mod.reset();                        // Reset the model
 }
}


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

   file ugiosparse.java
   ````````````````````
   Exchanging data between model and host application.
   - Sparse data (string indices) -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, 2006
********************************************************/

import com.dashoptimization.*;

public class ugiosparse
{
                        // Class to store initial values for array 'data'
 public static class MyData
 {
  public String ind;                // index name
  public double val,wght;           // value and weight data entries
  MyData(String i, double v, double w)
  { ind=i; val=v; wght=w; }
 }
                        // Class to receive solution values
 public static class MySol
 {
  public String ind;                // index name
  public double val;                // solution value
 }

 public static void main(String[] args) throws Exception
 {
  XPRM mosel;
  XPRMModel mod;
  MyData data[]={new MyData("camera",15,2), new MyData("necklace",100,20), 
                 new MyData("vase",90,20), new MyData("picture",60,30), 
		 new MyData("tv",40,40), new MyData("video",15,30), 
                 new MyData("chest",10,60), new MyData("brick",1,10)};
  MySol[] solution=new MySol[8];
  
  for(int i=0;i<8;i++) solution[i] = new MySol();

  mosel = new XPRM();                 // Initialize Mosel

  mosel.compile("burglar9.mos");      // Compile & load the model
  mod = mosel.loadModel("burglar9.bim");

                        // Associate the Java objects with names in Mosel
  mosel.bind("dt", data);
  mosel.bind("sol", solution);
                        // File names are passed through execution parameters
  mod.execParams = "DATA='dt(ind,val,wght)',SOL='sol(ind,val)'";

  mod.run();                          // Run the model

  if(mod.getProblemStatus()!=mod.PB_OPTIMAL) 
   System.exit(1);                    // Stop if no solution found

                        // Display solution values obtained from the model
  System.out.println("Objective value: " + mod.getObjectiveValue());
  for(int i=0;i<8;i++)
   System.out.println(" take(" + solution[i].ind + "): " + solution[i].val);

  mod.reset();                        // Reset the model
 }
}


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

   file ugstreamdense.java
   ```````````````````````
   Exchanging data between model and host application
   using Java streams.
   - Dense data -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, July 2008
********************************************************/

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

public class ugstreamdense
{                                     // Input data
 static final double[] vdata={15,100,90,60,40,15,10, 1};   // VALUE
 static final double[] wdata={ 2, 20,20,30,40,30,60,10};   // WEIGHT

                                      // Array to receive solution values
 static final int SOLSIZE=8;
 static double[] solution = new double[SOLSIZE];  

 // A stream to send the 'MyData' array in a form suitable for the 'raw:' driver
 public static class MyInitStream extends InputStream
 {
  int ndx;
  double mydata[];

  MyInitStream(double data[])
  {
   ndx=0;
   mydata=data;
  }

  public int read(byte[] b)
  {
   ByteBuffer buf;
   byte [] bs;

   if(ndx

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

   file ugstreamsparse.java
   ````````````````````````
   Exchanging data between model and host application
   using Java streams.
   - Sparse data (string indices) -
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, July 2008
********************************************************/

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

public class ugstreamsparse
{
                        // Class to store initial values for array 'data'
 public static class MyData
 {
  public String ind;                // index name
  public double val,wght;           // value and weight data entries
  MyData(String i, double v, double w)
  { ind=i; val=v; wght=w; }
 }
                        // Class to receive solution values
 public static class MySol
 {
  public String ind;                // index name
  public double val;                // solution value
 }

 // A stream to send the 'MyData' array in a form suitable for the 'raw:' driver
 public static class MyInitStream extends InputStream
 {
  int ndx;
  MyData mydata[];

  MyInitStream(MyData data[])
  {
   ndx=0;
   mydata=data;
  }

  public int read(byte[] b)
  {
   ByteBuffer buf;
   byte [] bs;

   if(ndx

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

   file burglar13.mos
   ``````````````````
   Same as burglar2.mos, with input from/output to
   calling application.
   
   (c) 2009 Fair Isaac Corporation
       author: S. Heipcke, Nov. 2009
*******************************************************!)

model Burglar13
 uses "mmxprs"
 
 parameters
  DATAFILE = ''                    ! Location of input data
  SOLFILE = ''                     ! Location for solution data output
  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

 initializations from DATAFILE
  [VALUE,WEIGHT] as "DATA"
 end-initializations

 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 MIP-problem

! Output solution to calling application
 forall(i in ITEMS) soltake(i):= getsol(take(i))

 initializations to SOLFILE
  soltake as "SOL"
 end-initializations

end-model

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

   file burglar11.mos
   ``````````````````
   Same as burglari.mos, with some scalars 
   input from/output to calling application.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2008, rev. Apr. 2013
*******************************************************!)

model Burglar11
 uses "mmxprs"
 
 parameters
  NUM = ''                         ! Location for no. of items output
  SOLVAL = ''                      ! Location for objective value output
  WMAX = ''                        ! Maximum weight allowed
  IODRV = 'jraw:'
 end-parameters

 declarations
  WTMAX: integer                   ! 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
  soltake: array(ITEMS) of real    ! Solution values
 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]

 initializations from IODRV
  WTMAX as WMAX
 end-initializations

 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 MIP-problem

! Print out the solution
 writeln("Solution:")
 forall(i in ITEMS)  writeln(" take(", i, "): ", getsol(take(i)))

! Output solution to calling application
 initializations to IODRV
  evaluation of getobjval as SOLVAL
  evaluation of round(sum(i in ITEMS) getsol(take(i))) as NUM
 end-initializations

end-model

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

   file burglar8.mos
   `````````````````
   Model as in burglar.mos, with data input/ouput
   from/to calling application.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2006
*******************************************************!)

model Burglar8
 uses "mmxprs"

 parameters
  VDATA = ''; WDATA = ''         ! Location of input data
  SOL = ''                       ! Location for solution data output
  WTMAX = 102                    ! Maximum weight allowed
 end-parameters
  
 declarations
  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
  soltake: array(ITEMS) of real  ! Solution values
 end-declarations

 initializations from 'jraw:'
  VALUE as VDATA  WEIGHT as WDATA
 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

! Output solution to calling application
 forall(i in ITEMS) soltake(i):= getsol(take(i))

 initializations to 'jraw:'
  soltake as SOL
 end-initializations

end-model

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

   file burglar8s.mos
   ``````````````````
   Model as in burglar.mos, with data input/ouput
   from/to calling application through Java streams.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, July 2008
*******************************************************!)

model Burglar8
 uses "mmxprs"

 parameters
  VDATA = ''; WDATA = ''         ! Location of input data
  SOL = ''                       ! Location for solution data output
  WTMAX = 102                    ! Maximum weight allowed
 end-parameters
  
 declarations
  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
  soltake: array(ITEMS) of real  ! Solution values
 end-declarations

 initializations from 'raw:noalign,noindex'
  VALUE as "java:"+VDATA  WEIGHT as "java:"+WDATA
 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

! Output solution to calling application
 forall(i in ITEMS) soltake(i):= getsol(take(i))

 initializations to 'raw:noalign,noindex'
  soltake as "java:"+SOL
 end-initializations

end-model

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

   file burglar9.mos
   `````````````````
   Same as burglar2.mos, with input from/output to
   calling application.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, Mar. 2006
*******************************************************!)

model Burglar9
 uses "mmxprs"
 
 parameters
  DATA = ''                        ! Location of input data
  SOL = ''                         ! Location for solution data output
  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

 initializations from 'jraw:'
  [VALUE,WEIGHT] as DATA
 end-initializations

 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 MIP-problem

! Output solution to calling application
 forall(i in ITEMS) soltake(i):= getsol(take(i))

 initializations to 'jraw:'
  soltake as SOL
 end-initializations

end-model

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

   file burglar9s.mos
   ``````````````````
   Same as burglar2.mos, with input from/output to
   calling application through Java streams.
   
   (c) 2008 Fair Isaac Corporation
       author: S. Heipcke, July 2008
*******************************************************!)

model Burglar9s
 uses "mmxprs"
 
 parameters
  DATA = ''                        ! Name of input data stream
  SOL = ''                         ! Name of solution data output stream
  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

 initializations from 'raw:noalign'
  [VALUE,WEIGHT] as "java:"+DATA
 end-initializations

 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 MIP-problem

! Output solution to calling application
 forall(i in ITEMS) soltake(i):= getsol(take(i))

 initializations to 'raw:noalign'
  soltake as "java:"+SOL
 end-initializations

end-model