Knapsack problem/Continuous: Difference between revisions

Realize in MiniZinc
(Realize in MiniZinc)
Line 2,170:
The solution is here at [[Knapsack problem/Continuous/Mathprog]].
 
=={{header|MiniZinc}}==
<lang MiniZinc>
%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)]
</lang>
{{out}}
<pre>
Take all of ham
Take all of greaves
Take all of brawn
Take 3.5 of welt
Take all of salami
 
Total Weight=15.0 Total Value=349.38
</pre>
=={{header|Nim}}==
<lang Nim>
2,172

edits