-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmetro_exchange.py
341 lines (271 loc) · 9.54 KB
/
metro_exchange.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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
#!/usr/local/env python
"""
===========================
Metro exchange route search
===========================
------------------
Heuristic function
------------------
1. Base on distance only
h(n) = g(n) + f(n), where g(n) denote the distance between current station to
the target station and, f(n) denote the distance between the start station to
the current station
2. Base on distance and line information
h(n) = g(n) + h(n)
{ 0, if both stations in the same line
l(n) = {
{ 1, if the two stations not in the same line
dg(n) denote the normalized distance between current station to the target station
dh(n) denote the normalized distance between start station to the target station
both dg(n) and dh(n) are normalized to 0 to 1, 0 means the shortest distance, wheras 1
denotes the longest distance
g(n) = w1 * dg(n) + w2 * l(n)
h(n) = w1 * dh(n) + w2 * l(n)
w1, w2 are the weight for the components
------------------
Metro file format
------------------
{
'city': 'BJ',
'lines': [
{
'line_name': '1号线',
'stations': [
{'station': '苹果园', 'lon': 116.177388, 'lat': 39.926727},
{'station': '古城', 'lon': 116.190337, 'lat': 39.907450}
]
},
{
'line_name': '2号线',
'stations': [
{'station': '西直门', 'lon': 116.177388, 'lat': 39.926727},
{'station': '积水潭', 'lon': 116.177388, 'lat': 39.926727}
]
}
]
}
------------------
Usage
------------------
Step 1:
Crawl the metro data with `amap_metro_api.py`
$ python amap_metro_api.py BJ amap.bj.metro.json
Step 2:
Run metro exchange program to get exchange plan
# python metro_exchange.py amap.bj.metro.json 苹果园 南锣鼓巷 1
"""
import sys
import json
import math
import heapq
import logging
import argparse
import networkx as nx
import matplotlib.pyplot as plt
def degree_to_radians(degrees):
return math.pi / 180 * degrees
def distance_in_km_between_earth_coordinates(lat1, lon1, lat2, lon2):
"""
Calcuate the distance between two coordinates by KM
Ref: https://stackoverflow.com/questions/365826/calculate-distance-between-2-gps-coordinates
"""
earthRadiusKm = 6371
dLat = degree_to_radians(lat2 - lat1)
dLon = degree_to_radians(lon2 - lon1)
lat1 = degree_to_radians(lat1)
lat2 = degree_to_radians(lat2)
a = math.sin(dLat / 2) * math.sin(dLat / 2) + math.sin(dLon / 2) * math.sin(dLon / 2) * math.cos(lat1) * math.cos(lat2)
c = 2 * math.atan2(math.sqrt(a), math.sqrt(1 - a))
return earthRadiusKm * c
def distance_heuristic(stations_meta, from_node, cur_node, to_node):
"""
Distance base heuristic function
(A.K.A shortest path)
"""
gn = distance_in_km_between_earth_coordinates(
stations_meta[to_node]['lat'], stations_meta[to_node]['lon'],
stations_meta[cur_node]['lat'], stations_meta[cur_node]['lon'],
)
fn = distance_in_km_between_earth_coordinates(
stations_meta[to_node]['lat'], stations_meta[to_node]['lon'],
stations_meta[from_node]['lat'], stations_meta[from_node]['lon'],
)
return gn + fn
def normalize_distance(max_dist, node1, node2):
dist = distance_in_km_between_earth_coordinates(
stations_meta[node1]['lat'], stations_meta[node1]['lon'],
stations_meta[node2]['lat'], stations_meta[node2]['lon'],
)
return dist / max_dist
def distance_and_line_heuristic(w1=0.5, w2=0.25, w3=0.25):
"""
Heuristic function base on distance and line information
(A.K.A least exchange plan)
"""
max_dist = find_max_distance_in_graph(stations_meta)
def wrapper(stations_meta, from_node, cur_node, to_node):
dgn = normalize_distance(max_dist, cur_node, to_node)
dfn = normalize_distance(max_dist, from_node, cur_node)
gn = w1 * dgn + w2 * (0 if len(stations_meta[to_node]['lines'] & stations_meta[cur_node]['lines']) != 0 else 1)
fn = w1 * dfn + w2 * (0 if len(stations_meta[from_node]['lines'] & stations_meta[cur_node]['lines']) != 0 else 1)
return gn + fn
return wrapper
def heuristic(stations_meta, from_node, cur_node, to_node, algorithm):
"""
Heuristic funciton: h(n) = f(n) + g(n)
"""
return algorithm(stations_meta, from_node, cur_node, to_node)
def astar_search_with_priority_queue(graph, stations_meta, from_node, to_node, strategy):
"""
A* search with priority queue
Find the route in `graph` from `from_node` to `to_node` with A* search
:params dict graph: The metro station grap
:params dict stations_meta: A dict for metro station meta data
:param str from_node: Station from, eg., 苹果园
:param str to_node: To which station, eg., 南锣鼓巷
:param int strategy: The exchange strategy, 1 - shortest distance, 2 - less exchange
:returns: A optimal path from `from_node` to `to_node`
"""
assert strategy in [1, 2], 'not support strategy {}'.format(strategy)
if strategy == 1:
strategy_func = distance_heuristic
else:
strategy_func = distance_and_line_heuristic()
pending = []
seen = set()
path = []
dist = heuristic(stations_meta, from_node, from_node, to_node, strategy_func)
heapq.heappush(pending, (dist, from_node))
while len(pending) > 0:
cur_node = heapq.heappop(pending)[1]
if to_node == cur_node:
path.append(cur_node)
return path
if cur_node in seen:
continue
neighbors = graph[cur_node]
for neighbor in neighbors:
if neighbor not in pending:
dist = heuristic(stations_meta, from_node, neighbor, to_node, strategy_func)
heapq.heappush(pending, (dist, neighbor))
path.append(cur_node)
seen.add(cur_node)
FORMAT = '[%(levelname)s]: %(message)s'
logging.basicConfig(format=FORMAT)
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
def draw_graph(graph):
gph = nx.Graph(graph)
plt.subplot()
nx.draw(gph, with_labels=True, node_size=4, font_size='8')
plt.show()
input()
def build_metro_stations_meta(metro):
"""
Build a metro stations meta data map:
{
'苹果园', {'lon': 116.177388, 'lat': 39.926727, lines: ['1号线']},
'古城': {'lon': 116.190337, 'lat': 39.907450, lines: ['1号线']},
...
}
"""
try:
meta = {}
for line in metro['lines']:
for st in line['stations']:
st_name = st['station']
try:
item = meta[st_name]
item['lines'].add(line['line_name'])
except KeyError:
meta[st_name] = {
'lon': st['lon'],
'lat': st['lat'],
'lines': set([line['line_name']])
}
return meta
except Exception:
logger.exception('metro file format not correct')
def build_metro_graph(metro):
"""
Build a metro station map like this:
{
"鼓楼大街": {"安德里北街", "积水潭", "安定门", "什刹海"},
"苹果园": {"古城"},
...
}
"""
try:
lines = metro['lines']
stations = {}
for line in lines:
prev_st = ''
nodes = line['stations']
for node in nodes:
st_name = node['station']
if st_name not in stations:
stations[st_name] = set()
if prev_st != '':
stations[st_name].add(prev_st)
stations[prev_st].add(st_name)
prev_st = st_name
return stations
except Exception:
logger.exception('metro file format not correct')
def find_max_distance_in_graph(stations_meta):
"""
Find the max distance between two nodes in the graph
"""
return max([
distance_in_km_between_earth_coordinates(
stations_meta[left]['lat'], stations_meta[left]['lon'],
stations_meta[right]['lat'], stations_meta[right]['lon'],
)
for left in stations_meta.keys() for right in stations_meta.keys() if left != right
])
def load_metro(path):
with open(path, 'r') as f:
metro = json.load(f)
return metro
def print_path(path):
print(' -> '.join(path))
if __name__ == '__main__':
parser = argparse.ArgumentParser()
parser.add_argument(
'data',
type=str,
help='The metro map json'
)
parser.add_argument(
'from_station',
type=str,
help='From station'
)
parser.add_argument(
'to_station',
type=str,
help='To station'
)
parser.add_argument(
'strategy',
type=int,
help='Exchange strategy, 1 - Less exchange, 2 - Less stations, 3 - More stations'
)
args = parser.parse_args(sys.argv[1:])
metro = load_metro(args.data)
graph = build_metro_graph(metro)
# draw_graph(graph)
stations_meta = build_metro_stations_meta(metro)
dist = distance_in_km_between_earth_coordinates(
stations_meta[args.from_station]['lat'], stations_meta[args.from_station]['lon'],
stations_meta[args.to_station]['lat'], stations_meta[args.to_station]['lon'],
)
print('Distance between {} and {} is {:.2f}km'.format(
args.from_station,
args.to_station,
dist,
))
path = astar_search_with_priority_queue(
graph, stations_meta, args.from_station, args.to_station, args.strategy
)
print_path(path)