space_agent.jl 4.16 KB
Newer Older
Victor's avatar
Victor committed
1
using LightGraphs
2
3
4
using Test
using Revise,ABMEv

Victor's avatar
Victor committed
5
##### SPACES #####
Victor's avatar
Victor committed
6
7
mysegment = DiscreteSegment(1,10)
mygraph = GraphSpace(SimpleGraph(10,10))
8
9
real2d = RealSpace{2,Float64}()
myline = RealSpace{1,Float16}()
Victor's avatar
Victor committed
10
mydiscreteline = NaturalSpace{1,Int8}()
11
12
13
mycontinuoussegment = ContinuousSegment(-1.,1.)
myspace = (mysegment,mygraph,myline)
myspace2 = (mysegment,mycontinuoussegment,real2d)
Victor's avatar
Victor committed
14

15
16
17
18
19
20
21
22
@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
23
    @test eltype(myspace2) == Tuple{Int64,Float64,Float64}
24

25
    # increment on infinite spaces
Victor's avatar
Victor committed
26
27
28
    @test ABMEv.get_inc(0.,myline)  (0.)
    @test ABMEv.get_inc(0.,mydiscreteline)  (0.)
    @test !(ABMEv.get_inc(1.,myline)  0.)
29
    @test !(get_inc(1,1.,myline)  0.)
30
    # @test !(get_inc(1,1.,mydiscreteline) ≈ 0.)
Victor's avatar
Victor committed
31

32

33
34
35
36
    @test typeof(ABMEv.get_inc([1.,0.],real2d)) == Vector{Float64}
    @test typeof(get_inc([1.,0.],[1.,0.],real2d)) == Vector{Float64}
    @test typeof(ABMEv.get_inc([1.],real2d)) == Vector{Float64}
    @test typeof(ABMEv.get_inc(1.,real2d)) == Vector{Float64}
37
38
    # ABMEv._get_inc([1.],real2d)
    # ABMEv.initpos(myspace2)
39

Victor's avatar
Victor committed
40

41
42
43
44
45
46
47
48
49
50
51
    # 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
52

53
54
    #checking if graph works
    @test prod([get_inc(1,10,mygraph) + 1  vertices(mygraph.g) for i in 1:30])
55
    @test prod([get_inc(1,nothing,mygraph) + 1  vertices(mygraph.g) for i in 1:30])
56
end
Victor's avatar
Victor committed
57

58
@testset "Agent properties" begin
59
60
61
62
63
64
65
    ##### 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
Victor's avatar
Victor committed
66
    p_myspace = Dict("D"=>Union{Float64,Float16}[1e-10,1e-10,Float16(1e-10)],"mu" =>Union{Float64,Float16}[1.,1.,Float16(1)] )
67
68
    p_myspace2 = Dict("D"=>[1,1,[1,1]],"mu" =>[1,1,[1,1]])

69
70
71
72
73
    # basic test
    @test typeof(a1) <: AbstractAgent
    @test eltype(a1) == eltype(myspace)
    @test eltype(a5) == eltype(myspace2)
    @test typeof(a1) <: AbstractAgent
74
    @test typeof(a1) == typeof(a3)
Victor's avatar
Victor committed
75

76
77
    old_a1 = deepcopy(a1)
    @test prod((get_x(old_a1) . get_x(increment_x!(a1,myspace,p_myspace,0.))))
Victor's avatar
Victor committed
78
    # @test !prod((get_x(old_a1) .== get_x(increment_x!(a1,myspace,p_myspace,0.))))
79
80
81
    @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.))
82
    @test typeof(ABMEv._get_xinc(a2,myspace,p_myspace,0.)) == typeof(get_x(a2))
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109

    # 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)
110
end