Knapsack problem/Continuous: Difference between revisions

No edit summary
Line 2,701:
Take all the greaves
Take 3.5 kg of welt</pre>
 
=={{header|Picat}}==
<lang Picat>go =>
items(Items),
weights(Weights),
values(Values),
knapsack_max_weight(MaxWeight),
knapsack(Weights,Values,MaxWeight, X,TotalWeight,TotalValue),
nl,
printf("Total weight: %0.2f Total value: %0.2f\n", TotalWeight,TotalValue),
foreach(I in 1..Items.len)
if X[I] > 0.0 then
printf("%-8w: ",Items[I]),
if X[I] == Weights[I] then
printf("%0.2f (%w)", Weights[I], all)
else
printf("%-0.2f",X[I])
end,
nl
end
end,
nl.
 
knapsack(Weights,Values,MaxWeight, X,TotalWeight,TotalValue) =>
N = Weights.len,
 
X = new_list(N),
X :: 0.0..max(Weights),
 
TotalWeight #= sum(X),
TotalWeight #<= MaxWeight,
foreach(I in 1..N)
X[I] #<= Weights[I]
end,
 
WeightsInv = [1/Weights[I] : I in 1..N],
TotalValue #= sum([X[I]*Values[I]*WeightsInv[I] : I in 1..N]),
 
Vars = X ++ [TotalWeight],
solve($[glpk,max(TotalValue)],Vars).
 
% data
knapsack_max_weight(15.0).
items([beef,pork,ham,greaves,flitch,brawn,welt,salami,sausage]).
weights([3.8,5.4,3.6,2.4,4.0,2.5,3.7,3.0,5.9]).
values([36,43,90,45,30,56,67,95,98]).</lang>
 
{{out}}
<pre>otal weight: 15.00 Total value: 349.38
ham : 3.60 (all)
greaves : 2.40 (all)
brawn : 2.50 (all)
welt : 3.50
salami : 3.00</pre>
 
 
=={{header|PicoLisp}}==
495

edits