-
Notifications
You must be signed in to change notification settings - Fork 0
/
DijkstraSolver.py
647 lines (539 loc) · 30.2 KB
/
DijkstraSolver.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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
# Solve travel-time-based time-dependent routing problem based on GTFS trip update data with a Dijkstra approach.
# The result is a record-based OD matrix with scheduled-based results (with SC suffix, SChedule) and real-time-based results (with RT suffix, Real-Time).
# The results (for both versions) include:
# 1) origin and destination stop with stop_id;
# 2) travel time between the origin and destination, including total travel time, bus time, walk time, wait time;
# 3) last stop, according to Dijkstra algorithm's backward structure;
# 4) last trip, including trip type ("bus" or "walk"), trip_id (if trip type is bus), transfer count (which is not right for now).
# The results are retrospective OD records generated by optimal algorithm. The locations are only bus stops, not including other places.
import BasicSolver
import sys
import os
import time, math
import multiprocessing
import copy
from pymongo import MongoClient
from datetime import timedelta, date, datetime
from math import sin, cos, sqrt, atan2, pi, acos
from tqdm import tqdm
from collections import OrderedDict
import time as atime
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import transfer_tools
client = MongoClient('mongodb://localhost:27017/')
class DijkstraSolver(BasicSolver.BasicSolver):
def __init__(self, args):
BasicSolver.BasicSolver.__init__(self)
timestamp = args[0] # The start timestamp when the hypothesis user leaves the start point, e.g. 8am, 12pm, 6pm
walkingDistanceLimit = args[1] # Walking distance limit, usually 700 meters
timeDeltaLimit = args[2] # A hard limit of stop time limit. The buses after that time limit will not be considered as a possible trip, even if it exists. Uusally 3 hours = 180 * 60 seconds
walkingSpeed = args[3] # Usually 1.4 meters per second
scooterSpeed = args[4] # Usually 10 mph. Not used in this project.
scooterDistanceLimit = args[5] # The limit of scooter travel disctance. Not used in this project.
isRealTime = args[6] # Is the calcuation retrospective real-time based
isScooter = args[7] # Is the calculation including scooters.
self.walkingDistanceLimit = walkingDistanceLimit
self.scooterDistanceLimit = scooterDistanceLimit
self.timestamp = timestamp
self.timeDeltaLimit = timeDeltaLimit
self.walkingSpeed = walkingSpeed
self.scooterSpeed = scooterSpeed
self.isRealTime = isRealTime
self.walkingTimeLimit = walkingDistanceLimit/walkingSpeed
self.isScooter = isScooter
# Time
self.curDateTime = datetime.fromtimestamp(timestamp)
self.curDate = self.curDateTime.date()
todayDate = self.curDate.strftime("%Y%m%d")
self.todaySeconds = time.mktime(time.strptime(todayDate, "%Y%m%d"))
self.timeLimit = min(self.todaySeconds + 86400, timestamp + timeDeltaLimit) # Limit the time to just one day.
self.todayDate = todayDate
self.curGTFSTimestamp = self.find_gtfs_time_stamp(self.curDate)
# print("current GTFS timestamp", self.curGTFSTimestamp)
# Mongo GTFS setup
self.db_GTFS = client.cota_gtfs # GTFS static schedule database
self.db_real_time = client.cota_real_time # GTFS real-time database
self.col_stops = self.db_GTFS[str(self.curGTFSTimestamp) + "_stops"]
self.col_stop_times = self.db_GTFS[str(
self.curGTFSTimestamp) + "_stop_times"]
self.col_real_times = self.db_real_time["R" + todayDate]
self.db_scooter = client.lime_location
self.col_scooter = self.db_scooter[todayDate]
self.db_access = client.cota_access_rel
self.rl_stops = list(self.col_stops.find({}))
self.rl_stop_times_rt = list(self.col_real_times.find({"time": {"$gt": timestamp, "$lt": self.timeLimit}})) # Real-time
self.rl_stop_times_sc = list(self.col_real_times.find({"scheduled_time": {"$gt": timestamp, "$lt": self.timeLimit}})) # Scheduled
# print(len(self.rl_stop_times), timestamp, self.timeLimit)
if self.rl_stop_times_rt != None:
self.rl_stop_times_rt.sort(key=self.sortrlStopTimesRealtime)
self.rl_stop_times_sc.sort(key=self.sortrlStopTimesSchedule)
self.stopsDic = {}
self.scooterDic = {}
self.visitedSet = {}
self.timeListByStopRT = {}
self.timeListByTripRT = {}
self.timeListByStopSC = {}
self.timeListByTripSC = {}
self.arcsDicRT = {}
self.arcsDicSC = {}
for eachStop in self.rl_stops:
self.stopsDic[eachStop["stop_id"]] = eachStop
for eachTime in self.rl_stop_times_rt:
stopID = eachTime["stop_id"]
tripID = eachTime["trip_id"]
try:
self.timeListByStopRT[stopID]
except:
self.timeListByStopRT[stopID] = []
self.timeListByStopRT[stopID].append(eachTime)
try:
self.timeListByTripRT[tripID]
except:
self.timeListByTripRT[tripID] = []
self.timeListByTripRT[tripID].append(eachTime)
for eachTime in self.rl_stop_times_sc:
stopID = eachTime["stop_id"]
tripID = eachTime["trip_id"]
try:
self.timeListByStopSC[stopID]
except:
self.timeListByStopSC[stopID] = []
self.timeListByStopSC[stopID].append(eachTime)
try:
self.timeListByTripSC[tripID]
except:
self.timeListByTripSC[tripID] = []
self.timeListByTripSC[tripID].append(eachTime)
for eachTripID, eachTrip in self.timeListByTripRT.items():
if len(eachTrip) < 2:
continue
for index in range(0, len(eachTrip)-1):
generatingStopID = eachTrip[index]["stop_id"]
receivingStopID = eachTrip[index + 1]["stop_id"]
try:
self.arcsDicRT[generatingStopID]
except:
self.arcsDicRT[generatingStopID] = {}
try:
self.arcsDicRT[generatingStopID][receivingStopID]
except:
self.arcsDicRT[generatingStopID][receivingStopID] = {}
timeGen = eachTrip[index]["time"]
timeRec = eachTrip[index + 1]["time"]
try:
self.arcsDicRT[generatingStopID][receivingStopID][timeGen]
except:
self.arcsDicRT[generatingStopID][receivingStopID][timeGen] = {
'time_gen': timeGen, 'time_rec': timeRec, 'bus_time': - timeGen + timeRec, "trip_id": eachTripID}
for eachTripID, eachTrip in self.timeListByTripSC.items():
if len(eachTrip) < 2:
continue
for index in range(0, len(eachTrip)-1):
generatingStopID = eachTrip[index]["stop_id"]
receivingStopID = eachTrip[index + 1]["stop_id"]
try:
self.arcsDicSC[generatingStopID]
except:
self.arcsDicSC[generatingStopID] = {}
try:
self.arcsDicSC[generatingStopID][receivingStopID]
except:
self.arcsDicSC[generatingStopID][receivingStopID] = {}
timeGen = eachTrip[index]["scheduled_time"]
timeRec = eachTrip[index + 1]["scheduled_time"]
try:
self.arcsDicSC[generatingStopID][receivingStopID][timeGen]
except:
self.arcsDicSC[generatingStopID][receivingStopID][timeGen] = {
'time_gen': timeGen, 'time_rec': timeRec, 'bus_time': - timeGen + timeRec, "trip_id": eachTripID}
# Sort the arcsDics with OrderedDic.
def return_time_gen(e):
# return e["time_gen"]
return e[0]
for startStopID, startStop in self.arcsDicRT.items():
for endStopID, endStop in startStop.items():
self.arcsDicRT[startStopID][endStopID] = OrderedDict(sorted(endStop.items(), key=return_time_gen))
for startStopID, startStop in self.arcsDicSC.items():
for endStopID, endStop in startStop.items():
self.arcsDicSC[startStopID][endStopID] = OrderedDict(sorted(endStop.items(), key=return_time_gen))
self.count = 0
# print("Initialization: Done!")
def sortrlStopTimesSchedule(self, value):
return value["scheduled_time"]
def sortrlStopTimesRealtime(self, value):
return value["time"]
def calculateDistance(self, latlng1, latlng2):
R = 6373
lat1 = float(latlng1["stop_lat"])
lon1 = float(latlng1["stop_lon"])
lat2 = float(latlng2["stop_lat"])
lon2 = float(latlng2["stop_lon"])
theta = lon1 - lon2
radtheta = pi * theta / 180
radlat1 = pi * lat1 / 180
radlat2 = pi * lat2 / 180
dist = sin(radlat1) * sin(radlat2) + cos(radlat1) * \
cos(radlat2) * cos(radtheta)
try:
dist = acos(dist)
except:
dist = 0
dist = dist * 180 / pi * 60 * 1.1515 * 1609.344
return dist
def findClosestStop(self, isRT): # Find the geographically closest stop.
minDistance = sys.maxsize
closestStop = False
if isRT == True:
for eachStop in self.rl_stops:
eachStopID = eachStop["stop_id"]
# print(minDistance, self.visitedSet[eachStopID]['time'])
# if self.visitedSet[eachStopID]['time'] < minDistance:
# print(eachStopID, self.visitedSet[eachStopID]['visitTag'])
if self.visitedSet[eachStopID]['timeRT'] < minDistance and self.visitedSet[eachStopID]['visitTagRT'] == False:
minDistance = self.visitedSet[eachStopID]['timeRT']
closestStop = eachStop
else:
for eachStop in self.rl_stops:
eachStopID = eachStop["stop_id"]
# print(minDistance, self.visitedSet[eachStopID]['time'])
# if self.visitedSet[eachStopID]['time'] < minDistance:
# print(eachStopID, self.visitedSet[eachStopID]['visitTag'])
if self.visitedSet[eachStopID]['timeSC'] < minDistance and self.visitedSet[eachStopID]['visitTagSC'] == False:
minDistance = self.visitedSet[eachStopID]['timeSC']
closestStop = eachStop
# print(closestStop)
return closestStop
def getTravelTimeRT(self, closestStopID, eachStopID, aStartTime): # Get the travel time (network cost) between the two stops given the arrival time at the start stop
# With bus trip, can wait and bus or walk
# All methods are exclusive between two stops. Options:
# 1. bus + wait. For all arcs.
# 2. walk.
# Risk1: may incur meaningless transfers; but we are not controlling that anyway. Possible solution: control trip_id or transfer_count
# Risk2: how to deal with last-mile scooters? Mimic the first version. (Don't have this issue in this project)
# Risk3: how to distinguish the first-mile trip? if timestamp is the starttimestamp, propagate using scooter; if not, propagate using walking (Don't have this issue in this project)
travelTime = {
"generatingStopIDRT": closestStopID,
"receivingStopIDRT": eachStopID,
"timeRT": sys.maxsize,
"walkTimeRT": None,
"busTimeRT": None,
"waitTimeRT": None,
"tripIDRT": None,
"tripTypeRT": None
}
closestStop = self.stopsDic[closestStopID]
eachStop = self.stopsDic[eachStopID]
# 1. Propagate with bus and wait. Not controlling transfer count. Select the most recent trip.
# More greedy: if there is a bus trip, wait for that regardless of the expected saved time by walking.
try:
self.arcsDicRT[closestStopID][eachStopID]
except:
pass # Not bus trip
else:
thisArcsDic = self.arcsDicRT[closestStopID][eachStopID]
for eachIndex, eachTrip in thisArcsDic.items():
# if closestStopID == "NORBARNW":
# a = 1
if eachTrip["time_gen"] >= aStartTime: # This requires that arcDic[Start][End] to be an ascending sequence. So use OrderedDic to maintain this feature.
recentBusTime = eachTrip["time_gen"]
travelTime["tripIDRT"] = eachTrip["trip_id"]
travelTime["busTimeRT"] = eachTrip["bus_time"]
travelTime["waitTimeRT"] = recentBusTime - aStartTime
travelTime["walkTimeRT"] = 0
travelTime["timeRT"] = travelTime["busTimeRT"] + travelTime["waitTimeRT"]
travelTime["tripTypeRT"] = "bus"
break
# if walkTime_walk > travelTime["timeRT"]:
return travelTime
# 2. Propagate with walk
# No edge, can only walk/scooter
if self.visitedSet[closestStopID]["lastTripTypeRT"] == "walk": # cannot make two subsequent non-transit arcs.
return travelTime
dist = self.calculateDistance(self.stopsDic[closestStopID], self.stopsDic[eachStopID]) # Can be precalculated
walkTime_walk = dist/self.walkingSpeed
totalTime_walk = walkTime_walk
if totalTime_walk > self.walkingTimeLimit:
pass
elif travelTime["timeRT"] > totalTime_walk: # if no bus trip, travelTime["time"] should be maxsize; or, the bus trip is very slow, but very unlikely
travelTime["busTimeRT"] = 0
travelTime["waitTimeRT"] = 0
travelTime["walkTimeRT"] = walkTime_walk
travelTime["timeRT"] = totalTime_walk
travelTime["tripTypeRT"] = "walk"
travelTime["tripIDRT"] = None
return travelTime
def getTravelTimeSC(self, closestStopID, eachStopID, aStartTime):
# With bus trip, can wait and bus or walk
# All methods are exclusive between two stops. Options:
# 1. bus + wait. For all arcs.
# 2. walk.
# Risk1: may incur meaningless transfers; but we are not controlling that anyway. Possible solution: control trip_id or transfer_count
# Risk2: how to deal with last-mile scooters? Mimic the first version.
# Risk3: how to distinguish the first-mile trip? if timestamp is the starttimestamp, propagate using scooter; if not, propagate using walking
travelTime = {
"generatingStopIDSC": closestStopID,
"receivingStopIDSC": eachStopID,
"timeSC": sys.maxsize,
"walkTimeSC": None,
"busTimeSC": None,
"waitTimeSC": None,
"tripIDSC": None,
"tripTypeSC": None
}
closestStop = self.stopsDic[closestStopID]
eachStop = self.stopsDic[eachStopID]
# 1. Propagate with bus and wait. Not controlling transfer count. Select the most recent trip.
# More greedy: if there is a bus trip, wait for that regardless of the expected saved time by walking.
try:
self.arcsDicSC[closestStopID][eachStopID]
except:
pass # Not bus trip
else:
for eachIndex, eachTrip in self.arcsDicSC[closestStopID][eachStopID].items():
if eachTrip["time_gen"] >= aStartTime:
recentBusTime = eachTrip["time_gen"]
travelTime["tripIDSC"] = eachTrip["trip_id"]
travelTime["busTimeSC"] = eachTrip["bus_time"]
travelTime["waitTimeSC"] = recentBusTime - aStartTime
travelTime["walkTimeSC"] = 0
travelTime["timeSC"] = travelTime["busTimeSC"] + travelTime["waitTimeSC"]
travelTime["tripTypeSC"] = "bus"
break
return travelTime
# 2. Propagate with walk
# No edge, can only walk/scooter
if self.visitedSet[closestStopID]["lastTripTypeSC"] == "walk": # cannot make two subsequent non-transit arcs.
return travelTime
dist = self.calculateDistance(self.stopsDic[closestStopID], self.stopsDic[eachStopID]) # Can be precalculated
walkTime_walk = dist/self.walkingSpeed
totalTime_walk = walkTime_walk
if totalTime_walk > self.walkingTimeLimit:
pass
elif travelTime["timeSC"] > totalTime_walk: # if no bus trip, travelTime["time"] should be maxsize; or, the bus trip is very slow, but very unlikely
travelTime["busTimeSC"] = 0
travelTime["waitTimeSC"] = 0
travelTime["walkTimeSC"] = walkTime_walk
travelTime["timeSC"] = totalTime_walk
travelTime["tripTypeSC"] = "walk"
return travelTime
def addToTravelTime(self, originalStrucuture, closestStructure, travelTime, tempStartTimestamp, isRT=True):
if isRT:
originalStrucuture["timeRT"] = closestStructure["timeRT"] + travelTime["timeRT"]
originalStrucuture["walkTimeRT"] = closestStructure["walkTimeRT"] + travelTime["walkTimeRT"]
originalStrucuture["busTimeRT"] = closestStructure["busTimeRT"] + travelTime["busTimeRT"]
originalStrucuture["waitTimeRT"] = closestStructure["waitTimeRT"] + travelTime["waitTimeRT"]
if travelTime["tripTypeRT"] == "bus" and travelTime["tripIDRT"] != originalStrucuture["lastTripIDRT"]:
originalStrucuture["transferCountRT"] += 1
originalStrucuture["lastTripTypeRT"] = travelTime["tripTypeRT"]
originalStrucuture["lastTripIDRT"] = travelTime["tripIDRT"]
originalStrucuture["generatingStopIDRT"] = travelTime["generatingStopIDRT"]
else:
originalStrucuture["timeSC"] = closestStructure["timeSC"] + travelTime["timeSC"]
originalStrucuture["walkTimeSC"] = closestStructure["walkTimeSC"] + travelTime["walkTimeSC"]
originalStrucuture["busTimeSC"] = closestStructure["busTimeSC"] + travelTime["busTimeSC"]
originalStrucuture["waitTimeSC"] = closestStructure["waitTimeSC"] + travelTime["waitTimeSC"]
if travelTime["tripTypeSC"] == "bus" and travelTime["tripIDSC"] != originalStrucuture["lastTripIDSC"]:
originalStrucuture["transferCountSC"] += 1
originalStrucuture["lastTripTypeSC"] = travelTime["tripTypeSC"]
originalStrucuture["lastTripIDSC"] = travelTime["tripIDSC"]
originalStrucuture["generatingStopIDSC"] = travelTime["generatingStopIDSC"]
# if isRT: # Use a same field for the RT and SC, because RT is before SC in the extendStop
# originalStrucuture["receivingStopID"] = travelTime["receivingStopIDRT"]
# originalStrucuture["stop_lat"] = self.stopsDic[travelTime["receivingStopIDRT"]]["stop_lat"]
# originalStrucuture["stop_lon"] = self.stopsDic[travelTime["receivingStopIDRT"]]["stop_lon"]
return originalStrucuture
# Dijkstraly find shortest time to each stop.
def extendStops(self, startStopID): # Corresponded to dijjkstra.docx, Line 1
self.aStartStopID = startStopID
startTimestamp = self.timestamp
self.visitedSet = {} # Line 2
for eachStop in self.rl_stops: # Initialization, Line 3
eachStopID = eachStop["stop_id"]
self.visitedSet[eachStopID] = {
"startStopID": startStopID, # Origin stop
"receivingStopID": eachStopID, # Destination stop
"timeRT": sys.maxsize, # Line 4
"walkTimeRT": 0,
"busTimeRT": 0,
"waitTimeRT": 0,
"generatingStopIDRT": None, # The last visited stop by Real-time, Line 5
"lastTripIDRT": None,
"lastTripTypeRT": None,
"transferCountRT": 0,
"visitTagRT": False,
"timeSC": sys.maxsize, # Line 4
"walkTimeSC": 0,
"busTimeSC": 0,
"waitTimeSC": 0,
"generatingStopIDSC": None, # The last visited stop by Schedule, Line 5
"lastTripIDSC": None,
"lastTripTypeSC": None,
"transferCountSC": 0,
"visitTagSC": False,
"stop_lat": self.stopsDic[eachStopID]["stop_lat"],
"stop_lon": self.stopsDic[eachStopID]["stop_lon"]
} # Line 6
self.visitedSet[startStopID]["timeRT"] = 0 # initialization, Line 7
self.visitedSet[startStopID]["timeSC"] = 0 # initialization, Line 7
# OD and trips with RT timetable
for _ in (self.rl_stops): # Line 8. Instead of using a while loop, I use a for loop to enumerate all the stops, which is equivilent except some redundants
closestStop = self.findClosestStop(True) # Line 9: find the closest node in set Q to the S set, which is the ones having the confirmed clostest distance
if closestStop == False: # Cannot find any closest nodes, every node is visited.
# print("break!")
break
closestStopID = closestStop["stop_id"]
self.visitedSet[closestStopID]["visitTagRT"] = True # Line 10: remove u from Q
# Can modify self.rl_stops to improve performance.
for eachStop in (self.rl_stops): # Line 11: For every neighbor of the closest stop to the set S, which is the ones having the confirmed closest distance.
eachStopID = eachStop["stop_id"]
if self.visitedSet[eachStopID]["visitTagRT"] == True: # Only for vertices that not in S.
continue
tempStartTimestamp = startTimestamp + self.visitedSet[closestStopID]["timeRT"] # Line 12
travelTime = self.getTravelTimeRT(closestStopID, eachStopID, tempStartTimestamp) # Find the weight between closestStop and eachStop
# if travelTime["tripType"] != None:
# print(self.visitedSet[eachStopID]["time"] > self.visitedSet[closestStopID]["time"] + travelTime["time"])
if travelTime["timeRT"] == None:
continue
if self.visitedSet[eachStopID]["timeRT"] > self.visitedSet[closestStopID]["timeRT"] + travelTime["timeRT"]: # Line 13
self.visitedSet[eachStopID] = self.addToTravelTime(self.visitedSet[eachStopID], self.visitedSet[closestStopID], travelTime, tempStartTimestamp, True) # Line 14 -15
# print("changed: ", self.visitedSet[eachStopID])
# print(_["stop_id"], "finished!")
# break
# print(self.visitedSet)
# OD and trips with scheduled timetable
for _ in (self.rl_stops):
closestStop = self.findClosestStop(False) # Dijkstra's algorithm: find the closest node to the S set, which is the ones having the confirmed clostest distance.
if closestStop == False:
# print("break!")
break
closestStopID = closestStop["stop_id"]
self.visitedSet[closestStopID]["visitTagSC"] = True # Add closest stop to S set.
# Can modify self.rl_stops to improve performance.
for eachStop in (self.rl_stops):
eachStopID = eachStop["stop_id"]
if self.visitedSet[eachStopID]["visitTagSC"] == True:
continue
tempStartTimestamp = startTimestamp + self.visitedSet[closestStopID]["timeSC"]
travelTime = self.getTravelTimeSC(closestStopID, eachStopID, tempStartTimestamp)
# if travelTime["tripType"] != None:
# print(self.visitedSet[eachStopID]["time"] > self.visitedSet[closestStopID]["time"] + travelTime["time"])
if travelTime["timeSC"] == None:
continue
if travelTime['timeSC'] > 0 and self.visitedSet[eachStopID]["visitTagSC"] == False and self.visitedSet[eachStopID]["timeSC"] > self.visitedSet[closestStopID]["timeSC"] + travelTime["timeSC"]:
self.visitedSet[eachStopID] = self.addToTravelTime(self.visitedSet[eachStopID], self.visitedSet[closestStopID], travelTime, tempStartTimestamp, False)
# if eachStopID == "NORKINS1":
# print("changed: ", self.visitedSet[eachStopID])
# print(_["stop_id"], "finished!")
# break
# print(self.visitedSet)
return self.visitedSet
def singleAccessibilitySolve(args, startLocation): # Calculate travel time from startLocation to all other stops.
# print(args, startLocation)
accessibilitySolver = DijkstraSolver(args)
lenStops = accessibilitySolver.extendStops(startLocation)
del accessibilitySolver
return lenStops
def collectiveAccessibilitySolve(args, sampledStopsList):
cores = 30
total_length = len(sampledStopsList)
batch = math.ceil(total_length/cores)
for i in tqdm(list(range(batch))):
pool = multiprocessing.Pool(processes=cores)
sub_output = []
try:
sub_sampledStopsList = sampledStopsList[cores*i:cores*(i+1)]
except:
sub_sampledStopsList = sampledStopsList[cores*i:]
sub_output = pool.starmap(singleAccessibilitySolve, zip([args]*len(sub_sampledStopsList), sub_sampledStopsList))
pool.close()
pool.join()
collectiveInsert(args, sub_output)
def collectiveInsert(args, output):
timestamp = args[0]
walkingDistanceLimit = args[1]
timeDeltaLimit = args[2]
walkingSpeed = args[3]
scooterSpeed = args[4]
scooterDistanceLimit = args[5]
isRealTime = args[6]
isScooter = args[7]
curDateTime = datetime.fromtimestamp(timestamp)
curDate = curDateTime.date()
todayDate = curDate.strftime("%Y%m%d")
recordCollection = []
col_access = client.cota_access_rel["add_" + todayDate + "_" +str(int(timestamp))]
count = 0
for eachVisitedSet in output:
accessibleStops = eachVisitedSet
for eachStopIndex, eachStopValue in accessibleStops.items():
# print(eachStopValue)
# if eachStopValue["lastTripTypeRT"] != None:
recordCollection.append(eachStopValue)
count += 1
col_access.insert_many(recordCollection)
# print("-----", todayDate, "-----", int(timestamp), "-----", count)
col_access.create_index([("startStopID", 1)])
if __name__ == "__main__":
basicSolver = BasicSolver.BasicSolver()
# startDate = date(2019, 6, 20)
# startDate = date(2018, 2, 25)
startDate = date(2019, 5, 9)
endDate = date(2019, 5, 15)
walkingDistanceLimit = 700
timeDeltaLimit = 180 * 60
walkingSpeed = 1.4
scooterSpeed = 4.47 # 10 mph
scooterDistanceLimit = (5-1)/0.32*4.47*60 # 5 dollar
sampleRate = 20
isRealTime = True
isScooter = False
daterange = (basicSolver.daterange(startDate, endDate))
numberOfTimeSamples = 1 # how many samples you want to calculate per hour. If the value = 1, then every 1 hour. If 4, then every 15 minutes.
for singleDate in (daterange):
weekday = singleDate.weekday()
# if weekday != 2:
# continue
GTFSTimestamp = basicSolver.find_gtfs_time_stamp(singleDate)
todaySeconds = atime.mktime(singleDate.timetuple())
gtfsSeconds = str(transfer_tools.find_gtfs_time_stamp(singleDate))
# 1. Sample stops list
# sampledStopsList = ['MOREASS', 'HIGCOON', '4TH15TN', 'TREZOLS', 'KARPAUN', 'LIVGRAE', 'GRESHEW', 'MAIOHIW', 'AGL540W', 'WHIJAEE', '3RDCAMW', 'HARZETS', 'MAIBRICE', 'SAI2NDS', '3RDMAIS', 'STYCHAS', 'LOC230N', 'BETDIEW', 'STEMCCS', 'INNWESE', 'HANMAIN', 'HIGINDN', '4THCHIN', 'RIDSOME', 'KARHUYN', 'LIVBURE', 'LONWINE', 'MAICHAW', 'BROHAMIW', 'WHI3RDE', '1STLINW', 'MAINOEW', 'MAIIDLE', '5THCLEE', '3RDTOWS', 'STYGAMS', 'KOE113W', 'TAM464S', 'CAS150S', 'BROOUTE', 'ALUGLENS', 'FRABREN', 'SOU340N', 'HILTINS', 'STRHOVE', 'SAWCOPN', 'HAMWORN', 'DALDUBN', 'MCNCHEN', 'HILBEAS', 'NOROWEN', 'SOUTER2A', 'GENSHAN', 'VACLINIC', 'MORHEATE', 'KOEEDSW1', 'TRAMCKW', 'FAISOUN', 'SAWSAWN', 'CLIHOLE', 'CHAMARN', 'CLE24THN']
# 2. Full stops
db_GTFS = client.cota_gtfs
col_stop = db_GTFS[gtfsSeconds + '_stops']
rl_stop = list(col_stop.find({}))
sampledStopsList = []
for i in rl_stop:
sampledStopsList.append(i["stop_id"])
# # 3. Sampled stops
# dbStops = client.cota_gtfs[str(GTFSTimestamp) + "_stops"]
# allStopsList = dbStops.find({})
# sampledStopsList = []
# sampler = 0
# for eachStops in allStopsList:
# if sampler % sampleRate == 1:
# sampledStopsList.append(eachStops["stop_id"])
# sampler += 1
todayTimestampList = []
# for i in range(24*numberOfTimeSamples):
# todayTimestampList.append(todaySeconds + i* 60*60/numberOfTimeSamples)
for i in [8]:
# for i in list(range(6,24)):
todayTimestampList.append(todaySeconds + i* 60*60/numberOfTimeSamples)
# print("Date: ", singleDate, "; Stops: ", len(sampledStopsList))
# todayTimestampList = [todaySeconds + 28800, todaySeconds + 46800, todaySeconds + 64800]
for eachTimestamp in todayTimestampList:
print("******************", singleDate, eachTimestamp, "******************")
# if eachTimestamp < 1567598400: # Restart point add_20190904_1567598400
# continue
# if eachTimestamp != 1563192000: # debug and restart
# continue
args = [int(eachTimestamp), walkingDistanceLimit, timeDeltaLimit, walkingSpeed, scooterSpeed, scooterDistanceLimit, isRealTime, isScooter]
resultsFeedback = collectiveAccessibilitySolve(args, sampledStopsList)
# resultsFeedback = collectiveAccessibilitySolve(args, ["3RDCAMW"])
# testStopID = "3RDCAMW"
# resultsFeedback = singleAccessibilitySolve(args, testStopID)
# print("eachTimestamp:", int(eachTimestamp), "results lens: ", len(resultsFeedback))
print("******************", singleDate, eachTimestamp, "******************")
# break
# break