13 lines
1.0 KiB
Plaintext
13 lines
1.0 KiB
Plaintext
%Solve Knapsack Problem Bounded. Nigel Galloway, Octoer 12th., 2020.
|
|
enum Items={map,compass,water,sandwich,glucose,tin,banana,apple,cheese,beer,suntan_cream,camera,t_shirt,trousers,umbrella,waterproof_trousers,waterproof_overclothes,note_case,sunglasses,towel,socks,book};
|
|
array[Items] of int: weight =[9,13,153,50,15,68,27,39,23,52,11,32,24,48,73,42,43,22,7,18,4,30];
|
|
array[Items] of int: value =[150,35,200,60,60,45,60,40,30,10,70,30,15,10,40,70,75,80,20,12,50,10];
|
|
array[Items] of int: quantity=[1,1,2,2,2,3,3,3,1,3,1,1,2,2,1,1,1,1,1,2,1,2];
|
|
array[Items] of var 0..max(quantity): take; constraint forall(n in Items)(take[n]<=quantity[n]);
|
|
int: maxWeight=400;
|
|
var int: wTaken=sum(n in Items)(weight[n]*take[n]);
|
|
var int: wValue=sum(n in Items)(value[n]*take[n]);
|
|
constraint wTaken <= maxWeight;
|
|
solve maximize wValue;
|
|
output[concat([let {string: g=show(take[n])} in "Take "++(if g==show(quantity[n]) then "all" else g endif)++" of \(n)\n" | n in Items where show(take[n])!="0"])++"\nTotal Weight=\(wTaken) Total Value="++show_float(4,2,wValue)]
|