How to resolve the algorithm Knapsack problem/Continuous step by step in the MiniZinc programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Knapsack problem/Continuous step by step in the MiniZinc programming language
Table of Contents
Problem Statement
A thief burgles a butcher's shop, where he can select from some items. The thief knows the weights and prices of each items. Because he has a knapsack with 15 kg maximal capacity, he wants to select the items such that he would have his profit maximized. He may cut the items; the item has a reduced price after cutting that is proportional to the original price by the ratio of masses. That means: half of an item has half the price of the original.
This is the item list in the butcher's shop:
Show which items the thief carries in his knapsack so that their total weight does not exceed 15 kg, and their total value is maximized.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Knapsack problem/Continuous step by step in the MiniZinc programming language
Source code in the minizinc programming language
%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)]
You may also check:How to resolve the algorithm Sorting algorithms/Bead sort step by step in the COBOL programming language
You may also check:How to resolve the algorithm Honeycombs step by step in the Python programming language
You may also check:How to resolve the algorithm Generate lower case ASCII alphabet step by step in the VBA programming language
You may also check:How to resolve the algorithm Hello world/Text step by step in the Visual Basic programming language
You may also check:How to resolve the algorithm Tau function step by step in the C programming language