ABMEv_Sim.jl 3.34 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
function Base.show(io::IO, s::Simulation{A,S,T,F}) where {A,S,T,F}
     println(io, "Simulation with agents of type", A)
 end
Victor's avatar
Victor committed
39
40
41
# 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}
42
43
44

"""
$(SIGNATURES)
Victor's avatar
Victor committed
45
Add `w` with callbacks `s.cb` to `s` if provided
46
"""
Victor's avatar
Victor committed
47
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
48
49
50
51
52
53
54
55
    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

56
57
58
59
60
61
62
63
64
function add_entry_cb_only!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
    push!(s.agentarray,[Agent(w.space)]) # pushing NA agents
    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

65
#TODO : code it
Victor's avatar
Victor committed
66
function get_xnt(s::Simulation;trait = 1)
67
    return [getindex.(wa,trait) for wa in s.agentarray],[fill(t,size(s[j])) for (j,t) in enumerate(s.tspan)]
Victor's avatar
Victor committed
68
end
69
70
71
72
# 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
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
#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