Skip to content
Snippets Groups Projects
Commit 33319d9b authored by Bentriou Mahmoud's avatar Bentriou Mahmoud
Browse files

Small improvment perf for next_state! of LHA

parent bf35d240
No related branches found
No related tags found
No related merge requests found
...@@ -33,9 +33,44 @@ function init_state(A::LHA, x0::Vector{Int}, t0::Float64) ...@@ -33,9 +33,44 @@ function init_state(A::LHA, x0::Vector{Int}, t0::Float64)
return S0 return S0
end end
function _find_edge_candidates!(edge_candidates::Vector{Edge}, current_loc::Location,
A::LHA, Snplus1::StateLHA)
for loc in A.l_loc
tuple_edges = (current_loc, loc)
if haskey(A.map_edges, tuple_edges)
for edge in A.map_edges[tuple_edges]
if edge.check_constraints(A, Snplus1)
push!(edge_candidates, edge)
end
end
end
end
end
function _get_edge_index(edge_candidates::Vector{Edge}, first_round::Bool,
detected_event::Bool, tr_nplus1::Transition)
ind_edge = 0
bool_event = detected_event
for i in eachindex(edge_candidates)
edge = edge_candidates[i]
if edge.transitions[1] == nothing
return (i, bool_event)
end
if first_round || !detected_event
if (length(edge.transitions) == 1 && tr_nplus1 != nothing && edge.transitions[1] == "ALL") ||
(tr_nplus1 in edge.transitions)
ind_edge = i
bool_event = true
end
end
end
return (ind_edge, bool_event)
end
function next_state!(Snplus1::StateLHA, A::LHA, function next_state!(Snplus1::StateLHA, A::LHA,
xnplus1::Vector{Int}, tnplus1::Float64, tr_nplus1::Transition, xnplus1::Vector{Int}, tnplus1::Float64, tr_nplus1::Transition,
Sn::StateLHA; verbose = false) Sn::StateLHA; verbose::Bool = false)
for i in eachindex(Snplus1.l_var) for i in eachindex(Snplus1.l_var)
Snplus1.l_var[i] += (A.l_flow[Sn.loc])[i]*(tnplus1 - Sn.time) Snplus1.l_var[i] += (A.l_flow[Sn.loc])[i]*(tnplus1 - Sn.time)
end end
...@@ -43,61 +78,34 @@ function next_state!(Snplus1::StateLHA, A::LHA, ...@@ -43,61 +78,34 @@ function next_state!(Snplus1::StateLHA, A::LHA,
# En fait d'apres observation de Cosmos, après qu'on ait lu la transition on devrait stop. # En fait d'apres observation de Cosmos, après qu'on ait lu la transition on devrait stop.
edge_candidates = Edge[] edge_candidates = Edge[]
tuple_candidates = Tuple{Location, Location}[] first_round::Bool = true
first_round = true detected_event::Bool = (tr_nplus1 == nothing) ? true : false
detected_event = (tr_nplus1 == nothing) ? true : false
turns = 1 turns = 1
while first_round || !detected_event || length(edge_candidates) > 0 while first_round || !detected_event || length(edge_candidates) > 0
edge_candidates = Edge[] edge_candidates = empty!(edge_candidates)
tuple_candidates = Tuple{Location,Location}[]
current_loc = Snplus1.loc current_loc = Snplus1.loc
if verbose if verbose @show turns end
@show turns
end
# Save all edges that satisfies transition predicate (synchronous or autonomous) # Save all edges that satisfies transition predicate (synchronous or autonomous)
for loc in A.l_loc _find_edge_candidates!(edge_candidates, current_loc, A, Snplus1)
tuple_edges = (current_loc, loc)
if haskey(A.map_edges, tuple_edges)
for edge in A.map_edges[tuple_edges]
if edge.check_constraints(A, Snplus1)
push!(edge_candidates, edge)
push!(tuple_candidates, tuple_edges)
end
end
end
end
# Search the one we must chose # Search the one we must chose
ind_edge = 0 ind_edge, detected_event = _get_edge_index(edge_candidates, first_round, detected_event, tr_nplus1)
for (i, edge) in enumerate(edge_candidates)
if edge.transitions[1] == nothing
ind_edge = i
break
end
if first_round || !detected_event
if (length(edge.transitions) == 1 && tr_nplus1 != nothing && edge.transitions[1] == "ALL") ||
(tr_nplus1 in edge.transitions)
detected_event = true
ind_edge = i
end
end
end
# Update the state with the chosen one (if it exists) # Update the state with the chosen one (if it exists)
if ind_edge > 0 if ind_edge > 0
edge_candidates[ind_edge].update_state!(A, Snplus1, xnplus1) edge_candidates[ind_edge].update_state!(A, Snplus1, xnplus1)
# Should add something like if edges_candidates[ind_edge].transition != nohting break end ?? # Should add something like if edges_candidates[ind_edge].transition != nohting break end ??
end end
if ind_edge == 0 break end
if verbose if verbose
@show first_round, detected_event @show first_round detected_event
@show tnplus1, tr_nplus1, xnplus1 @show tnplus1 tr_nplus1 xnplus1
@show ind_edge @show ind_edge
@show edge_candidates @show edge_candidates
@show tuple_candidates @show tuple_candidates
@show Snplus1 @show Snplus1
end end
if ind_edge == 0 break end
# For debug # For debug
if turns > 10 if turns > 100
println("Turns, Bad behavior of region2 automaton") println("Number of turns in next_state! is suspicious")
@show first_round, detected_event @show first_round, detected_event
@show length(edge_candidates) @show length(edge_candidates)
@show tnplus1, tr_nplus1, xnplus1 @show tnplus1, tr_nplus1, xnplus1
...@@ -105,14 +113,14 @@ function next_state!(Snplus1::StateLHA, A::LHA, ...@@ -105,14 +113,14 @@ function next_state!(Snplus1::StateLHA, A::LHA,
for edge in edge_candidates for edge in edge_candidates
@show edge.check_constraints(A, Snplus1) @show edge.check_constraints(A, Snplus1)
end end
error("Unpredicted behavior automaton F v2") error("Unpredicted behavior automaton")
end end
turns += 1 turns += 1
first_round = false first_round = false
end end
end end
# For debug purposes # For tests purposes
function read_trajectory(A::LHA, σ::Trajectory; verbose = false) function read_trajectory(A::LHA, σ::Trajectory; verbose = false)
A_new = LHA(A, (σ.m)._map_obs_var_idx) A_new = LHA(A, (σ.m)._map_obs_var_idx)
l_t = times(σ) l_t = times(σ)
......
...@@ -46,7 +46,7 @@ function simulate(m::ContinuousTimeModel; p::Union{Nothing,AbstractVector{Float6 ...@@ -46,7 +46,7 @@ function simulate(m::ContinuousTimeModel; p::Union{Nothing,AbstractVector{Float6
transitions[1] = nothing transitions[1] = nothing
# Values at time n # Values at time n
n = 1 n = 1
xn = m.x0 # View for type stability xn = m.x0
tn = m.t0 tn = m.t0
# at time n+1 # at time n+1
isabsorbing::Bool = m.isabsorbing(p_sim,xn) isabsorbing::Bool = m.isabsorbing(p_sim,xn)
...@@ -144,7 +144,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl ...@@ -144,7 +144,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
S0 = init_state(A, m.x0, m.t0) S0 = init_state(A, m.x0, m.t0)
# Values at time n # Values at time n
n = 1 n = 1
xn = m.x0 # View for type stability xn = m.x0
tn = m.t0 tn = m.t0
Sn = copy(S0) Sn = copy(S0)
isabsorbing::Bool = m.isabsorbing(p_sim,xn) isabsorbing::Bool = m.isabsorbing(p_sim,xn)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment