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.cs

/*************************************************************************
  BCL Example Problems
  ====================

  file xbexpl3.cs
  ```````````````
  User error handling.
/* This small, infeasible example shows how the error handling and all
   printed messages can be intercepted by the user's program. This is done
   by defining the corresponding BCL callback functions and changing
   the error handling flag.

  (c) 2008-2024 Fair Isaac Corporation
      Authors: S.Heipcke and D.Brett
*************************************************************************/


using System;
using System.Text;
using System.IO;
using BCL;


namespace Examples
{

    public class TestUGExpl3
    {
        public static int rtsbefore = 1;

        public void modexpl3(ref XPRBprob prob)
        {
            XPRBvar[] x = new XPRBvar[3];
            XPRBctr[] ctr = new XPRBctr[2];
            XPRBexpr cobj;
            int i;

            for(i=0;i<2;i++)
                x[i] = prob.newVar("x_"+i, BCLconstant.XPRB_UI, 0, 100);

            /* Create the constraints:
            C1: 2x0 + 3x1 >= 41
            C2:  x0 + 2x1  = 13 */
            XPRBexpr C1linexp = new XPRBexpr();

            XPRBexpr C2linexp = new XPRBexpr();
            C1linexp = 2 * x[0] + 3 * x[1];
            C2linexp = x[0] + 2 * x[1];
            prob.newCtr("C1", C1linexp >= 41);
            prob.newCtr("C2", C2linexp == 13);


            /* Uncomment the following line to cause an error in the model that
            triggers the user error handling: */

            //x[3] = prob.newVar("x_2", BCLconstant.XPRB_UI, 10, 1);

            /* Objective: minimize x0+x1 */
            cobj = new XPRBexpr(0);
            for(i=0;i<2;i++)
                cobj += x[i];
            prob.setObj(prob.newCtr("OBJ", cobj));

            /* Set objective sense to minimization */
            prob.setSense(BCLconstant.XPRB_MINIM);

            /* Print current problem definition */
            prob.print();

            /* Solve the LP */
            prob.lpOptimize();
            prob.printF("Problem status: " + prob.getProbStat() +
                "  LP status: " + prob.getLPStat() + "  MIP status: " +
                prob.getMIPStat() + "\n");

            /* This problem is infeasible, that means the following command
             * will fail.
             * It prints a warning if the message level is at least 2 */

            prob.printF("Objective: " + prob.getObjVal() + "\n");

            /* Print solution values */
            for(i=0;i<2;i++)
                prob.printF(x[i].getName() + ":" + x[i].getSol() + ", ");
            prob.printF("\n");
        }

        /***********************************************************************/

        /**** User error handling function ****/
        public static void usererror(IntPtr prob, object vp, int num, int type,
                                                                string t)
        {
            Exception eBCL = new Exception("Error in usererror().");
            System.Console.WriteLine("BCL error " +num+ ": " + t);
            if(type==BCLconstant.XPRB_ERR)
                throw eBCL;
        }

        /**** User printing function ****/
        public static void userprint(IntPtr prob, object vp, string msg)
        {

            /* Print 'BCL output' whenever a new output line starts,
            otherwise continue to print the current line. */
            if(rtsbefore==1)
                System.Console.Write("BCL output: " + msg);
            else
                System.Console.Write(msg);

            rtsbefore = (msg.Length > 0 && msg[msg.Length - 1] == '\n') ? 1 : 0;
        }

        /***********************************************************************/

        // This is where one might add custom logging
        static void DoSomeErrorLogging(string msg)
        {
            Console.WriteLine("Here's an error message! {0}", msg);
        }

        public static int Main()
        {
            try
            {
                /* Switch to error handling by the user's program */
                XPRB.setErrCtrl(0); // no auto quit on error
                int initCode = XPRB.init();
                if (initCode != 0 && initCode != 32) // both values are valid
                {
                    DoSomeErrorLogging(Optimizer.XPRS.GetLicErrMsg());
                    return initCode;
                }
                TestUGExpl3 TestInstance = new TestUGExpl3();

                XPRBprob prob = new XPRBprob("EXPL3");
                if (!prob.isValid())
                {
                    DoSomeErrorLogging("Unable to create XPRBprob \"EXPL3\"");
                    return 1;
                }

                /* Set the printing flag. Try other values:
                        0 - no printed output, 1 - only errors,
                        2 - errors and warnings, 3 - all messages */
                prob.setMsgLevel(2);

                /* Define the printing callback function */
                prob.MessageCallbacks += new XPRBMessageCallback(userprint);


                try
                {
                    prob.ErrorCallbacks += new XPRBErrorCallback(usererror);

                    /* Formulate and solve the problem */
                    TestInstance.modexpl3(ref prob);

                    System.Console.WriteLine("I'm about to exit cleanly");
                    return 0;
                }
                catch
                {
                    System.Console.WriteLine("I cannot build the problem");
                    return 1;
                }

            }
            catch
            {
                System.Console.WriteLine("I cannot create the problem");
                return 1;
            }
        }
    }
}
Back to examples browserPrevious exampleNext example