Commit 9cf25cea authored by Bentriou Mahmoud's avatar Bentriou Mahmoud

Another rewriting of LHA methods. Improves readability and avoid an

allocation per next_state!. Now update_state! functions returns a Symbol
as location instead of modifying a vector.
parent 5d8c423e
......@@ -4,7 +4,7 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
@assert sym_obs in m.g "$(sym_obs) is not observed."
@assert (x1 <= x2) "x1 > x2 impossible for F automaton."
@assert (t1 <= t2) "t1 > t2 impossible for F automaton."
# Locations
locations = [:l0, :l1, :l2, :l3]
......@@ -12,7 +12,7 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
@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))
## Init and final loc
locations_init = [:l0]
locations_final = [:l2]
......@@ -34,7 +34,7 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
for loc in locations
map_edges[loc] = Dict{Location, Vector{Edge}}()
end
idx_obs_var = getfield(m, :map_var_idx)[sym_obs]
idx_var_n = map_var_automaton_idx[:n]
idx_var_d = map_var_automaton_idx[:d]
......@@ -54,78 +54,72 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
# "cc" as check_constraints and "us" as update_state
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l1;
S_values[$(idx_var_n)] = x[$(idx_obs_var)];
@everywhere $(func_name(:us, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_values[$(idx_var_n)] = x[$(idx_obs_var)];
setindex!(S_values, Inf, $(idx_var_d));
setindex!(S_values, getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs)))
setindex!(S_values, getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs));
:l1)
# l1 loc
# l1 => l2
#=
@everywhere $(func_name(:cc, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S_time >= $t1 &&
($x1 <= S_values[$(idx_var_n)] <= $x2)
@everywhere $(func_name(:us, :l1, :l2, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l2;
setindex!(S_values, 0, $(idx_var_d)))
=#
@everywhere $(func_name(:us, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, 0, $(idx_var_d));
:l2) =#
#=
@everywhere $(func_name(:cc, :l1, :l2, 3))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
istrue(S_values[$(idx_var_isabs)]) && S_time <= $t2
@everywhere $(func_name(:us, :l1, :l2, 3))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l2)
@everywhere $(func_name(:us, :l1, :l2, 3))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(:l2)
=#
@everywhere $(func_name(:cc, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S_time >= $t1 &&
S_values[$(idx_var_d)] == 0
@everywhere $(func_name(:us, :l1, :l2, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l2)
@everywhere $(func_name(:us, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(:l2)
@everywhere $(func_name(:cc, :l1, :l2, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_time >= $t2) &&
(S_values[$(idx_var_n)] < $x1 || S_values[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l2, 2))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l2;)
@everywhere $(func_name(:us, :l1, :l2, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(:l2;)
#setindex!(S_values, min(abs(S_values[$(idx_var_n)] - $x1), abs(S_values[$(idx_var_n)] - $x2)), $(idx_var_d)))
# l1 => l3
@everywhere $(func_name(:cc, :l1, :l3, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_time <= $t1) &&
(S_values[$(idx_var_n)] < $x1 || S_values[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l3, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l3;
setindex!(S_values, min(sqrt((S_time - $t1)^2 + (S_values[$(idx_var_n)] - $x2)^2),
sqrt((S_time - $t1)^2 + (S_values[$(idx_var_n)] - $x1)^2)), $(idx_var_d)))
@everywhere $(func_name(:us, :l1, :l3, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, min(sqrt((S_time - $t1)^2 + (S_values[$(idx_var_n)] - $x2)^2),
sqrt((S_time - $t1)^2 + (S_values[$(idx_var_n)] - $x1)^2)), $(idx_var_d));
:l3)
@everywhere $(func_name(:cc, :l1, :l3, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
($x1 <= S_values[$(idx_var_n)] <= $x2)
@everywhere $(func_name(:us, :l1, :l3, 2))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l3;
setindex!(S_values, 0, $(idx_var_d)))
@everywhere $(func_name(:us, :l1, :l3, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, 0, $(idx_var_d));
:l3)
@everywhere $(func_name(:cc, :l1, :l3, 3))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_time >= $t1) &&
(S_values[$(idx_var_n)] < $x1 || S_values[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l3, 3))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l3;
val_min = min(S_values[$(idx_var_d)],
@everywhere $(func_name(:us, :l1, :l3, 3))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(val_min = min(S_values[$(idx_var_d)],
min(abs(S_values[$(idx_var_n)] - $x1), abs(S_values[$(idx_var_n)] - $x2)));
setindex!(S_values, val_min, $(idx_var_d)))
setindex!(S_values, val_min, $(idx_var_d));
:l3)
# l3 loc
# l3 => l1
@everywhere $(func_name(:cc, :l3, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l3, :l1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l1;
S_values[$(idx_var_n)] = x[$(idx_obs_var)];
setindex!(S_values, getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs)))
@everywhere $(func_name(:us, :l3, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_values[$(idx_var_n)] = x[$(idx_obs_var)];
setindex!(S_values, getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs));
:l1)
# l3 => l2
@everywhere $(func_name(:cc, :l3, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(S_time >= $t2 || istrue(S_values[$(idx_var_isabs)]))
@everywhere $(func_name(:us, :l3, :l2, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = :l2)
@everywhere $(func_name(:us, :l3, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(:l2)
end
eval(meta_elementary_functions)
......@@ -148,12 +142,12 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
edge2 = Edge(nothing, getfield(Main, func_name(:cc, :l1, :l3, 2)), getfield(Main, func_name(:us, :l1, :l3, 2)))
edge3 = Edge(nothing, getfield(Main, func_name(:cc, :l1, :l3, 3)), getfield(Main, func_name(:us, :l1, :l3, 3)))
map_edges[:l1][:l3] = [edge1, edge2, edge3]
# l3 loc
# l3 => l1
edge1 = Edge([:ALL], getfield(Main, func_name(:cc, :l3, :l1, 1)), getfield(Main, func_name(:us, :l3, :l1, 1)))
map_edges[:l3][:l1] = [edge1]
# l3 => l2
edge1 = Edge(nothing, getfield(Main, func_name(:cc, :l3, :l2, 1)), getfield(Main, func_name(:us, :l3, :l2, 1)))
map_edges[:l3][:l2] = [edge1]
......
This diff is collapsed.
This diff is collapsed.
......@@ -35,7 +35,7 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
idx_var_n = map_var_automaton_idx[:n]
idx_var_d = map_var_automaton_idx[:d]
idx_var_idx = map_var_automaton_idx[:idx]
nbr_rand = rand(1:1000)
basename_func = "$(replace(m.name, ' '=>'_'))_$(nbr_rand)"
basename_func = replace(basename_func, '-'=>'_')
......@@ -45,11 +45,11 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
# l0 loc
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = Symbol("l1");
setindex!(S_values, x[$(idx_obs_var)], $(idx_var_n));
@everywhere $(func_name(:us, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(idx_var_n));
setindex!(S_values, 0.0, $(idx_var_d));
setindex!(S_values, 1.0, $(idx_var_idx)))
setindex!(S_values, 1.0, $(idx_var_idx));
:l1)
# l1 loc
# l1 => l1
......@@ -58,23 +58,25 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
(tml = $(Tuple(timeline));
tml_idx = tml[convert(Int, S_values[$(idx_var_idx)])];
S_values[$(idx_var_t)] >= tml_idx)
@everywhere $(func_name(:us, :l1, :l1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
@everywhere $(func_name(:us, :l1, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(y_obs = $(Tuple(observations));
y_obs_idx = y_obs[convert(Int, S_values[$(idx_var_idx)])];
setindex!(S_values, S_values[$(idx_var_d)] + (S_values[$(idx_var_n)] - y_obs_idx)^2,
$(idx_var_d));
setindex!(S_values, S_values[$(idx_var_idx)] + 1.0, $(idx_var_idx)))
$(idx_var_d));
setindex!(S_values, S_values[$(idx_var_idx)] + 1.0, $(idx_var_idx));
:l1)
@everywhere $(func_name(:cc, :l1, :l1, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l1, :l1, 2))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(idx_var_n)))
@everywhere $(func_name(:us, :l1, :l1, 2))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(idx_var_n));
:l1)
# l1 => l2
@everywhere $(func_name(:cc, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S_values[$(idx_var_idx)] >= ($nbr_observations + 1)
@everywhere $(func_name(:us, :l1, :l2, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = Symbol("l2");
setindex!(S_values, sqrt(S_values[$(idx_var_d)]), $(idx_var_d)))
@everywhere $(func_name(:us, :l1, :l2, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, sqrt(S_values[$(idx_var_d)]), $(idx_var_d));
:l2)
end
eval(meta_elementary_functions)
......@@ -89,15 +91,15 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
edge1 = Edge([:ALL], getfield(Main, func_name(:cc, :l1, :l1, 1)), getfield(Main, func_name(:us, :l1, :l2, 1)))
map_edges[:l1][:l1] = [edge1]
for i = 1:nbr_observations
meta_edge_i = quote
@everywhere $(func_name(:cc, :l1, :l1, 1+i))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S[:t] >= $(timeline[i])
@everywhere $(func_name(:us, :l1, :l1, 1+i))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, S[:d] + (S[:n] - $(observations[i]))^2, $(idx_var_d));
setindex!(S_values, S[:idx] + 1.0, $(idx_var_idx)))
end
eval(meta_edge_i)
push!(map_edges[:l1][:l1], Edge(nothing, getfield(Main, func_name(:cc, :l1, :l1, 1+i)), getfield(Main, func_name(:us, :l1, :l1, 1+i))))
meta_edge_i = quote
@everywhere $(func_name(:cc, :l1, :l1, 1+i))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S[:t] >= $(timeline[i])
@everywhere $(func_name(:us, :l1, :l1, 1+i))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, S[:d] + (S[:n] - $(observations[i]))^2, $(idx_var_d));
setindex!(S_values, S[:idx] + 1.0, $(idx_var_idx)))
end
eval(meta_edge_i)
push!(map_edges[:l1][:l1], Edge(nothing, getfield(Main, func_name(:cc, :l1, :l1, 1+i)), getfield(Main, func_name(:us, :l1, :l1, 1+i))))
end
=#
edge1 = Edge(nothing, getfield(Main, func_name(:cc, :l1, :l1, 1)), getfield(Main, func_name(:us, :l1, :l1, 1)))
......
......@@ -28,13 +28,13 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
for loc in locations
flow[loc] = vector_flow
end
## Edges
map_edges = Dict{Location, Dict{Location, Vector{Edge}}}()
for loc in locations
map_edges[loc] = Dict{Location, Vector{Edge}}()
end
idx_obs_var = getfield(m, :map_var_idx)[sym_obs]
to_idx(var::Symbol) = map_var_automaton_idx[var]
nbr_rand = rand(1:1000)
......@@ -47,24 +47,25 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
# l0 loc
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = Symbol("l1");
setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n)));
setindex!(S_values, 0.0, $(to_idx(:d))))
@everywhere $(func_name(:us, :l0, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n)));
setindex!(S_values, 0.0, $(to_idx(:d)));
:l1)
# lnbr_obs => lfinal
@everywhere $(func_name(:cc, loc_nbr_obs, :lfinal, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S_values[$(to_idx(:t))] >= $(timeline[nbr_observations])
@everywhere $(func_name(:us, loc_nbr_obs, :lfinal, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = Symbol("lfinal");
setindex!(S_values, S_values[$(to_idx(:d))] + (S_values[$(to_idx(:n))]-$(observations[nbr_observations]))^2,
$(to_idx(:d)));
setindex!(S_values, sqrt(S_values[$(to_idx(:d))]), $(to_idx(:d))))
@everywhere $(func_name(:us, loc_nbr_obs, :lfinal, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, S_values[$(to_idx(:d))] + (S_values[$(to_idx(:n))]-$(observations[nbr_observations]))^2,
$(to_idx(:d)));
setindex!(S_values, sqrt(S_values[$(to_idx(:d))]), $(to_idx(:d)));
:lfinal)
# lnbr_obs => lnbr_obs
@everywhere $(func_name(:cc, loc_nbr_obs, loc_nbr_obs, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, loc_nbr_obs, loc_nbr_obs, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n))))
@everywhere $(func_name(:us, loc_nbr_obs, loc_nbr_obs, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n)));
$(Meta.quot(loc_nbr_obs)))
end
eval(meta_elementary_functions)
# l0 loc
......@@ -88,14 +89,15 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
# Defined below
@everywhere $(func_name(:cc, loci, locip1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
S_values[$(to_idx(:t))] >= $(timeline[i])
@everywhere $(func_name(:us, loci, locip1, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(ptr_loc[1] = $(Meta.quot(locip1));
setindex!(S_values, S_values[$(to_idx(:d))] + (S_values[$(to_idx(:n))]-$(observations[i]))^2,
$(to_idx(:d))))
@everywhere $(func_name(:us, loci, locip1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, S_values[$(to_idx(:d))] + (S_values[$(to_idx(:n))]-$(observations[i]))^2,
$(to_idx(:d)));
$(Meta.quot(locip1)))
@everywhere $(func_name(:cc, loci, loci, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, loci, loci, 1))(ptr_loc::Vector{Symbol}, S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n))))
@everywhere $(func_name(:us, loci, loci, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(S_values, x[$(idx_obs_var)], $(to_idx(:n)));
$(Meta.quot(loci)))
end
eval(meta_elementary_functions_loci)
......@@ -106,7 +108,7 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
edge1 = Edge([:ALL], getfield(Main, func_name(:cc, loci, loci, 1)), getfield(Main, func_name(:us, loci, loci, 1)))
map_edges[loci][loci] = [edge1]
end
## Constants
constants = Dict{Symbol,Float64}(:nbr_obs => nbr_observations)
......
This diff is collapsed.
......@@ -70,7 +70,7 @@ function Base.copyto!(Sdest::StateLHA, Ssrc::StateLHA)
Sdest.A = Ssrc.A
Sdest.loc = Ssrc.loc
for i = eachindex(Sdest.values)
Sdest.values[i] = Ssrc.values[i]
@inbounds Sdest.values[i] = Ssrc.values[i]
end
Sdest.time = Ssrc.time
end
......@@ -96,7 +96,7 @@ end
# are not greater than 2
function _push_edge!(edge_candidates::Vector{Edge}, edge::Edge, nbr_candidates::Int)
if nbr_candidates < 2
edge_candidates[nbr_candidates+1] = edge
@inbounds edge_candidates[nbr_candidates+1] = edge
else
push!(edge_candidates, edge)
end
......@@ -110,8 +110,9 @@ function _find_edge_candidates!(edge_candidates::Vector{Edge},
only_asynchronous::Bool)
nbr_candidates = 0
for target_loc in keys(edges_from_current_loc)
if !Λ[target_loc](x) continue end
for edge in edges_from_current_loc[target_loc]
if Λ[target_loc](x) && getfield(edge, :check_constraints)(S_time, S_values, x, p)
if getfield(edge, :check_constraints)(S_time, S_values, x, p)
if getfield(edge, :transitions) == nothing
_push_edge!(edge_candidates, edge, nbr_candidates)
nbr_candidates += 1
......@@ -159,8 +160,11 @@ function next_state!(Snplus1::StateLHA, A::LHA,
turns = 0
current_values = getfield(Snplus1, :values)
current_time = getfield(Snplus1, :time)
ptr_current_loc = [getfield(Snplus1, :loc)]
current_loc = getfield(Snplus1, :loc)
Λ = getfield(A, :Λ)
flow = getfield(A, :flow)
map_edges = getfield(A, :map_edges)
if verbose
println("##### Begin next_state!")
@show xnplus1, tnplus1, tr_nplus1
......@@ -171,9 +175,9 @@ function next_state!(Snplus1::StateLHA, A::LHA,
while true
turns += 1
#edge_candidates = empty!(edge_candidates)
edges_from_current_loc = getfield(A, :map_edges)[ptr_current_loc[1]]
edges_from_current_loc = map_edges[current_loc]
# Save all edges that satisfies transition predicate (asynchronous ones)
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ),
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, Λ,
current_time, current_values, xn, p, true)
# Search the one we must chose, here the event is nothing because
# we're not processing yet the next event
......@@ -182,7 +186,7 @@ function next_state!(Snplus1::StateLHA, A::LHA,
# Should be xn here
#first_round = false
if ind_edge > 0
getfield(edge_candidates[ind_edge], :update_state!)(ptr_current_loc, current_time, current_values, xn, p)
current_loc = getfield(edge_candidates[ind_edge], :update_state!)(current_time, current_values, xn, p)
else
if verbose println("No edge fired") end
break
......@@ -191,7 +195,7 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show turns
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
@show ptr_current_loc[1]
@show current_loc
@show current_time
@show current_values
if turns == 500
......@@ -218,29 +222,29 @@ function next_state!(Snplus1::StateLHA, A::LHA,
end
# Now time flies according to the flow
for i in eachindex(current_values)
@inbounds coeff_deriv = (getfield(A, :flow)[ptr_current_loc[1]])[i]
@inbounds coeff_deriv = flow[current_loc][i]
if coeff_deriv > 0
@inbounds current_values[i] += coeff_deriv*(tnplus1 - current_time)
end
end
current_time = tnplus1
if verbose
@show ptr_current_loc[1]
@show current_loc
@show current_time
@show current_values
end
# Now firing an edge according to the event
while true
turns += 1
edges_from_current_loc = getfield(A, :map_edges)[ptr_current_loc[1]]
edges_from_current_loc = map_edges[current_loc]
# Save all edges that satisfies transition predicate (synchronous ones)
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ),
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, Λ,
current_time, current_values, xnplus1, p, false)
# Search the one we must chose
ind_edge, detected_event = _get_edge_index(edge_candidates, nbr_candidates, detected_event, tr_nplus1)
# Update the state with the chosen one (if it exists)
if ind_edge > 0
getfield(edge_candidates[ind_edge], :update_state!)(ptr_current_loc, current_time, current_values, xnplus1, p)
current_loc = getfield(edge_candidates[ind_edge], :update_state!)(current_time, current_values, xnplus1, p)
end
if ind_edge == 0 || detected_event
if verbose
......@@ -250,7 +254,7 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
@show detected_event
@show ptr_current_loc[1]
@show current_loc
@show current_time
@show current_values
else
......@@ -264,7 +268,7 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
@show detected_event
@show ptr_current_loc[1]
@show current_loc
@show current_time
@show current_values
if turns == 500
......@@ -286,7 +290,7 @@ function next_state!(Snplus1::StateLHA, A::LHA,
end
=#
end
setfield!(Snplus1, :loc, ptr_current_loc[1])
setfield!(Snplus1, :loc, current_loc)
setfield!(Snplus1, :time, current_time)
if verbose
@show Snplus1
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment