From c0cc3a703f1f86b196fa60696624be5f140c493f Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Tue, 5 Mar 2019 19:36:39 +0100 Subject: [PATCH 01/26] Added A* search algorithm implementation --- src/OpenStreetMapX.jl | 3 ++ src/a_star.jl | 93 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 96 insertions(+) create mode 100644 src/a_star.jl diff --git a/src/OpenStreetMapX.jl b/src/OpenStreetMapX.jl index bfd4ebf..3ed4371 100644 --- a/src/OpenStreetMapX.jl +++ b/src/OpenStreetMapX.jl @@ -3,6 +3,7 @@ module OpenStreetMapX using LibExpat using LightGraphs using SparseArrays +using DataStructures using Serialization using JSON using HTTP @@ -12,6 +13,7 @@ export distance, getX, getY, getZ # Auxiliary functions to calculate distances export center, inbounds, onbounds, boundary_point #Functions for map bounds export ECEF, LLA, ENU #Conversion functions export MapData +export get_distance, a_star_algorithm #A* search algorithm implementation export extract_highways, filter_highways #Highways extraction export filter_roadways, classify_roadways, filter_walkways, classify_walkways, filter_cycleways, classify_cycleways #Filtering and classification of cars, cycles and pedestrian Highways @@ -33,6 +35,7 @@ include("types.jl") #types used in the package include("classes.jl") #grouping highways into classes for routing and plotting include("speeds.jl") # speed limits in kilometers per hour include("polyline.jl") +include("a_star.jl") include("points.jl") # points coordinates and constants include("bounds.jl") #bounds of the map include("conversion.jl") #conversion of geographical coordinates diff --git a/src/a_star.jl b/src/a_star.jl new file mode 100644 index 0000000..da6337f --- /dev/null +++ b/src/a_star.jl @@ -0,0 +1,93 @@ +""" + get_distance(A::Int, B::Int, + nodes::Dict{Int,T} , + vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} + +Auxiliary function - takes two vertices of graph and return the distance between them. +Used to compute straight line distance heuristic for A* algorithm. + +**Arguments** + +* `A` : start vertex +* `B` : end vertex +* `nodes` : dictionary of .osm nodes ID's and correspoding points coordinates in ECEF or ENU system +* `vertices_to_nodes` : dictionary mapping graph vertices to .osm file nodes; basically it is a reversed v dictionary from MapData type +""" +function get_distance(A::Int, B::Int, + nodes::Dict{Int,T} , + vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} + A,B = vertices_to_nodes[A], vertices_to_nodes[B] + OpenStreetMapX.distance(nodes[A],nodes[B]) +end + + +function extract_a_star_route(parents::Vector{Int},s::Int, u::Int) + route = Int[] + index = u + push!(route,index) + while index != s + index = parents[index] + push!(route, index) + end + reverse!(route) +end + +""" + a_star_algorithm(g::AbstractGraph{U}, + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} + +High level function - implementation of A star search algorithm (https://en.wikipedia.org/wiki/A*_search_algorithm). +Based on the implementation in LightGraphs library, however significantly improved in terms of performance. + +**Arguments** + +* `g` : graph object +* `S` : start vertex +* `t` : end vertex +* `distmx` : distance matrix +* `heuristic` : search heuristic function; by default returns zero which reduce algorithm to Dijkstra shortest path +""" +function a_star_algorithm(g::AbstractGraph{U}, # the g + s::Integer, # the start vertex + t::Integer, # the end vertex + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} + + checkbounds(distmx, Base.OneTo(nv(g)), Base.OneTo(nv(g))) + frontier = DataStructures.PriorityQueue{Tuple{T, U},T}() + frontier[(zero(T), U(s))] = zero(T) + nvg = nv(g) + visited = zeros(Bool, nvg) + dists = fill(typemax(T), nvg) + parents = zeros(U, nvg) + colormap = LightGraphs.empty_colormap(nvg) + colormap[s] = 1 + @inbounds while !isempty(frontier) + (cost_so_far, u) = dequeue!(frontier) + u == t && (return OpenStreetMapX.extract_a_star_route(parents,s,u), cost_so_far) + for v in LightGraphs.outneighbors(g, u) + if get(colormap, v, 0) < 2 + dist = distmx[u, v] + colormap[v] = 1 + path_cost = cost_so_far + dist + if !visited[v] + visited[v] = true + parents[v] = u + dists[v] = path_cost + enqueue!(frontier, + (path_cost, v), + path_cost + heuristic(v)) + elseif path_cost < dists[v] + parents[v] = u + dists[v] = path_cost + frontier[path_cost, v] = path_cost + heuristic(v) + end + end + end + colormap[u] = 2 + end + Vector{U}(), Inf +end \ No newline at end of file From 45e57b36a210f20db81b6ff1ca52543ace72e700 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Tue, 5 Mar 2019 20:05:41 +0100 Subject: [PATCH 02/26] updated --- src/a_star.jl | 24 +++++++++++++----------- 1 file changed, 13 insertions(+), 11 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index da6337f..0413e78 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -1,7 +1,7 @@ """ get_distance(A::Int, B::Int, - nodes::Dict{Int,T} , - vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} + nodes::Dict{Int,T} , + vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} Auxiliary function - takes two vertices of graph and return the distance between them. Used to compute straight line distance heuristic for A* algorithm. @@ -10,8 +10,8 @@ Used to compute straight line distance heuristic for A* algorithm. * `A` : start vertex * `B` : end vertex -* `nodes` : dictionary of .osm nodes ID's and correspoding points coordinates in ECEF or ENU system -* `vertices_to_nodes` : dictionary mapping graph vertices to .osm file nodes; basically it is a reversed v dictionary from MapData type +* `nodes` : dictionary of .osm nodes ID's and correspoding points coordinates +* `vertices_to_nodes` : dictionary mapping graph vertices to .osm file nodes """ function get_distance(A::Int, B::Int, nodes::Dict{Int,T} , @@ -34,13 +34,15 @@ end """ a_star_algorithm(g::AbstractGraph{U}, - s::Integer, - t::Integer, - distmx::AbstractMatrix{T}=LightGraphs.weights(g), - heuristic::Function = n -> zero(T)) where {T, U} + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} -High level function - implementation of A star search algorithm (https://en.wikipedia.org/wiki/A*_search_algorithm). -Based on the implementation in LightGraphs library, however significantly improved in terms of performance. +High level function - implementation of A star search algorithm: +(https://en.wikipedia.org/wiki/A*_search_algorithm). +Based on the implementation in LightGraphs library, +however significantly improved in terms of performance. **Arguments** @@ -48,7 +50,7 @@ Based on the implementation in LightGraphs library, however significantly improv * `S` : start vertex * `t` : end vertex * `distmx` : distance matrix -* `heuristic` : search heuristic function; by default returns zero which reduce algorithm to Dijkstra shortest path +* `heuristic` : search heuristic function; by default returns zero """ function a_star_algorithm(g::AbstractGraph{U}, # the g s::Integer, # the start vertex From 91647fe01d2c844ce58451ed557617e7fcf5a0b8 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Tue, 5 Mar 2019 20:40:55 +0100 Subject: [PATCH 03/26] asasa --- src/a_star.jl | 25 ++++++++++++------------- 1 file changed, 12 insertions(+), 13 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index 0413e78..b0b7d32 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -1,7 +1,7 @@ """ get_distance(A::Int, B::Int, - nodes::Dict{Int,T} , - vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} + nodes::Dict{Int,T} , + vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} Auxiliary function - takes two vertices of graph and return the distance between them. Used to compute straight line distance heuristic for A* algorithm. @@ -14,8 +14,8 @@ Used to compute straight line distance heuristic for A* algorithm. * `vertices_to_nodes` : dictionary mapping graph vertices to .osm file nodes """ function get_distance(A::Int, B::Int, - nodes::Dict{Int,T} , - vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} + nodes::Dict{Int,T}, + vertices_to_nodes::Dict{Int,Int}) where T<:Union{OpenStreetMapX.ENU,OpenStreetMapX.ECEF} A,B = vertices_to_nodes[A], vertices_to_nodes[B] OpenStreetMapX.distance(nodes[A],nodes[B]) end @@ -34,10 +34,10 @@ end """ a_star_algorithm(g::AbstractGraph{U}, - s::Integer, - t::Integer, - distmx::AbstractMatrix{T}=LightGraphs.weights(g), - heuristic::Function = n -> zero(T)) where {T, U} + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} High level function - implementation of A star search algorithm: (https://en.wikipedia.org/wiki/A*_search_algorithm). @@ -53,11 +53,10 @@ however significantly improved in terms of performance. * `heuristic` : search heuristic function; by default returns zero """ function a_star_algorithm(g::AbstractGraph{U}, # the g - s::Integer, # the start vertex - t::Integer, # the end vertex - distmx::AbstractMatrix{T}=LightGraphs.weights(g), - heuristic::Function = n -> zero(T)) where {T, U} - + s::Integer, # the start vertex + t::Integer, # the end vertex + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} checkbounds(distmx, Base.OneTo(nv(g)), Base.OneTo(nv(g))) frontier = DataStructures.PriorityQueue{Tuple{T, U},T}() frontier[(zero(T), U(s))] = zero(T) From bb53f2314b38e22bb131ce5eb431bd3b3a01d6b3 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Tue, 5 Mar 2019 20:42:19 +0100 Subject: [PATCH 04/26] I hope now it's ok --- src/a_star.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index b0b7d32..fb4e961 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -34,10 +34,10 @@ end """ a_star_algorithm(g::AbstractGraph{U}, - s::Integer, - t::Integer, - distmx::AbstractMatrix{T}=LightGraphs.weights(g), - heuristic::Function = n -> zero(T)) where {T, U} + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g), + heuristic::Function = n -> zero(T)) where {T, U} High level function - implementation of A star search algorithm: (https://en.wikipedia.org/wiki/A*_search_algorithm). From bae75f6798b0b8ba97eb6a839cd0e9a5de0337af Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Tue, 5 Mar 2019 21:01:39 +0100 Subject: [PATCH 05/26] LightGraphs prefix added --- src/a_star.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/a_star.jl b/src/a_star.jl index fb4e961..90969b1 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -52,7 +52,7 @@ however significantly improved in terms of performance. * `distmx` : distance matrix * `heuristic` : search heuristic function; by default returns zero """ -function a_star_algorithm(g::AbstractGraph{U}, # the g +function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g s::Integer, # the start vertex t::Integer, # the end vertex distmx::AbstractMatrix{T}=LightGraphs.weights(g), From 3ffb27d722464ed1ec42854d3b5dc042e6fa52b1 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Thu, 7 Mar 2019 19:07:33 +0100 Subject: [PATCH 06/26] fixed create_weights_matrix function --- src/routing.jl | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/src/routing.jl b/src/routing.jl index e016fea..11c5d76 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -86,7 +86,14 @@ end ### Create a Sparse Matrix for a given vector of weights ### function create_weights_matrix(m::OpenStreetMapX.MapData,weights::Vector{Float64}) - return SparseArrays.sparse(map(i -> m.v[i[1]], m.e), map(i -> m.v[i[2]], m.e),weights) + w = Dict{Tuple{Int,Int},Float64}() + sizehint!(w,length(weights)) + for (i,edge) in enumerate(m.e) + w[m.v[edge[1]],m.v[edge[2]]] = weights[i] + end + return SparseArrays.sparse(map(x->getfield.(collect(keys(w)), x), + fieldnames(eltype(collect(keys(w)))))..., + collect(values(w)),length(m.v),length(m.v)) end ### Extract route from Dijkstra results object ### From c41fb46a80a4e90f33413918680daa251d85718c Mon Sep 17 00:00:00 2001 From: Przemyslaw Szufel Date: Fri, 8 Mar 2019 01:25:08 +0100 Subject: [PATCH 07/26] add DataStructures dep --- Manifest.toml | 26 +++++++++++++------------- Project.toml | 1 + 2 files changed, 14 insertions(+), 13 deletions(-) diff --git a/Manifest.toml b/Manifest.toml index aea0a05..644f4b9 100644 --- a/Manifest.toml +++ b/Manifest.toml @@ -15,15 +15,15 @@ version = "0.5.3" [[CodecZlib]] deps = ["BinaryProvider", "Libdl", "Test", "TranscodingStreams"] -git-tree-sha1 = "e3df104c84dfc108f0ca203fd7f5bbdc98641ae9" +git-tree-sha1 = "36bbf5374c661054d41410dc53ff752972583b9b" uuid = "944b1d66-785c-5afd-91f1-9de20f533193" -version = "0.5.1" +version = "0.5.2" [[Compat]] deps = ["Base64", "Dates", "DelimitedFiles", "Distributed", "InteractiveUtils", "LibGit2", "Libdl", "LinearAlgebra", "Markdown", "Mmap", "Pkg", "Printf", "REPL", "Random", "Serialization", "SharedArrays", "Sockets", "SparseArrays", "Statistics", "Test", "UUIDs", "Unicode"] -git-tree-sha1 = "49269e311ffe11ac5b334681d212329002a9832a" +git-tree-sha1 = "195a3ffcb8b0762684b6821de18f83a16455c6ea" uuid = "34da2185-b29b-5c13-b0c7-acf172513d20" -version = "1.5.1" +version = "2.0.0" [[DataStructures]] deps = ["InteractiveUtils", "OrderedCollections", "Random", "Serialization", "Test"] @@ -92,19 +92,19 @@ uuid = "56ddb016-857b-54e1-b83d-db4d58db5568" [[MacroTools]] deps = ["Compat"] -git-tree-sha1 = "c443e1c8d58a4e9f61b708ad0a88286c7042145b" +git-tree-sha1 = "3fd1a3022952128935b449c33552eb65895380c1" uuid = "1914dd2f-81c6-5fcd-8719-6d5c9610ff09" -version = "0.4.4" +version = "0.4.5" [[Markdown]] deps = ["Base64"] uuid = "d6f4376e-aef5-505a-96c1-9c027394607a" [[MbedTLS]] -deps = ["BinaryProvider", "Dates", "Libdl", "Random", "Sockets", "Test"] -git-tree-sha1 = "40b4a9149f0967714991328b8155c9ff5f91e755" +deps = ["BinaryProvider", "Dates", "Distributed", "Libdl", "Random", "Sockets", "Test"] +git-tree-sha1 = "2d94286a9c2f52c63a16146bb86fd6cdfbf677c6" uuid = "739be429-bea8-5141-9913-cc70e7f3736d" -version = "0.6.7" +version = "0.6.8" [[Mmap]] uuid = "a63ad114-7e13-5084-954f-fe012c677804" @@ -156,9 +156,9 @@ uuid = "2f01184e-e22b-5df5-ae63-d93ebab69eaf" [[StaticArrays]] deps = ["InteractiveUtils", "LinearAlgebra", "Random", "Statistics", "Test"] -git-tree-sha1 = "1eb114d6e23a817cd3e99abc3226190876d7c898" +git-tree-sha1 = "3841b39ed5f047db1162627bf5f80a9cd3e39ae2" uuid = "90137ffa-7385-5640-81b9-e52037218182" -version = "0.10.2" +version = "0.10.3" [[Statistics]] deps = ["LinearAlgebra", "SparseArrays"] @@ -170,9 +170,9 @@ uuid = "8dfed614-e22c-5e08-85e1-65c5234f0b40" [[TranscodingStreams]] deps = ["Pkg", "Random", "Test"] -git-tree-sha1 = "a34a2d588e2d2825602bf14a24216d5c8b0921ec" +git-tree-sha1 = "8a032ceb5cf7a28bf1bdb77746b250b9e9fda565" uuid = "3bb67fe8-82b1-5028-8e26-92a6c54297fa" -version = "0.8.1" +version = "0.9.0" [[UUIDs]] deps = ["Random"] diff --git a/Project.toml b/Project.toml index 49c43fd..993c306 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Przemyslaw Szufel ", "Bartosz Pankratz Date: Fri, 8 Mar 2019 08:49:59 +0100 Subject: [PATCH 08/26] routing with A* added + minor fixes --- src/google_routing.jl | 4 +- src/routing.jl | 120 ++++++++++++++++++++++++++---------------- test/runtests.jl | 3 ++ 3 files changed, 81 insertions(+), 46 deletions(-) diff --git a/src/google_routing.jl b/src/google_routing.jl index 838739b..1be0215 100644 --- a/src/google_routing.jl +++ b/src/google_routing.jl @@ -158,7 +158,7 @@ function get_google_route(origin::Int,destination::Int,waypoint::Int, return OpenStreetMapX.get_google_route(origin,destination,waypoint,map_data,googleapi_key,googleapi_parameters = googleapi_parameters) else #get route based on OSM routing - warn("Google Distances API cannot get a proper results - route will be calculated with OSMSim Routing module") + @warn "Google Distances API cannot get a proper results - route will be calculated with OSMSim Routing module" if rand() < 0.5 route_nodes, distance, route_time = OpenStreetMapX.shortest_route(map_data, origin, waypoint, destination) return route_nodes, "shortest" @@ -194,7 +194,7 @@ function get_google_route(origin::Int,destination::Int, return OpenStreetMapX.get_google_route(origin,destination,map_data,googleapi_key,googleapi_parameters = googleapi_parameters) else #get route based on OSM routing - warn("Google Distances API cannot get a proper results - route will be calculated with OSMSim Routing module") + @warn "Google Distances API cannot get a proper results - route will be calculated with OSMSim Routing module" if rand() < 0.5 route_nodes, distance, route_time = OpenStreetMapX.shortest_route(map_data, origin, destination) return route_nodes, "shortest" diff --git a/src/routing.jl b/src/routing.jl index 11c5d76..220f9ea 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -143,33 +143,47 @@ calculate_distance(m::OpenStreetMapX.MapData, weights::SparseArrays.SparseMatrix ### Find Route with Given Weights ### ##################################### -function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}, get_distance::Bool = false, get_time::Bool = false) +function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, + weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; + routing::Symbol = :astar, heuristic::= n -> zero(Float64), + get_distance::Bool = false, get_time::Bool = false) result = Any[] - start_vertex = m.v[node0] - dijkstra_result = OpenStreetMapX.dijkstra(m, weights, start_vertex) - finish_vertex= m.v[node1] - route_indices, route_values = OpenStreetMapX.extract_route(dijkstra_result, start_vertex, finish_vertex) - route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) - push!(result, route_nodes, route_values) + if routing == :dijkstra + start_vertex = m.v[node0] + dijkstra_result = OpenStreetMapX.dijkstra(m, weights, start_vertex) + finish_vertex= m.v[node1] + route_indices, route_values = OpenStreetMapX.extract_route(dijkstra_result, start_vertex, finish_vertex) + route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) + push!(result, route_nodes, route_values) + elseif routing == :astar + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, node0, node1, weights, heuristic) + route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) + push!(result, route_nodes, route_values) + else + @warn "routing module declared wrongly - a star algorithm will be used instead!" + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, node0, node1, weights, heuristic) + route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) + push!(result, route_nodes, route_values) + end if get_distance - if isempty(route_indices) - distance = Inf - elseif length(route_indices) == 1 - distance = 0 - else - distance = OpenStreetMapX.calculate_distance(m, m.w, route_indices) - end + if isempty(route_indices) + distance = Inf + elseif length(route_indices) == 1 + distance = 0 + else + distance = OpenStreetMapX.calculate_distance(m, m.w, route_indices) + end push!(result, distance) end if get_time w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m)) - if isempty(route_indices) - route_time = Inf - elseif length(route_indices) == 1 - route_time = 0 + if isempty(route_indices) + route_time = Inf + elseif length(route_indices) == 1 + route_time = 0 else - route_time = OpenStreetMapX.calculate_distance(m, w, route_indices) - end + route_time = OpenStreetMapX.calculate_distance(m, w, route_indices) + end push!(result, route_time) end return result @@ -180,15 +194,22 @@ end ### Find Route Connecting 3 Points with Given Weights ### ######################################################### -function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, node2::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}, get_distance::Bool = false, get_time::Bool = false) - result = Any[] - route1 = OpenStreetMapX.find_route(m, node0, node1, weights, get_distance, get_time) - route2 = OpenStreetMapX.find_route(m, node1, node2, weights, get_distance, get_time) - push!(result,vcat(route1[1],route2[1])) - for i = 2:length(route1) - push!(result,route1[i] + route2[i]) - end - return result +function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, node2::Int, + weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; + routing::Symbol = :astar, heuristic::= n -> zero(Float64), + get_distance::Bool = false, get_time::Bool = false) + result = Any[] + route1 = OpenStreetMapX.find_route(m, node0, node1, weights, + routing = routing, heuristic = heuristic, + get_distance = get_distance, get_time = get_time) + route2 = OpenStreetMapX.find_route(m, node1, node2, weights, + routing = routing, heuristic = heuristic, + get_distance = get_distance, get_time = get_time) + push!(result,vcat(route1[1],route2[1])) + for i = 2:length(route1) + push!(result,route1[i] + route2[i]) + end + return result end ########################### @@ -200,20 +221,23 @@ end Find Shortest route between `node1` and `node2` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int) - route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2,m.w,false,true) - return route_nodes, distance, route_time +function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar, heuristic::= n -> zero(Float64)) + route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2,m.w, + routing = routing, heuristic = heuristic + get_distance =false, get_time = true) + return route_nodes, distance, route_time end - """ shortest_route(m::MapData, node1::Int, node2::Int, node3::Int) Find Shortest route between `node1` and `node2` and `node3` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int) - route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2, node3, m.w,false,true) - return route_nodes, distance, route_time +function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar, heuristic::= n -> zero(Float64)) + route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2, node3, m.w, + routing = routing, heuristic = heuristic + get_distance =false, get_time = true) + return route_nodes, distance, route_time end """ @@ -223,10 +247,14 @@ end Find fastest route between `node1` and `node2` on map `m` with assuming `speeds` for road classes. """ -function fastest_route(m::MapData, node1::Int, node2::Int, speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) - w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) - route_nodes, route_time, distance = OpenStreetMapX.find_route(m,node1,node2,w,true, false) - return route_nodes, distance, route_time +function fastest_route(m::MapData, node1::Int, node2::Int; + routing::Symbol = :astar, heuristic::= n -> zero(Float64), + speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) + w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) + route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, + routing = routing, heuristic = heuristic + get_distance = true, get_time = false) + return route_nodes, distance, route_time end """ @@ -236,10 +264,14 @@ end Find fastest route between `node1` and `node2` and `node3` on map `m` with assuming `speeds` for road classes. """ -function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int, speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) - w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) - route_nodes, route_time, distance = OpenStreetMapX.find_route(m,node1,node2, node3, w,true, false) - return route_nodes, distance, route_time +function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; + routing::Symbol = :astar, heuristic::= n -> zero(Float64), + speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) + w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) + route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, + routing = routing, heuristic = heuristic + get_distance = true, get_time = false) + return route_nodes, distance, route_time end ########################################### diff --git a/test/runtests.jl b/test/runtests.jl index 90e9c0c..4f8b220 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -24,4 +24,7 @@ pointB = point_to_nodes(pB, m) sr1, shortest_distance1, shortest_time1 = shortest_route(m, pointA, pointB) @test (sr1[1], sr1[end]) == (pointA, pointB) +@test shortest_route(m, pointA, pointB; routing = :astar) == shortest_route(m, pointA, pointB; routing = :dijkstra) +@test fastest_route(m, pointA, pointB; routing = :astar) == fastest_route(m, pointA, pointB; routing = :dijkstra) + end; From 152b739b65f2a3a0b3d2b9a0b92cee4d968d4e3a Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 09:17:30 +0100 Subject: [PATCH 09/26] Update routing.jl --- src/routing.jl | 20 ++++++++++---------- 1 file changed, 10 insertions(+), 10 deletions(-) diff --git a/src/routing.jl b/src/routing.jl index 220f9ea..67c962b 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -145,7 +145,7 @@ calculate_distance(m::OpenStreetMapX.MapData, weights::SparseArrays.SparseMatrix function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, heuristic::= n -> zero(Float64), + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), get_distance::Bool = false, get_time::Bool = false) result = Any[] if routing == :dijkstra @@ -196,7 +196,7 @@ end function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, node2::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, heuristic::= n -> zero(Float64), + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), get_distance::Bool = false, get_time::Bool = false) result = Any[] route1 = OpenStreetMapX.find_route(m, node0, node1, weights, @@ -221,9 +221,9 @@ end Find Shortest route between `node1` and `node2` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar, heuristic::= n -> zero(Float64)) +function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar, heuristic::Function = n -> zero(Float64)) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2,m.w, - routing = routing, heuristic = heuristic + routing = routing, heuristic = heuristic, get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -233,9 +233,9 @@ end Find Shortest route between `node1` and `node2` and `node3` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar, heuristic::= n -> zero(Float64)) +function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar, heuristic::Function = n -> zero(Float64)) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2, node3, m.w, - routing = routing, heuristic = heuristic + routing = routing, heuristic = heuristic, get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -248,11 +248,11 @@ Find fastest route between `node1` and `node2` on map `m` with assuming `speeds """ function fastest_route(m::MapData, node1::Int, node2::Int; - routing::Symbol = :astar, heuristic::= n -> zero(Float64), + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, - routing = routing, heuristic = heuristic + routing = routing, heuristic = heuristic, get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -265,11 +265,11 @@ Find fastest route between `node1` and `node2` and `node3` on map `m` with assu """ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; - routing::Symbol = :astar, heuristic::= n -> zero(Float64), + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = heuristic + routing = routing, heuristic = heuristic, get_distance = true, get_time = false) return route_nodes, distance, route_time end From 6ce08e3dc41d7be1347e13475038ad5e32f5c64f Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 09:25:00 +0100 Subject: [PATCH 10/26] Update a_star.jl --- src/a_star.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/a_star.jl b/src/a_star.jl index 90969b1..c9c3adc 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -64,7 +64,7 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g visited = zeros(Bool, nvg) dists = fill(typemax(T), nvg) parents = zeros(U, nvg) - colormap = LightGraphs.empty_colormap(nvg) + colormap = zeros(UInt8, nvg) colormap[s] = 1 @inbounds while !isempty(frontier) (cost_so_far, u) = dequeue!(frontier) From 3554aa9b40bc9d5f0831718de5300a0e4822c56a Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 09:37:32 +0100 Subject: [PATCH 11/26] Update routing.jl --- src/routing.jl | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/routing.jl b/src/routing.jl index 67c962b..73b3f7d 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -148,20 +148,20 @@ function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), get_distance::Bool = false, get_time::Bool = false) result = Any[] + start_vertex = m.v[node0] + finish_vertex= m.v[node1] if routing == :dijkstra - start_vertex = m.v[node0] dijkstra_result = OpenStreetMapX.dijkstra(m, weights, start_vertex) - finish_vertex= m.v[node1] route_indices, route_values = OpenStreetMapX.extract_route(dijkstra_result, start_vertex, finish_vertex) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) elseif routing == :astar - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, node0, node1, weights, heuristic) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) else @warn "routing module declared wrongly - a star algorithm will be used instead!" - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, node0, node1, weights, heuristic) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) end From cb438eddcf58205f3fe2151e8fc1989a281b2eed Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 10:20:02 +0100 Subject: [PATCH 12/26] Update routing.jl --- src/routing.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/routing.jl b/src/routing.jl index 73b3f7d..2fabef2 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -117,7 +117,7 @@ end function get_route_nodes(m::OpenStreetMapX.MapData, route_indices::Array{Int64,1}) route_nodes = Array{Int}(undef,length(route_indices)) - v = Dict{Int,Int}(reverse(p) for p = pairs(m.v)) + v = Dict{Int,Int}(reverse.(collect(m.v))) for n = 1:length(route_nodes) route_nodes[n] = v[route_indices[n]] end From e71bec0f7bdd84d6b34d09b92484a355de85d4fd Mon Sep 17 00:00:00 2001 From: Przemyslaw Szufel Date: Fri, 8 Mar 2019 15:27:22 +0100 Subject: [PATCH 13/26] perftest --- test/runtests.jl | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/test/runtests.jl b/test/runtests.jl index 4f8b220..ec63257 100644 --- a/test/runtests.jl +++ b/test/runtests.jl @@ -26,5 +26,26 @@ sr1, shortest_distance1, shortest_time1 = shortest_route(m, pointA, pointB) @test shortest_route(m, pointA, pointB; routing = :astar) == shortest_route(m, pointA, pointB; routing = :dijkstra) @test fastest_route(m, pointA, pointB; routing = :astar) == fastest_route(m, pointA, pointB; routing = :dijkstra) + +function perftest() + sr_len=length(shortest_route(m, pointA, pointB; routing = :astar)[1]) + fr_len=length(fastest_route(m, pointA, pointB; routing = :astar)[1]) + shortest_route(m, pointA, pointB; routing = :dijkstra) + fastest_route(m, pointA, pointB; routing = :dijkstra) + + print("shortest_route(...; routing = :astar) of $sr_len nodes") + @time shortest_route(m, pointA, pointB; routing = :astar) + print("shortest_route(...; routing = :dijkstra) of $sr_len nodes") + @time shortest_route(m, pointA, pointB; routing = :dijkstra) + + print("fastest_route(...; routing = :astar) of $fr_len nodes") + @time fastest_route(m, pointA, pointB; routing = :astar) + print("fastest_route(...; routing = :dijkstra) of $fr_len nodes") + @time fastest_route(m, pointA, pointB; routing = :dijkstra) + +end + +perftest() + end; From e4201e9c3073d58425f797ccd41dc7f705d81e2d Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 16:08:11 +0100 Subject: [PATCH 14/26] A* star works by default with straight line heuristic --- src/a_star.jl | 23 +++++++++++++++++++++++ src/parseMap.jl | 3 ++- src/routing.jl | 28 ++++++++++++++-------------- src/types.jl | 1 + 4 files changed, 40 insertions(+), 15 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index c9c3adc..6895cdd 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -91,4 +91,27 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g colormap[u] = 2 end Vector{U}(), Inf +end + +""" + a_star_algorithm(m::OpenStreetMapX.MapData + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} + +A star search algorithm with straight line distance heuristic + +**Arguments** + +* `m` : MapData object +* `S` : start vertex +* `t` : end vertex +* `distmx` : distance matrix +""" +_star_algorithm(m::OpenStreetMapX.MapData + s::Integer, + t::Integer, + distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} + heuristic(u) = OpenStreetMapX.get_distance(u, t, m.nodes, m.n) + a_star_algorithm(m.g,s,t,distmx,heuristic) end \ No newline at end of file diff --git a/src/parseMap.jl b/src/parseMap.jl index db5733d..b6c6dbe 100644 --- a/src/parseMap.jl +++ b/src/parseMap.jl @@ -125,6 +125,7 @@ function get_map_data(filepath::String,filename::Union{String,Nothing}=nothing; end # (node id) => (graph vertex) v = OpenStreetMapX.get_vertices(e) + n = Dict(reverse.(collect(v))) edges = [v[id] for id in reinterpret(Int, e)] I = edges[1:2:end] J = edges[2:2:end] @@ -132,7 +133,7 @@ function get_map_data(filepath::String,filename::Union{String,Nothing}=nothing; w = SparseArrays.sparse(I, J, weights, length(v), length(v)) g = LightGraphs.DiGraph(w) - res = OpenStreetMapX.MapData(bounds,nodes,roadways,intersections,g,v,e,w,class) + res = OpenStreetMapX.MapData(bounds,nodes,roadways,intersections,g,v,n,e,w,class) if use_cache f=open(cachefile,"w"); Serialization.serialize(f,res); diff --git a/src/routing.jl b/src/routing.jl index 2fabef2..ceb630b 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -145,7 +145,7 @@ calculate_distance(m::OpenStreetMapX.MapData, weights::SparseArrays.SparseMatrix function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), + routing::Symbol = :astar, get_distance::Bool = false, get_time::Bool = false) result = Any[] start_vertex = m.v[node0] @@ -156,12 +156,12 @@ function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) elseif routing == :astar - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m, start_vertex, finish_vertex, weights) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) else @warn "routing module declared wrongly - a star algorithm will be used instead!" - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m, start_vertex, finish_vertex, weights) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) end @@ -196,14 +196,14 @@ end function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, node2::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), + routing::Symbol = :astar, get_distance::Bool = false, get_time::Bool = false) result = Any[] route1 = OpenStreetMapX.find_route(m, node0, node1, weights, - routing = routing, heuristic = heuristic, + routing = routing, get_distance = get_distance, get_time = get_time) route2 = OpenStreetMapX.find_route(m, node1, node2, weights, - routing = routing, heuristic = heuristic, + routing = routing, get_distance = get_distance, get_time = get_time) push!(result,vcat(route1[1],route2[1])) for i = 2:length(route1) @@ -221,9 +221,9 @@ end Find Shortest route between `node1` and `node2` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar, heuristic::Function = n -> zero(Float64)) +function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2,m.w, - routing = routing, heuristic = heuristic, + routing = routing, get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -233,9 +233,9 @@ end Find Shortest route between `node1` and `node2` and `node3` on map `m`. """ -function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar, heuristic::Function = n -> zero(Float64)) +function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2, node3, m.w, - routing = routing, heuristic = heuristic, + routing = routing, get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -248,11 +248,11 @@ Find fastest route between `node1` and `node2` on map `m` with assuming `speeds """ function fastest_route(m::MapData, node1::Int, node2::Int; - routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), + routing::Symbol = :astar, speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, - routing = routing, heuristic = heuristic, + routing = routing, get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -265,11 +265,11 @@ Find fastest route between `node1` and `node2` and `node3` on map `m` with assu """ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; - routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), + routing::Symbol = :astar, speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = heuristic, + routing = routing, get_distance = true, get_time = false) return route_nodes, distance, route_time end diff --git a/src/types.jl b/src/types.jl index f51a5de..39108f4 100644 --- a/src/types.jl +++ b/src/types.jl @@ -233,6 +233,7 @@ struct MapData # Transporation network graph data and helpers to increase routing speed g::LightGraphs.SimpleGraphs.SimpleDiGraph{Int64} # Graph object v::Dict{Int,Int} # (node id) => (graph vertex) + n::Dict{Int,Int} # (graph vertex) => (node id) e::Array{Tuple{Int64,Int64},1} # Edges in graph, stored as a tuple (source,destination) w::SparseArrays.SparseMatrixCSC{Float64, Int} # Edge weights, indexed by graph id class::Vector{Int} # Road class of each edge From 976b4f06ce46bf460df4a51ff4521649e1bcf1ba Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 16:17:56 +0100 Subject: [PATCH 15/26] sasasa --- src/a_star.jl | 2 +- src/routing.jl | 14 ++++++++------ 2 files changed, 9 insertions(+), 7 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index 6895cdd..7bd2709 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -94,7 +94,7 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g end """ - a_star_algorithm(m::OpenStreetMapX.MapData + a_star_algorithm(m::OpenStreetMapX.MapData, s::Integer, t::Integer, distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} diff --git a/src/routing.jl b/src/routing.jl index ceb630b..0da2642 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -216,7 +216,7 @@ end ### ### ########################### """ - shortest_route(m::MapData, node1::Int, node2::Int) + shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar) Find Shortest route between `node1` and `node2` on map `m`. @@ -228,7 +228,7 @@ function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :a return route_nodes, distance, route_time end """ - shortest_route(m::MapData, node1::Int, node2::Int, node3::Int) + shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar) Find Shortest route between `node1` and `node2` and `node3` on map `m`. @@ -241,8 +241,9 @@ function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing: end """ - fastest_route(m::MapData, node1::Int, node2::Int, - speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) + ffastest_route(m::MapData, node1::Int, node2::Int; + routing::Symbol = :astar, + speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) Find fastest route between `node1` and `node2` on map `m` with assuming `speeds` for road classes. @@ -258,8 +259,9 @@ function fastest_route(m::MapData, node1::Int, node2::Int; end """ - fastest_route(m::MapData, node1::Int, node2::Int, node3::Int, - speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) + fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; + routing::Symbol = :astar, + speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) Find fastest route between `node1` and `node2` and `node3` on map `m` with assuming `speeds` for road classes. From f65c8e75d1be7f3ace3829525109958402e6e561 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Fri, 8 Mar 2019 16:27:26 +0100 Subject: [PATCH 16/26] Update a_star.jl --- src/a_star.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index 7bd2709..7fb4e12 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -108,10 +108,10 @@ A star search algorithm with straight line distance heuristic * `t` : end vertex * `distmx` : distance matrix """ -_star_algorithm(m::OpenStreetMapX.MapData +function a_star_algorithm(m::OpenStreetMapX.MapData, s::Integer, t::Integer, distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} heuristic(u) = OpenStreetMapX.get_distance(u, t, m.nodes, m.n) - a_star_algorithm(m.g,s,t,distmx,heuristic) + OpenStreetMapX.a_star_algorithm(m.g,s,t,distmx,heuristic) end \ No newline at end of file From 662c5db6a38ce5335e3fb4ff7acb56a5e363854d Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 12:25:19 +0100 Subject: [PATCH 17/26] ddsds --- src/a_star.jl | 15 ++++++--------- src/routing.jl | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 19 deletions(-) diff --git a/src/a_star.jl b/src/a_star.jl index 7fb4e12..8c4cd29 100644 --- a/src/a_star.jl +++ b/src/a_star.jl @@ -80,11 +80,11 @@ function a_star_algorithm(g::LightGraphs.AbstractGraph{U}, # the g dists[v] = path_cost enqueue!(frontier, (path_cost, v), - path_cost + heuristic(v)) + path_cost + heuristic(v,t)) elseif path_cost < dists[v] parents[v] = u dists[v] = path_cost - frontier[path_cost, v] = path_cost + heuristic(v) + frontier[path_cost, v] = path_cost + heuristic(v,t) end end end @@ -96,9 +96,7 @@ end """ a_star_algorithm(m::OpenStreetMapX.MapData, s::Integer, - t::Integer, - distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} - + t::Integer) A star search algorithm with straight line distance heuristic **Arguments** @@ -110,8 +108,7 @@ A star search algorithm with straight line distance heuristic """ function a_star_algorithm(m::OpenStreetMapX.MapData, s::Integer, - t::Integer, - distmx::AbstractMatrix{T}=LightGraphs.weights(g)) where {T} - heuristic(u) = OpenStreetMapX.get_distance(u, t, m.nodes, m.n) - OpenStreetMapX.a_star_algorithm(m.g,s,t,distmx,heuristic) + t::Integer) + heuristic(u,v) = OpenStreetMapX.get_distance(u, v, m.nodes, m.n) + OpenStreetMapX.a_star_algorithm(m.g,s,t,m.w,heuristic) end \ No newline at end of file diff --git a/src/routing.jl b/src/routing.jl index 0da2642..ae2074b 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -145,7 +145,7 @@ calculate_distance(m::OpenStreetMapX.MapData, weights::SparseArrays.SparseMatrix function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), get_distance::Bool = false, get_time::Bool = false) result = Any[] start_vertex = m.v[node0] @@ -156,12 +156,12 @@ function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) elseif routing == :astar - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m, start_vertex, finish_vertex, weights) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) else @warn "routing module declared wrongly - a star algorithm will be used instead!" - route_indices, route_values = OpenStreetMapX.a_star_algorithm(m, start_vertex, finish_vertex, weights) + route_indices, route_values = OpenStreetMapX.a_star_algorithm(m.g, start_vertex, finish_vertex, weights, heuristic) route_nodes = OpenStreetMapX.get_route_nodes(m, route_indices) push!(result, route_nodes, route_values) end @@ -196,14 +196,14 @@ end function find_route(m::OpenStreetMapX.MapData, node0::Int, node1::Int, node2::Int, weights::SparseArrays.SparseMatrixCSC{Float64,Int64}; - routing::Symbol = :astar, + routing::Symbol = :astar, heuristic::Function = n -> zero(Float64), get_distance::Bool = false, get_time::Bool = false) result = Any[] route1 = OpenStreetMapX.find_route(m, node0, node1, weights, - routing = routing, + routing = routing, heuristic = heuristic, get_distance = get_distance, get_time = get_time) route2 = OpenStreetMapX.find_route(m, node1, node2, weights, - routing = routing, + routing = routing, heuristic = heuristic, get_distance = get_distance, get_time = get_time) push!(result,vcat(route1[1],route2[1])) for i = 2:length(route1) @@ -223,7 +223,7 @@ Find Shortest route between `node1` and `node2` on map `m`. """ function shortest_route(m::MapData, node1::Int, node2::Int; routing::Symbol = :astar) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2,m.w, - routing = routing, + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n), get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -235,7 +235,7 @@ Find Shortest route between `node1` and `node2` and `node3` on map `m`. """ function shortest_route(m::MapData, node1::Int, node2::Int, node3::Int; routing::Symbol = :astar) route_nodes, distance, route_time = OpenStreetMapX.find_route(m,node1,node2, node3, m.w, - routing = routing, + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n), get_distance =false, get_time = true) return route_nodes, distance, route_time end @@ -253,7 +253,8 @@ function fastest_route(m::MapData, node1::Int, node2::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, - routing = routing, + routing = routing, + heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(w)), get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -271,7 +272,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(w)), get_distance = true, get_time = false) return route_nodes, distance, route_time end From 1a23401d93ce7836b911bb628fef6e0d02182b15 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 12:43:02 +0100 Subject: [PATCH 18/26] Update routing.jl --- src/routing.jl | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/routing.jl b/src/routing.jl index ae2074b..249a731 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -96,6 +96,19 @@ function create_weights_matrix(m::OpenStreetMapX.MapData,weights::Vector{Float64 collect(values(w)),length(m.v),length(m.v)) end +### Get velocieties matrix ### + +function get_velocities(m::OpenStreetMapX.MapData, + class_speeds::Dict{Int,Int} = OpenStreetMapX.SPEED_ROADS_URBAN) + @assert length(m.e) == length(m.w.nzval) + indices = [(m.v[i],m.v[j]) for (i,j) in m.e] + V = Array{Float64}(undef,length(m.e)) + for i = 1:length(indices) + V[i] = class_speeds[m.class[i]]/3.6 + end + return SparseArrays.sparse(map(i -> m.v[i[1]], m.e), map(i -> m.v[i[2]], m.e),V) +end + ### Extract route from Dijkstra results object ### function extract_route(dijkstra::LightGraphs.DijkstraState{Float64,Int64}, startIndex::Int, finishIndex::Int) @@ -254,7 +267,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int; w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, routing = routing, - heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(w)), + heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -272,7 +285,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(w)), + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end From 047b0947af8489fe891847616861a38c65d24243 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 12:51:34 +0100 Subject: [PATCH 19/26] Update OpenStreetMapX.jl --- src/OpenStreetMapX.jl | 1 + 1 file changed, 1 insertion(+) diff --git a/src/OpenStreetMapX.jl b/src/OpenStreetMapX.jl index 3ed4371..90fbab2 100644 --- a/src/OpenStreetMapX.jl +++ b/src/OpenStreetMapX.jl @@ -3,6 +3,7 @@ module OpenStreetMapX using LibExpat using LightGraphs using SparseArrays +using StatsBase using DataStructures using Serialization using JSON From 4a8a3924c9a60f506638ca87dd1e61faba359979 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 12:59:17 +0100 Subject: [PATCH 20/26] Update Project.toml --- Project.toml | 1 + 1 file changed, 1 insertion(+) diff --git a/Project.toml b/Project.toml index 993c306..c72e3f3 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Przemyslaw Szufel ", "Bartosz Pankratz Date: Sat, 9 Mar 2019 12:59:46 +0100 Subject: [PATCH 21/26] Update Project.toml --- Project.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Project.toml b/Project.toml index c72e3f3..b64653f 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,7 @@ authors = ["Przemyslaw Szufel ", "Bartosz Pankratz Date: Sat, 9 Mar 2019 13:07:11 +0100 Subject: [PATCH 22/26] sasa --- Project.toml | 1 - src/routing.jl | 4 ++-- 2 files changed, 2 insertions(+), 3 deletions(-) diff --git a/Project.toml b/Project.toml index b64653f..993c306 100644 --- a/Project.toml +++ b/Project.toml @@ -4,7 +4,6 @@ authors = ["Przemyslaw Szufel ", "Bartosz Pankratz OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -285,7 +285,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / StatsBase.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end From d41c41b5e5808b9d37286da8950c2f08bcb2329d Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 13:08:21 +0100 Subject: [PATCH 23/26] Update OpenStreetMapX.jl --- src/OpenStreetMapX.jl | 1 - 1 file changed, 1 deletion(-) diff --git a/src/OpenStreetMapX.jl b/src/OpenStreetMapX.jl index 90fbab2..3ed4371 100644 --- a/src/OpenStreetMapX.jl +++ b/src/OpenStreetMapX.jl @@ -3,7 +3,6 @@ module OpenStreetMapX using LibExpat using LightGraphs using SparseArrays -using StatsBase using DataStructures using Serialization using JSON From bad81aef2adce51212fd0e643ccdba9fb1afbac4 Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 13:14:17 +0100 Subject: [PATCH 24/26] Update routing.jl --- src/routing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routing.jl b/src/routing.jl index 6feaedf..3321c1a 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -96,10 +96,10 @@ function create_weights_matrix(m::OpenStreetMapX.MapData,weights::Vector{Float64 collect(values(w)),length(m.v),length(m.v)) end -### Get velocieties matrix ### +### Get velocities matrix ### function get_velocities(m::OpenStreetMapX.MapData, - class_speeds::Dict{Int,Int} = OpenStreetMapX.SPEED_ROADS_URBAN) + class_speeds::Dict{Int,Float64} = OpenStreetMapX.SPEED_ROADS_URBAN) @assert length(m.e) == length(m.w.nzval) indices = [(m.v[i],m.v[j]) for (i,j) in m.e] V = Array{Float64}(undef,length(m.e)) From 0bb79256ec1de37618169eb5883a7e5f046b381a Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sat, 9 Mar 2019 13:20:54 +0100 Subject: [PATCH 25/26] Statistics added to dependencies --- Project.toml | 1 + src/OpenStreetMapX.jl | 1 + src/routing.jl | 4 ++-- 3 files changed, 4 insertions(+), 2 deletions(-) diff --git a/Project.toml b/Project.toml index 993c306..527c4f0 100644 --- a/Project.toml +++ b/Project.toml @@ -4,6 +4,7 @@ authors = ["Przemyslaw Szufel ", "Bartosz Pankratz OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / Statistics.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -285,7 +285,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / Statistics.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), get_distance = true, get_time = false) return route_nodes, distance, route_time end From bf05295c3446000c7d4a15761d09b9fc4752037f Mon Sep 17 00:00:00 2001 From: bartoszpankratz <32309411+bartoszpankratz@users.noreply.github.com> Date: Sun, 10 Mar 2019 11:39:17 +0100 Subject: [PATCH 26/26] Update routing.jl --- src/routing.jl | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/routing.jl b/src/routing.jl index 443eabe..39bad13 100644 --- a/src/routing.jl +++ b/src/routing.jl @@ -267,7 +267,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int; w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, w, routing = routing, - heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / Statistics.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / maximum(values(speeds)), get_distance = true, get_time = false) return route_nodes, distance, route_time end @@ -285,7 +285,7 @@ function fastest_route(m::MapData, node1::Int, node2::Int, node3::Int; speeds::Dict{Int,Float64}=SPEED_ROADS_URBAN) w = OpenStreetMapX.create_weights_matrix(m,network_travel_times(m, speeds)) route_nodes, route_time, distance = OpenStreetMapX.find_route(m, node1, node2, node3, w, - routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / Statistics.mean(SparseArrays.nonzeros(OpenStreetMapX.get_velocities(m))), + routing = routing, heuristic = (u,v) -> OpenStreetMapX.get_distance(u, v, m.nodes, m.n) / maximum(values(speeds)), get_distance = true, get_time = false) return route_nodes, distance, route_time end