1
1
@testitem " LinMPC construction" setup= [SetupMPCtests] begin
2
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, DAQP
2
3
model = LinModel (sys, Ts, i_d= [3 ])
3
4
mpc1 = LinMPC (model, Hp= 15 )
4
5
@test isa (mpc1. estim, SteadyKalmanFilter)
55
56
end
56
57
57
58
@testitem " LinMPC moves and getinfo" setup= [SetupMPCtests] begin
59
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
60
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
58
61
linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3 ), yop= [10 ])
59
62
mpc1 = LinMPC (linmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
60
63
r = [15 ]
88
91
end
89
92
90
93
@testitem " LinMPC step disturbance rejection" setup= [SetupMPCtests] begin
94
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
91
95
linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop= [10 ])
92
96
r = [15 ]
93
97
outdist = [5 ]
130
134
end
131
135
132
136
@testitem " LinMPC other methods" setup= [SetupMPCtests] begin
137
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
133
138
linmodel1 = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
134
139
mpc1 = LinMPC (linmodel1)
135
140
@test initstate! (mpc1, [10 , 50 ], [50 , 30 + 1 ]) ≈ [zeros (3 ); [1 ]]
148
153
end
149
154
150
155
@testitem " LinMPC set constraints" setup= [SetupMPCtests] begin
156
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
151
157
model = LinModel (sys, Ts, i_d= [3 ])
152
158
mpc = LinMPC (model, Hp= 1 , Hc= 1 )
153
159
215
221
end
216
222
217
223
@testitem " LinMPC constraint violation" setup= [SetupMPCtests] begin
224
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
218
225
model = LinModel (tf ([2 ], [10 , 1 ]), 3.0 )
219
226
mpc = LinMPC (model, Hp= 50 , Hc= 5 )
220
227
273
280
end
274
281
275
282
@testitem " LinMPC terminal cost" setup= [SetupMPCtests] begin
283
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
276
284
model = LinModel (ss ([0.5 - 0.4 ;0.6 0.5 ], [1 0 ;0 1 ], [1 0 ; 0 1 ], 0 , 1 ))
277
285
K = lqr (Discrete, model. A, model. Bu, I, 0.5 I)
278
286
M_end = ControlSystemsBase. are (Discrete, model. A, model. Bu, I, 0.5 I)
300
308
end
301
309
302
310
@testitem " LinMPC set model" setup= [SetupMPCtests] begin
311
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
303
312
estim = KalmanFilter (setop! (LinModel (tf (5 , [2 , 1 ]), 3 ), yop= [10 ], uop= [1 ]))
304
313
mpc = LinMPC (estim, Nwt= [0 ], Cwt= 1e4 , Hp= 1000 , Hc= 1 )
305
314
mpc = setconstraint! (mpc, umin= [- 24 ], umax= [26 ])
339
348
end
340
349
341
350
@testitem " LinMPC real-time simulations" setup= [SetupMPCtests] begin
351
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
342
352
linmodel1 = LinModel (tf (2 , [10 , 1 ]), 0.1 )
343
353
mpc1 = LinMPC (linmodel1)
344
354
times1 = zeros (5 )
352
362
end
353
363
354
364
@testitem " ExplicitMPC construction" setup= [SetupMPCtests] begin
365
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
355
366
model = LinModel (sys, Ts, i_d= [3 ])
356
367
mpc1 = ExplicitMPC (model, Hp= 15 )
357
368
@test isa (mpc1. estim, SteadyKalmanFilter)
382
393
end
383
394
384
395
@testitem " ExplicitMPC moves and getinfo" setup= [SetupMPCtests] begin
396
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
385
397
mpc1 = ExplicitMPC (LinModel (tf (5 , [2 , 1 ]), 3 ), Nwt= [0 ], Hp= 1000 , Hc= 1 )
386
398
r = [5 ]
387
399
preparestate! (mpc1, [0 ])
409
421
410
422
411
423
@testitem " ExplicitMPC step disturbance rejection" setup= [SetupMPCtests] begin
424
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
412
425
linmodel = setop! (LinModel (tf (5 , [2 , 1 ]), 3.0 ), yop= [10 ])
413
426
r = [15 ]
414
427
outdist = [5 ]
451
464
end
452
465
453
466
@testitem " ExplicitMPC other methods" setup= [SetupMPCtests] begin
467
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
454
468
linmodel1 = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
455
469
mpc1 = ExplicitMPC (linmodel1)
456
470
@test initstate! (mpc1, [10 , 50 ], [50 , 30 + 1 ]) ≈ [zeros (3 ); [1 ]]
@@ -465,12 +479,14 @@ end
465
479
end
466
480
467
481
@testitem " ExplicitMPC constraints" setup= [SetupMPCtests] begin
482
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
468
483
model = LinModel (sys, Ts, i_d= [3 ])
469
484
mpc = ExplicitMPC (model, Hp= 1 , Hc= 1 )
470
485
@test_throws ErrorException setconstraint! (mpc, umin= [0.0 , 0.0 ])
471
486
end
472
487
473
488
@testitem " ExplicitMPC set model" setup= [SetupMPCtests] begin
489
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
474
490
estim = KalmanFilter (setop! (LinModel (tf (5 , [2 , 1 ]), 3 ), yop= [10 ], uop= [1 ]))
475
491
mpc = ExplicitMPC (estim, Nwt= [0 ], Hp= 1000 , Hc= 1 )
476
492
@test mpc. Yop ≈ fill (10.0 , 1000 )
500
516
end
501
517
502
518
@testitem " NonLinMPC construction" setup= [SetupMPCtests] begin
519
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
503
520
linmodel1 = LinModel (sys,Ts,i_d= [3 ])
504
521
nmpc0 = NonLinMPC (linmodel1, Hp= 15 )
505
522
@test isa (nmpc0. estim, SteadyKalmanFilter)
563
580
end
564
581
565
582
@testitem " NonLinMPC moves and getinfo" setup= [SetupMPCtests] begin
583
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
566
584
linmodel = setop! (LinModel (tf (5 , [2000 , 1 ]), 3000.0 ), yop= [10 ])
567
585
Hp = 1000
568
586
nmpc_lin = NonLinMPC (linmodel, Nwt= [0 ], Hp= Hp, Hc= 1 )
635
653
end
636
654
637
655
@testitem " NonLinMPC step disturbance rejection" setup= [SetupMPCtests] begin
656
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
638
657
linmodel = setop! (LinModel (tf (5 , [2000 , 1 ]), 3000.0 ), yop= [10 ])
639
658
r = [15 ]
640
659
outdist = [5 ]
677
696
end
678
697
679
698
@testitem " NonLinMPC other methods" setup= [SetupMPCtests] begin
699
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
680
700
linmodel = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
681
701
f = (x,u,_,_) -> linmodel. A* x + linmodel. Bu* u
682
702
h = (x,_,_) -> linmodel. C* x
694
714
end
695
715
696
716
@testitem " NonLinMPC set constraints" setup= [SetupMPCtests] begin
717
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
697
718
linmodel1 = LinModel (sys,Ts,i_d= [3 ])
698
719
nmpc_lin = NonLinMPC (linmodel1, Hp= 1 , Hc= 1 )
699
720
729
750
end
730
751
731
752
@testitem " NonLinMPC constraint violation" setup= [SetupMPCtests] begin
753
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
732
754
gc (Ue, Ŷe, _ ,p , ϵ) = [p[1 ]* (Ue[1 : end - 1 ] .- 4.2 .- ϵ); p[2 ]* (Ŷe[2 : end ] .- 3.14 .- ϵ)]
733
755
Hp= 50
734
756
737
759
738
760
setconstraint! (nmpc_lin, x̂min= [- 1e6 ,- Inf ], x̂max= [1e6 ,+ Inf ])
739
761
setconstraint! (nmpc_lin, umin= [- 10 ], umax= [10 ])
740
- setconstraint! (nmpc_lin, Δumin= [- 15 ], Δumax= [15 ])
762
+ setconstraint! (nmpc_lin, Δumin= [- 1e6 ], Δumax= [1e6 ])
741
763
setconstraint! (nmpc_lin, ymin= [- 100 ], ymax= [100 ])
742
764
preparestate! (nmpc_lin, [0 ])
743
765
805
827
nonlinmodel = NonLinModel (f, h, linmodel. Ts, 1 , 1 , 1 , solver= nothing , p= linmodel)
806
828
nmpc = NonLinMPC (nonlinmodel, Hp= 50 , Hc= 5 , gc= gc, nc= 2 Hp, p= [0 ; 0 ])
807
829
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 ])
810
832
setconstraint! (nmpc, Δumin= [- 15 ], Δumax= [15 ])
811
833
setconstraint! (nmpc, ymin= [- 100 ], ymax= [100 ])
812
834
preparestate! (nmpc, [0 ])
818
840
moveinput! (nmpc, [100 ])
819
841
info = getinfo (nmpc)
820
842
@test all (isapprox .(info[:U ], 4 ; atol= 1e-1 ))
821
- setconstraint! (nmpc, umin= [- 10 ], umax= [10 ])
843
+ setconstraint! (nmpc, umin= [- 1e6 ], umax= [+ 1e6 ])
822
844
823
845
setconstraint! (nmpc, Δumin= [- 1.5 ], Δumax= [1.25 ])
824
846
moveinput! (nmpc, [- 100 ])
827
849
moveinput! (nmpc, [100 ])
828
850
info = getinfo (nmpc)
829
851
@test all (isapprox .(info[:ΔU ], 1.25 ; atol= 1e-1 ))
830
- setconstraint! (nmpc, Δumin= [- 15 ], Δumax= [15 ])
852
+ setconstraint! (nmpc, Δumin= [- 1e6 ], Δumax= [+ 1e6 ])
831
853
832
854
setconstraint! (nmpc, ymin= [- 0.5 ], ymax= [0.9 ])
833
855
moveinput! (nmpc, [- 100 ])
@@ -839,13 +861,13 @@ end
839
861
setconstraint! (nmpc, ymin= [- 100 ], ymax= [100 ])
840
862
841
863
setconstraint! (nmpc, Ymin= [- 0.5 ; fill (- 100 , Hp- 1 )], Ymax= [0.9 ; fill (+ 100 , Hp- 1 )])
842
- moveinput! (nmpc, [- 10 ])
864
+ moveinput! (nmpc, [- 200 ])
843
865
info = getinfo (nmpc)
844
- @test info[:Ŷ ][end ] ≈ - 10 atol= 1e-1
866
+ @test info[:Ŷ ][end ] ≈ - 100 atol= 1e-1
845
867
@test info[:Ŷ ][begin ] ≈ - 0.5 atol= 1e-1
846
- moveinput! (nmpc, [10 ])
868
+ moveinput! (nmpc, [200 ])
847
869
info = getinfo (nmpc)
848
- @test info[:Ŷ ][end ] ≈ 10 atol= 1e-1
870
+ @test info[:Ŷ ][end ] ≈ 100 atol= 1e-1
849
871
@test info[:Ŷ ][begin ] ≈ 0.9 atol= 1e-1
850
872
setconstraint! (nmpc, ymin= [- 100 ], ymax= [100 ])
851
873
873
895
end
874
896
875
897
@testitem " NonLinMPC set model" setup= [SetupMPCtests] begin
898
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra
876
899
estim = KalmanFilter (setop! (LinModel (tf (5 , [200 , 1 ]), 300 ), yop= [10 ], uop= [1 ]))
877
900
mpc = NonLinMPC (estim, Nwt= [0 ], Cwt= 1e4 , Hp= 1000 , Hc= 1 )
878
901
mpc = setconstraint! (mpc, umin= [- 24 ], umax= [26 ])
925
948
end
926
949
927
950
@testitem " LinMPC v.s. NonLinMPC" setup= [SetupMPCtests] begin
951
+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt
928
952
linmodel = setop! (LinModel (sys,Ts,i_d= [3 ]), uop= [10 ,50 ], yop= [50 ,30 ], dop= [20 ])
929
953
f = (x,u,d,_) -> linmodel. A* x + linmodel. Bu* u + linmodel. Bd* d
930
954
h = (x,d,_) -> linmodel. C* x + linmodel. Dd* d
0 commit comments