ABMEv_Sim.jl 3.15 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}
Victor's avatar
Victor committed
5
    agentarray::Array{AbstractAgentM,2}
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
21
22
23
"""
$(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))
24
25
    !isnothing(cb.agg) ? df_agg = [Dict(cb.names .=> [f(w0) for f in cb.agg])] : df_agg = [Dict()]
    Simulation{A,S,T,typeof(cb.agg)}(agentarray,space(w0),tspan,cb,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)
30
get_tspan(s::Simulation) = s.tspan
Victor's avatar
Victor committed
31
Base.getindex(s::Simulation,i,j) = s.agentarray[i,j]
32
33
Base.getindex(s::Simulation,measure::String) = [agg[measure] for agg in s.df_agg]

Victor's avatar
Victor committed
34
35
36
import Base:lastindex,size
Base.lastindex(s::Simulation,i) = lastindex(s.agentarray,i)
Base.size(s::Simulation,i) = size(s.agentarray,i)
Victor's avatar
Victor committed
37

Victor's avatar
Victor committed
38
39
40
# 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}
41
42
43

"""
$(SIGNATURES)
Victor's avatar
Victor committed
44
Add `w` with callbacks `s.cb` to `s` if provided
45
"""
Victor's avatar
Victor committed
46
47
48
49
50
51
52
53
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
    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
    s.agentarray[:,i+1] .= copy.(collect(w.agents))
54
    push!(s.tspan,w.t)
55
56
    if !(F==Nothing)
        push!(s.df_agg,Dict(s.cb.names .=> [f(w) for f in s.cb.agg]))
Victor's avatar
Victor committed
57
    end
Victor's avatar
Victor committed
58
59
end

60
61
62
63
64
clean!(sim::Simulation) = sim.agentarray = sim.agentarray[:,1:length(sim.tspan)]
# 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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
#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