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

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

  file xbburg.c
  `````````````
  Burglar problem, binary variable formulation.

  (c) 2008 Fair Isaac Corporation
      author: S.Heipcke, Jan. 2000, rev. Mar. 2011
********************************************************/

#include <stdio.h>
#include "xprb.h"

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

int main(int argc, char **argv)
{
 XPRBvar x[NItems];
 XPRBctr cobj,ctr;
 int i;
 XPRBprob prob;

 prob=XPRBnewprob("Burglar");           /* Initialize a new problem in BCL */

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

/****OBJECTIVE****/
 cobj = XPRBnewctr(prob,"OBJ",XPRB_N);  /* Maximize the total value */
 for(i=0;i<NItems;i++) XPRBaddterm(cobj,x[i],VALUE[i]);
 XPRBsetobj(prob,cobj);                 /* Select objective function */

/****CONSTRAINTS****/
 ctr=XPRBnewctr(prob,"WtMax", XPRB_L);  /* Weight restriction */
 for(i=0;i<NItems;i++)
  XPRBaddterm(ctr, x[i], WEIGHT[i]);
 XPRBaddterm(ctr, NULL, WTMAX);

/****SOLVING + OUTPUT****/
 XPRBsetsense(prob,XPRB_MAXIM);         /* Choose the sense of optimization */
 XPRBmipoptimize(prob,"");              /* Solve the MIP-problem */
 printf("Objective: %g\n", XPRBgetobjval(prob));   /* Get objective value */

 for(i=0;i<NItems;i++)                  /* Print out the solution values */
  printf("%s:%g ", XPRBgetvarname(x[i]), XPRBgetsol(x[i]));
 printf("\n");

 return 0;
}


Back to examples browserPrevious exampleNext example