common.jl 4.09 KB
Newer Older
1

2
3
4
import Distributions: Distribution, Univariate, Continuous, UnivariateDistribution, 
                      MultivariateDistribution, product_distribution

5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
abstract type Model end 
abstract type AbstractTrajectory end

const Transition = Union{String,Nothing}
const Location = String
const VariableAutomaton = String

mutable struct ContinuousTimeModel <: Model
    d::Int # state space dim
    k::Int # parameter space dim
    map_var_idx::Dict{String,Int} # maps str to full state space
    _map_obs_var_idx::Dict{String,Int} # maps str to observed state space
    map_param_idx::Dict{String,Int} # maps str in parameter space
    l_transitions::Vector{Transition}
    p::Vector{Float64}
    x0::Vector{Int}
    t0::Float64
    f!::Function
    g::Vector{String} # of dimension dobs
    _g_idx::Vector{Int} # of dimension dobs
25
    isabsorbing::Function
26
27
    time_bound::Float64
    buffer_size::Int
28
    estim_min_states::Int
29
30
31
32
end

struct Trajectory <: AbstractTrajectory
    m::ContinuousTimeModel
33
    values::Vector{Vector{Int}}
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
    times::Vector{Float64}
    transitions::Vector{Transition}
end

struct Edge
    transitions::Vector{Transition}
    check_constraints::Function
    update_state!::Function
end

struct LHA
    l_transitions::Vector{Transition}
    l_loc::Vector{Location} 
    Λ::Dict{Location,Function}
    l_loc_init::Vector{Location}
    l_loc_final::Vector{Location}
    map_var_automaton_idx::Dict{VariableAutomaton,Int} # nvar keys : str_var => idx in l_var
    l_flow::Dict{Location,Vector{Float64}} # output of length nvar
    map_edges::Dict{Tuple{Location,Location},Vector{Edge}}
    l_ctes::Dict{String,Float64}
    map_var_model_idx::Dict{String,Int} # of dim d (of a model)
end

mutable struct StateLHA
    A::LHA
    loc::Location
    l_var::Vector{Float64}
    time::Float64
end

64
mutable struct SynchronizedModel <: Model
65
66
67
68
    m::ContinuousTimeModel
    automaton::LHA
end

69
70
71
struct SynchronizedTrajectory <: AbstractTrajectory
    S::StateLHA
    m::SynchronizedModel
72
    values::Vector{Vector{Int}}
73
74
75
76
    times::Vector{Float64}
    transitions::Vector{Transition}
end

77
78
79
80
81
82
struct ParametricModel
    m::Model
    l_param::Vector{String}
    dist::Distribution
end

83
84
85
# Constructors
function ContinuousTimeModel(d::Int, k::Int, map_var_idx::Dict, map_param_idx::Dict, l_transitions::Vector{String}, 
              p::Vector{Float64}, x0::Vector{Int}, t0::Float64, 
86
              f!::Function, isabsorbing::Function; 
87
              g::Vector{String} = keys(map_var_idx), time_bound::Float64 = Inf, buffer_size::Int = 10, estim_min_states::Int = 50)
88
89
90
91
92
93
94
95
96
97
98
    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
  
    if length(methods(f!)) >= 2
        @warn "You have possibly redefined a function Model.f! used in a previously instantiated model."
    end
99
100
    if length(methods(isabsorbing)) >= 2
        @warn "You have possibly redefined a function Model.isabsorbing used in a previously instantiated model."
101
102
    end

103
    return ContinuousTimeModel(d, k, map_var_idx, _map_obs_var_idx, map_param_idx, l_transitions, p, x0, t0, f!, g, _g_idx, isabsorbing, time_bound, buffer_size, estim_min_states)
104
105
106
107
108
end

LHA(A::LHA, map_var::Dict{String,Int}) = LHA(A.l_transitions, A.l_loc, A.Λ, 
                                             A.l_loc_init, A.l_loc_final, A.map_var_automaton_idx, A.l_flow,
                                             A.map_edges, A.l_ctes, map_var)
109
110
Base.:*(m::ContinuousTimeModel, A::LHA) = SynchronizedModel(m, A)
Base.:*(A::LHA, m::ContinuousTimeModel) = SynchronizedModel(m, A)
111

112
113
114
115
function ParametricModel(am::Model, priors::Tuple{String,UnivariateDistribution}...)
    m = get_proba_model(am)
    l_param = String[]
    l_dist = Distribution{Univariate,Continuous}[]
116
    for prior in priors
117
        check_vars = true
118
119
120
121
122
        str_p = prior[1]
        dist = prior[2]
        @assert str_p in keys(m.map_param_idx)
        push!(l_param, str_p)
        push!(l_dist, dist) 
123
    end
124
    return ParametricModel(m, l_param, product_distribution(l_dist))
125
126
end

127