model.jl 2.43 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
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
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}
    p::AbstractVector{Real}
    x0::AbstractVector{Int}
    t0::Real
    f::Function
    g::AbstractVector{String} # of dimension dobs
    _g_idx::AbstractVector{Int} # of dimension dobs
    is_absorbing::Function
    time_bound::Real
end

function CTMC(d::Int, k::Int, map_var_idx::Dict, map_param_idx::Dict, l_name_transitions::AbstractVector{String}, 
              p::AbstractVector{Real}, x0::AbstractVector{Int}, t0::Real, 
              f::Function, is_absorbing::Function; g::AbstractVector{String} = keys(map_var_idx), time_bound::Real = Inf)
    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)
    transitions = Vector{String}(undef, 0)
    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,:] 
    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

65
66
67
68
69
function check_consistency(m::Model) end
function simulate(m::Model, n::Int; bound::Real = Inf)::AbstractObservations end
function set_param!(m::Model, p::AbstractVector{Real})::Nothing end
function get_param(m::Model)::AbstractVector{Real} end

70
71
72
73
function load_model(name_model::String)
    include(pathof(@__MODULE__) * "/../../models/" * name_model * ".jl")
end