Knapsack problem/Unbounded/Python dynamic programming: Difference between revisions

Content added Content deleted
m (→‎{{header|Python}}: Removed "header" template and adapted header levels)
Line 1: Line 1:
{{collection|Knapsack problem/Unbounded}}
{{collection|Knapsack problem/Unbounded}}


==Dynamic Programming Solution==
=={{header|Python}}==
===Dynamic Programming Solution===
This solution trades off having to search over all possible combinations of items by having to enumerate over all possible sizes of (weight, volume) for each item. The example builds in complexity to the final DP program.
This solution trades off having to search over all possible combinations of items by having to enumerate over all possible sizes of (weight, volume) for each item. The example builds in complexity to the final DP program.


====Brute force, single size attribute====
===Brute force, single size attribute===
A brute-force solution for items with only one 'size' attribute would look like the following and would not scale:
A brute-force solution for items with only one 'size' attribute would look like the following and would not scale:
<lang python>from operator import itemgetter as iget
<lang python>from operator import itemgetter as iget
Line 47: Line 46:
</lang>
</lang>


====DP, single size dimension====
===DP, single size dimension===
The dynamic programming version where 'size' has only one dimension would be the following and produces an optimal solution:
The dynamic programming version where 'size' has only one dimension would be the following and produces an optimal solution:
<lang python>def knapsack_unbounded_dp(items, C):
<lang python>def knapsack_unbounded_dp(items, C):
Line 75: Line 74:
return value, size, numbagged, bagged</lang>
return value, size, numbagged, bagged</lang>


====DP, multiple size dimensions====
===DP, multiple size dimensions===
Our original problem has two dimensions to 'size': weight and volume. We can create a python size object, that knows how to enumerate itself over its given dimensions, as well as perform logical and simple mathematical operations. With the use of the Size object, a correct solution to the given unbounded knapsack problem can be found by the following proceedure:
Our original problem has two dimensions to 'size': weight and volume. We can create a python size object, that knows how to enumerate itself over its given dimensions, as well as perform logical and simple mathematical operations. With the use of the Size object, a correct solution to the given unbounded knapsack problem can be found by the following proceedure:
<lang python>from knapsack_sizer import makesize
<lang python>from knapsack_sizer import makesize
Line 119: Line 118:
print(capacity, dp)</lang>
print(capacity, dp)</lang>


====Sample output====
===Sample output===
The solution found is printed as:
The solution found is printed as:
<pre>Size(wt=250, vol=250) (54500, Size(247, 247), 20, [('gold', 11), ('panacea', 9)])</pre>
<pre>Size(wt=250, vol=250) (54500, Size(247, 247), 20, [('gold', 11), ('panacea', 9)])</pre>
I.e. a choice of 20 items: 11 gold and 9 panacea, for a total value of 54500.
I.e. a choice of 20 items: 11 gold and 9 panacea, for a total value of 54500.


====Ancillary module====
===Ancillary module===
An ancillary file called knapsack_sizer.py must be made available on your PYTHONPATH with the following contents:
An ancillary file called knapsack_sizer.py must be made available on your PYTHONPATH with the following contents:
<lang python>from operator import itemgetter as _itemgetter
<lang python>from operator import itemgetter as _itemgetter