-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathastar.go
243 lines (190 loc) · 5.45 KB
/
astar.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
package bifrost
import (
"container/heap"
"fmt"
"github.com/Vector-Hector/fptf"
util "github.com/Vector-Hector/goutil"
"time"
)
type VehicleType uint32
const (
VehicleTypeCar VehicleType = iota
VehicleTypeBicycle
VehicleTypeWalking
)
type dijkstraNode struct {
Arrival uint64
Vertex uint64
TransferTime uint32 // time in ms to walk or cycle to this stop
Score uint64
Index int // Index of the node in the heap
}
type priorityQueue []*dijkstraNode
func (pq *priorityQueue) Len() int {
return len(*pq)
}
func (pq *priorityQueue) Less(i, j int) bool {
l := *pq
return l[i].Score < l[j].Score
}
func (pq *priorityQueue) Swap(i, j int) {
l := *pq
l[i], l[j] = l[j], l[i]
l[i].Index = i
l[j].Index = j
}
func (pq *priorityQueue) Push(x interface{}) {
*pq = append(*pq, x.(*dijkstraNode))
}
func (pq *priorityQueue) Pop() interface{} {
old := *pq
n := len(old)
x := old[n-1]
x.Index = -1 // for safety
*pq = old[:n-1]
return x
}
func (pq *priorityQueue) update(node *dijkstraNode, arrival uint64, targetWalkTime uint32) {
node.Arrival = arrival
node.TransferTime = targetWalkTime
heap.Fix(pq, node.Index)
}
func (b *Bifrost) RouteOnlyTimeIndependent(rounds *Rounds, origins []SourceKey, destKey uint64, vehicle VehicleType, debug bool) (*fptf.Journey, error) {
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()
}
for _, origin := range origins {
departure := timeToMs(origin.Departure)
rounds.Rounds[0][origin.StopKey] = StopArrival{Arrival: departure, Trip: TripIdOrigin, Vehicles: 1 << vehicle}
rounds.MarkedStopsForTransfer[origin.StopKey] = true
rounds.EarliestArrivals[origin.StopKey] = departure
}
b.runTransferRound(rounds, destKey, 0, vehicle, true)
if debug {
fmt.Println("Getting transfer times took", time.Since(t))
}
_, ok := rounds.EarliestArrivals[destKey]
if !ok {
panic(NoRouteError(true))
}
journey := b.ReconstructJourney(destKey, 1, rounds)
if debug {
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) runTransferRound(rounds *Rounds, target uint64, current int, vehicle VehicleType, noTransferCap bool) {
round := rounds.Rounds[current]
next := rounds.Rounds[current+1]
for stop, t := range round {
next[stop] = StopArrival{
Arrival: t.Arrival,
Trip: TripIdNoChange,
Vehicles: t.Vehicles,
}
}
queue := make(priorityQueue, 0)
heap.Init(&queue)
targetVertex := &b.Data.Vertices[target]
// perform dijkstra on street graph
for stop, marked := range rounds.MarkedStopsForTransfer {
if !marked {
continue
}
sa, ok := next[stop]
if !ok {
continue
}
if sa.Vehicles&(1<<vehicle) == 0 && vehicle != VehicleTypeWalking { // foot is always allowed
continue
}
heap.Push(&queue, &dijkstraNode{
Arrival: sa.Arrival,
Vertex: stop,
TransferTime: sa.TransferTime,
Score: sa.Arrival + b.HeuristicMs(&b.Data.Vertices[stop], targetVertex, vehicle),
})
delete(rounds.MarkedStopsForTransfer, stop)
}
tripType := TripIdWalk
if vehicle == VehicleTypeBicycle {
tripType = TripIdCycle
} else if vehicle == VehicleTypeCar {
tripType = TripIdCar
}
nodeMap := make(map[uint64]*dijkstraNode)
for queue.Len() > 0 {
node := heap.Pop(&queue).(*dijkstraNode)
delete(nodeMap, node.Vertex)
arcs := b.Data.StreetGraph[node.Vertex]
for _, arc := range arcs {
dist := arc.WalkDistance
if vehicle == VehicleTypeBicycle {
dist = arc.CycleDistance
}
if vehicle == VehicleTypeCar {
dist = arc.CarDistance
}
if dist == 0 {
continue
}
targetTransferTime := node.TransferTime + dist
if !noTransferCap && vehicle == VehicleTypeWalking && targetTransferTime > b.MaxWalkingMs {
continue
}
if !noTransferCap && vehicle == VehicleTypeBicycle && targetTransferTime > b.MaxCyclingMs {
continue
}
arrival := node.Arrival + uint64(dist)
ea, ok := rounds.EarliestArrivals[arc.Target]
targetEa, targetOk := rounds.EarliestArrivals[target]
if (ok && ea <= arrival) || (targetOk && targetEa <= arrival) {
continue
}
next[arc.Target] = StopArrival{
Arrival: arrival,
Trip: tripType,
EnterKey: node.Vertex,
Departure: node.Arrival,
TransferTime: targetTransferTime,
Vehicles: 1 << vehicle,
}
rounds.MarkedStops[arc.Target] = true
rounds.EarliestArrivals[arc.Target] = arrival
targetNode, ok := nodeMap[arc.Target]
if ok {
queue.update(targetNode, arrival, targetTransferTime)
continue
}
targetNode = &dijkstraNode{
Arrival: arrival,
Vertex: arc.Target,
TransferTime: targetTransferTime,
Score: arrival + b.HeuristicMs(&b.Data.Vertices[arc.Target], targetVertex, vehicle),
}
nodeMap[arc.Target] = targetNode
heap.Push(&queue, targetNode)
}
}
}
func (b *Bifrost) HeuristicMs(from, to *Vertex, vehicle VehicleType) uint64 {
return uint64(b.DistanceMs(from, to, vehicle))
}