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