11export ABM, StandardABM, UnkillableABM, FixedMassABM
22using StaticArrays: SizedVector
33
4- ContainerType{A} = Union{AbstractDict{Int,A}, AbstractVector{A}}
4+ ContainerType{A} = Union{AbstractDict{Int,A},AbstractVector{A}}
55
66# And the three implementations here are just variants with different `C` type.
77struct SingleContainerABM{S<: SpaceType ,A<: AbstractAgent ,C<: ContainerType{A} ,F,P,R<: AbstractRNG } <: AgentBasedModel{S,A}
@@ -39,12 +39,12 @@ single container. Offers the variants:
3939"""
4040function SingleContainerABM (
4141 :: Type{A} ,
42- space:: S = nothing ;
43- container:: Type = Dict{Int},
44- scheduler:: F = Schedulers. fastest,
45- properties:: P = nothing ,
46- rng:: R = Random. default_rng (),
47- warn = true
42+ space:: S = nothing ;
43+ container:: Type = Dict{Int},
44+ scheduler:: F = Schedulers. fastest,
45+ properties:: P = nothing ,
46+ rng:: R = Random. default_rng (),
47+ warn= true ,
4848) where {A<: AbstractAgent ,S<: SpaceType ,F,P,R<: AbstractRNG }
4949 agent_validator (A, space, warn)
5050 C = construct_agent_container (container, A)
5959construct_agent_container (:: Type{<:Dict} , A) = Dict{Int,A}
6060construct_agent_container (:: Type{<:Vector} , A) = Vector{A}
6161construct_agent_container (container, A) = throw (
62- " Unrecognised container $container , please specify either Dict or Vector."
62+ " Unrecognised container $container , please specify either Dict or Vector." ,
6363)
6464
6565"""
@@ -101,13 +101,13 @@ in the given `agent_vector`.
101101"""
102102function FixedMassABM (
103103 agents:: AbstractVector{A} ,
104- space:: S = nothing ;
105- scheduler:: F = Schedulers. fastest,
106- properties:: P = nothing ,
107- rng:: R = Random. default_rng (),
108- warn = true
109- ) where {A<: AbstractAgent , S<: SpaceType ,F,P,R<: AbstractRNG }
110- C = SizedVector{length (agents), A}
104+ space:: S = nothing ;
105+ scheduler:: F = Schedulers. fastest,
106+ properties:: P = nothing ,
107+ rng:: R = Random. default_rng (),
108+ warn= true ,
109+ ) where {A<: AbstractAgent ,S<: SpaceType ,F,P,R<: AbstractRNG }
110+ C = SizedVector{length (agents),A}
111111 fixed_agents = C (agents)
112112 # Validate that agent ID is the same as its order in the vector.
113113 for (i, a) in enumerate (agents)
@@ -120,11 +120,11 @@ end
120120# ######################################################################################
121121# %% Model accessing api
122122# ######################################################################################
123- nextid (model:: SingleContainerABM{<:SpaceType,A,Dict{Int, A}} ) where {A} = getfield (model, :maxid )[] + 1
123+ nextid (model:: SingleContainerABM{<:SpaceType,A,Dict{Int,A}} ) where {A} = getfield (model, :maxid )[] + 1
124124nextid (model:: SingleContainerABM{<:SpaceType,A,Vector{A}} ) where {A} = nagents (model) + 1
125125nextid (:: SingleContainerABM{<:SpaceType,A,<:SizedVector} ) where {A} = error (" There is no `nextid` in a `FixedMassABM`. Most likely an internal error." )
126126
127- function add_agent_to_model! (agent, model:: SingleContainerABM{<:SpaceType,A,Dict{Int, A}} ) where {A<: AbstractAgent }
127+ function add_agent_to_model! (agent, model:: SingleContainerABM{<:SpaceType,A,Dict{Int,A}} ) where {A<: AbstractAgent }
128128 if haskey (agent_container (model), agent. id)
129129 error (" Can't add agent to model. There is already an agent with id=$(agent. id) " )
130130 else
@@ -133,7 +133,9 @@ function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Dict
133133 # Only the `Dict` implementation actually uses the `maxid` field.
134134 # The `Vector` one uses the defaults, and the `Sized` one errors anyways.
135135 maxid = getfield (model, :maxid )
136- if maxid[] < agent. id; maxid[] = agent. id; end
136+ if maxid[] < agent. id
137+ maxid[] = agent. id
138+ end
137139 return
138140end
139141
@@ -144,12 +146,12 @@ function add_agent_to_model!(agent, model::SingleContainerABM{<:SpaceType,A,Vect
144146end
145147
146148function remove_agent_from_model! (agent:: A , model:: SingleContainerABM{<:SpaceType,A,<:AbstractDict{Int,A}} ) where {A<: AbstractAgent }
147- delete! (agent_container (model), agent. id)
149+ return delete! (agent_container (model), agent. id)
148150end
149151function remove_agent_from_model! (:: A , model:: SingleContainerABM{<:SpaceType,A,<:AbstractVector} ) where {A<: AbstractAgent }
150- error (
151- " Cannot remove agents stored in $(containertype (model)) . " *
152- " Use the vanilla `SingleContainerABM` to be able to remove agents."
152+ return error (
153+ " Cannot remove agents stored in $(containertype (model)) . " *
154+ " Use the vanilla `SingleContainerABM` to be able to remove agents." ,
153155 )
154156end
155157
@@ -188,18 +190,18 @@ end
188190 do_checks(agent, space)
189191Helper function for `agent_validator`.
190192"""
191- function do_checks (:: Type{A} , space:: S , warn:: Bool ) where {A<: AbstractAgent , S<: SpaceType }
193+ function do_checks (:: Type{A} , space:: S , warn:: Bool ) where {A<: AbstractAgent ,S<: SpaceType }
192194 if warn
193195 isbitstype (A) &&
194- @warn " Agent type is not mutable, and most library functions assume that it is."
196+ @warn " Agent type is not mutable, and most library functions assume that it is."
195197 end
196198 (any (isequal (:id ), fieldnames (A)) && fieldnames (A)[1 ] == :id ) ||
197- throw (ArgumentError (" First field of agent type must be `id` (and should be of type `Int`)." ))
199+ throw (ArgumentError (" First field of agent type must be `id` (and should be of type `Int`)." ))
198200 fieldtype (A, :id ) <: Integer ||
199- throw (ArgumentError (" `id` field in agent type must be of type `Int`." ))
201+ throw (ArgumentError (" `id` field in agent type must be of type `Int`." ))
200202 if space != = nothing
201203 (any (isequal (:pos ), fieldnames (A)) && fieldnames (A)[2 ] == :pos ) ||
202- throw (ArgumentError (" Second field of agent type must be `pos` when using a space." ))
204+ throw (ArgumentError (" Second field of agent type must be `pos` when using a space." ))
203205 # Check `pos` field in A has the correct type
204206 pos_type = fieldtype (A, :pos )
205207 space_type = typeof (space)
0 commit comments