/
model.jl
267 lines (230 loc) · 10.1 KB
/
model.jl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
export ABM, AgentBasedModel
#######################################################################################
# %% Fundamental type definitions
#######################################################################################
abstract type AbstractSpace end
SpaceType=Union{Nothing, AbstractSpace}
abstract type DiscreteSpace <: AbstractSpace end
# This is a collection of valid position types, sometimes used for ambiguity resolution
ValidPos = Union{Int, NTuple{N, Int}, NTuple{M, <:AbstractFloat}, Tuple{Int, Int, Float64}} where {N, M}
struct AgentBasedModel{S<:SpaceType, A<:AbstractAgent, F, P}
agents::Dict{Int,A}
space::S
scheduler::F
properties::P
maxid::Base.RefValue{Int64}
end
const ABM = AgentBasedModel
agenttype(::ABM{S, A}) where {S, A} = A
spacetype(::ABM{S}) where {S} = S
"""
union_types(U)
Return a set of types within a `Union`. Preserves order.
"""
union_types(x::Union) = union_types(x.a, x.b)
union_types(a::Union, b::Type) = (union_types(a)..., b)
union_types(a::Type, b::Type) = (a, b)
union_types(x::Type) = (x,)
# For completness
union_types(a::Type, b::Union) = (a, union_types(b)...)
"""
AgentBasedModel(AgentType [, space]; scheduler, properties) → model
Create an agent based model from the given agent type and `space`.
You can provide an agent _instance_ instead of type, and the type will be deduced.
`ABM` is equivalent with `AgentBasedModel`.
The agents are stored in a dictionary that maps unique ids (integers)
to agents. Use `model[id]` to get the agent with the given `id`.
`space` is a subtype of `AbstractSpace`: [`GraphSpace`](@ref), [`GridSpace`](@ref) or
[`ContinuousSpace`](@ref).
If it is ommited then all agents are virtually in one position and have no spatial structure.
**Note:** Spaces are mutable objects and are not designed to be shared between models.
Create a fresh instance of a space with the same properties if you need to do this.
`properties = nothing` is additional model-level properties (typically a dictionary)
that can be accessed as `model.properties`. However, if `properties` is a dictionary with
key type `Symbol`, or of it is a struct, then the syntax
`model.name` is short hand for `model.properties[:name]` (or `model.properties.name`
for structs).
This syntax can't be used for `name` being `agents, space, scheduler, properties`,
which are the fields of `AgentBasedModel`.
`scheduler = fastest` decides the order with which agents are activated
(see e.g. [`by_id`](@ref) and the scheduler API).
Type tests for `AgentType` are done, and by default
warnings are thrown when appropriate. Use keyword `warn=false` to supress that.
"""
function AgentBasedModel(
::Type{A}, space::S = nothing;
scheduler::F = fastest, properties::P = nothing, warn = true
) where {A<:AbstractAgent, S<:SpaceType, F, P}
agent_validator(A, space, warn)
agents = Dict{Int, A}()
return ABM{S, A, F, P}(agents, space, scheduler, properties, Ref(0))
end
function AgentBasedModel(agent::AbstractAgent, args...; kwargs...)
return ABM(typeof(agent), args...; kwargs...)
end
#######################################################################################
# %% Model accessing api
#######################################################################################
export random_agent, nagents, allagents, allids, nextid
"""
model[id]
getindex(model::ABM, id::Integer)
Return an agent given its ID.
"""
Base.getindex(m::ABM, id::Integer) = m.agents[id]
"""
model[id] = agent
setindex!(model::ABM, agent::AbstractAgent, id::Int)
Add an `agent` to the `model` at a given index: `id`.
Note this method will return an error if the `id` requested is not equal to `agent.id`.
**Internal method**, use [`add_agents!`](@ref) instead to actually add an agent.
"""
function Base.setindex!(m::ABM, a::AbstractAgent, id::Int)
a.id ≠ id && throw(ArgumentError("You are adding an agent to an ID not equal with the agent's ID!"))
m.agents[id] = a
m.maxid[] < id && (m.maxid[] += 1)
return a
end
"""
nextid(model::ABM) → id
Return a valid `id` for creating a new agent with it.
"""
nextid(model::ABM) = model.maxid[] + 1
"""
model.prop
getproperty(model::ABM, prop::Symbol)
Return a property from the current `model`, assuming the model `properties` are either
a dictionary with key type `Symbol` or a Julia struct.
For example, if a model has the set of properties `Dict(:weight => 5, :current => false)`,
retrieving these values can be obtained via `model.weight`.
The property names `:agents, :space, :scheduler, :properties, :maxid` are internals
and **should not be accessed by the user**.
"""
function Base.getproperty(m::ABM{S, A, F, P}, s::Symbol) where {S, A, F, P}
if s === :agents
return getfield(m, :agents)
elseif s === :space
return getfield(m, :space)
elseif s === :scheduler
return getfield(m, :scheduler)
elseif s === :properties
return getfield(m, :properties)
elseif s === :maxid
return getfield(m, :maxid)
elseif P <: Dict
return getindex(getfield(m, :properties), s)
else # properties is assumed to be a struct
return getproperty(getfield(m, :properties), s)
end
end
function Base.setproperty!(m::ABM{S, A, F, P}, s::Symbol, x) where {S, A, F, P}
properties = getfield(m, :properties)
if properties ≠ nothing && haskey(properties, s)
properties[s] = x
else
throw(ErrorException("Cannot set $(s) in this manner. Please use the `AgentBasedModel` constructor."))
end
end
"""
random_agent(model) → agent
Return a random agent from the model.
"""
random_agent(model) = model[rand(keys(model.agents))]
"""
random_agent(model, condition) → agent
Return a random agent from the model that satisfies `condition(agent) == true`.
The function generates a random permutation of agent IDs and iterates through them.
If no agent satisfies the condition, `nothing` is returned instead.
"""
function random_agent(model, condition)
ids = shuffle!(collect(keys(model.agents)))
i, L = 1, length(ids)
a = model[ids[1]]
while !condition(a)
i += 1
i > L && return nothing
a = model[ids[i]]
end
return a
end
"""
nagents(model::ABM)
Return the number of agents in the `model`.
"""
nagents(model::ABM) = length(model.agents)
"""
allagents(model)
Return an iterator over all agents of the model.
"""
allagents(model) = values(model.agents)
"""
allids(model)
Return an iterator over all agent IDs of the model.
"""
allids(model) = keys(model.agents)
#######################################################################################
# %% Model construction validation
#######################################################################################
"""
agent_validator(agent, space)
Validate the user supplied agent (subtype of `AbstractAgent`).
Checks for mutability and existence and correct types for fields depending on `SpaceType`.
"""
function agent_validator(::Type{A}, space::S, warn::Bool) where {A<:AbstractAgent, S<:SpaceType}
# Check A for required properties & fields
if isconcretetype(A)
do_checks(A, space, warn)
else
warn && @warn "AgentType is not concrete. If your agent is parametrically typed, you're probably seeing this warning because you gave `Agent` instead of `Agent{Float64}` (for example) to this function. You can also create an instance of your agent and pass it to this function. If you want to use `Union` types for mixed agent models, you can silence this warning."
for type in union_types(A)
do_checks(type, space, warn)
end
end
end
"""
do_checks(agent, space)
Helper function for `agent_validator`.
"""
function do_checks(::Type{A}, space::S, warn::Bool) where {A<:AbstractAgent, S<:SpaceType}
if warn
isbitstype(A) && @warn "AgentType should be mutable. Try adding the `mutable` keyword infront of `struct` in your agent definition."
end
(any(isequal(:id), fieldnames(A)) && fieldnames(A)[1] == :id) || throw(ArgumentError("First field of Agent struct must be `id` (it should be of type `Int`)."))
fieldtype(A, :id) <: Integer || throw(ArgumentError("`id` field in Agent struct must be of type `Int`."))
if space !== nothing
(any(isequal(:pos), fieldnames(A)) && fieldnames(A)[2] == :pos) || throw(ArgumentError("Second field of Agent struct must be `pos` when using a space."))
# Check `pos` field in A has the correct type
pos_type = fieldtype(A, :pos)
space_type = typeof(space)
if space_type <: GraphSpace && !(pos_type <: Integer)
throw(ArgumentError("`pos` field in Agent struct must be of type `Int` when using GraphSpace."))
elseif space_type <: GridSpace && !(pos_type <: NTuple{D, Integer} where {D})
throw(ArgumentError("`pos` field in Agent struct must be of type `NTuple{Int}` when using GridSpace."))
elseif space_type <: ContinuousSpace || space_type <: ContinuousSpace
if !(pos_type <: NTuple{D, <:AbstractFloat} where {D})
throw(ArgumentError("`pos` field in Agent struct must be of type `NTuple{<:AbstractFloat}` when using ContinuousSpace."))
end
if warn && any(isequal(:vel), fieldnames(A)) && !(fieldtype(A, :vel) <: NTuple{D, <:AbstractFloat} where {D})
@warn "`vel` field in Agent struct should be of type `NTuple{<:AbstractFloat}` when using ContinuousSpace."
end
end
end
end
#######################################################################################
# %% Pretty printing
#######################################################################################
function Base.show(io::IO, abm::ABM{S, A}) where {S, A}
n = isconcretetype(A) ? nameof(A) : string(A)
s = "AgentBasedModel with $(nagents(abm)) agents of type $(n)"
if abm.space === nothing
s*= "\n no space"
else
s*= "\n space: $(sprint(show, abm.space))"
end
s*= "\n scheduler: $(schedulername(abm.scheduler))"
print(io, s)
if abm.properties ≠ nothing
print(io, "\n properties: ", abm.properties)
end
end
schedulername(x::Union{Function, DataType}) = nameof(x)