-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathraptor.go
576 lines (437 loc) · 14.4 KB
/
raptor.go
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
package bifrost
import (
"fmt"
"github.com/Vector-Hector/fptf"
util "github.com/Vector-Hector/goutil"
"time"
)
type SourceKey struct {
StopKey uint64 // stop key in RoutingData
Departure time.Time // departure time
}
type SourceLocation struct {
Location *fptf.Location
Departure time.Time
}
func timeToMs(day time.Time) uint64 {
return uint64(day.UnixMilli())
}
func (b *Bifrost) Route(rounds *Rounds, origins []SourceLocation, dest *fptf.Location, modes []fptf.Mode, debug bool) (*fptf.Journey, error) {
vehicleType, isTransit := getVehicleType(modes)
originKeys, err := b.matchSourceLocations(origins, vehicleType)
if err != nil {
return nil, err
}
destKey, err := b.matchTargetLocation(dest, vehicleType)
if err != nil {
return nil, err
}
var journey *fptf.Journey
if !isTransit {
journey, err = b.RouteOnlyTimeIndependent(rounds, originKeys, destKey, vehicleType, debug)
} else {
journey, err = b.RouteTransit(rounds, originKeys, destKey, debug)
}
if err != nil {
return nil, err
}
b.addSourceAndDestination(journey, origins, dest)
return journey, nil
}
func getVehicleType(modes []fptf.Mode) (VehicleType, bool) {
vehicleType := VehicleTypeWalking
for _, mode := range modes {
if mode == fptf.ModeTrain || mode == fptf.ModeBus {
return 0, true
}
if mode == fptf.ModeBicycle {
vehicleType = VehicleTypeBicycle
} else if mode == fptf.ModeCar {
vehicleType = VehicleTypeCar
}
}
return vehicleType, false
}
func (b *Bifrost) RouteTransit(rounds *Rounds, origins []SourceKey, destKey uint64, debug bool) (*fptf.Journey, error) {
// todo add vehicle support (take more of the vehicle bitmask into account, what if bicycle is taken with you on the train?)
// what if bicycle is taken with you on a car? what if that car is going to a train station and you take the bicycle with you on the train?
// todo investigate graph issues: some vertices are not reachable and can only be reached by choosing a close vertex as destKey instead
t := time.Now()
rounds.NewSession()
if debug {
fmt.Println("resetting rounds took", time.Since(t))
t = time.Now()
}
if debug {
fmt.Println("finding routes to", destKey)
fmt.Println("origins:")
for _, origin := range origins {
fmt.Println("stop", origin.StopKey, "at", origin.Departure)
}
fmt.Println("Data stats:")
b.Data.PrintStats()
}
calcStart := time.Now()
for _, origin := range origins {
departure := timeToMs(origin.Departure)
rounds.Rounds[0][origin.StopKey] = StopArrival{Arrival: departure, Trip: TripIdOrigin, Vehicles: 1 << VehicleTypeWalking}
rounds.MarkedStops[origin.StopKey] = true
rounds.EarliestArrivals[origin.StopKey] = departure
}
lastRound := 0
if debug {
fmt.Println("setting up took", time.Since(t))
t = time.Now()
}
for k := 0; k < b.TransferLimit+1; k++ {
if debug {
fmt.Println("------ Round", k, "------")
}
ttsKey := k * 2
t = time.Now()
b.runRaptorRound(rounds, destKey, ttsKey, debug)
for _, sa := range rounds.Rounds[ttsKey] {
if sa.Arrival < uint64(DayInMs*2) {
panic("arrival too small")
}
}
if debug {
fmt.Println("Getting trip times took", time.Since(t))
t = time.Now()
fmt.Println("Marked", len(rounds.MarkedStops), "stops for trips")
}
if k == 0 {
for _, origin := range origins {
rounds.MarkedStops[origin.StopKey] = true
}
}
if debug {
fmt.Println("num reached stops:", len(rounds.Rounds[ttsKey]))
}
for stop, marked := range rounds.MarkedStops {
rounds.MarkedStopsForTransfer[stop] = marked
}
b.runTransferRound(rounds, destKey, ttsKey+1, VehicleTypeWalking, false)
if debug {
fmt.Println("Getting transfer times took", time.Since(t))
}
if debug {
fmt.Println("Marked", len(rounds.MarkedStops), "stops in total in round", k)
}
if debug {
fmt.Println("num reached stops:", len(rounds.Rounds[ttsKey+1]))
}
if len(rounds.MarkedStops) == 0 {
break
}
lastRound = ttsKey + 2
}
if debug {
fmt.Println("Done in", time.Since(calcStart))
}
_, ok := rounds.EarliestArrivals[destKey]
if !ok {
// add an unrestricted transfer round
// first, mark all vertices that are reachable already
for vert := range rounds.EarliestArrivals {
rounds.MarkedStopsForTransfer[vert] = true
}
// then, run a transfer round
b.runTransferRound(rounds, destKey, lastRound, VehicleTypeWalking, true)
lastRound++
}
_, ok = rounds.EarliestArrivals[destKey]
if !ok {
// look for very close, walkable vertices
loc := b.Data.Vertices[destKey]
nearest := b.Data.WalkableVertexTree.KNN(&loc, 30)
for _, point := range nearest {
streetVert := point.(*GeoPoint)
_, ok = rounds.EarliestArrivals[streetVert.VertKey]
if !ok {
continue
}
if !b.fastDistWithin(&loc, streetVert, b.MaxStopsConnectionSeconds) {
break
}
dist := b.DistanceMs(&loc, streetVert, VehicleTypeWalking)
if dist > b.MaxStopsConnectionSeconds {
break
}
destKey = streetVert.VertKey // replace destination with the closest reachable vertex
break
}
}
_, ok = rounds.EarliestArrivals[destKey]
if !ok {
return nil, NoRouteError(true)
}
journey := b.ReconstructJourney(destKey, lastRound, rounds)
if debug {
fmt.Println("max tts size", len(rounds.Rounds[lastRound]))
dep := journey.GetDeparture()
arr := journey.GetArrival()
origin := journey.GetOrigin().GetName()
destination := journey.GetDestination().GetName()
fmt.Println("Journey from", origin, "to", destination, "took", arr.Sub(dep), ". dep", dep, ", arr", arr)
fmt.Println("Journey:")
util.PrintJSON(journey)
}
return journey, nil
}
func (b *Bifrost) runRaptorRound(rounds *Rounds, target uint64, current int, debug bool) {
t := time.Now()
round := rounds.Rounds[current]
next := rounds.Rounds[current+1]
for stop, stopArr := range round {
next[stop] = StopArrival{
Arrival: stopArr.Arrival,
Trip: TripIdNoChange,
}
}
if debug {
fmt.Println("copying tts took", time.Since(t))
t = time.Now()
}
// clear queue
for k := range rounds.Queue {
delete(rounds.Queue, k)
}
if debug {
fmt.Println("clearing queue took", time.Since(t))
t = time.Now()
}
// add routes to queue
for stop := range rounds.MarkedStops {
for _, pair := range b.Data.StopToRoutes[stop] {
enter, ok := rounds.Queue[pair.Route]
if !ok {
rounds.Queue[pair.Route] = pair.StopKeyInTrip
continue
}
if enter < pair.StopKeyInTrip {
continue
}
rounds.Queue[pair.Route] = pair.StopKeyInTrip
}
delete(rounds.MarkedStops, stop)
}
if debug {
fmt.Println("adding trips to queue took", time.Since(t))
t = time.Now()
fmt.Println("q size", len(rounds.Queue))
}
numVisited := 0
// scan routes
for routeKey, enterKey := range rounds.Queue {
route := b.Data.Routes[routeKey]
tripKey := uint32(0)
departureDay := uint32(0)
var trip *Trip
for stopSeqKeyShifted, stopKey := range route.Stops[enterKey:] {
stopSeqKey := enterKey + uint32(stopSeqKeyShifted)
numVisited++
if trip != nil {
arr := trip.StopTimes[stopSeqKey].ArrivalAtDay(uint64(departureDay))
ea, ok := rounds.EarliestArrivals[stopKey]
targetEa, targetOk := rounds.EarliestArrivals[target]
if (!ok || arr < ea) && (!targetOk || arr < targetEa) {
next[stopKey] = StopArrival{
Arrival: arr,
Trip: tripKey,
EnterKey: uint64(stopSeqKey),
Departure: uint64(departureDay),
}
rounds.MarkedStops[stopKey] = true
rounds.EarliestArrivals[stopKey] = arr
}
}
sa, ok := round[stopKey]
if ok && (trip == nil || sa.Arrival <= trip.StopTimes[stopSeqKey].ArrivalAtDay(uint64(departureDay))) {
et, key, depDay := b.Data.earliestTrip(routeKey, stopSeqKey, sa.Arrival+b.TransferPaddingMs)
if et != nil {
trip = et
tripKey = key
departureDay = depDay
}
}
}
}
if debug {
fmt.Println("scanning trips took", time.Since(t))
fmt.Println("visited", numVisited, "stops")
}
}
func (r *RoutingData) tripRunsOnDay(trip *Trip, day uint32) bool {
service := r.Services[trip.Service]
if daysSliceContains(service.RemovedExceptions, day) {
return false
}
if daysSliceContains(service.AddedExceptions, day) {
return true
}
if day < service.StartDay || day > service.EndDay {
return false
}
// day is relative to unix epoch, which was a thursday
// our weekdays are relative to monday, so we need to add 4 to the day
weekday := (day + 4) % 7
if (service.Weekdays & (1 << weekday)) == 0 {
return false
}
return true
}
func daysSliceContains(days []uint32, day uint32) bool {
if len(days) == 0 {
return false
}
key := binarySearchDays(days, day)
return days[key] == day
}
func binarySearchDays(days []uint32, day uint32) int {
left := 0
right := len(days) - 1
for left < right {
mid := (left + right) / 2
if days[mid] < day {
left = mid + 1
} else {
right = mid
}
}
return left
}
func (r *RoutingData) earliestTrip(routeKey uint32, stopSeqKey uint32, minDeparture uint64) (*Trip, uint32, uint32) {
day := uint32(minDeparture / uint64(DayInMs))
minDepartureInDay := uint32(minDeparture % uint64(DayInMs))
for i := uint32(0); i <= r.MaxTripDayLength; i++ {
trip, key := r.earliestTripInDay(routeKey, stopSeqKey, minDepartureInDay+i*DayInMs, day)
if trip != nil {
return trip, key, day
}
day--
}
return nil, 0, 0
}
func (r *RoutingData) earliestTripInDay(routeKey uint32, stopSeqKey uint32, minDepartureInDay uint32, day uint32) (*Trip, uint32) {
route := r.Routes[routeKey]
routeStopKey := uint64(routeKey)<<32 | uint64(stopSeqKey)
reorder, ok := r.Reorders[routeStopKey]
if !ok {
return r.earliestTripOrdered(route, stopSeqKey, minDepartureInDay, day)
}
return r.earliestTripReordered(route, stopSeqKey, minDepartureInDay, day, reorder)
}
func (r *RoutingData) earliestTripOrdered(route *Route, stopSeqKey uint32, minDepartureInDay uint32, day uint32) (*Trip, uint32) {
if r.Trips[route.Trips[0]].StopTimes[stopSeqKey].Departure >= minDepartureInDay {
return r.earliestExistentTripOrdered(route, day, 0)
}
if r.Trips[route.Trips[len(route.Trips)-1]].StopTimes[stopSeqKey].Departure < minDepartureInDay {
return nil, 0
}
return r.earliestTripBinarySearch(route, stopSeqKey, minDepartureInDay, day, 0, len(route.Trips)-1)
}
func (r *RoutingData) earliestExistentTripOrdered(route *Route, day uint32, indexStart int) (*Trip, uint32) {
for i := indexStart; i < len(route.Trips); i++ {
trip := r.Trips[route.Trips[i]]
if r.tripRunsOnDay(trip, day) {
return trip, route.Trips[i]
}
}
return nil, 0
}
// binary searches for the earliest trip, starting later than minDepartureInDay at stopSeqKey.
// this assumes that left is below minDeparture and right is above minDeparture
func (r *RoutingData) earliestTripBinarySearch(route *Route, stopSeqKey uint32, minDepartureInDay uint32, day uint32, left int, right int) (*Trip, uint32) {
mid := (left + right) / 2
if left == mid {
return r.earliestExistentTripOrdered(route, day, right)
}
trip := r.Trips[route.Trips[mid]]
dep := trip.StopTimes[stopSeqKey].Departure
if dep < minDepartureInDay {
return r.earliestTripBinarySearch(route, stopSeqKey, minDepartureInDay, day, mid, right)
}
return r.earliestTripBinarySearch(route, stopSeqKey, minDepartureInDay, day, left, mid)
}
func (r *RoutingData) earliestExistentTripReordered(route *Route, day uint32, indexStart int, reorder []uint32) (*Trip, uint32) {
for i := indexStart; i < len(route.Trips); i++ {
trip := r.Trips[route.Trips[reorder[i]]]
if r.tripRunsOnDay(trip, day) {
return trip, route.Trips[reorder[i]]
}
}
return nil, 0
}
func (r *RoutingData) earliestTripReordered(route *Route, stopSeqKey uint32, minDepartureInDay uint32, day uint32, reorder []uint32) (*Trip, uint32) {
if r.Trips[route.Trips[reorder[0]]].StopTimes[stopSeqKey].Departure >= minDepartureInDay {
return r.earliestExistentTripReordered(route, day, 0, reorder)
}
if r.Trips[route.Trips[reorder[len(route.Trips)-1]]].StopTimes[stopSeqKey].Departure < minDepartureInDay {
return nil, 0
}
return r.earliestTripBinarySearchReordered(route, stopSeqKey, minDepartureInDay, day, reorder, 0, len(route.Trips)-1)
}
// binary searches for the earliest trip, starting later than minDeparture at stopSeqKey.
// this assumes that left is below minDeparture and right is above minDeparture
func (r *RoutingData) earliestTripBinarySearchReordered(route *Route, stopSeqKey uint32, minDepartureInDay uint32, day uint32, reorder []uint32, left int, right int) (*Trip, uint32) {
mid := (left + right) / 2
if left == mid {
return r.earliestExistentTripReordered(route, day, right, reorder)
}
trip := r.Trips[route.Trips[reorder[mid]]]
dep := trip.StopTimes[stopSeqKey].Departure
if dep < minDepartureInDay {
return r.earliestTripBinarySearchReordered(route, stopSeqKey, minDepartureInDay, day, reorder, mid, right)
}
return r.earliestTripBinarySearchReordered(route, stopSeqKey, minDepartureInDay, day, reorder, left, mid)
}
func (b *Bifrost) matchSourceLocations(origins []SourceLocation, vehicleToStart VehicleType) ([]SourceKey, error) {
originKeys := make([]SourceKey, 0)
tree := b.Data.WalkableVertexTree
if vehicleToStart == VehicleTypeBicycle {
tree = b.Data.CycleableVertexTree
} else if vehicleToStart == VehicleTypeCar {
tree = b.Data.CarableVertexTree
}
for _, origin := range origins {
loc := &GeoPoint{
Latitude: origin.Location.Latitude,
Longitude: origin.Location.Longitude,
}
vertices := tree.KNN(loc, 30)
for _, vertex := range vertices {
point := vertex.(*GeoPoint)
dist := b.DistanceMs(loc, point, vehicleToStart)
originKeys = append(originKeys, SourceKey{
StopKey: point.VertKey,
Departure: origin.Departure.Add(time.Duration(dist) * time.Millisecond),
})
}
}
if len(originKeys) == 0 {
return nil, fmt.Errorf("no origin vertex found for any provided location")
}
return originKeys, nil
}
func (b *Bifrost) matchTargetLocation(dest *fptf.Location, vehicleToReach VehicleType) (uint64, error) {
loc := &GeoPoint{
Latitude: dest.Latitude,
Longitude: dest.Longitude,
}
tree := b.Data.WalkableVertexTree
if vehicleToReach == VehicleTypeBicycle {
tree = b.Data.CycleableVertexTree
} else if vehicleToReach == VehicleTypeCar {
tree = b.Data.CarableVertexTree
}
vertices := tree.KNN(loc, 30)
if len(vertices) == 0 {
return 0, fmt.Errorf("no stop within tolerance found for location %v", loc)
}
for _, vert := range vertices {
point := vert.(*GeoPoint)
return point.VertKey, nil
}
return 0, fmt.Errorf("no destination vertex found for location %v", loc)
}