ABMEv_world.jl 3.29 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
vboussange's avatar
vboussange committed
16
17
18
19
    if typeof(first(w)) !== A
        throw(ArgumentError("eltype(w) should be equal to typeof(first(w)), which is not the case.\n
                            Try to generate the array of Agents in an other way"))
    end
20
    World{A,S,T}(w,s,p,t)
21
22
end

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

Victor's avatar
Victor committed
41
42
43
44
function Base.show(io::IO, w::World{A,S,T}) where {A,S,T}
     println(io, "World with agents of type", A)
 end

45
addAgent!(w::World,a::AbstractAgent) = begin
46
    push!(w.agents,a)
Victor's avatar
Victor committed
47
end
48
removeAgent!(w::World,i::Int) = begin
49
    deleteat!(w.agents,i)
Victor's avatar
Victor committed
50
51
52
53
54
55
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
56

Victor's avatar
Victor committed
57
58
59
60
61
62

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

63
64
65
66
"""
$(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
67
68
!!! warning "Warning"
    Geotrait might be deprecated in the future.
69
"""
70
function get_x(w::World,trait)
Victor's avatar
Victor committed
71
72
73
74
75
76
    if !(trait == 0)
        if ndims(space(w)[trait]) > 1
            return hcat(collect.(getindex.(agents(w),trait))...)'
        else
            return collect(getindex.(agents(w),trait))
        end
77
    else
Victor's avatar
Victor committed
78
        return collect(get_geo(w))
79
80
    end
end
Victor's avatar
Victor committed
81
82
83

"""
$(SIGNATURES)
84
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
85
If `geotrait=true` the geotrait is also added to the set of trait, in the last column.
86
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
87
88
!!! warning "Warning"
    It does not work with subspace where ndims(subspace) > 1.
Victor's avatar
Victor committed
89
"""
90
function get_xarray(world::World,geotrait::Bool=false)
Victor's avatar
Victor committed
91
    xarray = get_x(world,Colon())
92
    if geotrait
93
        xarray = hcat(xarray, get_geo.(agents(world),world.t))
94
95
96
97
    end
    return xarray
end
@deprecate get_xarray(world,geotrait=false) get_x(world,Colon())
Victor's avatar
Victor committed
98
99
100
101
102
103
104
105
106
107
108

"""
    function give_birth(mum_idx::Int,w::World)
Copies agent within index `mum_idx`, and increment it by dx.
Return new agent (offspring).
"""
function give_birth(mum_idx::Int,w::World)
    new_a = copyxt(w[mum_idx])
    increment_x!(new_a,space(w),parameters(w),time(w))
    return new_a
end