ABMEv_Space.jl 2.54 KB
Newer Older
Victor's avatar
Victor committed
1

Victor's avatar
Victor committed
2
3
4
5

abstract type IsFinite{T} end

#ife stands for is finite
Victor's avatar
Victor committed
6
7
8
9
10
"""
$(TYPEDEF)
`Dim` is the dimension of the space, `T` is the element type,
`I` to indicate finiteness
"""
Victor's avatar
Victor committed
11
12
abstract type AbstractSpace{Dim,T,I} end
AbstractSpacesTuple = Tuple{Vararg{AbstractSpace}}
13
import Base:ndims,isfinite,eltype
Victor's avatar
Victor committed
14
Base.ndims(x::AbstractSpace{Dim,T,I}) where {Dim,T,I} = Dim
Victor's avatar
Victor committed
15
Base.isfinite(x::AbstractSpace{Dim,T,IsFinite{t}}) where {Dim,T,t} = t #not sure we need this
16
17
18
Base.eltype(::AbstractSpace{Dim,T,I}) where {Dim,T,I} = Dim > 1 ? Tuple{Vararg{T,Dim}} : T
Base.ndims(ss::AbstractSpacesTuple) = length(ss)
Base.eltype(ss::AbstractSpacesTuple) where {Dim,T,I} = Tuple{eltype.(ss)...}
Victor's avatar
Victor committed
19
20
21

SpaceType=Union{Nothing, AbstractSpace} # not sure what is this used for

22
23
24
"""
$(TYPEDEF)
"""
Victor's avatar
Victor committed
25
struct GraphSpace{T} <: AbstractSpace{1,T,IsFinite{true}}
Victor's avatar
Victor committed
26
27
28
    g::AbstractGraph{T}
end

29
abstract type AbstractSegment{T<:Number}  <: AbstractSpace{1,T,IsFinite{true}} end
30
31
32
33

"""
$(TYPEDEF)
"""
34
struct ContinuousSegment{T<:AbstractFloat} <:  AbstractSegment{T}
Victor's avatar
Victor committed
35
36
37
38
    s::T
    e::T
end

39
40
41
"""
$(TYPEDEF)
"""
42
struct DiscreteSegment{T<:Integer} <: AbstractSegment{T}
Victor's avatar
Victor committed
43
44
45
    s::T
    e::T
end
Victor's avatar
Victor committed
46

47
48
"""
$(TYPEDEF)
49
50

A real pace with dimension N and type T
51
52
"""
struct RealSpace{N,T} <: AbstractSpace{N,T,IsFinite{false}} end
Victor's avatar
Victor committed
53

Victor's avatar
Victor committed
54
# TODO: find a way to put a type on D in get_inc
55
56

function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:AbstractFloat,I<:IsFinite{false}}
Victor's avatar
Victor committed
57
    if Dim > 1
58
        return Tuple(D .* randn(T,Dim))
Victor's avatar
Victor committed
59
    else
60
        return D * randn(T)
Victor's avatar
Victor committed
61
    end
Victor's avatar
Victor committed
62
end
63
64
65
66
67
68

"""
$(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)
Victor's avatar
Victor committed
69

70
#TODO: there is probably a better way of dealing with those two functions
Victor's avatar
Victor committed
71
function get_inc(x,D,s::ContinuousSegment{T}) where {T}
72
    inc = D * randn(T)
Victor's avatar
Victor committed
73
    return _reflect1D(x,inc,s)
Victor's avatar
Victor committed
74
75
76
end

function get_inc(x,D,s::DiscreteSegment{T}) where {T}
77
    inc = D * randn()
Victor's avatar
Victor committed
78
    return round(T,_reflect1D(x,inc,s))
Victor's avatar
Victor committed
79
80
81
end

function get_inc(x,D,s::GraphSpace{T}) where {T}
82
83
    niter = round(Int,abs(D*randn())) + 1
    # here we add +1 since randomwalk(s.g,x,niter) returns x
Victor's avatar
Victor committed
84
85
86
87
88
    if niter > 0
        return last(randomwalk(s.g,x,niter)) - x
    else
        return 0
    end
Victor's avatar
Victor committed
89
90
91
end

"""
Victor's avatar
Victor committed
92
93
$(SIGNATURES)
Here we increment the trajectory of trait 1 such that it follows a reflected brownian motion (1D)
Victor's avatar
Victor committed
94
"""
Victor's avatar
Victor committed
95
96
function _reflect1D(x::Number,inc::Number,s::AbstractSegment)
    if x + inc < s.s
Victor's avatar
Victor committed
97
        inc = 2 * ( s.s - x ) - inc
Victor's avatar
Victor committed
98
    elseif  x + inc > s.e
Victor's avatar
Victor committed
99
        inc = 2 * ( s.e - x ) - inc
Victor's avatar
Victor committed
100
101
102
    else
        return inc
    end
Victor's avatar
Victor committed
103
    _reflect1D(x,inc,s)
Victor's avatar
Victor committed
104
end