UG - Examples from 'BCL Reference Manual'
|  | |
| Type: | Programming | 
| Rating: | 3 | 
| Description: | The following examples are discussed in detail in the 'BCL User Guide and Reference Manual': 
 | 
| File(s): | xbexpl1.java, xbexpl1i.java, xbexpl3.java, xbexpl2.java, xbcutex.java, xbqpr12.java, xbairport.java, xbcontr1.java, xbcontr2.java, xbcontr2s.java | 
| Data file(s): | durations.dat | 
|  | |
| xbexpl1.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbexpl1.java
  `````````````````
  BCL user guide example.
  Definition of variables and constraints, 
  variable arrays and SOS, followed by file output,
  solving and printing of solutions.
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbexpl1
{
/**************************************************************************/
/* Expl 1: This example corresponds to the one printed in the             */
/*  BCL User Guide. It shows how to define variables and                  */
/*  constraints, and Special Ordered Sets, followed by file output,       */
/*  solving and printing of solutions.                                    */
/* Set the following parameter to true to try out a problem formulation   */
/* using Special Ordered Sets:                                            */
 static final boolean SOS = false;
/**************************************************************************/
 static final int NJ = 4;        /* Number of jobs */   
 static final int NT = 10;       /* Time limit */   
/**** DATA ****/
 static final double[] DUR = {3,4,2,2};   /* Durations of jobs   */ 
 static XPRBvar[] start;         /* Start times of jobs  */ 
 static XPRBvar[][] delta;       /* Binaries for start times */  
 static XPRBvar z;               /* Maximum completion time (makespan) */ 
 static XPRBsos[] set;           /* Sets regrouping start times for jobs */
 static XPRB bcl;
 static XPRBprob p; 
             
/*************************************************************************/
 static void jobsModel() throws IOException
 {
  XPRBexpr le;
  int j,t;
/****VARIABLES****/
  start = new XPRBvar[NJ];       /* Create start time variables */
  for(j=0;j | 
| xbexpl1i.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbexpl1i.java
  ``````````````````
  BCL user guide example.
  Version using index sets.
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2003, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbexpl1i
{
 static final String DATAFILE = System.getProperty("XPRBDATA") + 
   "/jobs/durations.dat";
 static final int MAXNJ = 4;     /* Max. number of jobs */   
 static final int NT = 10;       /* Time limit */   
/**** DATA ****/
 static int NJ = 0;              /* Number of jobs read in */
 static double[] DUR;            /* Durations of jobs   */ 
 static XPRBindexSet Jobs;	 /* Job names */
 static XPRBvar[] start;         /* Start times of jobs  */ 
 static XPRBvar[][] delta;       /* Binaries for start times */  
 static XPRBvar z;               /* Maximum completion time (makespan) */ 
 static XPRB bcl;
 static XPRBprob p; 
             
/*************************************************************************/
    /**** Initialize the stream tokenizer ****/
 static StreamTokenizer initST(FileReader file)
 {
  StreamTokenizer st=null;
  st= new StreamTokenizer(file);   /* Initialize the stream tokenizer */
  st.commentChar('!');             /* Use the character '!' for comments */
  st.eolIsSignificant(true);       /* Return end-of-line character */
  st.ordinaryChar(',');            /* Use ',' as separator */
  st.parseNumbers();               /* Read numbers as numbers (not strings)*/ 
  return st;
 }
    /**** Read data from files ****/
 static void readData() throws IOException
 {
  FileReader datafile=null;
  StreamTokenizer st;
  int i;
 
                                   /* Create a new index set */
  Jobs = p.newIndexSet("Jobs", MAXNJ);
  DUR = new double[MAXNJ];
  
        /* Read the durations data file */
  datafile = new FileReader(DATAFILE);
  st = initST(datafile);
  do
  {
   do
   { 
    st.nextToken();
   } while(st.ttype==st.TT_EOL);    /* Skip empty lines */
   if(st.ttype != st.TT_WORD) break; 
   i=Jobs.addElement(st.sval);
   if(st.nextToken() != ',') break;
   if(st.nextToken() != st.TT_NUMBER) break;
   DUR[i] = st.nval;
   NJ+=1;
  } while( st.nextToken() == st.TT_EOL && NJ | 
| xbexpl3.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbexpl3.java
  `````````````````
  Error handling and output redirection.
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Jan. 2012
********************************************************/
import java.io.*;
import com.dashoptimization.*;
/* This small, infeasible example shows how all printed messages can 
   be intercepted by the user's program. */
public class xbexpl3
{
 static XPRB bcl;
/***********************************************************************/
 public static void modexpl3(XPRBprob prob) throws XPRBerror
 {
  XPRBvar[] x;
  XPRBexpr cobj;
  int i;
  x = new XPRBvar[3];                 /* Create the variables */
  for(i=0;i<2;i++) x[i] = prob.newVar("x_"+i, XPRB.UI, 0, 100);
                /* Create the constraints:
                   C1: 2x0 + 3x1 >= 41
                   C2:  x0 + 2x1  = 13 */
  prob.newCtr("C1", x[0].mul(2).add(x[1].mul(3)) .gEql(41));
  prob.newCtr("C2", x[0].add(x[1].mul(2)) .eql(13));
/* Uncomment the following line to cause an error in the model that
   triggers the error handling: */
//  x[2] = prob.newVar("x_2", XPRB.UI, 10, 1); 
 
                /* Objective: minimize x0+x1 */
  cobj = new XPRBexpr(); 
  for(i=0;i<2;i++) cobj.add(x[i]); 
  prob.setObj(cobj);             /* Select objective function */ 
  prob.setSense(XPRB.MINIM);     /* Set objective sense to minimization */ 
  prob.print();                  /* Print current problem definition */
  prob.lpOptimize("");           /* Solve the LP */
  System.out.println("Problem status: " + prob.getProbStat() +
                     "  LP status: " + prob.getLPStat() + 
                     "  MIP status: " + prob.getMIPStat());
/* This problem is infeasible, that means the following command will fail.
   It prints a warning if the message level is at least 2 */
   
  System.out.println("Objective: " + prob.getObjVal());
  for(i=0;i<2;i++)               /* Print solution values */ 
   System.out.print(x[i].getName() + ":" + x[i].getSol() + ", ");
  System.out.println();
}
/***********************************************************************/
 public static void main(String[] args)
 {
  FileWriter f;
  XPRBprob prob;
  try
  {
   bcl = new XPRB();             /* Initialize BCL */
  }
  catch(XPRBlicenseError e)
  {
   System.err.println("BCL error "+ e.getErrorCode() + ": " + e.getMessage());
   System.exit(1);
  } 
  bcl.setMsgLevel(2);            /* Set the printing flag. Try other values:
                                    0 - no printed output,
                                    2 - print warnings, 3 - all messages */
  try
  {
   f=new FileWriter("expl3out.txt");
   bcl.setOutputStream(f);       /* Redirect all output from BCL to a file */
   prob = bcl.newProb("Expl3");  /* Create a new problem */
   prob.setOutputStream();       /* Output for this prob. on standard output */
   modexpl3(prob);               /* Formulate and solve the problem */
   prob.setOutputStream(f);      /* Redirect problem output to file */
   prob.print();                 /* Write to the output file */
   prob.setOutputStream();       /* Re-establish standard output for prob */
   bcl.setOutputStream();        /* Re-establish standard output for BCL */
   f.close();
   prob.finalize();              /* Delete the problem */
   prob=null;
  
   bcl.finalize();               /* Release license */
   bcl=null;
   System.gc();                  /* Force garbage collection */
   System.runFinalization();
   System.err.flush();
  }
  catch(IOException e)
  {
   System.err.println(e.getMessage());
   System.exit(1);   
  }
  catch(XPRBerror e)
  {
   System.err.println("BCL error "+ e.getErrorCode() + ": " + e.getMessage());
   System.exit(1);
  } 
 } 
} 
 | 
| xbexpl2.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbexpl2.java
  `````````````````
  Transportation model demonstrating use of index sets.
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbexpl2
{
 static final int MaxSuppliers = 100; /* Max. number of suppliers */
 static final int MaxCustomers = 1000;/* Max. number of customers */
 static final int MaxArcs = 10000;    /* Max. number of non-zero cost values */
 static final String DEMANDFILE = System.getProperty("XPRBDATA") + 
   "/trans/ex2dem1.dat";  
                                /* Demand data file (comma-separated format) */
 static final String AVAILFILE = System.getProperty("XPRBDATA") + 
   "/trans/ex2avail.dat"; 
                                /* Supply data file (comma-separated format) */
 static final String COSTFILE = System.getProperty("XPRBDATA") + 
   "/trans/ex2cost.dat";   
                                /* Cost data file (comma-separated format) */
 static XPRBindexSet Suppliers; /* Set of suppliers */
 static XPRBindexSet Customers; /* Set of customers */
 static double[] AVAIL;         /* Availability of products */
 static double[] DEMAND;        /* Demand by customers */
 static class TData {
        int suppl;
        int custm;
        double value;
 };
 
 static TData[] COST = new TData[MaxArcs]; /* Cost per supplier-customer pair*/
 static int NSuppl=0, NCustom=0, NArc=0;   /* Actual numbers of suppliers,
                                              customers, and arcs */
 static XPRB bcl;
 static XPRBprob p;
/***********************************************************************/
 static void modTrans() throws IOException
 {
  XPRBexpr lobj, av[], de[];
  int s,c,a;
  XPRBvar[] x;
 
/****VARIABLES****/
  x = new XPRBvar[NArc];
  for(a=0; a | 
| xbcutex.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbcutex.java
  `````````````````
  Simplified version of xbexpl1.java showing how 
  to define cuts with BCL.
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbcutex
{
 static final int NJ = 4;        /* Number of jobs */   
 static final int NT = 10;       /* Time limit */   
/**** DATA ****/
 static final double[] DUR = {3,4,2,2};   /* Durations of jobs   */ 
 static XPRBvar[] start;         /* Start times of jobs  */ 
 static XPRBvar[][] delta;       /* Binaries for start times */  
 static XPRBvar z;               /* Maximum completion time (makespan) */ 
 static XPRB bcl;
 static XPRBprob p; 
             
/*************************************************************************/
 static class CutMgrCallback implements XPRScutMgrListener
 {
  public int XPRScutMgrEvent(XPRSprob oprob, Object data)
  {
   XPRBprob bprob;
   XPRBcut[] ca;
   int num,i;
   bprob = (XPRBprob)data;       /* Get the BCL problem */
   try 
   {   
    bprob.beginCB(oprob);
    num = oprob.getIntAttrib(XPRS.NODES);
    if(num == 2)                 /* Only generate cuts at node 2 */
    {
     ca = new XPRBcut[2];
     ca[0] = bprob.newCut(start[1].add(2) .lEql(start[0]), 2); 
     ca[1] = bprob.newCut(start[2].mul(4) .add(start[3].mul(-5.3)) .lEql(-17), 2);
     System.out.println("Adding constraints:");
     for(i=0;i<2;i++) ca[i].print();
     bprob.addCuts(ca);
    }
    bprob.endCB();
   }
   catch(XPRSprobException e)
   {
    System.out.println("Error  " + e.getCode() + ": " + e.getMessage());
   } 
   return 0;                     /* Call this method once per node */
  }
 }
/*************************************************************************/
 static void jobsModel()
 {
  XPRBexpr le;
  int j,t;
/****VARIABLES****/
  start = new XPRBvar[NJ];       /* Create start time variables */
  for(j=0;j | 
| xbqpr12.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbqpr12.java
  `````````````````
  Small Quadratic Programming example.
       minimize x1 + x1^2 +2x1x2 +2x2^2 +x4^2
       s.t. C1:  x1 +2x2 -4x4 >= 0
            C2: 3x1 -2x3 - x4 <= 100
            C3: 10 <= x1 +3x2 +3x3 -2x4 <= 30
            0<=x1<=20
            0<=x2,x3
            x4 free
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbqpr12
{
 static final int NXPRBvar = 4;
/***********************************************************************/
 public static void main(String[] args) throws IOException
 {
  XPRB bcl;
  XPRBctr c;
  XPRBexpr le, qobj;
  XPRBvar[] x;
  int i;
  XPRBprob p;
  
  bcl = new XPRB();               /* Initialize BCL */
  p = bcl.newProb("QPr12");       /* Create a new problem in BCL */
/**** VARIABLES ****/
  x = new XPRBvar[NXPRBvar];
  x[0] = p.newVar("x1", XPRB.PL, 0, 20);
  x[1] = p.newVar("x2");
  x[2] = p.newVar("x3");
  x[3] = p.newVar("x4", XPRB.PL, -XPRB.INFINITY, XPRB.INFINITY);
/****OBJECTIVE****/
  qobj = new XPRBexpr();          /* Define the objective function */
  qobj.add(x[0]);
  qobj.add( (x[0].sqr()) .add(x[0].mul(2).mul(x[1]))
   .add(x[1].sqr().mul(2)) .add(x[3].sqr()) );
  p.setObj(qobj);
 
/**** CONSTRAINTS ****/
  p.newCtr("C1", x[0].add(x[1].mul(2)).add(x[3].mul(-4)) .gEql(0) );
  p.newCtr("C2", x[0].mul(3).add(x[2].mul(-2)).add(x[3].mul(-1)) .lEql(100) );
  c = p.newCtr("C3", x[0].add(x[1].mul(3)) .add(x[2].mul(3)) 
      .add(x[3].mul(-2)) );
  c.setRange(10,30);
   
/****SOLVING + OUTPUT****/
  p.print();			  /* Print out the problem definition */
  p.exportProb(XPRB.MPS,"QPr12"); /* Output the matrix in MPS format */
  p.exportProb(XPRB.LP,"QPr12");  /* Output the matrix in LP format */
  
  p.setSense(XPRB.MINIM);         /* Choose the sense of the  optimization */   
  p.lpOptimize("");               /* Solve the QP-problem */
  System.out.println("Objective function value: " + p.getObjVal());
  for(i=0;i | 
| xbairport.java | 
| /********************************************************
  BCL Example Problems
  ====================
  file xbairport.java
  ```````````````````
  QCQP problem by
      Rodrigo de Barros Nabholz & Maria Aparecida Diniz Ehrhardt
      November 1994, DMA - IMECC- UNICAMP.
  Based on AMPL model airport.mod by Hande Y. Benson
  (Source: http://www.orfe.princeton.edu/~rvdb/ampl/nlmodels/ )
   
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, June 2008, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbairport
{
 static final int N = 42;
 static final double CX[] = {-6.3, -7.8, -9, -7.2, -5.7, -1.9, -3.5, -0.5, 1.4,
              4, 2.1, 5.5, 5.7, 5.7, 3.8, 5.3, 4.7, 3.3, 0, -1, -0.4, 4.2, 
              3.2, 1.7, 3.3, 2, 0.7, 0.1, -0.1, -3.5, -4, -2.7, -0.5, -2.9,
              -1.2, -0.4, -0.1, -1, -1.7, -2.1, -1.8, 0};
 static final double CY[] = {8, 5.1, 2, 2.6, 5.5, 7.1, 5.9, 6.6, 6.1, 5.6, 4.9, 
              4.7, 4.3, 3.6, 4.1, 3, 2.4, 3, 4.7, 3.4, 2.3, 1.5, 0.5, -1.7, -2,
              -3.1, -3.5, -2.4, -1.3, 0, -1.7, -2.1, -0.4, -2.9, -3.4, -4.3,
              -5.2, -6.5, -7.5, -6.4, -5.1, 0};
 static final double R[] = {0.09, 0.3, 0.09, 0.45, 0.5, 0.04, 0.1, 0.02, 0.02,  
              0.07, 0.4, 0.045, 0.05, 0.056, 0.36, 0.08, 0.07, 0.36, 0.67, 0.38,
              0.37, 0.05, 0.4, 0.66, 0.05, 0.07, 0.08, 0.3, 0.31, 0.49, 0.09, 
              0.46, 0.12, 0.07, 0.07, 0.09, 0.05, 0.13, 0.16, 0.46, 0.25, 0.1};
 public static void main(String[] args) throws IOException
 {
  XPRB bcl;
  XPRBprob prob;
  int i,j;
  XPRBvar[] x,y;                        /* x-/y-coordinates to determine */
  XPRBexpr qe;
  XPRBctr cobj, c;
  bcl = new XPRB();                     /* Initialize BCL */
  prob = bcl.newProb("airport");        /* Create a new problem in BCL */
/**** VARIABLES ****/
  x = new XPRBvar[N];
  for(i=0;i | 
| xbcontr1.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbcontr1.java
  ``````````````````
  Contract allocation example.
  Combining BCL problem input with problem solving 
  in Xpress-Optimizer.
  
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbcontr1
{
 static final int District = 6;   /* Number of districts */
 static final int Contract = 10;  /* Number of contracts */
/**** DATA ****/
 static final int[] OUTPUT = {50, 40, 10, 20, 70, 50};    
                                  /* Max. output per district */
 static final int[] COST   = {50, 20, 25, 30, 45, 40};    
                                  /* Cost per district */
 static final int[] VOLUME = {20, 10, 30, 15, 20, 30, 10, 50, 10, 20};  
                                  /* Volume of contracts */
/***********************************************************************/
 public static void main(String[] args) throws XPRSexception, XPRSprobException
 {
  int d, c, i, stat, ncol, len;
  double [] sol;
  double val;
  java.lang.String [] names;
  XPRB bcl;
  XPRBprob p;
  XPRSprob op;
  XPRBexpr l1,l2,lobj;
  XPRBvar[][] x;                  /* Variables indicating whether a project 
                                     is chosen */
  XPRBvar[][] y;                  /* Quantities allocated to contractors */
  bcl = new XPRB();               /* Initialize BCL */
  p = bcl.newProb("Contract1");   /* Create a new problem in BCL */
  XPRS.init();                    /* Initialize Xpress-Optimizer */
 
/**** VARIABLES ****/
  x = new XPRBvar[District][Contract];
  y = new XPRBvar[District][Contract];
  for(d=0;d | 
| xbcontr2.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbcontr2.java
  ``````````````````
  Contract allocation example.
  Combining BCL problem input with problem solving 
  and callbacks in Xpress-Optimizer.
  
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbcontr2
{
 static final int District = 6;   /* Number of districts */
 static final int Contract = 10;  /* Number of contracts */
/**** DATA ****/
 static final int[] OUTPUT = {50, 40, 10, 20, 70, 50};    
                                  /* Max. output per district */
 static final int[] COST   = {50, 20, 25, 30, 45, 40};    
                                  /* Cost per district */
 static final int[] VOLUME = {20, 10, 30, 15, 20, 30, 10, 50, 10, 20};  
                                  /* Volume of contracts */
 /***********************************************************************/
 static class IntSolCallback implements XPRSintSolListener
 {
  public void XPRSintSolEvent(XPRSprob oprob, Object data)
  {
   int num, d, c;
   XPRBprob bprob;   
   XPRBvar y;
   try {
    bprob = (XPRBprob)data;
    bprob.beginCB(oprob);
    num = oprob.getIntAttrib(XPRS.MIPSOLS);  /* Get number of the solution */
    bprob.sync(XPRB.XPRS_SOL);               /* Update BCL solution values */
  
    System.out.println("Solution " + num + ": Objective value: " +
                       bprob.getObjVal());
    for(d=0;d | 
| xbcontr2s.java | 
| /********************************************************
  Xpress-BCL Java Example Problems
  ================================
  file xbcontr2s.java
  ```````````````````
  Contract allocation example.
  Combining BCL problem input with problem solving 
  and callbacks in Xpress-Optimizer.
  -- Single MIP thread --
  
  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, 2005, rev. Dec. 2011
********************************************************/
import java.io.*;
import com.dashoptimization.*;
public class xbcontr2s
{
 static final int District = 6;   /* Number of districts */
 static final int Contract = 10;  /* Number of contracts */
/**** DATA ****/
 static final int[] OUTPUT = {50, 40, 10, 20, 70, 50};    
                                  /* Max. output per district */
 static final int[] COST   = {50, 20, 25, 30, 45, 40};    
                                  /* Cost per district */
 static final int[] VOLUME = {20, 10, 30, 15, 20, 30, 10, 50, 10, 20};  
                                  /* Volume of contracts */
 /***********************************************************************/
 static class IntSolCallback implements XPRSintSolListener
 {
  public void XPRSintSolEvent(XPRSprob oprob, Object data)
  {
   int num, d, c;
   XPRBprob bprob;   
   XPRBvar y;
   try {
    bprob = (XPRBprob)data;
    num = oprob.getIntAttrib(XPRS.MIPSOLS);  /* Get number of the solution */
    bprob.sync(XPRB.XPRS_SOL);               /* Update BCL solution values */
  
    System.out.println("Solution " + num + ": Objective value: " +
                       bprob.getObjVal());
    for(d=0;d | 
 
