model.jl 2.92 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
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
14
15
16
    l_name_transitions::Vector{String}
    p::Vector{Float64}
    x0::Vector{Int}
17
    t0::Float64
18
19
    f!::Function
    g::Vector{String} # of dimension dobs
20
    _g_idx::Vector{Int} # of dimension dobs
21
    is_absorbing::Function
22
    time_bound::Float64
23
24
end

25
26
27
function CTMC(d::Int, k::Int, map_var_idx::Dict, map_param_idx::Dict, l_name_transitions::Vector{String}, 
              p::Vector{Float64}, x0::Vector{Int}, t0::Float64, 
              f!::Function, is_absorbing::Function; g::Vector{String} = keys(map_var_idx), time_bound::Float64 = Inf)
28
29
30
31
32
33
34
    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
35
    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)
36
37
38
end

function simulate(m::ContinuousTimeModel)
39
    # trajectory fields
40
41
    full_values = zeros(m.d, 0)
    times = zeros(0)
42
43
44
    transitions = Vector{Union{String,Nothing}}(undef,0)
    # values at time n
    n = 0
45
46
    xn = m.x0
    tn = m.t0 
47
48
49
50
51
52
53
    tr = [""]
    # at time n+1
    xnplus1 = zeros(Int, m.d)
    tnplus1 = zeros(Float64, 1)
    is_absorbing = (m.is_absorbing(m.p,xn))::Bool
    while !is_absorbing && (tn <= m.time_bound)
        m.f!(xnplus1, tnplus1, tr, xn, tn, m.p)
54
        full_values = hcat(full_values, xnplus1)
55
56
57
        push!(times, tnplus1[1])
        push!(transitions, tr[1])
        xn, tn = xnplus1, tnplus1[1]
58
        n += 1
59
        is_absorbing = m.is_absorbing(m.p,xn)::Bool
60
61
    end
    values = full_values[m._g_idx,:] 
Bentriou Mahmoud's avatar
Bentriou Mahmoud committed
62
63
64
65
66
67
68
69
    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
    
70
71
72
73
74
75
76
77
78
79
80
    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
81
is_bounded(m::Model) = m.time_bound < Inf
82
function check_consistency(m::Model) end
83
function simulate(m::Model, n::Int; bound::Float64 = Inf)::AbstractObservations end
84
85
function set_param!(m::Model, p::Vector{Float64})::Nothing end
function get_param(m::Model)::Vector{Float64} end
86

87
get_module_path() = dirname(dirname(pathof(@__MODULE__)))
88
function load_model(name_model::String)
89
    include(get_module_path() * "/models/" * name_model * ".jl")
90
91
end