Skip to content

Commit f7b774e

Browse files
committed
test: added a FiniteDiff test with MHE
1 parent dba8d43 commit f7b774e

1 file changed

Lines changed: 19 additions & 13 deletions

File tree

‎test/2_test_state_estim.jl‎

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1251,7 +1251,7 @@ end
12511251
h(x,d,model) = model.C*x + model.Dd*d
12521252
nonlinmodel = NonLinModel(f, h, Ts, 2, 4, 2, 1, solver=nothing, p=linmodel)
12531253
nonlinmodel = setop!(nonlinmodel, uop=[10,50], yop=[50,30], dop=[5])
1254-
#=
1254+
12551255
mhe1 = MovingHorizonEstimator(nonlinmodel, He=3)
12561256
JuMP.set_attribute(mhe1.optim, "tol", 1e-7)
12571257
preparestate!(mhe1, [50, 30], [5])
@@ -1327,7 +1327,7 @@ end
13271327
end
13281328
preparestate!(mhe2, [50, 30], [5])
13291329
@test mhe2([5]) ≈ [50, 30] atol=5e-3
1330-
=#
1330+
13311331
Q̂ = diagm([1/4, 1/4, 1/4, 1/4].^2)
13321332
R̂ = diagm([1, 1].^2)
13331333
optim = Model(Ipopt.Optimizer)
@@ -1341,29 +1341,35 @@ end
13411341
@test evaloutput(mhe5, [5]) ≈ mhe5([5]) ≈ [50, 30]
13421342
info = getinfo(mhe5)
13431343
@test info[:x̂] ≈ x̂ atol=1e-9
1344-
@test info[:Ŷ][end-1:end] ≈ [50, 30] atol=1e-9
1344+
@test info[:Ŷ][end-1:end] ≈ [50, 30] atol=1e-9
1345+
1346+
@test_nowarn ModelPredictiveControl.info2debugstr(info)
1347+
@test_throws ErrorException setstate!(mhe1, [1,2,3,4,5,6], diagm(.1:.1:.6))
13451348

13461349
# coverage of the branch with error termination status (with an infeasible problem):
1347-
mhe_infeas = setconstraint!(mhe5, v̂min=[1, 1], v̂max=[-1, -1])
1350+
mhe_infeas = MovingHorizonEstimator(nonlinmodel, He=1, Cwt=Inf)
1351+
mhe_infeas = setconstraint!(mhe_infeas, v̂min=[1, 1], v̂max=[-1, -1])
13481352
@test_logs(
13491353
(:error, "MHE terminated without solution: estimation in open-loop "*
13501354
"(more info in debug log)"),
13511355
preparestate!(mhe_infeas, [0, 0], [0])
13521356
)
13531357

1354-
@test_nowarn ModelPredictiveControl.info2debugstr(info)
1355-
@test_throws ErrorException setstate!(mhe1, [1,2,3,4,5,6], diagm(.1:.1:.6))
1356-
1357-
hessian = AutoFiniteDiff()
1358+
gradient, jacobian, hessian = AutoFiniteDiff(), AutoFiniteDiff(), AutoFiniteDiff()
13581359

1359-
mhe8 = MovingHorizonEstimator(nonlinmodel; He=2, hessian)
1360-
@test mhe8.hessian isa AutoFiniteDiff
1360+
mhe8 = MovingHorizonEstimator(nonlinmodel; He=2, gradient, jacobian, hessian)
1361+
@test mhe8.gradient isa AutoFiniteDiff
1362+
@test mhe8.jacobian isa AutoFiniteDiff
1363+
@test mhe8.hessian isa AutoFiniteDiff
1364+
x̂ = preparestate!(mhe8, [50, 30], [5])
1365+
@test x̂ ≈ zeros(6) atol=1e-6
1366+
updatestate!(mhe8, [10, 50], [50, 30], [5])
13611367
@test_logs(
13621368
(:warn, "NaN values in the MHE measurements ym: ignoring them in the objective"),
13631369
preparestate!(mhe8, [50, NaN], [5])
13641370
)
1365-
@test mhe8.x̂0 ≈ zeros(6) atol=1e-9
1366-
#=
1371+
@test mhe8.x̂0 ≈ zeros(6) atol=1e-6
1372+
13671373
function f!(ẋ, x, u, d, p)
13681374
ẋ[] = -p[] * (x[] - 0.2 * u[] - 0.2 * d[])
13691375
return nothing
@@ -1384,7 +1390,7 @@ end
13841390
@test mhe_foh([0.0]) ≈ [1.5]
13851391
info = getinfo(mhe_foh)
13861392
@test info[:x̂] ≈ x̂
1387-
=#
1393+
13881394
end
13891395

13901396
@testitem "MHE estim. & getinfo (NonLinModel, MS)" setup=[SetupMPCtests] begin

0 commit comments

Comments
 (0)