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 ``\m athbf{e_x̂} = [\b egin{smallmatrix}\m athbf{0} & \m athbf{I}\e nd{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 ``\m athbf{0}``, except
386+ for ``\m athbf{e_x̂} = [\b egin{smallmatrix}\m athbf{0} & \m athbf{I}\e nd{smallmatrix}]``
381387"""
382388function 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̂
396406end
397407
@@ -508,6 +518,89 @@ function init_defectmat(
508518 return Eŝ, Gŝ, Jŝ, Kŝ, Vŝ, Bŝ
509519end
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+ \b egin{aligned}
533+ \m athbf{A Z̃ } &≤ \m athbf{b} \\
534+ \m athbf{A_{eq} Z̃} &= \m athbf{b_{eq}} \\
535+ \m athbf{g(Z̃)} &≤ \m athbf{0} \\
536+ \m athbf{g_{eq}(Z̃)} &= \m athbf{0} \\
537+ \e nd{aligned}
538+ ```
539+ The argument `nc` is the number of custom nonlinear inequality constraints in
540+ ``\m athbf{g_c}``. `i_b` is a `BitVector` including the indices of ``\m athbf{b}`` that are
541+ finite numbers. `i_g` is a similar vector but for the indices of ``\m athbf{g}``. The method
542+ also returns the ``\m athbf{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 ``\m athbf{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
785878end
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