RosettaCodeData/Task/Knapsack-problem-Unbounded/11l/knapsack-problem-unbounded.11l
2023-07-01 13:44:08 -04:00

33 lines
1.5 KiB
Text
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

T Bounty
Int value
Float weight, volume
F (value, weight, volume)
(.value, .weight, .volume) = (value, weight, volume)
V panacea = Bounty(3000, 0.3, 0.025)
V ichor = Bounty(1800, 0.2, 0.015)
V gold = Bounty(2500, 2.0, 0.002)
V sack = Bounty( 0, 25.0, 0.25)
V best = Bounty( 0, 0, 0)
V current = Bounty( 0, 0, 0)
V best_amounts = (0, 0, 0)
V max_panacea = Int(min(sack.weight I/ panacea.weight, sack.volume I/ panacea.volume))
V max_ichor = Int(min(sack.weight I/ ichor.weight, sack.volume I/ ichor.volume))
V max_gold = Int(min(sack.weight I/ gold.weight, sack.volume I/ gold.volume))
L(npanacea) 0 .< max_panacea
L(nichor) 0 .< max_ichor
L(ngold) 0 .< max_gold
current.value = npanacea * panacea.value + nichor * ichor.value + ngold * gold.value
current.weight = npanacea * panacea.weight + nichor * ichor.weight + ngold * gold.weight
current.volume = npanacea * panacea.volume + nichor * ichor.volume + ngold * gold.volume
I current.value > best.value & current.weight <= sack.weight & current.volume <= sack.volume
best = current
best_amounts = (npanacea, nichor, ngold)
print(Maximum value achievable is best.value)
print(This is achieved by carrying (one solution) #. panacea, #. ichor and #. gold.format(best_amounts[0], best_amounts[1], best_amounts[2]))
print(The weight to carry is #2.1 and the volume used is #.3.format(best.weight, best.volume))