FICO Xpress Optimization Examples Repository
FICO Optimization Community FICO Xpress Optimization Home
Back to examples browserPrevious exampleNext example

Data input/output via I/O drivers

  • Use I/O drivers to handle Mosel output with a callback function, compile a model from memory to memory, load a bim file from memory, initialise arrays in the Model program from Java objects and retrieve information from the model through memory.
  • Use the 'java' I/O driver to handle Mosel output and provide initial data to the model.
Further explanation of this example: 'Mosel Library Reference javadoc'; Whitepaper 'Generalized file handling in Mosel'

Source Files

/*  Mosel Library Examples                              */
/*  ======================                              */
/*                                                      */
/*  file                                  */
/*  ``````````````````                                  */
/*  Example for the use of the Mosel libraries          */
/*  (using IOdrivers for data exchange)                 */
/*                                                      */
/*  (c) 2008 Fair Isaac Corporation                     */
/*      author: Y. Colombani, 2004                      */

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

public class mmexdrvs
                /* The source of the model as a string */
 static final String source_of_model=
"model drivers\n"+

" DATA=''\n"+
" SOL=''\n"+

" S:set of string\n"+
" R:range\n"+
" data:array(S,R) of real\n"+
" sol:array(1..10) of real\n"+

"initialisations from 'jraw:'\n"+
" data as DATA\n"+

"writeln('set   S=',S)\n"+
"writeln('range R=',R)\n"+
"writeln('array data=',data)\n"+

"forall(i in 1..10) sol(i):=i^2\n"+

"initialisations to 'jraw:'\n"+
" sol as SOL\n"+

                /* Class to store initial values for array 'data': */
public static class MyData
 public String s;           // text for the first index
 public int r;              // integer value for the second index
 public double v;           // corresponding value: data(s,r)
 MyData(String i1,int i2,double v0)
 { s=i1; r=i2; v=v0; }

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

                /* OutputStream class to handle default output */
public static class MyOut extends OutputStream
 public void flush()
 { System.out.flush(); }
 public void write(byte[] b)
 { System.out.print("Mosel: "); System.out.write(b,0,b.length); }
 // Other methods are not used by Mosel
 public void write(byte[] b,int off,int len) {}
 public void write(int b) {}
 public void close() {}

                /* Main function */
public static void main(String[] args) throws Exception
 XPRM mosel;                 // An instance of Mosel
 XPRMModel mod;              // The model
 ByteBuffer bimfile;         // Buffer to store BIM file
 ByteBuffer mosfile;         // Buffer to store source file
 MyOut cbmsg=new MyOut();    // Output stream as "MyOut"
 MyData[] data={new MyData("one",2,12.5),new MyData("two",1,15),
                new MyData("three",16,9),new MyData("hundred",2,17)};

//                                                        Initialize Mosel
 mosel=new XPRM();

//                                   Set default output stream to cbmsg
 mosel.bind("cbmsg",cbmsg);                   // Save a reference

                        /* Prepare file names for compilation            */
                                              // Wrap source in a byte buffer
 mosel.bind("mosfile",mosfile);               // Save a reference
 bimfile=ByteBuffer.allocateDirect(1024);     // Create a 1K byte buffer
 mosel.bind("bimfile",bimfile);               // Save a reference

//                                     Compile model from memory to memory
  mosel.compile("","java:mosfile","java:bimfile","test mem comp");
 catch(XPRMCompileException e)
 mosel.unbind("mosfile");                     // Release memory
 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.");

//                                             Load a BIM file from memory
 mosel.unbind("bimfile");                     // Release memory

                        // file names are passed through execution parameters

//                                                           Run the model;

//                        Display solution values obtained from the model
 System.out.print("Solution values:");
 for(int i=0;i<10;i++)
  System.out.print(" "+solution[i]);

Back to examples browserPrevious exampleNext example