| |||||||||||||||
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.cxx /******************************************************** Xpress-BCL C++ Example Problems =============================== file xbburg.cxx ``````````````` Burglar problem, binary variable formulation. (c) 2008-2024 Fair Isaac Corporation author: S.Heipcke, Jan. 2000, rev. Mar. 2011 ********************************************************/ #include <iostream> #include "xprb_cpp.h" using namespace std; using namespace ::dashoptimization; #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]; XPRBexpr lobj, kn; int i; XPRBprob p("Burglar"); /* Initialize a new problem in BCL */ /****VARIABLES****/ /* 1 if we take item i; 0 otherwise */ for(i=0;i<NItems;i++) x[i]=p.newVar("x",XPRB_BV); /****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 */ /****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 solution */ cout << x[i].getName() << ":" << x[i].getSol() << " "; cout << endl; return 0; } | |||||||||||||||
© Copyright 2024 Fair Isaac Corporation. |