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 ...@@ -31,7 +31,7 @@ module ABMEv
export World,parameters,time,space,agents,size,maxsize,addAgent!,removeAgent! export World,parameters,time,space,agents,size,maxsize,addAgent!,removeAgent!
export run!,give_birth,updateWorld!,update_clock!,updateBirthEvent!, export run!,give_birth,updateWorld!,update_clock!,updateBirthEvent!,
updateDeathEvent!#,runWorld_G!,runWorld_WF!, 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, export H_discrete,findclusters,var,covgeo,hamming,get_beta_div, get_alpha_div,
get_dist_hist,get_pairwise_average_isolation, get_dist_hist,get_pairwise_average_isolation,
get_local_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 { ...@@ -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_tend(s::Simulation) = s.tspan[end]
get_size(s::Simulation) = length(s.tspan) get_size(s::Simulation) = length(s.tspan)
get_tspan(s::Simulation) = 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] 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) ...@@ -11,41 +11,11 @@ function update_rates_graph!(world,C,p::Dict,t::Float64)
end end
""" """
function runWorld_store_WF(p,world0;mode="std") $(SIGNATURES)
Wright Fisher process. Returns an array worldall with every agents. Run `w` with algorithm `alg`, until `tend` is reached.
""" Returns a `Simulation` type.
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'
- `worldall` stores the world every `p["dt_saving"]` time steps. - `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. 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. >:warning: if you choose `nagents = 1` then nothing is saved until the end of the simulation.
""" """
......
...@@ -2,31 +2,31 @@ ...@@ -2,31 +2,31 @@
# TODO: do a constructor that ensures the parameters numerics are of the same type as the agents # 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} mutable struct World{A<:AbstractAgent, S<:AbstractSpacesTuple,T<:Number}
agents::Vector{AbstractAgent} agents::Vector{A}
space::S space::S
parameters::Dict p::Dict
t::T t::T
end end
#constructor #constructor
function World(w::Vector{A},s::S,p::Dict,t::T=0.) where {A<:AbstractAgent,S<:AbstractSpacesTuple,T} 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]) # if typeof(p["D"]) != eltype(skipmissing(w)[1])
throw(ArgumentError("Diffusion coefficient does not match with underlying space\n `D::Tuple`")) # throw(ArgumentError("Diffusion coefficient does not match with underlying space\n `D::Tuple`"))
end # end
World{A,S,T}(w,s,p,t) World{A,S,T}(w,s,p,t)
end end
# this throws an iterators of agents in the world # this throws an iterators of agents in the world
agents(world::World) = world.agents agents(world::World) = world.agents
parameters(world::World) = world.parameters parameters(world::World) = world.p
time(w::World) = w.t time(w::World) = w.t
space(w::World) = w.space 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 indices that are occupied by agents
# this throws agents of an abstract array of size size(world) # this throws agents of an abstract array of size size(world)
import Base:size,getindex import Base:size,getindex
Base.size(world::World) = length(world.agents) 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 ## Accessors
""" """
$(SIGNATURES) $(SIGNATURES)
......
# In this file lie the function for WF algorithm # In this file lie the function for WF algorithm
""" """
function updateWorld_WF!(world,newworld,C,p,update_rates!,t) $(TYPEDEF)
If p["reflected"]=true, we reflect only first trait corresponding to geographic position """
""" struct WF <: AbstractAlg end
function updateWorld_WF!(world,newworld,p,update_rates!,t) export WF
@debug "updating rates"
update_rates!(world,p,t); """
# normalise to make sure that we have a probability vector function updateWorld_WF!(world,newworld,C,p,update_rates!,t)
fitness = get_fitness.(world) If p["reflected"]=true, we reflect only first trait corresponding to geographic position
# we need to substract the minimum, otherwise fitness can be negative """
prob = normalize(fitness .- minimum(fitness) .+ eps(1.),1) function updateWorld_WF!(world,newworld,p,update_rates!,t)
offsprings = rand(Multinomial(length(world),prob)) @debug "updating rates"
# this guy can be done in parallel update_rates!(world,p,t);
@debug "filling new offspring row" # normalise to make sure that we have a probability vector
for (parentID,nboffspring) in collect(enumerate(offsprings)) fitness = get_fitness.(world)
for j in 1:nboffspring # we need to substract the minimum, otherwise fitness can be negative
newworld[sum(offsprings[1:(parentID-1)]) + j] = copy(world[parentID]) prob = normalize(fitness .- minimum(fitness) .+ eps(1.),1)
end offsprings = rand(Multinomial(length(world),prob))
end # this guy can be done in parallel
# we introduce randomness here @debug "filling new offspring row"
@debug "incrementing offsprings traits" for (parentID,nboffspring) in collect(enumerate(offsprings))
for w in newworld for j in 1:nboffspring
increment_x!(w,p) newworld[sum(offsprings[1:(parentID-1)]) + j] = copy(world[parentID])
end end
end end
# we introduce randomness here
@debug "incrementing offsprings traits"
for w in newworld
increment_x!(w,p)
end
end
...@@ -50,6 +50,9 @@ eltype(cb.agg) ...@@ -50,6 +50,9 @@ eltype(cb.agg)
@test typeof(sim["gamma_div"]) <: Vector @test typeof(sim["gamma_div"]) <: Vector
@test typeof(get_world(sim,get_size(sim))) <: World
@test typeof(sim[2]) <: Vector
## testing plot ## testing plot
using Plots using Plots
plot(get_tspan(sim),sim["gamma_div"]) 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