automaton_G.jl 8.92 KB
Newer Older
1

2
3
function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1::Float64, t2::Float64, sym_obs::VariableModel)
    @assert sym_obs in m.g
4
    # Locations
5
    locations = [:l0, :l1, :l2, :l3, :l4]
6
7

    # Invariant predicates
8
    true_inv_predicate = (x::Vector{Int}) -> return true 
9
10
11
    Λ_F = Dict(:l0 => true_inv_predicate, :l1 => true_inv_predicate,
               :l2 => true_inv_predicate, :l3 => true_inv_predicate, 
               :l4 => true_inv_predicate)
12
13
    
    ## Init and final loc
14
15
    locations_init = [:l0]
    locations_final = [:l2]
16
17

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

    ## Flow of variables
22
23
24
25
26
    flow = Dict{Location,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

    ## Edges
29
30
31
32
    map_edges = Dict{Location, Dict{Location, Vector{Edge}}}()
    for loc in locations 
        map_edges[loc] = Dict{Location, Vector{Edge}}()
    end
33
    istrue(val::Float64) = convert(Bool, val)
34
35

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

    # l1 loc
48
    tuple = (:l1, :l3)
49
50
51
52
    cc_aut_G_l1l3_1(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
        getfield(S, :time) <= constants[:t1] && 
        S[:n] < constants[:x1] || S[:n] > constants[:x2]
    us_aut_G_l1l3_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
53
        (S.loc = :l3; 
54
         S[:d] = min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])); 
55
         S[:in] = false)
56
    edge1 = Edge([nothing], cc_aut_G_l1l3_1, us_aut_G_l1l3_1!)
57

58
    cc_aut_G_l1l3_3(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
59
         !istrue(S[:in]) && 
60
61
62
         (constants[:t1] <= getfield(S, :time) <= constants[:t2]) && 
         (constants[:x1] <= S[:n] <= constants[:x2])
    us_aut_G_l1l3_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
63
        (S.loc = :l3; 
64
         S[:d] = S[:d] * (getfield(S, :time) - constants[:t1]); 
65
         S[:tprime] = 0.0)
66
67
    edge3 = Edge([nothing], cc_aut_G_l1l3_3, us_aut_G_l1l3_3!)
   
68
69
70
71
    cc_aut_G_l1l3_2(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
        (getfield(S, :time) <= constants[:t1]) && 
        (constants[:x1] <= S[:n] <= constants[:x2])
    us_aut_G_l1l3_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
72
73
74
        (S.loc = :l3; 
         S[:d] = 0; 
         S[:in] = false)
75
    edge2 = Edge([nothing], cc_aut_G_l1l3_2, us_aut_G_l1l3_2!)
76

77
    cc_aut_G_l1l3_4(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
78
        istrue(S[:in]) && 
79
80
81
        (constants[:t1] <= getfield(S, :time) <= constants[:t2]) && 
        (constants[:x1] <= S[:n] <= constants[:x2])
    us_aut_G_l1l3_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
82
83
        (S.loc = :l3; 
         S[:tprime] = 0.0)
84
    edge4 = Edge([nothing], cc_aut_G_l1l3_4, us_aut_G_l1l3_4!)
85
    
86
    map_edges[:l1][:l3] = [edge1, edge2, edge3, edge4]
87

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

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

    # l3 loc
141
    tuple = (:l3, :l1)
142
143
    cc_aut_G_l3l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = true
    us_aut_G_l3l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
144
        (S.loc = :l1; 
145
146
         S[:n] = get_value(S, x, sym_obs); 
         S[:isabs] = getfield(m, :isabsorbing)(getfield(m, :p),x))
147
    edge1 = Edge([:ALL], cc_aut_G_l3l1_1, us_aut_G_l3l1_1!)
148
149
    
    map_edges[:l3][:l1] = [edge1]
150

151
    tuple = (:l3, :l2)
152
    cc_aut_G_l3l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
153
        istrue(S[:in]) && 
154
155
        getfield(S, :time) >= constants[:t2]
    us_aut_G_l3l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
156
        (S.loc = :l2;
157
         S[:d] = S[:d] * (constants[:t2] - constants[:t1]))
158
    edge2 = Edge([nothing], cc_aut_G_l3l2_2, us_aut_G_l3l2_2!)
159
    cc_aut_G_l3l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
160
        !istrue(S[:in]) && 
161
162
        getfield(S, :time) >= constants[:t2]
    us_aut_G_l3l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
163
        (S.loc = :l2)
164
    edge1 = Edge([nothing], cc_aut_G_l3l2_1, us_aut_G_l3l2_1!)
165
 
166
    map_edges[:l3][:l2] = [edge1, edge2]
167
168

    # l4 loc
169
    tuple = (:l4, :l1)
170
171
    cc_aut_G_l4l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = true
    us_aut_G_l4l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
172
        (S.loc = :l1; 
173
         S[:d] += S[:tprime] * min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])); 
174
         S[:tprime] = 0.0; 
175
         S[:n] = get_value(S, x, sym_obs); 
176
         S[:in] = true; 
177
         S[:isabs] = getfield(m, :isabsorbing)(getfield(m, :p),x))
178
    edge1 = Edge([:ALL], cc_aut_G_l4l1_1, us_aut_G_l4l1_1!)
179
180
    
    map_edges[:l4][:l1] = [edge1]
181

182
    tuple = (:l4, :l2)
183
184
185
    cc_aut_G_l4l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, xn::Vector{Int}) = 
        (getfield(S, :time) >= constants[:t2])
    us_aut_G_l4l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}) = 
186
        (S.loc = :l2; 
187
         S[:d] +=  S[:tprime] * min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])); 
188
         S[:tprime] = 0.0)
189
    edge1 = Edge([nothing], cc_aut_G_l4l2_1, us_aut_G_l4l2_1!)
190
    
191
    map_edges[:l4][:l2] = [edge1]
192
193

    ## Constants
194
    constants = Dict{Symbol,Float64}(:x1 => x1,  :x2 => x2, :t1 => t1, :t2 => t2)
195

196
197
    A = LHA(m.transitions, locations, Λ_F, locations_init, locations_final, 
            map_var_automaton_idx, flow, map_edges, constants, m.map_var_idx)
198
    return A
199
   
200
201
202
203
end

export create_automaton_G