Skip to content

Commit

Permalink
Fix GridSpaceSingle deserialization, add tests (#661)
Browse files Browse the repository at this point in the history
* Fix `GridSpaceSingle` deserialization, add tests

* Increment patch version
  • Loading branch information
AayushSabharwal authored Jul 28, 2022
1 parent 19d0cc2 commit d365242
Show file tree
Hide file tree
Showing 3 changed files with 42 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"]
version = "5.4.2"
version = "5.4.3"

[deps]
CSV = "336ed68f-0bac-5ca0-87d4-7b16caf5d00b"
Expand Down
2 changes: 1 addition & 1 deletion src/submodules/io/jld2_integration.jl
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ function from_serializable(t::SerializableGridSpace{D,P}; kwargs...) where {D,P}
end
function from_serializable(t::SerializableGridSpaceSingle{D,P}; kwargs...) where {D,P}
s = zeros(Int, t.dims)
return GridSpaceSingle{D,P}(s, t.metric, Dict())
return GridSpaceSingle{D,P}(s, t.metric, Dict(), Dict())
end

function from_serializable(t::SerializableContinuousSpace{D,P,T}; kwargs...) where {D,P,T}
Expand Down
41 changes: 40 additions & 1 deletion test/jld2_tests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@
@test space.metric == other.metric
end

function test_space(space::GridSpaceSingle, other)
@test size(space) == size(other)
@test all(other.stored_ids[pos] == space.stored_ids[pos] for pos in eachindex(space.stored_ids))
@test space.metric == other.metric
end

function test_space(space::ContinuousSpace, other)
test_space(space.grid, other.grid)
@test space.update_vel! == other.update_vel!
Expand Down Expand Up @@ -89,7 +95,40 @@
end

@testset "GridSpace" begin
# predator_prey used since properties is a NamedTuple, and contains an Array
model, astep, mstep = Models.schelling()
step!(model, astep, mstep, 50)
AgentsIO.save_checkpoint("test.jld2", model)
other = AgentsIO.load_checkpoint("test.jld2"; scheduler = Schedulers.Randomly())

# agent data
@test nagents(other) == nagents(model)
@test all(haskey(other.agents, i) for i in allids(model))
@test all(model[i].mood == other[i].mood for i in allids(model))
@test all(model[i].group == other[i].group for i in allids(model))
# properties
@test model.min_to_be_happy == other.min_to_be_happy
# model data
test_model_data(model, other)
# space data
@test typeof(model.space) == typeof(other.space) # to check periodicity
test_space(model.space, other.space)

rm("test.jld2")
end

@testset "GridSpaceSingle" begin
function schelling_single(; numagents = 320, griddims = (20, 20), min_to_be_happy = 3)
@assert numagents < prod(griddims)
space = GridSpaceSingle(griddims, periodic = false)
properties = Dict(:min_to_be_happy => min_to_be_happy)
model = ABM(Models.SchellingAgent, space; properties, scheduler = Schedulers.Randomly())
for n in 1:numagents
agent = Models.SchellingAgent(n, (1, 1), false, n < numagents / 2 ? 1 : 2)
add_agent_single!(agent, model)
end
return model, Models.schelling_agent_step!, dummystep
end

model, astep, mstep = Models.schelling()
step!(model, astep, mstep, 50)
AgentsIO.save_checkpoint("test.jld2", model)
Expand Down

0 comments on commit d365242

Please sign in to comment.