-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathknapsack encapsulate.py
152 lines (130 loc) · 3.82 KB
/
knapsack encapsulate.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
class dinamic:
def getMax(self, group, row, groups, n):
max_ = 0
for i in range(1,n+1):
if groups[i] == group:
if row[i] > max_:
max_ = row[i]
return max_
def IsMaxInGroup(self, n, w, groups, matrix, N):
group = groups[n]
max_ = 0;
for i in range(1, N+1):
if groups[i] == group:
if matrix[w][i] > max_:
max_ = matrix[w][i]
if matrix[w][n] != max_:
return False
return True
W = 10
profit = [0,0,3,4,5,0,4,6,10,10,5,8,12,18,17,10,12,18,30,24,15,20,27,44,30]
weight = [0,0,1,2,5,0,1,2,5,10,1,2,4,9,17,2,3,6,15,24,3,5,9,22,30]
group = [0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 2, 2, 2, 2, 2, 3, 3, 3, 3, 3, 4, 4, 4, 4, 4]
max_profit = 0
def MCKS(profit, weight, group):
global max_profit
N = len(profit) - 1
din = dinamic()
matrix = [] # W + 1, N + 1
sol = [] # W + 1, N + 1
for i in range(W + 1):
temp = []
for j in range(N + 1):
temp.append(0)
matrix.append(temp)
for i in range(W + 1):
temp = []
for j in range(N + 1):
temp.append(False)
sol.append(temp)
for i in range(W + 1):
matrix[i][0] = i
for w in range(1, W + 1):
for n in range(1, N + 1):
if group[n] == 0:
if weight[n] <= w:
matrix[w][n] = profit[n];
sol[w][n] = True
else:
if group[n] != group[n-1]:
option1 = din.getMax(group[n-1], matrix[w], group, n)
option2 = -10000000
if weight[n] <= w:
option2 = profit[n] + din.getMax(group[n-1], matrix[w-weight[n]], group, n)
matrix[w][n] = max(option1, option2)
if option2 > option1:
sol[w][n] = True
else:
sol[w][n] = False
if group[n] == group[n-1]:
option1 = din.getMax(group[n]-1, matrix[w], group, n)
option2 = -10000000
if weight[n] <= w:
option2 = profit[n] + din.getMax(group[n]-1, matrix[w-weight[n]], group, n)
matrix[w][n] = max(option2, option1)
if option2 > option1:
sol[w][n] = True
else:
sol[w][n] = False
take = []
for i in range(N + 1):
take.append(False)
for n in range(N, 0, -1):
w = W
if sol[w][n] and din.IsMaxInGroup(n,w,group, matrix, N):
take[n] = True
w = w - weight[i]
else:
take[n] = False
max_group = max(group)
group_id = []
for i in range(len(group)):
if max_group == group[i]:
group_id.append(i)
max_profit = max(matrix[W])
max_profit_id = []
temp_profit_max_list = []
for i in range(len(group_id)):
temp_profit_max_list.append(matrix[W][group_id[i]])
for i in range(len(group_id)):
if max_profit == matrix[W][group_id[i]]:
max_profit_id.append(group_id[i])
print("max profit: " + str(max_profit))
rest_profit = max_profit
rest_weight = W
group_number = max_group
func(rest_weight, group_number, rest_profit, "", matrix)
temp_result = []
def get_result(result):
temp_result.append(result)
def func(rest_weight,group_number,rest_profit , path, matrix): # rest_weiht = row
temp_result = []
if(rest_weight<=0 or rest_profit <=0):
# print(path)
get_result(path)
# print(temp_result)
return True
find_profit = rest_profit
find_profit_id = []
for i in range(len(profit)):
if (group[i] == group_number) and (find_profit == matrix[rest_weight][i]):
find_profit_id.append(i)
for i in range(len(find_profit_id)):
temp_result.append(find_profit_id[i]+1)
next_path = path + "%d," % (find_profit_id[i]+1)
next_profit = rest_profit - profit[find_profit_id[i]]
next_weight = rest_weight - weight[find_profit_id[i]]
func(next_weight,group_number-1,next_profit,next_path, matrix)
MCKS(profit, weight, group)
# print(temp_result)
final_result = []
for i in range(len(temp_result)):
if i == 0:
final_result.append(max_profit)
temp = []
for j in range(len(temp_result[i].split(","))):
if j < len(temp_result[i].split(","))-1:
temp.append(int(temp_result[i].split(",")[j]))
final_result.append(temp)
print(final_result)
#final result array is final_result. you can use this array for output.