12 lines
786 B
Plaintext
12 lines
786 B
Plaintext
%Knapsack Continuous. Nigel Galloway: October 7th., 2020.
|
|
enum Items={beef,pork,ham,greaves,flitch,brawn,welt,salami,sausage};
|
|
array[Items] of float: weight=[3.8,5.4,3.6,2.4,4.0,2.5,3.7,3.0,5.9];
|
|
array[Items] of int: value =[36,43,90,45,30,56,67,95,9];
|
|
float: maxWeight=15.0;
|
|
var float: wTaken=sum(n in Items)(quantity[n]);
|
|
var float: wValue=sum(n in Items)(value[n]*quantity[n]/weight[n]);
|
|
array[Items] of var 0.0..(max(weight)): quantity; constraint forall(n in Items)(quantity[n]<=weight[n]);
|
|
constraint wTaken <= maxWeight;
|
|
solve maximize wValue;
|
|
output[concat([let {string: g=show(quantity[n])} in "Take "++(if g==show(weight[n]) then "all" else g endif)++" of \(n)\n" | n in Items where show(quantity[n])!="0.0"])++"\nTotal Weight=\(wTaken) Total Value="++show_float(4,2,wValue)]
|