Skip to content

Commit

Permalink
Fix #652 (#653)
Browse files Browse the repository at this point in the history
* Fix #652

By adding one more operation that ensures that we do not exceed the prevfloat of the extent

* bump version

* update docstring for more clarity

* fix unexpected `end`
  • Loading branch information
Datseris authored Jul 16, 2022
1 parent b30aee2 commit 09eafa5
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 8 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"]
version = "5.4.1"
version = "5.4.2"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
9 changes: 4 additions & 5 deletions src/spaces/continuous.jl
Original file line number Diff line number Diff line change
Expand Up @@ -144,15 +144,14 @@ end
move_agent!(agent::A, model::ABM{<:ContinuousSpace,A}, dt::Real)
Propagate the agent forwards one step according to its velocity, _after_ updating the
agent's velocity (if configured using `update_vel!`, see [`ContinuousSpace`](@ref)).
Also take care of periodic boundary conditions.
For this continuous space version of `move_agent!`, the "evolution algorithm"
For this continuous space version of `move_agent!`, the "time evolution"
is a trivial Euler scheme with `dt` the step size, i.e. the agent position is updated
as `agent.pos += agent.vel * dt`.
Unlike `move_agent!(agent, [pos,] model)`, this function respects the space size
and if movement exceeds the extent in non-periodic spaces, the agent stops at the
space end, while for periodic spaces it properly wraps around the end.
Unlike `move_agent!(agent, [pos,] model)`, this function respects the space size.
For non-periodic spaces, agents will walk up to, but not reach, the space extent.
For periodic spaces movement properly wraps around the extent.
"""
function move_agent!(
agent::A,
Expand Down
5 changes: 3 additions & 2 deletions src/spaces/utilities.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,11 +157,11 @@ end
walk!(agent, direction::NTuple, model; ifempty = false)
Move agent in the given `direction` respecting periodic boundary conditions.
If `periodic = false`, agents will walk to, but not exceed the boundary value.
For non-periodic spaces, agents will walk to, but not exceed the boundary value.
Available for both `AbstractGridSpace` and `ContinuousSpace`s.
The type of `direction` must be the same as the space position. `AbstractGridSpace` asks
for `Int`, and `ContinuousSpace` for `Float64` vectors, describing the walk distance in
for `Int`, and `ContinuousSpace` for `Float64` tuples, describing the walk distance in
each direction. `direction = (2, -3)` is an example of a valid direction on a
`AbstractGridSpace`, which moves the agent to the right 2 positions and down 3 positions.
Agent velocity is ignored for this operation in `ContinuousSpace`.
Expand Down Expand Up @@ -207,6 +207,7 @@ function walk!(
kwargs...,
) where {D}
target = mod1.(agent.pos .+ direction, spacesize(model))
target = min.(target, prevfloat.(spacesize(model)))
move_agent!(agent, target, model)
end
function walk!(
Expand Down
9 changes: 9 additions & 0 deletions test/continuous_space_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,15 @@ using StableRNGs
walk!(a, rand, model)
@test a.pos[1] 6.5824829589163665
@test a.pos[2] 4.842266936412905

@testset "periodic" begin
model = ABM(ContinuousAgent{2}, ContinuousSpace((12, 10); periodic = true))
a = add_agent!((11.0, 9.0), model, (3.0, 1.0))
move_agent!(a, model, 1.0)
@test a.pos[1] == 2
@test a.pos[2] == prevfloat(10.0)
end

end

@testset "collisions" begin
Expand Down

0 comments on commit 09eafa5

Please sign in to comment.