Skip to content

Commit 9ecb633

Browse files
committed
tests: add preparestate! when necessary
1 parent ed2dd51 commit 9ecb633

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

test/test_predictive_control.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,10 @@ end
127127
setstate!(mpc1, [1,2,3,4])
128128
@test mpc1.estim.x̂0 [1,2,3,4]
129129
setstate!(mpc1, [0,0,0,0])
130+
preparestate!(mpc1, mpc1.estim())
130131
updatestate!(mpc1, mpc1.estim.model.uop, mpc1.estim())
131132
@test mpc1.estim.x̂0 [0,0,0,0]
133+
preparestate!(mpc1, mpc1.estim())
132134
@test_throws ArgumentError updatestate!(mpc1, [0,0])
133135
end
134136

@@ -246,6 +248,7 @@ end
246248
setstate!(model, [1,1])
247249
for i=1:20
248250
y = model()
251+
preparestate!(mpc, y)
249252
u = moveinput!(mpc, [0, 0])
250253
X_mpc[:,i] = model.x0
251254
updatestate!(mpc, u, y)
@@ -398,8 +401,10 @@ end
398401
setstate!(mpc1, [1,2,3,4])
399402
@test mpc1.estim.x̂0 [1,2,3,4]
400403
setstate!(mpc1, [0,0,0,0])
404+
preparestate!(mpc1, mpc1.estim())
401405
updatestate!(mpc1, mpc1.estim.model.uop, mpc1.estim())
402406
@test mpc1.estim.x̂0 [0,0,0,0]
407+
preparestate!(mpc1, mpc1.estim())
403408
@test_throws ArgumentError updatestate!(mpc1, [0,0])
404409
end
405410

@@ -596,6 +601,7 @@ end
596601
setstate!(nmpc1, [1,2,3,4])
597602
@test nmpc1.estim.x̂0 [1,2,3,4]
598603
setstate!(nmpc1, [0,0,0,0])
604+
preparestate!(nmpc1, nmpc1.estim())
599605
updatestate!(nmpc1, nmpc1.estim.model.uop, nmpc1.estim())
600606
@test nmpc1.estim.x̂0 [0,0,0,0] atol=1e-6
601607
end

test/test_state_estim.jl

Lines changed: 45 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,14 +67,16 @@ end
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
@@ -103,6 +105,7 @@ end
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
= updatestate!(skalmanfilter3, [0], [0])
107110
@test [0, 0]
108111
@test isa(x̂, Vector{Float32})
@@ -170,7 +173,9 @@ end
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]
@@ -201,6 +206,7 @@ end
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
= updatestate!(kalmanfilter3, [0], [0])
205211
@test [0, 0]
206212
@test isa(x̂, Vector{Float32})
@@ -213,6 +219,7 @@ end
213219
kalmanfilter = KalmanFilter(linmodel, nint_ym=0)
214220
@test kalmanfilter. [0.5]
215221
@test evaloutput(kalmanfilter) [50.0]
222+
preparestate!(kalmanfilter, [50.0])
216223
= updatestate!(kalmanfilter, [2.0], [50.0])
217224
@test [3.0]
218225
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
@@ -221,6 +228,7 @@ end
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
= updatestate!(kalmanfilter, [3.0], [55.0])
225233
@test [3.0]
226234
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
@@ -274,7 +282,9 @@ end
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]
@@ -305,6 +315,7 @@ end
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
= updatestate!(lo3, [0], [0])
309320
@test [0, 0]
310321
@test isa(x̂, Vector{Float32})
@@ -411,6 +422,7 @@ end
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
= updatestate!(internalmodel3, [0], [0])
415427
@test [0]
416428
@test isa(x̂, Vector{Float32})
@@ -422,6 +434,7 @@ end
422434
internalmodel = InternalModel(linmodel)
423435
@test internalmodel. [0.5]
424436
@test evaloutput(internalmodel) [50.0]
437+
preparestate!(internalmodel, [50.0])
425438
= updatestate!(internalmodel, [2.0], [50.0])
426439
@test [3.0]
427440
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
@@ -430,6 +443,7 @@ end
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
= updatestate!(internalmodel, [3.0], [55.0])
434448
@test [3.0]
435449
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
@@ -500,7 +514,9 @@ end
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]
@@ -531,6 +547,7 @@ end
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
= updatestate!(ukf3, [0], [0])
535552
@test [0, 0] atol=1e-3
536553
@test isa(x̂, Vector{Float32})
@@ -542,6 +559,7 @@ end
542559
ukf1 = UnscentedKalmanFilter(linmodel, nint_ym=0)
543560
@test ukf1. [0.5]
544561
@test evaloutput(ukf1) [50.0]
562+
preparestate!(ukf1, [50.0])
545563
= updatestate!(ukf1, [2.0], [50.0])
546564
@test [3.0]
547565
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
@@ -550,6 +568,7 @@ end
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
= updatestate!(ukf1, [3.0], [55.0])
554573
@test [3.0]
555574
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
@@ -627,7 +646,9 @@ end
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]
@@ -658,6 +679,7 @@ end
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
= updatestate!(ekf3, [0], [0])
662684
@test [0, 0]
663685
@test isa(x̂, Vector{Float32})
@@ -669,6 +691,7 @@ end
669691
ekf1 = ExtendedKalmanFilter(linmodel, nint_ym=0)
670692
@test ekf1. [0.5]
671693
@test evaloutput(ekf1) [50.0]
694+
preparestate!(ekf1, [50.0])
672695
= updatestate!(ekf1, [2.0], [50.0])
673696
@test [3.0]
674697
newlinmodel = LinModel(ss(0.2, 0.3, 1.0, 0, 10.0))
@@ -677,6 +700,7 @@ end
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
= updatestate!(ekf1, [3.0], [55.0])
681705
@test [3.0]
682706
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])
@@ -782,6 +806,7 @@ end
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
= updatestate!(mhe1, [10, 50], [50, 30], [5])
786811
@test zeros(6) atol=1e-9
787812
@test mhe1.x̂0 zeros(6) atol=1e-9
@@ -824,6 +849,7 @@ end
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
= updatestate!(mhe3, [0], [0])
828854
@test [0, 0] atol=1e-3
829855
@test isa(x̂, Vector{Float32})
@@ -840,6 +866,7 @@ end
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
= updatestate!(mhe5, [10, 50], [50, 30], [5])
844871
@test zeros(4) atol=1e-9
845872
@test mhe5.x̂0 zeros(4) atol=1e-9
@@ -906,6 +933,7 @@ end
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
= updatestate!(mhe, [10, 50], [50, 30])
942971
@test [1, 1] atol=5e-2
943972

944973
setconstraint!(mhe, x̂min=[-100,-100], x̂max=[-1,-1])
974+
preparestate!(mhe, [50, 30])
945975
= updatestate!(mhe, [10, 50], [50, 30])
946976
@test [-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
= 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
= 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
= 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
= 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
= updatestate!(mhe2, [10, 50], [50, 30])
9851020
@test [1, 1] atol=5e-2
9861021

9871022
setconstraint!(mhe2, x̂min=[-100,-100], x̂max=[-1,-1])
1023+
preparestate!(mhe2, [50, 30])
9881024
= updatestate!(mhe2, [10, 50], [50, 30])
9891025
@test [-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
= 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
= 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
= 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
= 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
= updatestate!(mhe, [2.0], [50.0])
10581101
@test [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
= updatestate!(mhe, [3.0], [55.0])
10681112
@test [3.0]
10691113
newlinmodel = setop!(newlinmodel, uop=[3.0], yop=[55.0], xop=[8.0], fop=[8.0])

0 commit comments

Comments
 (0)