Skip to content

Commit

Permalink
Add OSM destination check (#1024)
Browse files Browse the repository at this point in the history
  • Loading branch information
simsurace authored Apr 12, 2024
1 parent 25bbcbf commit 2032e47
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Agents"
uuid = "46ada45e-f475-11e8-01d0-f70cc89e6671"
authors = ["George Datseris", "Tim DuBois", "Aayush Sabharwal", "Ali Vahdati", "Adriano Meligrana"]
version = "6.0.8"
version = "6.0.9"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
15 changes: 13 additions & 2 deletions src/spaces/openstreetmap.jl
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,12 @@ keywords are passed to
[`LightOSM.shortest_path`](https://deloittedigitalapac.github.io/LightOSM.jl/docs/shortest_path/#LightOSM.shortest_path).
Return `true` if a path to `dest` exists, and hence the route planning was successful.
Otherwise return `false`. Specifying `return_trip = true` also requires the existence
of a return path for a route to be planned.
Otherwise return `false`. When `dest` is an invalid position, i.e. if it contains node
indices that are not in the graph, or if the distance along the road is not between zero and
the length of the road, return `false` as well.
Specifying `return_trip = true` also requires the existence of a return path for a route to
be planned.
"""
function Agents.plan_route!(
agent::AbstractAgent,
Expand All @@ -241,6 +245,7 @@ function Agents.plan_route!(
kwargs...
)

isa_valid_position(dest, model) || return false
delete!(abmspace(model).routes, agent.id) # clear old route
same_position(agent.pos, dest, model) && return true

Expand Down Expand Up @@ -368,6 +373,12 @@ function Agents.plan_route!(
return true
end

function isa_valid_position(pos::Tuple{Int,Int,Float64}, model::ABM{<:OpenStreetMapSpace})
index_to_node = abmspace(model).map.index_to_node
return haskey(index_to_node, pos[1]) && haskey(index_to_node, pos[2]) &&
0.0 pos[3] road_length(pos, model)
end

# Allows passing destination as a node number
Agents.plan_route!(agent::AbstractAgent, dest::Int, model; kwargs...) =
plan_route!(agent, (dest, dest, 0.0), model; kwargs...)
Expand Down
12 changes: 12 additions & 0 deletions test/osm_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,18 @@ end

@test OSM.road_length(model[1].pos, model) 0.0002591692620559716
@test OSM.road_length(finish_road[1], finish_road[2], model) 0.00030269737299400725

# Test that if the destination is invalid, route planning fails (returns `false`)
@testset "test invalid destination: $dest" for dest in (
# Distance along road is greater than the length of the road:
(finish_road[1], finish_road[2], 1.2 * OSM.road_length(finish_road, model)),
# Distance along road is negative:
(finish_road[1], finish_road[2], -1.0),
# Road does not exist:
(finish_road[1], Graphs.nv(abmspace(model).map.graph) + 1, 0.5)
)
@test !plan_route!(model[1], dest, model)
end
end

@testset "Moving along routes" begin
Expand Down

0 comments on commit 2032e47

Please sign in to comment.