/********************************************************
  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();

   System.gc();                  /* Force garbage collection */
   System.runFinalization();
   
   prob.finalize();              /* Delete the problem */
   prob=null;
  
   bcl.finalize();               /* Release license */
   bcl=null;     

   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);
  } 
 } 
} 
