Skip to content

Commit 58ddf24

Browse files
committed
added: call gc with NaN values at MHE construction
1 parent bed4316 commit 58ddf24

2 files changed

Lines changed: 21 additions & 6 deletions

File tree

src/controller/nonlinmpc.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@ function test_custom_function_mpc(NT, model::SimModel, JE, gc!, nc, Uop, Yop, Do
542542
gc = Vector{NT}(undef, nc)
543543
try
544544
gc!(gc, Ue, Ŷe, D̂e, p, ϵ)
545+
all(isfinite, gc) || error("the gc function returned non-finite values: gc = $gc")
545546
catch err
546547
@warn(
547548
"""

src/estimator/mhe/construct.jl

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ struct MovingHorizonEstimator{
184184
P̂arr_old = copy(cov.P̂_0)
185185
Nk = [0]
186186
corrected = [false]
187-
test_custom_function_mhe(NT, model, i_ym, He, gc!, nc, x̂op, p)
187+
test_custom_function_mhe(NT, model, i_ym, He, gc!, nc, x̂op, p, direct)
188188
buffer = StateEstimatorBuffer{NT}(nu, nx̂, nym, ny, nd, nk, He, nε)
189189
estim = new{NT, SM, KC, JM, GB, JB, HB, PT, GCfunc, CE}(
190190
model,
@@ -649,16 +649,18 @@ function get_mutating_gc_mhe(NT, gc)
649649
end
650650

651651
"""
652-
test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p) -> nothing
652+
test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p, direct) -> nothing
653653
654654
Test the custom functions `gc!` at the operating points.
655655
656656
This function is called at the end of `MovingHorizonEstimator` construction. It warns the
657657
user if the custom constraint `gc!` function crashes at `model` operating points. It
658-
will also verify the custom function work with the growing windows. It should ease
659-
troubleshooting of simple bugs e.g.: the user forgets to set the `nc` argument.
658+
will also verify the custom function work with the growing windows, and with the `NaN`
659+
values at the boundaries (see [`MovingHorizonEstimator`](@ref) Extended Help for details on
660+
the data windows). It should ease troubleshooting of simple bugs e.g.: the user forgets to
661+
set the `nc` argument.
660662
"""
661-
function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p)
663+
function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p, direct)
662664
nx̂, nŵ, nym = length(x̂op), length(x̂op), length(i_ym)
663665
nu, nd = model.nu, model.nd
664666
uop, dop, yop = model.uop, model.dop, model.yop
@@ -673,7 +675,17 @@ function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op,
673675
for i in 2:He+1
674676
X̂e, V̂e, Ŵe = X̂e_He[1:(i*nx̂)], V̂e_He[1:(i*nym)], Ŵe_He[1:(i*nŵ)]
675677
Ue, Yem, De = Ue_He[1:(i*nu)], Yem_He[1:(i*nym)], De_He[1:(i*nd)]
678+
if direct
679+
V̂e[1:nym] .= NaN
680+
else
681+
V̂e[end-nym+1:end] .= NaN
682+
Yem[end-nym+1:end] .= NaN
683+
De[end-nd+1:end] .= NaN
684+
end
685+
Ŵe[end-nŵ+1:end] .= NaN
686+
Ue[end-nu+1:end] .= NaN
676687
gc!(gc, X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ε)
688+
all(isfinite, gc) || error("the gc function returned non-finite values: gc = $gc")
677689
end
678690
catch err
679691
@warn(
@@ -682,7 +694,9 @@ function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op,
682694
fixed at x̂op=$x̂op, uop=$uop, yop=$yop, dop=$dop,
683695
P̄=I, x̄=0, p=$p, ϵ=0 failed with the following stacktrace.
684696
Did you forget to set the keyword argument p or nc?
685-
Did you handle the growing data windows in your function?
697+
Did you handle the growing data windows with the NaN values at the boundaries?
698+
See the Extended Help of MovingHorizonEstimator for details on the arguments and
699+
the data windows.
686700
""",
687701
exception=(err, catch_backtrace())
688702
)

0 commit comments

Comments
 (0)