-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathq3-1.py
More file actions
64 lines (55 loc) · 2.06 KB
/
q3-1.py
File metadata and controls
64 lines (55 loc) · 2.06 KB
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
'''
In this programming problem and the next you'll code up the knapsack algorithm from lecture. Let's start with a warm-up. Download the text file here. This file describes a knapsack instance, and it has the following format:
[knapsack_size][number_of_items]
[value_1] [weight_1]
[value_2] [weight_2]
...
For example, the third line of the file is "50074 659", indicating that the second item has value 50074 and size 659, respectively.
You can assume that all numbers are positive. You should assume that item weights and the knapsack capacity are integers.
In the box below, type in the value of the optimal solution.
ADVICE: If you're not getting the correct answer, try debugging your algorithm using some small test cases. And then post them to the discussion forum!
answer to question 1: 2493893
'''
import numpy as np
def buildKmatric(value,weight,wlimit):
nv = len(value)+1
nw = wlimit+1
ma = np.zeros([nv,nw],dtype=int)
count = 0
for i in range(1,nv):
# the matrix starts from 0 to nv but value/weight starts from 1 to nv
id = i-1
for j in range(nw):
if j < weight[id]:
ma[i,j] = ma[i-1,j]
else:
ma[i,j] = max(ma[i-1,j],ma[i-1,j-weight[id]]+value[id])
return ma
# reconstruct the item in use
def finditem(ma,value,weight):
#print ma
items = []
target = ma[-1,-1]
i = len(ma)-1
j = len(ma[0])-1
while target > 0:
id = np.where(ma[:,j]==target)
i = id[0][0]-1
print '(',value[i],',',weight[i],')'
j = j-weight[i]
target = ma[i,j]
# return the maximum weight
def printweight(ma):
print 'maximum weight:',ma[-1,-1]
if __name__ == "__main__":
data = np.loadtxt('knapsack1.txt')
wlimit = int(data[0,0])
nelement = int(data[0,1])
data = np.int_(data[1:])
# sanity check
if len(data) != nelement:
print "!!wrong number of items!!"
exit()
ma = buildKmatric(data[:,0],data[:,1],wlimit)
finditem(ma,data[:,0],data[:,1])
printweight(ma)