To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit b8d18c9e authored by Victor's avatar Victor
Browse files

adding a method get_world(s::Simulation)

parent 477531cc
......@@ -31,7 +31,7 @@ module ABMEv
export World,parameters,time,space,agents,size,maxsize,addAgent!,removeAgent!
export run!,give_birth,updateWorld!,update_clock!,updateBirthEvent!,
updateDeathEvent!#,runWorld_G!,runWorld_WF!,
export Simulation,add_entry!,get_tend,get_size,get_tspan
export Simulation,add_entry!,get_tend,get_size,get_tspan,get_world
export H_discrete,findclusters,var,covgeo,hamming,get_beta_div, get_alpha_div,
get_dist_hist,get_pairwise_average_isolation,
get_local_pairwise_average_isolation,
......
......@@ -25,7 +25,11 @@ function Simulation(w0::World{A,S,T};cb=(names = String[],agg =nothing)) where {
get_tend(s::Simulation) = s.tspan[end]
get_size(s::Simulation) = length(s.tspan)
get_tspan(s::Simulation) = s.tspan
Base.getindex(s::Simulation,i,j) = s.agentarray[i][j]
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)
Base.getindex(s::Simulation,measure::String) = [agg[measure] for agg in s.df_agg]
......
......@@ -11,41 +11,11 @@ function update_rates_graph!(world,C,p::Dict,t::Float64)
end
"""
function runWorld_store_WF(p,world0;mode="std")
Wright Fisher process. Returns an array worldall with every agents.
"""
function runWorld_store_WF(p,world0;mode="std")
# worldall = repeat(world0,inner = (1,length(1:Int(p["tend"]))))
# N=length(world0);
# newworld = copy.(world0)
# if mode == "std"
# update_rates! = update_rates_std!
# elseif mode == "2D"
# update_rates! = update_rates_2D!
# elseif mode == "grad2D"
# update_rates! = update_rates_grad2D!
# elseif mode == "mountain"
# update_rates! = update_rates_mountain!
# elseif mode == "split"
# update_rates! = update_rates_std_split!
# elseif mode == "graph"
# update_rates! = update_rates_graph!
# else
# error("Mode $mode is not recognized")
# end
# for i in 1:(Int(p["tend"])-1)
# # we have to take views, otherwise it does not affect worldall
# world = @view worldall[:,i];newworld = @view worldall[:,i+1];
# updateWorld_WF!(world,newworld,p,update_rates!,Float64(i))
# end
# return worldall,collect(0:Int(p["tend"]-1))
end
"""
function runWorld_store_G(p,world0)
Gillepsie process.
- returns a tuple 'worldall, tspanarray'
$(SIGNATURES)
Run `w` with algorithm `alg`, until `tend` is reached.
Returns a `Simulation` type.
- `worldall` stores the world every `p["dt_saving"]` time steps.
If `p["dt_saving"]` not specified, it returns an array with two columns,
If `dt_saving` not specified, `sim` contains an array of two elements,
first corresponding to initial conditions and last corresponding to world in the last time step.
>:warning: if you choose `nagents = 1` then nothing is saved until the end of the simulation.
"""
......
......@@ -2,31 +2,31 @@
# TODO: do a constructor that ensures the parameters numerics are of the same type as the agents
mutable struct World{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
agents::Vector{AbstractAgent}
agents::Vector{A}
space::S
parameters::Dict
p::Dict
t::T
end
#constructor
function World(w::Vector{A},s::S,p::Dict,t::T=0.) where {A<:AbstractAgent,S<:AbstractSpacesTuple,T}
if typeof(p["D"]) != eltype(skipmissing(w)[1])
throw(ArgumentError("Diffusion coefficient does not match with underlying space\n `D::Tuple`"))
end
# if typeof(p["D"]) != eltype(skipmissing(w)[1])
# throw(ArgumentError("Diffusion coefficient does not match with underlying space\n `D::Tuple`"))
# end
World{A,S,T}(w,s,p,t)
end
# this throws an iterators of agents in the world
agents(world::World) = world.agents
parameters(world::World) = world.parameters
parameters(world::World) = world.p
time(w::World) = w.t
space(w::World) = w.space
maxsize(w::World) = w.parameters["NMax"]
maxsize(w::World) = w.p["NMax"]
# this throws indices that are occupied by agents
# this throws agents of an abstract array of size size(world)
import Base:size,getindex
Base.size(world::World) = length(world.agents)
Base.copy(w::W) where {W<:World} = W(copy.(w.agents),w.space,w.parameters,copy(w.t))
Base.copy(w::W) where {W<:World} = W(copy.(w.agents),w.space,w.p,copy(w.t))
## Accessors
"""
$(SIGNATURES)
......
# In this file lie the function for WF algorithm
"""
"""
$(TYPEDEF)
"""
struct WF <: AbstractAlg end
export WF
"""
function updateWorld_WF!(world,newworld,C,p,update_rates!,t)
If p["reflected"]=true, we reflect only first trait corresponding to geographic position
"""
function updateWorld_WF!(world,newworld,p,update_rates!,t)
"""
function updateWorld_WF!(world,newworld,p,update_rates!,t)
@debug "updating rates"
update_rates!(world,p,t);
# normalise to make sure that we have a probability vector
......@@ -23,4 +29,4 @@
for w in newworld
increment_x!(w,p)
end
end
end
......@@ -50,6 +50,9 @@ eltype(cb.agg)
@test typeof(sim["gamma_div"]) <: Vector
@test typeof(get_world(sim,get_size(sim))) <: World
@test typeof(sim[2]) <: Vector
## testing plot
using Plots
plot(get_tspan(sim),sim["gamma_div"])
Markdown is supported
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