11Ts = 4.0
22sys = [ tf (1.90 ,[18.0 ,1 ]) tf (1.90 ,[18.0 ,1 ]) tf (1.90 ,[18.0 ,1 ]);
33 tf (- 0.74 ,[8.0 ,1 ]) tf (0.74 ,[8.0 ,1 ]) tf (- 0.74 ,[8.0 ,1 ]) ]
4-
4+ #=
55@testset "SteadyKalmanFilter construction" begin
66 linmodel1 = LinModel(sys,Ts,i_u=[1,2])
77 skalmanfilter1 = SteadyKalmanFilter(linmodel1)
@@ -618,45 +618,43 @@ end
618618 @test_throws ArgumentError MovingHorizonEstimator(linmodel1)
619619 @test_throws ArgumentError MovingHorizonEstimator(linmodel1, He=0)
620620end
621-
621+ =#
622622@testset " MovingHorizonEstimator estimator methods" begin
623- linmodel1 = LinModel (sys,Ts,i_u= [1 ,2 ])
624- f (x,u,_ ) = linmodel1. A* x + linmodel1. Bu* u
625- h (x,_ ) = linmodel1. C* x
626- nonlinmodel = setop! (NonLinModel (f, h, 1000 * Ts, 2 , 2 , 2 ), uop= [10 ,50 ], yop= [50 ,30 ])
623+ linmodel1 = LinModel (sys,Ts,i_u= [1 ,2 ], i_d = [ 3 ] )
624+ f (x,u,d ) = linmodel1. A* x + linmodel1. Bu* u + linmodel1 . Bd * d
625+ h (x,d ) = linmodel1. C* x + linmodel1 . Dd * d
626+ nonlinmodel = setop! (NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 ), uop= [10 ,50 ], yop= [50 ,30 ], dop = [ 5 ])
627627 mhe1 = MovingHorizonEstimator (nonlinmodel, He= 2 )
628- @test updatestate! (mhe1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 ) atol= 1e-9
629- @test updatestate! (mhe1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 ) atol= 1e-9
630- @test mhe1. x̂ ≈ zeros (4 ) atol= 1e-9
631- @test evaloutput (mhe1) ≈ mhe1 () ≈ [50 , 30 ]
632- @test evaloutput (mhe1, Float64[]) ≈ mhe1 (Float64[]) ≈ [50 , 30 ]
633- @test initstate! (mhe1, [10 , 50 ], [50 , 30 + 1 ]) ≈ zeros (4 ) atol= 1e-9
634- setstate! (mhe1, [1 ,2 ,3 ,4 ])
635- @test mhe1. x̂ ≈ [1 ,2 ,3 ,4 ]
628+ @test updatestate! (mhe1, [10 , 50 ], [50 , 30 ], [5 ]) ≈ zeros (6 ) atol= 1e-9
629+ @test mhe1. x̂ ≈ zeros (6 ) atol= 1e-9
630+ @test evaloutput (mhe1, [5 ]) ≈ mhe1 ([5 ]) ≈ [50 , 30 ]
631+ @test initstate! (mhe1, [10 , 50 ], [50 , 30 + 1 ], [5 ]) ≈ zeros (6 ) atol= 1e-9
632+ setstate! (mhe1, [1 ,2 ,3 ,4 ,5 ,6 ])
633+ @test mhe1. x̂ ≈ [1 ,2 ,3 ,4 ,5 ,6 ]
636634 for i in 1 : 100
637- updatestate! (mhe1, [11 , 52 ], [50 , 30 ])
635+ updatestate! (mhe1, [11 , 52 ], [50 , 30 ], [ 5 ] )
638636 end
639- @test mhe1 () ≈ [50 , 30 ] atol= 1e-3
637+ @test mhe1 ([ 5 ] ) ≈ [50 , 30 ] atol= 1e-3
640638 for i in 1 : 100
641- updatestate! (mhe1, [10 , 50 ], [51 , 32 ])
639+ updatestate! (mhe1, [10 , 50 ], [51 , 32 ], [ 5 ] )
642640 end
643- @test mhe1 () ≈ [51 , 32 ] atol= 1e-3
641+ @test mhe1 ([ 5 ] ) ≈ [51 , 32 ] atol= 1e-3
644642 mhe2 = MovingHorizonEstimator (linmodel1, He= 2 , nint_u= [1 , 1 ], nint_ym= [0 , 0 ])
645643 for i in 1 : 100
646- updatestate! (mhe2, [11 , 52 ], [50 , 30 ])
644+ updatestate! (mhe2, [11 , 52 ], [50 , 30 ], [ 5 ] )
647645 end
648- @test mhe2 () ≈ [50 , 30 ] atol= 1e-3
646+ @test mhe2 ([ 5 ] ) ≈ [50 , 30 ] atol= 1e-3
649647 for i in 1 : 100
650- updatestate! (mhe2, [10 , 50 ], [51 , 32 ])
648+ updatestate! (mhe2, [10 , 50 ], [51 , 32 ], [ 5 ] )
651649 end
652- @test mhe2 () ≈ [51 , 32 ] atol= 1e-3
650+ @test mhe2 ([ 5 ] ) ≈ [51 , 32 ] atol= 1e-3
653651 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
654652 mhe3 = MovingHorizonEstimator (linmodel3, He= 1 )
655653 x̂ = updatestate! (mhe3, [0 ], [0 ])
656654 @test x̂ ≈ [0 , 0 ] atol= 1e-3
657655 @test isa (x̂, Vector{Float32})
658656
659- mhe4 = setconstraint! (MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 ), x̂max= [50 ,50 ])
657+ mhe4 = setconstraint! (MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 ), x̂max= [50 ,50 , 50 , 50 ])
660658 g_X̂max_end = mhe4. optim. nlp_model. operators. registered_multivariate_operators[end ]. f
661659 @test g_X̂max_end ((1.0 , 1.0 , 1.0 , 1.0 )) ≤ 0.0 # test gfunc_i(i,::NTuple{N, Float64})
662660 # test gfunc_i(i,::NTuple{N, ForwardDiff.Dual}) :
@@ -669,18 +667,34 @@ end
669667 setconstraint! (mhe1, x̂min= [- 51 ,- 52 ], x̂max= [53 ,54 ])
670668 @test all ((mhe1. con. X̂min, mhe1. con. X̂max) .≈ ([- 51 ,- 52 ], [53 ,54 ]))
671669 @test all ((mhe1. con. x̂min, mhe1. con. x̂max) .≈ ([- 51 ,- 52 ], [53 ,54 ]))
670+ setconstraint! (mhe1, ŵmin= [- 55 ,- 56 ], ŵmax= [57 ,58 ])
671+ @test all ((mhe1. con. Ŵmin, mhe1. con. Ŵmax) .≈ ([- 55 ,- 56 ], [57 ,58 ]))
672+ setconstraint! (mhe1, v̂min= [- 59 ,- 60 ], v̂max= [61 ,62 ])
673+ @test all ((mhe1. con. V̂min, mhe1. con. V̂max) .≈ ([- 59 ,- 60 ], [61 ,62 ]))
672674
673675 mhe2 = MovingHorizonEstimator (linmodel1, He= 4 , nint_ym= 0 )
674676 setconstraint! (mhe2, X̂min= - 1 (1 : 10 ), X̂max= 1 (1 : 10 ))
675677 @test all ((mhe2. con. X̂min, mhe2. con. X̂max) .≈ (- 1 (3 : 10 ), 1 (3 : 10 )))
676678 @test all ((mhe2. con. x̂min, mhe2. con. x̂max) .≈ (- 1 (1 : 2 ), 1 (1 : 2 )))
679+ setconstraint! (mhe2, Ŵmin= - 1 (11 : 18 ), Ŵmax= 1 (11 : 18 ))
680+ @test all ((mhe2. con. Ŵmin, mhe2. con. Ŵmax) .≈ (- 1 (11 : 18 ), 1 (11 : 18 )))
681+ setconstraint! (mhe2, V̂min= - 1 (31 : 38 ), V̂max= 1 (31 : 38 ))
682+ @test all ((mhe2. con. V̂min, mhe2. con. V̂max) .≈ (- 1 (31 : 38 ), 1 (31 : 38 )))
677683
678684 @test_throws ArgumentError setconstraint! (mhe2, x̂min= [- 1 ])
679685 @test_throws ArgumentError setconstraint! (mhe2, x̂max= [+ 1 ])
686+ @test_throws ArgumentError setconstraint! (mhe2, ŵmin= [- 1 ])
687+ @test_throws ArgumentError setconstraint! (mhe2, ŵmax= [+ 1 ])
688+ @test_throws ArgumentError setconstraint! (mhe2, v̂min= [- 1 ])
689+ @test_throws ArgumentError setconstraint! (mhe2, v̂max= [+ 1 ])
680690
681691 updatestate! (mhe1, [10 , 50 ], [50 , 30 ])
682692 @test_throws ErrorException setconstraint! (mhe1, x̂min= [- Inf ,- Inf ])
683693 @test_throws ErrorException setconstraint! (mhe1, x̂max= [+ Inf ,+ Inf ])
694+ @test_throws ErrorException setconstraint! (mhe1, ŵmin= [- Inf ,- Inf ])
695+ @test_throws ErrorException setconstraint! (mhe1, ŵmax= [+ Inf ,+ Inf ])
696+ @test_throws ErrorException setconstraint! (mhe1, v̂min= [- Inf ,- Inf ])
697+ @test_throws ErrorException setconstraint! (mhe1, v̂max= [+ Inf ,+ Inf ])
684698end
685699
686700@testset " MovingHorizonEstimator constraint violation" begin
@@ -690,11 +704,35 @@ end
690704 nonlinmodel = setop! (NonLinModel (f, h, Ts, 2 , 2 , 2 ), uop= [10 ,50 ], yop= [50 ,30 ])
691705 mhe = MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 )
692706
707+ setconstraint! (mhe, x̂min= [- 100 ,- 100 ], x̂max= [100 ,100 ])
708+ setconstraint! (mhe, ŵmin= [- 100 ,- 100 ], ŵmax= [100 ,100 ])
709+ setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
710+
693711 setconstraint! (mhe, x̂min= [1 ,1 ], x̂max= [100 ,100 ])
694712 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
695713 @test x̂ ≈ [1 , 1 ] atol= 1e-3
696714
697715 setconstraint! (mhe, x̂min= [- 100 ,- 100 ], x̂max= [- 1 ,- 1 ])
698716 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
699717 @test x̂ ≈ [- 1 , - 1 ] atol= 1e-3
700- end
718+
719+ setconstraint! (mhe, x̂min= [- 100 ,- 100 ], x̂max= [100 ,100 ])
720+ setconstraint! (mhe, ŵmin= [- 100 ,- 100 ], ŵmax= [100 ,100 ])
721+ setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
722+
723+ setconstraint! (mhe, ŵmin= [1 ,1 ], ŵmax= [100 ,100 ])
724+ x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
725+ @test mhe. Ŵ ≈ [1 ,1 ] atol= 1e-3
726+
727+ setconstraint! (mhe, ŵmin= [- 100 ,- 100 ], ŵmax= [- 1 ,- 1 ])
728+ x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
729+ @test mhe. Ŵ ≈ [- 1 ,- 1 ] atol= 1e-3
730+
731+ setconstraint! (mhe, x̂min= [- 100 ,- 100 ], x̂max= [100 ,100 ])
732+ setconstraint! (mhe, ŵmin= [- 100 ,- 100 ], ŵmax= [100 ,100 ])
733+ setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
734+
735+ # setconstraint!(mhe, v̂min=[1,1], v̂max=[100,100])
736+ # x̂ = updatestate!(mhe, [10, 50], [50, 30])
737+ # @test [50,30] - ModelPredictiveControl.ĥ(mhe,mhe.mode,X̂ ≈ [1,1] atol=1e-3
738+ end
0 commit comments