FICO Xpress Optimization Examples Repository
 FICO Optimization Community FICO Xpress Optimization Home

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

/********************************************************
Xpress-BCL Java Example Problems
================================

file xbburg.java

Burglar problem, binary variable formulation.

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

import com.dashoptimization.*;

public class xbburg {
static final int NItems = 8;        /* Number of items */

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

public static void main(String[] args) {
XPRBvar[] x;
XPRBexpr lobj, kn;
int i;

try (XPRBprob p = new XPRBprob("Burglar")) { /* Initialize BCL and create a new problem */

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

/****OBJECTIVE****/
lobj = new XPRBexpr();
p.setObj(lobj);                    /* Set objective: maximize total value */

/****CONSTRAINTS****/
kn = new XPRBexpr();
p.newCtr("WtMax", kn.lEql(WTMAX) );   /* Weight restriction */

/****SOLVING + OUTPUT****/
p.setSense(XPRB.MAXIM);            /* Choose the sense of the optimization */
p.mipOptimize("");                 /* Solve the MIP-problem */
System.out.println("Objective: " + p.getObjVal());  /* Get objective value */

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