diff --git a/automata/automaton_F.jl b/automata/automaton_F.jl index 19183884f5aea1133895a1b16f6c88c2057518d4..7ec52a5f3ef9f3df9ed2f0c87c02623d58a12b39 100644 --- a/automata/automaton_F.jl +++ b/automata/automaton_F.jl @@ -2,7 +2,7 @@ function create_automaton_F(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"] + locations = ["l0", "l1", "l2", "l3"] ## Invariant predicates true_inv_predicate = (A::LHA, S:: StateLHA) -> return true @@ -10,17 +10,17 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 "l2" => true_inv_predicate, "l3" => true_inv_predicate) ## Init and final loc - l_loc_init = ["l0"] - l_loc_final = ["l2"] + locations_init = ["l0"] + locations_final = ["l2"] - #S.n <=> S.l_var[A.map_var_automaton_idx["n"]] - #P <=> xn[map_var_model_idx[l_ctes[str_O]] with str_O = "P". On stock str_O dans l_ctes + #S.n <=> S.values[A.map_var_automaton_idx["n"]] + #P <=> xn[map_var_model_idx[constants[str_O]] with str_O = "P". On stock str_O dans constants # P = get_value(A, x, str_obs) ## Map of automaton variables map_var_automaton_idx = Dict{VariableAutomaton,Int}("n" => 1, "d" => 2, "isabs" => 3) ## Flow of variables - l_flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0], + flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0], "l1" => [0.0,0.0,0.0], "l2" => [0.0,0.0,0.0], "l3" => [0.0,0.0,0.0]) @@ -45,8 +45,8 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 # l1 loc : we construct the edges of the form l1 => (..) tuple = ("l1", "l2") cc_aut_F_l1l2_1(A::LHA, S::StateLHA) = - (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"]) && - (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) + (A.constants["x1"] <= S["n"] <= A.constants["x2"]) && + (A.constants["t1"] <= S.time <= A.constants["t2"]) us_aut_F_l1l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2"; S["d"] = 0) @@ -54,14 +54,14 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 cc_aut_F_l1l2_2(A::LHA, S::StateLHA) = S["d"] > 0 && - (S.time > A.l_ctes["t2"] || istrue(S["isabs"])) + (S.time > A.constants["t2"] || istrue(S["isabs"])) us_aut_F_l1l2_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2") edge2 = Edge([nothing], cc_aut_F_l1l2_2, us_aut_F_l1l2_2!) cc_aut_F_l1l2_3(A::LHA, S::StateLHA) = S["d"] == 0 && - S.time >= A.l_ctes["t1"] + S.time >= A.constants["t1"] us_aut_F_l1l2_3!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2") edge3 = Edge([nothing], cc_aut_F_l1l2_3, us_aut_F_l1l2_3!) @@ -70,27 +70,27 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 tuple = ("l1", "l3") cc_aut_F_l1l3_1(A::LHA, S::StateLHA) = - (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"]) + (A.constants["x1"] <= S["n"] <= A.constants["x2"]) us_aut_F_l1l3_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["d"] = 0;) edge1 = Edge([nothing], cc_aut_F_l1l3_1, us_aut_F_l1l3_1!) cc_aut_F_l1l3_2(A::LHA, S::StateLHA) = - (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"]) && - (S.time <= A.l_ctes["t1"]) + (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"]) && + (S.time <= A.constants["t1"]) us_aut_F_l1l3_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; - S["d"] = min(sqrt((S.time - A.l_ctes["t1"])^2 + (S["n"] - A.l_ctes["x2"])^2), - sqrt((S.time - A.l_ctes["t1"])^2 + (S["n"] - A.l_ctes["x1"])^2))) + S["d"] = min(sqrt((S.time - A.constants["t1"])^2 + (S["n"] - A.constants["x2"])^2), + sqrt((S.time - A.constants["t1"])^2 + (S["n"] - A.constants["x1"])^2))) edge2 = Edge([nothing], cc_aut_F_l1l3_2, us_aut_F_l1l3_2!) cc_aut_F_l1l3_3(A::LHA, S::StateLHA) = - (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"]) && - (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) + (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"]) && + (A.constants["t1"] <= S.time <= A.constants["t2"]) us_aut_F_l1l3_3!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; - S["d"] = min(S["d"], min(abs(S["n"] - A.l_ctes["x1"]), abs(S["n"] - A.l_ctes["x2"])))) + S["d"] = min(S["d"], min(abs(S["n"] - A.constants["x1"]), abs(S["n"] - A.constants["x2"])))) edge3 = Edge([nothing], cc_aut_F_l1l3_3, us_aut_F_l1l3_3!) map_edges[tuple] = [edge1, edge2, edge3] @@ -104,17 +104,17 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 edge1 = Edge(["ALL"], cc_aut_F_l3l1_1, us_aut_F_l3l1_1!) tuple = ("l3", "l2") cc_aut_F_l3l2_1(A::LHA, S::StateLHA) = - (S.time >= A.l_ctes["t2"]) + (S.time >= A.constants["t2"]) us_aut_F_l3l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2") edge2 = Edge([nothing], cc_aut_F_l3l2_1, us_aut_F_l3l2_1!) map_edges[tuple] = [edge1, edge2] ## Constants - l_ctes = Dict{String,Float64}("x1" => x1, "x2" => x2, "t1" => t1, "t2" => t2) + constants = 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) + A = LHA(m.transitions, locations, Λ_F, locations_init, locations_final, + map_var_automaton_idx, flow, map_edges, constants, m.map_var_idx) return A end diff --git a/automata/automaton_G.jl b/automata/automaton_G.jl index c2210988432fe6a47de48740dcc204f01ad97c42..9e1ed810d46eeffc257fd82bc5d59bbe5a129809 100644 --- a/automata/automaton_G.jl +++ b/automata/automaton_G.jl @@ -2,7 +2,7 @@ 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"] + locations = ["l0", "l1", "l2", "l3", "l4"] # Invariant predicates true_inv_predicate = (A::LHA, S:: StateLHA) -> return true @@ -11,15 +11,15 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 "l4" => true_inv_predicate) ## Init and final loc - l_loc_init = ["l0"] - l_loc_final = ["l2"] + locations_init = ["l0"] + locations_final = ["l2"] ## Map of automaton variables map_var_automaton_idx = Dict{VariableAutomaton,Int}("tprime" => 1, "in" => 2, "n" => 3, "d" => 4, "isabs" => 5) ## Flow of variables - l_flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0,0.0,0.0], + 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], @@ -45,27 +45,27 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 # l1 loc tuple = ("l1", "l3") cc_aut_G_l1l3_1(A::LHA, S::StateLHA) = - S.time <= A.l_ctes["t1"] && - S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"] + S.time <= A.constants["t1"] && + S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"] us_aut_G_l1l3_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; - S["d"] = min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); + S["d"] = min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["in"] = false) edge1 = Edge([nothing], cc_aut_G_l1l3_1, us_aut_G_l1l3_1!) 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"]) + (A.constants["t1"] <= S.time <= A.constants["t2"]) && + (A.constants["x1"] <= S["n"] <= A.constants["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"]); + S["d"] = S["d"] * (S.time - A.constants["t1"]); S["tprime"] = 0.0) edge3 = Edge([nothing], cc_aut_G_l1l3_3, us_aut_G_l1l3_3!) cc_aut_G_l1l3_2(A::LHA, S::StateLHA) = - (S.time <= A.l_ctes["t1"]) && - (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"]) + (S.time <= A.constants["t1"]) && + (A.constants["x1"] <= S["n"] <= A.constants["x2"]) us_aut_G_l1l3_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["d"] = 0; @@ -74,8 +74,8 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 cc_aut_G_l1l3_4(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"]) + (A.constants["t1"] <= S.time <= A.constants["t2"]) && + (A.constants["x1"] <= S["n"] <= A.constants["x2"]) us_aut_G_l1l3_4!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["tprime"] = 0.0) @@ -86,16 +86,16 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 tuple = ("l1", "l4") cc_aut_G_l1l4_1(A::LHA, S::StateLHA) = !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"]) + (A.constants["t1"] <= S.time <= A.constants["t2"]) && + (S["n"] < A.constants["x1"] || S["n"] > A.constants["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"])) + S["d"] += S["d"] * (S.time - A.constants["t1"])) edge1 = Edge([nothing], cc_aut_G_l1l4_1, us_aut_G_l1l4_1!) cc_aut_G_l1l4_2(A::LHA, S::StateLHA) = 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"]) + (A.constants["t1"] <= S.time <= A.constants["t2"]) && + (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"]) us_aut_G_l1l4_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l4") edge2 = Edge([nothing], cc_aut_G_l1l4_2, us_aut_G_l1l4_2!) @@ -104,31 +104,31 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 tuple = ("l1", "l2") cc_aut_G_l1l2_1(A::LHA, S::StateLHA) = istrue(S["in"]) && - S.time >= A.l_ctes["t2"] + S.time >= A.constants["t2"] us_aut_G_l1l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2") edge1 = Edge([nothing], cc_aut_G_l1l2_1, us_aut_G_l1l2_1!) cc_aut_G_l1l2_2(A::LHA, S::StateLHA) = !istrue(S["in"]) && - S.time >= A.l_ctes["t2"] + S.time >= A.constants["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"])) + S["d"] = S["d"] * (A.constants["t2"] - A.constants["t1"])) edge2 = Edge([nothing], cc_aut_G_l1l2_2, us_aut_G_l1l2_2!) cc_aut_G_l1l2_3(A::LHA, S::StateLHA) = istrue(S["isabs"]) && - S.time <= A.l_ctes["t1"] + S.time <= A.constants["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"])) + S["d"] = S["d"] * (A.constants["t2"] - A.constants["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"]) + (A.constants["t1"] <= S.time <= A.constants["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"]))) + S["d"] += (A.constants["t2"] - S.time) * + min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x1"] - S["n"]))) edge4 = Edge([nothing], cc_aut_G_l1l2_4, us_aut_G_l1l2_4!) map_edges[tuple] = [edge1, edge2, edge3, edge4] @@ -146,14 +146,14 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 tuple = ("l3", "l2") cc_aut_G_l3l2_2(A::LHA, S::StateLHA) = istrue(S["in"]) && - S.time >= A.l_ctes["t2"] + S.time >= A.constants["t2"] 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"])) + S["d"] = S["d"] * (A.constants["t2"] - A.constants["t1"])) edge2 = Edge([nothing], cc_aut_G_l3l2_2, us_aut_G_l3l2_2!) cc_aut_G_l3l2_1(A::LHA, S::StateLHA) = !istrue(S["in"]) && - S.time >= A.l_ctes["t2"] + S.time >= A.constants["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!) @@ -165,7 +165,7 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 cc_aut_G_l4l1_1(A::LHA, S::StateLHA) = true us_aut_G_l4l1_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l1"; - S["d"] += S["tprime"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); + S["d"] += S["tprime"] * min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["tprime"] = 0.0; S["n"] = get_value(A, x, str_obs); S["in"] = true; @@ -175,20 +175,20 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1 tuple = ("l4", "l2") cc_aut_G_l4l2_1(A::LHA, S::StateLHA) = - (S.time >= A.l_ctes["t2"]) + (S.time >= A.constants["t2"]) us_aut_G_l4l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2"; - S["d"] += S["tprime"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); + S["d"] += S["tprime"] * min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["tprime"] = 0.0) edge1 = Edge([nothing], cc_aut_G_l4l2_1, us_aut_G_l4l2_1!) map_edges[tuple] = [edge1] ## Constants - l_ctes = Dict{String,Float64}("x1" => x1, "x2" => x2, "t1" => t1, "t2" => t2) + constants = 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) + A = LHA(m.transitions, locations, Λ_F, locations_init, locations_final, + map_var_automaton_idx, flow, map_edges, constants, m.map_var_idx) return A end diff --git a/automata/automaton_G_F.jl b/automata/automaton_G_F.jl index 4cd8df992e930e38eac391e3cc54400d68cee9e0..d00c25ba1310d9acceab8792261c64c948d83f27 100644 --- a/automata/automaton_G_F.jl +++ b/automata/automaton_G_F.jl @@ -3,7 +3,7 @@ function create_automaton_G_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1::Float64, t2::Float64, t1::Float64, t2::Float64, str_obs::String) @assert str_obs in m.g # Locations - l_loc = ["l0", "l1", "l2", "l3", "l4"] + locations = ["l0", "l1", "l2", "l3", "l4"] # Invariant predicates true_inv_predicate = (A::LHA, S:: StateLHA) -> return true @@ -12,15 +12,15 @@ function create_automaton_G_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, "l4" => true_inv_predicate) ## Init and final loc - l_loc_init = ["l0"] - l_loc_final = ["l2"] + locations_init = ["l0"] + locations_final = ["l2"] ## Map of automaton variables map_var_automaton_idx = Dict{VariableAutomaton,Int}("t'" => 1, "in" => 2, "n" => 3, "d" => 4) ## Flow of variables - l_flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0,0.0], + flow = Dict{VariableAutomaton,Vector{Float64}}("l0" => [0.0,0.0,0.0,0.0], "l1" => [0.0,0.0,0.0,0.0], "l2" => [0.0,0.0,0.0,0.0], "l3" => [0.0,0.0,0.0,0.0], @@ -41,42 +41,42 @@ function create_automaton_G_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, # l1 loc tuple = ("l1", "l3") cc_aut_G_l1l3_1(A::LHA, S::StateLHA) = - (S.time < A.l_ctes["t1"] && (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"])) + (S.time < A.constants["t1"] && (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"])) us_aut_G_l1l3_1!(A::LHA, S::StateLHA, x::Vector{Int}) = - (S.loc = "l3"; S["d"] = min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); S["in"] = false) + (S.loc = "l3"; S["d"] = min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["in"] = false) edge1 = Edge([nothing], cc_aut_G_l1l3_1, us_aut_G_l1l3_1!) cc_aut_G_l1l3_2(A::LHA, S::StateLHA) = - (S.time < A.l_ctes["t1"] && (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"])) + (S.time < A.constants["t1"] && (A.constants["x1"] <= S["n"] <= A.constants["x2"])) us_aut_G_l1l3_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["d"] = 0; S["in"] = false) edge2 = Edge([nothing], cc_aut_G_l1l3_2, us_aut_G_l1l3_2!) cc_aut_G_l1l3_3(A::LHA, S::StateLHA) = - (!isin(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"]); S["t'"] = 0.0) + (!isin(S["in"]) && (A.constants["t1"] <= S.time <= A.constants["t2"]) && (A.constants["x1"] <= S["n"] <= A.constants["x2"])) + us_aut_G_l1l3_3!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["d"] = S["d"] * (S.time - A.constants["t1"]); S["t'"] = 0.0) edge3 = Edge([nothing], cc_aut_G_l1l3_3, us_aut_G_l1l3_3!) cc_aut_G_l1l3_4(A::LHA, S::StateLHA) = - (isin(S["in"]) && (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && (A.l_ctes["x1"] <= S["n"] <= A.l_ctes["x2"])) + (isin(S["in"]) && (A.constants["t1"] <= S.time <= A.constants["t2"]) && (A.constants["x1"] <= S["n"] <= A.constants["x2"])) us_aut_G_l1l3_4!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l3"; S["t'"] = 0.0) edge4 = Edge([nothing], cc_aut_G_l1l3_4, us_aut_G_l1l3_4!) map_edges[tuple] = [edge1, edge2, edge3, edge4] tuple = ("l1", "l4") cc_aut_G_l1l4_1(A::LHA, S::StateLHA) = - (!isin(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"])) + (!isin(S["in"]) && (A.constants["t1"] <= S.time <= A.constants["t2"]) && (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"])) + us_aut_G_l1l4_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l4"; S["d"] += S["d"] * (S.time - A.constants["t1"])) edge1 = Edge([nothing], cc_aut_G_l1l4_1, us_aut_G_l1l4_1!) cc_aut_G_l1l4_2(A::LHA, S::StateLHA) = - (isin(S["in"]) && (A.l_ctes["t1"] <= S.time <= A.l_ctes["t2"]) && (S["n"] < A.l_ctes["x1"] || S["n"] > A.l_ctes["x2"])) + (isin(S["in"]) && (A.constants["t1"] <= S.time <= A.constants["t2"]) && (S["n"] < A.constants["x1"] || S["n"] > A.constants["x2"])) us_aut_G_l1l4_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l4") edge2 = Edge([nothing], cc_aut_G_l1l4_2, us_aut_G_l1l4_2!) map_edges[tuple] = [edge1, edge2] tuple = ("l1", "l2") - cc_aut_G_l1l2_1(A::LHA, S::StateLHA) = (isin(S["in"]) && S.time >= A.l_ctes["t2"]) + cc_aut_G_l1l2_1(A::LHA, S::StateLHA) = (isin(S["in"]) && S.time >= A.constants["t2"]) us_aut_G_l1l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2") edge1 = Edge([nothing], cc_aut_G_l1l2_1, us_aut_G_l1l2_1!) - cc_aut_G_l1l2_2(A::LHA, S::StateLHA) = (!isin(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"])) + cc_aut_G_l1l2_2(A::LHA, S::StateLHA) = (!isin(S["in"]) && S.time >= A.constants["t2"]) + us_aut_G_l1l2_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2"; S["d"] = S["d"] * (A.constants["t2"] - A.constants["t1"])) edge2 = Edge([nothing], cc_aut_G_l1l2_2, us_aut_G_l1l2_2!) map_edges[tuple] = [edge1, edge2] @@ -88,11 +88,11 @@ function create_automaton_G_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, map_edges[tuple] = [edge1] tuple = ("l3", "l2") - cc_aut_G_l3l2_1(A::LHA, S::StateLHA) = (isin(S["in"]) && S.time >= A.l_ctes["t2"]) + cc_aut_G_l3l2_1(A::LHA, S::StateLHA) = (isin(S["in"]) && S.time >= A.constants["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!) - cc_aut_G_l3l2_2(A::LHA, S::StateLHA) = (!isin(S["in"]) && S.time >= A.l_ctes["t2"]) - 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"])) + cc_aut_G_l3l2_2(A::LHA, S::StateLHA) = (!isin(S["in"]) && S.time >= A.constants["t2"]) + us_aut_G_l3l2_2!(A::LHA, S::StateLHA, x::Vector{Int}) = (S.loc = "l2"; S["d"] = S["d"] * (A.constants["t2"] - A.constants["t1"])) edge2 = Edge([nothing], cc_aut_G_l3l2_2, us_aut_G_l3l2_2!) map_edges[tuple] = [edge1, edge2] @@ -100,23 +100,23 @@ function create_automaton_G_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, tuple = ("l4", "l1") cc_aut_G_l4l1_1(A::LHA, S::StateLHA) = true us_aut_G_l4l1_1!(A::LHA, S::StateLHA, x::Vector{Int}) = - (S.loc = "l1"; S["d"] += S["t'"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); + (S.loc = "l1"; S["d"] += S["t'"] * min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["t'"] = 0.0; S["n"] = get_value(A, x, str_obs); S["in"] = true) edge1 = Edge(["ALL"], cc_aut_G_l4l1_1, us_aut_G_l4l1_1!) map_edges[tuple] = [edge1] tuple = ("l4", "l2") - cc_aut_G_l4l2_1(A::LHA, S::StateLHA) = (S.time >= A.l_ctes["t2"]) + cc_aut_G_l4l2_1(A::LHA, S::StateLHA) = (S.time >= A.constants["t2"]) us_aut_G_l4l2_1!(A::LHA, S::StateLHA, x::Vector{Int}) = - (S.loc = "l2"; S["d"] += S["t'"] * min(abs(A.l_ctes["x1"] - S["n"]), abs(A.l_ctes["x2"] - S["n"])); S["t'"] = 0.0) + (S.loc = "l2"; S["d"] += S["t'"] * min(abs(A.constants["x1"] - S["n"]), abs(A.constants["x2"] - S["n"])); S["t'"] = 0.0) edge1 = Edge([nothing], cc_aut_G_l4l2_1, us_aut_G_l4l2_1!) map_edges[tuple] = [edge1] ## Constants - l_ctes = Dict{String,Float64}("x1" => x1, "x2" => x2, "t1" => t1, "t2" => t2) + constants = 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) + A = LHA(m.transitions, locations, Λ_F, locations_init, locations_final, + map_var_automaton_idx, flow, map_edges, constants, m.map_var_idx) return A end diff --git a/core/_tests_simulate.jl b/core/_tests_simulate.jl index b5bd48051690186bb88827d3098298cbf2459816..9279efd0cd070ecaa07bc0f1cbeae3c1d522f425 100644 --- a/core/_tests_simulate.jl +++ b/core/_tests_simulate.jl @@ -29,7 +29,7 @@ _get_value_row(σ::OldTrajectory, var::String, idx::Int) = function _simulate_col(m::ContinuousTimeModel) # trajectory fields - full_values = zeros(m.d, 0) + full_values = zeros(m.dim_state, 0) times = zeros(0) transitions = Vector{Union{String,Nothing}}(undef,0) # values at time n @@ -38,7 +38,7 @@ function _simulate_col(m::ContinuousTimeModel) tn = m.t0 tr = [""] # at time n+1 - xnplus1 = zeros(Int, m.d) + xnplus1 = zeros(Int, m.dim_state) tnplus1 = zeros(Float64, 1) isabsorbing = (m.isabsorbing(m.p,xn))::Bool while !isabsorbing && (tn <= m.time_bound) @@ -63,7 +63,7 @@ end function _simulate_row(m::ContinuousTimeModel) # trajectory fields - full_values = zeros(m.d, 0) + full_values = zeros(m.dim_state, 0) times = zeros(0) transitions = Vector{Union{String,Nothing}}(undef,0) # values at time n @@ -72,7 +72,7 @@ function _simulate_row(m::ContinuousTimeModel) tn = m.t0 tr = [""] # at time n+1 - xnplus1 = zeros(Int, m.d) + xnplus1 = zeros(Int, m.dim_state) tnplus1 = zeros(Float64, 1) isabsorbing = (m.isabsorbing(m.p,xn))::Bool while !isabsorbing && (tn <= m.time_bound) @@ -98,7 +98,7 @@ end function _simulate_col_buffer(m::ContinuousTimeModel; buffer_size::Int = 5) # trajectory fields - full_values = zeros(m.d, 0) + full_values = zeros(m.dim_state, 0) times = zeros(0) transitions = Vector{Union{String,Nothing}}(undef,0) # values at time n @@ -106,7 +106,7 @@ function _simulate_col_buffer(m::ContinuousTimeModel; buffer_size::Int = 5) xn = @view m.x0[:] tn = m.t0 # at time n+1 - mat_x = zeros(Int, m.d, buffer_size) + mat_x = zeros(Int, m.dim_state, buffer_size) l_t = zeros(Float64, buffer_size) l_tr = Vector{Union{String,Nothing}}(undef, buffer_size) isabsorbing = m.isabsorbing(m.p,xn)::Bool @@ -138,7 +138,7 @@ end function _simulate_row_buffer(m::ContinuousTimeModel; buffer_size::Int = 5) # trajectory fields - full_values = zeros(0, m.d) + full_values = zeros(0, m.dim_state) times = zeros(0) transitions = Vector{Union{String,Nothing}}(undef,0) # values at time n @@ -146,7 +146,7 @@ function _simulate_row_buffer(m::ContinuousTimeModel; buffer_size::Int = 5) xn = @view m.x0[:] tn = m.t0 # at time n+1 - mat_x = zeros(Int, buffer_size, m.d) + mat_x = zeros(Int, buffer_size, m.dim_state) l_t = zeros(Float64, buffer_size) l_tr = Vector{Union{String,Nothing}}(undef, buffer_size) isabsorbing = m.isabsorbing(m.p,xn)::Bool @@ -178,7 +178,7 @@ end function _simulate_without_view(m::ContinuousTimeModel) # trajectory fields - full_values = Matrix{Int}(undef, 1, m.d) + full_values = Matrix{Int}(undef, 1, m.dim_state) full_values[1,:] = m.x0 times = Float64[m.t0] transitions = Union{String,Nothing}[nothing] @@ -187,7 +187,7 @@ function _simulate_without_view(m::ContinuousTimeModel) xn = @view m.x0[:] tn = m.t0 # at time n+1 - mat_x = zeros(Int, m.buffer_size, m.d) + mat_x = zeros(Int, m.buffer_size, m.dim_state) l_t = zeros(Float64, m.buffer_size) l_tr = Vector{Union{String,Nothing}}(undef, m.buffer_size) isabsorbing = m.isabsorbing(m.p,xn)::Bool @@ -212,7 +212,7 @@ function _simulate_without_view(m::ContinuousTimeModel) times[end] = m.time_bound transitions[end] = nothing else - full_values = vcat(full_values, reshape(full_values[end,:], 1, m.d)) + full_values = vcat(full_values, reshape(full_values[end,:], 1, m.dim_state)) push!(times, m.time_bound) push!(transitions, nothing) end @@ -224,16 +224,16 @@ end # With trajectory values in Matrix type function _simulate_27d56(m::ContinuousTimeModel) # trajectory fields - full_values = Matrix{Int}(undef, 1, m.d) + full_values = Matrix{Int}(undef, 1, m.dim_state) full_values[1,:] = m.x0 times = Float64[m.t0] transitions = Union{String,Nothing}[nothing] # values at time n n = 0 - xn = view(reshape(m.x0, 1, m.d), 1, :) # View for type stability + xn = view(reshape(m.x0, 1, m.dim_state), 1, :) # View for type stability tn = m.t0 # at time n+1 - mat_x = zeros(Int, m.buffer_size, m.d) + mat_x = zeros(Int, m.buffer_size, m.dim_state) l_t = zeros(Float64, m.buffer_size) l_tr = Vector{Union{String,Nothing}}(undef, m.buffer_size) isabsorbing::Bool = m.isabsorbing(m.p,xn) @@ -265,7 +265,7 @@ function _simulate_27d56(m::ContinuousTimeModel) else end =# - full_values = vcat(full_values, reshape(full_values[end,:], 1, m.d)) + full_values = vcat(full_values, reshape(full_values[end,:], 1, m.dim_state)) push!(times, m.time_bound) push!(transitions, nothing) end @@ -276,17 +276,17 @@ end function _simulate_d7458(m::ContinuousTimeModel) # trajectory fields - full_values = Vector{Vector{Int}}(undef, m.d) - for i = 1:m.d full_values[i] = Int[m.x0[i]] end - for i = 1:m.d sizehint!(full_values[i], m.estim_min_states) end + full_values = Vector{Vector{Int}}(undef, m.dim_state) + for i = 1:m.dim_state full_values[i] = Int[m.x0[i]] end + for i = 1:m.dim_state sizehint!(full_values[i], m.estim_min_states) end times = Float64[m.t0] transitions = Transition[nothing] # values at time n n = 0 - xn = view(reshape(m.x0, 1, m.d), 1, :) # View for type stability + xn = view(reshape(m.x0, 1, m.dim_state), 1, :) # View for type stability tn = m.t0 # at time n+1 - mat_x = zeros(Int, m.buffer_size, m.d) + mat_x = zeros(Int, m.buffer_size, m.dim_state) l_t = zeros(Float64, m.buffer_size) l_tr = Vector{Union{String,Nothing}}(undef, m.buffer_size) isabsorbing::Bool = m.isabsorbing(m.p,xn) @@ -311,20 +311,20 @@ function _simulate_d7458(m::ContinuousTimeModel) if end_idx != -1 break end - for k = 1:m.d + for k = 1:m.dim_state append!(full_values[k], view(mat_x, :, k)) end append!(times, l_t) append!(transitions, l_tr) n += m.buffer_size end - for k = 1:m.d + for k = 1:m.dim_state append!(full_values[k], view(mat_x, 1:end_idx, k)) end append!(times, view(l_t, 1:end_idx)) append!(transitions, view(l_tr, 1:end_idx)) if isbounded(m) - for k = 1:m.d + for k = 1:m.dim_state push!(full_values[k], full_values[k][end]) end push!(times, m.time_bound) diff --git a/core/common.jl b/core/common.jl index 1b878b932c6dfc9bc16f82a20e13bcbc0fed8318..e283f8330e438d2c63827436463205b358c51183 100644 --- a/core/common.jl +++ b/core/common.jl @@ -10,18 +10,18 @@ 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} + dim_state::Int # state space dim + dim_params::Int # parameter space dim + map_var_idx::Dict{String,Int} # maps variable str to index in the state space + _map_obs_var_idx::Dict{String,Int} # maps variable str to index in the observed state space + map_param_idx::Dict{String,Int} # maps parameter str to index in the parameter space + 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 + g::Vector{String} # of dimension dim_obs_state + _g_idx::Vector{Int} # of dimension dim_obs_state isabsorbing::Function time_bound::Float64 buffer_size::Int @@ -42,22 +42,22 @@ struct Edge end struct LHA - l_transitions::Vector{Transition} - l_loc::Vector{Location} + transitions::Vector{Transition} + locations::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 + locations_init::Vector{Location} + locations_final::Vector{Location} + map_var_automaton_idx::Dict{VariableAutomaton,Int} # nvar keys : str_var => idx in values + flow::Dict{Location,Vector{Float64}} # output of length nvar map_edges::Dict{Tuple{Location,Location},Vector{Edge}} - l_ctes::Dict{String,Float64} + constants::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} + values::Vector{Float64} time::Float64 end @@ -67,7 +67,7 @@ mutable struct SynchronizedModel <: Model end struct SynchronizedTrajectory <: AbstractTrajectory - S::StateLHA + state_lha_end::StateLHA m::SynchronizedModel values::Vector{Vector{Int}} times::Vector{Float64} @@ -76,20 +76,20 @@ end struct ParametricModel m::Model - l_param::Vector{String} - dist::Distribution + params::Vector{String} + distribution::Distribution _param_idx::Vector{Int} end # Constructors -function ContinuousTimeModel(d::Int, k::Int, map_var_idx::Dict, map_param_idx::Dict, l_transitions::Vector{String}, +function ContinuousTimeModel(dim_state::Int, dim_params::Int, map_var_idx::Dict, map_param_idx::Dict, transitions::Vector{String}, p::Vector{Float64}, x0::Vector{Int}, t0::Float64, f!::Function, isabsorbing::Function; g::Vector{String} = keys(map_var_idx), time_bound::Float64 = Inf, buffer_size::Int = 10, estim_min_states::Int = 50) - dobs = length(g) + dim_obs_state = length(g) _map_obs_var_idx = Dict() - _g_idx = Vector{Int}(undef, dobs) - for i = 1:dobs + _g_idx = Vector{Int}(undef, dim_obs_state) + for i = 1:dim_obs_state _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 @@ -100,40 +100,40 @@ function ContinuousTimeModel(d::Int, k::Int, map_var_idx::Dict, map_param_idx::D if length(methods(isabsorbing)) >= 2 @warn "You have possibly redefined a function Model.isabsorbing used in a previously instantiated model." end - new_model = 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) + new_model = ContinuousTimeModel(dim_state, dim_params, map_var_idx, _map_obs_var_idx, map_param_idx, transitions, p, x0, t0, f!, g, _g_idx, isabsorbing, time_bound, buffer_size, estim_min_states) @assert check_consistency(new_model) return new_model 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) +LHA(A::LHA, map_var::Dict{String,Int}) = LHA(A.transitions, A.locations, A.Λ, + A.locations_init, A.locations_final, A.map_var_automaton_idx, A.flow, + A.map_edges, A.constants, map_var) Base.:*(m::ContinuousTimeModel, A::LHA) = SynchronizedModel(m, A) Base.:*(A::LHA, m::ContinuousTimeModel) = SynchronizedModel(m, A) function ParametricModel(am::Model, priors::Tuple{String,UnivariateDistribution}...) m = get_proba_model(am) - l_param = String[] - l_dist = Distribution{Univariate,Continuous}[] + params = String[] + distributions = Distribution{Univariate,Continuous}[] _param_idx = zeros(Int, 0) for prior in priors check_vars = true str_p = prior[1] - dist = prior[2] + distribution = prior[2] @assert str_p in keys(m.map_param_idx) - push!(l_param, str_p) - push!(l_dist, dist) + push!(params, str_p) + push!(distributions, distribution) push!(_param_idx, m.map_param_idx[str_p]) end - return ParametricModel(am, l_param, product_distribution(l_dist), _param_idx) + return ParametricModel(am, params, product_distribution(distributions), _param_idx) end -function ParametricModel(am::Model, l_param::Vector{String}, dist::MultivariateDistribution) +function ParametricModel(am::Model, params::Vector{String}, distribution::MultivariateDistribution) m = get_proba_model(am) _param_idx = zeros(Int, 0) - for str_p in l_param + for str_p in params push!(_param_idx, m.map_param_idx[str_p]) end - return ParametricModel(am, l_param, dist, _param_idx) + return ParametricModel(am, params, distribution, _param_idx) end diff --git a/core/lha.jl b/core/lha.jl index af93550c86add7222ce30f06ee556bf24651bfd7..a9920ec84d81a2be2186fbbecc1c490d76616f21 100644 --- a/core/lha.jl +++ b/core/lha.jl @@ -2,12 +2,12 @@ length_var(A::LHA) = length(A.map_var_automaton_idx) get_value(A::LHA, x::Vector{Int}, var::String) = x[A.map_var_model_idx[var]] -copy(S::StateLHA) = StateLHA(S.A, S.loc, S.l_var, S.time) +copy(S::StateLHA) = StateLHA(S.A, S.loc, S.values, 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) -setindex!(S::StateLHA, val::Bool, var::VariableAutomaton) = (S.l_var)[(S.A).map_var_automaton_idx[var]] = convert(Float64, val) +getindex(S::StateLHA, var::VariableAutomaton) = (S.values)[(S.A).map_var_automaton_idx[var]] +setindex!(S::StateLHA, val::Float64, var::VariableAutomaton) = (S.values)[(S.A).map_var_automaton_idx[var]] = val +setindex!(S::StateLHA, val::Int, var::VariableAutomaton) = (S.values)[(S.A).map_var_automaton_idx[var]] = convert(Float64, val) +setindex!(S::StateLHA, val::Bool, var::VariableAutomaton) = (S.values)[(S.A).map_var_automaton_idx[var]] = convert(Float64, val) function Base.show(io::IO, S::StateLHA) print(io, "State of LHA\n") @@ -15,25 +15,25 @@ function Base.show(io::IO, S::StateLHA) 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]) (idx = $idx)\n") + print(io, "* $var = $(S.values[idx]) (idx = $idx)\n") end end function Base.copyto!(Sdest::StateLHA, Ssrc::StateLHA) Sdest.A = Ssrc.A Sdest.loc = Ssrc.loc - for i = eachindex(Sdest.l_var) - Sdest.l_var[i] = Ssrc.l_var[i] + for i = eachindex(Sdest.values) + Sdest.values[i] = Ssrc.values[i] end Sdest.time = Ssrc.time end -isaccepted(S::StateLHA) = (S.loc in (S.A).l_loc_final) +isaccepted(S::StateLHA) = (S.loc in (S.A).locations_final) # Methods for synchronize / read the trajectory function init_state(A::LHA, x0::Vector{Int}, t0::Float64) S0 = StateLHA(A, "", zeros(length_var(A)), t0) - for loc in A.l_loc_init + for loc in A.locations_init if A.Λ[loc](A,S0) S0.loc = loc break @@ -44,7 +44,7 @@ end function _find_edge_candidates!(edge_candidates::Vector{Edge}, current_loc::Location, A::LHA, Snplus1::StateLHA, only_asynchronous::Bool) - for loc in A.l_loc + for loc in A.locations tuple_edges = (current_loc, loc) if haskey(A.map_edges, tuple_edges) for edge in A.map_edges[tuple_edges] @@ -142,10 +142,10 @@ function next_state!(Snplus1::StateLHA, A::LHA, println("Time flies with the flow...") end # Now time flies according to the flow - for i in eachindex(Snplus1.l_var) - coeff_deriv = (A.l_flow[Snplus1.loc])[i] + for i in eachindex(Snplus1.values) + coeff_deriv = (A.flow[Snplus1.loc])[i] if coeff_deriv > 0 - Snplus1.l_var[i] += coeff_deriv*(tnplus1 - Snplus1.time) + Snplus1.values[i] += coeff_deriv*(tnplus1 - Snplus1.time) end end Snplus1.time = tnplus1 @@ -196,7 +196,7 @@ end # For tests purposes function read_trajectory(A::LHA, σ::Trajectory; verbose = false) - @assert σ.m.d == σ.m.dobs # Model should be entirely obserbed + @assert (σ.m).dim_state == σ.m.dim_obs_state # Model should be entirely obserbed A_new = LHA(A, (σ.m)._map_obs_var_idx) l_t = times(σ) l_tr = transitions(σ) @@ -207,7 +207,7 @@ function read_trajectory(A::LHA, σ::Trajectory; verbose = false) for n in 1:length_states(σ) next_state!(Snplus1, A_new, σ[n], l_t[n], l_tr[n], Sn; verbose = verbose) copyto!(Sn, Snplus1) - if Snplus1.loc in A_new.l_loc_final + if Snplus1.loc in A_new.locations_final break end end diff --git a/core/model.jl b/core/model.jl index 75874f1e159f5fa62cf18624efc349d596d780c1..894a7a31f74e860a33643b7cce0ba6deb2ed1c99 100644 --- a/core/model.jl +++ b/core/model.jl @@ -36,7 +36,7 @@ function simulate(m::ContinuousTimeModel; p::Union{Nothing,AbstractVector{Float6 p_sim = p end # First alloc - full_values = Vector{Vector{Int}}(undef, m.d) + full_values = Vector{Vector{Int}}(undef, m.dim_state) for i = eachindex(full_values) full_values[i] = zeros(Int, m.estim_min_states) end times = zeros(Float64, m.estim_min_states) transitions = Vector{Transition}(undef, m.estim_min_states) @@ -60,7 +60,7 @@ function simulate(m::ContinuousTimeModel; p::Union{Nothing,AbstractVector{Float6 return Trajectory(m, values, times, transitions) end # Alloc of vectors where we stock n+1 values - vec_x = zeros(Int, m.d) + vec_x = zeros(Int, m.dim_state) l_t = Float64[0.0] l_tr = Transition[nothing] # First we fill the allocated array @@ -134,7 +134,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl p_sim = p end # First alloc - full_values = Vector{Vector{Int}}(undef, m.d) + full_values = Vector{Vector{Int}}(undef, m.dim_state) for i = eachindex(full_values) full_values[i] = zeros(Int, m.estim_min_states) end times = zeros(Float64, m.estim_min_states) transitions = Vector{Transition}(undef, m.estim_min_states) @@ -151,7 +151,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl isabsorbing::Bool = m.isabsorbing(p_sim,xn) isacceptedLHA::Bool = isaccepted(Sn) # Alloc of vectors where we stock n+1 values - vec_x = zeros(Int, m.d) + vec_x = zeros(Int, m.dim_state) l_t = Float64[0.0] l_tr = Transition[nothing] Snplus1 = copy(Sn) @@ -269,7 +269,7 @@ function volatile_simulate(product::SynchronizedModel; isabsorbing::Bool = m.isabsorbing(p_sim,xn) isacceptedLHA::Bool = isaccepted(Sn) # Alloc of vectors where we stock n+1 values - vec_x = zeros(Int, m.d) + vec_x = zeros(Int, m.dim_state) l_t = Float64[0.0] l_tr = Transition[nothing] Snplus1 = copy(Sn) @@ -309,7 +309,7 @@ end Simulates the model contained in pm with p_prior values. It simulates the model by taking the parameters contained in get_proba_model(pm).p and -replace the 1D parameters pm.l_param with p_prior. +replace the 1D parameters pm.params with p_prior. """ function simulate(pm::ParametricModel, p_prior::AbstractVector{Float64}) full_p = copy(get_proba_model(pm).p) @@ -338,7 +338,9 @@ Distribute over workers the computation of the mean value of a LHA over `nbr_sim` simulations of the model. """ function distribute_mean_value_lha(sm::SynchronizedModel, str_var::String, nbr_sim::Int) - sum_val = @distributed (+) for i = 1:nbr_sim volatile_simulate(sm)[str_var] end + sum_val = @distributed (+) for i = 1:nbr_sim + volatile_simulate(sm)[str_var] + end return sum_val / nbr_sim end @@ -351,17 +353,19 @@ function mean_value_lha(sm::SynchronizedModel, str_var::String, nbr_sim::Int) end function distribute_prob_accept_lha(sm::SynchronizedModel, nbr_sim::Int) - sum_val = @distributed (+) for i = 1:nbr_sim Int(isaccepted(volatile_simulate(sm))) end + sum_val = @distributed (+) for i = 1:nbr_sim + Int(isaccepted(volatile_simulate(sm))) + end return sum_val / nbr_sim end isbounded(m::Model) = get_proba_model(m).time_bound < Inf function check_consistency(m::ContinuousTimeModel) - @assert m.d == length(m.map_var_idx) - @assert m.d == length(m.x0) - @assert m.k == length(m.map_param_idx) - @assert m.k == length(m.p) - @assert length(m.g) <= m.d + @assert m.dim_state == length(m.map_var_idx) + @assert m.dim_state == length(m.x0) + @assert m.dim_params == length(m.map_param_idx) + @assert m.dim_params == length(m.p) + @assert length(m.g) <= m.dim_state @assert length(m._g_idx) == length(m.g) @assert m.buffer_size >= 0 @assert typeof(m.isabsorbing(m.p, m.x0)) == Bool @@ -371,10 +375,10 @@ end # Set and get Model fields function set_observed_var!(am::Model, g::Vector{String}) m = get_proba_model(am) - dobs = length(g) + dim_obs_state = length(g) _map_obs_var_idx = Dict{String}{Int}() - _g_idx = zeros(Int, dobs) - for i = 1:dobs + _g_idx = zeros(Int, dim_obs_state) + for i = 1:dim_obs_state _g_idx[i] = m.map_var_idx[g[i]] # = ( (g[i] = i-th obs var)::String => idx in state space ) _map_obs_var_idx[g[i]] = i end @@ -384,8 +388,8 @@ function set_observed_var!(am::Model, g::Vector{String}) end function observe_all!(am::Model) m = get_proba_model(am) - g = Vector{String}(undef, m.d) - _g_idx = collect(1:m.d) + g = Vector{String}(undef, m.dim_state) + _g_idx = collect(1:m.dim_state) for var in keys(m.map_var_idx) g[m.map_var_idx[var]] = var end @@ -395,7 +399,7 @@ function observe_all!(am::Model) end function set_param!(am::Model, new_p::Vector{Float64}) m = get_proba_model(am) - @assert length(new_p) == m.k + @assert length(new_p) == m.dim_params m.p = new_p end function set_param!(am::Model, name_p::String, p_i::Float64) @@ -411,7 +415,7 @@ function set_param!(am::Model, l_name_p::Vector{String}, p::Vector{Float64}) end function set_x0!(am::Model, new_x0::Vector{Int}) m = get_proba_model(am) - @assert length(new_x0) == m.d + @assert length(new_x0) == m.dim_state m.x0 = new_x0 end set_time_bound!(am::Model, b::Float64) = (get_proba_model(am).time_bound = b) @@ -423,7 +427,7 @@ function getindex(am::Model, name_p::String) m.p[m.map_param_idx[name_p]] end function getproperty(m::ContinuousTimeModel, sym::Symbol) - if sym == :dobs + if sym == :dim_obs_state return length(m.g) else return getfield(m, sym) @@ -431,7 +435,7 @@ function getproperty(m::ContinuousTimeModel, sym::Symbol) end function getproperty(pm::ParametricModel, sym::Symbol) if sym == :df - return length(pm.l_param) + return length(pm.params) else return getfield(pm, sym) end @@ -450,13 +454,13 @@ get_observed_var(m::Model) = get_proba_model(am).g Draw a parameter from the prior disitribution defined in `pm::ParametricModel` and save it in the model contained in `pm`. """ -draw_model!(pm::ParametricModel) = set_param!(get_proba_model(pm), pm.l_param, rand(pm.dist)) +draw_model!(pm::ParametricModel) = set_param!(get_proba_model(pm), pm.params, rand(pm.distribution)) """ `draw!(vec_p, pm)` Draw a parameter from the prior distribution defined in pm and stores it in vec_p. """ -draw!(vec_p::AbstractVector{Float64}, pm::ParametricModel) = rand!(pm.dist, vec_p) +draw!(vec_p::AbstractVector{Float64}, pm::ParametricModel) = rand!(pm.distribution, vec_p) """ `draw!(mat_p, pm)` @@ -473,7 +477,7 @@ end Computes the density of the prior distribution defined in pm on argument p_prior. """ -prior_pdf(pm::ParametricModel, p_prior::AbstractVector{Float64}) = pdf(pm.dist, p_prior) +prior_pdf(pm::ParametricModel, p_prior::AbstractVector{Float64}) = pdf(pm.distribution, p_prior) """ `prior_pdf(vec_res, mat_p, pm)` @@ -486,7 +490,7 @@ function prior_pdf!(res_pdf::AbstractVector{Float64}, pm::ParametricModel, mat_p end end fill!(pm::ParametricModel, p_prior::AbstractVector{Float64}) = get_proba_model(pm).p[pm._param_idx] = p_prior -insupport(pm::ParametricModel, p_prior::AbstractVector{Float64}) = insupport(pm.dist, p_prior) +insupport(pm::ParametricModel, p_prior::AbstractVector{Float64}) = insupport(pm.distribution, p_prior) # to do: simulate(pm), create_res_dir, check_bounds, ajouter un champ _param_idx pour pm. # diff --git a/core/plots.jl b/core/plots.jl index 898267c7cbf167d0fe3760330dc466ee473192c5..6dbd44e4855703ab020499a9e1ad35b324705525 100644 --- a/core/plots.jl +++ b/core/plots.jl @@ -51,7 +51,7 @@ function plot(σ::AbstractTrajectory, vars::String...; filename::String = "", pl end function plot!(A::LHA) - x1, x2, t1, t2 = A.l_ctes["x1"], A.l_ctes["x2"], A.l_ctes["t1"], A.l_ctes["t2"] + x1, x2, t1, t2 = A.constants["x1"], A.constants["x2"], A.constants["t1"], A.constants["t2"] plot!(Shape([(t1,x1), (t1,x2), (t2,x2), (t2,x1), (t1,x1)]), opacity = 0.5) end diff --git a/core/trajectory.jl b/core/trajectory.jl index 89a79715da1a119e548134fa432736189c35b2d3..a337415eebb1d35edcfe31f800122ff9c46b5348 100644 --- a/core/trajectory.jl +++ b/core/trajectory.jl @@ -151,7 +151,7 @@ end function check_consistency(σ::AbstractTrajectory) test_length_var = true - for i = 1:get_proba_model(σ.m).dobs + for i = 1:get_proba_model(σ.m).dim_obs_state test_length_i = (length(σ.values[1]) == length(σ.values[i])) test_length_var = test_length_var && test_length_i end @@ -159,7 +159,7 @@ function check_consistency(σ::AbstractTrajectory) (length(σ.times) == length(σ.values[1])) && test_length_var end - @assert length_obs_var(σ) == get_proba_model(σ.m).dobs + @assert length_obs_var(σ) == get_proba_model(σ.m).dim_obs_state return true end @@ -168,7 +168,7 @@ length_states(σ::AbstractTrajectory) = length(σ.times) length_obs_var(σ::AbstractTrajectory) = length(σ.values) get_obs_var(σ::AbstractTrajectory) = get_proba_model(σ.m).g isbounded(σ::AbstractTrajectory) = σ.transitions[end] == nothing && length_states(σ) >= 2 -isaccepted(σ::SynchronizedTrajectory) = isaccepted(σ.S) +isaccepted(σ::SynchronizedTrajectory) = isaccepted(σ.state_lha_end) issteadystate(σ::AbstractTrajectory) = @warn "Unimplemented" # Access to trajectory values diff --git a/tests/automata/sync_simulate_last_state_F.jl b/tests/automata/sync_simulate_last_state_F.jl index 0b4ae2b2dce2e19d8ba72a1fb1d7345219932a2e..ae6ed61f2b6267cbf4b1fcb8e4dc3067246a940b 100644 --- a/tests/automata/sync_simulate_last_state_F.jl +++ b/tests/automata/sync_simulate_last_state_F.jl @@ -11,15 +11,15 @@ sync_SIR = A_F * SIR function test_last_state(A::LHA, m::SynchronizedModel) σ = simulate(m) - test = (get_state_from_time(σ, (σ.S).time)[1] == (σ.S)["n"]) && ((σ.S)["d"] == 0) + test = (get_state_from_time(σ, (σ.state_lha_end).time)[1] == (σ.state_lha_end)["n"]) && ((σ.state_lha_end)["d"] == 0) if !test - @show σ.S + @show σ.state_lha_end @show times(σ)[end] @show σ[end] @show times(σ)[end-1] @show σ[end-1] - @show get_state_from_time(σ, (σ.S).time)[1] - @show (σ.S)["n"], (σ.S)["d"] + @show get_state_from_time(σ, (σ.state_lha_end).time)[1] + @show (σ.state_lha_end)["n"], (σ.state_lha_end)["d"] error("Ouch") end return test diff --git a/tests/automata/sync_simulate_last_state_G.jl b/tests/automata/sync_simulate_last_state_G.jl index 2a891db55ad3edcf365fae433f8aee03c1e4e803..d05d3caed9d4a3053e8c05eef262806a300c5560 100644 --- a/tests/automata/sync_simulate_last_state_G.jl +++ b/tests/automata/sync_simulate_last_state_G.jl @@ -11,7 +11,7 @@ sync_ER = A_G * ER function test_last_state(A::LHA, m::SynchronizedModel) σ = simulate(m) - test = (get_state_from_time(σ, (σ.S).time)[1] == (σ.S)["n"]) && ((σ.S)["d"] == 0) + test = (get_state_from_time(σ, (σ.state_lha_end).time)[1] == (σ.state_lha_end)["n"]) && ((σ.state_lha_end)["d"] == 0) return test end diff --git a/tests/unit/side_effects_1.jl b/tests/unit/side_effects_1.jl index cd45dfbe081dfe8614609d301d879e2f89a29dfb..f9d781b69e74198ded34f1119dc3d5b21e8ae620 100644 --- a/tests/unit/side_effects_1.jl +++ b/tests/unit/side_effects_1.jl @@ -6,7 +6,7 @@ load_model("ER") test = SIR.map_var_idx !== ER.map_var_idx && SIR.map_param_idx !== ER.map_var_idx && - SIR.l_transitions !== ER.l_transitions && + SIR.transitions !== ER.transitions && SIR.g !== ER.g && SIR._g_idx !== ER._g_idx && SIR.f! != ER.f! &&