Skip to content

Commit f8eee4a

Browse files
committed
test: more exact MHE vs KF tests
also decreases the tolerances since computation are more exact than before
1 parent 90506bf commit f8eee4a

File tree

1 file changed

+23
-11
lines changed

1 file changed

+23
-11
lines changed

test/2_test_state_estim.jl

Lines changed: 23 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1334,8 +1334,8 @@ end
13341334
@testitem "MovingHorizonEstimator v.s. Kalman filters" setup=[SetupMPCtests] begin
13351335
using .SetupMPCtests, ControlSystemsBase, LinearAlgebra
13361336
linmodel1 = setop!(LinModel(sys,Ts,i_d=[3]), uop=[10,50], yop=[50,30], dop=[20])
1337-
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=false)
13381337
kf = KalmanFilter(linmodel1, nint_ym=0, direct=false)
1338+
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=false)
13391339
X̂_mhe = zeros(4, 6)
13401340
X̂_kf = zeros(4, 6)
13411341
for i in 1:6
@@ -1347,28 +1347,33 @@ end
13471347
updatestate!(mhe, [11, 50], y, [25])
13481348
updatestate!(kf, [11, 50], y, [25])
13491349
end
1350-
@test X̂_mhe X̂_kf atol=1e-3 rtol=1e-3
1351-
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=true)
1350+
@test X̂_mhe X̂_kf atol=1e-6 rtol=1e-6
13521351
kf = KalmanFilter(linmodel1, nint_ym=0, direct=true)
1352+
# recuperate P̂(-1|-1) exact value using the Kalman filter:
1353+
preparestate!(kf, [50, 30], [20])
1354+
σP̂ = sqrt.(diag(kf.P̂))
1355+
mhe = MovingHorizonEstimator(linmodel1, He=3, nint_ym=0, direct=true, σP_0=σP̂)
1356+
updatestate!(kf, [10, 50], [50, 30], [20])
13531357
X̂_mhe = zeros(4, 6)
13541358
X̂_kf = zeros(4, 6)
13551359
for i in 1:6
1356-
y = [50,31] + randn(2)
1360+
y = [50,31] #+ randn(2)
13571361
x̂_mhe = preparestate!(mhe, y, [25])
13581362
x̂_kf = preparestate!(kf, y, [25])
13591363
X̂_mhe[:,i] = x̂_mhe
13601364
X̂_kf[:,i] = x̂_kf
13611365
updatestate!(mhe, [11, 50], y, [25])
13621366
updatestate!(kf, [11, 50], y, [25])
13631367
end
1364-
@test X̂_mhe X̂_kf atol=1e-3 rtol=1e-3
1368+
@test X̂_mhe X̂_kf atol=1e-6 rtol=1e-6
1369+
13651370
f = (x,u,d,model) -> model.A*x + model.Bu*u + model.Bd*d
13661371
h = (x,d,model) -> model.C*x + model.Dd*d
13671372
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, p=linmodel1, solver=nothing)
13681373
nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30], dop=[20])
1369-
mhe = MovingHorizonEstimator(nonlinmodel, He=5, nint_ym=0, direct=false)
13701374
ukf = UnscentedKalmanFilter(nonlinmodel, nint_ym=0, direct=false)
13711375
ekf = ExtendedKalmanFilter(nonlinmodel, nint_ym=0, direct=false)
1376+
mhe = MovingHorizonEstimator(nonlinmodel, He=5, nint_ym=0, direct=false)
13721377
X̂_mhe = zeros(4, 6)
13731378
X̂_ukf = zeros(4, 6)
13741379
X̂_ekf = zeros(4, 6)
@@ -1384,11 +1389,18 @@ end
13841389
updatestate!(ukf, [11, 50], y, [25])
13851390
updatestate!(ekf, [11, 50], y, [25])
13861391
end
1387-
@test X̂_mhe X̂_ukf atol=1e-3 rtol=1e-3
1388-
@test X̂_mhe X̂_ekf atol=1e-3 rtol=1e-3
1389-
mhe = MovingHorizonEstimator(nonlinmodel, He=5, nint_ym=0, direct=true)
1392+
@test X̂_mhe X̂_ukf atol=1e-6 rtol=1e-6
1393+
@test X̂_mhe X̂_ekf atol=1e-6 rtol=1e-6
1394+
13901395
ukf = UnscentedKalmanFilter(nonlinmodel, nint_ym=0, direct=true)
13911396
ekf = ExtendedKalmanFilter(nonlinmodel, nint_ym=0, direct=true)
1397+
# recuperate P̂(-1|-1) exact value using the Unscented Kalman filter:
1398+
preparestate!(ukf, [50, 30], [20])
1399+
preparestate!(ekf, [50, 30], [20])
1400+
σP̂ = sqrt.(diag(ukf.P̂))
1401+
mhe = MovingHorizonEstimator(nonlinmodel, He=5, nint_ym=0, direct=true, σP_0=σP̂)
1402+
updatestate!(ukf, [10, 50], [50, 30], [20])
1403+
updatestate!(ekf, [10, 50], [50, 30], [20])
13921404
X̂_mhe = zeros(4, 6)
13931405
X̂_ukf = zeros(4, 6)
13941406
X̂_ekf = zeros(4, 6)
@@ -1404,8 +1416,8 @@ end
14041416
updatestate!(ukf, [11, 50], y, [25])
14051417
updatestate!(ekf, [11, 50], y, [25])
14061418
end
1407-
@test X̂_mhe X̂_ukf atol=1e-3 rtol=1e-3
1408-
@test X̂_mhe X̂_ekf atol=1e-3 rtol=1e-3
1419+
@test X̂_mhe X̂_ukf atol=1e-6 rtol=1e-6
1420+
@test X̂_mhe X̂_ekf atol=1e-6 rtol=1e-6
14091421
end
14101422

14111423
@testitem "MovingHorizonEstimator LinModel v.s. NonLinModel" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)