Commit 44ec2c31 authored by Victor's avatar Victor
Browse files

modifying some documentation

parent 02bc3edb
......@@ -5,8 +5,10 @@ module ABMEv
using LightGraphs
include("ABMEv_Agent.jl")
include("ABMEv_Space.jl")
include("ABMEv_WF.jl")
include("ABMEv_Gillepsie.jl")
include("ABMEv_CFM.jl")
include("ABMEv_runworld.jl")
include("ABMEv_metrics.jl")
include("ABMEv_plot.jl")
......
......@@ -24,29 +24,38 @@ import Base.copy
copy(a::Agent{T,U}) where {T,U} = Agent{T,U}(copy(a.x_history),copy(a.t_history),copy(a.d),copy(a.b))
copy(m::Missing) = missing
#####################
###Agent accessors###
#####################
"""
function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.)
Returns an array of type Array{Union{Missing,Agent}} initialised with normal distribution.
Only relevant for Gillepsie algorithm as of now.
get_x(a::Agent)
Returns trait i of the agent
"""
function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.)
typeof(spread) <: Array ? spread = spread[:] : nothing;
typeof(offset) <: Array ? offset = offset[:] : nothing;
agent0 = [Agent( spread .* randn(length(spread)) .+ offset) for i in 1:nagents]
world0 = vcat(agent0[:],repeat([missing],Int(p["NMax"] - nagents)))
return world0
end
# returns trait i of the agent
get_x(a::Agent) = a.x_history[:,end]
"""
function get_geo(a::Agent{U,T},t::Number) where {U,T}
Returns geotrait of agent `a` at time `t`
"""
function get_geo(a::Agent{U,T},t::Number) where {U,T}
tarray = vcat(a.t_history[2:end],convert(T,t))
tarray .-= a.t_history
return sum(get_xhist(a,1) .* tarray)
end
# This method can acces geotrait, while the second not
"""
get_x(a::Agent,t::Number,i::Integer)
Returns trait `i` of the agent.
Geotrait corresponds to dimension `i=0`.
"""
get_x(a::Agent,t::Number,i::Integer) = i > 0 ? a.x_history[Int(i),end] : get_geo(a,t)
get_x(a::Agent,i::Integer) = a.x_history[Int(i),end]
"""
get_t(a::Agent) = a.t_history[end]
Get time when agent born.
"""
get_t(a::Agent) = a.t_history[end]
get_xhist(a::Agent,i::Number) = a.x_history[Int(i),:]
get_xhist(a::Agent) = a.x_history
......@@ -57,23 +66,34 @@ get_fitness(a::Agent) = a.b - a.d
get_dim(a::Agent) = size(a.x_history,1)
get_nancestors(a::Agent) = size(a.x_history,2)
#####################
###World accessors###
#####################
"""
get_x(world::Array{T},trait::Integer) where {T <: Agent}
Get x of world without geotrait.
"""
get_x(world::Array{T},trait::Integer) where {T <: Agent} = trait > 0 ? reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2)) : throw(ErrorException("Not the right method, need `t` as an argument"))
"""
get_x(world::Array{T},t::Number,trait::Integer) where {T <: Agent}
Returns trait of every agents of world in the form of an array which dimensions corresponds to the input.
If trait = 0 , we return the geotrait.
If `trait = 0` , we return the geotrait.
"""
get_x(world::Array{T},t::Number,trait::Integer) where {T <: Agent} = trait > 0 ? reshape(hcat(get_x.(world,trait)),size(world,1),size(world,2)) : reshape(hcat(get_geo.(world,t)),size(world,1),size(world,2))
"""
get_xarray(world::Array{Agent,1})
function get_xarray(world::Array{T,1}) where {T <: Agent}
Returns every traits of every agents of world in the form of an array
"""
function get_xarray(world::Array{T,1}) where {T <: Agent}
return hcat(get_x.(world)...)
end
"""
function get_xarray(world::Array{T,1},t::Number,geotrait::Bool=false) where {T <: Agent}
Returns every traits of every agents of `world` in the form **of a one dimensional array** (in contrast to `get_x`).
If `geotrait=true` the geotrait is also added to the set of trait, in the last line.
If you do not want to specify `t` (only useful for geotrait), it is also possible to use `get_xarray(world::Array{T,1}) where {T <: Agent}`.
"""
function get_xarray(world::Array{T,1},t::Number,geotrait::Bool=false) where {T <: Agent}
xarray = hcat(get_x.(world)...)
if geotrait
......@@ -148,62 +168,6 @@ function world2df(world::Array{T,1},t::Number,geotrait = false) where {T <: Agen
return dfw
end
"""
function increment_x!(a::Agent{StdAgent,U},t::U,p::Dict) where U
This function increments agent by random numbers specified in p
ONLY FOR CONTINUOUS DOMAINS
"""
function increment_x!(a::Agent{StdAgent,U},t,p::Dict) where U
tdim = length(p["D"])
reflected = haskey(p,"reflected") ? p["reflected"] : false
if reflected
inc = [get_inc_reflected(get_x(a,1),p["D"][1] *randn())]
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));
push!(a.t_history,t)
end
"""
function increment_x!(a::Agent{MixedAgent,U},t::U,p::Dict) where U
This function increments first trait of agent with integer values, that are automatically reflected between 1 and p["nodes"].
Other traits are incremented as usual.
TODO : make it work for a graph type landscape, where domain is not a line anymore.
"""
function increment_x!(a::Agent{MixedAgent,U},t,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));
push!(a.t_history,t)
end
"""
get_inc_reflected(x::Number,inc::Number,s=-1,e=1)
Here we increment the trajectory of trait 1 such that it follows a reflected brownian motion (1D)
"""
function get_inc_reflected(x::Number,inc::Number,s=-1,e=1)
if x + inc < s
inc = 2 * ( s - x ) - inc
elseif x + inc > e
inc = 2 * ( e - x ) - inc
else
return inc
end
get_inc_reflected(x,inc,s,e)
end
"""
function tin(t::Number,a::Number,b::Number)
if t in [a,b) returns 1. else returns 0
......
# CFM = Champagnat Ferriere Meleard
# This contains all methods for algorithm described in article
# DOI : 10.1016/j.tpb.2005.10.004
......@@ -2,7 +2,7 @@
"""
give_birth(a::Agent,p)
function give_birth(a::Agent,t,p::Dict)
Used for Gillepsie setting
"""
function give_birth(a::Agent,t,p::Dict)
......@@ -77,3 +77,17 @@ Get rid of missing value in `world`
"""
#TODO : put some type specs here
clean_world(world) = collect(skipmissing(world))
"""
function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.)
Returns an array `world0` of size `p["NMax"]`, with `nagents` ancestors agents.
Those agents have traits distributed according to the normal distribution with mean `offset` and standard deviation `spread`.
The dimension of the domain is determined by the size of the array `spread`.
"""
function new_world_G(nagents::Int,p::Dict; spread = 1., offset = 0.)
typeof(spread) <: Array ? spread = spread[:] : nothing;
typeof(offset) <: Array ? offset = offset[:] : nothing;
agent0 = [Agent( spread .* randn(length(spread)) .+ offset) for i in 1:nagents]
world0 = vcat(agent0[:],repeat([missing],Int(p["NMax"] - nagents)))
return world0
end
"""
function increment_x!(a::Agent{StdAgent,U},t::U,p::Dict) where U
This function increments agent by random numbers specified in p
ONLY FOR CONTINUOUS DOMAINS
"""
function increment_x!(a::Agent{StdAgent,U},t,p::Dict) where U
tdim = length(p["D"])
reflected = haskey(p,"reflected") ? p["reflected"] : false
if reflected
inc = [get_inc_reflected(get_x(a,1),p["D"][1] *randn())]
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));
push!(a.t_history,t)
end
"""
function increment_x!(a::Agent{MixedAgent,U},t::U,p::Dict) where U
This function increments first trait of agent with integer values, that are automatically reflected between 1 and p["nodes"].
Other traits are incremented as usual.
TODO : make it work for a graph type landscape, where domain is not a line anymore.
"""
function increment_x!(a::Agent{MixedAgent,U},t,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));
push!(a.t_history,t)
end
"""
function get_inc_reflected(x::Number,inc::Number,s=-1,e=1)
Here we increment the trajectory of trait 1 such that it follows a reflected brownian motion (1D)
"""
function get_inc_reflected(x::Number,inc::Number,s=-1,e=1)
if x + inc < s
inc = 2 * ( s - x ) - inc
elseif x + inc > e
inc = 2 * ( e - x ) - inc
else
return inc
end
get_inc_reflected(x,inc,s,e)
end
......@@ -187,9 +187,12 @@ function runWorld_store_WF(p,world0;mode="std")
end
"""
function runWorld_store_G(p,world0)
Gillepsie process. Returns a tuple worldall,tspanarray
If specified p["dt_saving"] determines the time step to save the simulation. If not only last time step is saved
Gillepsie process.
- returns a tuple 'worldall, tspanarray'
- `worldall` stores the world every `p["dt_saving"]` time steps.
If `p["dt_saving"]` not specified, it returns an array with two columns,
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.
"""
function runWorld_store_G(p,world0)
# we store the value of world every 100 changes by default
......
......@@ -36,7 +36,7 @@ p = Dict("mu" => [1. 1.],"D" => [0. 0.], "nodes" =>10 )
@testset "Isolation by history - hamming distance" begin
a1 = Agent{StdAgent,Float64}([1 2 3],[0,1,4],0.,1.)
a2 = Agent{StdAgent,Float64}([1 10 3 10],[0,3,4,5],0.,1.)
@test get_dist_hist(a1,a2,isnotequal_dist,1) 3.0
@test get_dist_hist(a1,a2,(x,y)->y!=x,1) 3.0
end
end
......
using DataFrames
""" function test_interpol()
function testing `interpolate_df(df,xlab,ylab,zlab)`
"""
function test_interpol()
df = DataFrame(zeros(100^2,3),[:x,:y,:z])
x = cumsum(rand(100));y = cumsum(rand(100))
for (i,t) in enumerate(Iterators.product(x,y))
df[ i, [:x,:y]] .= t
df[i,:z] = sin(sum(t))
end
df = df[shuffle(1:100^2),:]
fun = interpolate_df(df,:x,:y,:z)
idx = 9999
return (fun.itp(df.x[idx],df.y[idx]) df.z[idx])
end
@testset "Interpolations" begin
@test test_interpol()
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