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

UG - Examples from 'BCL Reference Manual'

Description
The following examples are discussed in detail in the 'BCL User Guide and Reference Manual':
  • modeling and solving a small MIP scheduling problem (xbexpl1 version BASIC)
  • using variable arrays and constraint templates (xbexpl1 versions ARRAY and ARRAYC)
  • definition of SOS-1 (xbexpl1 version SOS)
  • data input from file, index sets (xbexpl1i)
  • user error handling, output redirection (xbexpl3)
  • solving multiple scenarios of a transportation problem in parallel (xbexpl2: standard, single thread version)
  • cut generation / adding cuts at MIP tree nodes (xbcutex)
  • quadratic programming (quadratic objective: xbqpr12, quadratic constraints: xbairport)
  • combine BCL problem input with problem solving in Xpress Optimizer (xbcontr1)
  • use an Xpress Optimizer solution callback with a BCL model (xbcontr2s: single MIP thread; xbcontr2: multiple MIP threads)
Further explanation of this example: 'BCL Reference Manual', Appendix B Using BCL with the Optimizer library


Source Files

Data Files





xbexpl3.java

/********************************************************
  Xpress-BCL Java Example Problems
  ================================

  file xbexpl3.java
  `````````````````
  Error handling and output redirection.

  (c) 2008-2024 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) {
        try (XPRB bcl = new XPRB()) {  /* Initialize BCL */

            bcl.setMsgLevel(2);            /* Set the printing flag. Try other values:
                                              0 - no printed output,
                                              2 - print warnings, 3 - all messages */
            try (XPRBprob prob = bcl.newProb("Expl3")) {  /* Create a new problem */
                FileWriter f=new FileWriter("expl3out.txt");
                bcl.setOutputStream(f);       /* Redirect all output from BCL to a file */

                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.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);
            }
        }
        catch(XPRBlicenseError e) {
            System.err.println("BCL error "+ e.getErrorCode() + ": " + e.getMessage());
            System.exit(1);
        }

    }
}

Back to examples browserPrevious exampleNext example