From e3bf80c2d125186f2cc6e31f6d45bf24dd279c40 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Sun, 29 Nov 2026 15:19:06 -0500 Subject: [PATCH 1/7] =?UTF-8?q?added:=20additional=20`A=CC=82e`=20arg=20in?= =?UTF-8?q?g=20MHE=20`gc`=20function?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/estimator/mhe/construct.jl | 55 +++++++++++++++++----------------- 1 file changed, 28 insertions(+), 27 deletions(-) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 3cbc11978..33b06d039 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -282,7 +282,7 @@ however, since it minimizes the following objective function at each discrete ti ``` subject to [`setconstraint!`](@ref) bounds and the custom nonlinear inequality constraints: ```math -\mathbf{g_c}(\mathbf{X̂_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε) ≤ \mathbf{0} +\mathbf{g_c}(\mathbf{X̂_e, Â_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε) ≤ \mathbf{0} ``` and in which the arrival costs are evaluated from the states estimated at time ``k-N_k``: ```math @@ -309,13 +309,13 @@ The chosen [`TranscriptionMethod`](@ref) determines the content of the decision and ``\mathbf{Ŵ}``). The vectors ``\mathbf{Ŵ}`` and ``\mathbf{V̂}`` respectively encompass the estimated process noises ``\mathbf{ŵ}(k-j+p)`` and sensor noises ``\mathbf{v̂}(k-j+1)`` from ``j=N_k`` to ``1``. The arguments of ``\mathbf{g_c}`` include the extended vectors of -the estimated states ``\mathbf{X̂_e}``, estimated sensor noises ``\mathbf{V̂_e}``, estimated -process noises ``\mathbf{Ŵ_e}``, manipulated inputs ``\mathbf{U_e}``, measured outputs -``\mathbf{Y_e^m}`` and measured disturbances ``\mathbf{D_e}``. The Extended Help details all -these vectors, the slack variable ``ε`` and the estimation of the covariance at arrival -``\mathbf{P̂}_{k-N_k}(k-N_k+p)``. If the keyword argument `direct=true` (default value), the -constant ``p=0`` in the equations above, and the MHE is in the current form. Else ``p=1``, -leading to the prediction form. +the estimated states ``\mathbf{X̂_e}``, estimated algebraic variables ``\mathbf{Â_e}``, +estimated sensor noises ``\mathbf{V̂_e}``, estimated process noises ``\mathbf{Ŵ_e}``, +manipulated inputs ``\mathbf{U_e}``, measured outputs ``\mathbf{Y_e^m}`` and measured +disturbances ``\mathbf{D_e}``. The Extended Help details all these vectors, the slack +variable ``ε`` and the estimation of the covariance at arrival ``\mathbf{P̂}_{k-N_k}(k-N_k+p)``. +If the keyword argument `direct=true` (default value), the constant ``p=0`` in the equations +above, and the MHE is in the current form. Else ``p=1``, leading to the prediction form. See [`UnscentedKalmanFilter`](@ref) for details on the augmented process model and ``\mathbf{R̂}, \mathbf{Q̂}`` covariances. This estimator allocates a fair amount of memory @@ -352,9 +352,9 @@ at each time step for the optimization. - `σPint_ym_0=fill(1,sum(nint_ym))` or *`sigmaPint_ym_0`* : same than `σP_0` but for the unmeasured disturbances at measured outputs ``\mathbf{P_{int_{ym}}}(0)`` (composed of integrators). - `Cwt=Inf` : slack variable weight ``C``, default to `Inf` meaning hard constraints only. -- `gc=(_,_,_,_,_,_,_,_,_,_,_)->nothing` or `gc!` : custom nonlinear inequality constraint function - ``\mathbf{g_c}(\mathbf{X̂_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε)``, mutating or not - (details in Extended Help). +- `gc=(_,_,_,_,_,_,_,_,_,_,_,_)->nothing` or `gc!` : custom nonlinear inequality constraint function + ``\mathbf{g_c}(\mathbf{X̂_e, Â_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε)``, mutating or + not (details in Extended Help). - `nc=0` : number of custom nonlinear inequality constraints. - `p=model.p` : ``\mathbf{g_c}`` functions parameter ``\mathbf{p}`` (any type). - `transcription=default_transcription(model)` : a [`TranscriptionMethod`](@ref) object, @@ -463,6 +463,7 @@ MovingHorizonEstimator estimator with a sample time Ts = 5.0 s: | ARGUMENT | SIZE | FIRST SAMPLE | LAST SAMPLE | MISSING SAMPLES | | :--------------- | :-------------- | :-------------- | :-------------- | :----------------- | | ``\mathbf{X̂_e}`` | `((Nk+1)*nx̂,)` | ``k - N_k + p`` | ``k + p`` | — | + | ``\mathbf{Â_e}`` | `((Nk+1)*na,)` | ``k - N_k + p`` | ``k + p`` | — | | ``\mathbf{V̂_e}`` | `((Nk+1)*nym,)` | ``k - N_k + p`` | ``k + p`` | ``k - N_k, k + 1`` | | ``\mathbf{Ŵ_e}`` | `((Nk+1)*nx̂,)` | ``k - N_k + p`` | ``k + p`` | ``k + p`` | | ``\mathbf{U_e}`` | `((Nk+1)*nu,)` | ``k - N_k + p`` | ``k + p`` | ``k + p`` | @@ -474,13 +475,13 @@ MovingHorizonEstimator estimator with a sample time Ts = 5.0 s: | ``ε`` | `()` | — | — | — | If `LHS` represents the result of the left-hand side in the inequality - ``\mathbf{g_c}(\mathbf{X̂_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε) ≤ \mathbf{0}``, + ``\mathbf{g_c}(\mathbf{X̂_e, Â_e, V̂_e, Ŵ_e, U_e, Y_e^m, D_e, P̄, x̄, p}, ε) ≤ \mathbf{0}``, the function `gc` can be implemented in two possible ways: - 1. **Non-mutating function** (out-of-place): define it as `gc(X̂e, V̂e, Ŵe, Ue, Yem, De, + 1. **Non-mutating function** (out-of-place): define it as `gc(X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ε) -> LHS`. This syntax is simple and intuitive but it allocates more memory. - 2. **Mutating function** (in-place): define it as `gc!(LHS, X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, - x̄, p, ε) -> nothing`. This syntax reduces the allocations and potentially the + 2. **Mutating function** (in-place): define it as `gc!(LHS, X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, + P̄, x̄, p, ε) -> nothing`. This syntax reduces the allocations and potentially the computational burden as well. The keyword argument `nc` is the number of elements in `LHS`, and `gc!`, an alias for @@ -490,7 +491,7 @@ MovingHorizonEstimator estimator with a sample time Ts = 5.0 s: on the state estimate at the beginning of the window ``k-N_k+p``, that is, in the past. It is not the same as the current estimate covariance ``\mathbf{P̂}_k(k)``, a value not computed by the MHE (contrarily to e.g. the [`KalmanFilter`](@ref)). Three keyword - arguments specify its initial value with ``\mathbf{P̂_i} = \mathrm{diag}\{ \mathbf{P}(0), + arguments specify its initial value with ``\mathbf{P̂_i} = \mathrm{diag}\{ \mathbf{P}(0), \mathbf{P_{int_{u}}}(0), \mathbf{P_{int_{ym}}}(0) \}``. The initial state estimate ``\mathbf{x̂_i}`` can be manually specified with [`setstate!`](@ref), or automatically with [`initstate!`](@ref) for [`LinModel`](@ref). Note the MHE with ``p=0`` is slightly @@ -566,7 +567,7 @@ function MovingHorizonEstimator( sigmaPint_ym_0 = fill(1, max(sum(nint_ym), 0)), sigmaQint_ym = fill(1, max(sum(nint_ym), 0)), Cwt::Real = Inf, - gc!::Function = (_,_,_,_,_,_,_,_,_,_,_) -> nothing, + gc!::Function = (_,_,_,_,_,_,_,_,_,_,_,_) -> nothing, gc ::Function = gc!, nc ::Int = 0, p = model.p, @@ -600,7 +601,7 @@ end @doc raw""" MovingHorizonEstimator( model, He, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, Cwt=Inf; - gc!=(_,_,_,_,_,_,_,_,_,_,_) -> nothing, + gc!=(_,_,_,_,_,_,_,_,_,_,_,_) -> nothing, gc=gc!, nc=0, transcription=default_transcription(model), @@ -620,7 +621,7 @@ argument. If `isnothing(P̂_0)`, its value will be fetch in `covestim.cov.P̂`. """ function MovingHorizonEstimator( model::SM, He, i_ym, nint_u, nint_ym, P̂_0, Q̂, R̂, Cwt=Inf; - gc!::Function = (_,_,_,_,_,_,_,_,_,_,_) -> nothing, + gc!::Function = (_,_,_,_,_,_,_,_,_,_,_,_) -> nothing, gc ::Function = gc!, nc = 0, p = model.p, @@ -707,8 +708,8 @@ function validate_gc_mhe(NT, gc) ismutating = hasmethod( gc, Tuple{ - # LHS, , X̂e , V̂e , Ŵe - Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, + # LHS, , X̂e , Âe , V̂e , Ŵe + Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, # Ue , Yem , De , P̄ , x̄ , p , ε Vector{NT}, Vector{NT}, Vector{NT}, AbstractMatrix{NT}, Vector{NT}, Any, NT } @@ -716,8 +717,8 @@ function validate_gc_mhe(NT, gc) isnonmutating = hasmethod( gc, Tuple{ - # X̂e , V̂e , Ŵe - Vector{NT}, Vector{NT}, Vector{NT}, + # X̂e , Âe , V̂e , Ŵe + Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, # Ue , Yem , De , P̄ , x̄ , p , ε Vector{NT}, Vector{NT}, Vector{NT}, AbstractMatrix{NT}, Vector{NT}, Any, NT } @@ -725,11 +726,11 @@ function validate_gc_mhe(NT, gc) if !(ismutating || isnonmutating) error( "the custom constraint function has no method with type signature "* - "gc(X̂e::Vector{$(NT)}, V̂e::Vector{$(NT)}, Ŵe::Vector{$(NT)}, "* + "gc(X̂e::Vector{$(NT)}, Âe::Vector{$(NT)}, V̂e::Vector{$(NT)}, Ŵe::Vector{$(NT)}, "* "Ue::Vector{$(NT)}, Yem::Vector{$(NT)}, De::Vector{$(NT)}, "* "P̄::Vector{$(NT)}, x̄::Vector{$(NT)}, p::Any, ϵ::$(NT)) "* "or mutating form gc!(LHS::Vector{$(NT)}, "* - "X̂e::Vector{$(NT)}, V̂e::Vector{$(NT)}, Ŵe::Vector{$(NT)}, "* + "X̂e::Vector{$(NT)}, Âe::Vector{$(NT)}, V̂e::Vector{$(NT)}, Ŵe::Vector{$(NT)}, "* "Ue::Vector{$(NT)}, Yem::Vector{$(NT)}, De::Vector{$(NT)}, "* "P̄::Vector{$(NT)}, x̄::Vector{$(NT)}, p::Any, ϵ::$(NT))" ) @@ -743,8 +744,8 @@ function get_mutating_gc_mhe(NT, gc) gc! = if ismutating_gc gc else - function gc!(LHS, X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ϵ) - LHS .= gc(X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ϵ) + function gc!(LHS, X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ϵ) + LHS .= gc(X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ϵ) return nothing end end From 120f49c48e0b2d25a382a5ac1827bde9d6336ea7 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Sun, 29 Nov 2026 15:32:13 -0500 Subject: [PATCH 2/7] =?UTF-8?q?changed:=20testing=20`gc`=20with=20initial?= =?UTF-8?q?=20guesses=20`xs=5F0`=20and=20`as=5F0`=20It=20was=20tested=20wi?= =?UTF-8?q?th=20the=20`x=CC=82op`=20operating=20points=20before=20this=20c?= =?UTF-8?q?hange.=20It=20does=20not=20make=20much=20sense=20for=20`NonLinM?= =?UTF-8?q?odel`=20and=20`NonLinModelDAE`,=20now=20that=20the=20initial=20?= =?UTF-8?q?guess=20parameters=20exist.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/estimator/mhe/construct.jl | 39 ++++++++++++++++++++-------------- 1 file changed, 23 insertions(+), 16 deletions(-) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 33b06d039..87e1c0dc2 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -229,7 +229,7 @@ struct MovingHorizonEstimator{ P̂arr_old = copy(cov.P̂_0) Nk = [0] prepared = [false] - test_custom_function_mhe(NT, model, i_ym, He, gc!, nc, x̂op, p, direct) + test_custom_function_mhe(NT, model, i_ym, He, gc!, nc, nx̂, p, direct) # force computation of derivatives for the first NLP iteration: force∇J, force∇g, force∇geq = [true], [true], [true] buffer = StateEstimatorBuffer{NT}( @@ -753,31 +753,38 @@ function get_mutating_gc_mhe(NT, gc) end """ - test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p, direct) -> nothing + test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, nx̂, p, direct) -> nothing Test the custom functions `gc!` at the operating points. This function is called at the end of `MovingHorizonEstimator` construction. It warns the -user if the custom constraint `gc!` function crashes at `model` operating points. It -will also verify the custom function work with the growing windows, and with the `NaN` -values at the boundaries (see [`MovingHorizonEstimator`](@ref) Extended Help for details on -the data windows). It should ease troubleshooting of simple bugs e.g.: the user forgets to -set the `nc` argument. +user if the custom constraint `gc!` function crashes at `model` initial guesses and +operating points. It will also verify the custom function work with the growing windows, and +with the `NaN` values at the boundaries (see [`MovingHorizonEstimator`](@ref) Extended Help +for details on the data windows). It should ease troubleshooting of simple bugs e.g.: the +user forgets to set the `nc` argument. """ -function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, p, direct) - nx̂, nŵ, nym = length(x̂op), length(x̂op), length(i_ym) - nu, nd = model.nu, model.nd +function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, nx̂, p, direct) + nŵ, nym = nx̂, length(i_ym) + nu, nd, na = model.nu, model.nd, get_na(model) + x̂s_0 = zeros(NT, nx̂) + x̂s_0[1:model.nx] .= model.xs_0 + âs_0 = get_as_0(model) uop, dop, yop = model.uop, model.dop, model.yop yopm = yop[i_ym] - X̂e_He, V̂e_He, Ŵe_He = repeat(x̂op, He+1), zeros(NT, (He+1)*nym), zeros(NT, (He+1)*nŵ) - Ue_He, Yem_He, De_He = repeat(uop, He+1), repeat(yopm, He+1), repeat(dop, He+1) + X̂e_He, Âe_He = repeat(x̂s_0, He+1), repeat(âs_0, He+1) + V̂e_He, Ŵe_He = zeros(NT, (He+1)*nym), zeros(NT, (He+1)*nŵ) + Ue_He, Yem_He, De_He = repeat(uop, He+1), repeat(yopm, He+1), repeat(dop, He+1) x̄ = zeros(NT, nx̂) P̄ = Hermitian(Matrix{NT}(I, nx̂, nx̂), :L) ε = zero(NT) gc = Vector{NT}(undef, nc) try for i in 2:He+1 - X̂e, V̂e, Ŵe = X̂e_He[1:(i*nx̂)], V̂e_He[1:(i*nym)], Ŵe_He[1:(i*nŵ)] + a = X̂e_He[1:(i*nx̂)] + b = Âe_He[1:(i*na)] + X̂e, Âe = X̂e_He[1:(i*nx̂)], Âe_He[1:(i*na)] + V̂e, Ŵe = V̂e_He[1:(i*nym)], Ŵe_He[1:(i*nŵ)] Ue, Yem, De = Ue_He[1:(i*nu)], Yem_He[1:(i*nym)], De_He[1:(i*nd)] if direct V̂e[1:nym] .= NaN @@ -788,14 +795,14 @@ function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, x̂op, end Ŵe[end-nŵ+1:end] .= NaN Ue[end-nu+1:end] .= NaN - gc!(gc, X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ε) + gc!(gc, X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, p, ε) all(isfinite, gc) || error("the gc function returned non-finite values: gc = $gc") end catch err @warn( """ - Calling the gc function with X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, ε arguments - fixed at x̂op=$x̂op, uop=$uop, yop=$yop, dop=$dop, + Calling the gc function with X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, ε arguments + fixed at xs_0=$xs_0, as_0=$âs_0, uop=$uop, yop=$yop, dop=$dop, P̄=I, x̄=0, p=$p, ϵ=0 failed with the following stacktrace. Did you forget to set the keyword argument p or nc? Did you handle the growing data windows with the NaN values at the boundaries? From a31608859fbefbf34b90932210e8c57db3625858 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Sun, 29 Nov 2026 16:16:28 -0500 Subject: [PATCH 3/7] test: update `gc` signature in MHE tests --- test/2_test_state_estim.jl | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/2_test_state_estim.jl b/test/2_test_state_estim.jl index 0d00fb0ef..ee74cb0ae 100644 --- a/test/2_test_state_estim.jl +++ b/test/2_test_state_estim.jl @@ -1013,7 +1013,7 @@ end mhe15 = MovingHorizonEstimator(linmodel; He=1, σP_0, covestim) @test mhe15.cov.invP̄ ≈ inv(covestim.cov.P̂) - function gcl(X̂e, _ , _ , _ , _ , _ , _ , _ , nc, ε) + function gcl(X̂e, _ , _ , _ , _ , _ , _ , _ , _ , nc, ε) gc = X̂e .- 100 .- ε return [gc; zeros(nc .- length(X̂e))] end @@ -1892,7 +1892,7 @@ end linmodel2 = LinModel(sys, Ts, i_u=[1,2], i_d=[3]) linmodel2 = setop!(linmodel2, uop=[10,50], yop=[50,30], dop=[5]) - function gclv!(LHS, X̂e, _, _, _, _, _, _, _, nx̂, _ ) + function gclv!(LHS, X̂e, _, _, _, _, _, _, _, _, nx̂, _ ) nc_Nk = length(X̂e) ÷ nx̂ LHS .= 0 # for when Nk < He, fill unused values with 0s for i in 1:nc_Nk @@ -1907,7 +1907,7 @@ end x̂ = updatestate!(mhe, [10, 50], [50, 30], [5]) @test x̂[1] ≈ 0.5 atol = 5e-2 - function gcln!(LHS, _, _, Ŵe, _, _, _, _,_, nx̂, _) + function gcln!(LHS, _, _, _, Ŵe, _, _, _, _,_, nx̂, _) LHS .= Ŵe[1:nx̂] return nothing end @@ -1980,7 +1980,7 @@ end h = (x,d,model) -> model.C*x + model.Dd*d nonlinmodel2 = NonLinModel(f, h, Ts, 2, 4, 2, 1, solver=nothing, p=linmodel2) nonlinmodel2 = setop!(nonlinmodel2, uop=[10,50], yop=[50,30], dop=[5]) - function gcnlv!(LHS, X̂e, _, _, _, _, _, _, _, nx̂, _) + function gcnlv!(LHS, X̂e, _, _, _, _, _, _, _, _, nx̂, _) nc_Nk = length(X̂e) ÷ nx̂ LHS .= 0 # for when Nk < He, fill unused values with 0s for i in 1:nc_Nk @@ -1995,7 +1995,7 @@ end x̂ = updatestate!(mhe2, [10, 50], [50, 30], [5]) @test x̂[1] ≈ 0.5 atol = 5e-2 - function gclnl!(LHS, _, _, Ŵe, _, _, _, _,_, nx̂, _) + function gclnl!(LHS, _, _, _, Ŵe, _, _, _, _,_, nx̂, _) LHS .= Ŵe[1:nx̂] return nothing end From 2700709b5f8e271bb18974304a2b9f2b5ef35351 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Sun, 29 Nov 2026 16:19:37 -0500 Subject: [PATCH 4/7] =?UTF-8?q?debug:=20`A=CC=82e`=20argument=20in=20`gc`?= =?UTF-8?q?=20now=20works?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/estimator/mhe/construct.jl | 53 +++++++++++++--------- src/estimator/mhe/execute.jl | 82 ++++++++++++++++++++-------------- 2 files changed, 81 insertions(+), 54 deletions(-) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 87e1c0dc2..feab07923 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -1563,23 +1563,26 @@ function get_nonlinobj_op( nc, neq, ng = con.nc, con.neq, length(con.i_g) nŴ, nV̂, nX̂, nÂ, ng, nZ̃ = He*nx̂, He*nym, He*nx̂, He*na, length(con.i_g), length(estim.Z̃) nK̄, nU, nŶ = He*nk̄, He*nu, He*nŷ - nŴe, nX̂e, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*nym + nŴe, nX̂e, nÂe, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*na, (He+1)*nym strict = Val(true) J::Vector{JNT} = zeros(JNT, 1) x̂0arr::Vector{JNT}, x̄::Vector{JNT} = zeros(JNT, nx̂), zeros(JNT, nx̂) â0arr::Vector{JNT}, Â0::Vector{JNT} = zeros(JNT, na), zeros(JNT, nÂ) Ŵ::Vector{JNT} = zeros(JNT, nŴ) V̂::Vector{JNT}, X̂0::Vector{JNT} = zeros(JNT, nV̂), zeros(JNT, nX̂) - Ŵe::Vector{JNT} = zeros(JNT, nŴe) - V̂e::Vector{JNT}, X̂e::Vector{JNT} = zeros(JNT, nV̂e), zeros(JNT, nX̂e) + Ŵe::Vector{JNT}, V̂e::Vector{JNT} = zeros(JNT, nŴe), zeros(JNT, nV̂e) + X̂e::Vector{JNT}, Âe::Vector{JNT} = zeros(JNT, nX̂e), zeros(JNT, nÂe) K̄::Vector{JNT} = zeros(JNT, nK̄) Û0::Vector{JNT}, Ŷ0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nŶ) Ûi::Vector{JNT} = zeros(JNT, nU) gc::Vector{JNT}, g::Vector{JNT} = zeros(JNT, nc), zeros(JNT, ng) geq::Vector{JNT} = zeros(JNT, neq) - function J!(Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq) + function J!( + Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq + ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return obj_nonlinprog(estim, model, x̄, V̂, Ŵ, Z̃) end @@ -1587,7 +1590,7 @@ function get_nonlinobj_op( J_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) @@ -1686,15 +1689,15 @@ function get_nonlincon_oracle( ngi = sum(con.i_g) nŴ, nV̂, nX̂, nÂ, nZ̃ = He*nx̂, He*nym, He*nx̂, He*na, length(estim.Z̃) nK̄, nU, nŶ = He*nk̄, He*nu, He*nŷ - nŴe, nX̂e, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*nym + nŴe, nX̂e, nÂe, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*na, (He+1)*nym strict = Val(true) myInf = convert(JNT, Inf) x̂0arr::Vector{JNT}, x̄::Vector{JNT} = zeros(JNT, nx̂), zeros(JNT, nx̂) â0arr::Vector{JNT}, Â0::Vector{JNT} = zeros(JNT, na), zeros(JNT, nÂ) Ŵ::Vector{JNT} = zeros(JNT, nŴ) V̂::Vector{JNT}, X̂0::Vector{JNT} = zeros(JNT, nV̂), zeros(JNT, nX̂) - Ŵe::Vector{JNT} = zeros(JNT, nŴe) - V̂e::Vector{JNT}, X̂e::Vector{JNT} = zeros(JNT, nV̂e), zeros(JNT, nX̂e) + Ŵe::Vector{JNT}, V̂e::Vector{JNT} = zeros(JNT, nŴe), zeros(JNT, nV̂e) + X̂e::Vector{JNT}, Âe::Vector{JNT} = zeros(JNT, nX̂e), zeros(JNT, nÂe) K̄::Vector{JNT} = zeros(JNT, nK̄) Û0::Vector{JNT}, Ŷ0::Vector{JNT} = zeros(JNT, nU), zeros(JNT, nŶ) Ûi::Vector{JNT} = zeros(JNT, nU) @@ -1703,18 +1706,22 @@ function get_nonlincon_oracle( gi::Vector{JNT} = zeros(JNT, ngi) λi::Vector{JNT}, λeq::Vector{JNT} = rand(JNT, ngi), rand(JNT, neq) # -------------- inequality constraint: nonlinear oracle ------------------------- - function gi!(gi, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq) + function gi!( + gi, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq + ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) gi .= @views g[i_g] return nothing end function ℓ_gi( - Z̃, λi, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, gi + Z̃, λi, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, gi ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) gi .= @views g[i_g] return dot(λi, gi) @@ -1723,7 +1730,7 @@ function get_nonlincon_oracle( ∇gi_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) @@ -1737,7 +1744,7 @@ function get_nonlincon_oracle( ∇²gi_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq), Cache(gi) @@ -1785,17 +1792,21 @@ function get_nonlincon_oracle( eval_hessian_lagrangian = isnothing(hess) ? nothing : ∇²gi_func! ) # ------------- equality constraints : nonlinear oracle ------------------------------ - function geq!(geq, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g) + function geq!( + geq, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g + ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return nothing end function ℓ_geq( - Z̃, λeq, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq + Z̃, λeq, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return dot(λeq, geq) end @@ -1803,7 +1814,7 @@ function get_nonlincon_oracle( ∇geq_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g) ) @@ -1816,7 +1827,7 @@ function get_nonlincon_oracle( ∇²geq_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) diff --git a/src/estimator/mhe/execute.jl b/src/estimator/mhe/execute.jl index b2ca8701a..88ea00a12 100644 --- a/src/estimator/mhe/execute.jl +++ b/src/estimator/mhe/execute.jl @@ -235,13 +235,13 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher ngi = sum(con.i_g) nV̂, nX̂, nÂ, nŴ = He*nym, He*nx̂, He*na, He*nx̂ nK̄, nU, nŶ = He*nk̄, He*nu, He*nŷ - nŴe, nX̂e, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*nym + nŴe, nX̂e, nÂe, nV̂e = (He+1)*nx̂, (He+1)*nx̂, (He+1)*na, (He+1)*nym x̂0arr, x̄ = zeros(NT, nx̂), zeros(NT, nx̂) â0arr, Â0 = zeros(NT, na), zeros(NT, nÂ) Ŵ = zeros(NT, nŴ) V̂, X̂0 = zeros(NT, nV̂), zeros(NT, nX̂) - Ŵe = zeros(NT, nŴe) - V̂e, X̂e = zeros(NT, nV̂e), zeros(NT, nX̂e) + Ŵe, V̂e = zeros(NT, nŴe), zeros(NT, nV̂e) + X̂e, Âe = zeros(NT, nX̂e), zeros(NT, nÂe) K̄ = zeros(NT, nK̄) Û0, Ŷ0 = zeros(NT, nU), zeros(NT, nŶ) Ûi = zeros(NT, nU) @@ -251,13 +251,14 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher J_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) - function J!(Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq) + function J!(Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return obj_nonlinprog(estim, model, x̄, V̂, Ŵ, Z̃) end @@ -274,13 +275,16 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher ∇g_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) - function gi!(gi, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq) + function gi!( + gi, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq + ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) gi .= @views g[i_g] return nothing @@ -306,16 +310,17 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher ∇²g_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq), Cache(gi) ) function ℓ_gi( - Z̃, λi, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, gi + Z̃, λi, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, gi ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) gi .= @views g[i_g] return dot(λi, gi) @@ -330,13 +335,16 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher ∇geq_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g) ) - function geq!(geq, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g) + function geq!( + geq, Z̃, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g + ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return nothing end @@ -361,15 +369,16 @@ function addinfo!(info, estim::MovingHorizonEstimator{NT}, model::SimModel) wher ∇²geq_cache = ( Cache(x̂0arr), Cache(â0arr), Cache(x̄), Cache(Ŵ), Cache(V̂), Cache(X̂0), Cache(Â0), - Cache(Ŵe), Cache(V̂e), Cache(X̂e), + Cache(Ŵe), Cache(V̂e), Cache(X̂e), Cache(Âe), Cache(Û0), Cache(Ûi), Cache(K̄), Cache(Ŷ0), Cache(gc), Cache(g), Cache(geq) ) function ℓ_geq( - Z̃, λeq, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq + Z̃, λeq, x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq ) update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim, Z̃ + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + estim, Z̃ ) return dot(λeq, geq) end @@ -911,7 +920,7 @@ end """ update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim::MovingHorizonEstimator, Z̃ ) -> nothing @@ -920,7 +929,7 @@ Update in-place the vectors for the predictions of `estim` estimator at decision The method mutates all the arguments before `estim` argument. """ function update_predictions!( - x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Û0, Ûi, K̄, Ŷ0, gc, g, geq, + x̂0arr, â0arr, x̄, Ŵ, V̂, X̂0, Â0, Ŵe, V̂e, X̂e, Âe, Û0, Ûi, K̄, Ŷ0, gc, g, geq, estim::MovingHorizonEstimator, Z̃ ) model, transcription = estim.model, estim.transcription @@ -931,9 +940,9 @@ function update_predictions!( V̂, X̂0, Â0 = predict_mhe!( V̂, X̂0, Â0, Û0, Ûi, K̄, Ŷ0, estim, model, transcription, x̂0arr, â0arr, Ŵ, Z̃ ) - Ŵe, V̂e, X̂e = extended_vectors!(Ŵe, V̂e, X̂e, estim, Ŵ, V̂, X̂0, x̂0arr) + Ŵe, V̂e, X̂e, Âe = extended_vectors!(Ŵe, V̂e, X̂e, Âe, estim, Ŵ, V̂, X̂0, Â0, x̂0arr, â0arr) ε = getslack(estim, Z̃) - gc = con_custom_mhe!(gc, estim, X̂e, V̂e, Ŵe, x̄, ε) + gc = con_custom_mhe!(gc, estim, X̂e, Âe, V̂e, Ŵe, x̄, ε) g = con_nonlinprog_mhe!(g, estim, model, transcription, X̂0, V̂, gc, ε) geq = con_nonlinprogeq_mhe!( geq, X̂0, Û0, Ûi, K̄, estim, model, transcription, x̂0arr, â0arr, Ŵ, Z̃ @@ -943,8 +952,8 @@ end """ extended_vectors!( - Ŵe, V̂e, X̂e, estim::MovingHorizonEstimator, Ŵ, V̂, X̂0, x̂0arr - ) -> Ŵe, V̂e, X̂e + Ŵe, V̂e, X̂e, Âe, estim::MovingHorizonEstimator, Ŵ, V̂, X̂0, Â0, x̂0arr, â0arr + ) -> Ŵe, V̂e, X̂e, Âe Compute the extended `Ŵe, V̂e` and `X̂e` vectors for NLP using the `Ŵ, V̂` and `X̂0` vectors. @@ -952,12 +961,17 @@ See [`MovingHorizonEstimator`](@ref) for the definition of the vectors, the exac steps of the samples in them and the missing values with `NaN`s. The method mutates all the arguments before `estim` argument. """ -function extended_vectors!(Ŵe, V̂e, X̂e, estim::MovingHorizonEstimator, Ŵ, V̂, X̂0, x̂0arr) - nym, nŵ, nx̂ = estim.nym, estim.nx̂, estim.nx̂ +function extended_vectors!( + Ŵe, V̂e, X̂e, Âe, + estim::MovingHorizonEstimator, Ŵ, V̂, X̂0, Â0, x̂0arr, â0arr +) + nym, nŵ, nx̂, na = estim.nym, estim.nx̂, estim.nx̂, get_na(estim.model) Ŵe[1:end-nŵ] .= Ŵ Ŵe[end-nŵ+1:end] .= NaN X̂e[1:nx̂] .= x̂0arr .+ estim.x̂op X̂e[nx̂+1:end] .= X̂0 .+ estim.X̂op + Âe[1:na] .= â0arr # no operating points for algebraic var. a + Âe[na+1:end] .= Â0 if estim.direct V̂e[1:nym] .= NaN V̂e[1+nym:end] .= V̂ @@ -965,30 +979,32 @@ function extended_vectors!(Ŵe, V̂e, X̂e, estim::MovingHorizonEstimator, Ŵ, V̂e[1:end-nym] .= V̂ V̂e[end-nym+1:end] .= NaN end - return Ŵe, V̂e, X̂e + return Ŵe, V̂e, X̂e, Âe end """ - con_custom_mhe!(gc, estim::MovingHorizonEstimator, X̂e, V̂e, Ŵe, x̄, ε) -> gc + con_custom_mhe!(gc, estim::MovingHorizonEstimator, X̂e, Âe, V̂e, Ŵe, x̄, ε) -> gc Evaluate the custom inequality constraint `gc` in-place for [`MovingHorizonEstimator`](@ref). """ -function con_custom_mhe!(gc, estim::MovingHorizonEstimator, X̂e, V̂e, Ŵe, x̄, ε) +function con_custom_mhe!(gc, estim::MovingHorizonEstimator, X̂e, Âe, V̂e, Ŵe, x̄, ε) if estim.con.nc > 0 P̄ = estim.P̂arr_old Nk = estim.Nk[] Ue, Yem, De = estim.Ue, estim.Yem, estim.De if Nk < estim.He # avoid views since allocations only when Nk < He and we want fast mul!: - nX̂e, nŴe, nYem = (Nk+1)*estim.nx̂, (Nk+1)*estim.nx̂, (Nk+1)*estim.nym - nUe, nDe = (Nk+1)*estim.model.nu, (Nk+1)*estim.model.nd + nX̂e, nÂe = (Nk+1)*estim.nx̂, (Nk+1)*get_na(estim.model) + nŴe, nYem = (Nk+1)*estim.nx̂, (Nk+1)*estim.nym + nUe, nDe = (Nk+1)*estim.model.nu, (Nk+1)*estim.model.nd Ue, Yem, De = estim.Ue[1:nUe], estim.Yem[1:nYem], estim.De[1:nDe] - X̂e, V̂e, Ŵe = X̂e[1:nX̂e], V̂e[1:nYem], Ŵe[1:nŴe] + X̂e, Âe = X̂e[1:nX̂e], Âe[1:nÂe] + V̂e, Ŵe = V̂e[1:nYem], Ŵe[1:nŴe] else Ue, Yem, De = estim.Ue, estim.Yem, estim.De end - estim.con.gc!(gc, X̂e, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, estim.p, ε) + estim.con.gc!(gc, X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, estim.p, ε) end return gc end From 3ccaa8f8025fb97fed6566275544de476a979cd6 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 30 Nov 2026 14:32:15 -0500 Subject: [PATCH 5/7] debug: correct interpolated variable in warning --- src/estimator/mhe/construct.jl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index feab07923..133f50d9e 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -802,7 +802,7 @@ function test_custom_function_mhe(NT, model::SimModel, i_ym, He, gc!, nc, nx̂, @warn( """ Calling the gc function with X̂e, Âe, V̂e, Ŵe, Ue, Yem, De, P̄, x̄, ε arguments - fixed at xs_0=$xs_0, as_0=$âs_0, uop=$uop, yop=$yop, dop=$dop, + fixed at x̂s_0=$x̂s_0, âs_0=$âs_0, uop=$uop, yop=$yop, dop=$dop, P̄=I, x̄=0, p=$p, ϵ=0 failed with the following stacktrace. Did you forget to set the keyword argument p or nc? Did you handle the growing data windows with the NaN values at the boundaries? From 5c0dcf201632a857928ba630234f8cf02e642bd8 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 30 Nov 2026 15:15:00 -0500 Subject: [PATCH 6/7] =?UTF-8?q?debug:=20correct=20`A=CC=82min`=20and=20`A?= =?UTF-8?q?=CC=82max`=20in=20`setconstraint!`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/estimator/mhe/construct.jl | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/estimator/mhe/construct.jl b/src/estimator/mhe/construct.jl index 133f50d9e..63b06e1bd 100644 --- a/src/estimator/mhe/construct.jl +++ b/src/estimator/mhe/construct.jl @@ -978,7 +978,7 @@ function setconstraint!( con.Âmin[i] = âmin[(i-1) % na + 1] end elseif !isnothing(Âmin) - size(X̂min) == (nÂe,) || throw(DimensionMismatch("Âmin size must be $((nÂe,))")) + size(Âmin) == (nÂe,) || throw(DimensionMismatch("Âmin size must be $((nÂe,))")) con.Âmin .= Âmin end if isnothing(Âmax) && !isnothing(âmax) @@ -987,8 +987,8 @@ function setconstraint!( con.Âmax[i] = âmax[(i-1) % na + 1] end elseif !isnothing(Âmax) - size(X̂max) == (nÂe,) || throw(DimensionMismatch("Âmax size must be $((nÂe,))")) - con.Âmax .= Âmin + size(Âmax) == (nÂe,) || throw(DimensionMismatch("Âmax size must be $((nÂe,))")) + con.Âmax .= Âmax end if isnothing(Ŵmin) && !isnothing(ŵmin) size(ŵmin) == (nŵ,) || throw(DimensionMismatch("ŵmin size must be $((nŵ,))")) From 362810a67ee2809f411c8065899aa3724630e696 Mon Sep 17 00:00:00 2001 From: franckgaga Date: Mon, 30 Nov 2026 15:25:57 -0500 Subject: [PATCH 7/7] =?UTF-8?q?test:=20simple=20custom=20NL=20tests=20on?= =?UTF-8?q?=20`A=CC=82e`=20with=20MHE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/2_test_state_estim.jl | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/2_test_state_estim.jl b/test/2_test_state_estim.jl index ee74cb0ae..adbe9b0ab 100644 --- a/test/2_test_state_estim.jl +++ b/test/2_test_state_estim.jl @@ -2058,6 +2058,19 @@ end info = getinfo(mhe3) @test info[:â] ≈ [-1.0] atol=5e-2 + gc_arr!(LHS, _, Âe, _, _, _, _, _, _, _, _, _) = (LHS[] = 3.5 - Âe[1]) + mhe4 = MovingHorizonEstimator(dae; He=1, nint_ym=0, gc=gc_arr!, nc=1, direct=false) + updatestate!(mhe4, [0.0], [0.0], [0.0]) + info = getinfo(mhe4) + @test info[:âarr] ≈ [3.5] atol=5e-2 + + gc_a!(LHS, _, Âe, _, _, _, _, _, _, _, _, _) = (LHS[] = 1.9 - Âe[2]) + direct = false + mhe5 = MovingHorizonEstimator(dae; He=1, nint_ym=0, gc=gc_a!, nc=1, direct=false) + updatestate!(mhe5, [0.0], [0.0], [0.0]) + info = getinfo(mhe5) + @test info[:â] ≈ [1.9] atol=5e-2 + end @testitem "MHE set model" setup=[SetupMPCtests] begin