ABMEv_Sim.jl 2.26 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
6
    agentarray::Vector{AbstractAgentM}
    space::S
Victor's avatar
Victor committed
7
    tspan::Vector{T}
8
9
    cb::F
    df_agg
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
21
22
23
24
25
"""
$(SIGNATURES)
"""
function Simulation(w0::World{A,S,T};cb=(names = String[],agg =nothing)) where {A,S,T}
    tspan = zeros(1);
    NMax = maxsize(w0)
    #agentarray is of size 2 at the beginning
    agentarray = copy.(collect(w0.agents))
    agentarray = hcat(agentarray,Array{Missing}(missing,NMax,1))
    !isnothing(cb.agg) ? df_agg = [Dict(cb.names .=> cb.agg(world))] : df_agg = nothing
    Simulation{A,S,T,cb.agg}([agentarray],space(w0),tspan,cb.agg,df_agg,parameters(w0))
Victor's avatar
Victor committed
26
27
28
 end

get_tend(s::Simulation) = s.tspan[end]
29
get_size(s::Simulation) = length(s.tspan)
Victor's avatar
Victor committed
30

31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
"""
$(SIGNATURES)
Add `w` with callbacks `s.cb` to `s`
"""
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F<:Function}
    push!(s.agentarray,copy.(collect(w.agents)))
    push!(s.tspan,w.t)
    push!(s.df_agg,Dict(s.cb.names .=> s.cb.agg(world)))
end

"""
$(SIGNATURES)
Add `w` to `s`
"""
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F<:Nothing}
    push!(s.agentarray,copy.(collect(w.agents)))
    push!(s.tspan,w.t)
Victor's avatar
Victor committed
48
49
end

Victor's avatar
Victor committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
#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