Commit c0473eae authored by Victor's avatar Victor
Browse files

v3.1 with changes in cb

parent 6bf9e766
Pipeline #84351 failed with stage
in 16 minutes and 49 seconds
name = "ABMEv"
uuid = "837ac870-fb52-4b0c-9a0e-030f2f36f5ed"
authors = ["Victor Boussange "]
version = "3.0.0"
version = "3.1.0"
[deps]
Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
......
abstract type AbstractAlg end
# this used to be world
mutable struct Simulation{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number,F}
mutable struct Simulation{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
agentarray::Vector{Vector{AbstractAgent}}
space::S
tspan::Vector{T}
cb::NamedTuple
df_agg::Vector{Dict}
p::Dict{String,Any}
end
......@@ -15,11 +14,11 @@ end
"""
$(SIGNATURES)
"""
function Simulation(w0::World{A,S,T};cb=(names = String[],agg =nothing)) where {A,S,T}
tspan = zeros(1);
function Simulation(w0::World{A,S,T};cb = nothing) where {A,S,T}
tspan = zeros(1)
#agentarray is of size 2 at the beginning
!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)}([copy.(agents(w0))],space(w0),tspan,cb,df_agg,parameters(w0))
isnothing(cb) ? df_agg = Dict[] : df_agg = [cb(w0)]
Simulation{A,S,T}([copy.(agents(w0))],space(w0),tspan,df_agg,parameters(w0))
end
get_tend(s::Simulation) = s.tspan[end]
......@@ -33,7 +32,7 @@ Base.lastindex(s::Simulation) = get_size(s)
Base.getindex(s::Simulation,measure::String) = [agg[measure] for agg in s.df_agg]
function Base.show(io::IO, s::Simulation{A,S,T,F}) where {A,S,T,F}
function Base.show(io::IO, s::Simulation{A,S,T}) where {A,S,T}
println(io, "Simulation with agents of type", A)
end
# TODO: define two functions with signatures
......@@ -44,20 +43,20 @@ function Base.show(io::IO, s::Simulation{A,S,T,F}) where {A,S,T,F}
$(SIGNATURES)
Add `w` with callbacks `s.cb` to `s` if provided
"""
function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
function add_entry!(s::Simulation{A,S,T},w::World,cb) where {A,S,T}
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]))
if !isnothing(cb)
push!(s.df_agg,cb(w))
end
return nothing
end
function add_entry_cb_only!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F}
function add_entry_cb_only!(s::Simulation{A,S,T},w::World,cb) where {A,S,T}
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]))
if !isnothing(cb)
push!(s.df_agg,cb(w))
end
return nothing
end
......
......@@ -15,7 +15,7 @@ This functionality is as of now only compatible with `dt_saving` not specified.
function run!(w::World{A,S,T},alg::L,tend::Number,b,d;
dt_saving=nothing,
t_saving_cb=nothing,
cb=(names = String[],agg =nothing)) where {A,S,T,L<:AbstractAlg}
cb=nothing) where {A,S,T,L<:AbstractAlg}
# argument check
_check_timedep(b,d)
(!isnothing(dt_saving) && !isnothing(dt_saving)) ? ArgumentError("For now, can not specify both `dt_saving` and `t_saving_cb`") : nothing
......@@ -45,11 +45,11 @@ function run!(w::World{A,S,T},alg::L,tend::Number,b,d;
end
if t - get_tend(sim) >= dt_saving
@info "saving world @ t = $(t)/ $(tend)"
add_entry!(sim,w)
add_entry!(sim,w,cb)
end
if t >= first(t_saving_cb)
@info "saving callback only @ t = $(t)/ $(tend)"
add_entry_cb_only!(sim,w)
add_entry_cb_only!(sim,w,cb)
popfirst!(t_saving_cb)
end
dt = updateWorld!(w,alg,b,d)
......@@ -57,7 +57,7 @@ function run!(w::World{A,S,T},alg::L,tend::Number,b,d;
i += 1
end
# Saving last time step
add_entry!(sim,w)
add_entry!(sim,w,cb)
@info "simulation stopped at t=$(t), after $(i) generations"
return sim
end
......
......@@ -21,7 +21,7 @@ typeof(s)
# adding an entry to sim
newa = give_birth(1,w0)
addAgent!(w0,newa)
@test isnothing(add_entry!(s,w0))
@test isnothing(add_entry!(s,w0,nothing))
@test get_size(s) == 2
#TODO: try with callbacks
......@@ -45,8 +45,7 @@ p = Dict{String,Any}();@pack! p = D,mu,NMax
myagents = [Agent(myspace,[0.],ancestors=true,rates=true) for i in 1:K0]
w0 = World(myagents,myspace,p,0.)
w1 = copy(w0)
cb = (names = ["gamma_div"], agg = Function[w -> var(Float64.(get_x(w,1)))])
eltype(cb.agg)
cb(w) = Dict("gamma_div" => w -> var(Float64.(get_x(w,1))))
@time sim = run!(w1,Gillepsie(),tend,b,d,cb=cb,dt_saving = .1)
@test typeof(sim["gamma_div"]) <: Vector
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment