From 8bd094c89e4badd1f6885265e3e48e66463a29d1 Mon Sep 17 00:00:00 2001 From: Mahmoud Bentriou <mahmoud.bentriou@centralesupelec.fr> Date: Mon, 7 Dec 2020 10:10:03 +0100 Subject: [PATCH] renaming a field in synchronized trajectory + pretty prints --- core/common.jl | 2 +- core/model.jl | 18 +++++++++--------- core/trajectory.jl | 39 +++++++++++++++++++++++++++++++++++---- 3 files changed, 45 insertions(+), 14 deletions(-) diff --git a/core/common.jl b/core/common.jl index 718d609..08f0b38 100644 --- a/core/common.jl +++ b/core/common.jl @@ -69,7 +69,7 @@ end struct SynchronizedTrajectory <: AbstractTrajectory state_lha_end::StateLHA - m::SynchronizedModel + sm::SynchronizedModel values::Vector{Vector{Int}} times::Vector{Float64} transitions::Vector{Transition} diff --git a/core/model.jl b/core/model.jl index edae92b..f4b9dd5 100644 --- a/core/model.jl +++ b/core/model.jl @@ -364,22 +364,22 @@ function distribute_prob_accept_lha(sm::SynchronizedModel, nbr_sim::Int) end function Base.show(io::IO, m::ContinuousTimeModel) - print(io, "$(m.name) model\n") + print(io, "$(m.name) model (ContinuousTimeModel)\n") print(io, "- variables :\n") for (var, idx) in m.map_var_idx - print(io, "* $var (idx = $idx in state space)\n") + print(io, "* $var (index = $idx in state space)\n") end print(io, "- parameters :\n") for (param, idx) in m.map_param_idx - print(io, "* $param (idx = $idx in parameter space)\n") + print(io, "* $param (index = $idx in parameter space)\n") end - print(io, "- transitions : $(m.transitions)\n") + print(io, "- transitions : $(join(m.transitions,','))\n") print(io, "- observed variables :\n") for i in eachindex(m.g) - print(io, "* $(m.g[i]) (idx = $i in observed state space)\n") + print(io, "* $(m.g[i]) (index = $i in observed state space, index = $(m._g_idx[i]) in state space)\n") end print(io, "p = $(m.p)\n") - print(io, "x0 = $(m.x0) (in full state space)\n") + print(io, "x0 = $(m.x0)\n") print(io, "t0 = $(m.t0)\n") print(io, "time bound = $(m.time_bound)") end @@ -424,7 +424,7 @@ function observe_all!(am::Model) end function set_param!(am::Model, new_p::Vector{Float64}) m = get_proba_model(am) - @assert length(new_p) == m.dim_params + @assert length(new_p) == m.dim_params "New parameter vector hasn't the same dimension of parameter space" m.p = new_p end function set_param!(am::Model, name_p::String, p_i::Float64) @@ -433,14 +433,14 @@ function set_param!(am::Model, name_p::String, p_i::Float64) end function set_param!(am::Model, l_name_p::Vector{String}, p::Vector{Float64}) m = get_proba_model(am) - @assert length(l_name_p) == length(p) + @assert length(l_name_p) == length(p) "Parameter names vector and parameter values haven't the same dimensions" for i = eachindex(l_name_p) set_param!(m, l_name_p[i], p[i]) end end function set_x0!(am::Model, new_x0::Vector{Int}) m = get_proba_model(am) - @assert length(new_x0) == m.dim_state + @assert length(new_x0) == m.dim_state "New x0 vector hasn't the dimension of state space" m.x0 = new_x0 end set_time_bound!(am::Model, b::Float64) = (get_proba_model(am).time_bound = b) diff --git a/core/trajectory.jl b/core/trajectory.jl index a337415..f1c11a2 100644 --- a/core/trajectory.jl +++ b/core/trajectory.jl @@ -151,7 +151,7 @@ end function check_consistency(σ::AbstractTrajectory) test_length_var = true - for i = 1:get_proba_model(σ.m).dim_obs_state + for i = 1:σ.m.dim_obs_state test_length_i = (length(σ.values[1]) == length(σ.values[i])) test_length_var = test_length_var && test_length_i end @@ -159,20 +159,44 @@ function check_consistency(σ::AbstractTrajectory) (length(σ.times) == length(σ.values[1])) && test_length_var end - @assert length_obs_var(σ) == get_proba_model(σ.m).dim_obs_state + @assert length_obs_var(σ) == σ.m.dim_obs_state return true end +function Base.show(io::IO, σ::Trajectory) + print(io, "Trajectory\n") + print(io, "- Model name: $(σ.m.name) \n") + print(io, "- Variable trajectories:\n") + for obs_var in σ.m.g + print(io, "* $obs_var: $(σ[obs_var])\n") + end + print(io, "- times = $(times(σ))\n") + print(io, "- transitions = $(transitions(σ))") +end +function Base.show(io::IO, σ::SynchronizedTrajectory) + print(io, "SynchronizedTrajectory\n") + print(io, "End LHA state:\n") + print(io, σ.state_lha_end) + print(io, "\n") + print(io, "- Model name: $(σ.m.name) \n") + print(io, "- Variable trajectories:\n") + for obs_var in σ.m.g + print(io, "* $obs_var: $(σ[obs_var])\n") + end + print(io, "- times = $(times(σ))\n") + print(io, "- transitions = $(transitions(σ))") +end + # Properties of the trajectory length_states(σ::AbstractTrajectory) = length(σ.times) length_obs_var(σ::AbstractTrajectory) = length(σ.values) -get_obs_var(σ::AbstractTrajectory) = get_proba_model(σ.m).g +get_obs_var(σ::AbstractTrajectory) = σ.m.g isbounded(σ::AbstractTrajectory) = σ.transitions[end] == nothing && length_states(σ) >= 2 isaccepted(σ::SynchronizedTrajectory) = isaccepted(σ.state_lha_end) issteadystate(σ::AbstractTrajectory) = @warn "Unimplemented" # Access to trajectory values -get_var_values(σ::AbstractTrajectory, var::String) = σ.values[get_proba_model(σ.m)._map_obs_var_idx[var]] +get_var_values(σ::AbstractTrajectory, var::String) = σ.values[σ.m._map_obs_var_idx[var]] get_state(σ::AbstractTrajectory, idx::Int) = [σ.values[i][idx] for i = 1:length(σ.values)] # /!\ Creates an array get_value(σ::AbstractTrajectory, var::String, idx::Int) = get_var_values(σ, var)[idx] # Operation σ@t @@ -194,6 +218,13 @@ function get_state_from_time(σ::AbstractTrajectory, t::Float64) end error("Unexpected behavior") end +function getproperty(σ::SynchronizedTrajectory, sym::Symbol) + if sym == :m + return (σ.sm).m + else + return getfield(σ, sym) + end +end states(σ::AbstractTrajectory) = σ.values times(σ::AbstractTrajectory) = σ.times -- GitLab