ABMEv_world.jl 2.75 KB
Newer Older
Victor's avatar
Victor committed
1
# this used to be the worldalive
2
3

# TODO: do a constructor that ensures the parameters numerics are of the same type as the agents
Victor's avatar
Victor committed
4
5
6
mutable struct World{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
    agents::Vector{AbstractAgentM}
    space::S
7
    parameters::Dict
Victor's avatar
Victor committed
8
9
10
    t::T
end

11
12
13
#constructor
function World(w::Vector{A},s::S,p::Dict,t::T=0.) where {A<:AbstractAgent,S<:AbstractSpacesTuple,T}
    if typeof(p["D"]) != eltype(skipmissing(w)[1])
Victor's avatar
Victor committed
14
        throw(ArgumentError("Diffusion coefficient does not match with underlying space\n `D::Tuple`"))
15
16
17
18
19
    end
    ww = vcat(w,repeat([missing],Int(p["NMax"] - length(w))))
    World{A,S,T}(ww,s,p,t)
end

20
21
# this throws an iterators of agents in the world
agents(world::World) = skipmissing(world.agents)
Victor's avatar
Victor committed
22
parameters(world::World) = world.parameters
23
time(w::World) = w.t
Victor's avatar
Victor committed
24
space(w::World) = w.space
25
26
maxsize(w::World) = w.parameters["NMax"]
_findfreeidx(w::World) = findfirst(ismissing,w.agents)
Victor's avatar
Victor committed
27
# this throws indices that are occupied by agents
28
_get_idx(world::World) = collect(eachindex(agents(world)))
Victor's avatar
Victor committed
29
# this throws agents of an abstract array of size size(world)
30
31
import Base:size,getindex
Base.size(world::World) = length(world.agents) - count(ismissing,world.agents)
32
33
34
35
36
37
Base.copy(w::W) where {W<:World} = W(copy.(w.agents),w.space,w.parameters,copy(w.t))
## Accessors
"""
$(SIGNATURES)
Get x of world without geotrait.
"""
Victor's avatar
Victor committed
38
39
Base.getindex(w::World,i::Int) = w.agents[_get_idx(w)[i]]

40
addAgent!(w::World,a::AbstractAgent) = begin
Victor's avatar
Victor committed
41
42
43
44
    idx = _findfreeidx(w)
    w.agents[idx] = a
    return nothing
end
45
removeAgent!(w::World,i::Int) = begin
46
    w.agents[_get_idx(w)[i]] = missing
Victor's avatar
Victor committed
47
48
49
50
51
52
53
    return nothing
end

update_clock!(w::World{A,S,T},dt) where {A,S,T} = begin
    w.t = convert(T,sum(w.t + dt))
    return nothing
end
Victor's avatar
Victor committed
54

Victor's avatar
Victor committed
55
#TODO : modify this
56
57
58
59
"""
$(SIGNATURES)
Returns trait of every agents of world in the form of an array which dimensions corresponds to the input.
If `trait = 0` , we return the geotrait.
Victor's avatar
Victor committed
60
61
# Warning
This works only for linear subspace, indexed with a single value
62
"""
Victor's avatar
Victor committed
63
get_x(w::World,trait) = hcat(collect.(getindex.(agents(w),trait))...)'
Victor's avatar
Victor committed
64
65
66

"""
$(SIGNATURES)
67
Returns every traits of every agents of `world` in the form **of a one dimensional array** (in contrast to `get_x`).
Victor's avatar
Victor committed
68
If `geotrait=true` the geotrait is also added to the set of trait, in the last column.
69
If you do not want to specify `t` (only useful for geotrait), it is also possible to use `get_xarray(world::Array{T,1}) where {T <: Agent}`.
Victor's avatar
Victor committed
70
"""
71
function get_xarray(world::World,geotrait::Bool=false)
Victor's avatar
Victor committed
72
    xarray = get_x(world,Colon())
73
    if geotrait
Victor's avatar
Victor committed
74
        xarray = hcat( xarray, get_geo.(agents(world),world.t))
75
76
77
78
    end
    return xarray
end
@deprecate get_xarray(world,geotrait=false) get_x(world,Colon())
Victor's avatar
Victor committed
79
80
81
82
83

"""
$(SIGNATURES)
"""
get_geo(w::World) = map(a-> get_geo(a,time(w)), agents(w))