Commit a6dfb5ab authored by Victor's avatar Victor

no message

parent 9a5e454e
Pipeline #76515 passed with stage
in 21 minutes and 32 seconds
......@@ -23,7 +23,7 @@ module ABMEv
@reexport using Distributions, DataFrames
export GraphSpace,ContinuousSegment,DiscreteSegment,RealSpace,NaturalSpace,
AbstractSpacesTuple,get_inc
AbstractSpacesTuple,get_inc,DynGraphSpace
export update_rates!
export AbstractAgent,Agent,get_fitness,get_x,get_t,get_dim,
nancestors,get_xarray,get_xhist,
......
......@@ -177,7 +177,7 @@ function _get_xinc(a::AbstractAgent,s::AbstractSpacesTuple,p::Dict,t::Number)
inc = zero(_x)
for (i,ss) in enumerate(s)
if rand() < mu[i]
inc[i] = get_inc(_x[i],D[i],ss)
inc[i] = get_inc(_x[i],D[i],ss,t)
end
end
tuple((_x .+ inc)...)
......
......@@ -5,7 +5,8 @@ abstract type IsFinite{T} end
#ife stands for is finite
"""
$(TYPEDEF)
`Dim` is the dimension of the space, `T` is the element type,
`Dim` is the dimension of the space,
`T` is the element type,
`I` to indicate finiteness
"""
abstract type AbstractSpace{Dim,T,I} end
......@@ -19,14 +20,17 @@ Base.eltype(ss::AbstractSpacesTuple) where {Dim,T,I} = Tuple{eltype.(ss)...}
SpaceType=Union{Nothing, AbstractSpace} # not sure what is this used for
# Static spaces
abstract type AbstractStatSpace{Dim,T,I} <: AbstractSpace{Dim,T,I} end
"""
$(TYPEDEF)
"""
struct GraphSpace{T} <: AbstractSpace{1,T,IsFinite{true}}
struct GraphSpace{T} <: AbstractStatSpace{1,T,IsFinite{true}}
g::AbstractGraph{T}
end
abstract type AbstractSegment{T<:Number} <: AbstractSpace{1,T,IsFinite{true}} end
abstract type AbstractSegment{T<:Number} <: AbstractStatSpace{1,T,IsFinite{true}} end
"""
$(TYPEDEF)
......@@ -46,16 +50,26 @@ end
"""
$(TYPEDEF)
A real space with dimension N and type T
"""
struct RealSpace{N,T} <: AbstractSpace{N,T,IsFinite{false}} end
struct NaturalSpace{N,T} <: AbstractSpace{N,T,IsFinite{false}} end
struct RealSpace{N,T} <: AbstractStatSpace{N,T,IsFinite{false}} end
"""
$(TYPEDEF)
A natural space with dimension N and type T
"""
struct NaturalSpace{N,T} <: AbstractStatSpace{N,T,IsFinite{false}} end
## Increments - specialised function
# TODO: find a way to put a type on D in get_inc
function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:AbstractFloat,I<:IsFinite{false}}
"""
$(SIGNATURES)
Returns increment corresponding to space `s`
"""
get_inc(x,D,s::AbstractStatSpace,t) = get_inc(x,D,s) # this is defined to skip representation of t for following specialised methods
get_inc(x,D,s::AbstractSpace{Dim,T,I}) where {Dim,T,I<:IsFinite{false}} = get_inc(D,s) # This is defined to skip representation of x for spaces which do not use reflections.
function get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:AbstractFloat,I<:IsFinite{false}}
if Dim > 1
return Tuple(D .* randn(T,Dim))
else
......@@ -63,7 +77,7 @@ function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:AbstractFloat,I<:Is
end
end
function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:Integer,I<:IsFinite{false}}
function get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:Integer,I<:IsFinite{false}}
if Dim > 1
return Tuple(round.(T,D .*randn(Float32,Dim)))
else
......@@ -71,12 +85,6 @@ function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:Integer,I<:IsFinite
end
end
"""
$(SIGNATURES)
Returns increment corresponding to space `s`
"""
get_inc(x,D,s::AbstractSpace{Dim,T,I}) where {Dim,T,I<:IsFinite{false}} = _get_inc(D,s)
#TODO: there is probably a better way of dealing with those two functions
function get_inc(x,D,s::ContinuousSegment{T}) where {T}
inc = D * randn(T)
......@@ -98,6 +106,41 @@ function get_inc(x,D,s::GraphSpace{T}) where {T}
end
end
## Dynamic spaces
abstract type AbstractDynSpace{Dim,T<:Number} <: AbstractSpace{Dim,T,IsFinite{true}} end
"""
$(TYPEDEF)
A dynamic graph space.
Function `f(t)` takes as argument time, and returns the index of the graph to pick at time `t` from array `g`
"""
struct DynGraphSpace{T<:Number} <: AbstractDynSpace{1,T}
g::Vector{AbstractGraph{T}}
f #update function
end
# This is surely not elegant, but we have not found an other way to do it yet
function DynGraphSpace(g::Array{A},f) where A <: AbstractGraph
DynGraphSpace{eltype(g[1])}(g,f)
end
"""
$SIGNATURES
Returns the graph correseponding to `d::DynGraphSpace` at time `t`
"""
get_graph(d::DynGraphSpace,t) = d.g[d.f(t)]
## Increments - specialised functions
function get_inc(x,D,d::DynGraphSpace{T},t) where {T}
niter = round(Int,abs(D*randn())) + 1
# here we add +1 since randomwalk(s.g,x,niter) returns x
if niter > 0
return last(randomwalk(get_graph(d,t),x,niter)) - x
else
return 0
end
end
"""
$(SIGNATURES)
Here we increment the trajectory of trait 1 such that it follows a reflected brownian motion (1D)
......
......@@ -15,12 +15,12 @@ d(X,Y,t) = gaussian(X[1],Y[1],sigma_a)/gaussian(X[1],0.,sigma_K)/K0
D = (1e-2,)
mu = [.1]
NMax = 2000
tend = 2000
tend = 1500
# Cbar = b([0],0.)/K0 + d([0],[0],0.)
dm = d([0],[0],0.);bm = 1.
p = Dict{String,Any}();@pack! p = d,b,D,mu,NMax,dm,bm
myagents = [Agent(myspace,(-.01 + 1e-2 * randn(),),ancestors=false,rates=false) for i in 1:K0]
myagents = [Agent(myspace,(1e-2 * randn(),),ancestors=false,rates=false) for i in 1:K0]
w0 = World(myagents,myspace,p,0.)
w1 = copy(w0)
@info "Running simulation with CFM algorithm"
......
cd(@__DIR__)
using Random
Random.seed!(0)
using LightGraphs
using Test
using Revise,ABMEv
using UnPack,JLD2
g1 = LightGraphs.grid([9,1])
g2 = SimpleGraph(9)
# This is the function to implement DynGraphSpace.
# Note that it returns indices
g = [g1,g2]
function periodic_update_graph(T,t)
if sin(t/T*2*π) > 0
1
else
2
end
end
update_g(t) = periodic_update_graph(10,t)
dyng = DynGraphSpace(g,update_g)
## testing atomic methods
@test last(randomwalk(ABMEv.get_graph(dyng,16.),1,10)) 1
@test !(last(randomwalk(ABMEv.get_graph(dyng,1.),1,10)) 1)
## simulations
myspace = (dyng,)
sigma_K = .9;
sigma_a = .7;
K0 = 1000;
b(X,t) = gaussian(X[1],0.,sigma_K)
d(X,Y,t) = gaussian(X[1],Y[1],sigma_a)/K0
D = (1e-2,)
mu = [.1]
NMax = 10000
tend = 1.5
p = Dict{String,Any}();@pack! p = d,b,D,mu,NMax
myagents = [Agent(myspace,(1,),ancestors=true,rates=true) for i in 1:K0]
w0 = World(myagents,myspace,p,0.)
w1 = copy(w0)
@info "Running simulation with Gillepsie algorithm"
sim = run!(w1,Gillepsie(),tend)
@test !(isnothing(sim))
......@@ -10,4 +10,5 @@ using ABMEv, Test, JLD2,Random
include("space_agent.jl")
include("world.jl")
include("utils.jl")
include("dyn_land.jl")
end
......@@ -23,17 +23,17 @@ myspace2 = (mysegment,mycontinuoussegment,real2d)
@test eltype(myspace2) == Tuple{Int64,Float64,Tuple{Float64,Float64}}
# increment on infinite spaces
@test ABMEv._get_inc(0.,myline) (0.)
@test ABMEv._get_inc(0.,mydiscreteline) (0.)
@test !(ABMEv._get_inc(1.,myline) 0.)
@test ABMEv.get_inc(0.,myline) (0.)
@test ABMEv.get_inc(0.,mydiscreteline) (0.)
@test !(ABMEv.get_inc(1.,myline) 0.)
@test !(get_inc(1,1.,myline) 0.)
@test !(get_inc(1,1.,mydiscreteline) 0.)
@test typeof(ABMEv._get_inc([1.,0.],real2d)) == Tuple{Float64,Float64}
@test typeof(ABMEv.get_inc([1.,0.],real2d)) == Tuple{Float64,Float64}
@test typeof(get_inc([1.,0.],[1.,0.],real2d)) == Tuple{Float64,Float64}
@test typeof(ABMEv._get_inc([1.],real2d)) == Tuple{Float64,Float64}
@test typeof(ABMEv._get_inc(1.,real2d)) == Tuple{Float64,Float64}
@test typeof(ABMEv.get_inc([1.],real2d)) == Tuple{Float64,Float64}
@test typeof(ABMEv.get_inc(1.,real2d)) == Tuple{Float64,Float64}
# ABMEv._get_inc([1.],real2d)
# ABMEv.initpos(myspace2)
......
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