0

Can someone please help me with a code to this problem. The problem wants me to maximize total benefit by filling a 10-lb knapsack with the following data pic:

1 Answers1

0

in OPL CPLEX you can write

tuple t
{
  key int itemNbr;
  float weight;
  float benefit;
}

{t} items=
{
  <1,4,11>,<2,3,7>,<3,5,12>
};

int knapsacksize=10;

dvar boolean take[items];

maximize sum(i in items) i.benefit*take[i];

subject to
{
  sum(i in items) i.weight*take[i]<=knapsacksize;
}

which gives

take = [1 0 1];
Alex Fleischer
  • 9,276
  • 2
  • 12
  • 15