-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathsolve_tsp_callback.py
305 lines (248 loc) · 8.68 KB
/
solve_tsp_callback.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
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
#!/usr/bin/env python
import sys
import time
import datetime
import math
from gurobipy import *
from collections import defaultdict
from dfs import *
import os
NODE_TYPE_DEPOT = 0
NODE_TYPE_CUST = 1
TYPE_TRUCK = 1
TYPE_UAV = 2
TRAVEL_UAV_PACKAGE = 1
TRAVEL_UAV_EMPTY = 2
TRAVEL_TRUCK_W_UAV = 3
TRAVEL_TRUCK_EMPTY = 4
VERTICAL_UAV_EMPTY = 5
VERTICAL_UAV_PACKAGE = 6
STATIONARY_UAV_EMPTY = 7
STATIONARY_UAV_PACKAGE = 8
STATIONARY_TRUCK_W_UAV = 9
STATIONARY_TRUCK_EMPTY = 10
GANTT_IDLE = 1
GANTT_TRAVEL = 2
GANTT_DELIVER = 3
GANTT_RECOVER = 4
GANTT_LAUNCH = 5
GANTT_FINISHED = 6
# There's a package color that corresponds to the VEHICLE that delivered the package.
packageIcons = ['', 'box_blue_centered.gltf', 'box_orange_centered.gltf', 'box_green_centered.gltf', 'box_gray_centered.gltf', 'box_brown_centered.gltf']
# =============================================================
# http://stackoverflow.com/questions/635483/what-is-the-best-way-to-implement-nested-dictionaries-in-python
def make_dict():
return defaultdict(make_dict)
# Usage:
# tau = defaultdict(make_dict)
# v = 17
# i = 3
# j = 12
# tau[v][i][j] = 44
class make_node:
def __init__(self, nodeType, latDeg, lonDeg, altMeters, parcelWtLbs, serviceTimeTruck, serviceTimeUAV, address):
# Set node[nodeID]
self.nodeType = nodeType
self.latDeg = latDeg
self.lonDeg = lonDeg
self.altMeters = altMeters
self.parcelWtLbs = parcelWtLbs
self.serviceTimeTruck = serviceTimeTruck # [seconds]
self.serviceTimeUAV = serviceTimeUAV # [seconds]
self.address = address # Might be None
class make_assignments:
def __init__(self, vehicleType, startTime, startNodeID, startLatDeg, startLonDeg, startAltMeters, endTime, endNodeID, endLatDeg, endLonDeg, endAltMeters, icon, description, UAVsOnBoard, ganttStatus):
# Set assignments[v][statusID][statusIndex]
self.vehicleType = vehicleType
self.startTime = startTime
self.startNodeID = startNodeID
self.startLatDeg = startLatDeg
self.startLonDeg = startLonDeg
self.startAltMeters = startAltMeters
self.endTime = endTime
self.endNodeID = endNodeID
self.endLatDeg = endLatDeg
self.endLonDeg = endLonDeg
self.endAltMeters = endAltMeters
self.icon = icon
self.description = description
self.UAVsOnBoard = UAVsOnBoard
self.ganttStatus = ganttStatus
class make_packages:
def __init__(self, packageType, latDeg, lonDeg, deliveryTime, icon):
# Set packages[nodeID]
self.packageType = packageType
self.latDeg = latDeg
self.lonDeg = lonDeg
self.deliveryTime = deliveryTime
self.icon = icon
def solve_tsp_callback(node, vehicle, travel):
# We want to return this collection of assignments and packages:
assignments = defaultdict(make_dict)
packages = defaultdict(make_dict)
# Establish system parameters:
C = []
N = []
N_zero = []
N_plus = []
tau = defaultdict(make_dict)
sigma = {}
c = 0
N.append(0) # Add the depot
N_zero.append(0)
for nodeID in node:
if (node[nodeID].nodeType == NODE_TYPE_CUST):
C.append(nodeID) # C is the vector of customer nodes. C = [1, 2, ..., c]
N.append(nodeID)
N_zero.append(nodeID)
N_plus.append(nodeID)
if (nodeID > c):
c = nodeID
N.append(c+1)
N_plus.append(c+1)
# We need to define node c+1, which is a copy of the depot.
#print node
node[c+1] = make_node(node[0].nodeType, node[0].latDeg, node[0].lonDeg, node[0].altMeters, node[0].parcelWtLbs, node[0].serviceTimeTruck, node[0].serviceTimeUAV, node[0].address)
# Build the customer service times:
for k in N_plus:
if (k == c+1):
sigma[k] = 0.0
else:
sigma[k] = node[k].serviceTimeTruck
cost = {}
# Build tau (truck travel time)
for vehicleID in vehicle:
for i in N_zero:
for j in C:
if (vehicle[vehicleID].vehicleType == TYPE_TRUCK):
tau[i][j] = travel[vehicleID][i][j].totalTime
cost[i,j] = travel[vehicleID][i][j].totalTime
# NOTE: We need to capture the travel time to node c+1 (which is the same physical location as node 0):
if (vehicle[vehicleID].vehicleType == TYPE_TRUCK):
tau[i][c+1] = travel[vehicleID][i][0].totalTime
cost[i,0] = travel[vehicleID][i][0].totalTime
# Callback - use lazy constraints to eliminate sub-tours
def subtourelim(model, where):
# this tells Gurobi to stop when it finds an integer solution
if where == GRB.Callback.MIPSOL:
# make a list of edges selected in the solution
delta_dfs = defaultdict(list)
for i in N_zero:
for j in N_zero:
if i != j:
x_sol = model.cbGetSolution(x[i,j])
if x_sol > 0.5:
delta_dfs[i].append(j)
delta_dfs[j].append(i)
# Here goes your DFS code for finding connected components
# use edges to construct the adjacency lists
component = dfs(N_zero, delta_dfs)
if (len(component) > 1):
# add subtour elimination constraints for every pair of nodes in the
# connected components you found. For each component add the cut
for k in component:
model.cbLazy(quicksum((quicksum(x[i,j] for j in component[k] if j != i)) for i in component[k]) <= len(component[k]) - 1)
# Generate a solution via DFJ - Callback:
m = Model("dfj_callback")
# Tell Gurobi not to print to a log file
m.params.OutputFlag = 0
# Create variables:
x = {}
for i in N_zero:
for j in N_zero:
if i != j:
x[i,j] = m.addVar(lb=0, obj=float(cost[i,j]), vtype=GRB.BINARY, name="x.%f.%f" % (i,j))
m.modelSense = GRB.MINIMIZE
m.Params.timeLimit = 600
m.update()
# add constraints
for i in N_zero:
m.addConstr(quicksum(x[i,j] for j in N_zero if j != i) == 1, "Constr.1.%f" % (i))
for j in N_zero:
m.addConstr(quicksum(x[i,j] for i in N_zero if i != j) == 1, "Constr.2.%f" % (j))
m.Params.lazyConstraints = 1
m.optimize(subtourelim)
myTour = [0]
tmp_i = 0
for tmploop in range(1,len(N_zero)):
for tmp_j in N_zero:
if tmp_i != tmp_j:
if x[tmp_i,tmp_j].x > 0.9:
myTour.append(tmp_j)
tmp_i = tmp_j
break
myTour.append(c+1)
# Build the assignment
vehicleType = TYPE_TRUCK
UAVsOnBoard = []
startAltMeters = 0.0
endAltMeters = 0.0
i = 0 # Start at the depot
mayEnd = 0
tmpDepart = 0.0
icon = 'ub_truck_1.gltf'
for myIndex in range(1,len(myTour)):
j = myTour[myIndex]
# We are traveling from i to j
# Capture the "traveling" component:
statusID = TRAVEL_TRUCK_EMPTY
ganttStatus = GANTT_TRAVEL
startTime = tmpDepart # When we departed from i
startNodeID = i
startLatDeg = node[i].latDeg
startLonDeg = node[i].lonDeg
endTime = startTime + tau[i][j] # This is when we arrive at j
endNodeID = j
endLatDeg = node[j].latDeg
endLonDeg = node[j].lonDeg
if ((i in C) and (j in C)):
description = 'Driving from Customer %d to Customer %d' % (i,j)
elif ((i == 0) and (j in C)):
description = 'Driving from Depot to Customer %d' % (j)
elif ((i in C) and (j == c+1)):
description = 'Returning to the Depot from Customer %d' % (i)
elif ((i == 0) and (j == c+1)):
description = 'Truck 1 was not used'
else:
print('WE HAVE A PROBLEM. What is the proper description?')
print('\t Quitting Now.')
exit()
if (0 in assignments[1][statusID]):
statusIndex = len(assignments[1][statusID])
else:
statusIndex = 0
assignments[1][statusID][statusIndex] = make_assignments(vehicleType, startTime, startNodeID, startLatDeg, startLonDeg, startAltMeters, endTime, endNodeID, endLatDeg, endLonDeg, endAltMeters, icon, description, UAVsOnBoard, ganttStatus)
# Now, capture the "service" component:
startTime = endTime # When we arrived at j
startNodeID = j
startLatDeg = node[j].latDeg
startLonDeg = node[j].lonDeg
endTime = startTime + sigma[j] # This is when we finish up at j
endNodeID = j
endLatDeg = node[j].latDeg
endLonDeg = node[j].lonDeg
objVal = endTime
if (j == c+1):
statusID = STATIONARY_TRUCK_EMPTY
ganttStatus = GANTT_FINISHED
tmpMin, tmpSec = divmod(endTime, 60)
tmpHour, tmpMin = divmod(tmpMin, 60)
description = 'Arrived at the Depot. Total Time = %d:%02d:%02d' % (tmpHour, tmpMin, tmpSec)
endTime = -1
else:
statusID = STATIONARY_TRUCK_EMPTY
ganttStatus = GANTT_DELIVER
description = 'Dropping off package to Customer %d' % (j)
packageType = TYPE_TRUCK
pkgIcon = packageIcons[1]
packages[j] = make_packages(packageType, endLatDeg, endLonDeg, endTime, pkgIcon)
if (0 in assignments[1][statusID]):
statusIndex = len(assignments[1][statusID])
else:
statusIndex = 0
assignments[1][statusID][statusIndex] = make_assignments(vehicleType, startTime, startNodeID, startLatDeg, startLonDeg, startAltMeters, endTime, endNodeID, endLatDeg, endLonDeg, endAltMeters, icon, description, UAVsOnBoard, ganttStatus)
tmpDepart = endTime
if (j != c+1):
# Go to the next arc
i = j
return (objVal, assignments, packages, myTour)