@@ -566,32 +566,42 @@ end
566566
567567@testset " NonLinMPC moves and getinfo" begin
568568 linmodel = setop! (LinModel (tf (5 , [2000 , 1 ]), 3000.0 ), yop= [10 ])
569- nmpc_lin = NonLinMPC (linmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
570- r = [15 ]
569+ Hp = 1000
570+ nmpc_lin = NonLinMPC (linmodel, Nwt= [0 ], Hp= Hp, Hc= 1 )
571+ ry, ru = [15 ], [4 ]
571572 preparestate! (nmpc_lin, [10 ])
572- u = moveinput! (nmpc_lin, r )
573+ u = moveinput! (nmpc_lin, ry )
573574 @test u ≈ [1 ] atol= 5e-2
574- u = nmpc_lin (r )
575+ u = nmpc_lin (ry )
575576 @test u ≈ [1 ] atol= 5e-2
576577 info = getinfo (nmpc_lin)
577578 @test info[:u ] ≈ u
578- @test info[:Ŷ ][end ] ≈ r[1 ] atol= 5e-2
579- Hp = 1000
580- R̂y = fill (r[1 ], Hp)
581- JE = (_ , Ŷe, _ , R̂y) -> sum ((Ŷe[2 : end ] - R̂y). ^ 2 )
582- nmpc = NonLinMPC (linmodel, Mwt= [0 ], Nwt= [0 ], Cwt= Inf , Ewt= 1 , JE= JE, p= R̂y, Hp= Hp, Hc= 1 )
579+ @test info[:Ŷ ][end ] ≈ ry[1 ] atol= 5e-2
580+ setmodel! (nmpc_lin; Mwt= [0 ], Lwt= [1 ])
581+ u = moveinput! (nmpc_lin; R̂u= fill (ru[1 ], Hp))
582+ @test u ≈ [4 ] atol= 5e-2
583+ function JE (Ue, Ŷe, _ , p)
584+ Wy, R̂y, Wu, R̂u = p
585+ return Wy* sum ((R̂y- Ŷe[2 : end ]). ^ 2 ) + Wu* sum ((R̂u- Ue[1 : end - 1 ]). ^ 2 )
586+ end
587+ R̂y, R̂u = fill (ry[1 ], Hp), fill (ru[1 ], Hp)
588+ p = [1 , R̂y, 0 , R̂u]
589+ nmpc = NonLinMPC (linmodel, Mwt= [0 ], Nwt= [0 ], Cwt= Inf , Ewt= 1 , JE= JE, p= p, Hp= Hp, Hc= 1 )
583590 preparestate! (nmpc, [10 ])
584591 u = moveinput! (nmpc)
585592 @test u ≈ [1 ] atol= 5e-2
586593 # ensure that the current estimated output is updated for correct JE values:
587594 @test nmpc. ŷ ≈ evaloutput (nmpc. estim, Float64[])
595+ nmpc. p .= [0 , R̂y, 1 , R̂u]
596+ u = moveinput! (nmpc)
597+ @test u ≈ [4 ] atol= 5e-2
588598 linmodel2 = LinModel ([tf (5 , [2000 , 1 ]) tf (7 , [8000 ,1 ])], 3000.0 , i_d= [2 ])
589599 f = (x,u,d,_) -> linmodel2. A* x + linmodel2. Bu* u + linmodel2. Bd* d
590600 h = (x,d,_) -> linmodel2. C* x + linmodel2. Dd* d
591601 nonlinmodel = NonLinModel (f, h, 3000.0 , 1 , 2 , 1 , 1 , solver= nothing )
592602 nmpc2 = NonLinMPC (nonlinmodel, Nwt= [0 ], Hp= 1000 , Hc= 1 )
593603 d = [0.1 ]
594- preparestate! (nmpc2, [0 ], [ 0 ] )
604+ preparestate! (nmpc2, [0 ], d )
595605 u = moveinput! (nmpc2, 7 d, d)
596606 @test u ≈ [0 ] atol= 5e-2
597607 u = nmpc2 (7 d, d)
613623 @test g_Y0min_end (20.0 , 10.0 ) ≤ 0.0
614624 # test gfunc_i(i,::NTuple{N, ForwardDiff.Dual}) :
615625 @test ForwardDiff. gradient (vec-> g_Y0min_end (vec... ), [20.0 , 10.0 ]) ≈ [- 5 , - 5 ] atol= 1e-3
616- linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros ( 1 , 0 ), zeros ( 1 , 0 ), 1 .0 )
626+ linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), 0 , 0 , 3000 .0 )
617627 nmpc6 = NonLinMPC (linmodel3, Hp= 10 )
618628 preparestate! (nmpc6, [0 ])
619629 @test moveinput! (nmpc6, [0 ]) ≈ [0.0 ]
@@ -720,10 +730,11 @@ end
720730end
721731
722732@testset " NonLinMPC constraint violation" begin
723- gc (Ue, Ŷe, _ ,p , ϵ) = [p[1 ]* (Ue .- 4.2 .- ϵ); p[2 ]* (Ŷe .- 3.14 .- ϵ)]
733+ gc (Ue, Ŷe, _ ,p , ϵ) = [p[1 ]* (Ue[1 : end - 1 ] .- 4.2 .- ϵ); p[2 ]* (Ŷe[2 : end ] .- 3.14 .- ϵ)]
734+ Hp= 50
724735
725736 linmodel = LinModel (tf ([2 ], [10000 , 1 ]), 3000.0 )
726- nmpc_lin = NonLinMPC (linmodel, Hp= 50 , Hc= 5 , gc= gc, nc= 2 * ( 50 + 1 ) , p= [0 ; 0 ])
737+ nmpc_lin = NonLinMPC (linmodel, Hp= Hp , Hc= 5 , gc= gc, nc= 2 Hp , p= [0 ; 0 ])
727738
728739 setconstraint! (nmpc_lin, x̂min= [- 1e6 ,- Inf ], x̂max= [1e6 ,+ Inf ])
729740 setconstraint! (nmpc_lin, umin= [- 10 ], umax= [10 ])
758769 @test all (isapprox .(info[:Ŷ ], 0.9 ; atol= 1e-1 ))
759770 setconstraint! (nmpc_lin, ymin= [- 100 ], ymax= [100 ])
760771
761- setconstraint! (nmpc_lin, Ymin= [- 0.5 ; fill (- 100 , 49 )], Ymax= [0.9 ; fill (+ 100 , 49 )])
772+ setconstraint! (nmpc_lin, Ymin= [- 0.5 ; fill (- 100 , Hp - 1 )], Ymax= [0.9 ; fill (+ 100 , Hp - 1 )])
762773 moveinput! (nmpc_lin, [- 10 ])
763774 info = getinfo (nmpc_lin)
764775 @test info[:Ŷ ][end ] ≈ - 10 atol= 1e-1
@@ -782,17 +793,18 @@ end
782793 moveinput! (nmpc_lin, [100 ])
783794 info = getinfo (nmpc_lin)
784795 @test all (isapprox .(info[:U ], 4.2 ; atol= 1e-1 ))
796+ @test all (isapprox .(info[:gc ][1 : Hp], 0.0 ; atol= 1e-1 ))
785797
786798 nmpc_lin. p .= [0 ; 1 ]
787799 moveinput! (nmpc_lin, [100 ])
788800 info = getinfo (nmpc_lin)
789801 @test all (isapprox .(info[:Ŷ ], 3.14 ; atol= 1e-1 ))
790-
802+ @test all ( isapprox .(info[ :gc ][Hp + 1 : end ], 0.0 ; atol = 1e-1 ))
791803
792804 f = (x,u,_,p) -> p. A* x + p. Bu* u
793805 h = (x,_,p) -> p. C* x
794806 nonlinmodel = NonLinModel (f, h, linmodel. Ts, 1 , 1 , 1 , solver= nothing , p= linmodel)
795- nmpc = NonLinMPC (nonlinmodel, Hp= 50 , Hc= 5 , gc= gc, nc= 2 * ( 50 + 1 ) , p= [0 ; 0 ])
807+ nmpc = NonLinMPC (nonlinmodel, Hp= 50 , Hc= 5 , gc= gc, nc= 2 Hp , p= [0 ; 0 ])
796808
797809 setconstraint! (nmpc, x̂min= [- 1e6 ,- Inf ], x̂max= [1e6 ,+ Inf ])
798810 setconstraint! (nmpc, umin= [- 10 ], umax= [10 ])
827839 @test all (isapprox .(info[:Ŷ ], 0.9 ; atol= 1e-1 ))
828840 setconstraint! (nmpc, ymin= [- 100 ], ymax= [100 ])
829841
830- setconstraint! (nmpc, Ymin= [- 0.5 ; fill (- 100 , 49 )], Ymax= [0.9 ; fill (+ 100 , 49 )])
842+ setconstraint! (nmpc, Ymin= [- 0.5 ; fill (- 100 , Hp - 1 )], Ymax= [0.9 ; fill (+ 100 , Hp - 1 )])
831843 moveinput! (nmpc, [- 10 ])
832844 info = getinfo (nmpc)
833845 @test info[:Ŷ ][end ] ≈ - 10 atol= 1e-1
@@ -851,11 +863,13 @@ end
851863 moveinput! (nmpc, [100 ])
852864 info = getinfo (nmpc)
853865 @test all (isapprox .(info[:U ], 4.2 ; atol= 1e-1 ))
866+ @test all (isapprox .(info[:gc ][1 : Hp], 0.0 ; atol= 1e-1 ))
854867
855868 nmpc. p .= [0 ; 1 ]
856869 moveinput! (nmpc, [100 ])
857870 info = getinfo (nmpc)
858871 @test all (isapprox .(info[:Ŷ ], 3.14 ; atol= 1e-1 ))
872+ @test all (isapprox .(info[:gc ][Hp+ 1 : end ], 0.0 ; atol= 1e-1 ))
859873
860874end
861875
0 commit comments