Skip to content

Commit 035fe98

Browse files
authored
Merge pull request #242 from JuliaControl/doc_minor_mod
doc: changing the notation of transcription internals
2 parents 5f3a969 + 4bc771b commit 035fe98

File tree

8 files changed

+76
-76
lines changed

8 files changed

+76
-76
lines changed

src/controller/explicitmpc.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,9 @@ function Base.show(io::IO, mpc::ExplicitMPC)
181181
nu, nd = mpc.estim.model.nu, mpc.estim.model.nd
182182
nx̂, nym, nyu = mpc.estim.nx̂, mpc.estim.nym, mpc.estim.nyu
183183
n = maximum(ndigits.((Hp, Hc, nu, nx̂, nym, nyu, nd))) + 1
184-
println(io, "$(typeof(mpc).name.name) controller with a sample time Ts = "*
184+
println(io, "$(nameof(typeof(mpc))) controller with a sample time Ts = "*
185185
"$(mpc.estim.model.Ts) s, "*
186-
"$(typeof(mpc.estim).name.name) estimator and:")
186+
"$(nameof(typeof(mpc.estim))) estimator and:")
187187
println(io, "$(lpad(Hp, n)) prediction steps Hp")
188188
println(io, "$(lpad(Hc, n)) control steps Hc")
189189
print_estim_dim(io, mpc.estim, n)

src/controller/transcription.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1194,9 +1194,9 @@ Compute vectors if `model` is a [`NonLinModel`](@ref) and other [`TranscriptionM
11941194
The method mutates `Ŷ0` and `x̂0end` arguments. The augmented output function [`ĥ!`](@ref)
11951195
is called multiple times in a `for` loop:
11961196
```math
1197-
\mathbf{ŷ_0}(k) = \mathbf{ĥ}\Big(\mathbf{x̂_0^†}(k+j), \mathbf{d_0}(k) \Big)
1197+
\mathbf{ŷ_0}(k) = \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k) \Big)
11981198
```
1199-
in which ``\mathbf{x̂_0^†}`` is the augmented state extracted from the decision variable `Z̃`.
1199+
in which ``\mathbf{x̂_0}`` is the augmented state extracted from the decision variable `Z̃`.
12001200
"""
12011201
function predict!(
12021202
Ŷ0, x̂0end, _, _, _,
@@ -1316,10 +1316,10 @@ The method mutates the `geq`, `X̂0`, `Û0` and `K0` vectors in argument. The n
13161316
equality constraints `geq` only includes the augmented state defects, computed with:
13171317
```math
13181318
\mathbf{ŝ}(k+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big)
1319-
- \mathbf{x̂_0^†}(k+1)
1319+
- \mathbf{x̂_0}(k+1)
13201320
```
1321-
in which ``\mathbf{x̂_0^†}`` is the augmented state extracted from the decision variables
1322-
`Z̃`, and ``\mathbf{f̂}``, the augmented state function defined in [`f̂!`](@ref).
1321+
in which the augmented state ``\mathbf{x̂_0}`` are extracted from the decision variables
1322+
`Z̃`, and ``\mathbf{f̂}`` is the augmented state function defined in [`f̂!`](@ref).
13231323
"""
13241324
function con_nonlinprogeq!(
13251325
geq, X̂0, Û0, K0,
@@ -1366,18 +1366,18 @@ time state-space models, and the stochastic model of the unmeasured disturbances
13661366
is discrete-time. The deterministic and stochastic defects are respectively computed with:
13671367
```math
13681368
\begin{aligned}
1369-
\mathbf{s_d}(k+1) &= \mathbf{x_0}(k) - \mathbf{x_0^†}(k+1)
1369+
\mathbf{s_d}(k+1) &= \mathbf{x_0}(k) - \mathbf{x_0}(k+1)
13701370
+ 0.5 T_s (\mathbf{k}_1 + \mathbf{k}_2) \\
1371-
\mathbf{s_s}(k+1) &= \mathbf{A_s x_s}(k) - \mathbf{x_s^†}(k+1)
1371+
\mathbf{s_s}(k+1) &= \mathbf{A_s x_s}(k) - \mathbf{x_s}(k+1)
13721372
\end{aligned}
13731373
```
1374-
in which ``\mathbf{x_0^†}`` and ``\mathbf{x_s^†}`` are the deterministic and stochastic
1375-
states extracted from the decision variables `Z̃`. The ``\mathbf{k}`` coefficients are
1374+
in which ``\mathbf{x_0}`` and ``\mathbf{x_s}`` are the deterministic and stochastic states
1375+
extracted from the decision variables `Z̃`. The ``\mathbf{k}`` coefficients are
13761376
evaluated from the continuous-time function `model.f!` and:
13771377
```math
13781378
\begin{aligned}
1379-
\mathbf{k}_1 &= \mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d_0}(k) \Big) \\
1380-
\mathbf{k}_2 &= \mathbf{f}\Big(\mathbf{x_0^†}(k+1), \mathbf{û_0}(k), \mathbf{d_0}(k+1)\Big)
1379+
\mathbf{k}_1 &= \mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d_0}(k) \Big) \\
1380+
\mathbf{k}_2 &= \mathbf{f}\Big(\mathbf{x_0}(k+1), \mathbf{û_0}(k), \mathbf{d_0}(k+1)\Big)
13811381
\end{aligned}
13821382
```
13831383
and the input of the augmented model is:

src/estimator/kalman.jl

Lines changed: 52 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,8 @@ struct ExtendedKalmanFilter{
877877
SM<:SimModel,
878878
KC<:KalmanCovariances,
879879
JB<:AbstractADType,
880-
LF<:Function
880+
FF<:Function,
881+
HF<:Function
881882
} <: StateEstimator{NT}
882883
model::SM
883884
cov ::KC
@@ -907,13 +908,23 @@ struct ExtendedKalmanFilter{
907908
::Matrix{NT}
908909
Ĥm ::Matrix{NT}
909910
jacobian::JB
910-
linfunc!::LF
911+
linfuncF̂!::FF
912+
linfuncĤ!::HF
911913
direct::Bool
912914
corrected::Vector{Bool}
913915
buffer::StateEstimatorBuffer{NT}
914916
function ExtendedKalmanFilter{NT}(
915-
model::SM, i_ym, nint_u, nint_ym, cov::KC; jacobian::JB, linfunc!::LF, direct=true
916-
) where {NT<:Real, SM<:SimModel, KC<:KalmanCovariances, JB<:AbstractADType, LF<:Function}
917+
model::SM,
918+
i_ym, nint_u, nint_ym, cov::KC;
919+
jacobian::JB, linfuncF̂!::FF, linfuncĤ!::HF, direct=true
920+
) where {
921+
NT<:Real,
922+
SM<:SimModel,
923+
KC<:KalmanCovariances,
924+
JB<:AbstractADType,
925+
FF<:Function,
926+
HF<:Function
927+
}
917928
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
918929
nym, nyu = validate_ym(model, i_ym)
919930
As, Cs_u, Cs_y, nint_u, nint_ym = init_estimstoch(model, i_ym, nint_u, nint_ym)
@@ -927,7 +938,7 @@ struct ExtendedKalmanFilter{
927938
Ĥ, Ĥm = zeros(NT, ny, nx̂), zeros(NT, nym, nx̂)
928939
corrected = [false]
929940
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd, nk)
930-
return new{NT, SM, KC, JB, LF}(
941+
return new{NT, SM, KC, JB, FF, HF}(
931942
model,
932943
cov,
933944
x̂op, f̂op, x̂0,
@@ -936,7 +947,7 @@ struct ExtendedKalmanFilter{
936947
Â, B̂u, Ĉ, B̂d, D̂d, Ĉm, D̂dm,
937948
K̂,
938949
F̂_û, F̂, Ĥ, Ĥm,
939-
jacobian, linfunc!,
950+
jacobian, linfuncF̂!, linfuncĤ!,
940951
direct, corrected,
941952
buffer
942953
)
@@ -1044,60 +1055,54 @@ function ExtendedKalmanFilter(
10441055
) where {NT<:Real, SM<:SimModel{NT}}
10451056
P̂_0, Q̂, R̂ = to_mat(P̂_0), to_mat(Q̂), to_mat(R̂)
10461057
cov = KalmanCovariances(model, i_ym, nint_u, nint_ym, Q̂, R̂, P̂_0)
1047-
linfunc! = get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
1058+
linfuncF̂!, linfuncĤ! = get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian)
10481059
return ExtendedKalmanFilter{NT}(
1049-
model, i_ym, nint_u, nint_ym, cov; jacobian, direct, linfunc!
1060+
model, i_ym, nint_u, nint_ym, cov; jacobian, linfuncF̂!, linfuncĤ!, direct
10501061
)
10511062
end
10521063

10531064
"""
1054-
get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian) -> linfunc!
1065+
get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian) -> linfuncF̂!, linfuncĤ!
10551066
1056-
Return the `linfunc!` function that computes the Jacobians of the augmented model.
1067+
Return the functions that computes the `F̂` and `Ĥ` Jacobians of the augmented model.
10571068
1058-
The function has the two following methods:
1069+
The functions has the following signatures:
10591070
```
1060-
linfunc!(x̂0next , ::Nothing, F̂ , ::Nothing, backend, x̂0, cst_u0, cst_d0) -> nothing
1061-
linfunc!(::Nothing, ŷ0 , ::Nothing, Ĥ , backend, x̂0, _ , cst_d0) -> nothing
1071+
linfuncF̂!(F̂, x̂0next , backend, x̂0, cst_u0, cst_d0) -> nothing
1072+
linfuncĤ!(Ĥ, ŷ0 , backend, x̂0, cst_d0) -> nothing
10621073
```
1063-
To respectively compute only `F̂` or `Ĥ` Jacobian. The methods mutate all the arguments
1064-
before `backend` argument. The `backend` argument is an `AbstractADType` object from
1074+
They mutates all the arguments before `backend`, which is an `AbstractADType` object from
10651075
`DifferentiationInterface`. The `cst_u0` and `cst_d0` are `DifferentiationInterface.Constant`
10661076
objects with the linearization points.
10671077
"""
1068-
function get_ekf_linfunc(NT, model, i_ym, nint_u, nint_ym, jacobian)
1078+
function get_ekf_linfuncs(NT, model, i_ym, nint_u, nint_ym, jacobian)
10691079
As, Cs_u, Cs_y = init_estimstoch(model, i_ym, nint_u, nint_ym)
1070-
nxs = size(As, 1)
1071-
x̂op, f̂op = [model.xop; zeros(nxs)], [model.fop; zeros(nxs)]
1072-
f̂_ekf!(x̂0next, x̂0, û0, k0, u0, d0) = f̂!(
1073-
x̂0next, û0, k0, model, As, Cs_u, f̂op, x̂op, x̂0, u0, d0
1074-
)
1075-
ĥ_ekf!(ŷ0, x̂0, d0) = ĥ!(
1076-
ŷ0, model, Cs_y, x̂0, d0
1077-
)
1078-
strict = Val(true)
10791080
nu, ny, nd, nk = model.nu, model.ny, model.nd, model.nk
1080-
nx̂ = model.nx + nxs
1081+
nx̂ = model.nx + size(As, 1)
1082+
x̂op = f̂op = zeros(nx̂) # not important for Jacobian computations
1083+
function f̂_ekf!(x̂0next, x̂0, û0, k0, u0, d0)
1084+
return f̂!(x̂0next, û0, k0, model, As, Cs_u, f̂op, x̂op, x̂0, u0, d0)
1085+
end
1086+
ĥ_ekf!(ŷ0, x̂0, d0) = ĥ!(ŷ0, model, Cs_y, x̂0, d0)
1087+
strict = Val(true)
10811088
x̂0next = zeros(NT, nx̂)
10821089
ŷ0 = zeros(NT, ny)
10831090
x̂0 = zeros(NT, nx̂)
1084-
tmp_û0 = Cache(zeros(NT, nu))
1085-
tmp_x0i = Cache(zeros(NT, nk))
1091+
û0 = Cache(zeros(NT, nu))
1092+
k0 = Cache(zeros(NT, nk))
10861093
cst_u0 = Constant(zeros(NT, nu))
10871094
cst_d0 = Constant(zeros(NT, nd))
1088-
F̂_prep = prepare_jacobian(
1089-
f̂_ekf!, x̂0next, jacobian, x̂0, tmp_û0, tmp_x0i, cst_u0, cst_d0; strict
1095+
F̂prep = prepare_jacobian(
1096+
f̂_ekf!, x̂0next, jacobian, x̂0, û0, k0, cst_u0, cst_d0; strict
10901097
)
1091-
Ĥ_prep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1092-
function linfunc!(x̂0next, ŷ0::Nothing, F̂, Ĥ::Nothing, backend, x̂0, cst_u0, cst_d0)
1093-
return jacobian!(
1094-
f̂_ekf!, x̂0next, F̂, F̂_prep, backend, x̂0, tmp_û0, tmp_x0i, cst_u0, cst_d0
1095-
)
1098+
Ĥprep = prepare_jacobian(ĥ_ekf!, ŷ0, jacobian, x̂0, cst_d0; strict)
1099+
function linfuncF̂!(F̂, x̂0next, backend, x̂0, cst_u0, cst_d0)
1100+
return jacobian!(f̂_ekf!, x̂0next, F̂, F̂prep, backend, x̂0, û0, k0, cst_u0, cst_d0)
10961101
end
1097-
function linfunc!(x̂0next::Nothing, ŷ0, ::Nothing, Ĥ, backend, x̂0, _ , cst_d0)
1098-
return jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥ_prep, backend, x̂0, cst_d0)
1102+
function linfuncĤ!(Ĥ, ŷ0, backend, x̂0, cst_d0)
1103+
return jacobian!(ĥ_ekf!, ŷ0, Ĥ, Ĥprep, backend, x̂0, cst_d0)
10991104
end
1100-
return linfunc!
1105+
return linfuncF̂!, linfuncĤ!
11011106
end
11021107

11031108
"""
@@ -1106,12 +1111,12 @@ end
11061111
Do the same but for the [`ExtendedKalmanFilter`](@ref).
11071112
"""
11081113
function correct_estimate!(estim::ExtendedKalmanFilter, y0m, d0)
1109-
model, x̂0 = estim.model, estim.x̂0
1114+
x̂0 = estim.x̂0
11101115
cst_d0 = Constant(d0)
1111-
ŷ0, Ĥ = estim.buffer.ŷ, estim.
1112-
estim.linfunc!(nothing, ŷ0, nothing, Ĥ, estim.jacobian, x̂0, nothing, cst_d0)
1113-
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
1114-
return correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
1116+
ŷ0, Ĥ, Ĥm = estim.buffer.ŷ, estim., estim.Ĥm
1117+
estim.linfuncĤ!(Ĥ, ŷ0, estim.jacobian, x̂0, cst_d0)
1118+
Ĥm .= @views Ĥ[estim.i_ym, :]
1119+
return correct_estimate_kf!(estim, y0m, d0, Ĥm)
11151120
end
11161121

11171122

@@ -1154,19 +1159,14 @@ and prediction step equations are provided below. The correction step is skipped
11541159
```
11551160
"""
11561161
function update_estimate!(estim::ExtendedKalmanFilter{NT}, y0m, d0, u0) where NT<:Real
1157-
model, x̂0 = estim.model, estim.x̂0
1158-
nx̂, nu = estim.nx̂, model.nu
1159-
cst_u0, cst_d0 = Constant(u0), Constant(d0)
11601162
if !estim.direct
1161-
ŷ0, Ĥ = estim.buffer.ŷ, estim.
1162-
estim.linfunc!(nothing, ŷ0, nothing, Ĥ, estim.jacobian, x̂0, nothing, cst_d0)
1163-
estim.Ĥm .= @views estim.Ĥ[estim.i_ym, :]
1164-
correct_estimate_kf!(estim, y0m, d0, estim.Ĥm)
1163+
correct_estimate!(estim, y0m, d0)
11651164
end
1165+
cst_u0, cst_d0 = Constant(u0), Constant(d0)
11661166
x̂0corr = estim.x̂0
11671167
x̂0next, F̂ = estim.buffer.x̂, estim.
1168-
estim.linfunc!(x̂0next, nothing, F̂, nothing, estim.jacobian, x̂0corr, cst_u0, cst_d0)
1169-
return predict_estimate_kf!(estim, u0, d0, estim.F̂)
1168+
estim.linfuncF̂!(F̂, x̂0next, estim.jacobian, x̂0corr, cst_u0, cst_d0)
1169+
return predict_estimate_kf!(estim, u0, d0, F̂)
11701170
end
11711171

11721172
"Set `estim.cov.P̂` to `estim.cov.P̂_0` for the time-varying Kalman Filters."

src/estimator/mhe.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ function Base.show(io::IO, estim::MovingHorizonEstimator)
55
nu, nd = estim.model.nu, estim.model.nd
66
nx̂, nym, nyu = estim.nx̂, estim.nym, estim.nyu
77
n = maximum(ndigits.((nu, nx̂, nym, nyu, nd))) + 1
8-
println(io, "$(typeof(estim).name.name) estimator with a sample time "*
8+
println(io, "$(nameof(typeof(estim))) estimator with a sample time "*
99
"Ts = $(estim.model.Ts) s, $(JuMP.solver_name(estim.optim)) optimizer, "*
10-
"$(typeof(estim.model).name.name) and:")
10+
"$(nameof(typeof(estim.model))) and:")
1111
print_estim_dim(io, estim, n)
1212
end
1313

src/plot_sim.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ get_nx̂(mpc::PredictiveController) = mpc.estim.nx̂
101101

102102
function Base.show(io::IO, res::SimResult)
103103
N = length(res.T_data)
104-
print(io, "Simulation results of $(typeof(res.obj).name.name) with $N time steps.")
104+
print(io, "Simulation results of $(nameof(typeof(res.obj))) with $N time steps.")
105105
end
106106

107107

@@ -137,7 +137,7 @@ function sim!(
137137
D_data = Matrix{NT}(undef, plant.nd, N)
138138
X_data = Matrix{NT}(undef, plant.nx, N)
139139
setstate!(plant, x_0)
140-
@progress name="$(typeof(plant).name.name) simulation" for i=1:N
140+
@progress name="$(nameof(typeof(plant))) simulation" for i=1:N
141141
y = evaloutput(plant, d)
142142
Y_data[:, i] .= y
143143
U_data[:, i] .= u
@@ -282,7 +282,7 @@ function sim_closedloop!(
282282
lastd, lasty = d, evaloutput(plant, d)
283283
initstate!(est_mpc, lastu, lasty[estim.i_ym], lastd)
284284
isnothing(x̂_0) || setstate!(est_mpc, x̂_0)
285-
@progress name="$(typeof(est_mpc).name.name) simulation" for i=1:N
285+
@progress name="$(nameof(typeof(est_mpc))) simulation" for i=1:N
286286
d = lastd + d_step + d_noise.*randn(plant.nd)
287287
y = evaloutput(plant, d) + y_step + y_noise.*randn(plant.ny)
288288
ym = y[estim.i_ym]

src/predictive_control.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ function Base.show(io::IO, mpc::PredictiveController)
3131
nu, nd = mpc.estim.model.nu, mpc.estim.model.nd
3232
nx̂, nym, nyu = mpc.estim.nx̂, mpc.estim.nym, mpc.estim.nyu
3333
n = maximum(ndigits.((Hp, Hc, nu, nx̂, nym, nyu, nd))) + 1
34-
println(io, "$(typeof(mpc).name.name) controller with a sample time Ts = "*
34+
println(io, "$(nameof(typeof(mpc))) controller with a sample time Ts = "*
3535
"$(mpc.estim.model.Ts) s, $(JuMP.solver_name(mpc.optim)) optimizer, "*
36-
"$(typeof(mpc.transcription).name.name) transcription, "*
37-
"$(typeof(mpc.estim).name.name) estimator and:")
36+
"$(nameof(typeof(mpc.transcription))) transcription, "*
37+
"$(nameof(typeof(mpc.estim))) estimator and:")
3838
println(io, "$(lpad(Hp, n)) prediction steps Hp")
3939
println(io, "$(lpad(Hc, n)) control steps Hc")
4040
println(io, "$(lpad(nϵ, n)) slack variable ϵ (control constraints)")

src/sim_model.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ function Base.show(io::IO, model::SimModel)
372372
nu, nd = model.nu, model.nd
373373
nx, ny = model.nx, model.ny
374374
n = maximum(ndigits.((nu, nx, ny, nd))) + 1
375-
println(io, "$(typeof(model).name.name) with a sample time Ts = $(model.Ts) s"*
375+
println(io, "$(nameof(typeof(model))) with a sample time Ts = $(model.Ts) s"*
376376
"$(detailstr(model)) and:")
377377
println(io, "$(lpad(nu, n)) manipulated inputs u")
378378
println(io, "$(lpad(nx, n)) states x")

src/state_estim.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ function Base.show(io::IO, estim::StateEstimator)
3232
nu, nd = estim.model.nu, estim.model.nd
3333
nx̂, nym, nyu = estim.nx̂, estim.nym, estim.nyu
3434
n = maximum(ndigits.((nu, nx̂, nym, nyu, nd))) + 1
35-
println(io, "$(typeof(estim).name.name) estimator with a sample time "*
36-
"Ts = $(estim.model.Ts) s, $(typeof(estim.model).name.name) and:")
35+
println(io, "$(nameof(typeof(estim))) estimator with a sample time "*
36+
"Ts = $(estim.model.Ts) s, $(nameof(typeof(estim.model))) and:")
3737
print_estim_dim(io, estim, n)
3838
end
3939

0 commit comments

Comments
 (0)