ABMEv_Space.jl 2.54 KB
 Victor committed Oct 02, 2020 1 `````` `````` Victor committed Sep 27, 2020 2 3 4 5 `````` abstract type IsFinite{T} end #ife stands for is finite `````` Victor committed Oct 02, 2020 6 7 8 9 10 ``````""" \$(TYPEDEF) `Dim` is the dimension of the space, `T` is the element type, `I` to indicate finiteness """ `````` Victor committed Sep 27, 2020 11 12 ``````abstract type AbstractSpace{Dim,T,I} end AbstractSpacesTuple = Tuple{Vararg{AbstractSpace}} `````` Victor committed Sep 29, 2020 13 ``````import Base:ndims,isfinite,eltype `````` Victor committed Sep 27, 2020 14 ``````Base.ndims(x::AbstractSpace{Dim,T,I}) where {Dim,T,I} = Dim `````` Victor committed Sep 28, 2020 15 ``````Base.isfinite(x::AbstractSpace{Dim,T,IsFinite{t}}) where {Dim,T,t} = t #not sure we need this `````` Victor committed Sep 29, 2020 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 committed Sep 27, 2020 19 20 21 `````` SpaceType=Union{Nothing, AbstractSpace} # not sure what is this used for `````` Victor committed Sep 28, 2020 22 23 24 ``````""" \$(TYPEDEF) """ `````` Victor committed Sep 28, 2020 25 ``````struct GraphSpace{T} <: AbstractSpace{1,T,IsFinite{true}} `````` Victor committed Sep 27, 2020 26 27 28 `````` g::AbstractGraph{T} end `````` Victor committed Sep 29, 2020 29 ``````abstract type AbstractSegment{T<:Number} <: AbstractSpace{1,T,IsFinite{true}} end `````` Victor committed Sep 28, 2020 30 31 32 33 `````` """ \$(TYPEDEF) """ `````` Victor committed Sep 29, 2020 34 ``````struct ContinuousSegment{T<:AbstractFloat} <: AbstractSegment{T} `````` Victor committed Sep 27, 2020 35 36 37 38 `````` s::T e::T end `````` Victor committed Sep 28, 2020 39 40 41 ``````""" \$(TYPEDEF) """ `````` Victor committed Sep 29, 2020 42 ``````struct DiscreteSegment{T<:Integer} <: AbstractSegment{T} `````` Victor committed Sep 28, 2020 43 44 45 `````` s::T e::T end `````` Victor committed Sep 27, 2020 46 `````` `````` Victor committed Sep 28, 2020 47 48 ``````""" \$(TYPEDEF) `````` Victor committed Sep 29, 2020 49 50 `````` A real pace with dimension N and type T `````` Victor committed Sep 28, 2020 51 52 ``````""" struct RealSpace{N,T} <: AbstractSpace{N,T,IsFinite{false}} end `````` Victor committed Sep 27, 2020 53 `````` `````` Victor committed Oct 02, 2020 54 ``````# TODO: find a way to put a type on D in get_inc `````` Victor committed Oct 06, 2020 55 56 `````` function _get_inc(D,s::AbstractSpace{Dim,T,I}) where {Dim,T<:AbstractFloat,I<:IsFinite{false}} `````` Victor committed Sep 28, 2020 57 `````` if Dim > 1 `````` Victor committed Sep 29, 2020 58 `````` return Tuple(D .* randn(T,Dim)) `````` Victor committed Sep 26, 2020 59 `````` else `````` Victor committed Sep 29, 2020 60 `````` return D * randn(T) `````` Victor committed Sep 26, 2020 61 `````` end `````` Victor committed Sep 28, 2020 62 ``````end `````` Victor committed Oct 06, 2020 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 committed Sep 28, 2020 69 `````` `````` Victor committed Sep 29, 2020 70 ``````#TODO: there is probably a better way of dealing with those two functions `````` Victor committed Sep 28, 2020 71 ``````function get_inc(x,D,s::ContinuousSegment{T}) where {T} `````` Victor committed Sep 29, 2020 72 `````` inc = D * randn(T) `````` Victor committed Sep 28, 2020 73 `````` return _reflect1D(x,inc,s) `````` Victor committed Sep 28, 2020 74 75 76 ``````end function get_inc(x,D,s::DiscreteSegment{T}) where {T} `````` Victor committed Sep 29, 2020 77 `````` inc = D * randn() `````` Victor committed Sep 28, 2020 78 `````` return round(T,_reflect1D(x,inc,s)) `````` Victor committed Sep 28, 2020 79 80 81 ``````end function get_inc(x,D,s::GraphSpace{T}) where {T} `````` Victor committed Oct 06, 2020 82 83 `````` niter = round(Int,abs(D*randn())) + 1 # here we add +1 since randomwalk(s.g,x,niter) returns x `````` Victor committed Sep 28, 2020 84 85 86 87 88 `````` if niter > 0 return last(randomwalk(s.g,x,niter)) - x else return 0 end `````` Victor committed Sep 26, 2020 89 90 91 ``````end """ `````` Victor committed Oct 02, 2020 92 93 ``````\$(SIGNATURES) Here we increment the trajectory of trait 1 such that it follows a reflected brownian motion (1D) `````` Victor committed Sep 26, 2020 94 ``````""" `````` Victor committed Sep 28, 2020 95 96 ``````function _reflect1D(x::Number,inc::Number,s::AbstractSegment) if x + inc < s.s `````` Victor committed Sep 28, 2020 97 `````` inc = 2 * ( s.s - x ) - inc `````` Victor committed Sep 28, 2020 98 `````` elseif x + inc > s.e `````` Victor committed Sep 28, 2020 99 `````` inc = 2 * ( s.e - x ) - inc `````` Victor committed Sep 26, 2020 100 101 102 `````` else return inc end `````` Victor committed Sep 28, 2020 103 `````` _reflect1D(x,inc,s) `````` Victor committed Sep 26, 2020 104 ``end``