@@ -907,14 +907,15 @@ end
907907end
908908
909909@testitem " MovingHorizonEstimator estimation and getinfo" setup= [SetupMPCtests] begin
910- using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
910+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
911+ using JuMP, Ipopt, DAQP
911912 linmodel = LinModel (sys,Ts,i_u= [1 ,2 ], i_d= [3 ])
912913 linmodel = setop! (linmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
913914 f (x,u,d,model) = model. A* x + model. Bu* u + model. Bd* d
914915 h (x,d,model) = model. C* x + model. Dd* d
915916 nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 , solver= nothing , p= linmodel)
916917 nonlinmodel = setop! (nonlinmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
917-
918+
918919 mhe1 = MovingHorizonEstimator (nonlinmodel, He= 2 )
919920 JuMP. set_attribute (mhe1. optim, " tol" , 1e-7 )
920921 preparestate! (mhe1, [50 , 30 ], [5 ])
950951 x̂ = updatestate! (mhe1, [10 , 50 ], [50 , 30 ], [5 ])
951952 @test x̂ ≈ zeros (6 ) atol= 1e-9
952953 @test mhe1. x̂0 ≈ zeros (6 ) atol= 1e-9
953- @test evaloutput (mhe1, [5 ]) ≈ mhe1 ([5 ]) ≈ [50 , 30 ]
954954 info = getinfo (mhe1)
955955 @test info[:x̂ ] ≈ x̂ atol= 1e-9
956956 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
976976 @test x̂ ≈ zeros (6 ) atol= 1e-9
977977 @test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
978978 preparestate! (mhe2, [50 , 30 ], [5 ])
979- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
980979 info = getinfo (mhe2)
981980 @test info[:x̂ ] ≈ x̂ atol= 1e-9
982981 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
998997 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ], [5 ])
999998 @test x̂ ≈ zeros (6 ) atol= 1e-9
1000999 @test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
1001- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
10021000 info = getinfo (mhe2)
10031001 @test info[:x̂ ] ≈ x̂ atol= 1e-9
10041002 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
@@ -1012,12 +1010,29 @@ end
10121010 updatestate! (mhe2, [10 , 50 ], [51 , 32 ], [5 ])
10131011 end
10141012 @test mhe2 ([5 ]) ≈ [51 , 32 ] atol= 1e-2
1013+
1014+ Q̂ = diagm ([1 / 4 , 1 / 4 , 1 / 4 , 1 / 4 ]. ^ 2 )
1015+ R̂ = diagm ([1 , 1 ]. ^ 2 )
1016+ optim = Model (DAQP. Optimizer)
1017+ covestim = SteadyKalmanFilter (linmodel, 1 : 2 , 0 , 0 , Q̂, R̂)
1018+ P̂_0 = covestim. cov. P̂
1019+ mhe3 = MovingHorizonEstimator (linmodel, 2 , 1 : 2 , 0 , 0 , P̂_0, Q̂, R̂; optim, covestim)
1020+ preparestate! (mhe3, [50 , 30 ], [5 ])
1021+ x̂ = updatestate! (mhe3, [10 , 50 ], [50 , 30 ], [5 ])
1022+ @test x̂ ≈ zeros (4 ) atol= 1e-9
1023+ @test mhe3. x̂0 ≈ zeros (4 ) atol= 1e-9
1024+ preparestate! (mhe3, [50 , 30 ], [5 ])
1025+ info = getinfo (mhe3)
1026+ @test info[:x̂ ] ≈ x̂ atol= 1e-9
1027+ @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
1028+
10151029 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
10161030 mhe3 = MovingHorizonEstimator (linmodel3, He= 1 )
10171031 preparestate! (mhe3, [0 ])
10181032 x̂ = updatestate! (mhe3, [0 ], [0 ])
10191033 @test x̂ ≈ [0 , 0 ] atol= 1e-3
10201034 @test isa (x̂, Vector{Float32})
1035+
10211036 mhe4 = setconstraint! (MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 ), v̂max= [50 ,50 ])
10221037 g_V̂max_end = mhe4. optim[:g_V̂max_2 ]. func
10231038 # execute update_predictions! branch in `gfunc_i` for coverage:
@@ -1027,7 +1042,7 @@ end
10271042 R̂ = diagm ([1 , 1 ]. ^ 2 )
10281043 optim = Model (Ipopt. Optimizer)
10291044 covestim = ExtendedKalmanFilter (nonlinmodel, 1 : 2 , 0 , 0 , Q̂, Q̂, R̂)
1030- mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂, Inf ; optim, covestim)
1045+ mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂; optim, covestim)
10311046 preparestate! (mhe5, [50 , 30 ], [5 ])
10321047 x̂ = updatestate! (mhe5, [10 , 50 ], [50 , 30 ], [5 ])
10331048 @test x̂ ≈ zeros (4 ) atol= 1e-9
0 commit comments