![]() | |||||||||||||||
| |||||||||||||||
Burglar - Use of index sets, formulating logical constraints Description Several versions of a simple knapsack problem:
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(); for(i=0;i<NItems;i++) lobj.add(x[i].mul(VALUE[i])); p.setObj(lobj); /* Set objective: maximize total value */ /****CONSTRAINTS****/ kn = new XPRBexpr(); for(i=0;i<NItems;i++) kn.add(x[i].mul(WEIGHT[i])); 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(); } } }
| |||||||||||||||
© Copyright 2022 Fair Isaac Corporation. |