-
Notifications
You must be signed in to change notification settings - Fork 2
/
shortest_path_ortools.py
266 lines (219 loc) · 9.63 KB
/
shortest_path_ortools.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
#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
Shortest path using Google's ortools TSP solver
"""
import os
import sys
import argparse
import pandas as pd
import numpy as np
from random import randint
from ortools.constraint_solver import pywrapcp
# You need to import routing_enums_pb2 after pywrapcp!
from ortools.constraint_solver import routing_enums_pb2
from allocator.distance_matrix import (euclidean_distance_matrix,
haversine_distance_matrix,
osrm_distance_matrix)
class DistanceMatrix(object):
"""Random matrix."""
def __init__(self, A, args):
"""Initialize distance matrix."""
if args.distance_func == 'euclidean':
distances = euclidean_distance_matrix(A)
elif args.distance_func == 'haversine':
distances = haversine_distance_matrix(A)
elif args.distance_func == 'osrm':
distances = osrm_distance_matrix(A,
chunksize=args.osrm_max_table_size,
osrm_base_url=args.osrm_base_url)
(nx, ny) = distances.shape
self.matrix = {}
for from_node in range(nx):
self.matrix[from_node] = {}
for to_node in range(ny):
if from_node == to_node:
self.matrix[from_node][to_node] = 0
else:
self.matrix[from_node][to_node] = distances[from_node,
to_node]
def Distance(self, from_node, to_node):
return self.matrix[from_node][to_node]
def ortools_tsp(A, args):
# TSP of size args.tsp_size
# Second argument = 1 to build a single tour (it's a TSP).
# Nodes are indexed from 0 to parser_tsp_size - 1, by default the start of
# the route is node 0.
tsp_size = len(A)
routing = pywrapcp.RoutingModel(tsp_size, 1, 0)
search_parameters = pywrapcp.RoutingModel.DefaultSearchParameters()
# Setting first solution heuristic (cheapest addition).
search_parameters.first_solution_strategy = (
routing_enums_pb2.FirstSolutionStrategy.PATH_CHEAPEST_ARC)
# Setting the cost function.
# Put a callback to the distance accessor here. The callback takes two
# arguments (the from and to node inidices) and returns the distance
# between these nodes.
matrix = DistanceMatrix(A, args)
matrix_callback = matrix.Distance
routing.SetArcCostEvaluatorOfAllVehicles(matrix_callback)
# Solve, returns a solution if any.
# assignment = routing.SolveWithParameters(search_parameters)
assignment = routing.Solve()
path = []
cost = 0
if assignment:
# Solution cost.
print(assignment.ObjectiveValue())
cost = assignment.ObjectiveValue()
# Inspect solution.
# Only one route here; otherwise iterate from 0 to
# routing.vehicles() - 1
route_number = 0
node = routing.Start(route_number)
route = ''
while not routing.IsEnd(node):
route += str(node) + ' -> '
path.append(node)
node = assignment.Value(routing.NextVar(node))
route += '0'
path.append(0)
print(route)
else:
print('No solution found.')
return cost, path
def do_save_map(args, label, C):
try:
import folium
from folium.features import DivIcon
import requests
import polyline
a = ';'.join([','.join(str(p) for p in ll)
for ll in zip(C['start_long'], C['start_lat'])])
base_url = 'http://router.project-osrm.org/route/v1/driving/'
url = base_url + a + '?overview=full'
r = requests.get(url)
out = r.json()
points = polyline.decode(out['routes'][0]['geometry'])
cost = int(float(out['routes'][0]['distance'] / 1000.0))
duration = out['routes'][0]['duration']
print("Route map distance: {:.1f}, duration: {:.1f}"
.format(cost, duration))
# FIXME: unused
# legs = out['routes'][0]['legs']
# waypoints = out['waypoints']
map_osm = folium.Map(location=points[0], zoom_start=12)
folium.PolyLine(points, color="blue", weight=2.5,
opacity=0.5).add_to(map_osm)
for idx, sid, lat, lon in C[['segment_id',
'start_lat',
'start_long']].itertuples(index=True):
folium.Marker([lat, lon],
popup='{0:d}'.format(sid)).add_to(map_osm)
html = ('<div style="font-size: 10pt; color: red">{0:d}</div>'
.format(idx))
folium.map.Marker([lat, lon],
icon=DivIcon(icon_size=(150, 36),
icon_anchor=(0, 0),
html=html)
).add_to(map_osm)
fn, fe = os.path.splitext(args.save_map)
fname = "{:s}-{:d}{:s}".format(fn, label, fe)
print("Save map HTML to file '{:s}'".format(fname))
map_osm.save(fname)
except Exception as e:
print("Error: Cannot save map to file ({!s})".format(e))
def main(argv=sys.argv[1:]):
desc = 'Shortest Path for across points assigned'
parser = argparse.ArgumentParser(description=desc)
parser.add_argument('input', default=None,
help='Worker assigned road segments file')
parser.add_argument('-i', '--init-location', default=None,
help='First segment_id to start from each worker')
parser.add_argument('-o', '--output', default='shortest-path-output.csv',
help='Output file name')
parser.add_argument('-d', '--distance-func', default='euclidean',
choices=['euclidean', 'haversine', 'osrm'],
help='Distance function for distance matrix')
parser.add_argument('--plot', dest='plot', action='store_true',
help='Plot the output')
parser.set_defaults(plot=False)
parser.add_argument('--save-plot', dest='save_plot', default=None,
help='Save plotting to file')
parser.add_argument('--save-map', dest='save_map', default=None,
help='Save map to HTML file')
parser.add_argument('--osrm-base-url', dest='osrm_base_url', default=None,
help='Custom OSRM service URL')
parser.add_argument('--osrm-max-table-size', dest='osrm_max_table_size',
default=100, type=int, help='Maximum OSRM table size')
args = parser.parse_args(argv)
print(args)
df = pd.read_csv(args.input)
if args.init_location:
idf = pd.read_csv(args.init_location)
if args.plot or args.save_plot:
import matplotlib
if not args.plot:
matplotlib.use('agg')
import matplotlib.pyplot as plt
output = []
total_cost = 0
for i, l in enumerate(sorted(df.assigned_points.unique())):
print("Search TSP path for #{:d}...".format(l))
adf = df.loc[df.assigned_points == l, ['start_long', 'start_lat']]
A = adf.as_matrix()
# FIXME: OSRM distance matrix actually isn't distance but it's duration
cost, tour = ortools_tsp(A, args)
total_cost += cost
N = len(tour) - 1
B = df.loc[df.assigned_points == l, ['segment_id',
'start_lat', 'start_long']]
B.reset_index(drop=True, inplace=True)
C = B.loc[tour, :]
C.reset_index(drop=True, inplace=True)
title = ('TSP: {:d}, Cost: {:0.1f}, N: {:d} ({:s})'
.format(l, cost, N, args.distance_func.title()))
print(title)
if args.plot or args.save_plot:
fig = plt.figure(figsize=(16, 16))
ax = fig.add_subplot(1, 1, 1)
ax.plot(C['start_long'],
C['start_lat'], 'k', markerfacecolor='#00FF00',
marker='.', markersize=10)
step = 0
for x, y in zip(C['start_long'], C['start_lat']):
ox = randint(-24, -16) if randint(0, 1) else randint(16, 24)
oy = randint(-24, -16) if randint(0, 1) else randint(16, 24)
ax.annotate(str(step), xy=(x, y), xytext=(ox, oy),
textcoords='offset points',
arrowprops=dict(arrowstyle="->", color='r'),
fontsize=6, color='r')
step += 1
ax.set_title(title)
if args.plot:
plt.show()
if args.save_plot:
fn, fe = os.path.splitext(args.save_plot)
fname = "{:s}-{:d}{:s}".format(fn, l, fe)
print("Plotting to file '{:s}'".format(fname))
fig.savefig(fname)
plt.close()
if args.save_map:
do_save_map(args, l, C)
# Rotate path to start from the initial segments
path = list(C['segment_id'])[:-1]
if args.init_location:
start_segment_id = idf.loc[i][0]
pos = path.index(start_segment_id)
else:
pos = 0
new_path = path[pos:] + path[:pos]
output.append([l, cost, N, ';'.join([str(p) for p in new_path])])
print("Total cost: {0:.1f}".format(total_cost))
# save output to file
print("Save the output file to '{:s}'".format(args.output))
odf = pd.DataFrame(output, columns=['worker_id', 'cost', 'n',
'path_order'])
odf.to_csv(args.output, index=False)
if __name__ == "__main__":
sys.exit(main())