1+ " Include all the objective function weights of [`PredictiveController`](@ref)"
2+ struct ControllerWeights{NT<: Real }
3+ M_Hp:: Hermitian{NT, Matrix{NT}}
4+ Ñ_Hc:: Hermitian{NT, Matrix{NT}}
5+ L_Hp:: Hermitian{NT, Matrix{NT}}
6+ E :: NT
7+ function ControllerWeights {NT} (
8+ model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt= Inf , Ewt= 0
9+ ) where NT<: Real
10+ validate_weights (model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt)
11+ # convert `Diagonal` to normal `Matrix` if required:
12+ M_Hp = Hermitian (convert (Matrix{NT}, M_Hp), :L )
13+ N_Hc = Hermitian (convert (Matrix{NT}, N_Hc), :L )
14+ L_Hp = Hermitian (convert (Matrix{NT}, L_Hp), :L )
15+ nΔU = size (N_Hc, 1 )
16+ C = Cwt
17+ if ! isinf (Cwt)
18+ # ΔŨ = [ΔU; ϵ] (ϵ is the slack variable)
19+ Ñ_Hc = Hermitian ([N_Hc zeros (NT, nΔU, 1 ); zeros (NT, 1 , nΔU) C], :L )
20+ else
21+ # ΔŨ = ΔU (only hard constraints)
22+ Ñ_Hc = N_Hc
23+ end
24+ E = Ewt
25+ return new {NT} (M_Hp, Ñ_Hc, L_Hp, E)
26+ end
27+ end
28+
129" Include all the data for the constraints of [`PredictiveController`](@ref)"
230struct ControllerConstraint{NT<: Real , GCfunc<: Function }
331 ẽx̂ :: Matrix{NT}
@@ -609,7 +637,7 @@ function init_predmat(estim::StateEstimator{NT}, model::SimModel, Hp, Hc) where
609637end
610638
611639@doc raw """
612- init_quadprog(model::LinModel, Ẽ, S, M_Hp, N_Hc, L_Hp ) -> H̃
640+ init_quadprog(model::LinModel, weights::ControllerWeights, Ẽ, S ) -> H̃
613641
614642Init the quadratic programming Hessian `H̃` for MPC.
615643
@@ -626,29 +654,30 @@ The vector ``\mathbf{q̃}`` and scalar ``r`` need recalculation each control per
626654[`initpred!`](@ref). ``r`` does not impact the minima position. It is thus useless at
627655optimization but required to evaluate the minimal ``J`` value.
628656"""
629- function init_quadprog (:: LinModel , Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp)
657+ function init_quadprog (:: LinModel , weights:: ControllerWeights , Ẽ, S̃)
658+ M_Hp, Ñ_Hc, L_Hp = weights. M_Hp, weights. Ñ_Hc, weights. L_Hp
630659 H̃ = Hermitian (2 * (Ẽ' * M_Hp* Ẽ + Ñ_Hc + S̃' * L_Hp* S̃), :L )
631660 return H̃
632661end
633- " Return empty matrices if `model` is not a [`LinModel`](@ref)."
634- function init_quadprog (:: SimModel{NT} , Ẽ, S̃, M_Hp, Ñ_Hc, L_Hp ) where {NT<: Real }
662+ " Return empty matrix if `model` is not a [`LinModel`](@ref)."
663+ function init_quadprog (:: SimModel{NT} , weights :: ControllerWeights , _, _ ) where {NT<: Real }
635664 H̃ = Hermitian (zeros (NT, 0 , 0 ), :L )
636665 return H̃
637666end
638667
639668"""
640669 init_defaultcon_mpc(
641- estim, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂,
670+ estim, C, S, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂,
642671 gc!=(_,_,_,_,_,_)->nothing, nc=0
643- ) -> con, S̃, Ñ_Hc, Ẽ
672+ ) -> con, S̃, Ẽ
644673
645674Init `ControllerConstraint` struct with default parameters based on estimator `estim`.
646675
647- Also return `S̃`, `Ñ_Hc` and `Ẽ` matrices for the the augmented decision vector `ΔŨ`.
676+ Also return `S̃` and `Ẽ` matrices for the the augmented decision vector `ΔŨ`.
648677"""
649678function init_defaultcon_mpc (
650679 estim:: StateEstimator{NT} ,
651- Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂, bx̂,
680+ Hp, Hc, C, S, E, ex̂, fx̂, gx̂, jx̂, kx̂, vx̂, bx̂,
652681 gc!:: GCfunc = (_,_,_,_,_,_)-> nothing , nc= 0
653682) where {NT<: Real , GCfunc<: Function }
654683 model = estim. model
@@ -667,9 +696,7 @@ function init_defaultcon_mpc(
667696 C_umin, C_umax, C_Δumin, C_Δumax, C_ymin, C_ymax =
668697 repeat_constraints (Hp, Hc, c_umin, c_umax, c_Δumin, c_Δumax, c_ymin, c_ymax)
669698 A_Umin, A_Umax, S̃ = relaxU (model, nϵ, C_umin, C_umax, S)
670- A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc = relaxΔU (
671- model, nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc
672- )
699+ A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax = relaxΔU (model, nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax)
673700 A_Ymin, A_Ymax, Ẽ = relaxŶ (model, nϵ, C_ymin, C_ymax, E)
674701 A_x̂min, A_x̂max, ẽx̂ = relaxterminal (model, nϵ, c_x̂min, c_x̂max, ex̂)
675702 i_Umin, i_Umax = .! isinf .(U0min), .! isinf .(U0max)
@@ -689,7 +716,7 @@ function init_defaultcon_mpc(
689716 A , b , i_b , C_ymin , C_ymax , c_x̂min , c_x̂max , i_g,
690717 gc! , nc
691718 )
692- return con, nϵ, S̃, Ñ_Hc, Ẽ
719+ return con, nϵ, S̃, Ẽ
693720end
694721
695722" Repeat predictive controller constraints over prediction `Hp` and control `Hc` horizons."
@@ -742,17 +769,14 @@ function relaxU(::SimModel{NT}, nϵ, C_umin, C_umax, S) where NT<:Real
742769end
743770
744771@doc raw """
745- relaxΔU(
746- model, nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc
747- ) -> A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc
772+ relaxΔU(model, nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax) -> A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax
748773
749774Augment input increments constraints with slack variable ϵ for softening.
750775
751776Denoting the input increments augmented with the slack variable
752777``\m athbf{ΔŨ} = [\b egin{smallmatrix} \m athbf{ΔU} \\ ϵ \e nd{smallmatrix}]``, it returns the
753- augmented input increment weights ``\m athbf{Ñ}_{H_c}`` (that incorporate ``C``). It also
754- returns the augmented constraints ``\m athbf{ΔŨ_{min}}`` and ``\m athbf{ΔŨ_{max}}`` and the
755- ``\m athbf{A}`` matrices for the inequality constraints:
778+ augmented constraints ``\m athbf{ΔŨ_{min}}`` and ``\m athbf{ΔŨ_{max}}`` and the ``\m athbf{A}``
779+ matrices for the inequality constraints:
756780```math
757781\b egin{bmatrix}
758782 \m athbf{A_{ΔŨ_{min}}} \\
@@ -764,21 +788,19 @@ returns the augmented constraints ``\mathbf{ΔŨ_{min}}`` and ``\mathbf{ΔŨ_{
764788\e nd{bmatrix}
765789```
766790"""
767- function relaxΔU (:: SimModel{NT} , nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc ) where NT<: Real
768- nΔU = size (N_Hc, 1 )
791+ function relaxΔU (:: SimModel{NT} , nϵ, C, C_Δumin, C_Δumax, ΔUmin, ΔUmax) where NT<: Real
792+ nΔU = length (ΔUmin )
769793 if nϵ == 1 # ΔŨ = [ΔU; ϵ]
770794 # 0 ≤ ϵ ≤ ∞
771795 ΔŨmin, ΔŨmax = [ΔUmin; NT[0.0 ]], [ΔUmax; NT[Inf ]]
772- A_ϵ = [zeros (NT, 1 , length (ΔUmin) ) NT[1.0 ]]
796+ A_ϵ = [zeros (NT, 1 , nΔU ) NT[1.0 ]]
773797 A_ΔŨmin, A_ΔŨmax = - [I C_Δumin; A_ϵ], [I - C_Δumax; A_ϵ]
774- Ñ_Hc = Hermitian ([N_Hc zeros (NT, nΔU, 1 );zeros (NT, 1 , nΔU) C], :L )
775798 else # ΔŨ = ΔU (only hard constraints)
776799 ΔŨmin, ΔŨmax = ΔUmin, ΔUmax
777800 I_Hc = Matrix {NT} (I, nΔU, nΔU)
778801 A_ΔŨmin, A_ΔŨmax = - I_Hc, I_Hc
779- Ñ_Hc = N_Hc
780802 end
781- return A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc
803+ return A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax
782804end
783805
784806@doc raw """
0 commit comments