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_Sim.jl 3.2 KB
Newer Older
1
abstract type AbstractAlg end
Victor's avatar
Victor committed
2
3

# this used to be  world
Victor's avatar
Victor committed
4
mutable struct Simulation{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number,F}
5
    agentarray::Vector{Vector{AbstractAgent}}
6
    space::S
Victor's avatar
Victor committed
7
    tspan::Vector{T}
8
9
    cb::NamedTuple
    df_agg::Vector{Dict}
Victor's avatar
Victor committed
10
11
12
    p::Dict{String,Any}
end

13
# callbacks has to be of the form (names" => String[],"aggregates" => Function)
Victor's avatar
Victor committed
14

15
16
17
18
19
20
"""
$(SIGNATURES)
"""
function Simulation(w0::World{A,S,T};cb=(names = String[],agg =nothing)) where {A,S,T}
    tspan = zeros(1);
    #agentarray is of size 2 at the beginning
21
    !isnothing(cb.agg) ? df_agg = [Dict(cb.names .=> [f(w0) for f in cb.agg])] : df_agg = [Dict()]
22
    Simulation{A,S,T,typeof(cb.agg)}([copy.(agents(w0))],space(w0),tspan,cb,df_agg,parameters(w0))
Victor's avatar
Victor committed
23
24
25
 end

get_tend(s::Simulation) = s.tspan[end]
26
get_size(s::Simulation) = length(s.tspan)
27
get_tspan(s::Simulation) = s.tspan
28
29
30
31
32
get_world(s::Simulation,i) = World(s.agentarray[i],s.space,s.p,s.tspan[i])
Base.getindex(s::Simulation,i) = s.agentarray[i]
import Base.lastindex
Base.lastindex(s::Simulation) = get_size(s)

33
34
Base.getindex(s::Simulation,measure::String) = [agg[measure] for agg in s.df_agg]

Victor's avatar
Victor committed
35

Victor's avatar
Victor committed
36
37
38
# TODO: define two functions with signatures
# function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F<:Function}
# function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F<:Nothing}
39
40
41

"""
$(SIGNATURES)
Victor's avatar
Victor committed
42
Add `w` with callbacks `s.cb` to `s` if provided
43
"""
Victor's avatar
Victor committed
44
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
45
46
47
48
49
50
51
52
53
54
    push!(s.agentarray,copy.(agents(w)))
    push!(s.tspan,w.t)
    if !(F==Nothing)
        push!(s.df_agg,Dict(s.cb.names .=> [f(w) for f in s.cb.agg]))
    end
    return nothing
end

#TODO : code it
function get_x(s::Simulation,i)
Victor's avatar
Victor committed
55
56
57
58
59
60
    i = get_size(s)
    j = size(s.agentarray,2)
    if i == j
        # we double the siwe of agent array
        s.agentarray = hcat(s.agentarray,Array{Missing}(missing,maxsize(w),j))
    end
61
    s.agentarray[1:size(w),i+1] .= copy.(agents(w))
62
    push!(s.tspan,w.t)
63
64
    if !(F==Nothing)
        push!(s.df_agg,Dict(s.cb.names .=> [f(w) for f in s.cb.agg]))
Victor's avatar
Victor committed
65
    end
66
    return nothing
Victor's avatar
Victor committed
67
end
68
69
70
71
# get_x(agentarray::Array{T},t,trait::Integer) where {T <: AbstractAgent} = reshape(hcat(get_x.(agentarray,t,trait)),size(agentarray,1),size(agentarray,2))
# @deprecate get_x(agentarray::Array{T},t::Number,trait::Integer)


Victor's avatar
Victor committed
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
#TODO: code it
# function world2df(world::Array{T,1},geotrait=false) where {T <: Agent}
#     xx = get_xarray(world)
#     dfw = DataFrame(:f => get_fitness.(world))
#     for i in 1:size(xx,1)
#         dfw[Meta.parse("x$i")] = xx[i,:]
#     end
#     if geotrait
#         dfw[:g] = get_geo.(world)
#     end
#     return dfw
# end
#
# """
#     world2df(world::Array{T,1},t::Number,geotrait = false) where {T <: Agent}
# Converts the array of agent world to a datafram, where each column corresponds to a trait of the
# agent, and an extra column captures fitness.
# Each row corresponds to an agent
# """
# function world2df(world::Array{T,1},t::Number,geotrait = false) where {T <: Agent}
#     xx = get_xarray(world)
#     dfw = DataFrame(:f => get_fitness.(world))
#     for i in 1:size(xx,1)
#         dfw[Meta.parse("x$i")] = xx[i,:]
#     end
#     if geotrait
#         dfw[:g] = get_geo.(world,t)
#     end
#     return dfw
# end