Skip to content

Commit 6601bda

Browse files
committed
debug: ExplicitMPC updates with transcription methods
1 parent a5bdab5 commit 6601bda

File tree

3 files changed

+27
-42
lines changed

3 files changed

+27
-42
lines changed

src/controller/execute.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -652,9 +652,9 @@ end
652652

653653
"Update the prediction matrices, linear constraints and JuMP optimization."
654654
function setmodel_controller!(mpc::PredictiveController, x̂op_old)
655-
estim, model = mpc.estim, mpc.estim.model
655+
model, estim, transcription = mpc.estim.model, mpc.estim, mpc.transcription
656656
nu, ny, nd, Hp, Hc = model.nu, model.ny, model.nd, mpc.Hp, mpc.Hc
657-
transcription, optim, con = mpc.transcription, mpc.optim, mpc.con
657+
optim, con = mpc.optim, mpc.con
658658
# --- predictions matrices ---
659659
E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = init_predmat(
660660
model, estim, transcription, Hp, Hc

src/controller/explicitmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ The solution is ``\mathbf{Z̃ = - H̃^{-1} q̃}``, see [`init_quadprog`](@ref).
193193
optim_objective!(mpc::ExplicitMPC) = lmul!(-1, ldiv!(mpc.Z̃, mpc.H̃_chol, mpc.q̃))
194194

195195
"Compute the predictions but not the terminal states if `mpc` is an [`ExplicitMPC`](@ref)."
196-
function predict!(Ŷ, x̂, _ , _ , _ , mpc::ExplicitMPC, ::LinModel, Z̃)
196+
function predict!(Ŷ, x̂, _ , _ , _ , mpc::ExplicitMPC, ::LinModel, ::TranscriptionMethod, Z̃)
197197
# in-place operations to reduce allocations :
198198
Ŷ .= mul!(Ŷ, mpc.Ẽ, Z̃) .+ mpc.F
199199
x̂ .= NaN
@@ -213,7 +213,7 @@ addinfo!(info, mpc::ExplicitMPC) = info
213213

214214
"Update the prediction matrices and Cholesky factorization."
215215
function setmodel_controller!(mpc::ExplicitMPC, _ )
216-
estim, model = mpc.estim, mpc.estim.model
216+
model, estim, transcription = mpc.estim.model, mpc.estim, mpc.transcription
217217
nu, ny, nd, Hp, Hc = model.nu, model.ny, model.nd, mpc.Hp, mpc.Hc
218218
# --- predictions matrices ---
219219
E, G, J, K, V, B = init_predmat(model, estim, transcription, Hp, Hc)
@@ -225,7 +225,7 @@ function setmodel_controller!(mpc::ExplicitMPC, _ )
225225
mpc.V .= V
226226
mpc.B .= B
227227
# --- quadratic programming Hessian matrix ---
228-
= init_quadprog(model, mpc.weights, mpc.Ẽ, mpc.S̃)
228+
= init_quadprog(model, mpc.weights, mpc.Ẽ, mpc.P̃, mpc.S̃)
229229
mpc.H̃ .=
230230
set_objective_hessian!(mpc)
231231
# --- operating points ---

src/controller/transcription.jl

Lines changed: 22 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -35,17 +35,17 @@ The decision variable is (excluding ``ϵ``):
3535
thus it also includes the predicted states, expressed as deviation vectors from the
3636
operating point ``\mathbf{x̂_{op}}`` (see [`augment_model`](@ref)):
3737
```math
38-
\mathbf{X̂_0} = \mathbf{X̂ - X̂_{op}} \begin{bmatrix}
38+
\mathbf{X̂_0} = \mathbf{X̂ - X̂_{op}} = \begin{bmatrix}
3939
\mathbf{x̂}_i(k+1) - \mathbf{x̂_{op}} \\
4040
\mathbf{x̂}_i(k+2) - \mathbf{x̂_{op}} \\
4141
\vdots \\
4242
\mathbf{x̂}_i(k+H_p) - \mathbf{x̂_{op}} \end{bmatrix}
4343
```
4444
where ``\mathbf{x̂}_i(k+j)`` is the state prediction for time ``k+j``, estimated by the
45-
observer at time ``i=k`` or ``i=k-1`` depending on the `direct` flag. This transcription
45+
observer at time ``i=k`` or ``i=k-1`` depending on its `direct` flag. This transcription
4646
method is generally more efficient for large control horizon ``H_c``, unstable or highly
47-
nonlinear plant models/constraints. Sparse optimizers like `OSQP.jl` or `Ipopt.jl` are
48-
recommended for large-scale problems.
47+
nonlinear plant models/constraints. Sparse optimizers like `OSQP` or `Ipopt` are recommended
48+
for large-scale problems.
4949
"""
5050
struct MultipleShooting <: TranscriptionMethod end
5151

@@ -365,13 +365,15 @@ function init_predmat(
365365
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
366366
end
367367

368-
"""
369-
init_predmat(model::SimModel, estim, transcription::SingleShooting, Hp, Hc)
368+
@doc raw"""
369+
init_predmat(model::SimModel, estim, transcription::MultipleShooting, Hp, Hc)
370+
371+
Return empty matrices except `ex̂` for [`SimModel`](@ref) and [`MultipleShooting`](@ref).
370372
371-
Return empty matrices for [`SimModel`](@ref) and [`SingleShooting`](@ref) (N/A).
373+
The matrix is ``\mathbf{e_x̂} = [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]``.
372374
"""
373375
function init_predmat(
374-
model::SimModel, estim::StateEstimator{NT}, transcription::SingleShooting, Hp, Hc
376+
model::SimModel, estim::StateEstimator{NT}, transcription::MultipleShooting, Hp, Hc
375377
) where {NT<:Real}
376378
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
377379
nZ = get_nZ(estim, transcription, Hp, Hc)
@@ -381,19 +383,18 @@ function init_predmat(
381383
K = zeros(NT, 0, nx̂)
382384
V = zeros(NT, 0, nu)
383385
B = zeros(NT, 0)
384-
ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = E, G, J, K, V, B
386+
ex̂ = [zeros(NT, nx̂, Hc*nu + (Hp-1)*nx̂) I]
387+
gx̂, jx̂, kx̂, vx̂, bx̂ = G, J, K, V, B
385388
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
386389
end
387390

388-
@doc raw"""
389-
init_predmat(model::SimModel, estim, transcription::MultipleShooting, Hp, Hc)
390-
391-
Return empty matrices except `ex̂` for [`SimModel`](@ref) and [`MultipleShooting`](@ref).
391+
"""
392+
init_predmat(model::SimModel, estim, transcription::TranscriptionMethod, Hp, Hc)
392393
393-
The matrix is ``\mathbf{ex̂} = [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]``.
394+
Return empty matrices for all other cases.
394395
"""
395396
function init_predmat(
396-
model::SimModel, estim::StateEstimator{NT}, transcription::MultipleShooting, Hp, Hc
397+
model::SimModel, estim::StateEstimator{NT}, transcription::TranscriptionMethod, Hp, Hc
397398
) where {NT<:Real}
398399
nu, nx̂, nd = model.nu, estim.nx̂, model.nd
399400
nZ = get_nZ(estim, transcription, Hp, Hc)
@@ -403,30 +404,10 @@ function init_predmat(
403404
K = zeros(NT, 0, nx̂)
404405
V = zeros(NT, 0, nu)
405406
B = zeros(NT, 0)
406-
ex̂ = [zeros(NT, nx̂, Hc*nu + (Hp-1)*nx̂) I]
407-
gx̂, jx̂, kx̂, vx̂, bx̂ = G, J, K, V, B
407+
ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = E, G, J, K, V, B
408408
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
409409
end
410410

411-
"""
412-
init_defectmat(model::SimModel, estim, transcription::SingleShooting, Hp, Hc)
413-
414-
Return empty matrices if `transcription` is a [`SingleShooting`](@ref) (N/A).
415-
"""
416-
function init_defectmat(
417-
model::SimModel, estim::StateEstimator{NT}, transcription::SingleShooting, Hp, Hc
418-
) where {NT<:Real}
419-
nx̂, nu, nd = estim.nx̂, model.nu, model.nd
420-
nZ = get_nZ(estim, transcription, Hp, Hc)
421-
Eŝ = zeros(NT, 0, nZ)
422-
Gŝ = zeros(NT, 0, nd)
423-
Jŝ = zeros(NT, 0, nd*Hp)
424-
Kŝ = zeros(NT, 0, nx̂)
425-
Vŝ = zeros(NT, 0, nu)
426-
Bŝ = zeros(NT, 0)
427-
return Eŝ, Gŝ, Jŝ, Kŝ, Vŝ, Bŝ
428-
end
429-
430411
@doc raw"""
431412
init_defectmat(model::LinModel, estim, transcription::MultipleShooting, Hp, Hc)
432413
@@ -501,7 +482,11 @@ function init_defectmat(
501482
return Eŝ, Gŝ, Jŝ, Kŝ, Vŝ, Bŝ
502483
end
503484

504-
"Return empty matrices if `model` is not a [`LinModel`](@ref) (N/A)."
485+
"""
486+
init_defectmat(model::SimModel, estim, transcription::TranscriptionMethod, Hp, Hc)
487+
488+
Return empty matrices for all other cases (N/A).
489+
"""
505490
function init_defectmat(
506491
model::SimModel, estim::StateEstimator{NT}, transcription::TranscriptionMethod, Hp, Hc
507492
) where {NT<:Real}

0 commit comments

Comments
 (0)