How to resolve the algorithm Knapsack problem/Unbounded step by step in the Mathematica/Wolfram Language programming language

Published on 22 June 2024 08:30 PM

How to resolve the algorithm Knapsack problem/Unbounded step by step in the Mathematica/Wolfram Language programming language

Table of Contents

Problem Statement

A traveler gets diverted and has to make an unscheduled stop in what turns out to be Shangri La.   Opting to leave, he is allowed to take as much as he likes of the following items, so long as it will fit in his knapsack, and he can carry it. He knows that he can carry no more than   25   'weights' in total;   and that the capacity of his knapsack is   0.25   'cubic lengths'. Looking just above the bar codes on the items he finds their weights and volumes.   He digs out his recent copy of a financial paper and gets the value of each item.

He can only take whole units of any item, but there is much more of any item than he could ever carry

Show how many of each item does he take to maximize the value of items he is carrying away with him.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Knapsack problem/Unbounded step by step in the Mathematica/Wolfram Language programming language

This Wolfram code defines three sets of parameters (p, i, g) for three different scenarios:

  • Personal vehicle (PV): pva, pwe, pvo
  • Industrial equipment (IW): iva, iwe, ivo
  • Goods vehicle (GV): gva, gwe, gvo

These parameters represent the power, energy, and volume of each scenario.

The code then sets the maximum values for energy (vomax) and power (wemax) to be 1/4 and 25, respectively.

It calculates the maximum values for p, i, and g using the Min function, which takes the minimum of two values for each parameter. The maximum values are calculated as follows:

  • pmax: Minimum of vomax/pvo and wemax/pwe
  • imax: Minimum of vomax/ivo and wemax/iwe
  • gmax: Minimum of vomax/gvo and wemax/gwe

The code then creates a data table by iterating over the values of p, i, and g from 0 to their respective maximum values and combining them in all possible combinations. This results in a flat list of tuples containing the power, energy, and volume for each scenario.

The data table is then filtered to remove any scenarios where the energy is greater than vomax or the power is greater than wemax.

The code finally sorts the data table by the first element (i.e., the power) and splits it into groups based on the first element. The first group, which contains the scenarios with the highest power, is printed.

The printed output shows the following scenarios:

{{54500,247/10,247/1000,{9,0,11}},{54500,124/5,247/1000,{6,5,11}},{54500,249/10,247/1000,{3,10,11}},{54500,25,247/1000,{0,15,11}}}

Each scenario is represented by a tuple containing the following values:

  • Power (PV)
  • Energy (IW)
  • Volume (GV)
  • Index of the three parameters (p, i, g)

For example, the first scenario has the following parameters:

  • Power (PV): 54500
  • Energy (IW): 247/10
  • Volume (GV): 247/1000
  • Index of the three parameters (p, i, g): {9, 0, 11}

This means that the scenario corresponds to a personal vehicle (PV) with a power of 9, an industrial equipment (IW) with energy of 0, and a goods vehicle (GV) with volume of 11.

Source code in the wolfram programming language

{pva,pwe,pvo}={3000,3/10,1/40};
{iva,iwe,ivo}={1800,2/10,3/200};
{gva,gwe,gvo}={2500,2,2/1000};
wemax=25;
vomax=1/4;
{pmax,imax,gmax}=Floor/@{Min[vomax/pvo,wemax/pwe],Min[vomax/ivo,wemax/iwe],Min[vomax/gvo,wemax/gwe]};

data=Flatten[Table[{{p,i,g}.{pva,iva,gva},{p,i,g}.{pwe,iwe,gwe},{p,i,g}.{pvo,ivo,gvo},{p,i,g}},{p,0,pmax},{i,0,imax},{g,0,gmax}],2];
data=Select[data,#[[2]]<=25&&#[[3]]<=1/4&];
First[SplitBy[Sort[data,First[#1]>First[#2]&],First]]


{{54500,247/10,247/1000,{9,0,11}},{54500,124/5,247/1000,{6,5,11}},{54500,249/10,247/1000,{3,10,11}},{54500,25,247/1000,{0,15,11}}}


p:9 i:0 v:11
p:6 i:5 v:11
p:3 i:10 v:11
p:0 i:15 v:11


  

You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Fortran programming language
You may also check:How to resolve the algorithm Reduced row echelon form step by step in the Wren programming language
You may also check:How to resolve the algorithm Array concatenation step by step in the MiniScript programming language
You may also check:How to resolve the algorithm Palindromic gapful numbers step by step in the C++ programming language
You may also check:How to resolve the algorithm Bitcoin/address validation step by step in the Seed7 programming language