Skip to content

Commit 6a66709

Browse files
committed
changed: using @testmodule to execute only once
1 parent 300ed35 commit 6a66709

File tree

5 files changed

+81
-19
lines changed

5 files changed

+81
-19
lines changed

test/runtests.jl

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,19 @@ using Test, TestItemRunner
1111

1212
@run_package_tests
1313

14-
#@testitem "ModelPredictiveControl.jl" begin
14+
@testset "ModelPredictiveControl.jl" begin
1515

16-
@testsnippet SetupMPCtests begin
17-
using ControlSystemsBase, LinearAlgebra
18-
using Random: randn
19-
using JuMP, OSQP, Ipopt, DAQP, ForwardDiff
20-
using Plots
16+
@testmodule SetupMPCtests begin
17+
using ControlSystemsBase
2118
Ts = 400.0
2219
sys = [ tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]) tf(1.90,[1800.0,1]);
2320
tf(-0.74,[800.0,1]) tf(0.74,[800.0,1]) tf(-0.74,[800.0,1]) ]
2421
sys_ss = minreal(ss(sys))
2522
Gss = c2d(sys_ss[:,1:2], Ts, :zoh)
2623
Gss2 = c2d(sys_ss[:,1:2], 0.5Ts, :zoh)
24+
export Ts, sys, sys_ss, Gss, Gss2
2725
end
2826

29-
3027
include("test_sim_model.jl")
3128
include("test_state_estim.jl")
3229
include("test_predictive_control.jl")
@@ -46,6 +43,6 @@ DocMeta.setdocmeta!(
4643
doctest(ModelPredictiveControl, testset="DocTest")
4744
ENV["JULIA_DEBUG"] = old_debug_level
4845

49-
#end;
46+
end;
5047

5148
nothing

test/test_plot_sim.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testitem "SimModel quick simulation" setup=[SetupMPCtests] begin
2+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
23
model = LinModel(sys, Ts, i_d=[3])
34
res = sim!(model, 15)
45
display(res)
@@ -18,6 +19,7 @@
1819
end
1920

2021
@testitem "SimModel Plots" setup=[SetupMPCtests] begin
22+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
2123
model = LinModel(sys, Ts, i_d=[3])
2224
res = sim!(model, 15, [1, 3], [-10])
2325
p = plot(res, plotx=true)
@@ -42,6 +44,7 @@ end
4244
end
4345

4446
@testitem "StateEstimator quick simulation" setup=[SetupMPCtests] begin
47+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
4548
estim = SteadyKalmanFilter(LinModel(sys, Ts, i_d=[3]))
4649
res = sim!(estim, 15)
4750
@test isa(res.obj, SteadyKalmanFilter)
@@ -64,6 +67,7 @@ end
6467
end
6568

6669
@testitem "StateEstimator Plots" setup=[SetupMPCtests] begin
70+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
6771
estim = MovingHorizonEstimator(LinModel(sys, Ts, i_d=[3]), He=5)
6872
estim = setconstraint!(estim, x̂min=[-100,-101,-102,-103,-Inf,-Inf])
6973
estim = setconstraint!(estim, x̂max=[+100,+101,+102,+103,+Inf,+Inf])
@@ -121,6 +125,7 @@ end
121125
end
122126

123127
@testitem "PredictiveController quick simulation" setup=[SetupMPCtests] begin
128+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
124129
mpc1 = LinMPC(LinModel(sys, Ts, i_d=[3]))
125130
res = sim!(mpc1, 15)
126131
@test isa(res.obj, LinMPC)
@@ -155,6 +160,7 @@ end
155160
end
156161

157162
@testitem "PredictiveController Plots" setup=[SetupMPCtests] begin
163+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, Plots
158164
estim = MovingHorizonEstimator(LinModel(sys, Ts, i_d=[3]), He=5)
159165
estim = setconstraint!(estim, x̂min=[-100,-101,-102,-103,-104,-105])
160166
estim = setconstraint!(estim, x̂max=[+100,+101,+102,+103,+104,+105])

test/test_predictive_control.jl

Lines changed: 33 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testitem "LinMPC construction" setup=[SetupMPCtests] begin
2+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
23
model = LinModel(sys, Ts, i_d=[3])
34
mpc1 = LinMPC(model, Hp=15)
45
@test isa(mpc1.estim, SteadyKalmanFilter)
@@ -55,6 +56,8 @@
5556
end
5657

5758
@testitem "LinMPC moves and getinfo" setup=[SetupMPCtests] begin
59+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
60+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
5861
linmodel = setop!(LinModel(tf(5, [2, 1]), 3), yop=[10])
5962
mpc1 = LinMPC(linmodel, Nwt=[0], Hp=1000, Hc=1)
6063
r = [15]
@@ -88,6 +91,7 @@ end
8891
end
8992

9093
@testitem "LinMPC step disturbance rejection" setup=[SetupMPCtests] begin
94+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
9195
linmodel = setop!(LinModel(tf(5, [2, 1]), 3.0), yop=[10])
9296
r = [15]
9397
outdist = [5]
@@ -130,6 +134,7 @@ end
130134
end
131135

132136
@testitem "LinMPC other methods" setup=[SetupMPCtests] begin
137+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
133138
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
134139
mpc1 = LinMPC(linmodel1)
135140
@test initstate!(mpc1, [10, 50], [50, 30+1]) [zeros(3); [1]]
@@ -148,6 +153,7 @@ end
148153
end
149154

150155
@testitem "LinMPC set constraints" setup=[SetupMPCtests] begin
156+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
151157
model = LinModel(sys, Ts, i_d=[3])
152158
mpc = LinMPC(model, Hp=1, Hc=1)
153159

@@ -215,6 +221,7 @@ end
215221
end
216222

217223
@testitem "LinMPC constraint violation" setup=[SetupMPCtests] begin
224+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
218225
model = LinModel(tf([2], [10, 1]), 3.0)
219226
mpc = LinMPC(model, Hp=50, Hc=5)
220227

@@ -273,6 +280,7 @@ end
273280
end
274281

275282
@testitem "LinMPC terminal cost" setup=[SetupMPCtests] begin
283+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
276284
model = LinModel(ss([0.5 -0.4;0.6 0.5], [1 0;0 1], [1 0; 0 1], 0, 1))
277285
K = lqr(Discrete, model.A, model.Bu, I, 0.5I)
278286
M_end = ControlSystemsBase.are(Discrete, model.A, model.Bu, I, 0.5I)
@@ -300,6 +308,7 @@ end
300308
end
301309

302310
@testitem "LinMPC set model" setup=[SetupMPCtests] begin
311+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
303312
estim = KalmanFilter(setop!(LinModel(tf(5, [2, 1]), 3), yop=[10], uop=[1]))
304313
mpc = LinMPC(estim, Nwt=[0], Cwt=1e4, Hp=1000, Hc=1)
305314
mpc = setconstraint!(mpc, umin=[-24], umax=[26])
@@ -339,6 +348,7 @@ end
339348
end
340349

341350
@testitem "LinMPC real-time simulations" setup=[SetupMPCtests] begin
351+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
342352
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
343353
mpc1 = LinMPC(linmodel1)
344354
times1 = zeros(5)
@@ -352,6 +362,7 @@ end
352362
end
353363

354364
@testitem "ExplicitMPC construction" setup=[SetupMPCtests] begin
365+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
355366
model = LinModel(sys, Ts, i_d=[3])
356367
mpc1 = ExplicitMPC(model, Hp=15)
357368
@test isa(mpc1.estim, SteadyKalmanFilter)
@@ -382,6 +393,7 @@ end
382393
end
383394

384395
@testitem "ExplicitMPC moves and getinfo" setup=[SetupMPCtests] begin
396+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
385397
mpc1 = ExplicitMPC(LinModel(tf(5, [2, 1]), 3), Nwt=[0], Hp=1000, Hc=1)
386398
r = [5]
387399
preparestate!(mpc1, [0])
@@ -409,6 +421,7 @@ end
409421

410422

411423
@testitem "ExplicitMPC step disturbance rejection" setup=[SetupMPCtests] begin
424+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
412425
linmodel = setop!(LinModel(tf(5, [2, 1]), 3.0), yop=[10])
413426
r = [15]
414427
outdist = [5]
@@ -451,6 +464,7 @@ end
451464
end
452465

453466
@testitem "ExplicitMPC other methods" setup=[SetupMPCtests] begin
467+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
454468
linmodel1 = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
455469
mpc1 = ExplicitMPC(linmodel1)
456470
@test initstate!(mpc1, [10, 50], [50, 30+1]) [zeros(3); [1]]
@@ -465,12 +479,14 @@ end
465479
end
466480

467481
@testitem "ExplicitMPC constraints" setup=[SetupMPCtests] begin
482+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
468483
model = LinModel(sys, Ts, i_d=[3])
469484
mpc = ExplicitMPC(model, Hp=1, Hc=1)
470485
@test_throws ErrorException setconstraint!(mpc, umin=[0.0, 0.0])
471486
end
472487

473488
@testitem "ExplicitMPC set model" setup=[SetupMPCtests] begin
489+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
474490
estim = KalmanFilter(setop!(LinModel(tf(5, [2, 1]), 3), yop=[10], uop=[1]))
475491
mpc = ExplicitMPC(estim, Nwt=[0], Hp=1000, Hc=1)
476492
@test mpc.Yop fill(10.0, 1000)
@@ -500,6 +516,7 @@ end
500516
end
501517

502518
@testitem "NonLinMPC construction" setup=[SetupMPCtests] begin
519+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
503520
linmodel1 = LinModel(sys,Ts,i_d=[3])
504521
nmpc0 = NonLinMPC(linmodel1, Hp=15)
505522
@test isa(nmpc0.estim, SteadyKalmanFilter)
@@ -563,6 +580,7 @@ end
563580
end
564581

565582
@testitem "NonLinMPC moves and getinfo" setup=[SetupMPCtests] begin
583+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
566584
linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10])
567585
Hp = 1000
568586
nmpc_lin = NonLinMPC(linmodel, Nwt=[0], Hp=Hp, Hc=1)
@@ -635,6 +653,7 @@ end
635653
end
636654

637655
@testitem "NonLinMPC step disturbance rejection" setup=[SetupMPCtests] begin
656+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
638657
linmodel = setop!(LinModel(tf(5, [2000, 1]), 3000.0), yop=[10])
639658
r = [15]
640659
outdist = [5]
@@ -677,6 +696,7 @@ end
677696
end
678697

679698
@testitem "NonLinMPC other methods" setup=[SetupMPCtests] begin
699+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
680700
linmodel = setop!(LinModel(sys,Ts,i_u=[1,2]), uop=[10,50], yop=[50,30])
681701
f = (x,u,_,_) -> linmodel.A*x + linmodel.Bu*u
682702
h = (x,_,_) -> linmodel.C*x
@@ -694,6 +714,7 @@ end
694714
end
695715

696716
@testitem "NonLinMPC set constraints" setup=[SetupMPCtests] begin
717+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
697718
linmodel1 = LinModel(sys,Ts,i_d=[3])
698719
nmpc_lin = NonLinMPC(linmodel1, Hp=1, Hc=1)
699720

@@ -729,6 +750,7 @@ end
729750
end
730751

731752
@testitem "NonLinMPC constraint violation" setup=[SetupMPCtests] begin
753+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
732754
gc(Ue, Ŷe, _ ,p , ϵ) = [p[1]*(Ue[1:end-1] .- 4.2 .- ϵ); p[2]*(Ŷe[2:end] .- 3.14 .- ϵ)]
733755
Hp=50
734756

@@ -737,7 +759,7 @@ end
737759

738760
setconstraint!(nmpc_lin, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf])
739761
setconstraint!(nmpc_lin, umin=[-10], umax=[10])
740-
setconstraint!(nmpc_lin, Δumin=[-15], Δumax=[15])
762+
setconstraint!(nmpc_lin, Δumin=[-1e6], Δumax=[1e6])
741763
setconstraint!(nmpc_lin, ymin=[-100], ymax=[100])
742764
preparestate!(nmpc_lin, [0])
743765

@@ -805,8 +827,8 @@ end
805827
nonlinmodel = NonLinModel(f, h, linmodel.Ts, 1, 1, 1, solver=nothing, p=linmodel)
806828
nmpc = NonLinMPC(nonlinmodel, Hp=50, Hc=5, gc=gc, nc=2Hp, p=[0; 0])
807829

808-
setconstraint!(nmpc, x̂min=[-1e6,-Inf], x̂max=[1e6,+Inf])
809-
setconstraint!(nmpc, umin=[-10], umax=[10])
830+
setconstraint!(nmpc, x̂min=[-1e6,-Inf], x̂max=[+1e6,+Inf])
831+
setconstraint!(nmpc, umin=[-1e6], umax=[+1e6])
810832
setconstraint!(nmpc, Δumin=[-15], Δumax=[15])
811833
setconstraint!(nmpc, ymin=[-100], ymax=[100])
812834
preparestate!(nmpc, [0])
@@ -818,7 +840,7 @@ end
818840
moveinput!(nmpc, [100])
819841
info = getinfo(nmpc)
820842
@test all(isapprox.(info[:U], 4; atol=1e-1))
821-
setconstraint!(nmpc, umin=[-10], umax=[10])
843+
setconstraint!(nmpc, umin=[-1e6], umax=[+1e6])
822844

823845
setconstraint!(nmpc, Δumin=[-1.5], Δumax=[1.25])
824846
moveinput!(nmpc, [-100])
@@ -827,7 +849,7 @@ end
827849
moveinput!(nmpc, [100])
828850
info = getinfo(nmpc)
829851
@test all(isapprox.(info[:ΔU], 1.25; atol=1e-1))
830-
setconstraint!(nmpc, Δumin=[-15], Δumax=[15])
852+
setconstraint!(nmpc, Δumin=[-1e6], Δumax=[+1e6])
831853

832854
setconstraint!(nmpc, ymin=[-0.5], ymax=[0.9])
833855
moveinput!(nmpc, [-100])
@@ -839,13 +861,13 @@ end
839861
setconstraint!(nmpc, ymin=[-100], ymax=[100])
840862

841863
setconstraint!(nmpc, Ymin=[-0.5; fill(-100, Hp-1)], Ymax=[0.9; fill(+100, Hp-1)])
842-
moveinput!(nmpc, [-10])
864+
moveinput!(nmpc, [-200])
843865
info = getinfo(nmpc)
844-
@test info[:Ŷ][end] -10 atol=1e-1
866+
@test info[:Ŷ][end] -100 atol=1e-1
845867
@test info[:Ŷ][begin] -0.5 atol=1e-1
846-
moveinput!(nmpc, [10])
868+
moveinput!(nmpc, [200])
847869
info = getinfo(nmpc)
848-
@test info[:Ŷ][end] 10 atol=1e-1
870+
@test info[:Ŷ][end] 100 atol=1e-1
849871
@test info[:Ŷ][begin] 0.9 atol=1e-1
850872
setconstraint!(nmpc, ymin=[-100], ymax=[100])
851873

@@ -873,6 +895,7 @@ end
873895
end
874896

875897
@testitem "NonLinMPC set model" setup=[SetupMPCtests] begin
898+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
876899
estim = KalmanFilter(setop!(LinModel(tf(5, [200, 1]), 300), yop=[10], uop=[1]))
877900
mpc = NonLinMPC(estim, Nwt=[0], Cwt=1e4, Hp=1000, Hc=1)
878901
mpc = setconstraint!(mpc, umin=[-24], umax=[26])
@@ -925,6 +948,7 @@ end
925948
end
926949

927950
@testitem "LinMPC v.s. NonLinMPC" setup=[SetupMPCtests] begin
951+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
928952
linmodel = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
929953
f = (x,u,d,_) -> linmodel.A*x + linmodel.Bu*u + linmodel.Bd*d
930954
h = (x,d,_) -> linmodel.C*x + linmodel.Dd*d

test/test_sim_model.jl

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
@testitem "LinModel construction" setup=[SetupMPCtests] begin
2+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
23
linmodel1 = LinModel(sys, Ts, i_u=1:2)
34
@test linmodel1.nx == 2
45
@test linmodel1.nu == 2
@@ -97,6 +98,7 @@
9798
end
9899

99100
@testitem "LinModel sim methods" setup=[SetupMPCtests] begin
101+
using .SetupMPCtests, ControlSystemsBase
100102
linmodel1 = setop!(LinModel(Gss), uop=[10,50], yop=[50,30])
101103
@test updatestate!(linmodel1, [10, 50]) zeros(2)
102104
@test updatestate!(linmodel1, [10, 50], Float64[]) zeros(2)
@@ -117,6 +119,7 @@ end
117119
end
118120

119121
@testitem "LinModel real time simulations" setup=[SetupMPCtests] begin
122+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
120123
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
121124
times1 = zeros(5)
122125
for i=1:5
@@ -136,6 +139,7 @@ end
136139
end
137140

138141
@testitem "NonLinModel construction" setup=[SetupMPCtests] begin
142+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
139143
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
140144
f1(x,u,_,model) = model.A*x + model.Bu*u
141145
h1(x,_,model) = model.C*x
@@ -243,6 +247,7 @@ end
243247
end
244248

245249
@testitem "NonLinModel sim methods" setup=[SetupMPCtests] begin
250+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
246251
linmodel1 = LinModel(sys,Ts,i_u=[1,2])
247252
f1(x,u,_,model) = model.A*x + model.Bu*u
248253
h1(x,_,model) = model.C*x
@@ -262,6 +267,7 @@ end
262267
end
263268

264269
@testitem "NonLinModel linearization" setup=[SetupMPCtests] begin
270+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
265271
Ts = 1.0
266272
f1(x,u,d,_) = x.^5 + u.^4 + d.^3
267273
h1(x,d,_) = x.^2 + d
@@ -317,6 +323,7 @@ end
317323
end
318324

319325
@testitem "NonLinModel real time simulations" setup=[SetupMPCtests] begin
326+
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
320327
linmodel1 = LinModel(tf(2, [10, 1]), 0.1)
321328
nonlinmodel1 = NonLinModel(
322329
(x,u,_,_)->linmodel1.A*x + linmodel1.Bu*u,

0 commit comments

Comments
 (0)