Skip to content

Commit 788e1f7

Browse files
Check if position is already occupied (#758)
* Update grid_single.jl * Update grid_single.jl * improve error message * Change tense in error message to past * Update src/spaces/grid_single.jl Co-authored-by: Martin Smit <57063134+jacobusmmsmit@users.noreply.github.com> * add test --------- Co-authored-by: Martin Smit <57063134+jacobusmmsmit@users.noreply.github.com>
1 parent 2e25a50 commit 788e1f7

File tree

2 files changed

+7
-1
lines changed

2 files changed

+7
-1
lines changed

src/spaces/grid_single.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,12 @@ function GridSpaceSingle(d::NTuple{D,Int}; periodic = true, metric = :chebyshev)
3737
end
3838
# Implementation of space API
3939
function add_agent_to_space!(a::A, model::ABM{<:GridSpaceSingle,A}) where {A<:AbstractAgent}
40-
model.space.stored_ids[a.pos...] = a.id
40+
pos = a.pos
41+
!isempty(pos, model) && error("Cannot add agent $(a) to occupied position $(pos)")
42+
model.space.stored_ids[pos...] = a.id
4143
return a
4244
end
45+
4346
function remove_agent_from_space!(a::A, model::ABM{<:GridSpaceSingle,A}) where {A<:AbstractAgent}
4447
model.space.stored_ids[a.pos...] = 0
4548
return a

test/grid_space_tests.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,9 @@ using StableRNGs
3737
agent = model[3]
3838
move_agent_single!(agent, model)
3939
@test agent.pos == posx
40+
if SpaceType == GridSpaceSingle
41+
@test_throws ErrorException add_agent!(posx, model)
42+
end
4043
end
4144

4245
@testset "positions + empty" begin

0 commit comments

Comments
 (0)