Typical problem and execution method
A knapsack with a capacity of $ c (\ gt 0) $ and $ n $ luggage $ N = \ {1, \ dots, n \} $ are given. Let the capacity of the package $ i \ in N $ be $ w_i (\ gt 0) $ and the value be $ p_i (\ gt 0) $. Find an assortment of packages that maximizes the sum of values within the $ c $ capacity limit.
usage
Signature: knapsack(size, weight, capacity)
Docstring:
Knapsack problem
Maximize value
input
size:List of luggage sizes
weight:List of luggage values
capacity:capacity
output
Sum of values and list of selected packages
python
from ortoolpy import knapsack
size = [21, 11, 15, 9, 34, 25, 41, 52]
weight = [22, 12, 16, 10, 35, 26, 42, 53]
capacity = 100
print(knapsack(size, weight, capacity))
result
(105.0, [0, 1, 3, 4, 5])
python
# pandas.DataFrame
from ortoolpy.optimization import Knapsack
Knapsack('data/knapsack.csv', 100)
size | weight | |
---|---|---|
0 | 21 | 22 |
1 | 11 | 12 |
3 | 9 | 10 |
4 | 34 | 35 |
5 | 25 | 26 |
Recommended Posts