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
By clicking on a file name, a preview is opened at the bottom of this page.
xbburg.cxx[download]
xbburgi.cxx[download]
xbburgl.cxx[download]





xbburgl.cxx

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

  file xbburgl.cxx
  ````````````````
  Burglar problem.
  Binary variable formulation with index sets.
  -- Formulating logical conditions 
     with indicator constraints --

  (c) 2009-2024 Fair Isaac Corporation
      author: S.Heipcke, June 2009, rev. Mar. 2011
********************************************************/

#include <iostream>
#include "xprb_cpp.h"

using namespace std;
using namespace ::dashoptimization;

/****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 */

char *ITEMNAMES[] = {"camera", "necklace", "vase", "picture", "tv", "video", 
            "chest", "brick"};
            
int NItems;                      /* Number of items */

int main(int argc, char **argv)
{
 XPRBvar *x;     
 XPRBindexSet ITEMS;             /* Set of items */
 int i;
 XPRBexpr lobj, kn;  
 XPRBctr Log3a,Log3b;
 XPRBprob p("BurglarL");         /* Initialize a new problem in BCL */
 
/****INDICES****/
 ITEMS=p.newIndexSet("Items",8);   /* Create the index set */
 for(i=0;i<8;i++)  ITEMS+=ITEMNAMES[i];
 
 NItems=ITEMS.getSize();         /* Get the size of the index set */
 
/****VARIABLES****/
 x = new XPRBvar[NItems];
 for(i=0;i<NItems;i++)
  x[i] = p.newVar(XPRBnewname("x_%s",ITEMS[i]), XPRB_BV);    
                                 /* 1 if we take item i; 0 otherwise */
 
/****OBJECTIVE****/
 for(i=0;i<NItems;i++)  lobj += VALUE[i]*x[i]; 
 p.setObj(p.newCtr("OBJ",lobj)); /* Set objective: maximize total value */ 

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

/** Logic constraint:
 ** Either take "vase" and "picture" or "tv" and "video" (but not both pairs). */

/* Values within each pair are the same */
 p.newCtr("Log1", x[ITEMS["vase"]] == x[ITEMS["picture"]]);
 p.newCtr("Log2", x[ITEMS["tv"]] == x[ITEMS["video"]]);

/* Choose exactly one pair */
 Log3a = p.newCtr("Log3a", x[ITEMS["tv"]]+x[ITEMS["video"]] <= 0);
 Log3b = p.newCtr("Log3b", x[ITEMS["tv"]]+x[ITEMS["video"]] >= 2);

/* Turn the 2 constraints into indicator constraints */
 Log3a.setIndicator(1, x[ITEMS["vase"]]);
                                   // x["vase"]=1 -> x["tv"]+x["video"]=0
 Log3b.setIndicator(-1, x[ITEMS["vase"]]);
                                   // x["vase"]=0 -> x["tv"]+x["video"]=2

/* Alternative MIP formulation (instead of Log3a and Log3b) */
// p.newCtr("Log3", x[ITEMS["tv"]] = 1 - x[ITEMS["vase"]]);


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

 for(i=0;i<NItems;i++)           /* Print out the chosen items */
  if(x[i].getSol()>0) 
   cout << ITEMS[i] << ": " << x[i].getSol() << endl;  

 delete [] x;
 
 return 0;
} 


Back to examples browserPrevious exampleNext example