From c809fe7efec967783960e681b0569ee169bca24d Mon Sep 17 00:00:00 2001
From: Mahmoud Bentriou <mahmoud.bentriou@centralesupelec.fr>
Date: Fri, 19 Feb 2021 14:13:06 +0100
Subject: [PATCH] Slight improvement of next_state! perforance by allocating
 edge_candidates on top-level of next_state! function.

---
 core/common.jl |  2 +-
 core/lha.jl    | 10 +++++-----
 core/model.jl  | 29 ++++++++++++++++++-----------
 core/plots.jl  |  3 ++-
 4 files changed, 26 insertions(+), 18 deletions(-)

diff --git a/core/common.jl b/core/common.jl
index 0225b1a..1623ef4 100644
--- a/core/common.jl
+++ b/core/common.jl
@@ -39,7 +39,7 @@ struct Trajectory <: AbstractTrajectory
 end
 
 struct Edge
-    transitions::Union{Nothing,Vector{Transition}}
+    transitions::Union{Nothing,Vector{Symbol}}
     check_constraints::Function
     update_state!::Function
 end
diff --git a/core/lha.jl b/core/lha.jl
index 2843d3f..0a5297c 100644
--- a/core/lha.jl
+++ b/core/lha.jl
@@ -92,8 +92,7 @@ function init_state(A::LHA, x0::Vector{Int}, t0::Float64)
     return S0
 end
 
-# A push! method implementend by myself because I know in most cases the edge candidates
-# are not greater than 2
+# A push! method implementend by myself because of preallocation of edge_candidates
 function _push_edge!(edge_candidates::Vector{Edge}, edge::Edge, nbr_candidates::Int)
     if nbr_candidates < 2
         @inbounds edge_candidates[nbr_candidates+1] = edge
@@ -153,9 +152,9 @@ end
 
 function next_state!(Snplus1::StateLHA, A::LHA, 
                      xnplus1::Vector{Int}, tnplus1::Float64, tr_nplus1::Transition, 
-                     Sn::StateLHA, xn::Vector{Int}, p::Vector{Float64}; verbose::Bool = false)
+                     Sn::StateLHA, xn::Vector{Int}, p::Vector{Float64},
+                     edge_candidates::Vector{Edge}; verbose::Bool = false)
     # En fait d'apres observation de Cosmos, après qu'on ait lu la transition on devrait stop.
-    edge_candidates = Vector{Edge}(undef, 2)
     detected_event::Bool = false
     turns = 0
     current_values = getfield(Snplus1, :values)
@@ -308,10 +307,11 @@ function read_trajectory(A::LHA, σ::AbstractTrajectory; verbose = false)
     l_tr = transitions(σ)
     Sn = init_state(A_new, σ[1], l_t[1])
     Snplus1 = copy(Sn)
+    edge_candidates = Vector{Edge}(undef, 2)
     if verbose println("Init: ") end
     if verbose @show Sn end
     for n in 2:length_states(σ)
-        next_state!(Snplus1, A_new, σ[n], l_t[n], l_tr[n], Sn, σ[n-1], p_sim; verbose = verbose)
+        next_state!(Snplus1, A_new, σ[n], l_t[n], l_tr[n], Sn, σ[n-1], p_sim, edge_candidates; verbose = verbose)
         copyto!(Sn, Snplus1)
         if Snplus1.loc in A_new.locations_final 
             break 
diff --git a/core/model.jl b/core/model.jl
index 4f33d5c..3151968 100644
--- a/core/model.jl
+++ b/core/model.jl
@@ -139,6 +139,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
     S0 = init_state(A, x0, t0)
     buffer_size = getfield(m, :buffer_size)
     estim_min_states = getfield(m, :estim_min_states)
+    edge_candidates = Vector{Edge}(undef, 2)
     # First alloc
     full_values = Vector{Vector{Int}}(undef, getfield(m, :dim_state))
     for i = eachindex(full_values) full_values[i] = zeros(Int, estim_min_states) end
@@ -153,7 +154,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
     xn = copy(x0)
     tn = copy(t0) 
     Sn = copy(S0)
-    next_state!(Sn, A, x0, t0, nothing, S0, x0, p_sim; verbose = verbose)
+    next_state!(Sn, A, x0, t0, nothing, S0, x0, p_sim, edge_candidates; verbose = verbose)
     isabsorbing::Bool = getfield(m, :isabsorbing)(p_sim,xn)
     isacceptedLHA::Bool = isaccepted(Sn)
     # Alloc of vectors where we stock n+1 values
@@ -169,7 +170,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
             _finish_bounded_trajectory!(values, times, transitions, time_bound)
         end
         if isabsorbing && !isacceptedLHA
-            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim; verbose = verbose)
+            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim, edge_candidates; verbose = verbose)
             copyto!(Sn, Snplus1)
         end
         return SynchronizedTrajectory(Sn, product, values, times, transitions)
@@ -183,7 +184,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
             break
         end
         n += 1
-        next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim; verbose = verbose)
+        next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim, edge_candidates; verbose = verbose)
         copyto!(xn, vec_x)
         tn = l_t[1]
         tr_n = l_tr[1]
@@ -202,7 +203,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
             _finish_bounded_trajectory!(values, times, transitions, time_bound)
         end
         if isabsorbing && !isacceptedLHA
-            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim; verbose = verbose)
+            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim, edge_candidates; verbose = verbose)
             copyto!(Sn, Snplus1)
         end
         return SynchronizedTrajectory(Sn, product, values, times, transitions)
@@ -226,7 +227,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
                 i -= 1
                 break
             end
-            next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim; verbose = verbose)
+            next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim, edge_candidates; verbose = verbose)
             copyto!(xn, vec_x)
             tn = l_t[1]
             tr_n = l_tr[1]
@@ -252,7 +253,7 @@ function simulate(product::SynchronizedModel; p::Union{Nothing,AbstractVector{Fl
         _finish_bounded_trajectory!(values, times, transitions, time_bound)
     end
     if isabsorbing && !isacceptedLHA
-        next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim; verbose = verbose)
+        next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim, edge_candidates; verbose = verbose)
         copyto!(Sn, Snplus1)
     end
     return SynchronizedTrajectory(Sn, product, values, times, transitions)
@@ -276,12 +277,13 @@ function volatile_simulate(product::SynchronizedModel;
     t0 = getfield(m, :t0)
     time_bound = getfield(m, :time_bound)
     S0 = init_state(A, x0, t0)
+    edge_candidates = Vector{Edge}(undef, 2)
     # Values at time n
     n = 1
     xn = copy(x0)
     tn = copy(t0) 
     Sn = copy(S0)
-    next_state!(Sn, A, x0, t0, nothing, S0, x0, p_sim; verbose = verbose)
+    next_state!(Sn, A, x0, t0, nothing, S0, x0, p_sim, edge_candidates; verbose = verbose)
     isabsorbing::Bool = getfield(m, :isabsorbing)(p_sim,xn)
     isacceptedLHA::Bool = isaccepted(Sn)
     # Alloc of vectors where we stock n+1 values
@@ -292,7 +294,7 @@ function volatile_simulate(product::SynchronizedModel;
     # If x0 is absorbing
     if isabsorbing || isacceptedLHA 
         if !isacceptedLHA
-            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim; verbose = verbose)
+            next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim, edge_candidates; verbose = verbose)
             copyto!(Sn, Snplus1)
         end
         return Sn
@@ -307,7 +309,7 @@ function volatile_simulate(product::SynchronizedModel;
             isabsorbing = true
             break
         end
-        next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim; verbose = verbose)
+        next_state!(Snplus1, A, vec_x, l_t[1], l_tr[1], Sn, xn, p_sim, edge_candidates; verbose = verbose)
         copyto!(xn, vec_x)
         tn = l_t[1]
         tr_n = l_tr[1]
@@ -316,7 +318,7 @@ function volatile_simulate(product::SynchronizedModel;
         n += 1
     end
     if isabsorbing && !isacceptedLHA
-        next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim; verbose = verbose)
+        next_state!(Snplus1, A, xn, time_bound, nothing, Sn, xn, p_sim, edge_candidates; verbose = verbose)
         copyto!(Sn, Snplus1)
     end
     return Sn
@@ -343,8 +345,13 @@ A volatile version of `simulate(pm::ParametricModel, p_prior::AbstractVector{Flo
 The model in pm should be of type SynchronizedModel (`typeof(pm.m) <: SynchronizedModel`).
 It returns `S::StateLHA`, not a trajectory.
 """
-function volatile_simulate(pm::ParametricModel, p_prior::AbstractVector{Float64})
+function volatile_simulate(pm::ParametricModel, p_prior::AbstractVector{Float64};
+                           epsilon::Float64)
     @assert typeof(pm.m) <: SynchronizedModel
+    # ABC related automata
+    if pm.m.name in ["ABC euclidean distance"]
+        nothing
+    end
     full_p = copy(get_proba_model(pm).p)
     full_p[pm._param_idx] = p_prior
     
diff --git a/core/plots.jl b/core/plots.jl
index 3f842e2..1bea64c 100644
--- a/core/plots.jl
+++ b/core/plots.jl
@@ -93,10 +93,11 @@ function plot_periodic_trajectory(A::LHA, σ::SynchronizedTrajectory, sym_obs::S
     idx_n = [1]
     values_n = [Sn[:n]]
     values_tp = [Sn[:tp]]
+    edge_candidates = Vector{Edge}(undef, 2)
     if verbose println("Init: ") end
     if verbose @show Sn end
     for n in 2:nbr_states
-        next_state!(Snplus1, A, σ[n], l_t[n], l_tr[n], Sn, σ[n-1], p_sim; verbose = verbose)
+        next_state!(Snplus1, A, σ[n], l_t[n], l_tr[n], Sn, σ[n-1], p_sim, edge_candidates; verbose = verbose)
         if Snplus1[:n] != values_n[end]
             push!(idx_n, n)
             push!(values_n, Snplus1[:n])
-- 
GitLab