To receive notifications about scheduled maintenance, please subscribe to the mailing-list gitlab-operations@sympa.ethz.ch. You can subscribe to the mailing-list at https://sympa.ethz.ch

Commit 3fc15f06 authored by Victor's avatar Victor
Browse files

Changing metrics to adapt multidimensional subspace

parent f169e794
......@@ -52,7 +52,7 @@ If trait > 0, returns the covariance matrix, with first row geotrait and second
"""
function var(world::World;trait=1)
xarray = get_x(world,trait)
xarray = Float64.(get_x(world,trait))
return var(xarray,dims=1,corrected=false)
end
"""
......@@ -94,12 +94,12 @@ Mean of the local variance of `trait` per patch. If trait=0, we get the geotrait
# Arguments
"""
function get_alpha_div(world::World,trait=1)
xarray = get_xarray(world,true)
g = groupby(x->x[1],collect(eachrow(xarray)))
g = groupby(a->a[1],agents(world))
if trait == 0
return mean([var(Float64.([x[end] for x in xp]),corrected=false) for xp in values(g)])
return mean([var(Float64.(get_geo(World(subw,space(world),parameters(world)))),corrected=false) for subw in values(g)])
else
return mean([var(Float64.([x[trait] for x in xp]),corrected=false) for xp in values(g)])
# here the second mean is here when subspace is multidimensional
return mean([mean(var(Float64.(get_x(World(subw,space(world),parameters(world)),trait)),corrected=false)) for subw in values(g)])
end
end
......@@ -109,13 +109,12 @@ Variance of the mean of `trait` per patch
# Arguments
"""
function get_beta_div(world::World,trait=1)
xarray = get_xarray(world,true)
g = groupby(x->x[1],collect(eachrow(xarray)))
g = groupby(a->a[1],agents(world))
if trait == 0
# need to convert to Float64, otherwise infinite variance
sbar_i = [mean(Float64.([x[end] for x in xp])) for xp in values(g)]
sbar_i = [mean(Float64.(get_geo(World(subw,space(world),parameters(world))))) for subw in values(g)]
else
sbar_i = [mean(Float64.([x[trait] for x in xp])) for xp in values(g)]
sbar_i = [mean(Float64.(get_x(World(subw,space(world),parameters(world)),trait))) for subw in values(g)]
end
return var(sbar_i,corrected=false)
end
......
......@@ -94,10 +94,10 @@ import DataFrames.groupby
function groupby(f, list::Array)
returns a dictionary that group `list` elements by value of function `f`
"""
groupby(f, list::Array) = begin
groupby(f, list::Array{T}) where {T}= begin
groups = Dict()
for v in list
push!(get!(groups, f(v), []), v)
push!(get!(groups, f(v), T[]), v)
end
groups
end
......@@ -58,13 +58,21 @@ If `trait = 0` , we return the geotrait.
# Warning
This works only for linear subspace, indexed with a single value
"""
get_x(w::World,trait) = hcat(collect.(getindex.(agents(w),trait))...)'
function get_x(w::World,trait)
if ndims(space(w)[trait]) > 1
hcat(collect.(getindex.(agents(w),trait))...)'
else
collect(getindex.(agents(w),trait))
end
end
"""
$(SIGNATURES)
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 column.
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}`.
# Warning
It does not work with subspace where ndims(subspace) > 1.
"""
function get_xarray(world::World,geotrait::Bool=false)
xarray = get_x(world,Colon())
......
......@@ -64,3 +64,19 @@ end
end
# TODO needs to test hamming distance
# testing for real space of dimension d>1
multispace = (DiscreteSegment{Int8}(1,9),RealSpace{3,Float64}(),)
K0 = 10000;
multia = [Agent(multispace, (rand(Int8(1):Int8(10)),tuple(randn(3)...),),ancestors=true) for i in 1:K0]
D = (Int8(1),tuple(fill(1.,3)...),)
mu = [1]
NMax = 1000
multip = Dict{String,Any}();@pack! multip = D,mu,NMax
multiw = World(multia,multispace,multip)
a = multia[10]
inc = get_inc(1,D[1],multispace[2])
@test inc[1] != inc[2] # checking that we have independent increments in each direction
@test isapprox(mean(var(multiw,trait = 2)),1.,atol=1e-2)
@test isapprox(get_alpha_div(multiw,2),1.,atol = 1e-2)
@test isapprox(get_beta_div(multiw,2),0.,atol = 1e-2)
......@@ -11,42 +11,44 @@ mycontinuoussegment = ContinuousSegment(-1.,1.)
myspace = (mysegment,mygraph,myline)
myspace2 = (mysegment,mycontinuoussegment,real2d)
# checking essential properties of spaces
@test isfinite(mysegment) true
@test isfinite(mygraph) true
@test isfinite(myline) false
@test ndims(real2d) 2
@test isfinite(mycontinuoussegment) true
@test typeof(myspace) <: AbstractSpacesTuple
@test eltype(myspace2) == Tuple{Int64,Float64,Tuple{Float64,Float64}}
@testset "Space properties" begin
# checking essential properties of spaces
@test isfinite(mysegment) true
@test isfinite(mygraph) true
@test isfinite(myline) false
@test ndims(real2d) 2
@test isfinite(mycontinuoussegment) true
@test typeof(myspace) <: AbstractSpacesTuple
@test eltype(myspace2) == Tuple{Int64,Float64,Tuple{Float64,Float64}}
# increment on infinite spaces
@test get_inc(0.,myline) (0.)
@test !(get_inc(1.,myline) 0.)
@test !(get_inc(1,1.,myline) 0.)
# increment on infinite spaces
@test ABMEv._get_inc(0.,myline) (0.)
@test !(ABMEv._get_inc(1.,myline) 0.)
@test !(get_inc(1,1.,myline) 0.)
@test typeof(get_inc([1.,0.],real2d)) == Tuple{Float64,Float64}
@test typeof(get_inc([1.,0.],[1.,0.],real2d)) == Tuple{Float64,Float64}
@test typeof(get_inc([1.],real2d)) == Tuple{Float64,Float64}
@test typeof(get_inc(1.,real2d)) == Tuple{Float64,Float64}
get_inc([1.],real2d)
ABMEv.initpos(myspace2)
@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}
# ABMEv._get_inc([1.],real2d)
# ABMEv.initpos(myspace2)
# increment on finite spaces
# checking if reflection works
@testset "Reflection" begin
@test mysegment.s - eps() < get_inc(5.,100.,mysegment) + 5. < mysegment.e + eps()
@test mycontinuoussegment.s < get_inc(0.,100.,mycontinuoussegment) < mycontinuoussegment.e
mysegment2 = DiscreteSegment(-1,1)
@test ABMEv._reflect1D(0.,2.0,mysegment2) .0
@test ABMEv._reflect1D(0.,-2.0,mysegment2) .0
@test ABMEv._reflect1D(0.,4.0,mysegment2) .0
@test ABMEv._reflect1D(0.,1.1,mysegment2) 1 - .1
end
#checking if graph works
@test prod([get_inc(1,10,mygraph) + 1 vertices(mygraph.g) for i in 1:30])
# increment on finite spaces
# checking if reflection works
@testset "Reflection" begin
@test mysegment.s - eps() < get_inc(5.,100.,mysegment) + 5. < mysegment.e + eps()
@test mycontinuoussegment.s < get_inc(0.,100.,mycontinuoussegment) < mycontinuoussegment.e
mysegment2 = DiscreteSegment(-1,1)
@test ABMEv._reflect1D(0.,2.0,mysegment2) .0
@test ABMEv._reflect1D(0.,-2.0,mysegment2) .0
@test ABMEv._reflect1D(0.,4.0,mysegment2) .0
@test ABMEv._reflect1D(0.,1.1,mysegment2) 1 - .1
end
#checking if graph works
@test prod([get_inc(1,10,mygraph) + 1 vertices(mygraph.g) for i in 1:30])
end
##### AGENTS #######
a1 = Agent(myspace)
......@@ -55,17 +57,19 @@ a3 = Agent(myspace,(1,1,1.))
a4 = Agent(myspace2,(1,1,(1.,1)),rates=true)
a5 = Agent(myspace2,ancestors=true)
# basic test
@test typeof(a1) <: AbstractAgent
@test eltype(a1) == eltype(myspace)
@test eltype(a5) == eltype(myspace2)
@test typeof(a1) <: AbstractAgent
@testset "Agent properties" begin
# basic test
@test typeof(a1) <: AbstractAgent
@test eltype(a1) == eltype(myspace)
@test eltype(a5) == eltype(myspace2)
@test typeof(a1) <: AbstractAgent
# increment test
p_myspace = Dict("D"=>[1,1,1],"mu" =>[1,1,1] )
p_myspace2 = Dict("D"=>[1,1,1],"mu" =>[1,1,1])
old_a1 = copy(a1)
@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.))
# increment test
p_myspace = Dict("D"=>[1,1,1],"mu" =>[1,1,1] )
p_myspace2 = Dict("D"=>[1,1,1],"mu" =>[1,1,1])
old_a1 = copy(a1)
@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.))
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