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

Contract - Semi-continuous variables, predefined constraint functions, combine BCL with Xpress Optimizer

Description
A small MIP-problem example demonstrating how to define semi-continuous variables, use predefined constraint functions and retrieve the problem status.

Two modified versions (documented in the 'BCL Reference Manual') show how to (1) combine BCL problem input with problem solving in Xpress Optimizer and (2) use an Xpress Optimizer solution callback with a BCL model.

Further explanation of this example: 'BCL Reference Manual', Appendix B Using BCL with the Optimizer library


Source Files
By clicking on a file name, a preview is opened at the bottom of this page.
xbcontr1.cs[download]
xbcontr1.csproj[download]
xbcontr1.cs[download]
xbcontr2.cs[download]
xbcontr2s.cs[download]





xbcontr1.cs

/********************************************************
  Xpress-BCL C# Example Problems
  ==============================

  file xbcontr1.cs
  ````````````````
  Contract allocation example.
  Combining BCL problem input with problem solving
  in Xpress-Optimizer.

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

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


namespace Examples
{

    public class TestUGContr1
    {
        const int District = 6;               /* Number of districts */
        const int Contract = 10;              /* Number of contracts */

        /**** DATA ****/
        int[] OUTPUT = {50, 40, 10, 20, 70, 50}; /* Max. output per district */
        int[] COST   = {50, 20, 25, 30, 45, 40}; /* Cost per district */
        int[] VOLUME   = {20, 10, 30, 15, 20, 30, 10, 50, 10, 20};
        /* Volume of contracts */

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

        public static void Main()
        {
            XPRB.init();
            int d,c;
            XPRBexpr l1,l2,lobj;

            /* Variables indicating whether a project is chosen */
            XPRBvar[,] x = new XPRBvar[District,Contract];

            /* Quantities allocated to contractors */
            XPRBvar[,] y = new XPRBvar[District,Contract];

            int i, ncol, len, stat;
            double[] sol;
            double val;

            /* Initialize a new problem in BCL */
            XPRBprob p = new XPRBprob("Contr1");

            XPRSprob xprsp;
            TestUGContr1 TestInstance = new TestUGContr1();

            /**** VARIABLES ****/
            for(d=0;d<District;d++)
                for(c=0;c<Contract;c++)
                {
                    x[d,c] = p.newVar("x_d" + (d+1) + "c" + (c+1),
                              BCLconstant.XPRB_BV);
                    y[d,c] = p.newVar("q_d" + (d+1) + "c" + (c+1),
                              BCLconstant.XPRB_SC, 0, TestInstance.OUTPUT[d]);
                    y[d,c].setLim(5);
                }

            /****OBJECTIVE****/
            lobj = new XPRBexpr();
            for(d=0;d<District;d++)
                for(c=0;c<Contract;c++)
                    lobj += TestInstance.COST[d]*y[d,c];

            p.setObj(p.newCtr("OBJ",lobj));    /* Set the objective function */

            /**** CONSTRAINTS ****/
            for(c=0;c<Contract;c++)
            {
                l1 = new XPRBexpr();
                l2 = new XPRBexpr();
                for(d=0;d<District;d++)
                {
                    l1 += y[d,c];
                    l2 += x[d,c];
                }

                /* "Size": cover the required volume */
                p.newCtr("Size", l1 >= TestInstance.VOLUME[c]);

                /* "Min": at least 2 districts per contract */
                p.newCtr("Min", l2 >= 2 );
            }

        /* Do not exceed max. output of any district */
            for(d=0;d<District;d++)
            {
                l1 = new XPRBexpr();
                for(c=0;c<Contract;c++)
                l1 += y[d,c];
                p.newCtr("Output", l1 <= TestInstance.OUTPUT[d]);
            }

            /* If a contract is allocated to a district,
               then at least 1 unit is allocated to it */
            for(d=0;d<District;d++)
                for(c=0;c<Contract;c++)
                    p.newCtr("XY", x[d,c] <= y[d,c]);

            /****SOLVING + OUTPUT****/
            p.loadMat();                   /* Load the matrix explicitly */
            xprsp = p.getXPRSprob();
            xprsp.ChgObjSense(ObjSense.Minimize);
            xprsp.MipOptimize();              /* Solve the MIP problem */

            stat = (int)xprsp.MIPStatus;
            /* Get the global (MIP) status */

        /* Test whether an integer solution was found */
            if (((int)MIPStatus.Solution==stat) ||
                ((int)MIPStatus.Optimal==stat))
            {
                val = xprsp.MIPObjVal;
                System.Console.WriteLine("Objective: " + val);

                ncol = xprsp.Cols;
                sol = new double[ncol];

                xprsp.GetMipSol(sol);
                /* Get the primal solution values */
                len = xprsp.NameLength;
                /* Get the maximum name length */


                string[] names = xprsp.GetNames(2, 0, ncol - 1);
                // Get the variable names
                for (i = 0; i < ncol; i++)         // Print out the solution
                    if (sol[i] != 0)
                    {
                        System.Console.Write(names[i] + ": " + sol[i] + ", ");
                    }
                System.Console.WriteLine();

            }

            return;
        }

    }

}
Back to examples browserPrevious exampleNext example