1- struct PredictiveControllerBuffer{NT<: Real }
1+ struct PredictiveControllerBuffer{NT<: Real ,M <: AbstractMatrix{NT} }
22 u :: Vector{NT}
33 Z̃ :: Vector{NT}
44 D̂ :: Vector{NT}
55 Ŷ :: Vector{NT}
66 U :: Vector{NT}
77 Ẽ :: Matrix{NT}
8- P̃u:: Matrix{NT}
8+ P̃u:: M
99 empty:: Vector{NT}
1010end
1111
@@ -29,14 +29,19 @@ function PredictiveControllerBuffer(
2929 Ẽ = Matrix {NT} (undef, ny* Hp, nZ̃)
3030 P̃u = Matrix {NT} (undef, nu* Hp, nZ̃)
3131 empty = Vector {NT} (undef, 0 )
32- return PredictiveControllerBuffer {NT} (u, Z̃, D̂, Ŷ, U, Ẽ, P̃u, empty)
32+ return PredictiveControllerBuffer {NT,typeof(P̃u) } (u, Z̃, D̂, Ŷ, U, Ẽ, P̃u, empty)
3333end
3434
3535" Include all the objective function weights of [`PredictiveController`](@ref)"
36- struct ControllerWeights{NT<: Real }
37- M_Hp:: Hermitian{NT, Matrix{NT}}
38- Ñ_Hc:: Hermitian{NT, Matrix{NT}}
39- L_Hp:: Hermitian{NT, Matrix{NT}}
36+ struct ControllerWeights{
37+ NT<: Real ,
38+ H1<: Hermitian{NT, <:AbstractMatrix{NT}} ,
39+ H2<: Hermitian{NT, <:AbstractMatrix{NT}} ,
40+ H3<: Hermitian{NT, <:AbstractMatrix{NT}} ,
41+ }
42+ M_Hp:: H1
43+ Ñ_Hc:: H2
44+ L_Hp:: H3
4045 E :: NT
4146 iszero_M_Hp:: Vector{Bool}
4247 iszero_Ñ_Hc:: Vector{Bool}
@@ -46,15 +51,15 @@ struct ControllerWeights{NT<:Real}
4651 model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt= Inf , Ewt= 0
4752 ) where NT<: Real
4853 validate_weights (model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt)
49- # convert `Diagonal` to normal `Matrix` if required:
50- M_Hp = Hermitian (convert (Matrix{NT}, M_Hp), :L )
51- N_Hc = Hermitian (convert (Matrix{NT}, N_Hc), :L )
52- L_Hp = Hermitian (convert (Matrix{NT}, L_Hp), :L )
54+ M_Hp = Hermitian (convert .(NT, M_Hp), :L )
55+ N_Hc = Hermitian (convert .(NT, N_Hc), :L )
56+ L_Hp = Hermitian (convert .(NT, L_Hp), :L )
5357 nΔU = size (N_Hc, 1 )
5458 C = Cwt
5559 if ! isinf (Cwt)
5660 # ΔŨ = [ΔU; ϵ] (ϵ is the slack variable)
57- Ñ_Hc = Hermitian ([N_Hc zeros (NT, nΔU, 1 ); zeros (NT, 1 , nΔU) C], :L )
61+ # Ñ_Hc = Hermitian([N_Hc zeros(NT, nΔU, 1); zeros(NT, 1, nΔU) C], :L)
62+ Ñ_Hc = Hermitian (blockdiag (sparse (N_Hc), sparse (Diagonal ([C]))), :L )
5863 else
5964 # ΔŨ = ΔU (only hard constraints)
6065 Ñ_Hc = N_Hc
@@ -64,7 +69,7 @@ struct ControllerWeights{NT<:Real}
6469 iszero_Ñ_Hc = [iszero (Ñ_Hc)]
6570 iszero_L_Hp = [iszero (L_Hp)]
6671 iszero_E = iszero (E)
67- return new {NT} (M_Hp, Ñ_Hc, L_Hp, E, iszero_M_Hp, iszero_Ñ_Hc, iszero_L_Hp, iszero_E)
72+ return new {NT,typeof(M_Hp),typeof(Ñ_Hc),typeof(L_Hp) } (M_Hp, Ñ_Hc, L_Hp, E, iszero_M_Hp, iszero_Ñ_Hc, iszero_L_Hp, iszero_E)
6873 end
6974end
7075
@@ -584,10 +589,10 @@ function relaxU(Pu::Matrix{NT}, C_umin, C_umax, nϵ) where NT<:Real
584589 # ϵ impacts Z → U conversion for constraint calculations:
585590 A_Umin, A_Umax = - [Pu C_umin], [Pu - C_umax]
586591 # ϵ has no impact on Z → U conversion for prediction calculations:
587- P̃u = [Pu zeros (NT, size (Pu, 1 ))]
592+ P̃u = sparse_hcat ( sparse (Pu), spzeros (NT, size (Pu, 1 )))
588593 else # Z̃ = Z (only hard constraints)
589594 A_Umin, A_Umax = - Pu, Pu
590- P̃u = Pu
595+ P̃u = sparse (Pu)
591596 end
592597 return A_Umin, A_Umax, P̃u
593598end
@@ -621,17 +626,17 @@ bound, which is more precise than a linear inequality constraint. However, it is
621626convenient to treat it as a linear inequality constraint since the optimizer `OSQP.jl` does
622627not support pure bounds on the decision variables.
623628"""
624- function relaxΔU (PΔu:: Matrix {NT} , C_Δumin, C_Δumax, ΔUmin, ΔUmax, nϵ) where NT<: Real
629+ function relaxΔU (PΔu:: AbstractMatrix {NT} , C_Δumin, C_Δumax, ΔUmin, ΔUmax, nϵ) where NT<: Real
625630 nZ = size (PΔu, 2 )
626631 if nϵ == 1 # Z̃ = [Z; ϵ]
627632 ΔŨmin, ΔŨmax = [ΔUmin; NT[0.0 ]], [ΔUmax; NT[Inf ]] # 0 ≤ ϵ ≤ ∞
628633 A_ϵ = [zeros (NT, 1 , nZ) NT[1.0 ]]
629634 A_ΔŨmin, A_ΔŨmax = - [PΔu C_Δumin; A_ϵ], [PΔu - C_Δumax; A_ϵ]
630- P̃Δu = [PΔu zeros (NT, size (PΔu, 1 ), 1 ); zeros (NT, 1 , size (PΔu, 2 )) NT[ 1.0 ]]
635+ P̃Δu = blockdiag ( sparse (PΔu), spdiagm ([ one (NT)]))
631636 else # Z̃ = Z (only hard constraints)
632637 ΔŨmin, ΔŨmax = ΔUmin, ΔUmax
633638 A_ΔŨmin, A_ΔŨmax = - PΔu, PΔu
634- P̃Δu = PΔu
639+ P̃Δu = sparse ( PΔu)
635640 end
636641 return A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, P̃Δu
637642end
0 commit comments