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

Forgot the file core/utils.jl in previous commit

Add of several useful functions for access values of model, lha and
trajectory objects.
parent 0a2d2796
No related branches found
No related tags found
No related merge requests found
...@@ -22,14 +22,18 @@ export load_automaton, get_index, get_value, length_var ...@@ -22,14 +22,18 @@ export load_automaton, get_index, get_value, length_var
# Model related methods # Model related methods
export simulate, set_param!, get_param, set_observed_var! export simulate, set_param!, get_param, set_observed_var!
export is_bounded export is_bounded, check_consistency
export load_model, get_module_path export load_model, get_module_path
# Utils
export get_module_path
include("common.jl") include("common.jl")
include("trajectory.jl") include("trajectory.jl")
include("lha.jl") include("lha.jl")
include("model.jl") include("model.jl")
include("utils.jl")
end end
...@@ -88,4 +88,6 @@ end ...@@ -88,4 +88,6 @@ end
LHA(A::LHA, map_var::Dict{String,Int}) = LHA(A.l_transitions, A.l_loc, A.Λ, LHA(A::LHA, map_var::Dict{String,Int}) = LHA(A.l_transitions, A.l_loc, A.Λ,
A.l_loc_init, A.l_loc_final, A.map_var_automaton_idx, A.l_flow, A.l_loc_init, A.l_loc_final, A.map_var_automaton_idx, A.l_flow,
A.map_edges, A.l_ctes, map_var) A.map_edges, A.l_ctes, map_var)
Base.:*(m::ContinuousTimeModel, A::LHA) = SynchronizedModel(m, A)
Base.:*(A::LHA, m::ContinuousTimeModel) = SynchronizedModel(m, A)
...@@ -19,6 +19,8 @@ function Base.show(io::IO, S::StateLHA) ...@@ -19,6 +19,8 @@ function Base.show(io::IO, S::StateLHA)
end end
end end
isaccepted(S::StateLHA) = S.loc in (S.A).l_loc_final
# Methods for synchronize / read the trajectory # Methods for synchronize / read the trajectory
function init_state(A::LHA, x0::SubArray{Int,1}, t0::Float64) function init_state(A::LHA, x0::SubArray{Int,1}, t0::Float64)
S0 = StateLHA(A, "", zeros(length_var(A)), t0) S0 = StateLHA(A, "", zeros(length_var(A)), t0)
......
load_model(name_model::String) = include(get_module_path() * "/models/" * name_model * ".jl")
# Simulation # Simulation
function simulate(m::ContinuousTimeModel) function simulate(m::ContinuousTimeModel)
# trajectory fields # trajectory fields
...@@ -120,14 +122,18 @@ function set_observed_var!(m::Model,g::Vector{String}) ...@@ -120,14 +122,18 @@ function set_observed_var!(m::Model,g::Vector{String})
m._map_obs_var_idx = _map_obs_var_idx m._map_obs_var_idx = _map_obs_var_idx
end end
is_bounded(m::Model) = m.time_bound < Inf is_bounded(m::ContinuousTimeModel) = m.time_bound < Inf
function check_consistency(m::Model) end function check_consistency(m::ContinuousTimeModel)
function simulate(m::Model, n::Int; bound::Float64 = Inf)::AbstractObservations end @assert m.d == length(m.map_var_idx)
function set_param!(m::Model, p::Vector{Float64})::Nothing end @assert m.k == length(m.map_param_idx)
function get_param(m::Model)::Vector{Float64} end @assert m.k == length(m.p)
@assert length(m.g) <= m.d
get_module_path() = dirname(dirname(pathof(@__MODULE__))) @assert length(m._g_idx) == length(m.g)
function load_model(name_model::String) @assert m.buffer_size >= 0
include(get_module_path() * "/models/" * name_model * ".jl") @assert typeof(m.is_absorbing(m.p, view(reshape(m.x0, 1, m.d), 1, :))) == Bool
return true
end end
set_param!(m::ContinuousTimeModel, p::Vector{Float64}) = (m.p = p)
set_param!(m::ContinuousTimeModel, name_p::String, p_i::Float64) = (m.p[m.map_param_idx[name_p]] = p_i)
get_param(m::ContinuousTimeModel) = m.p
get_module_path() = dirname(dirname(pathof(@__MODULE__)))
...@@ -5,10 +5,10 @@ d=4 ...@@ -5,10 +5,10 @@ d=4
k=3 k=3
dict_var = Dict("E" => 1, "S" => 2, "ES" => 3, "P" => 4) dict_var = Dict("E" => 1, "S" => 2, "ES" => 3, "P" => 4)
dict_p = Dict("k1" => 1, "k2" => 2, "k3" => 3) dict_p = Dict("k1" => 1, "k2" => 2, "k3" => 3)
l_tr = ["R1","R2","R3"] l_tr_ER = ["R1","R2","R3"]
p = [1.0, 1.0, 1.0] p_ER = [1.0, 1.0, 1.0]
x0 = [100, 100, 0, 0] x0_ER = [100, 100, 0, 0]
t0 = 0.0 t0_ER = 0.0
function ER_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, idx::Int, function ER_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, idx::Int,
xn::SubArray{Int,1}, tn::Float64, p::Vector{Float64}) xn::SubArray{Int,1}, tn::Float64, p::Vector{Float64})
a1 = p[1] * xn[1] * xn[2] a1 = p[1] * xn[1] * xn[2]
...@@ -43,8 +43,8 @@ function ER_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, i ...@@ -43,8 +43,8 @@ function ER_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, i
end end
is_absorbing_ER(p::Vector{Float64},xn::SubArray{Int,1}) = is_absorbing_ER(p::Vector{Float64},xn::SubArray{Int,1}) =
(p[1]*xn[1]*xn[2] + (p[2]+p[3])*xn[3]) === 0.0 (p[1]*xn[1]*xn[2] + (p[2]+p[3])*xn[3]) === 0.0
g = ["P"] g_ER = ["P"]
ER = ContinuousTimeModel(d,k,dict_var,dict_p,l_tr,p,x0,t0,ER_f!,is_absorbing_ER; g=g) ER = ContinuousTimeModel(d,k,dict_var,dict_p,l_tr_ER,p_ER,x0_ER,t0_ER,ER_f!,is_absorbing_ER; g=g_ER)
export ER export ER
...@@ -5,10 +5,10 @@ d=3 ...@@ -5,10 +5,10 @@ d=3
k=2 k=2
dict_var = Dict("S" => 1, "I" => 2, "R" => 3) dict_var = Dict("S" => 1, "I" => 2, "R" => 3)
dict_p = Dict("ki" => 1, "kr" => 2) dict_p = Dict("ki" => 1, "kr" => 2)
l_tr = ["R1","R2"] l_tr_SIR = ["R1","R2"]
p = [0.0012, 0.05] p_SIR = [0.0012, 0.05]
x0 = [95, 5, 0] x0_SIR = [95, 5, 0]
t0 = 0.0 t0_SIR = 0.0
function SIR_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, idx::Int, function SIR_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, idx::Int,
xn::SubArray{Int,1}, tn::Float64, p::Vector{Float64}) xn::SubArray{Int,1}, tn::Float64, p::Vector{Float64})
a1 = p[1] * xn[1] * xn[2] a1 = p[1] * xn[1] * xn[2]
...@@ -42,8 +42,8 @@ function SIR_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String}, ...@@ -42,8 +42,8 @@ function SIR_f!(mat_x::Matrix{Int}, l_t::Vector{Float64}, l_tr::Vector{String},
l_tr[idx] = "R$(reaction)" l_tr[idx] = "R$(reaction)"
end end
is_absorbing_SIR(p::Vector{Float64}, xn::SubArray{Int,1}) = (p[1]*xn[1]*xn[2] + p[2]*xn[2]) === 0.0 is_absorbing_SIR(p::Vector{Float64}, xn::SubArray{Int,1}) = (p[1]*xn[1]*xn[2] + p[2]*xn[2]) === 0.0
g = ["I"] g_SIR = ["I"]
SIR = ContinuousTimeModel(d,k,dict_var,dict_p,l_tr,p,x0,t0,SIR_f!,is_absorbing_SIR; g=g) SIR = ContinuousTimeModel(d,k,dict_var,dict_p,l_tr_SIR,p_SIR,x0_SIR,t0_SIR,SIR_f!,is_absorbing_SIR; g=g_SIR)
export SIR export SIR
...@@ -11,7 +11,7 @@ A_F = create_automaton_F(SIR, x1, x2, t1, t2, "I") # <: LHA ...@@ -11,7 +11,7 @@ A_F = create_automaton_F(SIR, x1, x2, t1, t2, "I") # <: LHA
function test_last_state(A::LHA, m::ContinuousTimeModel) function test_last_state(A::LHA, m::ContinuousTimeModel)
σ = simulate(m) σ = simulate(m)
Send = read_trajectory(A, σ) Send = read_trajectory(A, σ)
test = (get_state_from_time(σ, Send.time)[1] == Send["n"]) & (Send["d"] == 0) test = (get_state_from_time(σ, Send.time)[1] == Send["n"]) && (Send["d"] == 0)
#= #=
if !test if !test
@show Send @show Send
...@@ -25,7 +25,7 @@ test_all = true ...@@ -25,7 +25,7 @@ test_all = true
nbr_sim = 10000 nbr_sim = 10000
for i = 1:nbr_sim for i = 1:nbr_sim
test = test_last_state(A_F, SIR) test = test_last_state(A_F, SIR)
global test_all = test_all & test global test_all = test_all && test
end end
return test_all return test_all
......
...@@ -17,5 +17,8 @@ using Test ...@@ -17,5 +17,8 @@ using Test
@test include("unit/dist_lp.jl") @test include("unit/dist_lp.jl")
@test include("unit/l_dist_lp.jl") @test include("unit/l_dist_lp.jl")
@test include("unit/check_trajectory_consistency.jl") @test include("unit/check_trajectory_consistency.jl")
@test include("unit/check_model_consistency.jl")
@test include("unit/set_param.jl")
@test include("unit/side_effects_1.jl")
end end
using MarkovProcesses
load_model("SIR")
load_model("ER")
return check_consistency(ER) && check_consistency(SIR)
using MarkovProcesses
load_model("SIR") # p = 0.0012, 0.05
load_model("ER") # p = 1; 1; 1
test_all = true
new_param_ER = [1.0, 2.0, 1.5]
set_param!(ER, new_param_ER)
test_all = test_all && (ER.p == new_param_ER)
k2 = 4.0
set_param!(ER, "k2", k2)
test_all = test_all && (ER.p == [1.0, 4.0, 1.5])
k1 = 0.5
set_param!(ER, "k1", k1)
test_all = test_all && (ER.p == [0.5, 4.0, 1.5])
k3 = 10.0
set_param!(ER, "k3", 10.0)
test_all = test_all && (ER.p == [0.5, 4.0, 10.0])
new_param_SIR = [0.0013, 0.08]
set_param!(SIR, new_param_SIR)
test_all = test_all && (SIR.p == new_param_SIR)
kr = 0.06
set_param!(SIR, "kr", kr)
test_all = test_all && (SIR.p == [0.0013, 0.06])
ki = 0.011
set_param!(SIR, "ki", ki)
test_all = test_all && (SIR.p == [0.011, 0.06])
return test_all
using MarkovProcesses
load_model("SIR")
load_model("ER")
test = SIR.map_var_idx !== ER.map_var_idx &&
SIR.map_param_idx !== ER.map_var_idx &&
SIR.l_transitions !== ER.l_transitions &&
SIR.g !== ER.g &&
SIR._g_idx !== ER._g_idx &&
SIR.f! != ER.f! &&
SIR.is_absorbing != ER.is_absorbing
return test
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