-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmyalgorithm.py
More file actions
285 lines (199 loc) · 9.5 KB
/
myalgorithm.py
File metadata and controls
285 lines (199 loc) · 9.5 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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
from util import *
import itertools
import math
import gurobipy as gp
from gurobipy import GRB
from pulp import LpProblem, LpMinimize, LpVariable, lpSum, LpBinary, value, PULP_CBC_CMD
def solve_with_pulp(bike_bundles, walk_bundles, car_bundles, all_orders, all_riders):
solution = []
model = LpProblem("OGC", LpMinimize)
I = list(range(0, len(bike_bundles)))
J = list(range(0, len(walk_bundles)))
K = list(range(0, len(car_bundles)))
all_order_id = list(range(0, len(all_orders)))
bundles = []
for i in I:
bundles.append(bike_bundles[i].shop_seq)
for j in J:
bundles.append(walk_bundles[j].shop_seq)
for k in K:
bundles.append(car_bundles[k].shop_seq)
x = LpVariable.dicts("x", I, 0, 1, LpBinary)
y = LpVariable.dicts("y", J, 0, 1, LpBinary)
z = LpVariable.dicts("z", K, 0, 1, LpBinary)
xyz = {i: x[i] for i in I}
xyz.update({j + len(bike_bundles): y[j] for j in J})
xyz.update({k + len(bike_bundles) + len(walk_bundles): z[k] for k in K})
# Objective function
model += lpSum((all_riders[0].fixed_cost + all_riders[0].var_cost * bike_bundles[i].total_dist / 100) * x[i] for i in I) + \
lpSum((all_riders[1].fixed_cost + all_riders[1].var_cost * walk_bundles[j].total_dist / 100) * y[j] for j in J) + \
lpSum((all_riders[2].fixed_cost + all_riders[2].var_cost * car_bundles[k].total_dist / 100) * z[k] for k in K)
# Constraints
model += lpSum(x[i] for i in I) <= all_riders[0].available_number
model += lpSum(y[j] for j in J) <= all_riders[1].available_number
model += lpSum(z[k] for k in K) <= all_riders[2].available_number
for order in all_order_id:
model += lpSum(xyz[i] for i, subset in enumerate(bundles) if order in subset) == 1
# Solve the model
model.solve()
index_x = [i for i in I if value(x[i]) == 1]
index_y = [j for j in J if value(y[j]) == 1]
index_z = [k for k in K if value(z[k]) == 1]
# Construct the solution
for i in index_x:
solution.append([all_riders[0].type, bike_bundles[i].shop_seq, bike_bundles[i].dlv_seq])
for j in index_y:
solution.append([all_riders[1].type, walk_bundles[j].shop_seq, walk_bundles[j].dlv_seq])
for k in index_z:
solution.append([all_riders[2].type, car_bundles[k].shop_seq, car_bundles[k].dlv_seq])
return solution
def solve_lp(all_orders, all_riders, bike_bundles, walk_bundles, car_bundles):
solution = []
model = gp.Model("OGC")
I = list(range(0,len(bike_bundles)))
J = list(range(0,len(walk_bundles)))
K = list(range(0,len(car_bundles)))
all_order_id = list(range(0,len(all_orders)))
bundles = []
for i in I:
bundles.append(bike_bundles[i].shop_seq)
for j in J:
bundles.append(walk_bundles[j].shop_seq)
for k in K:
bundles.append(car_bundles[k].shop_seq)
x = model.addVars([i for i in I], vtype=GRB.BINARY, name="x")
y = model.addVars([j for j in J], vtype=GRB.BINARY, name="y")
z = model.addVars([k for k in K], vtype=GRB.BINARY, name="z")
xyz = {i: x[i] for i in I}
xyz.update({j + len(bike_bundles): y[j] for j in J})
xyz.update({k + len(bike_bundles) + len(walk_bundles): z[k] for k in K})
model.setObjective((gp.quicksum((all_riders[0].fixed_cost + all_riders[0].var_cost*bike_bundles[i].total_dist/100)*x[i] for i in I) +
gp.quicksum((all_riders[1].fixed_cost + all_riders[1].var_cost*walk_bundles[j].total_dist/100)*y[j] for j in J) +
gp.quicksum((all_riders[2].fixed_cost + all_riders[2].var_cost*car_bundles[k].total_dist/100)*z[k] for k in K)),
sense=GRB.MINIMIZE)
model.addConstr(gp.quicksum(x[i] for i in I) <= all_riders[0].available_number)
model.addConstr(gp.quicksum(y[j] for j in J) <= all_riders[1].available_number)
model.addConstr(gp.quicksum(z[k] for k in K) <= all_riders[2].available_number)
for order in all_order_id:
model.addConstr(gp.quicksum(xyz[i] for i, subset in enumerate(bundles) if order in subset) == 1)
model.optimize()
index_x = []
for i in I:
if x[i].X ==1:
index_x.append(i)
index_y = []
for i in J:
if y[i].X ==1:
index_y.append(i)
index_z = []
for i in K:
if z[i].X ==1:
index_z.append(i)
# Solution is a list of bundle information
for i in index_x:
solution.append([all_riders[0].type, bike_bundles[i].shop_seq, bike_bundles[i].dlv_seq])
for i in index_y:
solution.append([all_riders[1].type, walk_bundles[i].shop_seq, walk_bundles[i].dlv_seq])
for i in index_z:
solution.append([all_riders[2].type, car_bundles[i].shop_seq, car_bundles[i].dlv_seq])
return solution
def bundling(all_orders, all_riders, K, dist_mat, rider_type, rider_type_num, max_bundle, W):
bnum = range(max_bundle+1)
l = range(len(all_orders))
weight_matrix = [[0 for _ in l] for _ in l]
memo_no_merge_order = {i:[i] for i in l}
bundles = {i: [] for i in bnum}
# Single-order bundle -> feasible check needed
bundles[0] = [
Bundle(
all_orders,
rider=all_riders[rider_type_num],
shop_seq=[i],
dlv_seq=[i],
total_volume=all_orders[i].volume,
total_dist=get_total_distance(K, dist_mat, [i], [i]),
# feasible=assign_booltype_feasibility(test_route_feasibility(all_orders, all_riders[rider_type_num], [i], [i]))
) for i in l if test_route_feasibility(all_orders, all_riders[rider_type_num], [i], [i]) == 0
]
count = 0
while count < max_bundle:
for bundle1 in bundles[count]:
for og_bundle in bundles[0]:
ord_id = og_bundle.shop_seq[0]
if og_bundle.feasible != True:
continue
flag = False
for ord in bundle1.shop_seq:
if ord_id in memo_no_merge_order[ord]:
flag = True
break
# hard
# for ord in bundle1.shop_seq:
# if ord in memo_no_merge_order[idx]:
# flag = True
# continue
if flag:
continue
new_bundle = try_merging_bundles(K, dist_mat, all_orders, bundle1, og_bundle)
# new_bundle = VRP()
if new_bundle is not None:
bundles[count + 1].append(new_bundle)
else:
# soft
# for ord in bundle1.shop_seq:s
# memo_no_merge_order[ord].append(idx)
# weight
for ord in bundle1.shop_seq:
if count == 0:
weight_matrix[ord][ord_id] = 1
weight_matrix[ord_id][ord] = 1
memo_no_merge_order[ord].append(ord_id)
memo_no_merge_order[ord_id].append(ord)
else:
weight_matrix[ord][ord_id] += W
if weight_matrix[ord][ord_id] >= 1:
memo_no_merge_order[ord].append(ord_id)
# hard
# for ord in bundle1.shop_seq:
# memo_no_merge_order[idx].append(ord)
count += 1
# for key in bundles.keys():
# print(f"Length of {rider_type}-bundle {key+1}-orders: {len(bundles[key])}")
# Result Export
bundling_result = []
for bundle in bundles.values():
bundling_result += bundle
return bundling_result, weight_matrix
def algorithm(K, all_orders, all_riders, dist_mat, timelimit=60):
start_time = time.time()
for r in all_riders:
r.T = np.round(dist_mat/r.speed + r.service_time)
# A solution is a list of bundles
solution = []
#------------- Custom algorithm code starts from here --------------#
N = 4 # 최대 오더 개수: N+1
bundles = {}
weight = {}
for idx, rider in enumerate(all_riders):
bundles[rider.type], weight[rider.type] = bundling( all_orders=all_orders,
all_riders=all_riders,
K=K,
dist_mat=dist_mat,
rider_type=rider.type,
rider_type_num=idx,
max_bundle=N,
W=0.1 )
# solve
# solution = solve_lp(all_orders=all_orders,
# all_riders=all_riders,
# bike_bundles=bundles["BIKE"],
# walk_bundles=bundles["WALK"],
# car_bundles=bundles["CAR"])
# solve with pulp fuck gurobipy
# solution = solve_with_pulp(all_orders=all_orders,
# all_riders=all_riders,
# bike_bundles=bundles["BIKE"],
# walk_bundles=bundles["WALK"],
# car_bundles=bundles["CAR"])
#------------- End of custom algorithm code--------------#
return solution, bundles, weight