6767@testset " SteadyKalmanFilter estimator methods" begin
6868 linmodel1 = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
6969 skalmanfilter1 = SteadyKalmanFilter (linmodel1, nint_ym= [1 , 1 ])
70+ preparestate! (skalmanfilter1, [50 , 30 ])
7071 @test updatestate! (skalmanfilter1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 )
72+ preparestate! (skalmanfilter1, [50 , 30 ])
7173 @test updatestate! (skalmanfilter1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 )
7274 @test skalmanfilter1. x̂0 ≈ zeros (4 )
7375 @test evaloutput (skalmanfilter1) ≈ skalmanfilter1 () ≈ [50 , 30 ]
7476 @test evaloutput (skalmanfilter1, Float64[]) ≈ skalmanfilter1 (Float64[]) ≈ [50 , 30 ]
7577 @test initstate! (skalmanfilter1, [10 , 50 ], [50 , 30 + 1 ]) ≈ [zeros (3 ); [1 ]]
7678 linmodel2 = LinModel (append (tf (1 , [1 , 0 ]), tf (2 , [10 , 1 ])), 1.0 )
77- skalmanfilter2 = SteadyKalmanFilter (linmodel2, nint_u= [1 , 1 ])
79+ skalmanfilter2 = SteadyKalmanFilter (linmodel2, nint_u= [1 , 1 ], direct = false )
7880 x = initstate! (skalmanfilter2, [10 , 3 ], [0.5 , 6 + 0.1 ])
7981 @test evaloutput (skalmanfilter2) ≈ [0.5 , 6 + 0.1 ]
8082 @test updatestate! (skalmanfilter2, [10 , 3 ], [0.5 , 6 + 0.1 ]) ≈ x
103105 @test skalmanfilter2 () ≈ [51 , 32 ] atol= 1e-3
104106 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
105107 skalmanfilter3 = SteadyKalmanFilter (linmodel3)
108+ preparestate! (skalmanfilter3, [0 ])
106109 x̂ = updatestate! (skalmanfilter3, [0 ], [0 ])
107110 @test x̂ ≈ [0 , 0 ]
108111 @test isa (x̂, Vector{Float32})
170173@testset " KalmanFilter estimator methods" begin
171174 linmodel1 = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
172175 kalmanfilter1 = KalmanFilter (linmodel1)
176+ preparestate! (kalmanfilter1, [50 , 30 ])
173177 @test updatestate! (kalmanfilter1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 )
178+ preparestate! (kalmanfilter1, [50 , 30 ])
174179 @test updatestate! (kalmanfilter1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 )
175180 @test kalmanfilter1. x̂0 ≈ zeros (4 )
176181 @test evaloutput (kalmanfilter1) ≈ kalmanfilter1 () ≈ [50 , 30 ]
201206 @test kalmanfilter2 () ≈ [51 , 32 ] atol= 1e-3
202207 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
203208 kalmanfilter3 = KalmanFilter (linmodel3)
209+ preparestate! (kalmanfilter3, [0 ])
204210 x̂ = updatestate! (kalmanfilter3, [0 ], [0 ])
205211 @test x̂ ≈ [0 , 0 ]
206212 @test isa (x̂, Vector{Float32})
213219 kalmanfilter = KalmanFilter (linmodel, nint_ym= 0 )
214220 @test kalmanfilter. Â ≈ [0.5 ]
215221 @test evaloutput (kalmanfilter) ≈ [50.0 ]
222+ preparestate! (kalmanfilter, [50.0 ])
216223 x̂ = updatestate! (kalmanfilter, [2.0 ], [50.0 ])
217224 @test x̂ ≈ [3.0 ]
218225 newlinmodel = LinModel (ss (0.2 , 0.3 , 1.0 , 0 , 10.0 ))
221228 @test kalmanfilter. Â ≈ [0.2 ]
222229 @test evaloutput (kalmanfilter) ≈ [55.0 ]
223230 @test kalmanfilter. lastu0 ≈ [2.0 - 3.0 ]
231+ preparestate! (kalmanfilter, [55.0 ])
224232 x̂ = updatestate! (kalmanfilter, [3.0 ], [55.0 ])
225233 @test x̂ ≈ [3.0 ]
226234 newlinmodel = setop! (newlinmodel, uop= [3.0 ], yop= [55.0 ], xop= [8.0 ], fop= [8.0 ])
274282@testset " Luenberger estimator methods" begin
275283 linmodel1 = setop! (LinModel (sys,Ts,i_u= [1 ,2 ]), uop= [10 ,50 ], yop= [50 ,30 ])
276284 lo1 = Luenberger (linmodel1, nint_ym= [1 , 1 ])
285+ preparestate! (lo1, [50 , 30 ])
277286 @test updatestate! (lo1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 )
287+ preparestate! (lo1, [50 , 30 ])
278288 @test updatestate! (lo1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 )
279289 @test lo1. x̂0 ≈ zeros (4 )
280290 @test evaloutput (lo1) ≈ lo1 () ≈ [50 , 30 ]
305315 @test lo2 () ≈ [51 , 32 ] atol= 1e-3
306316 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
307317 lo3 = Luenberger (linmodel3)
318+ preparestate! (lo3, [0 ])
308319 x̂ = updatestate! (lo3, [0 ], [0 ])
309320 @test x̂ ≈ [0 , 0 ]
310321 @test isa (x̂, Vector{Float32})
411422
412423 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
413424 internalmodel3 = InternalModel (linmodel3)
425+ preparestate! (internalmodel3, [0 ])
414426 x̂ = updatestate! (internalmodel3, [0 ], [0 ])
415427 @test x̂ ≈ [0 ]
416428 @test isa (x̂, Vector{Float32})
422434 internalmodel = InternalModel (linmodel)
423435 @test internalmodel. Â ≈ [0.5 ]
424436 @test evaloutput (internalmodel) ≈ [50.0 ]
437+ preparestate! (internalmodel, [50.0 ])
425438 x̂ = updatestate! (internalmodel, [2.0 ], [50.0 ])
426439 @test x̂ ≈ [3.0 ]
427440 newlinmodel = LinModel (ss (0.2 , 0.3 , 1.0 , 0 , 10.0 ))
430443 @test internalmodel. Â ≈ [0.2 ]
431444 @test evaloutput (internalmodel) ≈ [55.0 ]
432445 @test internalmodel. lastu0 ≈ [2.0 - 3.0 ]
446+ preparestate! (internalmodel, [55.0 ])
433447 x̂ = updatestate! (internalmodel, [3.0 ], [55.0 ])
434448 @test x̂ ≈ [3.0 ]
435449 newlinmodel = setop! (newlinmodel, uop= [3.0 ], yop= [55.0 ], xop= [8.0 ], fop= [8.0 ])
500514 h (x,_) = linmodel1. C* x
501515 nonlinmodel = setop! (NonLinModel (f, h, Ts, 2 , 2 , 2 , solver= nothing ), uop= [10 ,50 ], yop= [50 ,30 ])
502516 ukf1 = UnscentedKalmanFilter (nonlinmodel)
517+ preparestate! (ukf1, [50 , 30 ])
503518 @test updatestate! (ukf1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 ) atol= 1e-9
519+ preparestate! (ukf1, [50 , 30 ])
504520 @test updatestate! (ukf1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 ) atol= 1e-9
505521 @test ukf1. x̂0 ≈ zeros (4 ) atol= 1e-9
506522 @test evaloutput (ukf1) ≈ ukf1 () ≈ [50 , 30 ]
531547 @test ukf2 () ≈ [51 , 32 ] atol= 1e-3
532548 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
533549 ukf3 = UnscentedKalmanFilter (linmodel3)
550+ preparestate! (ukf3, [0 ])
534551 x̂ = updatestate! (ukf3, [0 ], [0 ])
535552 @test x̂ ≈ [0 , 0 ] atol= 1e-3
536553 @test isa (x̂, Vector{Float32})
542559 ukf1 = UnscentedKalmanFilter (linmodel, nint_ym= 0 )
543560 @test ukf1. Â ≈ [0.5 ]
544561 @test evaloutput (ukf1) ≈ [50.0 ]
562+ preparestate! (ukf1, [50.0 ])
545563 x̂ = updatestate! (ukf1, [2.0 ], [50.0 ])
546564 @test x̂ ≈ [3.0 ]
547565 newlinmodel = LinModel (ss (0.2 , 0.3 , 1.0 , 0 , 10.0 ))
550568 @test ukf1. Â ≈ [0.2 ]
551569 @test evaloutput (ukf1) ≈ [55.0 ]
552570 @test ukf1. lastu0 ≈ [2.0 - 3.0 ]
571+ preparestate! (ukf1, [55.0 ])
553572 x̂ = updatestate! (ukf1, [3.0 ], [55.0 ])
554573 @test x̂ ≈ [3.0 ]
555574 newlinmodel = setop! (newlinmodel, uop= [3.0 ], yop= [55.0 ], xop= [8.0 ], fop= [8.0 ])
627646 h (x,_) = linmodel1. C* x
628647 nonlinmodel = setop! (NonLinModel (f, h, Ts, 2 , 2 , 2 , solver= nothing ), uop= [10 ,50 ], yop= [50 ,30 ])
629648 ekf1 = ExtendedKalmanFilter (nonlinmodel)
649+ preparestate! (ekf1, [50 , 30 ])
630650 @test updatestate! (ekf1, [10 , 50 ], [50 , 30 ]) ≈ zeros (4 ) atol= 1e-9
651+ preparestate! (ekf1, [50 , 30 ])
631652 @test updatestate! (ekf1, [10 , 50 ], [50 , 30 ], Float64[]) ≈ zeros (4 ) atol= 1e-9
632653 @test ekf1. x̂0 ≈ zeros (4 ) atol= 1e-9
633654 @test evaloutput (ekf1) ≈ ekf1 () ≈ [50 , 30 ]
658679 @test ekf2 () ≈ [51 , 32 ] atol= 1e-3
659680 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
660681 ekf3 = ExtendedKalmanFilter (linmodel3)
682+ preparestate! (ekf3, [0 ])
661683 x̂ = updatestate! (ekf3, [0 ], [0 ])
662684 @test x̂ ≈ [0 , 0 ]
663685 @test isa (x̂, Vector{Float32})
669691 ekf1 = ExtendedKalmanFilter (linmodel, nint_ym= 0 )
670692 @test ekf1. Â ≈ [0.5 ]
671693 @test evaloutput (ekf1) ≈ [50.0 ]
694+ preparestate! (ekf1, [50.0 ])
672695 x̂ = updatestate! (ekf1, [2.0 ], [50.0 ])
673696 @test x̂ ≈ [3.0 ]
674697 newlinmodel = LinModel (ss (0.2 , 0.3 , 1.0 , 0 , 10.0 ))
677700 @test ekf1. Â ≈ [0.2 ]
678701 @test evaloutput (ekf1) ≈ [55.0 ]
679702 @test ekf1. lastu0 ≈ [2.0 - 3.0 ]
703+ preparestate! (ekf1, [55.0 ])
680704 x̂ = updatestate! (ekf1, [3.0 ], [55.0 ])
681705 @test x̂ ≈ [3.0 ]
682706 newlinmodel = setop! (newlinmodel, uop= [3.0 ], yop= [55.0 ], xop= [8.0 ], fop= [8.0 ])
782806 h (x,d) = linmodel1. C* x + linmodel1. Dd* d
783807 nonlinmodel = setop! (NonLinModel (f, h, Ts, 2 , 4 , 2 , 1 , solver= nothing ), uop= [10 ,50 ], yop= [50 ,30 ], dop= [5 ])
784808 mhe1 = MovingHorizonEstimator (nonlinmodel, He= 2 )
809+ preparestate! (mhe1, [50 , 30 ], [5 ])
785810 x̂ = updatestate! (mhe1, [10 , 50 ], [50 , 30 ], [5 ])
786811 @test x̂ ≈ zeros (6 ) atol= 1e-9
787812 @test mhe1. x̂0 ≈ zeros (6 ) atol= 1e-9
824849 @test mhe2 ([5 ]) ≈ [51 , 32 ] atol= 1e-3
825850 linmodel3 = LinModel {Float32} (0.5 * ones (1 ,1 ), ones (1 ,1 ), ones (1 ,1 ), zeros (1 ,0 ), zeros (1 ,0 ), 1.0 )
826851 mhe3 = MovingHorizonEstimator (linmodel3, He= 1 )
852+ preparestate! (mhe3, [0 ])
827853 x̂ = updatestate! (mhe3, [0 ], [0 ])
828854 @test x̂ ≈ [0 , 0 ] atol= 1e-3
829855 @test isa (x̂, Vector{Float32})
840866 optim = Model (Ipopt. Optimizer)
841867 covestim = ExtendedKalmanFilter (nonlinmodel, 1 : 2 , 0 , 0 , Q̂, Q̂, R̂)
842868 mhe5 = MovingHorizonEstimator (nonlinmodel, 1 , 1 : 2 , 0 , 0 , Q̂, Q̂, R̂, Inf ; optim, covestim)
869+ preparestate! (mhe5, [50 , 30 ], [5 ])
843870 x̂ = updatestate! (mhe5, [10 , 50 ], [50 , 30 ], [5 ])
844871 @test x̂ ≈ zeros (4 ) atol= 1e-9
845872 @test mhe5. x̂0 ≈ zeros (4 ) atol= 1e-9
906933 @test_throws ArgumentError setconstraint! (mhe2, c_v̂min= [- 1 ])
907934 @test_throws ArgumentError setconstraint! (mhe2, c_v̂max= [+ 1 ])
908935
936+ preparestate! (mhe1, [50 , 30 ])
909937 updatestate! (mhe1, [10 , 50 ], [50 , 30 ])
910938 @test_throws ErrorException setconstraint! (mhe1, x̂min= [- Inf ,- Inf ])
911939 @test_throws ErrorException setconstraint! (mhe1, x̂max= [+ Inf ,+ Inf ])
@@ -938,10 +966,12 @@ end
938966 setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
939967
940968 setconstraint! (mhe, x̂min= [1 ,1 ], x̂max= [100 ,100 ])
969+ preparestate! (mhe, [50 , 30 ])
941970 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
942971 @test x̂ ≈ [1 , 1 ] atol= 5e-2
943972
944973 setconstraint! (mhe, x̂min= [- 100 ,- 100 ], x̂max= [- 1 ,- 1 ])
974+ preparestate! (mhe, [50 , 30 ])
945975 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
946976 @test x̂ ≈ [- 1 , - 1 ] atol= 5e-2
947977
@@ -950,10 +980,12 @@ end
950980 setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
951981
952982 setconstraint! (mhe, ŵmin= [1 ,1 ], ŵmax= [100 ,100 ])
983+ preparestate! (mhe, [50 , 30 ])
953984 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
954985 @test mhe. Ŵ ≈ [1 ,1 ] atol= 5e-2
955986
956987 setconstraint! (mhe, ŵmin= [- 100 ,- 100 ], ŵmax= [- 1 ,- 1 ])
988+ preparestate! (mhe, [50 , 30 ])
957989 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
958990 @test mhe. Ŵ ≈ [- 1 ,- 1 ] atol= 5e-2
959991
@@ -962,11 +994,13 @@ end
962994 setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
963995
964996 setconstraint! (mhe, v̂min= [1 ,1 ], v̂max= [100 ,100 ])
997+ preparestate! (mhe, [50 , 30 ])
965998 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
966999 info = getinfo (mhe)
9671000 @test info[:V̂ ] ≈ [1 ,1 ] atol= 5e-2
9681001
9691002 setconstraint! (mhe, v̂min= [- 100 ,- 100 ], v̂max= [- 1 ,- 1 ])
1003+ preparestate! (mhe, [50 , 30 ])
9701004 x̂ = updatestate! (mhe, [10 , 50 ], [50 , 30 ])
9711005 info = getinfo (mhe)
9721006 @test info[:V̂ ] ≈ [- 1 ,- 1 ] atol= 5e-2
@@ -981,10 +1015,12 @@ end
9811015 setconstraint! (mhe2, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
9821016
9831017 setconstraint! (mhe2, x̂min= [1 ,1 ], x̂max= [100 ,100 ])
1018+ preparestate! (mhe2, [50 , 30 ])
9841019 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
9851020 @test x̂ ≈ [1 , 1 ] atol= 5e-2
9861021
9871022 setconstraint! (mhe2, x̂min= [- 100 ,- 100 ], x̂max= [- 1 ,- 1 ])
1023+ preparestate! (mhe2, [50 , 30 ])
9881024 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
9891025 @test x̂ ≈ [- 1 , - 1 ] atol= 5e-2
9901026
@@ -993,10 +1029,12 @@ end
9931029 setconstraint! (mhe2, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
9941030
9951031 setconstraint! (mhe2, ŵmin= [1 ,1 ], ŵmax= [100 ,100 ])
1032+ preparestate! (mhe2, [50 , 30 ])
9961033 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
9971034 @test mhe2. Ŵ ≈ [1 ,1 ] atol= 5e-2
9981035
9991036 setconstraint! (mhe2, ŵmin= [- 100 ,- 100 ], ŵmax= [- 1 ,- 1 ])
1037+ preparestate! (mhe2, [50 , 30 ])
10001038 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
10011039 @test mhe2. Ŵ ≈ [- 1 ,- 1 ] atol= 5e-2
10021040
@@ -1005,11 +1043,13 @@ end
10051043 setconstraint! (mhe2, v̂min= [- 100 ,- 100 ], v̂max= [100 ,100 ])
10061044
10071045 setconstraint! (mhe2, v̂min= [1 ,1 ], v̂max= [100 ,100 ])
1046+ preparestate! (mhe2, [50 , 30 ])
10081047 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
10091048 info = getinfo (mhe2)
10101049 @test info[:V̂ ] ≈ [1 ,1 ] atol= 5e-2
10111050
10121051 setconstraint! (mhe2, v̂min= [- 100 ,- 100 ], v̂max= [- 1 ,- 1 ])
1052+ preparestate! (mhe2, [50 , 30 ])
10131053 x̂ = updatestate! (mhe2, [10 , 50 ], [50 , 30 ])
10141054 info = getinfo (mhe2)
10151055 @test info[:V̂ ] ≈ [- 1 ,- 1 ] atol= 5e-2
@@ -1041,7 +1081,9 @@ end
10411081 for i in 1 : 6
10421082 X̂0_mhe[:,i] = mhe. x̂0
10431083 X̂0_ukf[:,i] = ukf. x̂0
1084+ preparestate! (mhe, [50 , 31 ], [25 ])
10441085 updatestate! (mhe, [11 , 50 ], [50 , 31 ], [25 ])
1086+ preparestate! (ukf, [50 , 31 ], [25 ])
10451087 updatestate! (ukf, [11 , 50 ], [50 , 31 ], [25 ])
10461088 end
10471089 @test X̂0_mhe ≈ X̂0_ukf atol= 1e-3
@@ -1054,6 +1096,7 @@ end
10541096 setconstraint! (mhe, x̂min= [- 1000 ], x̂max= [1000 ])
10551097 @test mhe. Â ≈ [0.5 ]
10561098 @test evaloutput (mhe) ≈ [50.0 ]
1099+ preparestate! (mhe, [50.0 ])
10571100 x̂ = updatestate! (mhe, [2.0 ], [50.0 ])
10581101 @test x̂ ≈ [3.0 ]
10591102 newlinmodel = LinModel (ss (0.2 , 0.3 , 1.0 , 0 , 10.0 ))
@@ -1064,6 +1107,7 @@ end
10641107 @test mhe. lastu0 ≈ [2.0 - 3.0 ]
10651108 @test mhe. U0 ≈ [2.0 - 3.0 ]
10661109 @test mhe. Y0m ≈ [50.0 - 55.0 ]
1110+ preparestate! (mhe, [55.0 ])
10671111 x̂ = updatestate! (mhe, [3.0 ], [55.0 ])
10681112 @test x̂ ≈ [3.0 ]
10691113 newlinmodel = setop! (newlinmodel, uop= [3.0 ], yop= [55.0 ], xop= [8.0 ], fop= [8.0 ])
0 commit comments