/******************************************************* Mosel Example Problems ====================== file runelsd.java ````````````````` Run several instances of the model elsd.mos in parallel and coordinate the solution update. Before running this program, you need to set up the array NodeNames with machine names/addresses of your local network. All nodes that are used need to have the same version of Xpress installed and suitably licensed, and the server "xprmsrv" must have been started on these machines. All files are local to the root node, no write access is required at remote nodes. (c) 2012 Fair Isaac Corporation author: S. Heipcke, Jan 2012, rev. Jul. 2014 *******************************************************/ // javac -cp "xprd.jar;bindrv.jar;." runelsd.java import com.dashoptimization.*; import java.lang.*; import java.io.*; public class runelsd { static final String DATAFILE = "els.dat"; static final int T = 15; // Time periods static final int P = 4; // No. of products static final int I = 2; // No. of (remote) Mosel instances static final int M = 6; // No. of Mosel models static final int NEWSOL = 2; // Identifier for "sol. found" event static final int M1 = 1; static final int M2 = 3; /* Setting up remote connections: * Use machine names within your local network, IP addresses, or * empty string for the current node running this model */ static final String NodeNames[] = {"", "localhost"}; static double[][] solprod; // Sol. values for var.s produce static double[][] solsetup; // Sol. values for var.s setup static double[][] DEMAND = new double[P][T]; // Demand per period /***************** Reading result data ******************/ /**** Read a table of the form 'double ar[][]' ****/ /* static void assignTableVal(BinDrvReader bdrv, double[][] ar) throws IOException { int i,j,ctrl; if (bdrv.getControl()==BinDrvReader.CTRL_OPENLST) { while(bdrv.nextToken()>=0) { ctrl=bdrv.getControl(); if(ctrl==BinDrvReader.CTRL_CLOSELST) break; else if(ctrl==BinDrvReader.CTRL_OPENNDX) { i=bdrv.getInt(); j=bdrv.getInt(); // System.out.println(i + " " + j); if(bdrv.getControl()==BinDrvReader.CTRL_CLOSENDX) { ar[i-1][j-1]=bdrv.getReal(); // System.out.println(ar[i-1][j-1]); } else throw new IOException("Wrong file format. ')' expected."); } else throw new IOException("Wrong file format. '(' expected."); } } else throw new IOException("Wrong file format. '[' expected."); } */ /**** Fixed size version for reading 'double ar[][]' ****/ static void assignFixedTableVal(BinDrvReader bdrv, double[][] ar) throws IOException { int i,j,ctrl,i2,j2; if (bdrv.getControl()==BinDrvReader.CTRL_OPENLST) { while(bdrv.nextToken()>=0) { ctrl=bdrv.getControl(); if(ctrl==BinDrvReader.CTRL_CLOSELST) break; else if(ctrl==BinDrvReader.CTRL_OPENNDX) { i=bdrv.getInt(); j=bdrv.getInt(); if(bdrv.getControl()==BinDrvReader.CTRL_CLOSENDX) { for(j2=j;j2<=T;j2++) { ar[i-1][j2-1]=bdrv.getReal(); // System.out.println(i + " " + j2 + " " + ar[i-1][j2-1]); } } else throw new IOException("Wrong file format. ')' expected."); } else throw new IOException("Wrong file format. '(' expected."); } } else throw new IOException("Wrong file format. '[' expected."); } /**** Identify the label to read ****/ static void readSol(String filename) throws IOException { BinDrvReader bdrv; FileInputStream f; String s; f=new FileInputStream(filename); bdrv=new BinDrvReader(f); // Use Mosel bin reader while(bdrv.nextToken()>=0) { if(bdrv.getControl()==BinDrvReader.CTRL_LABEL) { s=bdrv.getString(); System.out.println("Reading "+s); if (s.equals("solprod")) assignFixedTableVal(bdrv, solprod); else if (s.equals("solsetup")) assignFixedTableVal(bdrv, solsetup); else if (s.equals("DEMAND")) assignFixedTableVal(bdrv, DEMAND); // Unknow labels are simply ignored } else throw new IOException("Wrong file format"); } f.close(); } /****** Run a submodel to read demand data for pretty solution printing ******/ static void readDem(XPRD xprd,XPRDMosel moselInst) throws IOException { XPRDModel modDem = null; // Model try { // Compile the model moselInst.compile("", "rmt:readelsdem.mos", "rmt:readelsdem.bim"); } catch(Exception e) { System.out.println("Compilation failed: " + e); System.exit(2); } try { // Load model modDem=moselInst.loadModel("rmt:readelsdem.bim"); } catch(IOException e) { System.out.println("Loading failed: " + e); System.exit(3); } modDem.execParams = "DATAFILE="+DATAFILE + ",T="+T + ",P="+P; try { modDem.run(); // Run the model } catch(Exception e) { System.out.println("Failed to run model: "+e); System.exit(4); } xprd.waitForEvent(); // Wait for termination event readSol("Demand"); // Read the demand data new File("readelsdem.bim").delete(); // Cleaning up temporary files new File("Demand").delete(); } /***************** Main ******************/ public static void main(String[] args) { XPRD xprd = new XPRD(); // Initialize XPRD XPRDMosel[] moselInst = new XPRDMosel[I]; // Mosel instances XPRDModel[] modELS = new XPRDModel[M]; // Models XPRDEvent Msg; // Messages sent by models solprod = new double[P][T]; solsetup = new double[P][T]; double objval; int algsol,algopt,senderid=0; try { for(int i=0;i