Skip to content

Commit 72373e7

Browse files
authored
Merge pull request #427 from JuliaControl/orthocolloc_mhe
added: 📐 `OrthogonalCollocation` 📐 for `MovingHorizonEstimator`
2 parents 376d683 + 5c13983 commit 72373e7

15 files changed

Lines changed: 621 additions & 225 deletions

‎Project.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
name = "ModelPredictiveControl"
22
uuid = "61f9bdb8-6ae4-484a-811f-bbf86720c31c"
3-
version = "2.11.0"
3+
version = "2.12.0"
44
authors = ["Francis Gagnon"]
55

66
[deps]

‎docs/make.jl‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ makedocs(
5555
"Plant Models" => "internals/sim_model.md",
5656
"State Estimators" => "internals/state_estim.md",
5757
"Predictive Controllers" => "internals/predictive_control.md",
58+
"Miscellaneous" => "internals/misc.md"
5859
],
5960
],
6061
"Index" => "func_index.md"

‎docs/src/index.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ Depth = 1
5858
Pages = [
5959
joinpath("internals", "sim_model.md"),
6060
joinpath("internals", "state_estim.md"),
61-
joinpath("internals", "predictive_control.md")
61+
joinpath("internals", "predictive_control.md"),
62+
joinpath("internals", "misc.md")
6263
]
6364
```

‎docs/src/internals/misc.md‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Functions: Miscellaneous Internals
2+
3+
```@contents
4+
Pages = ["misc.md"]
5+
```
6+
7+
## Orthogonal Collocation
8+
9+
```@docs
10+
ModelPredictiveControl.init_orthocolloc
11+
```

‎docs/src/internals/predictive_control.md‎

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ The prediction methodology of this module is mainly based on Maciejowski textboo
1515
ModelPredictiveControl.move_blocking
1616
ModelPredictiveControl.init_ZtoΔU
1717
ModelPredictiveControl.init_ZtoU
18-
ModelPredictiveControl.init_orthocolloc
1918
ModelPredictiveControl.init_predmat
2019
ModelPredictiveControl.init_defectmat
2120
ModelPredictiveControl.relaxU
@@ -45,7 +44,6 @@ ModelPredictiveControl.linconstrainteq!
4544
ModelPredictiveControl.optim_objective!(::PredictiveController)
4645
ModelPredictiveControl.set_warmstart_mpc!
4746
ModelPredictiveControl.predict!
48-
ModelPredictiveControl.disturbedinput!
4947
ModelPredictiveControl.con_nonlinprog!
5048
ModelPredictiveControl.con_nonlinprogeq!
5149
ModelPredictiveControl.getinput!

‎docs/src/internals/state_estim.md‎

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -69,24 +69,6 @@ ModelPredictiveControl.f̂!
6969
ModelPredictiveControl.ĥ!
7070
```
7171

72-
## Update Quadratic Optimization
73-
74-
```@docs
75-
ModelPredictiveControl.initpred!(::MovingHorizonEstimator, ::LinModel)
76-
ModelPredictiveControl.linconstraint!(::MovingHorizonEstimator, ::LinModel, ::TranscriptionMethod)
77-
```
78-
79-
## Solve Optimization Problem
80-
81-
```@docs
82-
ModelPredictiveControl.optim_objective!(::MovingHorizonEstimator)
83-
ModelPredictiveControl.set_warmstart_mhe!
84-
ModelPredictiveControl.predict_mhe!
85-
ModelPredictiveControl.con_nonlinprog_mhe!
86-
ModelPredictiveControl.con_nonlinprogeq_mhe!
87-
ModelPredictiveControl.getstate!
88-
```
89-
9072
## Remove Operating Points
9173

9274
```@docs
@@ -118,3 +100,22 @@ ModelPredictiveControl.correct_estimate!
118100
```@docs
119101
ModelPredictiveControl.update_estimate!
120102
```
103+
104+
## Update Quadratic Optimization
105+
106+
```@docs
107+
ModelPredictiveControl.initpred!(::MovingHorizonEstimator, ::LinModel)
108+
ModelPredictiveControl.linconstraint!(::MovingHorizonEstimator, ::LinModel, ::TranscriptionMethod)
109+
```
110+
111+
## Solve Optimization Problem
112+
113+
```@docs
114+
ModelPredictiveControl.optim_objective!(::MovingHorizonEstimator)
115+
ModelPredictiveControl.set_warmstart_mhe!
116+
ModelPredictiveControl.disturbedinput!
117+
ModelPredictiveControl.predict_mhe!
118+
ModelPredictiveControl.con_nonlinprog_mhe!
119+
ModelPredictiveControl.con_nonlinprogeq_mhe!
120+
ModelPredictiveControl.getstate!
121+
```

‎src/controller/transcription.jl‎

Lines changed: 29 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,7 @@ end
484484
@doc raw"""
485485
init_defectmat(
486486
model::SimModel, estim::StateEstimator, transcription::OrthogonalCollocation,
487-
Hp, Hc, nb, Co=nothing, λo=nothing
487+
Hp, Hc, _ , Co, λo
488488
) -> ES, GS, JS, KS, VS, BS
489489
490490
Init the matrices for computing the continuity constraints and stochastic state defects.
@@ -533,8 +533,9 @@ The matrices ``\mathbf{E_S}`` and ``\mathbf{K_S}`` are defined in the Extended H
533533
\mathbf{0} & \mathbf{0} \end{bmatrix}
534534
\end{aligned}
535535
```
536-
Note that if `estim` is an [`InternalModel`](@ref), the state vector is not augmented so
537-
the rows of ``\mathbf{E_S}`` and ``\mathbf{K_S}`` with an even index are removed.
536+
Note that if `estim` is an [`InternalModel`](@ref), the state vector is not augmented
537+
hence the rows of ``\mathbf{E_S}`` and ``\mathbf{K_S}`` related to the stochastic states
538+
are removed (2nd row of blocks, 4th row of blocks, and so on).
538539
"""
539540
function init_defectmat(
540541
model::NonLinModel, estim::StateEstimator, transcription::OrthogonalCollocation,
@@ -1009,7 +1010,7 @@ end
10091010
@doc raw"""
10101011
set_warmstart_mpc!(mpc::PredictiveController, ::OrthogonalCollocation, Z̃var) -> Z̃s
10111012
1012-
Set and return the warm-start value of `Z̃var` for [`OrthogonalCollocation`](@ref).
1013+
Do the same but for [`OrthogonalCollocation`](@ref).
10131014
10141015
It warm-starts the solver at:
10151016
```math
@@ -1065,7 +1066,7 @@ end
10651066
@doc raw"""
10661067
set_warmstart_mpc!(mpc::PredictiveController, ::TranscriptionMethod, Z̃var) -> Z̃s
10671068
1068-
Set and return the warm-start value of `Z̃var` for other [`TranscriptionMethod`](@ref).
1069+
Do the same but for other [`TranscriptionMethod`](@ref).
10691070
10701071
It warm-starts the solver at:
10711072
```math
@@ -1356,9 +1357,9 @@ function con_nonlinprogeq!(
13561357
k = @views K[(1 + nk*(j-1)):(nk*j)]
13571358
x̂dnext = @views X̂0[(1 + nx̂*(j-1)):(nx̂*(j-1) + nx)]
13581359
x̂dnext_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*(j-1) + nx)]
1359-
sdnext = @views geq[(1 + nx*(j-1)):(nx*j)]
1360+
ŝdnext = @views geq[(1 + nx*(j-1)):(nx*j)]
13601361
f!(x̂dnext, k, model, x̂d_Z̃, û0, d̂0, model.p)
1361-
sdnext .= @. x̂dnext - x̂dnext_Z̃
1362+
ŝdnext .= @. x̂dnext - x̂dnext_Z̃
13621363
end
13631364
return geq
13641365
end
@@ -1378,8 +1379,8 @@ The deterministic state defects are computed with:
13781379
- \mathbf{x̂_d}(k+j+1)
13791380
```
13801381
for ``j = 0, 1, ... , H_p-1``, and in which ``\mathbf{x̂_d}`` are the deterministic states
1381-
extracted from the decision variable `Z̃`. The ``\mathbf{k̇}`` coefficients are evaluated
1382-
from the continuous-time function `model.f!` and:
1382+
extracted from the decision variable `Z̃`. The ``\mathbf{k̇}`` coefficients are evaluated from
1383+
the continuous-time function `model.f!` and:
13831384
```math
13841385
\begin{aligned}
13851386
\mathbf{k̇}_1(k+j) &= \mathbf{f}\Big(\mathbf{x̂_d}(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j), \mathbf{p}\Big) \\
@@ -1398,7 +1399,7 @@ function con_nonlinprogeq!(
13981399
Hp, Hc = mpc.Hp, mpc.Hc
13991400
nΔU, nX̂ = nu*Hc, nx̂*Hp
14001401
f_threads = transcription.f_threads
1401-
Ts, p = model.Ts, model.p
1402+
Ts = model.Ts
14021403
nk = get_nk(model, transcription)
14031404
D̂0 = mpc.D̂0
14041405
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
@@ -1411,27 +1412,27 @@ function con_nonlinprogeq!(
14111412
x̂d_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-2)):(nx̂*(j-2) + nx)]
14121413
d̂0 = @views D̂0[(1 + nd*(j-2)):(nd*(j-1))]
14131414
end
1415+
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
14141416
k̇ = @views K̇[(1 + nk*(j-1)):(nk*j)]
14151417
d̂0next = @views D̂0[(1 + nd*(j-1)):(nd*j)]
14161418
x̂dnext_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*(j-1) + nx)]
1417-
sdnext = @views geq[(1 + nx*(j-1)):(nx*(j-1) + nx)]
1419+
ŝdnext = @views geq[(1 + nx*(j-1)):(nx*(j-1) + nx)]
14181420
k̇1, k̇2 = @views k̇[1:nx], k̇[nx+1:2*nx]
1419-
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
14201421
if f_threads || h < 1 || j < 2
14211422
# we need to recompute k1 with multi-threading, even with h==1, since the
14221423
# last iteration (j-1) may not be executed (iterations are re-orderable)
1423-
model.f!(k̇1, x̂d_Z̃, û0, d̂0, p)
1424+
model.f!(k̇1, x̂d_Z̃, û0, d̂0, model.p)
14241425
else
14251426
k̇1 .= @views K̇[(1 + nk*(j-1)-nx):(nk*(j-1))] # k2 of of the last iter. j-1
14261427
end
14271428
if h < 1
1428-
model.f!(k̇2, x̂dnext_Z̃, û0, d̂0next, p)
1429+
model.f!(k̇2, x̂dnext_Z̃, û0, d̂0next, model.p)
14291430
else
14301431
# j = Hp special case: u(k+Hp-1) = u(k+Hp) since Hc≤Hp implies Δu(k+Hp) = 0:
14311432
û0next = @views j ≥ Hp ? û0 : Û0[(1 + nu*j):(nu*(j+1))]
1432-
model.f!(k̇2, x̂dnext_Z̃, û0next, d̂0next, p)
1433+
model.f!(k̇2, x̂dnext_Z̃, û0next, d̂0next, model.p)
14331434
end
1434-
sdnext .= @. x̂d_Z̃ - x̂dnext_Z̃ + 0.5*Ts*(k̇1 + k̇2)
1435+
ŝdnext .= @. x̂d_Z̃ - x̂dnext_Z̃ + 0.5*Ts*(k̇1 + k̇2)
14351436
end
14361437
return geq
14371438
end
@@ -1449,7 +1450,7 @@ Nonlinear equality constrains for [`NonLinModel`](@ref) and [`OrthogonalCollocat
14491450
The defects between the deterministic state derivative at the ``n_o`` collocation points and
14501451
the model dynamics are computed by:
14511452
```math
1452-
\mathbf{s_k}(k+j)
1453+
\mathbf{ŝ_k}(k+j)
14531454
= \mathbf{M_o} \begin{bmatrix}
14541455
\mathbf{k}_1(k+j) - \mathbf{x̂_d}(k+j) \\
14551456
\mathbf{k}_2(k+j) - \mathbf{x̂_d}(k+j) \\
@@ -1461,27 +1462,11 @@ the model dynamics are computed by:
14611462
\vdots \\
14621463
\mathbf{k̇}_{n_o}(k+j) \end{bmatrix}
14631464
```
1464-
for ``j = 0, 1, ... , H_p-1``, and knowing that the ``\mathbf{k}_i(k+j)`` vectors are
1465-
extracted from the decision variable `Z̃`. The ``\mathbf{x̂_d}`` vectors are the
1466-
deterministic states extracted from ``\mathbf{X̂_0}`` also in `Z̃`, and they correspond to the
1467-
states at the beginning of the interval ``τ_0=0``. The ``\mathbf{k̇}_i`` derivative for the
1468-
``i``th collocation point is computed from the continuous-time function `model.f!` and:
1469-
```math
1470-
\mathbf{k̇}_i(k+j) = \mathbf{f}\Big(\mathbf{k}_i(k+j), \mathbf{û}_i(k+j), \mathbf{d̂}_i(k+j), \mathbf{p}\Big)
1471-
```
1472-
Based on the normalized time ``τ_i ∈ [0, 1]`` and hold order `transcription.h`, the inputs
1473-
and disturbances are piecewise constant or linear:
1474-
```math
1475-
\begin{aligned}
1476-
\mathbf{û}_i(k+j) &= \begin{cases}
1477-
\mathbf{û_0}(k+j) & h = 0 \\
1478-
(1-τ_i)\mathbf{û_0}(k+j) + τ_i\mathbf{û_0}(k+j+1) & h = 1 \end{cases} \\
1479-
\mathbf{d̂}_i(k+j) &= (1-τ_i)\mathbf{d̂_0}(k+j) + τ_i\mathbf{d̂_0}(k+j+1)
1480-
\end{aligned}
1481-
```
1482-
The disturbed input ``\mathbf{û_0}`` is defined in [`f̂!`](@ref). The defects for the
1483-
continuity constraints and stochastic states are linear equality constraints (see
1484-
[`init_defectmat`](@ref)).
1465+
for ``j = 0, 1, ... , H_p-1``, and knowing that the ``\mathbf{k}_i(k+j)`` and
1466+
``\mathbf{x̂_d}(k+j)`` vectors are extracted from the decision variables in `Z̃`. The
1467+
``\mathbf{k̇}_i`` vectors are evaluated from the continuous-time function `model.f`, as
1468+
described in [`init_orthocolloc`](@ref). The defects for the continuity constraints and the
1469+
stochastic states are linear equality constraints (see [`init_defectmat`](@ref)).
14851470
"""
14861471
function con_nonlinprogeq!(
14871472
geq, _ , Û0, K̇,
@@ -1492,7 +1477,6 @@ function con_nonlinprogeq!(
14921477
Hp, Hc = mpc.Hp, mpc.Hc
14931478
nΔU, nX̂ = nu*Hc, nx̂*Hp
14941479
f_threads = transcription.f_threads
1495-
p = model.p
14961480
Mo, no, τ = mpc.Mo, transcription.no, transcription.τ
14971481
nk = get_nk(model, transcription)
14981482
D̂0 = mpc.D̂0
@@ -1507,17 +1491,17 @@ function con_nonlinprogeq!(
15071491
x̂d_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-2)):(nx̂*(j-2) + nx)]
15081492
d̂0 = @views D̂0[(1 + nd*(j-2)):(nd*(j-1))]
15091493
end
1494+
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
15101495
k̇ = @views K̇[(1 + nk*(j-1)):(nk*j)]
15111496
k_Z̃ = @views K_Z̃[(1 + nk*(j-1)):(nk*j)]
15121497
d̂0next = @views D̂0[(1 + nd*(j-1)):(nd*j)]
1513-
sk = @views geq[(1 + nk*(j-1)):(nk*j)]
1498+
ŝk = @views geq[(1 + nk*(j-1)):(nk*j)]
15141499
# ----------------- collocation constraint defects -----------------------------
1515-
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
15161500
Δk = k̇
15171501
for i=1:no
15181502
Δk[(1 + (i-1)*nx):(i*nx)] = @views k_Z̃[(1 + (i-1)*nx):(i*nx)] .- x̂d_Z̃
15191503
end
1520-
mul!(sk, Mo, Δk)
1504+
mul!(ŝk, Mo, Δk)
15211505
d̂i = @views D̂temp[(1 + nd*(j-1)):(nd*j)]
15221506
if h > 0
15231507
ûi = similar(û0) # TODO: remove this allocation
@@ -1527,15 +1511,15 @@ function con_nonlinprogeq!(
15271511
ki_Z̃ = @views k_Z̃[(1 + (i-1)*nx):(i*nx)]
15281512
d̂i .= (1-τ[i]).*d̂0 .+ τ[i].*d̂0next
15291513
if h < 1
1530-
model.f!(k̇i, ki_Z̃, û0, d̂i, p)
1514+
model.f!(k̇i, ki_Z̃, û0, d̂i, model.p)
15311515
else
15321516
# j = Hp special case: u(k+Hp-1) = u(k+Hp) since Hc≤Hp implies Δu(k+Hp) = 0:
15331517
û0next = @views j ≥ Hp ? û0 : Û0[(1 + nu*j):(nu*(j+1))]
15341518
ûi .= (1-τ[i]).*û0 .+ τ[i].*û0next
1535-
model.f!(k̇i, ki_Z̃, ûi, d̂i, p)
1519+
model.f!(k̇i, ki_Z̃, ûi, d̂i, model.p)
15361520
end
15371521
end
1538-
sk .-= k̇
1522+
ŝk .-= k̇
15391523
end
15401524
return geq
15411525
end

‎src/estimator/construct.jl‎

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ struct StateEstimatorBuffer{NT<:Real}
88
Ŵ ::Vector{NT}
99
X̂ ::Vector{NT}
1010
Ŷ ::Vector{NT}
11+
D ::Vector{NT}
1112
P̂ ::Matrix{NT}
1213
Q̂ ::Matrix{NT}
1314
R̂ ::Matrix{NT}
@@ -34,8 +35,8 @@ function StateEstimatorBuffer{NT}(
3435
He::Int=0, nŵ::Int=nx̂, nε::Int=0,
3536
transcription::TranscriptionMethod = SingleShooting()
3637
) where NT <: Real
37-
nZ̃ = nε + get_nZ_mhe(transcription, He, nx̂, nŵ)
38-
nV̂, nŴ, nX̂, nŶ = nym*He, nŵ*He, nx̂*He, ny*He
38+
nZ̃ = nε + get_nZ_mhe(transcription, He, nx̂, nk, nŵ)
39+
nV̂, nŴ, nX̂, nŶ, nD = nym*He, nŵ*He, nx̂*He, ny*He, nd*(He+1)
3940
u = Vector{NT}(undef, nu)
4041
û = Vector{NT}(undef, nu)
4142
k = Vector{NT}(undef, nk)
@@ -45,6 +46,7 @@ function StateEstimatorBuffer{NT}(
4546
Ŵ = Vector{NT}(undef, nŴ)
4647
X̂ = Vector{NT}(undef, nX̂)
4748
Ŷ = Vector{NT}(undef, nŶ)
49+
D = Vector{NT}(undef, nD)
4850
P̂ = Matrix{NT}(undef, nx̂, nx̂)
4951
Q̂ = Matrix{NT}(undef, nx̂, nx̂)
5052
R̂ = Matrix{NT}(undef, nym, nym)
@@ -53,7 +55,9 @@ function StateEstimatorBuffer{NT}(
5355
ŷ = Vector{NT}(undef, ny)
5456
d = Vector{NT}(undef, nd)
5557
empty = Vector{NT}(undef, 0)
56-
return StateEstimatorBuffer{NT}(u, û, k, x̂, Z̃, V̂, Ŵ, X̂, Ŷ, P̂, Q̂, R̂, K̂, ym, ŷ, d, empty)
58+
return StateEstimatorBuffer{NT}(
59+
u, û, k, x̂, Z̃, V̂, Ŵ, X̂, Ŷ, D, P̂, Q̂, R̂, K̂, ym, ŷ, d, empty
60+
)
5761
end
5862

5963
"Include all the covariance matrices for the Kalman filters and moving horizon estimator."

‎src/estimator/mhe.jl‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ get_other_dims(estim::MovingHorizonEstimator) = (estim.He, estim.nε)
88
"Print optimizer and other information for `MovingHorizonEstimator`."
99
function print_details(io::IO, estim::MovingHorizonEstimator)
1010
println(io, "├ optimizer: $(JuMP.solver_name(estim.optim)) ")
11-
println(io, "├ transcription: $(nameof(typeof(estim.transcription)))")
11+
println(io, "├ transcription: $(transcription_str(estim.transcription))")
1212
print_backends(io, estim, estim.model)
1313
println(io, "├ arrival covariance: $(nameof(typeof(estim.covestim))) ")
1414
println(io, "├ direct: $(estim.direct)")

0 commit comments

Comments
 (0)