Skip to content

Commit 87a5eb1

Browse files
committed
debug: adequate terminal state matrices for NonLinModel and MultipleShooting
1 parent dcf5839 commit 87a5eb1

File tree

3 files changed

+98
-89
lines changed

3 files changed

+98
-89
lines changed

Project.toml

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

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"

src/controller/construct.jl

Lines changed: 0 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -400,90 +400,6 @@ function setconstraint!(
400400
return mpc
401401
end
402402

403-
404-
@doc raw"""
405-
init_matconstraint_mpc(
406-
model::LinModel, transcription::TranscriptionMethod, nc::Int,
407-
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
408-
args...
409-
) -> i_b, i_g, A, Aeq, neq
410-
411-
Init `i_b`, `i_g`, `neq`, and `A` and `Aeq` matrices for the all the MPC constraints.
412-
413-
The linear and nonlinear constraints are respectively defined as:
414-
```math
415-
\begin{aligned}
416-
\mathbf{A Z̃ } &≤ \mathbf{b} \\
417-
\mathbf{A_{eq} Z̃} &= \mathbf{b_{eq}} \\
418-
\mathbf{g(Z̃)} &≤ \mathbf{0} \\
419-
\mathbf{g_{eq}(Z̃)} &= \mathbf{0} \\
420-
\end{aligned}
421-
```
422-
The argument `nc` is the number of custom nonlinear inequality constraints in
423-
``\mathbf{g_c}``. `i_b` is a `BitVector` including the indices of ``\mathbf{b}`` that are
424-
finite numbers. `i_g` is a similar vector but for the indices of ``\mathbf{g}``. The method
425-
also returns the ``\mathbf{A, A_{eq}}`` matrices and `neq` if `args` is provided. In such a
426-
case, `args` needs to contain all the inequality and equality constraint matrices:
427-
`A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ`. The integer `neq`
428-
is the number of nonlinear equality constraints in ``\mathbf{g_{eq}}``.
429-
"""
430-
function init_matconstraint_mpc(
431-
::LinModel{NT}, ::TranscriptionMethod, nc::Int,
432-
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
433-
args...
434-
) where {NT<:Real}
435-
if isempty(args)
436-
A, Aeq, neq = nothing, nothing, nothing
437-
else
438-
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
439-
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
440-
Aeq = A_ŝ
441-
neq = 0
442-
end
443-
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Ymin; i_Ymax; i_x̂min; i_x̂max]
444-
i_g = trues(nc)
445-
return i_b, i_g, A, Aeq, neq
446-
end
447-
448-
"Init `i_b` without output constraints if [`NonLinModel`](@ref) & [`MultipleShooting`](@ref)."
449-
function init_matconstraint_mpc(
450-
::NonLinModel{NT}, ::MultipleShooting, nc::Int,
451-
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
452-
args...
453-
) where {NT<:Real}
454-
if isempty(args)
455-
A, Aeq, neq = nothing, nothing, nothing
456-
else
457-
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
458-
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
459-
Aeq = A_ŝ
460-
nΔŨ, nZ̃ = size(A_ΔŨmin)
461-
neq = nZ̃ - nΔŨ
462-
end
463-
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_x̂min; i_x̂max]
464-
i_g = [i_Ymin; i_Ymax; trues(nc)]
465-
return i_b, i_g, A, Aeq, neq
466-
end
467-
468-
"Init `i_b` without output & terminal constraints if [`NonLinModel`](@ref) & [`SingleShooting`](@ref)."
469-
function init_matconstraint_mpc(
470-
::NonLinModel{NT}, ::SingleShooting, nc::Int,
471-
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
472-
args...
473-
) where {NT<:Real}
474-
if isempty(args)
475-
A, Aeq, neq = nothing, nothing, nothing
476-
else
477-
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
478-
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
479-
Aeq = A_ŝ
480-
neq = 0
481-
end
482-
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax]
483-
i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max; trues(nc)]
484-
return i_b, i_g, A, Aeq, neq
485-
end
486-
487403
"""
488404
default_Hp(model::LinModel)
489405

src/controller/transcription.jl

Lines changed: 97 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,15 @@ end
375375
@doc raw"""
376376
init_predmat(model::SimModel, estim, transcription::MultipleShooting, Hp, Hc)
377377
378-
Return empty matrices except `ex̂` for [`SimModel`](@ref) and [`MultipleShooting`](@ref).
378+
Return the terminal state matrices for [`SimModel`](@ref) and [`MultipleShooting`](@ref).
379379
380-
The matrix is ``\mathbf{e_x̂} = [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]``.
380+
The output prediction matrices are all empty matrices. The terminal state matrices are
381+
given in the Extended Help section.
382+
383+
# Extended Help
384+
!!! details "Extended Help"
385+
The terminal state matrices all appropriately sized zero matrices ``\mathbf{0}``, except
386+
for ``\mathbf{e_x̂} = [\begin{smallmatrix}\mathbf{0} & \mathbf{I}\end{smallmatrix}]``
381387
"""
382388
function init_predmat(
383389
model::SimModel, estim::StateEstimator{NT}, transcription::MultipleShooting, Hp, Hc
@@ -391,7 +397,11 @@ function init_predmat(
391397
V = zeros(NT, 0, nu)
392398
B = zeros(NT, 0)
393399
ex̂ = [zeros(NT, nx̂, Hc*nu + (Hp-1)*nx̂) I]
394-
gx̂, jx̂, kx̂, vx̂, bx̂ = G, J, K, V, B
400+
gx̂ = zeros(NT, nx̂, nd)
401+
jx̂ = zeros(NT, nx̂, nd*Hp)
402+
kx̂ = zeros(NT, nx̂, nx̂)
403+
vx̂ = zeros(NT, nx̂, nu)
404+
bx̂ = zeros(NT, nx̂)
395405
return E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂
396406
end
397407

@@ -508,6 +518,89 @@ function init_defectmat(
508518
return Eŝ, Gŝ, Jŝ, Kŝ, Vŝ, Bŝ
509519
end
510520

521+
@doc raw"""
522+
init_matconstraint_mpc(
523+
model::LinModel, transcription::TranscriptionMethod, nc::Int,
524+
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
525+
args...
526+
) -> i_b, i_g, A, Aeq, neq
527+
528+
Init `i_b`, `i_g`, `neq`, and `A` and `Aeq` matrices for the all the MPC constraints.
529+
530+
The linear and nonlinear constraints are respectively defined as:
531+
```math
532+
\begin{aligned}
533+
\mathbf{A Z̃ } &≤ \mathbf{b} \\
534+
\mathbf{A_{eq} Z̃} &= \mathbf{b_{eq}} \\
535+
\mathbf{g(Z̃)} &≤ \mathbf{0} \\
536+
\mathbf{g_{eq}(Z̃)} &= \mathbf{0} \\
537+
\end{aligned}
538+
```
539+
The argument `nc` is the number of custom nonlinear inequality constraints in
540+
``\mathbf{g_c}``. `i_b` is a `BitVector` including the indices of ``\mathbf{b}`` that are
541+
finite numbers. `i_g` is a similar vector but for the indices of ``\mathbf{g}``. The method
542+
also returns the ``\mathbf{A, A_{eq}}`` matrices and `neq` if `args` is provided. In such a
543+
case, `args` needs to contain all the inequality and equality constraint matrices:
544+
`A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ`. The integer `neq`
545+
is the number of nonlinear equality constraints in ``\mathbf{g_{eq}}``.
546+
"""
547+
function init_matconstraint_mpc(
548+
::LinModel{NT}, ::TranscriptionMethod, nc::Int,
549+
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
550+
args...
551+
) where {NT<:Real}
552+
if isempty(args)
553+
A, Aeq, neq = nothing, nothing, nothing
554+
else
555+
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
556+
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
557+
Aeq = A_ŝ
558+
neq = 0
559+
end
560+
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Ymin; i_Ymax; i_x̂min; i_x̂max]
561+
i_g = trues(nc)
562+
return i_b, i_g, A, Aeq, neq
563+
end
564+
565+
"Init `i_b` without output constraints if `NonLinModel` and not `SingleShooting`."
566+
function init_matconstraint_mpc(
567+
::NonLinModel{NT}, ::TranscriptionMethod, nc::Int,
568+
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
569+
args...
570+
) where {NT<:Real}
571+
if isempty(args)
572+
A, Aeq, neq = nothing, nothing, nothing
573+
else
574+
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
575+
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
576+
Aeq = A_ŝ
577+
nΔŨ, nZ̃ = size(A_ΔŨmin)
578+
neq = nZ̃ - nΔŨ
579+
end
580+
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_x̂min; i_x̂max]
581+
i_g = [i_Ymin; i_Ymax; trues(nc)]
582+
return i_b, i_g, A, Aeq, neq
583+
end
584+
585+
"Init `i_b` without output & terminal constraints if `NonLinModel` and `SingleShooting`."
586+
function init_matconstraint_mpc(
587+
::NonLinModel{NT}, ::SingleShooting, nc::Int,
588+
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
589+
args...
590+
) where {NT<:Real}
591+
if isempty(args)
592+
A, Aeq, neq = nothing, nothing, nothing
593+
else
594+
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max, A_ŝ = args
595+
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
596+
Aeq = A_ŝ
597+
neq = 0
598+
end
599+
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax]
600+
i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max; trues(nc)]
601+
return i_b, i_g, A, Aeq, neq
602+
end
603+
511604
"""
512605
init_nonlincon!(
513606
mpc::PredictiveController, model::LinModel, transcription::TranscriptionMethod,
@@ -785,7 +878,7 @@ function linconstraint!(mpc::PredictiveController, model::LinModel, ::Transcript
785878
end
786879

787880
"Set `b` excluding predicted output constraints for `NonLinModel` and not `SingleShooting`."
788-
function linconstraint!(mpc::PredictiveController, ::NonLinModel, ::TranscriptionMethod)
881+
function linconstraint!(mpc::PredictiveController, model::NonLinModel, ::TranscriptionMethod)
789882
nU, nΔŨ, nY = length(mpc.con.U0min), length(mpc.con.ΔŨmin), length(mpc.con.Y0min)
790883
nx̂, fx̂ = mpc.estim.nx̂, mpc.con.fx̂
791884
fx̂ .= mpc.con.bx̂

0 commit comments

Comments
 (0)