common.jl 3.69 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

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
22
    isabsorbing::Function
23
24
    time_bound::Float64
    buffer_size::Int
25
    estim_min_states::Int
26
27
28
29
end

struct Trajectory <: AbstractTrajectory
    m::ContinuousTimeModel
30
    values::Vector{Vector{Int}}
31
32
33
34
    times::Vector{Float64}
    transitions::Vector{Transition}
end

35
36
37
38
39
struct ModelPrior
    m::Model
    map_l_param_dist::Dict{Vector{String},Distribution}
end

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
65
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

66
mutable struct SynchronizedModel <: Model
67
68
69
70
    m::ContinuousTimeModel
    automaton::LHA
end

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

79
80
81
# 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, 
82
              f!::Function, isabsorbing::Function; 
83
              g::Vector{String} = keys(map_var_idx), time_bound::Float64 = Inf, buffer_size::Int = 10, estim_min_states::Int = 50)
84
85
86
87
88
89
90
91
92
93
94
    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
95
96
    if length(methods(isabsorbing)) >= 2
        @warn "You have possibly redefined a function Model.isabsorbing used in a previously instantiated model."
97
98
    end

99
    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)
100
101
102
103
104
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)
105
106
Base.:*(m::ContinuousTimeModel, A::LHA) = SynchronizedModel(m, A)
Base.:*(A::LHA, m::ContinuousTimeModel) = SynchronizedModel(m, A)
107

108
109
110
111
112
113
114
115
function ModelPrior(m::Model, priors::Tuple{Vector{String},Distribution}...)
    map = Dict{Vector{String},Distribution}()
    for prior in priors
        map[prior[1]] = prior[2]
    end
    return ModelPrior(m, map)
end