To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

ABMEv_world.jl 2.39 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
mutable struct World{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
5
    agents::Vector{A}
Victor's avatar
Victor committed
6
    space::S
7
    p::Dict
Victor's avatar
Victor committed
8
9
10
    t::T
end

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

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

37
addAgent!(w::World,a::AbstractAgent) = begin
38
    push!(w.agents,a)
Victor's avatar
Victor committed
39
end
40
removeAgent!(w::World,i::Int) = begin
41
    deleteat!(w.agents,i)
Victor's avatar
Victor committed
42
43
44
45
46
47
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
48

Victor's avatar
Victor committed
49
#TODO : modify this
50
51
52
53
"""
$(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
54
55
# Warning
This works only for linear subspace, indexed with a single value
56
"""
Victor's avatar
Victor committed
57
get_x(w::World,trait) = hcat(collect.(getindex.(agents(w),trait))...)'
Victor's avatar
Victor committed
58
59
60

"""
$(SIGNATURES)
61
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
62
If `geotrait=true` the geotrait is also added to the set of trait, in the last column.
63
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
64
"""
65
function get_xarray(world::World,geotrait::Bool=false)
Victor's avatar
Victor committed
66
    xarray = get_x(world,Colon())
67
    if geotrait
68
        xarray = hcat(xarray, get_geo.(agents(world),world.t))
69
70
71
72
    end
    return xarray
end
@deprecate get_xarray(world,geotrait=false) get_x(world,Colon())
Victor's avatar
Victor committed
73
74
75
76
77

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