Skip to content

Commit 26064dc

Browse files
committed
test: LinMPC construction with custom linear constraints
1 parent b87fd44 commit 26064dc

File tree

2 files changed

+31
-9
lines changed

2 files changed

+31
-9
lines changed

src/controller/construct.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -108,18 +108,18 @@ function validate_weights(model, Hp, Hc, M_Hp, N_Hc, L_Hp, C=Inf, E=nothing)
108108
Hp < 1 && throw(ArgumentError("Prediction horizon Hp should be ≥ 1"))
109109
Hc < 1 && throw(ArgumentError("Control horizon Hc should be ≥ 1"))
110110
Hc > Hp && throw(ArgumentError("Control horizon Hc should be ≤ prediction horizon Hp"))
111-
size(M_Hp) (nM,nM) && throw(ArgumentError("M_Hp size $(size(M_Hp)) ≠ (ny*Hp, ny*Hp) ($nM,$nM)"))
112-
size(N_Hc) (nN,nN) && throw(ArgumentError("N_Hc size $(size(N_Hc)) ≠ (nu*Hc, nu*Hc) ($nN,$nN)"))
113-
size(L_Hp) (nL,nL) && throw(ArgumentError("L_Hp size $(size(L_Hp)) ≠ (nu*Hp, nu*Hp) ($nL,$nL)"))
111+
size(M_Hp) (nM,nM) && throw(DimensionMismatch("M_Hp size $(size(M_Hp)) ≠ (ny*Hp, ny*Hp) ($nM,$nM)"))
112+
size(N_Hc) (nN,nN) && throw(DimensionMismatch("N_Hc size $(size(N_Hc)) ≠ (nu*Hc, nu*Hc) ($nN,$nN)"))
113+
size(L_Hp) (nL,nL) && throw(DimensionMismatch("L_Hp size $(size(L_Hp)) ≠ (nu*Hp, nu*Hp) ($nL,$nL)"))
114114
(isdiag(M_Hp) && any(diag(M_Hp) .< 0)) && throw(ArgumentError("Mwt values should be nonnegative"))
115115
(isdiag(N_Hc) && any(diag(N_Hc) .< 0)) && throw(ArgumentError("Nwt values should be nonnegative"))
116116
(isdiag(L_Hp) && any(diag(L_Hp) .< 0)) && throw(ArgumentError("Lwt values should be nonnegative"))
117117
!ishermitian(M_Hp) && throw(ArgumentError("M_Hp should be hermitian"))
118118
!ishermitian(N_Hc) && throw(ArgumentError("N_Hc should be hermitian"))
119119
!ishermitian(L_Hp) && throw(ArgumentError("L_Hp should be hermitian"))
120-
size(C) () && throw(ArgumentError("Cwt should be a real scalar"))
120+
size(C) () && throw(DimensionMismatch("Cwt should be a real scalar"))
121121
C < 0 && throw(ArgumentError("Cwt weight should be ≥ 0"))
122-
!isnothing(E) && size(E) () && throw(ArgumentError("Ewt should be a real scalar"))
122+
!isnothing(E) && size(E) () && throw(DimensionMismatch("Ewt should be a real scalar"))
123123
end
124124

125125
"Include all the data for the constraints of [`PredictiveController`](@ref)"

test/3_test_predictive_control.jl

Lines changed: 26 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,23 @@
5353
mpc16 = LinMPC(model, Hc=[1,2,3,6,6,6], Hp=10, Cwt=Inf)
5454
@test mpc16.Hc == 4 # the last 2 elements of Hc are ignored
5555
@test size(mpc16.P̃u) == (10*mpc1.estim.model.nu, 4*mpc1.estim.model.nu)
56+
57+
Wy = ones(3, ny)
58+
mpc17 = LinMPC(model; Wy)
59+
@test mpc17.con.W̄y ModelPredictiveControl.repeatdiag(Wy, mpc17.Hp+1)
60+
@test mpc17.con.W̄u zeros(model.nu*(mpc17.Hp+1), model.nu*(mpc17.Hp+1))
61+
@test mpc17.con.W̄d zeros(model.nd*(mpc17.Hp+1), model.nd*(mpc17.Hp+1))
62+
@test mpc17.con.W̄r zeros(model.ny*(mpc17.Hp+1), model.ny*(mpc17.Hp+1))
63+
64+
Wy = ones(2, model.ny)
65+
Wu = 2*ones(2, model.nu)
66+
Wd = 3*ones(2, model.nd)
67+
Wr = 0.5*ones(2, model.ny)
68+
mpc18 = LinMPC(model; Wy, Wu, Wd, Wr)
69+
@test mpc18.con.W̄y ModelPredictiveControl.repeatdiag(Wy, mpc18.Hp+1)
70+
@test mpc18.con.W̄u ModelPredictiveControl.repeatdiag(Wu, mpc18.Hp+1)
71+
@test mpc18.con.W̄d ModelPredictiveControl.repeatdiag(Wd, mpc18.Hp+1)
72+
@test mpc18.con.W̄r ModelPredictiveControl.repeatdiag(Wr, mpc18.Hp+1)
5673

5774
@test_logs(
5875
(:warn,
@@ -62,14 +79,19 @@
6279
)
6380
@test_throws ArgumentError LinMPC(model, Hc=0)
6481
@test_throws ArgumentError LinMPC(model, Hp=1, Hc=2)
65-
@test_throws ArgumentError LinMPC(model, Mwt=[1])
66-
@test_throws ArgumentError LinMPC(model, Mwt=[1])
67-
@test_throws ArgumentError LinMPC(model, Lwt=[1])
68-
@test_throws ArgumentError LinMPC(model, Cwt=[1])
82+
@test_throws DimensionMismatch LinMPC(model, Mwt=[1])
83+
@test_throws DimensionMismatch LinMPC(model, Mwt=[1])
84+
@test_throws DimensionMismatch LinMPC(model, Lwt=[1])
85+
@test_throws DimensionMismatch LinMPC(model, Cwt=[1])
6986
@test_throws ArgumentError LinMPC(model, Mwt=[-1,1])
7087
@test_throws ArgumentError LinMPC(model, Nwt=[-1,1])
7188
@test_throws ArgumentError LinMPC(model, Lwt=[-1,1])
7289
@test_throws ArgumentError LinMPC(model, Cwt=-1)
90+
@test_throws DimensionMismatch LinMPC(model, Wy=ones(2, ny+1))
91+
@test_throws DimensionMismatch LinMPC(model, Wu=ones(2, nu-1))
92+
@test_throws DimensionMismatch LinMPC(model, Wd=ones(2, nd+1))
93+
@test_throws DimensionMismatch LinMPC(model, Wr=ones(2, ny-1))
94+
@test_throws DimensionMismatch LinMPC(model, Wy=ones(2, ny), Wu=ones(3, nu))
7395
end
7496

7597
@testitem "LinMPC moves and getinfo" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)