Commit 44a95f12 authored by vboussange's avatar vboussange

fixed a bug for where parent traits were modified when giving birth, if traits...

fixed a bug for where parent traits were modified when giving birth, if traits were multidimensional.
Also modified get_inc with GraphSpace, which was resulting in a wrong behaviour when D \neq nothing ..
parent 0e2751fa
Pipeline #88804 failed with stage
in 16 minutes and 38 seconds
name = "ABMEv"
uuid = "837ac870-fb52-4b0c-9a0e-030f2f36f5ed"
authors = ["Victor Boussange "]
version = "3.2.0"
version = "3.3.0"
[deps]
Arpack = "7d9fca2a-8960-54d3-9f78-7d1dccf2cb97"
......
......@@ -19,7 +19,8 @@ mutable struct Agent{A<:Ancestors,R<:Rates,T<:Tuple,U,V} <: AbstractAgent{A,R}
b::V
end
import Base:eltype
# This definition of eltype is to be discussed
eltype(a::Agent{A,R,T,U,V}) where {A,R,T,U,V} = T
# infers position type and zeros
......@@ -29,12 +30,13 @@ function initpos(s::S) where {S<:AbstractSpacesTuple}
pos = []
for i in 1:length(Eltype)
if Dims[i] > 1
push!(pos,ones(eltype(Eltype[i]),Dims[i]))
push!(pos,ones(Eltype[i],Dims[i]))
else
pos = push!(pos,one(Eltype[i]))
end
end
Tuple{Eltype...},pos
_Type = eltype.(pos)
Tuple{_Type...},pos
end
# default initialiser
......@@ -60,9 +62,9 @@ $(SIGNATURES)
function Agent(s::S, pos::P;ancestors=false,rates=false) where {P<:Vector,S <: AbstractSpacesTuple}
T = eltype.(s)
for (i,p) in enumerate(pos)
if typeof(p) !== T[i]
if eltype(p) !== T[i]
try
p = convert(T[i],p)
p = convert.(T[i],p)
catch e
throw(ArgumentError("Position provided does not match with underlying space"))
end
......@@ -175,7 +177,7 @@ import Base.(+)
function _get_xinc(a::AbstractAgent,s::AbstractSpacesTuple,p::Dict,t::Number)
@unpack D,mu = p
_x = copy(get_x(a))
_x = deepcopy(get_x(a))
for (i,ss) in enumerate(s)
if length(mu[i]) > 1
mut = rand(eltype(mu[i]),ndims(ss)) .< mu[i]
......
......@@ -33,7 +33,7 @@ Base.getindex(s::Simulation,measure::String) = [agg[measure] for agg in s.df_agg
function Base.show(io::IO, s::Simulation{A,S,T}) where {A,S,T}
println(io, "Simulation with agents of type", A)
println(io, "Simulation with agents of type ", A)
end
# TODO: define two functions with signatures
# function add_entry!(s::Simulation{A,S,T,F},w::World) where {A,S,T,F<:Function}
......
......@@ -14,7 +14,7 @@ AbstractSpacesTuple = Tuple{Vararg{AbstractSpace}}
import Base:ndims,isfinite,eltype
Base.ndims(x::AbstractSpace{Dim,T,I}) where {Dim,T,I} = Dim
Base.isfinite(x::AbstractSpace{Dim,T,IsFinite{t}}) where {Dim,T,t} = t #not sure we need this
Base.eltype(::AbstractSpace{Dim,T,I}) where {Dim,T,I} = Dim > 1 ? Vector{T} : T
Base.eltype(::AbstractSpace{Dim,T,I}) where {Dim,T,I} = T
Base.ndims(ss::AbstractSpacesTuple) = length(ss)
Base.eltype(ss::AbstractSpacesTuple) where {Dim,T,I} = Tuple{eltype.(ss)...}
......@@ -97,7 +97,7 @@ function get_inc(x,D,s::DiscreteSegment{T}) where {T}
end
# normal dispersal kernel that gets truncated
function get_inc(x,D,s::GraphSpace{T}) where {T}
function get_inc(x,D::Number,s::GraphSpace{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
......@@ -107,7 +107,7 @@ function get_inc(x,D,s::GraphSpace{T}) where {T}
end
end
# short range dispersal kernel, jump to neighbour node
function get_inc(x,D,s::GraphSpace{T}) where {T}
function get_inc(x,D::Nothing,s::GraphSpace{T}) where {T}
return last(randomwalk(s.g,x,2)) - x
end
......
......@@ -17,6 +17,15 @@ function World(w::Vector{A},s::S,p::Dict,t::T=0.) where {A<:AbstractAgent,S<:Abs
throw(ArgumentError("eltype(w) should be equal to typeof(first(w)), which is not the case.\n
Try to generate the array of Agents in an other way"))
end
for _m in mu
if typeof(m) !<: Float
throw(ArgumentError("elements of mu should be of type AbstractFloat\n
to decide if mutations occur from a uniform probability law"))
end
end
length(mu) == ndims(s) ? nothing : "Dimension of parameter mu should correspond to dimension of underlying space"
length(D) == ndims(s) ? nothing : "Dimension of parameter D should correspond to dimension of underlying space"
World{A,S,T}(w,s,p,t)
end
......
......@@ -20,7 +20,7 @@ myspace2 = (mysegment,mycontinuoussegment,real2d)
@test ndims(real2d) 2
@test isfinite(mycontinuoussegment) true
@test typeof(myspace) <: AbstractSpacesTuple
@test eltype(myspace2) == Tuple{Int64,Float64,Vector{Float64}}
@test eltype(myspace2) == Tuple{Int64,Float64,Float64}
# increment on infinite spaces
@test ABMEv.get_inc(0.,myline) (0.)
......@@ -55,27 +55,56 @@ myspace2 = (mysegment,mycontinuoussegment,real2d)
@test prod([get_inc(1,nothing,mygraph) + 1 vertices(mygraph.g) for i in 1:30])
end
##### AGENTS #######
a1 = Agent(myspace)
a2 = Agent(myspace,ancestors = true)
a3 = Agent(myspace,[1,1,1.])
a4 = Agent(myspace2,[1,1.,[1.,1]],rates=true)
a5 = Agent(myspace2,ancestors=true)
@testset "Agent properties" begin
##### AGENTS #######
a1 = Agent(myspace)
a2 = Agent(myspace,ancestors = true)
a3 = Agent(myspace,[1,1,1.])
a4 = Agent(myspace2,[1,1.,[1.,1]],rates=true)
a5 = Agent(myspace2,ancestors=true)
# increment test
p_myspace = Dict("D"=>[1e-10,1e-10,1e-10],"mu" =>[1,1,1] )
p_myspace2 = Dict("D"=>[1,1,[1,1]],"mu" =>[1,1,[1,1]])
# basic test
@test typeof(a1) <: AbstractAgent
@test eltype(a1) == eltype(myspace)
@test eltype(a5) == eltype(myspace2)
@test typeof(a1) <: AbstractAgent
@test typeof(a1) == typeof(a3)
# increment test
p_myspace = Dict("D"=>[1,1,1],"mu" =>[1,1,1] )
p_myspace2 = Dict("D"=>[1,1,[1,1]],"mu" =>[1,1,[1,1]])
old_a1 = copy(a1)
@test !prod((get_x(old_a1) . get_x(increment_x!(a1,myspace,p_myspace,0.))))
old_a1 = deepcopy(a1)
@test prod((get_x(old_a1) . get_x(increment_x!(a1,myspace,p_myspace,0.))))
@test !prod((get_x(old_a1) .== get_x(increment_x!(a1,myspace,p_myspace,0.))))
@test nancestors(increment_x!(a2,myspace,p_myspace,2.)) > 1
@test !isnothing(increment_x!(a4,myspace2,p_myspace2,2.))
@test !isnothing(increment_x!(a5,myspace2,p_myspace2,2.))
@test typeof(ABMEv._get_xinc(a2,myspace,p_myspace,0.)) == typeof(get_x(a2))
# testing if get_xinc does not modify the value of a4
a4 = Agent(myspace2,[1,1.,[1.,1]],rates=true)
_xinc = ABMEv._get_xinc(a4,myspace2,p_myspace2,0.)
@test get_x(a4) == [1,1.,[1.,1]]
end
# @show ABMEv._get_xinc(a4,myspace2,p_myspace2,0.)
@testset "Multidimensional spaces" begin
myspace = (RealSpace{10,Float64}(),)
a1 = Agent(myspace)
p_myspace = Dict("D"=>[1e0],"mu" =>[1.] )
increment_x!(a1,myspace,p_myspace,0.)
@test !(get_x(a1)[1] == ones(10))
# testing independence
myspace = (RealSpace{1000,Float64}(),)
a1 = Agent(myspace)
p_myspace = Dict("D"=>[1e0],"mu" =>[fill(.1,1000)] )
increment_x!(a1,myspace,p_myspace,0.)
@test isapprox(sum(get_x(a1)[1] .== ones(1000)),900,atol = 30)
myspace = (RealSpace{1000,Float64}(),)
a1 = Agent(myspace)
p_myspace = Dict("D"=>[rand(1000)],"mu" =>[fill(.1,1000)] )
increment_x!(a1,myspace,p_myspace,0.)
@test isapprox(sum(get_x(a1)[1] .== ones(1000)),900,atol = 30)
end
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