-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathalgorithms.py
More file actions
executable file
·249 lines (237 loc) · 8.25 KB
/
algorithms.py
File metadata and controls
executable file
·249 lines (237 loc) · 8.25 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
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
import time
import random
def standard_greedy(capacity, weights, prices): # from fractional knapsack
start = time.time()
values = list()
for i in range(len(weights)):
values.append((weights[i] / prices[i], weights[i], prices[i]))
values.sort(reverse = False)
knapsack = list()
tot_val = 0
tot_weight = 0
for value in values:
if value[1] + tot_weight <= capacity:
knapsack.append(value)
tot_val += value[2]
tot_weight += value[1] # exit when 100% full OR some % full???
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime)
def limited_greedy(capacity, weights, prices): # original
start = time.time()
values = list()
for i in range(len(weights)):
values.append((prices[i], weights[i]))
values.sort(reverse = True)
knapsack = list()
tot_val = 0
tot_weight = 0
capacity_left = capacity
for value in values:
if capacity_left > 0.4 * capacity and value[1] <= capacity_left * 0.8:
knapsack.append(value)
tot_val += value[0]
tot_weight += value[1]
capacity_left -= value[1]
elif capacity_left <= 0.4 * capacity and value[1] <= capacity_left:
knapsack.append(value)
tot_val += value[0]
tot_weight += value[1]
capacity_left -= value[1]
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime) # usually best
def heavy_greedy(capacity, weights, prices): # variation on standard greedy
start = time.time()
values = list()
for i in range(len(weights)):
values.append((prices[i], weights[i]))
values.sort(reverse = True)
knapsack = list()
tot_val = 0
tot_weight = 0
capacity_left = capacity
for value in values:
if value[1] <= capacity_left:
knapsack.append(value)
tot_val += value[0]
tot_weight += value[1]
capacity_left -= value[1]
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime)
def defensive_greedy(capacity, weights, prices): # original
start = time.time()
values = list()
for i in range(len(weights)):
values.append((weights[i], prices[i]))
values.sort()
knapsack = list()
tot_val = 0
tot_weight = 0
for value in values:
if value[0] + tot_weight <= capacity:
knapsack.append(value)
tot_val += value[1]
tot_weight += value[0]
else: # Once this value won't fit, the next ones are guaranteed not to due to the sorting. So, just break here.
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime)
def deal_stingy(capacity, weights, prices):
start = time.time()
values = list()
tot_val = 0
tot_weight = 0
for i in range(len(weights)):
values.append(((prices[i] ** 3) / (weights[i] ** 1.5), weights[i], prices[i]))
tot_val += prices[i]
tot_weight += weights[i]
values.sort(reverse = True)
while tot_weight > capacity:
tot_val -= values[-1][2]
tot_weight -= values[-1][1]
values.pop()
end = time.time()
runtime = end - start
return (values, tot_val, tot_weight, runtime)
def weight_stingy(capacity, weights, prices):
start = time.time()
values = list()
tot_val = 0
tot_weight = 0
for i in range(len(weights)):
values.append((weights[i], prices[i]))
tot_val += prices[i]
tot_weight += weights[i]
values.sort(reverse = False)
while tot_weight > capacity:
tot_val -= values[-1][1]
tot_weight -= values[-1][0]
values.pop()
end = time.time()
runtime = end - start
return (values, tot_val, tot_weight, runtime)
def sliding_threshold(capacity, weights, prices):
start = time.time()
w = random.randint(0, len(weights) - 1)
threshold = prices[w] / weights[w]
knapsack = list()
tot_val = 0
tot_weight = 0
for i in range(len(weights)):
if i > len(weights) / 4 and len(knapsack) <= len(weights) / 8: # TODO: compare to capacity left
threshold *= 0.8
elif i > len(weights) / 2 and len(knapsack) <= len(weights) / 4:
threshold *= 0.8
if prices[i] / weights[i] >= threshold and tot_weight + weights[i] <= capacity:
tot_val += prices[i]
tot_weight += weights[i]
knapsack.append((weights[i], prices[i]))
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime)
def scored_greedy(capacity, weights, prices): # from fractional knapsack
start = time.time()
values = list()
for i in range(len(weights)):
values.append(((prices[i] ** 3) / (weights[i] ** 1.5), weights[i], prices[i])) # TODO: change the score
values.sort(reverse = True)
knapsack = list()
tot_val = 0
tot_weight = 0
for value in values:
if value[1] + tot_weight <= capacity:
knapsack.append(value)
tot_val += value[2]
tot_weight += value[1]
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime)
def transitioning_greedy(capacity, weights, prices): # original
start = time.time()
values = list()
for i in range(len(weights)):
values.append((prices[i], weights[i]))
values.sort(reverse = True)
knapsack = list()
tot_val = 0
tot_weight = 0
capacity_left = capacity
for value in values:
if capacity_left > 0.6 * capacity and value[1] <= capacity_left * 0.4:
knapsack.append(value)
tot_val += value[0]
tot_weight += value[1]
capacity_left -= value[1]
values.remove(value)
elif capacity_left <= 0.6 * capacity:
break
new_values = list()
for i in range(len(values)):
new_values.append(((values[i][0] ** 3) / (values[i][1] ** 1.5), values[i][0], values[i][1]))
new_values.sort(reverse = True)
for value in new_values:
if capacity_left - value[2] > 0:
knapsack.append(value)
tot_val += value[1]
tot_weight += value[2]
capacity_left -= value[2]
if tot_weight == capacity:
break
end = time.time()
runtime = end - start
return (knapsack, tot_val, tot_weight, runtime) # best
def max_of_others(capacity, weights, prices):
start = time.time()
knapsacks = list()
knapsacks.append(standard_greedy(capacity, weights, prices))
knapsacks.append(limited_greedy(capacity, weights, prices))
knapsacks.append(heavy_greedy(capacity, weights, prices))
knapsacks.append(defensive_greedy(capacity, weights, prices))
knapsacks.append(deal_stingy(capacity, weights, prices))
knapsacks.append(weight_stingy(capacity, weights, prices))
knapsacks.append(sliding_threshold(capacity, weights, prices))
knapsacks.append(scored_greedy(capacity, weights, prices))
knapsacks.append(transitioning_greedy(capacity, weights, prices))
max_price = 0
maxknap = None
for knapsack in knapsacks:
if knapsack[1] > max_price:
maxknap = knapsack
max_price = knapsack[1]
end = time.time()
runtime = end - start
try:
return (maxknap[0], maxknap[1], maxknap[2], runtime)
except TypeError:
return ([(0,)], 0, 0, runtime)
# TODO: add max of just heavy and standard
def max_of_two(capacity, weights, prices):
start = time.time()
knapsacks = list()
knapsacks.append(standard_greedy(capacity, weights, prices))
knapsacks.append(heavy_greedy(capacity, weights, prices))
max_price = 0
maxknap = None
for knapsack in knapsacks:
if knapsack[1] > max_price:
maxknap = knapsack
max_price = knapsack[1]
end = time.time()
runtime = end - start
try:
return (maxknap[0], maxknap[1], maxknap[2], runtime)
except TypeError:
return ([(0,)], 0, 0, runtime)