-
Notifications
You must be signed in to change notification settings - Fork 0
/
short_path.py
408 lines (366 loc) · 14.6 KB
/
short_path.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
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
from functools import reduce
from auxiliary import swapByIndex
from randomcolor.randomcolor import RandomColor
from multiprocessing.dummy import Pool as ThreadPool
from geographiclib.geodesic import Geodesic
from polyline.codec import PolylineCodec
from data_loader import Clusters
from re import sub, compile
from copy import deepcopy
import requests
import numpy as np
import geojson as gs
import json as js
import datetime as dt
import sys
import os
SERVER_URL = 'http://79.170.167.101:5000/'
THREAD_COUNT = 8
# thread THREAD_COUNT thread pool
POOL = ThreadPool(processes=THREAD_COUNT)
# function: calculate distance from a to b
# input:
# a -- first point
# b -- second point
# output:
# distance in sphere
def getDistance(a, b):
return Geodesic.WGS84.Inverse(a[1], a[0], b[1], b[0])['s12']
# function: calculate new point
# input:
# a -- point coord ([lon, lat]
# azimut -- ...
# radius -- ...
def getNewCoord(a, azimut, radius):
lst = Geodesic.WGS84.Direct(a[1], a[0], azimut, radius)
return [lst['lon2'], lst['lat2']]
def itIndexDict(data):
for index in data.keys():
yield [index, data[index]]
def itIndexData(data):
for index in range(len(data)):
yield [index, data[index]]
def async_worker(iterator, func, data):
thread_list = []
result = []
for item in iterator:
# create job for new_route function
thread = POOL.apply_async(func, (item, data))
# add thread in list
thread_list.append(thread)
for thread in thread_list:
# get result of job
result.append(thread.get())
return result
class Route:
def __init__(self, ids, coord, key=0, metric=1):
self.coord = coord
self.ids = ids
self.key = key
if metric == 0:
self.dist = self.node()
else:
self.dist = self.distance()
# graph metric
def node(self):
result_distance = 0
for index in range(1, len(self.ids)):
a = self.coord.get(self.ids[index-1])
b = self.coord.get(self.ids[index+0])
result_distance += getDistance(a, b)
return result_distance
# osrm metric
def distance(self):
viaroute = SERVER_URL + 'viaroute?alt=false&geometry=false'
for item in self.ids:
try:
coords = self.coord.get(item)
viaroute += '&loc={},{}'.format(coords[1], coords[0])
except:
print('>> note {} not found!'.format(item))
sys.exit()
req = js.loads(requests.get(viaroute).text)
return req['route_summary']['total_distance']
# route with geometry
def route(self):
viaroute = SERVER_URL + 'viaroute?alt=false&geometry=true'
for item in self.ids:
try:
coords = self.coord.get(item)
viaroute += '&loc={},{}'.format(coords[1], coords[0])
except:
print('>> note {} not found!'.format(item))
sys.exit()
req = js.loads(requests.get(viaroute).text)
return list(map(
lambda x: [x[1] / 10.0, x[0] / 10.0], PolylineCodec().decode(req['route_geometry'])
))
def __getitem__(self, index):
return self.ids[index]
def __setitem__(self, index, value):
self.ids[index] = value
def index(self, value):
return self.ids.index(value)
def insert(self, index, value):
self.ids.insert(index, value)
def pairs(self):
pair = []
for index in range(len(self.ids)-1):
pair.append([self.ids[index+0], self.ids[index+1]])
return pair
def __str__(self):
return '[{}] {} : {}'.format(self.key, self.ids, self.dist)
class GeoConverter:
def __init__(self, matrix):
self.geo_data = None
self.convex_hull = None
self.outer_circle_points = None
self.geo_center = None
self.radius = None
self.route_points = []
self.matrix = matrix
def load(self, filename):
with open(filename, 'r') as jfile:
# self.geo_data = gs.load(jfile)['coordinates']
i = 0
for item in gs.load(jfile)['coordinates']:
self.geo_data[i] = item
i += 1
return self
raise Exception('data is not loaded')
def load_raw(self, filename):
with open(filename, 'r') as jfile:
self.geo_data = {}
raw_points = jfile.readlines()
for string in raw_points:
data = string.split(',')
if len(data) < 3:
continue
data_2 = int(data[2].replace(']', ''))
data_1 = float(data[1].replace('[', ''))
data_0 = float(data[0].replace('[', ''))
self.geo_data[data_2] = [data_1, data_0]
return self
raise Exception('data is not loaded')
def dump(self, filename):
with open(filename, 'w') as jfile:
pass
# # Need refactoring
# feature_convex = gs.Feature(
# geometry=gs.LineString(self.convex_hull),
# properties={'label': 'Convex Hull', 'color': '#1e90ff'})
# feature_center = gs.Feature(
# geometry=gs.Point(self.geo_center),
# properties={'label': 'Center of Convex Hull', 'color': '#B22222'})
# feature_pack = gs.FeatureCollection(
# [feature_convex, feature_center, self.geo_data])
# feature_color_point = gs.FeatureCollection(self.color_points)
# feature_outer_circle = gs.FeatureCollection(self.outer_circle_points)
# res_feature = gs.FeatureCollection([feature_pack, feature_color_point, feature_outer_circle])
# gs.dump(res_feature, jfile)
# return self
raise Exception('can\'t write convex hull points to file')
def graham(self):
def rotate(d, a, b, c):
return (d[b][1] - d[a][1]) * (d[c][0] - d[b][0]) - \
(d[b][0] - d[a][0]) * (d[c][1] - d[b][1])
coords = self.geo_data
length = len(coords)
index = sorted(coords.keys(), key=lambda x: coords[x][0])
for i in range(2, length):
j = i
while (j > 1) and (rotate(coords, index[0], index[j-1], index[j]) < 0):
swapByIndex(index, j, j-1)
j -= 1
cluster_id = [index[0], index[1]]
for i in range(2, length):
while rotate(coords, cluster_id[-2], cluster_id[-1], index[i]) < 0:
del cluster_id[-1]
cluster_id.append(index[i])
self.convex_hull = cluster_id
return self
def findCluster(self, obj):
return list(self.geo_data.keys())[list(self.geo_data.values()).index(obj)]
def indexToCoord(self, obj):
route = []
for index in obj:
route.append(self.geo_data[index])
return route
def coordToIndex(self, obj):
route = []
for coord in obj:
route.append(self.findCluster(coord))
return route
def center(self):
length = len(self.convex_hull)
center_c1 = reduce(lambda x, y: x + y, map(
lambda x: self.geo_data[x][0], self.convex_hull)) / length
center_c2 = reduce(lambda x, y: x + y, map(
lambda x: self.geo_data[x][1], self.convex_hull)) / length
self.geo_center = [center_c1, center_c2]
return self
def pointInCircle(self, center):
initial_coeff = 0.05
eps_multiplier = 1.5
eps = self.radius * initial_coeff
clusters = []
while len(clusters) == 0:
for index, item in itIndexDict(self.geo_data):
dist = getDistance(center, item)
if dist < eps:
clusters.append(index)
eps *= eps_multiplier
return clusters
def findTerminals(self, Nr):
def byPairs(a):
mid = len(a) // 2
for index in range(mid):
yield [a[index], a[index+mid]]
def pairing(item, etc):
p1, p2 = item
c1 = etc.pointInCircle(p1)
c2 = etc.pointInCircle(p2)
# sort list by max people in cluster
c1.sort(key=lambda x: -matrix.getPeople(x, x))
c2.sort(key=lambda x: -matrix.getPeople(x, x))
return [c1[0], c2[0]]
r0 = self.geo_center
distance = async_worker(self.convex_hull, lambda a, b: getDistance(r0, b[a]), (self.geo_data))
r1 = self.convex_hull[distance.index(max(distance))]
self.radius = getDistance(r0, self.geo_data[r1])
self.outer_circle_points = []
ncount = np.arange(0, 360, 360/(2*Nr))
self.outer_circle_points = async_worker(ncount, lambda a, b: getNewCoord(b[0], a, b[1]), (r0, self.radius))
self.initial_cluster = async_worker(byPairs(self.outer_circle_points), pairing, (self))
remove = set()
for item in self.initial_cluster:
remove.add(item[0])
remove.add(item[1])
other_clusters = []
for item in self.geo_data.keys():
if item not in remove:
other_clusters.append(item)
return self.initial_cluster, other_clusters
def routing(self, N_r, C_t, C_nt):
def new_route(item, etc):
NewRoutes = []
# unpacking data
C_nt, geo_data = etc[0], etc[1]
n_x, n_y = item[0], item[1]
OldRoute = Route([n_x, n_y], geo_data)
# step 3.2
for j, c_j in itIndexData(C_nt):
NewRoutes.append(Route([n_x, c_j, n_y], geo_data, key=j))
# j = argmin(|len(n_x, n_y) - len(n_x, c_j, n_y)|)
NewRoutes.sort(key=lambda x: abs(OldRoute.dist-x.dist))
# step 3.3
return NewRoutes[0]
def build_route(item, etc):
R_i, geo_data = etc[0], etc[1]
# get 'insert' node from RC: [item[0], >>item[1]<<, item[2]]
add_node = item[1]
# find index `item` in R_i: [.., item[0], item[2], ...]
idx = R_i.index(item[0]) + 1
# hard copy `R_i` data to `new_route`
new_route = deepcopy(R_i.ids)
# insert `add_node` to `new_route`
new_route.insert(idx, add_node)
# add `new_route` to `RCC`
return Route(new_route, geo_data, key=item.key)
RN = []
thread_list = []
# create initialization routes
RN = async_worker(C_t, lambda a, b: Route(a, b), (self.geo_data))
while C_nt:
for index in range(N_r):
# check `C_nt`
if C_nt == []:
break
# step 1 (hard copy)
R_i = deepcopy(RN[index])
# step 2
PN = R_i.pairs()
# step 3, 3.1, 3.3
RC = async_worker(PN, new_route, (C_nt, self.geo_data))
# step 4
RCC = async_worker(RC, build_route, (R_i, self.geo_data))
# step 5: sort by ascending
RCC.sort(key=lambda x: x.dist)
# step 6: replace `RN` element by `RCC`
RN[index] = RCC[0]
# step 7: remove `C_nt` element by `RCC` key (node indexing)
del C_nt[RCC[0].key]
return RN
def dump(list_data, data, filename, geometry='route'):
with open(filename, 'w') as jfile:
rnd_color = RandomColor()
colors = rnd_color.generate(count=len(list_data))
features = []
index = 1
features.append(gs.MultiPoint(list(data.geo_data.values())))
for item in list_data:
color = colors.pop()
terminal = [item[0], item[-1]]
# get route coords by road
if geometry == 'graph':
# graph geometry
RP = data.indexToCoord(item)
else:
# osrm geometry
RP = item.route()
coords = gs.Feature(geometry=gs.LineString(RP),
properties={'label': 'Route #{}'.format(index+1), 'color': color})
term = gs.Feature(geometry=gs.MultiPoint(data.indexToCoord(terminal)),
properties={'label': 'Terminal #{}'.format(index+1), 'color': color})
index += 1
features.append(coords)
features.append(term)
feature_r = gs.FeatureCollection([features])
gs.dump(features, jfile)
if __name__ == '__main__':
route_name = [100, 150, 200, 300]
N_count = range(8, 20, 2)
route_dist = []
matrix = Clusters()
for name in route_name:
print('{} >> loading data {}_p.js'.format(dt.datetime.now(), name))
matrix.generateMatrix('./data/{}_p.js'.format(name), './data/ways.js')
data = GeoConverter(matrix).load_raw('./data/{}_c.js'.format(name)).graham().center()
print('{} >> data loaded ... ok'.format(dt.datetime.now()))
for N_r in N_count:
terminals_file = 'article/info-{}-{}.out'.format(name, N_r)
if not os.path.isfile(terminals_file):
C_t, C_nt = data.findTerminals(N_r)
with open(terminals_file, 'w') as f:
f.write('C_t = {}\n'.format(C_t))
f.write('C_nt = {}\n'.format(C_nt))
else:
def readAndSplit(f, data_type, extend=False):
result = []
nonNumeric = compile(r'[^0-9,]')
data = nonNumeric.sub('', f.readline()).split(',')
while len(data) > 0:
two, one = data.pop(), data.pop()
if extend:
result.extend([data_type(one), data_type(two)])
else:
result.append([data_type(one), data_type(two)])
return result
with open(terminals_file, 'r') as f:
C_t = readAndSplit(f, int)
C_nt = readAndSplit(f, int, extend=True)
print('{} >> {} terminals founded ... ok'.format(dt.datetime.now(), N_r))
RN = data.routing(N_r, C_t, C_nt)
print('{} >> generated route ... ok'.format(dt.datetime.now()))
current = []
for index, item in itIndexData(RN):
print('{:04} -- {}'.format(index, item))
current.append(item.dist)
print('{} >> data saved in ./article/result-{}-{}.json'.format(dt.datetime.now(), name, N_r))
dump(RN, data, './article/result-{}-{}.json'.format(name, N_r))
route_dist.append(current)
print('distances = [\n')
for item in route_dist:
print('{},'.format(item))
print('];')
POOL.close()