-
Notifications
You must be signed in to change notification settings - Fork 6
/
tsp_multiple_days.py
283 lines (229 loc) · 11.6 KB
/
tsp_multiple_days.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
from ortools.constraint_solver import pywrapcp
from ortools.constraint_solver import routing_enums_pb2
from functools import partial
from datetime import datetime, time, timedelta
import argparse
import time_details as T
def timedelta_format(td):
ts = int(td.total_seconds())
tm = time(hour=(ts//3600),second=(ts%3600//60))
return tm.strftime("%H:%S")
def main():
parser = argparse.ArgumentParser(description='Solve routing problem to a fixed set of destinations')
parser.add_argument('--days', type=int, dest='days', default=2,
help='Number of days to schedule. Default is 2 days')
parser.add_argument('--start', type=int, dest='start', default=6,
help='The earliest any trip can start on any day, in hours. Default 6')
parser.add_argument('--end', type=int, dest='end', default=18,
help='The earliest any trip can end on any day, in hours. Default 18 (which is 6pm)')
parser.add_argument('--waittime', type=int, dest='service', default=30,
help='The time required to wait at each visited node, in minutes. Default is 30')
parser.add_argument('-t, --timelimit', type=int, dest='timelimit', default=10,
help='Maximum run time for solver, in seconds. Default is 10 seconds.')
parser.add_argument('--debug', action='store_true', dest='debug', default=False,
help="Turn on solver logging.")
parser.add_argument('--no_guided_local', action='store_true', dest='guided_local_off',
default=False,
help='Whether or not to use the guided local search metaheuristic')
parser.add_argument('--skip_mornings', action='store_true', dest='skip_mornings',
default=False,
help='Whether or not to use dummy morning nodes. Default is true')
args = parser.parse_args()
day_start = args.start * 3600
day_end = args.end * 3600
if args.days <= 0:
print("--days parameter must be 1 or more")
assert args.days > 0
num_days = args.days - 1
node_service_time = args.service * 60
overnight_time = (day_start - day_end) # -18*3600 #
disjunction_penalty = 10000000
Slack_Max = (day_end - day_start) - day_start # night node demand minus no-work day
# 3600*24
Capacity = day_end # most time that can be collected in one day
num_nodes = T.num_nodes()
# create dummy nodes for returning to the depot every night
night_nodes = range(num_nodes, num_nodes+num_days)
# create dummy nodes linked to night nodes that fix the AM depart time
morning_nodes = range(num_nodes+num_days, num_nodes+num_days+num_days)
if args.skip_mornings:
morning_nodes = []
total_nodes = num_nodes + len(night_nodes) +len(morning_nodes)
# Create the routing index manager.
manager = pywrapcp.RoutingIndexManager(total_nodes, 1, [0], [1])
print('made manager with total nodes {} = {} + {} + {}'.format(total_nodes,
num_nodes,
len(night_nodes),
len(morning_nodes)))
# Create Routing Model.
# use precaching on OR-Tools side. So Much Faster
model_parameters = pywrapcp.DefaultRoutingModelParameters()
model_parameters.max_callback_cache_size = 2 * total_nodes * total_nodes
routing = pywrapcp.RoutingModel(manager, model_parameters)
# routing = pywrapcp.RoutingModel(manager)
transit_callback_fn = partial(T.transit_callback,
manager,
day_end,
night_nodes,
morning_nodes)
transit_callback_index = routing.RegisterTransitCallback(transit_callback_fn)
routing.SetArcCostEvaluatorOfAllVehicles(transit_callback_index)
print('set the arc cost evaluator for all vehicles')
time_callback_fn = partial(T.time_callback,
manager,
node_service_time,
overnight_time,
night_nodes,
morning_nodes)
time_callback_index = routing.RegisterTransitCallback(time_callback_fn)
# Define cost of each arc.
# create time dimension
routing.AddDimension(
time_callback_index,
Slack_Max, # An upper bound for slack (the wait times at the locations).
Capacity, # An upper bound for the total time over each vehicle's route.
False, # Determine whether the cumulative variable is set to zero at the start of the vehicle's route.
'Time')
time_dimension = routing.GetDimensionOrDie('Time')
print('created time dimension')
# get rid of slack for all regular nodes, all morning nodes
# but keep for depot, night nodes
for node in range(2, num_nodes):
index = manager.NodeToIndex(node)
time_dimension.SlackVar(index).SetValue(0)
for node in morning_nodes:
index = manager.NodeToIndex(node)
time_dimension.SlackVar(index).SetValue(0)
# Allow all locations except the first two to be droppable.
for node in range(2, num_nodes):
routing.AddDisjunction([manager.NodeToIndex(node)], disjunction_penalty)
# Allow all overnight nodes to be dropped for free
for node in night_nodes:
routing.AddDisjunction([manager.NodeToIndex(node)], 0)
for node in morning_nodes:
routing.AddDisjunction([manager.NodeToIndex(node)], 0)
# Add time window constraints for each regular node
for node in range(2,num_nodes):
index = manager.NodeToIndex(node)
time_dimension.CumulVar(index).SetRange(day_start, day_end)
# This also applies to the overnight nodes and morning nodes
for node in range(num_nodes, total_nodes):
index = manager.NodeToIndex(node)
time_dimension.CumulVar(index).SetRange(day_start, day_end)
# Add time window constraints for each vehicle start/end node.
for veh in range(0,1):
index = routing.Start(veh)
time_dimension.CumulVar(index).SetMin(day_start)
index = routing.End(veh)
time_dimension.CumulVar(index).SetMax(day_end)
print('done with time constraints')
# make sure the days happen in order. first end day 1, end day 2, etc, then node 1
# create counting dimension
routing.AddConstantDimension(1, # increment by 1
total_nodes+1, # the max count is visit every node
True, # start count at zero
"Counting")
count_dimension = routing.GetDimensionOrDie('Counting')
print('created count dim')
# use count dim to enforce ordering of overnight, morning nodes
solver = routing.solver()
for i in range(len(night_nodes)):
inode = night_nodes[i]
iidx = manager.NodeToIndex(inode)
iactive = routing.ActiveVar(iidx)
for j in range(i+1, len(night_nodes)):
# make i come before j using count dimension
jnode = night_nodes[j]
jidx = manager.NodeToIndex(jnode)
jactive = routing.ActiveVar(jidx)
solver.Add(iactive >= jactive)
solver.Add(count_dimension.CumulVar(iidx) * iactive * jactive <=
count_dimension.CumulVar(jidx) * iactive * jactive)
# if night node is active, AND night_node is not the last night,
# must transition to corresponding morning node
if i < len(morning_nodes):
i_morning_idx = manager.NodeToIndex(morning_nodes[i])
i_morning_active = routing.ActiveVar(i_morning_idx)
solver.Add(iactive == i_morning_active)
solver.Add(count_dimension.CumulVar(iidx) + 1 ==
count_dimension.CumulVar(i_morning_idx))
for i in range(len(morning_nodes)):
inode = morning_nodes[i]
iidx = manager.NodeToIndex(inode)
iactive = routing.ActiveVar(iidx)
for j in range(i+1, len(morning_nodes)):
# make i come before j using count dimension
jnode = morning_nodes[j]
jidx = manager.NodeToIndex(jnode)
jactive = routing.ActiveVar(jidx)
solver.Add(iactive >= jactive)
solver.Add(count_dimension.CumulVar(iidx) * iactive * jactive <=
count_dimension.CumulVar(jidx) * iactive * jactive)
print('done setting up ordering constraints between days')
# Instantiate route start and end times to produce feasible times.
# routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.Start(0)))
# routing.AddVariableMinimizedByFinalizer(time_dimension.CumulVar(routing.End(0)))
# Setting first solution heuristic.
search_parameters = pywrapcp.DefaultRoutingSearchParameters()
#search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
search_parameters.first_solution_strategy = (routing_enums_pb2.FirstSolutionStrategy.PARALLEL_CHEAPEST_INSERTION)
print('set up the setup. Total nodes is ', total_nodes, ' and real nodes is ', num_nodes)
# Setting local search metaheuristics:
if not args.guided_local_off:
search_parameters.local_search_metaheuristic = (routing_enums_pb2.LocalSearchMetaheuristic.GUIDED_LOCAL_SEARCH)
search_parameters.time_limit.seconds = args.timelimit
search_parameters.log_search = args.debug
# Solve the problem.
solution = routing.SolveWithParameters(search_parameters)
if not solution:
print("no solution found")
else:
print("solution found. Objective value is ",solution.ObjectiveValue())
# Print the results
result = {
'Dropped': [],
'Scheduled': []
}
# Return the dropped locations
for index in range(routing.Size()):
if routing.IsStart(index) or routing.IsEnd(index):
continue
node = manager.IndexToNode(index)
if node in night_nodes or node in morning_nodes:
continue
if solution.Value(routing.NextVar(index)) == index:
result['Dropped'].append(node)
# Return the scheduled locations
cumultime = 0
index = routing.Start(0)
while not routing.IsEnd(index):
cumultime = time_dimension.CumulVar(index)
count = count_dimension.CumulVar(index)
node = manager.IndexToNode(index)
if node in night_nodes:
node = 'Overnight at {}, dummy for 1'.format(node)
if node in morning_nodes:
node = 'Starting day at {}, dummy for 1'.format(node)
mintime = timedelta(seconds=solution.Min(cumultime))
maxtime = timedelta(seconds=solution.Max(cumultime))
result['Scheduled'].append([node, solution.Value(count),
timedelta_format(mintime),
timedelta_format(maxtime)])
index = solution.Value(routing.NextVar(index))
cumultime = time_dimension.CumulVar(index)
count = count_dimension.CumulVar(index)
mintime = timedelta(seconds=solution.Min(cumultime))
maxtime = timedelta(seconds=solution.Max(cumultime))
result['Scheduled'].append([manager.IndexToNode(index),
solution.Value(count),
timedelta_format(mintime),
timedelta_format(maxtime)])
print('Dropped')
print(result['Dropped'])
print('Scheduled')
print('[node, order, min time, max time]')
for line in result['Scheduled']:
print(line)
#print(result)
if __name__ == '__main__':
main()