Skip to content

Commit 0dabeac

Browse files
committed
test: new construction and solving tests with move blocking
1 parent 05a788d commit 0dabeac

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

test/3_test_predictive_control.jl

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@
4747
@test mpc14.transcription == MultipleShooting()
4848
@test length(mpc14.Z̃) == model2.nu*mpc14.Hc + mpc14.estim.nx̂*mpc14.Hp + mpc14.
4949
@test size(mpc14.con.Aeq, 1) == mpc14.estim.nx̂*mpc14.Hp
50+
mpc15 = LinMPC(model, Hc=[1,2,3], Hp=10, Cwt=Inf)
51+
@test mpc15.Hc == 4 # the constructor will push an element to nb
52+
@test size(mpc15.P̃u) == (10*mpc1.estim.model.nu, 4*mpc1.estim.model.nu)
53+
mpc16 = LinMPC(model, Hc=[1,2,3,6,6,6], Hp=10, Cwt=Inf)
54+
@test mpc16.Hc == 4 # the last 2 elements of Hc are ignored
55+
@test size(mpc16.P̃u) == (10*mpc1.estim.model.nu, 4*mpc1.estim.model.nu)
5056

5157
@test_logs(
5258
(:warn,
@@ -108,6 +114,13 @@ end
108114
d = [0.1]
109115
u = moveinput!(mpc6, 7d, d)
110116
@test u [0] atol=1e-2
117+
mpc7 = LinMPC(linmodel, Hp=10, Hc=[1, 2, 3, 4], Nwt=[10])
118+
preparestate!(mpc7, [10])
119+
r = [15]
120+
moveinput!(mpc7, r)
121+
ΔU_diff = diff(getinfo(mpc7)[:U])
122+
@test ΔU_diff[[2, 4, 5, 7, 8, 9]] zeros(6)
123+
111124
@test_throws DimensionMismatch moveinput!(mpc1, [0,0,0])
112125
@test_throws DimensionMismatch moveinput!(mpc1, [0], [0,0])
113126
@test_throws DimensionMismatch moveinput!(mpc1; D̂ = fill(0, mpc1.Hp+1))
@@ -447,7 +460,8 @@ end
447460

448461
@testitem "ExplicitMPC moves and getinfo" setup=[SetupMPCtests] begin
449462
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
450-
mpc1 = ExplicitMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1000, Hc=1)
463+
model = LinModel(tf(5, [2, 1]), 3)
464+
mpc1 = ExplicitMPC(model, Nwt=[0], Hp=1000, Hc=1)
451465
r, y = [5], [0]
452466
preparestate!(mpc1, y)
453467
u = moveinput!(mpc1, r)
@@ -458,18 +472,24 @@ end
458472
info = getinfo(mpc1)
459473
@test info[:u] u
460474
@test info[:Ŷ][end] r[1] atol=1e-2
461-
mpc2 = ExplicitMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1000, Hc=1)
462-
preparestate!(mpc2, [0])
475+
mpc2 = ExplicitMPC(model, Nwt=[0], Hp=1000, Hc=1)
476+
preparestate!(mpc2, y)
463477
u = moveinput!(mpc2, [5])
464478
@test u [1] atol=1e-2
465-
mpc3 = ExplicitMPC(LinModel(tf(5, [2, 1]), 3), Mwt=[0], Nwt=[0], Lwt=[1])
466-
preparestate!(mpc3, [0])
479+
mpc3 = ExplicitMPC(model, Mwt=[0], Nwt=[0], Lwt=[1])
480+
preparestate!(mpc3, y)
467481
u = moveinput!(mpc3, [0], R̂u=fill(12, mpc3.Hp))
468482
@test u [12] atol=1e-2
469483
model2 = LinModel{Float32}(0.5*ones(1,1), ones(1,1), ones(1,1), zeros(1,0), zeros(1,0), 1.0)
470484
mpc4 = ExplicitMPC(model2)
471-
preparestate!(mpc4, [0])
485+
preparestate!(mpc4, y)
472486
moveinput!(mpc4, [0]) [0.0]
487+
mpc5 = ExplicitMPC(model, Hp=10, Hc=[1, 2, 3, 4], Nwt=[10])
488+
preparestate!(mpc5, y)
489+
moveinput!(mpc5, r)
490+
ΔU_diff = diff(getinfo(mpc5)[:U])
491+
@test ΔU_diff[[2, 4, 5, 7, 8, 9]] zeros(6)
492+
473493
@test_nowarn ModelPredictiveControl.info2debugstr(info)
474494
end
475495

@@ -770,6 +790,12 @@ end
770790
info = getinfo(nmpc10)
771791
@test info[:u] u
772792
@test info[:Ŷ][end] 10 atol=5e-2
793+
nmpc11 = NonLinMPC(nonlinmodel, Hp=10, Hc=[1, 2, 3, 4], Nwt=[10])
794+
preparestate!(nmpc11, y, [0])
795+
moveinput!(nmpc11, [10], [0])
796+
ΔU_diff = diff(getinfo(nmpc11)[:U])
797+
println(ΔU_diff)
798+
@test ΔU_diff[[2, 4, 5, 7, 8, 9]] zeros(6)
773799

774800
@test_nowarn ModelPredictiveControl.info2debugstr(info)
775801
end

0 commit comments

Comments
 (0)