automaton_G.jl 7.56 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1::Float64, t2::Float64, str_obs::String)
    @assert str_obs in m.g
    # Locations
    l_loc = ["l0", "l1", "l2", "l3", "l4"]

    # Invariant predicates
    true_inv_predicate = (A::LHA, S:: StateLHA) -> return true 
    Λ_F = Dict("l0" => true_inv_predicate, "l1" => true_inv_predicate,
               "l2" => true_inv_predicate, "l3" => true_inv_predicate, 
               "l4" => true_inv_predicate)
    
    ## Init and final loc
    l_loc_init = ["l0"]
    l_loc_final = ["l2"]

    ## Map of automaton variables
18
    map_var_automaton_idx = Dict{VariableAutomaton,Int}("tprime" => 1, "in" => 2,
19
                                                         "n" => 3,  "d" => 4, "isabs" => 5)
20
21

    ## Flow of variables
22
23
24
25
26
    l_flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0,0.0,0.0], 
                                                     "l1" => [0.0,0.0,0.0,0.0,0.0], 
                                                     "l2" => [0.0,0.0,0.0,0.0,0.0], 
                                                     "l3" => [0.0,0.0,0.0,0.0,0.0], 
                                                     "l4" => [1.0,0.0,0.0,0.0,0.0])
27
28
29
30

    ## Edges
    map_edges = Dict{Tuple{Location,Location}, Vector{Edge}}()

31
    istrue(val::Float64) = convert(Bool, val)
32
33
34
35

    # l0 loc
    tuple = ("l0", "l1")
    cc_aut_G_l0l1_1(A::LHA, S::StateLHA) = true
36
    us_aut_G_l0l1_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
37
38
39
40
41
        (S.loc = "l1"; 
         S["d"] = 0; 
         S["n"] = get_value(A, x, str_obs); 
         S["in"] = true; 
         S["isabs"] = m.isabsorbing(m.p,x))
42
43
44
45
46
47
    edge1 = Edge([nothing], cc_aut_G_l0l1_1, us_aut_G_l0l1_1!)
    map_edges[tuple] = [edge1]

    # l1 loc
    tuple = ("l1", "l3")
    cc_aut_G_l1l3_1(A::LHA, S::StateLHA) = 
48
49
        S.time <= A.l_ctes["t1"] && 
        S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"]
50
    us_aut_G_l1l3_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
51
52
53
        (S.loc = "l3"; 
         S["d"] = min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); 
         S["in"] = false)
54
    edge1 = Edge([nothing], cc_aut_G_l1l3_1, us_aut_G_l1l3_1!)
55
56
57
58
59
60
61
62

    cc_aut_G_l1l3_3(A::LHA, S::StateLHA) = 
         !istrue(S["in"]) && 
         (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && 
         (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"])
    us_aut_G_l1l3_3!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l3"; 
         S["d"] = S["d"] * (S.time - A.l_ctes["t1"]); 
63
         S["tprime"] = 0.0)
64
65
    edge3 = Edge([nothing], cc_aut_G_l1l3_3, us_aut_G_l1l3_3!)
   
66
    cc_aut_G_l1l3_2(A::LHA, S::StateLHA) = 
67
68
        (S.time <= A.l_ctes["t1"]) && 
        (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"])
69
    us_aut_G_l1l3_2!(A::LHA, S::StateLHA, x::Vector{Int}) = 
70
71
72
        (S.loc = "l3"; 
         S["d"] = 0; 
         S["in"] = false)
73
    edge2 = Edge([nothing], cc_aut_G_l1l3_2, us_aut_G_l1l3_2!)
74

75
    cc_aut_G_l1l3_4(A::LHA, S::StateLHA) = 
76
77
78
79
80
        istrue(S["in"]) && 
        (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && 
        (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"])
    us_aut_G_l1l3_4!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l3"; 
81
         S["tprime"] = 0.0)
82
    edge4 = Edge([nothing], cc_aut_G_l1l3_4, us_aut_G_l1l3_4!)
83
    
84
85
86
87
    map_edges[tuple] = [edge1, edge2, edge3, edge4]

    tuple = ("l1", "l4")
    cc_aut_G_l1l4_1(A::LHA, S::StateLHA) = 
88
89
90
91
92
93
        !istrue(S["in"]) && 
        (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && 
        (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"])
    us_aut_G_l1l4_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l4"; 
         S["d"] += S["d"] * (S.time - A.l_ctes["t1"]))
94
95
    edge1 = Edge([nothing], cc_aut_G_l1l4_1, us_aut_G_l1l4_1!)
    cc_aut_G_l1l4_2(A::LHA, S::StateLHA) = 
96
97
98
99
100
        istrue(S["in"]) && 
        (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && 
        (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"])
    us_aut_G_l1l4_2!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l4")
101
102
103
104
    edge2 = Edge([nothing], cc_aut_G_l1l4_2, us_aut_G_l1l4_2!)
    map_edges[tuple] = [edge1, edge2]

    tuple = ("l1", "l2")
105
106
107
108
109
    cc_aut_G_l1l2_1(A::LHA, S::StateLHA) = 
        istrue(S["in"]) && 
        S.time >= A.l_ctes["t2"]
    us_aut_G_l1l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2")
110
    edge1 = Edge([nothing], cc_aut_G_l1l2_1, us_aut_G_l1l2_1!)
111
112
113
114
115
116
    cc_aut_G_l1l2_2(A::LHA, S::StateLHA) = 
        !istrue(S["in"]) && 
        S.time >= A.l_ctes["t2"]
    us_aut_G_l1l2_2!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2"; 
         S["d"] = S["d"] * (A.l_ctes["t2"] - A.l_ctes["t1"]))
117
    edge2 = Edge([nothing], cc_aut_G_l1l2_2, us_aut_G_l1l2_2!)
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
    cc_aut_G_l1l2_3(A::LHA, S::StateLHA) = 
        istrue(S["isabs"]) && 
        S.time <= A.l_ctes["t1"]
    us_aut_G_l1l2_3!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2"; 
         S["d"] = S["d"] * (A.l_ctes["t2"] - A.l_ctes["t1"]))
    edge3 = Edge([nothing], cc_aut_G_l1l2_3, us_aut_G_l1l2_3!)
    cc_aut_G_l1l2_4(A::LHA, S::StateLHA) = 
        istrue(S["isabs"]) && 
        (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"])
    us_aut_G_l1l2_4!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2"; 
         S["d"] += (A.l_ctes["t2"] - S.time) * 
                    min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x1"] - S["n"])))
    edge4 = Edge([nothing], cc_aut_G_l1l2_4, us_aut_G_l1l2_4!)
    
    map_edges[tuple] = [edge1, edge2, edge3, edge4]
135
136
137
138

    # l3 loc
    tuple = ("l3", "l1")
    cc_aut_G_l3l1_1(A::LHA, S::StateLHA) = true
139
140
141
142
    us_aut_G_l3l1_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l1"; 
         S["n"] = get_value(A, x, str_obs); 
         S["isabs"] = m.isabsorbing(m.p,x))
143
144
145
146
    edge1 = Edge(["ALL"], cc_aut_G_l3l1_1, us_aut_G_l3l1_1!)
    map_edges[tuple] = [edge1]

    tuple = ("l3", "l2")
147
    cc_aut_G_l3l2_2(A::LHA, S::StateLHA) = 
148
149
        istrue(S["in"]) && 
        S.time >= A.l_ctes["t2"]
150
151
152
    us_aut_G_l3l2_2!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2";
         S["d"] = S["d"] * (A.l_ctes["t2"] - A.l_ctes["t1"]))
153
    edge2 = Edge([nothing], cc_aut_G_l3l2_2, us_aut_G_l3l2_2!)
154
155
156
157
158
159
    cc_aut_G_l3l2_1(A::LHA, S::StateLHA) = 
        !istrue(S["in"]) && 
        S.time >= A.l_ctes["t2"]
    us_aut_G_l3l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
        (S.loc = "l2")
    edge1 = Edge([nothing], cc_aut_G_l3l2_1, us_aut_G_l3l2_1!)
160
 
161
    map_edges[tuple] = [edge1, edge2]
162
163
164
165

    # l4 loc
    tuple = ("l4", "l1")
    cc_aut_G_l4l1_1(A::LHA, S::StateLHA) = true
166
    us_aut_G_l4l1_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
167
        (S.loc = "l1"; 
168
169
         S["d"] += S["tprime"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); 
         S["tprime"] = 0.0; 
170
171
172
         S["n"] = get_value(A, x, str_obs); 
         S["in"] = true; 
         S["isabs"] = m.isabsorbing(m.p,x))
173
174
175
176
    edge1 = Edge(["ALL"], cc_aut_G_l4l1_1, us_aut_G_l4l1_1!)
    map_edges[tuple] = [edge1]

    tuple = ("l4", "l2")
177
    cc_aut_G_l4l2_1(A::LHA, S::StateLHA) = 
178
        (S.time >= A.l_ctes["t2"])
179
    us_aut_G_l4l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = 
180
        (S.loc = "l2"; 
181
182
         S["d"] +=  S["tprime"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); 
         S["tprime"] = 0.0)
183
    edge1 = Edge([nothing], cc_aut_G_l4l2_1, us_aut_G_l4l2_1!)
184
    
185
    map_edges[tuple] = [edge1]
186
187
188
189
190
191
192

    ## Constants
    l_ctes = Dict{String,Float64}("x1" => x1, "x2" => x2, "t1" => t1, "t2" => t2)

    A = LHA(m.l_transitions, l_loc, Λ_F, l_loc_init, l_loc_final, 
            map_var_automaton_idx, l_flow, map_edges, l_ctes, m.map_var_idx)
    return A
193
   
194
195
196
197
end

export create_automaton_G