Commit b2089a2e authored by Bentriou Mahmoud's avatar Bentriou Mahmoud

Rewriting semantics for functions of a LHA, we change the signature of

check_constraints and update_state! functions of an Edge in order to
improve performance.
Acutally the gain is low, but it improved the readabilty of the code.
All tests passed.
parent b1eec5b6
......@@ -53,79 +53,79 @@ function create_automaton_F(m::ContinuousTimeModel, x1::Float64, x2::Float64, t1
# l0 loc : we construct the edges of the form l0 => (..)
# "cc" as check_constraints and "us" as update_state
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
getfield(S, :values)[$(idx_var_n)] = x[$(idx_obs_var)];
setindex!(getfield(S, :values), Inf, $(idx_var_d));
setindex!(getfield(S, :values), getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs)))
@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)];
setindex!(S_values, Inf, $(idx_var_d));
setindex!(S_values, getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs)))
# l1 loc
# l1 => l2
#=
@everywhere $(func_name(:cc, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= $t1 &&
($x1 <= getfield(S, :values)[$(idx_var_n)] <= $x2)
@everywhere $(func_name(:us, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;
setindex!(getfield(S, :values), 0, $(idx_var_d)))
@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(:cc, :l1, :l2, 3))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
istrue(getfield(S, :values)[$(idx_var_isabs)]) && getfield(S, :time) <= $t2
@everywhere $(func_name(:us, :l1, :l2, 3))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :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(:cc, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :time) >= $t1 &&
getfield(S, :values)[$(idx_var_d)] == 0
@everywhere $(func_name(:us, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2)
@everywhere $(func_name(:cc, :l1, :l2, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= $t2) &&
(getfield(S, :values)[$(idx_var_n)] < $x1 || getfield(S, :values)[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l2, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l2;)
#setindex!(getfield(S, :values), min(abs(getfield(S, :values)[$(idx_var_n)] - $x1), abs(getfield(S, :values)[$(idx_var_n)] - $x2)), $(idx_var_d)))
@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(: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;)
#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::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) <= $t1) &&
(getfield(S, :values)[$(idx_var_n)] < $x1 || getfield(S, :values)[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l3, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
setindex!(getfield(S, :values), min(sqrt((getfield(S, :time) - $t1)^2 + (getfield(S, :values)[$(idx_var_n)] - $x2)^2),
sqrt((getfield(S, :time) - $t1)^2 + (getfield(S, :values)[$(idx_var_n)] - $x1)^2)), $(idx_var_d)))
@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(:cc, :l1, :l3, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
($x1 <= getfield(S, :values)[$(idx_var_n)] <= $x2)
@everywhere $(func_name(:us, :l1, :l3, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
setindex!(getfield(S, :values), 0, $(idx_var_d)))
@everywhere $(func_name(:cc, :l1, :l3, 3))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= $t1) &&
(getfield(S, :values)[$(idx_var_n)] < $x1 || getfield(S, :values)[$(idx_var_n)] > $x2)
@everywhere $(func_name(:us, :l1, :l3, 3))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l3;
val_min = min(getfield(S, :values)[$(idx_var_d)],
min(abs(getfield(S, :values)[$(idx_var_n)] - $x1), abs(getfield(S, :values)[$(idx_var_n)] - $x2)));
setindex!(getfield(S, :values), val_min, $(idx_var_d)))
@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(: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)],
min(abs(S_values[$(idx_var_n)] - $x1), abs(S_values[$(idx_var_n)] - $x2)));
setindex!(S_values, val_min, $(idx_var_d)))
# l3 loc
# l3 => l1
@everywhere $(func_name(:cc, :l3, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l3, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :l1;
getfield(S, :values)[$(idx_var_n)] = x[$(idx_obs_var)];
setindex!(getfield(S, :values), getfield(Main, $(Meta.quot(sym_isabs_func)))(p, x), $(idx_var_isabs)))
@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)))
# l3 => l2
@everywhere $(func_name(:cc, :l3, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(getfield(S, :time) >= $t2 || istrue(getfield(S, :values)[$(idx_var_isabs)]))
@everywhere $(func_name(:us, :l3, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(S.loc = :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)
end
eval(meta_elementary_functions)
......
This diff is collapsed.
This diff is collapsed.
......@@ -44,37 +44,37 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
meta_elementary_functions = quote
# l0 loc
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setfield!(S, :loc, Symbol("l1"));
setindex!(getfield(S, :values), x[$(idx_obs_var)], $(idx_var_n));
setindex!(getfield(S, :values), 0.0, $(idx_var_d));
setindex!(getfield(S, :values), 1.0, $(idx_var_idx)))
@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));
setindex!(S_values, 0.0, $(idx_var_d));
setindex!(S_values, 1.0, $(idx_var_idx)))
# l1 loc
# l1 => l1
# Defined below
@everywhere $(func_name(:cc, :l1, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
@everywhere $(func_name(:cc, :l1, :l1, 1))(S_time::Float64, S_values::Vector{Float64}, x::Vector{Int}, p::Vector{Float64}) =
(tml = $(Tuple(timeline));
tml_idx = tml[convert(Int, getfield(S, :values)[$(idx_var_idx)])];
getfield(S, :values)[$(idx_var_t)] >= tml_idx)
@everywhere $(func_name(:us, :l1, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
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}) =
(y_obs = $(Tuple(observations));
y_obs_idx = y_obs[convert(Int, getfield(S, :values)[$(idx_var_idx)])];
setindex!(getfield(S, :values), getfield(S, :values)[$(idx_var_d)] + (getfield(S, :values)[$(idx_var_n)] - y_obs_idx)^2,
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!(getfield(S, :values), getfield(S, :values)[$(idx_var_idx)] + 1.0, $(idx_var_idx)))
setindex!(S_values, S_values[$(idx_var_idx)] + 1.0, $(idx_var_idx)))
@everywhere $(func_name(:cc, :l1, :l1, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l1, :l1, 2))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(getfield(S, :values), x[$(idx_obs_var)], $(idx_var_n)))
@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)))
# l1 => l2
@everywhere $(func_name(:cc, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
getfield(S, :values)[$(idx_var_idx)] >= ($nbr_observations + 1)
@everywhere $(func_name(:us, :l1, :l2, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setfield!(S, :loc, Symbol("l2"));
setindex!(getfield(S, :values), sqrt(getfield(S, :values)[$(idx_var_d)]), $(idx_var_d)))
@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)))
end
eval(meta_elementary_functions)
......@@ -90,11 +90,11 @@ function create_euclidean_distance_automaton(m::ContinuousTimeModel, timeline::A
map_edges[:l1][:l1] = [edge1]
for i = 1:nbr_observations
meta_edge_i = quote
@everywhere $(func_name(:cc, :l1, :l1, 1+i))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
@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::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(getfield(S, :values), S[:d] + (S[:n] - $(observations[i]))^2, $(idx_var_d));
setindex!(getfield(S, :values), S[:idx] + 1.0, $(idx_var_idx)))
@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))))
......
......@@ -46,25 +46,25 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
meta_elementary_functions = quote
# l0 loc
# l0 => l1
@everywhere $(func_name(:cc, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, :l0, :l1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setfield!(S, :loc, Symbol("l1"));
setindex!(getfield(S, :values), x[$(idx_obs_var)], $(to_idx(:n)));
setindex!(getfield(S, :values), 0.0, $(to_idx(:d))))
@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))))
# lnbr_obs => lfinal
@everywhere $(func_name(:cc, loc_nbr_obs, :lfinal, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
get_value(S, $(to_idx(:t))) >= $(timeline[nbr_observations])
@everywhere $(func_name(:us, loc_nbr_obs, :lfinal, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setfield!(S, :loc, Symbol("lfinal"));
setindex!(getfield(S, :values), get_value(S, $(to_idx(:d))) + (get_value(S, $(to_idx(:n)))-$(observations[nbr_observations]))^2,
@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!(getfield(S, :values), sqrt(get_value(S, $(to_idx(:d)))), $(to_idx(:d))))
setindex!(S_values, sqrt(S_values[$(to_idx(:d))]), $(to_idx(:d))))
# lnbr_obs => lnbr_obs
@everywhere $(func_name(:cc, loc_nbr_obs, loc_nbr_obs, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, loc_nbr_obs, loc_nbr_obs, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(getfield(S, :values), x[$(idx_obs_var)], $(to_idx(:n))))
@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))))
end
eval(meta_elementary_functions)
# l0 loc
......@@ -86,16 +86,16 @@ function create_euclidean_distance_automaton_2(m::ContinuousTimeModel, timeline:
# l1 loc
# l1 => l1
# Defined below
@everywhere $(func_name(:cc, loci, locip1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
get_value(S, $(to_idx(:t))) >= $(timeline[i])
@everywhere $(func_name(:us, loci, locip1, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setfield!(S, :loc, $(Meta.quot(locip1)));
setindex!(getfield(S, :values), get_value(S, $(to_idx(:d))) + (get_value(S, $(to_idx(:n)))-$(observations[i]))^2,
@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(:cc, loci, loci, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) = true
@everywhere $(func_name(:us, loci, loci, 1))(S::StateLHA, x::Vector{Int}, p::Vector{Float64}) =
(setindex!(getfield(S, :values), x[$(idx_obs_var)], $(to_idx(:n))))
@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))))
end
eval(meta_elementary_functions_loci)
......
This diff is collapsed.
......@@ -105,13 +105,13 @@ end
function _find_edge_candidates!(edge_candidates::Vector{Edge},
edges_from_current_loc::Dict{Location,Vector{Edge}},
Λ::Dict{Location,Function},
Snplus1::StateLHA,
S_time::Float64, S_values::Vector{Float64},
x::Vector{Int}, p::Vector{Float64},
only_asynchronous::Bool)
nbr_candidates = 0
for target_loc in keys(edges_from_current_loc)
for edge in edges_from_current_loc[target_loc]
if Λ[target_loc](x) && getfield(edge, :check_constraints)(Snplus1, x, p)
if Λ[target_loc](x) && 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
......@@ -155,26 +155,26 @@ function next_state!(Snplus1::StateLHA, A::LHA,
Sn::StateLHA, xn::Vector{Int}, p::Vector{Float64}; 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)
#first_round::Bool = true
detected_event::Bool = false
turns = 0
current_values = getfield(Snplus1, :values)
current_time = getfield(Snplus1, :time)
ptr_current_loc = [getfield(Snplus1, :loc)]
if verbose
println("##### Begin next_state!")
@show xnplus1, tnplus1, tr_nplus1
@show Sn
@show Snplus1
end
# In terms of values not reference, Snplus1 == Sn
# First, we check the asynchronous transitions
#while first_round || length(edge_candidates) > 0
while true
turns += 1
#edge_candidates = empty!(edge_candidates)
current_loc = getfield(Snplus1, :loc)
edges_from_current_loc = getfield(A, :map_edges)[current_loc]
#edge_candidates = empty!(edge_candidates)
edges_from_current_loc = getfield(A, :map_edges)[ptr_current_loc[1]]
# Save all edges that satisfies transition predicate (asynchronous ones)
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ), Sn, xn, p, true) #Snplus1=>Sn
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ),
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
ind_edge, detected_event = _get_edge_index(edge_candidates, nbr_candidates, detected_event, nothing)
......@@ -182,7 +182,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!)(Snplus1, xn, p)
getfield(edge_candidates[ind_edge], :update_state!)(ptr_current_loc, current_time, current_values, xn, p)
else
if verbose println("No edge fired") end
break
......@@ -191,8 +191,12 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show turns
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
println("After update")
@show Snplus1
@show ptr_current_loc[1]
@show current_time
@show current_values
if turns == 500
@warn "We've reached 500 turns"
end
end
# For debug
#=
......@@ -203,45 +207,40 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show tnplus1, tr_nplus1, xnplus1
@show edge_candidates
for edge in edge_candidates
@show getfield(edge, :check_constraints)(Snplus1, xn, p)
@show getfield(edge, :check_constraints)(time_S, values_S, xn, p)
end
error("Unpredicted behavior automaton")
end
=#
end
if verbose
@show Snplus1
println("Time flies with the flow...")
end
# Now time flies according to the flow
values_Snplus1 = getfield(Snplus1, :values)
time_Snplus1 = getfield(Snplus1, :time)
current_loc = getfield(Snplus1, :loc)
for i in eachindex(values_Snplus1)
@inbounds coeff_deriv = (getfield(A, :flow)[current_loc])[i]
for i in eachindex(current_values)
@inbounds coeff_deriv = (getfield(A, :flow)[ptr_current_loc[1]])[i]
if coeff_deriv > 0
@inbounds values_Snplus1[i] += coeff_deriv*(tnplus1 - time_Snplus1)
@inbounds current_values[i] += coeff_deriv*(tnplus1 - current_time)
end
end
setfield!(Snplus1, :time, tnplus1)
current_time = tnplus1
if verbose
@show Snplus1
@show ptr_current_loc[1]
@show current_time
@show current_values
end
# Now firing an edge according to the event
#first_round = true
#while first_round || length(edge_candidates) > 0
while true
turns += 1
current_loc = getfield(Snplus1, :loc)
edges_from_current_loc = getfield(A, :map_edges)[current_loc]
edges_from_current_loc = getfield(A, :map_edges)[ptr_current_loc[1]]
# Save all edges that satisfies transition predicate (synchronous ones)
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ), Snplus1, xnplus1, p, false)
nbr_candidates = _find_edge_candidates!(edge_candidates, edges_from_current_loc, getfield(A, :Λ),
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)
#first_round = false
if ind_edge > 0
getfield(edge_candidates[ind_edge], :update_state!)(Snplus1, xnplus1, p)
getfield(edge_candidates[ind_edge], :update_state!)(ptr_current_loc, current_time, current_values, xnplus1, p)
end
if ind_edge == 0 || detected_event
if verbose
......@@ -250,9 +249,10 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show turns
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
println("After update")
@show detected_event
@show Snplus1
@show ptr_current_loc[1]
@show current_time
@show current_values
else
println("No edge fired")
end
......@@ -263,9 +263,13 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show turns
@show edge_candidates
@show ind_edge, detected_event, nbr_candidates
println("After update")
@show detected_event
@show Snplus1
@show ptr_current_loc[1]
@show current_time
@show current_values
if turns == 500
@warn "We've reached 500 turns"
end
end
# For debug
#=
......@@ -276,13 +280,18 @@ function next_state!(Snplus1::StateLHA, A::LHA,
@show tnplus1, tr_nplus1, xnplus1
@show edge_candidates
for edge in edge_candidates
@show getfield(edge, :check_constraints)(Snplus1, x, p)
@show getfield(edge, :check_constraints)(time_S, values_S, x, p)
end
error("Unpredicted behavior automaton")
end
=#
end
if verbose println("##### End next_state!") end
setfield!(Snplus1, :loc, ptr_current_loc[1])
setfield!(Snplus1, :time, current_time)
if verbose
@show Snplus1
println("##### End next_state!")
end
end
# For tests purposes
......
using MarkovProcesses
load_plots()
load_model("doping_3way_oscillator")
load_automaton("period_automaton")
set_time_bound!(doping_3way_oscillator, 0.5)
set_x0!(doping_3way_oscillator, [:A, :B, :C], fill(333, 3))
set_x0!(doping_3way_oscillator, [:DA, :DB, :DC], fill(10, 3))
A_per = create_period_automaton(doping_3way_oscillator, 300.0, 360.0, 5, :A; ref_mean_tp = 0.01)
sync_doping = doping_3way_oscillator * A_per
σ = simulate(sync_doping)
test = (σ.state_lha_end[:n] == 5.0 && isapprox(σ.state_lha_end[:mean_tp], 0.01, atol = 0.011))
return test
......@@ -6,6 +6,7 @@ using Test
@test include("automata/accept_R5.jl")
@test include("automata/euclidean_distance.jl")
@test include("automata/euclidean_distance_single.jl")
@test include("automata/period_automaton_doping_single.jl")
@test include("automata/read_trajectory_last_state_F.jl")
@test include("automata/read_trajectory_last_state_G.jl")
@test include("automata/sync_simulate_last_state_F.jl")
......
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