RosettaCodeData/Task/Knapsack-problem-Unbounded/Zkl/knapsack-problem-unbounded.zkl

22 lines
976 B
Plaintext

panacea:=T(3000, 0.3, 0.025); // (value,weight,volume)
ichor :=T(1800, 0.2, 0.015);
gold :=T(2500, 2.0, 0.002);
sack :=T( 0, 25.0, 0.250); const VAL=0, W=1, VOL=2;
maxes:=T(panacea,ichor,gold)
.apply('wrap(t){ (sack[W]/t[W]).min(sack[VOL]/t[VOL]).toInt().walker() });
best:=Utils.Helpers.cprod3(maxes.xplode())
.apply('wrap(t){
T(T(panacea[VAL]*t[0] + ichor[VAL]*t[1] + gold[VAL]*t[2],
panacea[W] *t[0] + ichor[W] *t[1] + gold[W] *t[2],
panacea[VOL]*t[0] + ichor[VOL]*t[1] + gold[VOL]*t[2]), t)
})
.filter('wrap(t){ t[0][W]<=sack[W] and t[0][VOL]<=sack[VOL] })
.reduce(fcn(a,b){ a[0][VAL] > b[0][VAL] and a or b });
println("Maximum value achievable is %,d".fmt(best[0][VAL]));
println(("This is achieved by carrying (one solution):"
" %d panacea, %d ichor and %d gold").fmt(best[1].xplode()));
println("The weight to carry is %4.1f and the volume used is %5.3f"
.fmt(best[0][1,*].xplode()));