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





xbburgi.cs

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

  file xbburgi.cs
  ```````````````
  Burglar problem.
  Binary variable formulation with index sets.

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

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


namespace Examples
{
    
    public class TestBurglarI
    {
        /****DATA****/
        /* Item:           ca  ne  va  pi  tv  vi  ch  br */
        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 */

        string[] ITEMNAMES = {"camera", "necklace", "vase", "picture", "tv", "video", 
        "chest", "brick"};

        int NItems;                      /* Number of items */

        public static void Main()
        {
            XPRB.init();
            XPRBvar[] x;     
            XPRBindexSet ITEMS;             /* Set of items */
            int i, dummy;
            XPRBexpr lobj, kn;  
            XPRBprob p = new XPRBprob("Burglari");         /* Initialize a new problem in BCL */
            TestBurglarI TestInstance = new TestBurglarI();

            /****INDICES****/
            ITEMS=p.newIndexSet("Items",8);   /* Create the index set */
            for (i = 0; i < 8; i++) 
                dummy = ITEMS + TestInstance.ITEMNAMES[i];

            TestInstance.NItems = ITEMS.getSize();         /* Get the size of the index set */

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

            /****OBJECTIVE****/
            lobj = new XPRBexpr();
            for (i = 0; i < TestInstance.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 < TestInstance.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 < TestInstance.NItems; i++)           /* Print out the chosen items */
            if(x[i].getSol()>0)
                System.Console.WriteLine(ITEMS.getIndexName(i) + ": " + x[i].getSol());  

            return;
        } 
    }
}
Back to examples browserPrevious exampleNext example