lha.jl 5.12 KB
Newer Older
1
2

length_var(A::LHA) = length(A.map_var_automaton_idx)
3
get_value(A::LHA, x::Vector{Int}, var::String) = x[A.map_var_model_idx[var]]
4
5
6
7
8
9

copy(S::StateLHA) = StateLHA(S.A, S.loc, S.l_var, S.time)
# Not overring getproperty, setproperty to avoid a conversion Symbol => String for the dict key
getindex(S::StateLHA, var::VariableAutomaton) = (S.l_var)[(S.A).map_var_automaton_idx[var]]
setindex!(S::StateLHA, val::Float64, var::VariableAutomaton) = (S.l_var)[(S.A).map_var_automaton_idx[var]] = val
setindex!(S::StateLHA, val::Int, var::VariableAutomaton) = (S.l_var)[(S.A).map_var_automaton_idx[var]] = convert(Float64, val)
10
11
setindex!(S::StateLHA, val::Bool, var::VariableAutomaton) = (S.l_var)[(S.A).map_var_automaton_idx[var]] = convert(Float64, val)

12
13
14
15
16
17
18
19
20
21
function Base.show(io::IO, S::StateLHA)
    print(io, "State of LHA\n")
    print(io, "- location: $(S.loc)\n")
    print(io, "- time: $(S.time)\n")
    print(io, "- variables:\n")
    for (var, idx) in (S.A).map_var_automaton_idx
        print(io, "* $var = $(S.l_var[idx])\n")
    end
end

22
isaccepted(S::StateLHA) = (S.loc in (S.A).l_loc_final)
23

24
# Methods for synchronize / read the trajectory
25
function init_state(A::LHA, x0::Vector{Int}, t0::Float64)
26
27
28
29
30
31
32
33
34
35
    S0 = StateLHA(A, "", zeros(length_var(A)), t0)
    for loc in A.l_loc_init
        if A.Λ[loc](A,S0) 
            S0.loc = loc
            break
        end
    end
    return S0
end

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
65
66
67
68
69
70
function _find_edge_candidates!(edge_candidates::Vector{Edge}, current_loc::Location, 
                                A::LHA, Snplus1::StateLHA)
     
    for loc in A.l_loc
        tuple_edges = (current_loc, loc)
        if haskey(A.map_edges, tuple_edges)
            for edge in A.map_edges[tuple_edges]
                if edge.check_constraints(A, Snplus1)
                    push!(edge_candidates, edge)
                end
            end
        end
    end
end

function _get_edge_index(edge_candidates::Vector{Edge}, first_round::Bool, 
                         detected_event::Bool, tr_nplus1::Transition)
    ind_edge = 0
    bool_event = detected_event
    for i in eachindex(edge_candidates)
        edge = edge_candidates[i]
        if edge.transitions[1] == nothing
            return (i, bool_event)
        end
        if first_round || !detected_event
            if (length(edge.transitions) == 1 && tr_nplus1 != nothing && edge.transitions[1] == "ALL") || 
                (tr_nplus1 in edge.transitions)
                ind_edge = i
                bool_event = true
            end
        end
    end
    return (ind_edge, bool_event)
end

71
function next_state!(Snplus1::StateLHA, A::LHA, 
72
                    xnplus1::Vector{Int}, tnplus1::Float64, tr_nplus1::Transition, 
73
                    Sn::StateLHA; verbose::Bool = false)
74
75
76
77
78
79
80
    for i in eachindex(Snplus1.l_var)
        Snplus1.l_var[i] += (A.l_flow[Sn.loc])[i]*(tnplus1 - Sn.time) 
    end
    Snplus1.time = tnplus1

    # En fait d'apres observation de Cosmos, après qu'on ait lu la transition on devrait stop.
    edge_candidates = Edge[]
81
82
    first_round::Bool = true
    detected_event::Bool = (tr_nplus1 == nothing) ? true : false
83
84
    turns = 1
    while first_round || !detected_event || length(edge_candidates) > 0
85
        edge_candidates = empty!(edge_candidates)
86
        current_loc = Snplus1.loc
87
        if verbose @show turns end
88
        # Save all edges that satisfies transition predicate (synchronous or autonomous)
89
        _find_edge_candidates!(edge_candidates, current_loc, A, Snplus1)
90
        # Search the one we must chose
91
        ind_edge, detected_event = _get_edge_index(edge_candidates, first_round, detected_event, tr_nplus1)
92
93
94
95
96
        # Update the state with the chosen one (if it exists)
        if ind_edge > 0
            edge_candidates[ind_edge].update_state!(A, Snplus1, xnplus1)
            # Should add something like if edges_candidates[ind_edge].transition != nohting break end ??
        end
97
        if ind_edge == 0 break end
98
        if verbose
99
100
            @show first_round detected_event
            @show tnplus1 tr_nplus1 xnplus1
101
102
103
104
105
106
            @show ind_edge
            @show edge_candidates
            @show tuple_candidates
            @show Snplus1
        end
        # For debug
107
108
        if turns > 100
            println("Number of turns in next_state! is suspicious")
109
110
111
112
113
114
115
            @show first_round, detected_event
            @show length(edge_candidates)
            @show tnplus1, tr_nplus1, xnplus1
            @show edge_candidates
            for edge in edge_candidates
                @show edge.check_constraints(A, Snplus1)
            end
116
            error("Unpredicted behavior automaton")
117
118
119
120
121
122
        end
        turns += 1
        first_round = false
    end
end

123
# For tests purposes
124
125
126
127
128
129
130
131
132
function read_trajectory(A::LHA, σ::Trajectory; verbose = false)
    A_new = LHA(A, (σ.m)._map_obs_var_idx)
    l_t = times(σ)
    l_tr = transitions(σ)
    Sn = init_state(A_new, σ[1], l_t[1])
    Snplus1 = copy(Sn)
    if verbose println("Init: ") end
    if verbose @show Sn end
    for n in 1:length_states(σ)
133
        next_state!(Snplus1, A_new, σ[n], l_t[n], l_tr[n], Sn; verbose = verbose)
134
135
136
137
138
139
140
141
        if Snplus1.loc in A_new.l_loc_final 
            break 
        end
        Sn = Snplus1 
    end
    return Sn
end