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

Burglar - Use of index sets, formulating logical constraints

Description
Several versions of a simple knapsack problem:
  • xbburg: standard formlation
  • xbburgi: shows how to index an array of variables by an index set
  • xbburgl: adds several indicator constraints to state logical conditions
Further explanation of this example: Quick reference guide 'MIP formulations and linearizations', Section 4 Indicator constraints


Source Files





xbburg.cs

/********************************************************/
/*  Xpress-BCL C# Example Problems                      */
/*  ==============================                      */
/*                                                      */
/*  file xbburg.cs                                      */
/*  ``````````````                                      */
/*  Example for the use of Xpress-BCL                   */
/*  (Burglar problem from the XPRESS-MP Tutorial,       */
/*   binary variable formulation)                       */
/*                                                      */
/*  (c) 2008-2024 Fair Isaac Corporation                */
/*      authors: S.Heipcke, D.Brett.                    */
/********************************************************/

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


namespace Examples
{

    public class TestBurglar
    {

        public const int NItems = 8;                  /* Number of items */

        /****DATA****/
        /* Item:             1   2   3   4   5   6   7   8 */
        double[] VALUE  =  {15,100, 90, 60, 40, 15, 10,  1};    /* Value of items */
        double[] WEIGHT =  { 2, 20, 20, 30, 40, 30, 60, 10};    /* Weight of items */
        double WTMAX    = 102;            /* Max weight allowed for haul */

        public static void Main()
        {
            XPRB.init();
            XPRBvar[] x = new XPRBvar[NItems];
            XPRBexpr lobj, kn;
            int i;
            XPRBprob p = new XPRBprob("Burglar");           /* Initialize a new problem in BCL */
            TestBurglar TestInstance = new TestBurglar();

            /****VARIABLES****/
            /* 1 if we take item i; 0 otherwise */
            for(i=0;i<NItems;i++)
                x[i]=p.newVar("x", BCLconstant.XPRB_BV);

            /****OBJECTIVE****/
            lobj = new XPRBexpr();
            for(i=0;i<NItems;i++)
                lobj += TestInstance.VALUE[i] * x[i];
            p.setObj(p.newCtr("OBJ",lobj));  /* Set objective: maximize total value */

            /****CONSTRAINTS****/
            kn = new XPRBexpr();
            for(i=0;i<NItems;i++)
                kn += TestInstance.WEIGHT[i] * x[i];
            p.newCtr("WtMax", kn <= TestInstance.WTMAX);  /* Weight restriction */

            /****SOLVING + OUTPUT****/
            p.setSense(BCLconstant.XPRB_MAXIM);         /* Choose the sense of the optimization */
            p.mipOptimize();                    /* Solve the MIP-problem */
            System.Console.WriteLine("Objective: " + p.getObjVal());  /* Get objective value */

            for(i=0;i<NItems;i++)
                System.Console.Write(x[i].getName() + ":" + x[i].getSol() + " ");  /* Print out the solution */
            System.Console.WriteLine();

            return;
        }

    }

}
Back to examples browserPrevious exampleNext example