Commit 5163adc3 authored by Victor's avatar Victor
Browse files

Changes on type Agent, that is now Agent{T,U}. Added a function increment! for...

Changes on type Agent, that is now Agent{T,U}. Added a function increment! for type Agent{MixedAgent,U}.
parent eab9cde4
......@@ -15,7 +15,7 @@ module ABMEv
@reexport using Distributions
export update_rates!
export Mixed,Agent,get_fitness,get_x,get_dim,get_nancestors,get_xarray,get_xhist,
export MixedAgent,StdAgent,Agent,get_fitness,get_x,get_dim,get_nancestors,get_xarray,get_xhist,
get_geo,get_b,get_d,increment_x!,get_inc_reflected,
split_move,split_merge_move,KK,tin,new_world_G
export copy,runWorld_store_WF,runWorld_store_G #,runWorld_G!,runWorld_WF!,
......
......@@ -12,14 +12,14 @@ end
# Constructors
# This constructor should be used when one wants to impose the type of the agent (e.g. Mixed)
Agent{T}(xhist::Array{U}) where T = Agent{T,U}(reshape(xhist,:,1),0.,1.)
Agent{T}(xhist::Array{U}) where {T,U} = Agent{T,U}(reshape(xhist,:,1),0.,1.)
# This constructor is used by default
Agent(xhist::Array{U}) where U <: Number = Agent{StdAgent}(xhist)
Agent(xhist::Array{U}) where {U <: Number} = Agent{StdAgent}(xhist)
Agent() = Agent(Float64[],0.,1.)
import Base.copy
copy(a::Agent) = Agent(a.x_history,a.d,a.b)
copy(a::Agent{T,U}) where {T,U} = Agent{T,U}(a.x_history,a.d,a.b)
copy(m::Missing) = missing
"""
......@@ -52,7 +52,7 @@ Returns trait of every agents of world in the form of an array which dimensions
Particularly suited for an array world corresponding to a timeseries.
"""
get_xarray(world::Array{Agent},trait::Int) = reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2))
get_xarray(world::Array{T},trait::Int) where {T <: Agent}= reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2))
"""
get_xhist(world::Vector{Agent},geotrait = false)
......@@ -84,7 +84,7 @@ increment_x!(a::Agent{StdAgent,U},p::Dict)
This function increments agent by random numbers specified in p
ONLY FOR CONTINUOUS DOMAINS
"""
function increment_x!(a::Agent{StdAgent,U},p::Dict)
function increment_x!(a::Agent{StdAgent,U},p::Dict) where U
tdim = length(p["D"])
reflected = haskey(p,"reflected") ? p["reflected"] : false
if reflected
......@@ -104,19 +104,15 @@ function increment_x!(a::Agent{StdAgent,U},p::Dict)
This function increments first trait of agent with integer values, that are automatically reflected between 1 and p["nodes"].
ONLY FOR GRAPH TYPE DOMAINS
"""
# function increment_x!(a::Agent{Mixed},p::Dict)
# tdim = length(p["D"])
# inc = [get_inc_reflected(get_x(a,1),p["D"][1] *randn(),1,p["nodes"])]
# if tdim > 1
# inc = vcat(inc,(rand(tdim-1) < p["mu"][2:end]) .* p["D"][2:end] .* randn(tdim-1))
# end
# else
# # inc = yes no mutation * mutation
# inc = (rand(tdim) < vec(p["mu"])) .* vec(p["D"][:]) .* randn(tdim)
# end
# a.x_history = hcat(a.x_history, get_x(a) + reshape(inc,:,1));
# end
# end
function increment_x!(a::Agent{MixedAgent,U},p::Dict) where U
tdim = length(p["D"])
inc = [round(get_inc_reflected(get_x(a,1),p["D"][1] *randn(),1,p["nodes"]))]
if tdim > 1
inc = vcat(inc,(rand(tdim-1) < p["mu"][2:end]) .* p["D"][2:end] .* randn(tdim-1))
end
a.x_history = hcat(a.x_history, get_x(a) + reshape(inc,:,1));
end
"""
......
......@@ -32,13 +32,13 @@ function update_afterdeath_std!(world,x_death,p::Dict) where T
end
"""
function updateWorld_G!(world,tspan,p)
function updateWorld_G!(world,t,p)
Updating rule for gillepsie setting.
Returning dt drawn from an exponential distribution with parameter the total rates of events.
# Args
tspan is for now not used but might be used for dynamic landscape
t is for now not used but might be used for dynamic landscape
"""
function updateWorld_G!(world,p,update_rates!,tspan)
function updateWorld_G!(world,p,update_rates!,t)
# total update world
world_alive = skipmissing(world)
idx_world = collect(eachindex(world_alive))
......
......@@ -193,7 +193,7 @@ If specified p["dt_saving"] determines the time step to save the simulation. If
"""
function runWorld_store_G(p,world0)
# we store the value of world every 100 changes by default
tspan = zeros(1)
t = .0
i = 1;j=1;dt = 1.
N=length(world0);
tspanarray = zeros(1);
......@@ -204,7 +204,7 @@ function runWorld_store_G(p,world0)
worldall = hcat(worldall,Array{Missing}(missing,N,1))
# we instantiate C as the biggest size it can take
update_rates_std!(skipmissing(world0),p,0.)
while tspan[i]<p["tend"]
while t<p["tend"]
if dt < 0
throw("We obtained negative time step dt = $dt at event $i")
elseif count(ismissing,world0) == p["NMax"]
......@@ -214,8 +214,8 @@ function runWorld_store_G(p,world0)
@info "We have reached the maximum number of individuals allowed"
break
end
if tspan[end] - tspanarray[end] >= dt_saving
@info "saving world @ t = $(tspan[i])/ $(p["tend"])"
if t - tspanarray[end] >= dt_saving
@info "saving world @ t = $(t)/ $(p["tend"])"
j+=1;sw = size(worldall,2);
# we use <= because we save at the end of the wile loop
if sw <= j
......@@ -223,16 +223,16 @@ function runWorld_store_G(p,world0)
worldall = hcat(worldall,Array{Missing}(missing,N,sw))
end
worldall[1:Int(N - count(ismissing,world0)),j] .= copy.(collect(skipmissing(world0)));
push!(tspanarray,tspan[i])
push!(tspanarray,t)
end
dt = updateWorld_G!(world0,p,update_rates_std!,tspan)
push!(tspan, tspan[end] + dt)
dt = updateWorld_G!(world0,p,update_rates_std!,t)
t += dt
i += 1
end
# Saving last time step
j+=1
worldall[1:Int(N - count(ismissing,world0)),j] .= copy.(collect(skipmissing(world0)));
push!(tspanarray,tspan[i])
@info "simulation stopped at t=$(tspanarray[end])"
push!(tspanarray,t)
@info "simulation stopped at t=$(tspanarray[end]), after $(i) generations"
return worldall[:,1:j],tspanarray
end
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