Commit 00b622f3 authored by Bentriou Mahmoud's avatar Bentriou Mahmoud
Browse files

1) Detection of a bug when two synchronized models with two different

automata and proba moels are differents. This bug was introduced after
we change the structure of automata in order to better distributed
programming.
It leads to a restructuration of automata. In fine, the structure is
more stable and consistent. It is possible it is more computationally
efficient, but for now it is not well tested.

2) Creation of functions that collects data along with a timeline
over a trajectory + unit test vectorize.

3) Very small fix in plots and abc smc.
parent a90b2eda
......@@ -165,12 +165,12 @@ function _abc_smc(pm::ParametricModel, nbr_particles::Int, alpha::Float64,
normalize!(wl_current, 1)
@info "End"
end
current_time = time()
@info "After this step, time spent and number of simulations" steptime=(current_time-begin_time_ite) step_nbr_sim
mat_p_old = copy(mat_p)
wl_old = copy(wl_current)
fill!(l_nbr_sim, 0)
flush(stdout)
current_time = time()
old_epsilon = epsilon
end
......@@ -273,13 +273,13 @@ function _distributed_abc_smc(pm::ParametricModel, nbr_particles::Int, alpha::Fl
normalize!(wl_current, 1)
@info "End"
end
current_time = time()
@info "After this step, time spent and number of simulations" steptime=(current_time-begin_time_ite) step_nbr_sim
mat_p_old = mat_p
wl_old = wl_current
vec_dist = convert(Array, d_vec_dist)
fill!(l_nbr_sim, 0)
flush(stdout)
current_time = time()
old_epsilon = epsilon
end
......
@everywhere istrue(val::Float64) = convert(Bool, val)
# Invariant predicates functions
@everywhere true_inv_predicate(x::Vector{Int}) = true
# Check constraints and update state functions
# l0 loc : we construct the edges of the form l0 => (..)
# "cc" as check_constraints and "us" as update_state
# l0 => l1
@everywhere cc_aut_F_l0l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
# us_aut_F_l0l1_1! inside create_automaton_F
# l1 loc
# l1 => l2
@everywhere cc_aut_F_l1l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= constants[:t1] &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_F_l1l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = 0)
@everywhere cc_aut_F_l1l2_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= constants[:t1] &&
S[:d] == 0
@everywhere us_aut_F_l1l2_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
@everywhere cc_aut_F_l1l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_F_l1l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = min(abs(S[:n] - constants[:x1]), abs(S[:n] - constants[:x2])))
@everywhere cc_aut_F_l1l2_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) && getfield(S, :time) <= constants[:t2]
@everywhere us_aut_F_l1l2_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
# l1 => l3
@everywhere cc_aut_F_l1l3_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_F_l1l3_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = 0;)
@everywhere cc_aut_F_l1l3_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S[:n] < constants[:x1] || S[:n] > constants[:x2]) &&
(getfield(S, :time) <= constants[:t1])
@everywhere us_aut_F_l1l3_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(sqrt((getfield(S, :time) - constants[:t1])^2 + (S[:n] - constants[:x2])^2),
sqrt((getfield(S, :time) - constants[:t1])^2 + (S[:n] - constants[:x1])^2)))
@everywhere cc_aut_F_l1l3_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S[:n] < constants[:x1] || S[:n] > constants[:x2]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2])
@everywhere us_aut_F_l1l3_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(S[:d], min(abs(S[:n] - constants[:x1]), abs(S[:n] - constants[:x2]))))
# l3 loc
# l3 => l1
@everywhere cc_aut_F_l3l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
# l3 => l2
@everywhere cc_aut_F_l3l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= constants[:t2])
@everywhere us_aut_F_l3l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1::Float64, t2::Float64, sym_obs::VariableModel)
@assert sym_obs in m.g "$(sym_obs) is not observed."
# Locations
locations = [:l0, :l1, :l2, :l3]
## Invariant predicates
true_inv_predicate(x::Vector{Int}) = true
Λ_F = Dict(:l0 => true_inv_predicate, :l1 => true_inv_predicate,
:l2 => true_inv_predicate, :l3 => true_inv_predicate)
Λ_F = Dict(:l0 => getfield(Main, :true_inv_predicate), :l1 => getfield(Main, :true_inv_predicate),
:l2 => getfield(Main, :true_inv_predicate), :l3 => getfield(Main, :true_inv_predicate))
## Init and final loc
locations_init = [:l0]
......@@ -30,97 +101,53 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
for loc in locations
map_edges[loc] = Dict{Location, Vector{Edge}}()
end
istrue(val::Float64) = convert(Bool, val)
sym_isabs_func = Symbol(m.isabsorbing)
# l0 loc : we construct the edges of the form l0 => (..)
# "cc" as check_constraints
tuple = (:l0, :l1)
@everywhere cc_aut_F_l0l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere us_aut_F_l0l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
S[:n] = get_value(S, x, $(Meta.quot(sym_obs)));
S[:d] = Inf;
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))
edge1 = Edge([nothing], getfield(Main, :cc_aut_F_l0l1_1), getfield(Main, :us_aut_F_l0l1_1!))
idx_obs_var = getfield(m, :map_var_idx)[sym_obs]
nbr_rand = rand(1:1000)
basename_func = "$(replace(m.name, ' '=>'_'))_$(nbr_rand)"
basename_func = replace(basename_func, '-'=>'_')
# l0 loc
# l0 => l1
sym_func_us_l0l1_1 = Symbol("us_aut_F_$(basename_func)_l0l1_1!")
str_us_l0l1_1 = "
@everywhere $(sym_func_us_l0l1_1)(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = \n
(S.loc = :l1; \n
S[:n] = x[$(idx_obs_var)];\n
S[:d] = Inf; \n
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))"
eval(Meta.parse(str_us_l0l1_1))
edge1 = Edge([nothing], getfield(Main, :cc_aut_F_l0l1_1), getfield(Main, sym_func_us_l0l1_1))
map_edges[:l0][:l1] = [edge1]
# l1 loc : we construct the edges of the form l1 => (..)
tuple = (:l1, :l2)
@everywhere cc_aut_F_l1l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= constants[:t1] &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_F_l1l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = 0)
# l1 loc
# l1 => l2
edge1 = Edge([nothing], getfield(Main, :cc_aut_F_l1l2_1), getfield(Main, :us_aut_F_l1l2_1!))
@everywhere cc_aut_F_l1l2_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= constants[:t1] &&
S[:d] == 0
@everywhere us_aut_F_l1l2_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
edge4 = Edge([nothing], getfield(Main, :cc_aut_F_l1l2_4), getfield(Main, :us_aut_F_l1l2_4!))
@everywhere cc_aut_F_l1l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_F_l1l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = min(abs(S[:n] - constants[:x1]), abs(S[:n] - constants[:x2])))
edge2 = Edge([nothing], getfield(Main, :cc_aut_F_l1l2_2), getfield(Main, :us_aut_F_l1l2_2!))
@everywhere cc_aut_F_l1l2_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) && getfield(S, :time) <= constants[:t2]
@everywhere us_aut_F_l1l2_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
edge3 = Edge([nothing], getfield(Main, :cc_aut_F_l1l2_3), getfield(Main, :us_aut_F_l1l2_3!))
edge4 = Edge([nothing], getfield(Main, :cc_aut_F_l1l2_4), getfield(Main, :us_aut_F_l1l2_4!))
map_edges[:l1][:l2] = [edge1, edge2, edge3, edge4]
tuple = (:l1, :l3)
@everywhere cc_aut_F_l1l3_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_F_l1l3_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = 0;)
# l1 => l3
edge1 = Edge([nothing], getfield(Main, :cc_aut_F_l1l3_1), getfield(Main, :us_aut_F_l1l3_1!))
@everywhere cc_aut_F_l1l3_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S[:n] < constants[:x1] || S[:n] > constants[:x2]) &&
(getfield(S, :time) <= constants[:t1])
@everywhere us_aut_F_l1l3_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(sqrt((getfield(S, :time) - constants[:t1])^2 + (S[:n] - constants[:x2])^2),
sqrt((getfield(S, :time) - constants[:t1])^2 + (S[:n] - constants[:x1])^2)))
edge2 = Edge([nothing], getfield(Main, :cc_aut_F_l1l3_2), getfield(Main, :us_aut_F_l1l3_2!))
@everywhere cc_aut_F_l1l3_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S[:n] < constants[:x1] || S[:n] > constants[:x2]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2])
@everywhere us_aut_F_l1l3_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(S[:d], min(abs(S[:n] - constants[:x1]), abs(S[:n] - constants[:x2]))))
edge3 = Edge([nothing], getfield(Main, :cc_aut_F_l1l3_3), getfield(Main, :us_aut_F_l1l3_3!))
map_edges[:l1][:l3] = [edge1, edge2, edge3]
# l3 loc
tuple = (:l3, :l1)
@everywhere cc_aut_F_l3l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere us_aut_F_l3l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
S[:n] = get_value(S, x, $(Meta.quot(sym_obs)));
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))
edge1 = Edge([:ALL], getfield(Main, :cc_aut_F_l3l1_1), getfield(Main, :us_aut_F_l3l1_1!))
# l3 => l1
sym_func_us_l3l1_1 = Symbol("us_aut_F_$(basename_func)_l0l1_1!")
str_us_l3l1_1 =
"@everywhere $(sym_func_us_l3l1_1)(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = \n
(S.loc = :l1;\n
S[:n] = x[$(idx_obs_var)];\n
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))"
eval(Meta.parse(str_us_l3l1_1))
edge1 = Edge([:ALL], getfield(Main, :cc_aut_F_l3l1_1), getfield(Main, sym_func_us_l3l1_1))
map_edges[:l3][:l1] = [edge1]
tuple = (:l3, :l2)
@everywhere cc_aut_F_l3l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= constants[:t2])
@everywhere us_aut_F_l3l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
# l3 => l2
edge1 = Edge([nothing], getfield(Main, :cc_aut_F_l3l2_1), getfield(Main, :us_aut_F_l3l2_1!))
map_edges[:l3][:l2] = [edge1]
......
@everywhere istrue(val::Float64) = convert(Bool, val)
# Invariant predicate functions
@everywhere true_inv_predicate(x::Vector{Int}) = true
# l0 loc
# l0 => l1
@everywhere cc_aut_G_l0l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
# l1 => l3
@everywhere cc_aut_G_l1l3_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) <= constants[:t1] &&
S[:n] < constants[:x1] || S[:n] > constants[:x2]
@everywhere us_aut_G_l1l3_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n]));
S[:in] = false)
@everywhere cc_aut_G_l1l3_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) <= constants[:t1]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = 0;
S[:in] = false)
@everywhere cc_aut_G_l1l3_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = S[:d] * (getfield(S, :time) - constants[:t1]);
S[:tprime] = 0.0)
@everywhere cc_aut_G_l1l3_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:tprime] = 0.0)
# l1 => l4
@everywhere cc_aut_G_l1l4_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_G_l1l4_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l4;
S[:d] += S[:d] * (getfield(S, :time) - constants[:t1]))
@everywhere cc_aut_G_l1l4_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_G_l1l4_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l4)
# l1 => l2
@everywhere cc_aut_G_l1l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l1l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
@everywhere cc_aut_G_l1l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l1l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = S[:d] * (constants[:t2] - constants[:t1]))
@everywhere cc_aut_G_l1l2_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) &&
getfield(S, :time) <= constants[:t1]
@everywhere us_aut_G_l1l2_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = (constants[:t2] - constants[:t1]) *
min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])))
@everywhere cc_aut_G_l1l2_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2])
@everywhere us_aut_G_l1l2_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] += (constants[:t2] - getfield(S, :time)) *
min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])))
# l3 => l1
@everywhere cc_aut_G_l3l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
# l4 => l1
@everywhere cc_aut_G_l4l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
# l2 => l1
@everywhere cc_aut_G_l3l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l3l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = S[:d] * (constants[:t2] - constants[:t1]))
@everywhere cc_aut_G_l3l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l3l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
# l4 => l2
@everywhere cc_aut_G_l4l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= constants[:t2])
@everywhere us_aut_G_l4l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] += S[:tprime] * min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n]));
S[:tprime] = 0.0)
function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1::Float64, t2::Float64, sym_obs::VariableModel)
@assert sym_obs in m.g
# Locations
locations = [:l0, :l1, :l2, :l3, :l4]
# Invariant predicates
@everywhere true_inv_predicate(x::Vector{Int}) = true
Λ_F = Dict(:l0 => getfield(Main, :true_inv_predicate), :l1 => getfield(Main, :true_inv_predicate),
:l2 => getfield(Main, :true_inv_predicate), :l3 => getfield(Main, :true_inv_predicate),
:l4 => getfield(Main, :true_inv_predicate))
......@@ -30,166 +147,81 @@ function create_automaton_G(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
for loc in locations
map_edges[loc] = Dict{Location, Vector{Edge}}()
end
@everywhere istrue(val::Float64) = convert(Bool, val)
sym_isabs_func = Symbol(m.isabsorbing)
idx_obs_var = getfield(m, :map_var_idx)[sym_obs]
nbr_rand = rand(1:1000)
basename_func = "$(replace(m.name, ' '=>'_'))_$(nbr_rand)"
basename_func = replace(basename_func, '-'=>'_')
# l0 loc
tuple = (:l0, :l1)
@everywhere cc_aut_G_l0l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere us_aut_G_l0l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
S[:d] = 0;
S[:n] = get_value(S, x, $(Meta.quot(sym_obs)));
S[:in] = true;
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))
edge1 = Edge([nothing], getfield(Main, :cc_aut_G_l0l1_1), getfield(Main, :us_aut_G_l0l1_1!))
# l0 => l1
sym_func_us_l0l1_1 = Symbol("us_aut_G_$(basename_func)_l0l1_1!")
str_us_l0l1_1 = "
@everywhere $(sym_func_us_l0l1_1)(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = \n
(S.loc = :l1; \n
S[:d] = 0; \n
S[:n] = x[$(idx_obs_var)]; \n
S[:in] = true; \n
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))"
eval(Meta.parse(str_us_l0l1_1))
edge1 = Edge([nothing], getfield(Main, :cc_aut_G_l0l1_1), getfield(Main, sym_func_us_l0l1_1))
map_edges[:l0][:l1] = [edge1]
# l1 loc
tuple = (:l1, :l3)
@everywhere cc_aut_G_l1l3_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) <= constants[:t1] &&
S[:n] < constants[:x1] || S[:n] > constants[:x2]
@everywhere us_aut_G_l1l3_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n]));
S[:in] = false)
# l1 => l3
edge1 = Edge([nothing], getfield(Main, :cc_aut_G_l1l3_1), getfield(Main, :us_aut_G_l1l3_1!))
@everywhere cc_aut_G_l1l3_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = S[:d] * (getfield(S, :time) - constants[:t1]);
S[:tprime] = 0.0)
edge3 = Edge([nothing], getfield(Main, :cc_aut_G_l1l3_3), getfield(Main, :us_aut_G_l1l3_3!))
@everywhere cc_aut_G_l1l3_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) <= constants[:t1]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:d] = 0;
S[:in] = false)
edge2 = Edge([nothing], getfield(Main, :cc_aut_G_l1l3_2), getfield(Main, :us_aut_G_l1l3_2!))
@everywhere cc_aut_G_l1l3_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(constants[:x1] <= S[:n] <= constants[:x2])
@everywhere us_aut_G_l1l3_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
S[:tprime] = 0.0)
edge3 = Edge([nothing], getfield(Main, :cc_aut_G_l1l3_3), getfield(Main, :us_aut_G_l1l3_3!))
edge4 = Edge([nothing], getfield(Main, :cc_aut_G_l1l3_4), getfield(Main, :us_aut_G_l1l3_4!))
map_edges[:l1][:l3] = [edge1, edge2, edge3, edge4]
tuple = (:l1, :l4)
@everywhere cc_aut_G_l1l4_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_G_l1l4_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l4;
S[:d] += S[:d] * (getfield(S, :time) - constants[:t1]))
# l1 => l4
edge1 = Edge([nothing], getfield(Main, :cc_aut_G_l1l4_1), getfield(Main, :us_aut_G_l1l4_1!))
@everywhere cc_aut_G_l1l4_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2]) &&
(S[:n] < constants[:x1] || S[:n] > constants[:x2])
@everywhere us_aut_G_l1l4_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l4)
edge2 = Edge([nothing], getfield(Main, :cc_aut_G_l1l4_2), getfield(Main, :us_aut_G_l1l4_2!))
map_edges[:l1][:l4] = [edge1, edge2]
tuple = (:l1, :l2)
@everywhere cc_aut_G_l1l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l1l2_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
# l1 => l2
edge1 = Edge([nothing], getfield(Main, :cc_aut_G_l1l2_1), getfield(Main, :us_aut_G_l1l2_1!))
@everywhere cc_aut_G_l1l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l1l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = S[:d] * (constants[:t2] - constants[:t1]))
edge2 = Edge([nothing], getfield(Main, :cc_aut_G_l1l2_2), getfield(Main, :us_aut_G_l1l2_2!))
@everywhere cc_aut_G_l1l2_3(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) &&
getfield(S, :time) <= constants[:t1]
@everywhere us_aut_G_l1l2_3!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = (constants[:t2] - constants[:t1]) *
min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])))
edge3 = Edge([nothing], getfield(Main, :cc_aut_G_l1l2_3), getfield(Main, :us_aut_G_l1l2_3!))
@everywhere cc_aut_G_l1l2_4(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:isabs]) &&
(constants[:t1] <= getfield(S, :time) <= constants[:t2])
@everywhere us_aut_G_l1l2_4!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] += (constants[:t2] - getfield(S, :time)) *
min(abs(constants[:x1] - S[:n]), abs(constants[:x2] - S[:n])))
edge4 = Edge([nothing], getfield(Main, :cc_aut_G_l1l2_4), getfield(Main, :us_aut_G_l1l2_4!))
map_edges[:l1][:l2] = [edge1, edge2, edge3, edge4]
# l3 loc
tuple = (:l3, :l1)
@everywhere cc_aut_G_l3l1_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere us_aut_G_l3l1_1!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
S[:n] = get_value(S, x, $(Meta.quot(sym_obs)));
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))
edge1 = Edge([:ALL], getfield(Main, :cc_aut_G_l3l1_1), getfield(Main, :us_aut_G_l3l1_1!))
# l3 => l1
sym_func_us_l3l1_1 = Symbol("us_aut_G_$(basename_func)_l3l1_1!")
str_us_l3l1_1 = "
@everywhere $(sym_func_us_l3l1_1)(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
S[:n] = x[$(idx_obs_var)];
S[:isabs] = getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x))"
eval(Meta.parse(str_us_l3l1_1))
edge1 = Edge([:ALL], getfield(Main, :cc_aut_G_l3l1_1), getfield(Main, sym_func_us_l3l1_1))
map_edges[:l3][:l1] = [edge1]
tuple = (:l3, :l2)
@everywhere cc_aut_G_l3l2_2(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]
@everywhere us_aut_G_l3l2_2!(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
S[:d] = S[:d] * (constants[:t2] - constants[:t1]))
edge2 = Edge([nothing], getfield(Main, :cc_aut_G_l3l2_2), getfield(Main, :us_aut_G_l3l2_2!))
@everywhere cc_aut_G_l3l2_1(S::StateLHA, constants::Dict{Symbol,Float64}, x::Vector{Int}, p::Vector{Float64}) =
!istrue(S[:in]) &&
getfield(S, :time) >= constants[:t2]