-
Notifications
You must be signed in to change notification settings - Fork 48
/
knapsack.py
156 lines (124 loc) · 4.4 KB
/
knapsack.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# A Dynamic Programming based Python Program for 0-1 Knapsack problem
# Returns the maximum value that can be put in a knapsack of capacity W
import numpy as np
from ortools.algorithms import pywrapknapsack_solver
def knapsack(W, wt, val, n):
K = [[0 for x in range(W+1)] for x in range(n+1)]
# Build table K[][] in bottom up manner
for i in range(n+1):
for w in range(W+1):
if i==0 or w==0:
K[i][w] = 0
elif wt[i-1] <= w:
K[i][w] = max(val[i-1] + K[i-1][w-wt[i-1]], K[i-1][w])
else:
K[i][w] = K[i-1][w]
best = K[n][W]
amount = np.zeros(n)
a = best
j = n
Y = W
# j = j + 1;
#
# amount(j) = 1;
# Y = Y - weights(j);
# j = j - 1;
# a = A(j + 1, Y + 1);
while a > 0:
while K[j][Y] == a:
j = j - 1
j = j + 1
amount[j-1] = 1
Y = Y - wt[j-1]
j = j - 1
a = K[j][Y]
return amount
def test_knapsack():
weights = [1 ,1 ,1, 1 ,2 ,2 ,3]
values = [1 ,1 ,2 ,3, 1, 3 ,5]
best = 13
print(knapsack(7, weights, values, 7))
#===========================================
'''
------------------------------------------------
Use dynamic programming (DP) to solve 0/1 knapsack problem
Time complexity: O(nW), where n is number of items and W is capacity
Author: Kaiyang Zhou
Website: https://kaiyangzhou.github.io/
------------------------------------------------
knapsack_dp(values,weights,n_items,capacity,return_all=False)
Input arguments:
1. values: a list of numbers in either int or float, specifying the values of items
2. weights: a list of int numbers specifying weights of items
3. n_items: an int number indicating number of items
4. capacity: an int number indicating the knapsack capacity
5. return_all: whether return all info, defaulty is False (optional)
Return:
1. picks: a list of numbers storing the positions of selected items
2. max_val: maximum value (optional)
------------------------------------------------
'''
def knapsack_dp(values,weights,n_items,capacity,return_all=False):
check_inputs(values,weights,n_items,capacity)
table = np.zeros((n_items+1,capacity+1),dtype=np.float32)
keep = np.zeros((n_items+1,capacity+1),dtype=np.float32)
for i in range(1,n_items+1):
for w in range(0,capacity+1):
wi = weights[i-1] # weight of current item
vi = values[i-1] # value of current item
if (wi <= w) and (vi + table[i-1,w-wi] > table[i-1,w]):
table[i,w] = vi + table[i-1,w-wi]
keep[i,w] = 1
else:
table[i,w] = table[i-1,w]
picks = []
K = capacity
for i in range(n_items,0,-1):
if keep[i,K] == 1:
picks.append(i)
K -= weights[i-1]
picks.sort()
picks = [x-1 for x in picks] # change to 0-index
if return_all:
max_val = table[n_items,capacity]
return picks,max_val
return picks
def check_inputs(values,weights,n_items,capacity):
# check variable type
assert(isinstance(values,list))
assert(isinstance(weights,list))
assert(isinstance(n_items,int))
assert(isinstance(capacity,int))
# check value type
assert(all(isinstance(val,int) or isinstance(val,float) for val in values))
assert(all(isinstance(val,int) for val in weights))
# check validity of value
assert(all(val >= 0 for val in weights))
assert(n_items > 0)
assert(capacity > 0)
def test_knapsack_dp():
values = [2,3,4]
weights = [1,2,3]
n_items = 3
capacity = 3
picks = knapsack_dp(values,weights,n_items,capacity)
print (picks)
osolver = pywrapknapsack_solver.KnapsackSolver(
# pywrapknapsack_solver.KnapsackSolver.KNAPSACK_MULTIDIMENSION_BRANCH_AND_BOUND_SOLVER,
pywrapknapsack_solver.KnapsackSolver.KNAPSACK_DYNAMIC_PROGRAMMING_SOLVER,
'test')
def knapsack_ortools(values, weights, items, capacity ):
scale = 1000
values = np.array(values)
weights = np.array(weights)
values = (values * scale).astype(np.int)
weights = (weights).astype(np.int)
capacity = capacity
osolver.Init(values.tolist(), [weights.tolist()], [capacity])
computed_value = osolver.Solve()
packed_items = [x for x in range(0, len(weights))
if osolver.BestSolutionContains(x)]
return packed_items
if __name__ == "__main__":
test_knapsack_dp()
test_knapsack()