112112 @test x̂ ≈ [0 , 0 ]
113113 @test isa (x̂, Vector{Float32})
114114 @test_throws ArgumentError updatestate! (kalmanfilter1, [10 , 50 ])
115- @test_throws ErrorException setstate! (kalmanfilter1, [1 ,2 ,3 ,4 ], diagm (.1 : .1 : .4 ))
116115end
117116
118117@testitem " SteadyKalmanFilter set model" setup= [SetupMPCtests] begin
@@ -904,17 +903,26 @@ end
904903 @test_throws ArgumentError MovingHorizonEstimator (linmodel)
905904 @test_throws ArgumentError MovingHorizonEstimator (linmodel, He= 0 )
906905 @test_throws ArgumentError MovingHorizonEstimator (linmodel, Cwt= - 1 )
906+ @test_throws ErrorException MovingHorizonEstimator (
907+ nonlinmodel, 5 , 1 : 2 , 0 , [1 , 1 ], I_6, I_6, I_2, Inf ; optim,
908+ covestim = InternalModel (nonlinmodel)
909+ )
910+ @test_throws ArgumentError MovingHorizonEstimator (
911+ nonlinmodel, 5 , 1 : 2 , 0 , [1 , 1 ], I_6, I_6, I_2, Inf ; optim,
912+ covestim = UnscentedKalmanFilter (nonlinmodel, nint_ym= [2 ,2 ])
913+ )
907914end
908915
909916@testitem " MovingHorizonEstimator estimation and getinfo" setup= [SetupMPCtests] begin
910- using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, JuMP, Ipopt, ForwardDiff
917+ using . SetupMPCtests, ControlSystemsBase, LinearAlgebra, ForwardDiff
918+ using JuMP, Ipopt, DAQP
911919 linmodel = LinModel (sys,Ts,i_u= [1 ,2 ], i_d= [3 ])
912920 linmodel = setop! (linmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
913921 f (x,u,d,model) = model. A* x + model. Bu* u + model. Bd* d
914922 h (x,d,model) = model. C* x + model. Dd* d
915923 nonlinmodel = NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 , solver= nothing , p= linmodel)
916924 nonlinmodel = setop! (nonlinmodel, uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
917-
925+
918926 mhe1 = MovingHorizonEstimator (nonlinmodel, He= 2 )
919927 JuMP. set_attribute (mhe1. optim, " tol" , 1e-7 )
920928 preparestate! (mhe1, [50 , 30 ], [5 ])
950958 x̂ = updatestate! (mhe1, [10 , 50 ], [50 , 30 ], [5 ])
951959 @test x̂ ≈ zeros (6 ) atol= 1e-9
952960 @test mhe1. x̂0 ≈ zeros (6 ) atol= 1e-9
953- @test evaloutput (mhe1, [5 ]) ≈ mhe1 ([5 ]) ≈ [50 , 30 ]
954961 info = getinfo (mhe1)
955962 @test info[:x̂ ] ≈ x̂ atol= 1e-9
956963 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
976983 @test x̂ ≈ zeros (6 ) atol= 1e-9
977984 @test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
978985 preparestate! (mhe2, [50 , 30 ], [5 ])
979- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
980986 info = getinfo (mhe2)
981987 @test info[:x̂ ] ≈ x̂ atol= 1e-9
982988 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
9981004 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ], [5 ])
9991005 @test x̂ ≈ zeros (6 ) atol= 1e-9
10001006 @test mhe2. x̂0 ≈ zeros (6 ) atol= 1e-9
1001- @test evaloutput (mhe2, [5 ]) ≈ mhe2 ([5 ]) ≈ [50 , 30 ]
10021007 info = getinfo (mhe2)
10031008 @test info[:x̂ ] ≈ x̂ atol= 1e-9
10041009 @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
@@ -1012,12 +1017,29 @@ end
10121017 updatestate! (mhe2, [10 , 50 ], [51 , 32 ], [5 ])
10131018 end
10141019 @test mhe2 ([5 ]) ≈ [51 , 32 ] atol= 1e-2
1020+
1021+ Q̂ = diagm ([1 / 4 , 1 / 4 , 1 / 4 , 1 / 4 ]. ^ 2 )
1022+ R̂ = diagm ([1 , 1 ]. ^ 2 )
1023+ optim = Model (DAQP. Optimizer)
1024+ covestim = SteadyKalmanFilter (linmodel, 1 : 2 , 0 , 0 , Q̂, R̂)
1025+ P̂_0 = covestim. cov. P̂
1026+ mhe3 = MovingHorizonEstimator (linmodel, 2 , 1 : 2 , 0 , 0 , P̂_0, Q̂, R̂; optim, covestim)
1027+ preparestate! (mhe3, [50 , 30 ], [5 ])
1028+ x̂ = updatestate! (mhe3, [10 , 50 ], [50 , 30 ], [5 ])
1029+ @test x̂ ≈ zeros (4 ) atol= 1e-9
1030+ @test mhe3. x̂0 ≈ zeros (4 ) atol= 1e-9
1031+ preparestate! (mhe3, [50 , 30 ], [5 ])
1032+ info = getinfo (mhe3)
1033+ @test info[:x̂ ] ≈ x̂ atol= 1e-9
1034+ @test info[:Ŷ ][end - 1 : end ] ≈ [50 , 30 ] atol= 1e-9
1035+
10151036 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
10161037 mhe3 = MovingHorizonEstimator (linmodel3, He= 1 )
10171038 preparestate! (mhe3, [0 ])
10181039 x̂ = updatestate! (mhe3, [0 ], [0 ])
10191040 @test x̂ ≈ [0 , 0 ] atol= 1e-3
10201041 @test isa (x̂, Vector{Float32})
1042+
10211043 mhe4 = setconstraint! (MovingHorizonEstimator (nonlinmodel, He= 1 , nint_ym= 0 ), v̂max= [50 ,50 ])
10221044 g_V̂max_end = mhe4. optim[:g_V̂max_2 ]. func
10231045 # execute update_predictions! branch in `gfunc_i` for coverage:
@@ -1027,7 +1049,7 @@ end
10271049 R̂ = diagm ([1 , 1 ]. ^ 2 )
10281050 optim = Model (Ipopt. Optimizer)
10291051 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)
1052+ mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂; optim, covestim)
10311053 preparestate! (mhe5, [50 , 30 ], [5 ])
10321054 x̂ = updatestate! (mhe5, [10 , 50 ], [50 , 30 ], [5 ])
10331055 @test x̂ ≈ zeros (4 ) atol= 1e-9
0 commit comments