model.jl 2.71 KB
Newer Older
1

2
3
import StaticArrays: SVector

4
5
6
7
abstract type Model end 
abstract type ContinuousTimeModel <: Model end 
abstract type DiscreteTimeModel <: Model end 

8
9
10
11
12
13
14
mutable struct CTMC <: ContinuousTimeModel
    d::Int # state space dim
    k::Int # parameter space dim
    map_var_idx::Dict # maps str to full state space
    _map_obs_var_idx::Dict # maps str to observed state space
    map_param_idx::Dict # maps str in parameter space
    l_name_transitions::AbstractVector{String}
15
    p::AbstractVector{Float64}
16
    x0::AbstractVector{Int}
17
    t0::Float64
18
19
    f::Function
    g::AbstractVector{String} # of dimension dobs
20
    _g_idx::Vector{Int} # of dimension dobs
21
    is_absorbing::Function
22
    time_bound::Float64
23
24
25
end

function CTMC(d::Int, k::Int, map_var_idx::Dict, map_param_idx::Dict, l_name_transitions::AbstractVector{String}, 
26
27
              p::AbstractVector{Float64}, x0::AbstractVector{Int}, t0::Float64, 
              f::Function, is_absorbing::Function; g::AbstractVector{String} = keys(map_var_idx), time_bound::Float64 = Inf)
28
29
30
31
32
33
34
35
36
37
38
39
40
    dobs = length(g)
    _map_obs_var_idx = Dict()
    _g_idx = Vector{Int}(undef, dobs)
    for i = 1:dobs
        _g_idx[i] = map_var_idx[g[i]] # = ( (g[i] = i-th obs var)::String => idx in state space )
        _map_obs_var_idx[g[i]] = i
    end
    return CTMC(d, k, map_var_idx, _map_obs_var_idx, map_param_idx, l_name_transitions, p, x0, t0, f, g, _g_idx, is_absorbing, time_bound)
end

function simulate(m::ContinuousTimeModel)
    full_values = zeros(m.d, 0)
    times = zeros(0)
Bentriou Mahmoud's avatar
Bentriou Mahmoud committed
41
    transitions = Vector{Union{String,Nothing}}(undef, 0)
42
43
44
45
46
47
48
49
50
51
52
53
    xn = m.x0
    tn = m.t0 
    n = 0
    while !m.is_absorbing(m.p,xn) && tn <= m.time_bound
        xnplus1, tnplus1, tr = f(xn, tn, m.p)
        full_values = hcat(full_values, xnplus1)
        push!(times, tnplus1)
        push!(transitions, tr)
        xn, tn = xnplus1, tnplus1
        n += 1
    end
    values = full_values[m._g_idx,:] 
Bentriou Mahmoud's avatar
Bentriou Mahmoud committed
54
55
56
57
58
59
60
61
    if is_bounded(m)
        if times[end] > m.time_bound
            values[:, end] = values[:,end-1]
            times[end] = m.time_bound
            transitions[end] = nothing
        end
    end
    
62
63
64
65
66
67
68
69
70
71
72
    return Trajectory(m, values, times, transitions)
end

function simulate(m::ContinuousTimeModel, n::Int)
    obs = ContinuousObservations(undef, n)
    for i = 1:n
        obs[i] = simulate(m)
    end
    return obs
end

Bentriou Mahmoud's avatar
Bentriou Mahmoud committed
73
is_bounded(m::Model) = m.time_bound < Inf
74
function check_consistency(m::Model) end
75
76
77
function simulate(m::Model, n::Int; bound::Float64 = Inf)::AbstractObservations end
function set_param!(m::Model, p::AbstractVector{Float64})::Nothing end
function get_param(m::Model)::AbstractVector{Float64} end
78

79
80
81
82
function load_model(name_model::String)
    include(pathof(@__MODULE__) * "/../../models/" * name_model * ".jl")
end