Commit 1af8ceb3 authored by Victor Boussange's avatar Victor Boussange
Browse files

added utils file

Agent file: work in progress, we should remove competition kernel and carrying capacity
Gillepsie: modified updates to use kernel that we now pass as arguments through the parameter dictionary p
runworld: for now we only modified std update but this should be done for all the project
parent 47934404
......@@ -10,6 +10,7 @@ module ABMEv
include("ABMEv_runworld.jl")
include("ABMEv_metrics.jl")
include("ABMEv_plot.jl")
include("ABMEv_utils.jl")
@reexport using Distributions
......@@ -20,4 +21,5 @@ module ABMEv
export copy,runWorld_store_WF,runWorld_store_G #,runWorld_G!,runWorld_WF!,
export H_discrete,findclusters,var,covgeo,hamming
export update_afterbirth_std!,update_afterdeath_std!
export generalised_gaussian,gaussian
end
......@@ -92,11 +92,16 @@ end
# need to make sure that this is working correctly
"""
α(a1::Array{Float64},a2::Array{Float64},n_alpha::Float64,sigma_a::Array{Float64})
Gaussian competition kernel
Generalised gaussian competition kernel
"""
function α(a1::Array{Float64},a2::Array{Float64},n_alpha::Float64,sigma_a::Array{Float64})
return exp( -.5 sum(sum((a1 .- a2).^n_alpha,dims=2)./ sigma_a[:].^n_alpha))
return exp( -.5* sum(sum((a1 .- a2).^n_alpha,dims=2)./ sigma_a[:].^n_alpha))
end
"""
α(a1::Array{Float64},a2::Array{Float64},n_alpha::Float64,sigma_a::Array{Float64})
Default Gaussian competition kernel
"""
α(a1::Array{Float64},a2::Array{Float64},sigma_a::Array{Float64}) = α(a1,a2,2,sigma_a)
"""
K(x::Array{Float64},K0::Float64,μ::Array{Float64},sigma_K::Array{Float64})
......
......@@ -14,24 +14,25 @@ end
function update_afterbirth_std!(world,C,idx::Int,p::Dict) where T
traits = get_x.(world)
# updating competition only the two columns corresponding to agent idx
α = p["alpha"];K=p["K"]
for i in 1:length(traits)
C[i,idx] = α(traits[i],traits[idx],p["n_alpha"],p["sigma_a"])
C[i,idx] = α(traits[i],traits[idx])
C[idx,i] = C[i,idx]
end
# updating death rate only the two columns corresponding to agent idx
for (i,a) in enumerate(world)
a.d += C[i,idx] / p["K0"]
a.d += C[i,idx]
end
# Now updating new agent
world[idx].d = sum(C[idx,:]) / p["K0"]
world[idx].b = K(traits[idx][:,end],1.,p["n_K"],p["sigma_K"])
world[idx].d = sum(C[idx,:])
world[idx].b = K(traits[idx][:,end],1.)
end
function update_afterdeath_std!(world,C,idx::Int,p::Dict) where T
traits = get_x.(world)
# updating death rate only the two columns corresponding to agent idx
for (i,a) in enumerate(world)
a.d -= C[i,idx] / p["K0"]
a.d -= C[i,idx]
end
# updating competition only the two columns corresponding to agent idx
for i in 1:length(traits)
......
# for now we consider that competition is local within an array
"""
update_rates_std!(world,C,p::Dict,t::Float64)
This standard updates takes
- competition kernels of the form α(x,y) and
- carrying capacity of the form K(x)
"""
function update_rates_std!(world,C,p::Dict,t::Float64)
α = p["alpha"];K=p["K"];
# Competition matrix
traits = get_x.(world)
# traits = get_xhist.(world)
......@@ -9,7 +16,7 @@ function update_rates_std!(world,C,p::Dict,t::Float64)
# Here you should do a shared array to compute in parallel
@sync @distributed for i in 1:(N-1)
for j in i+1:N
C[i,j] = α(traits[i],traits[j],p["n_alpha"],p["sigma_a"])
C[i,j] = α(traits[i],traits[j])
C[j,i] = C[i,j]
end
end
......@@ -17,9 +24,9 @@ function update_rates_std!(world,C,p::Dict,t::Float64)
for (i,a) in enumerate(world)
# we only update death rate
# this could be imptrove since \alpha is symmetric, by using a symmetric matrix
a.d = sum(C[i,:]) / p["K0"]
a.d = sum(C[i,:])
# /!| not ideal to assign at every time step the birth rate that is constant
a.b = K(traits[i][:,end],1.,p["n_K"],p["sigma_K"])
a.b = K(traits[i][:,end])
end
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