-
Notifications
You must be signed in to change notification settings - Fork 36
/
knapsack.py
executable file
·77 lines (65 loc) · 2.49 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
import numpy as np
'''
------------------------------------------------
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 xrange(1,n_items+1):
for w in xrange(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 xrange(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)
if __name__ == '__main__':
values = [2,3,4]
weights = [1,2,3]
n_items = 3
capacity = 3
picks = knapsack_dp(values,weights,n_items,capacity)
print picks