1
1
"""
2
2
shortest_path([PathAlgorithm,]
3
- g::OSMGraph ,
3
+ g::AbstractOSMGraph ,
4
4
origin::Union{Integer,Node},
5
5
destination::Union{Integer,Node},
6
6
[weights::AbstractMatrix=g.weights;
@@ -21,7 +21,7 @@ Calculates the shortest path between two OpenStreetMap node ids.
21
21
Faster for small graphs and/or long paths.
22
22
- `AStarDict`: A* algorithm using the `Dict` implementation.
23
23
Faster for large graphs and/or short paths.
24
- - `g::OSMGraph {U,T,W}`: Graph container.
24
+ - `g::AbstractOSMGraph {U,T,W}`: Graph container.
25
25
- `origin::Union{Integer,Node}`: Origin OpenStreetMap node or node id.
26
26
- `destination::Union{Integer,Node},`: Destination OpenStreetMap node or node
27
27
id.
@@ -45,7 +45,7 @@ Calculates the shortest path between two OpenStreetMap node ids.
45
45
the shortest path.
46
46
"""
47
47
function shortest_path (:: Type{A} ,
48
- g:: OSMGraph {U,T,W} ,
48
+ g:: AbstractOSMGraph {U,T,W} ,
49
49
origin:: DEFAULT_OSM_ID_TYPE ,
50
50
destination:: DEFAULT_OSM_ID_TYPE ,
51
51
weights:: AbstractMatrix{W} ;
@@ -59,7 +59,7 @@ function shortest_path(::Type{A},
59
59
return index_to_node_id (g, path)
60
60
end
61
61
function shortest_path (:: Type{A} ,
62
- g:: OSMGraph {U,T,W} ,
62
+ g:: AbstractOSMGraph {U,T,W} ,
63
63
origin:: DEFAULT_OSM_ID_TYPE ,
64
64
destination:: DEFAULT_OSM_ID_TYPE ,
65
65
weights:: AbstractMatrix{W} ;
@@ -73,50 +73,50 @@ function shortest_path(::Type{A},
73
73
isnothing (path) && return
74
74
return index_to_node_id (g, path)
75
75
end
76
- function shortest_path (:: Type{A} , g:: OSMGraph {U,T,W} , origin:: DEFAULT_OSM_ID_TYPE , destination:: DEFAULT_OSM_ID_TYPE ; kwargs... ):: Union{Nothing,Vector{T}} where {A <: PathAlgorithm , U, T, W}
76
+ function shortest_path (:: Type{A} , g:: AbstractOSMGraph {U,T,W} , origin:: DEFAULT_OSM_ID_TYPE , destination:: DEFAULT_OSM_ID_TYPE ; kwargs... ):: Union{Nothing,Vector{T}} where {A <: PathAlgorithm , U, T, W}
77
77
return shortest_path (A, g, origin, destination, g. weights; kwargs... )
78
78
end
79
- function shortest_path (:: Type{A} , g:: OSMGraph {U,T,W} , origin:: Node{<:DEFAULT_OSM_ID_TYPE} , destination:: Node{<:DEFAULT_OSM_ID_TYPE} , args... ; kwargs... ):: Union{Nothing,Vector{T}} where {A <: PathAlgorithm , U, T, W}
79
+ function shortest_path (:: Type{A} , g:: AbstractOSMGraph {U,T,W} , origin:: Node{<:DEFAULT_OSM_ID_TYPE} , destination:: Node{<:DEFAULT_OSM_ID_TYPE} , args... ; kwargs... ):: Union{Nothing,Vector{T}} where {A <: PathAlgorithm , U, T, W}
80
80
return shortest_path (A, g, origin. id, destination. id, args... ; kwargs... )
81
81
end
82
- function shortest_path (g:: OSMGraph {U,T,W} , args... ; kwargs... ):: Union{Nothing,Vector{T}} where {U, T, W}
82
+ function shortest_path (g:: AbstractOSMGraph {U,T,W} , args... ; kwargs... ):: Union{Nothing,Vector{T}} where {U, T, W}
83
83
return shortest_path (Dijkstra, g, args... ; kwargs... )
84
84
end
85
85
86
86
"""
87
- set_dijkstra_state!(g::OSMGraph , src::Union{Integer,Vecotr{<:Integer}, weights::AbstractMatrix; cost_adjustment::Function=(u, v, parents) -> 0.0)
87
+ set_dijkstra_state!(g::AbstractOSMGraph , src::Union{Integer,Vecotr{<:Integer}, weights::AbstractMatrix; cost_adjustment::Function=(u, v, parents) -> 0.0)
88
88
89
89
Compute and set the dijkstra parent states for one or multiple src vertices. Threads are used for multiple srcs.
90
90
Note, computing dijkstra states for all vertices is a O(V² + ElogV) operation, use on large graphs with caution.
91
91
"""
92
- function set_dijkstra_state! (g:: OSMGraph , src:: Integer , weights:: AbstractMatrix ; cost_adjustment:: Function = (u, v, parents) -> 0.0 )
92
+ function set_dijkstra_state! (g:: AbstractOSMGraph , src:: Integer , weights:: AbstractMatrix ; cost_adjustment:: Function = (u, v, parents) -> 0.0 )
93
93
g. dijkstra_states[src] = dijkstra (g. graph, weights, src; cost_adjustment= cost_adjustment)
94
94
end
95
- function set_dijkstra_state! (g:: OSMGraph , srcs:: Vector{<:Integer} , weights:: AbstractMatrix ; cost_adjustment:: Function = (u, v, parents) -> 0.0 )
95
+ function set_dijkstra_state! (g:: AbstractOSMGraph , srcs:: Vector{<:Integer} , weights:: AbstractMatrix ; cost_adjustment:: Function = (u, v, parents) -> 0.0 )
96
96
Threads. @threads for src in srcs
97
97
set_dijkstra_state! (g, src, weights; cost_adjustment= cost_adjustment)
98
98
end
99
99
return g
100
100
end
101
- set_dijkstra_state! (g:: OSMGraph , src; kwargs... ) = set_dijkstra_state! (g, src, g. weights; kwargs... )
101
+ set_dijkstra_state! (g:: AbstractOSMGraph , src; kwargs... ) = set_dijkstra_state! (g, src, g. weights; kwargs... )
102
102
103
103
"""
104
- shortest_path_from_dijkstra_state(g::OSMGraph , origin::Integer, destination::Integer)
104
+ shortest_path_from_dijkstra_state(g::AbstractOSMGraph , origin::Integer, destination::Integer)
105
105
106
106
Extract shortest path from precomputed dijkstra state, from `origin` to `detination` node id.
107
107
108
108
Note, function will raise `UndefRefError: access to undefined reference` if the dijkstra state of the
109
109
origin node is not precomputed.
110
110
111
111
# Arguments
112
- - `g::OSMGraph `: Graph container.
112
+ - `g::AbstractOSMGraph `: Graph container.
113
113
- `origin::Integer`: Origin OpenStreetMap node or node id.
114
114
- `destination::Integer`: Destination OpenStreetMap node or node id.
115
115
116
116
# Return
117
117
- `Union{Nothing,Vector{T}}`: Array of OpenStreetMap node ids making up the shortest path.
118
118
"""
119
- function shortest_path_from_dijkstra_state (g:: OSMGraph , origin:: Integer , destination:: Integer )
119
+ function shortest_path_from_dijkstra_state (g:: AbstractOSMGraph , origin:: Integer , destination:: Integer )
120
120
parents = node_id_to_dijkstra_state (g, origin)
121
121
path = path_from_parents (parents, node_id_to_index (g, destination))
122
122
isnothing (path) && return
@@ -186,7 +186,7 @@ function restriction_cost(restrictions::AbstractDict{V,Vector{MutableLinkedList{
186
186
end
187
187
188
188
"""
189
- restriction_cost_adjustment(g::OSMGraph )
189
+ restriction_cost_adjustment(g::AbstractOSMGraph )
190
190
191
191
Returns the cost adjustment function (user in dijkstra and astar) for restrictions. The return function
192
192
takes 3 arguments, `u` being the current node, `v` being the neighbour node, `parents` being the array
@@ -195,17 +195,27 @@ of parent dijkstra states. By default `g.indexed_restrictions` is used to check
195
195
"""
196
196
restriction_cost_adjustment (g:: OSMGraph ) = (u, v, parents) -> restriction_cost (g. indexed_restrictions, u, v, parents)
197
197
198
+ """
199
+ restriction_cost_adjustment(g::SinplifiedOSMGraph)
200
+
201
+ Returns the cost adjustment function (user in dijkstra and astar) for restrictions. The return function
202
+ takes 3 arguments, `u` being the current node, `v` being the neighbour node, `parents` being the array
203
+ of parent dijkstra states. By default `g.indexed_restrictions` is used to check whether the path from
204
+ `u` to `v` is restricted given all previous nodes in `parents`.
205
+ """
206
+ restriction_cost_adjustment (g:: SimplifiedOSMGraph ) = (u, v, parents) -> restriction_cost (g. parent. indexed_restrictions, u, v, parents)
207
+
198
208
"""
199
209
distance_heuristic(g::OSMGraph)
200
210
201
211
Returns the heuristic function used in astar shortest path calculation, should be used with a graph with
202
212
`weight_type=:distance`. The heuristic function takes in 2 arguments, `u` being the current node and `v`
203
213
being the neighbour node, and returns the haversine distance between them.
204
214
"""
205
- distance_heuristic (g:: OSMGraph ) = (u, v) -> haversine (g. node_coordinates[u], g. node_coordinates[v])
215
+ distance_heuristic (g:: AbstractOSMGraph ) = (u, v) -> haversine (g. node_coordinates[u], g. node_coordinates[v])
206
216
207
217
"""
208
- time_heuristic(g::OSMGraph )
218
+ time_heuristic(g::AbstractOSMGraph )
209
219
210
220
Returns the heuristic function used in astar shortest path calculation, should be used with a graph with
211
221
`weight_type=:time` or `weight_type=:lane_efficiency`. The heuristic function takes in 2 arguments, `u`
@@ -214,40 +224,40 @@ Calculated by dividing the harversine distance by a fixed maxspeed of `100`. Rem
214
224
path, it is important to pick an *underestimating* heuristic that best estimates the cost remaining to the `goal`,
215
225
hence we pick the largest maxspeed across all ways.
216
226
"""
217
- time_heuristic (g:: OSMGraph ) = (u, v) -> haversine (g. node_coordinates[u], g. node_coordinates[v]) / 100.0
227
+ time_heuristic (g:: AbstractOSMGraph ) = (u, v) -> haversine (g. node_coordinates[u], g. node_coordinates[v]) / 100.0
218
228
219
229
"""
220
- weights_from_path(g::OSMGraph {U,T,W}, path::Vector{T}; weights=g.weights)::Vector{W} where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
230
+ weights_from_path(g::AbstractOSMGraph {U,T,W}, path::Vector{T}; weights=g.weights)::Vector{W} where {U <: DEFAULT_OSM_INDEX_TYPE,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
221
231
222
232
Extracts edge weights from a path using the weight matrix stored in `g.weights` unless
223
233
a different matrix is passed to the `weights` kwarg.
224
234
225
235
# Arguments
226
- - `g::OSMGraph `: Graph container.
236
+ - `g::AbstractOSMGraph `: Graph container.
227
237
- `path::Vector{T}`: Array of OpenStreetMap node ids.
228
238
- `weights=g.weights`: the matrix that the edge weights are extracted from. Defaults to `g.weights`.
229
239
230
240
# Return
231
241
- `Vector{W}`: Array of edge weights, distances are in km, time is in hours.
232
242
"""
233
- function weights_from_path (g:: OSMGraph {U,T,W} , path:: Vector{T} ; weights= g. weights):: Vector{W} where {U <: DEFAULT_OSM_INDEX_TYPE ,T <: DEFAULT_OSM_ID_TYPE ,W <: Real }
243
+ function weights_from_path (g:: AbstractOSMGraph {U,T,W} , path:: Vector{T} ; weights= g. weights):: Vector{W} where {U <: DEFAULT_OSM_INDEX_TYPE ,T <: DEFAULT_OSM_ID_TYPE ,W <: Real }
234
244
return [weights[g. node_to_index[path[i]], g. node_to_index[path[i + 1 ]]] for i in 1 : length (path) - 1 ]
235
245
end
236
246
237
247
"""
238
- total_path_weight(g::OSMGraph {U,T,W}, path::Vector{T}; weights=g.weights)::W where {U <: Integer,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
248
+ total_path_weight(g::AbstractOSMGraph {U,T,W}, path::Vector{T}; weights=g.weights)::W where {U <: Integer,T <: DEFAULT_OSM_ID_TYPE,W <: Real}
239
249
240
250
Extract total edge weight along a path.
241
251
242
252
# Arguments
243
- - `g::OSMGraph `: Graph container.
253
+ - `g::AbstractOSMGraph `: Graph container.
244
254
- `path::Vector{T}`: Array of OpenStreetMap node ids.
245
255
- `weights=g.weights`: the matrix that the edge weights are extracted from. Defaults to `g.weights`.
246
256
247
257
# Return
248
258
- `sum::W`: Total path edge weight, distances are in km, time is in hours.
249
259
"""
250
- function total_path_weight (g:: OSMGraph {U,T,W} , path:: Vector{T} ; weights= g. weights):: W where {U <: Integer ,T <: DEFAULT_OSM_ID_TYPE ,W <: Real }
260
+ function total_path_weight (g:: AbstractOSMGraph {U,T,W} , path:: Vector{T} ; weights= g. weights):: W where {U <: Integer ,T <: DEFAULT_OSM_ID_TYPE ,W <: Real }
251
261
sum:: W = zero (W)
252
262
for i in 1 : length (path) - 1
253
263
sum += weights[g. node_to_index[path[i]], g. node_to_index[path[i + 1 ]]]
0 commit comments