@@ -914,7 +914,7 @@ function linconstrainteq!(
914914 JuMP. delete (optim, optim[:linconstrainteq_temp ])
915915 JuMP. unregister (optim, :linconstrainteq_temp )
916916 end
917- if estim . Nk[] < estim. He
917+ if Nk < estim. He
918918 if haskey (optim, :linconstrainteq )
919919 JuMP. delete (optim, optim[:linconstrainteq ])
920920 JuMP. unregister (optim, :linconstrainteq )
@@ -1002,10 +1002,10 @@ end
10021002
10031003@doc raw """
10041004 set_warmstart_mhe!(
1005- estim::MovingHorizonEstimator, transcription::MultipleShooting , Z̃var
1005+ estim::MovingHorizonEstimator, transcription::TranscriptionMethod , Z̃var
10061006 ) -> Z̃s
10071007
1008- Do the same but based on a [`MultipleShooting `](@ref) transcription .
1008+ Do the same but for other transcription [`TranscriptionMethod `](@ref).
10091009
10101010If supported by `estim.optim`, it warm-starts the solver at:
10111011```math
@@ -1035,7 +1035,7 @@ last control period ``k-1``, expressed as a deviation from the operating point
10351035of vectors ``\mathbf{0_x̂}`` and ``\mathbf{0_ŵ}``.
10361036"""
10371037function set_warmstart_mhe! (
1038- estim:: MovingHorizonEstimator{NT} , transcription:: MultipleShooting , Z̃var
1038+ estim:: MovingHorizonEstimator{NT} , transcription:: TranscriptionMethod , Z̃var
10391039) where NT<: Real
10401040 model, buffer = estim. model, estim. buffer
10411041 nu, nk = model. nu, model. nk
@@ -1342,13 +1342,14 @@ end
13421342
13431343Nonlinear MHE equality constrains for [`NonLinModel`](@ref) and [`MultipleShooting`](@ref).
13441344
1345- The method mutates the `geq`, `X̂0`, `Û0` and `K` vectors in argument. The defects of the
1346- estimated states are computed with:
1345+ The method mutates the `geq`, `X̂0`, `Û0` and `K` vectors in argument. By introducing
1346+ the integer ``ℓ = k - N_k + p`` to shorten the notation, the defects of the estimated states
1347+ are computed with:
13471348```math
1348- \mathbf{ŝ}(k +j+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(k +j), \mathbf{u_0}(k +j), \mathbf{d_0}(k +j)\Big)
1349- - \mathbf{x̂_0}(k +j+1)
1349+ \mathbf{ŝ}(ℓ +j+1) = \mathbf{f̂}\Big(\mathbf{x̂_0}(ℓ +j), \mathbf{u_0}(ℓ +j), \mathbf{d_0}(ℓ +j)\Big)
1350+ + \mathbf{ŵ}(ℓ+j) - \mathbf{x̂_0}(ℓ +j+1)
13501351```
1351- for ``j = 0, 1, ... , H_p -1`` and in which the augmented state vectors ``\mathbf{x̂_0}`` are
1352+ for ``j = 0, 1, ... , N_k -1`` and in which the augmented state vectors ``\mathbf{x̂_0}`` are
13521353extracted from the decision variable `Z̃`. The function ``\mathbf{f̂}`` is defined at [`f̂!`](@ref).
13531354"""
13541355function con_nonlinprogeq_mhe! (
@@ -1385,5 +1386,85 @@ function con_nonlinprogeq_mhe!(
13851386 Nk < He && (geq[nx̂* Nk+ 1 : end ] .= 0 )
13861387 return geq
13871388end
1389+
1390+ @doc raw """
1391+ con_nonlinprogeq_mhe!(
1392+ geq, X̂0, Û0, K̇,
1393+ estim::MovingHorizonEstimator, model::NonLinModel, ::TrapezoidalCollocation,
1394+ x̂0arr, Ŵ, Z̃
1395+ ) -> geq
1396+
1397+ Nonlinear MHE equality constrains for [`NonLinModel`](@ref) and [`TrapezoidalCollocation`](@ref).
1398+
1399+ By introducing the integer ``ℓ = k - N_k + p`` to shorten the notation, the deterministic
1400+ state defects are computed with:
1401+ ```math
1402+ \mathbf{ŝ_d}(ℓ+j+1) = \mathbf{x̂_d}(ℓ+j) + 0.5 T_s [\mathbf{k̇}_1(ℓ+j) + \mathbf{k̇}_2(ℓ+j)]
1403+ + \mathbf{ŵ_d}(ℓ+j) - \mathbf{x̂_d}(ℓ+j+1)
1404+ ```
1405+ for ``j = 0, 1, ... , N_k-1``, and in which ``\mathbf{x̂_d}`` and ``\mathbf{ŵ_d}`` are the
1406+ deterministic state and process noise estimates, respectively, extracted from the decision
1407+ variable `Z̃`. The ``\mathbf{k̇}`` coefficients are evaluated from the continuous-time
1408+ function `model.f!` and:
1409+ ```math
1410+ \begin{aligned}
1411+ \mathbf{k̇}_1(ℓ+j) &= \mathbf{f}\Big(\mathbf{x̂_d}(ℓ+j), \mathbf{û_0}(ℓ+j), \mathbf{d̂_0}(ℓ+j), \mathbf{p}\Big) \\
1412+ \mathbf{k̇}_2(ℓ+j) &= \mathbf{f}\Big(\mathbf{x̂_d}(ℓ+j+1), \mathbf{û_0}(ℓ+j+h), \mathbf{d̂_0}(ℓ+j+1), \mathbf{p}\Big)
1413+ \end{aligned}
1414+ ```
1415+ in which ``h`` is the hold order `transcription.h` and the disturbed input ``\mathbf{û_0}``
1416+ is defined in [`f̂!`](@ref) documentation.
1417+ """
1418+ function con_nonlinprogeq_mhe! (
1419+ geq, _ , Û0, K̇,
1420+ estim:: MovingHorizonEstimator , model:: NonLinModel , transcription:: TrapezoidalCollocation ,
1421+ x̂0arr, Ŵ, Z̃
1422+ )
1423+ nu, nx, nd, h = model. nu, model. nx, model. nd, transcription. h
1424+ nx̂, nxs, nŵ, He = estim. nx̂, estim. nxs, estim. nx̂, estim. He
1425+ Nk = estim. Nk[]
1426+ f_threads = transcription. f_threads
1427+ Ts = model. Ts
1428+ nk = get_nk (model, transcription)
1429+ nw = nŵ - nxs
1430+ nx̃ = estim. nε + nx̂
1431+ p = estim. direct ? 0 : 1
1432+ X̂0_Z̃ = @views Z̃[(nx̃+ 1 ): (nx̃+ nx̂* He)]
1433+ Û0 = disturbedinput! (Û0, estim, x̂0arr, X̂0_Z̃, estim. U0)
1434+ @threadsif f_threads for j= 1 : Nk
1435+ if j < 2
1436+ x̂d_Z̃ = @views x̂0arr[1 : nx]
1437+ else
1438+ x̂d_Z̃ = @views X̂0_Z̃[(1 + nx̂* (j- 2 )): (nx̂* (j- 2 ) + nx)]
1439+ end
1440+ d0 = @views estim. D0[(1 + nd* (j+ p- 1 )): (nd* (j+ p))]
1441+ û0 = @views Û0[(1 + nu* (j- 1 )): (nu* j)]
1442+ k̇ = @views K̇[(1 + nk* (j- 1 )): (nk* j)]
1443+ ŵd = @views Ŵ[(1 + nŵ* (j- 1 )): (nŵ* (j- 1 ) + nw)]
1444+ x̂dnext_Z̃ = @views X̂0_Z̃[(1 + nx̂* (j- 1 )): (nx̂* (j- 1 ) + nx)]
1445+ sdnext = @views geq[(1 + nx* (j- 1 )): (nx* j)]
1446+ k̇1, k̇2 = @views k̇[1 : nx], k̇[nx+ 1 : 2 * nx]
1447+ d0next = @views estim. D0[(1 + nd* (j+ p)): (nd* (j+ p+ 1 ))]
1448+ if f_threads || h < 1 || j < 2
1449+ # we need to recompute k1 with multi-threading, even with h==1, since the
1450+ # last iteration (j-1) may not be executed (iterations are re-orderable)
1451+ model. f! (k̇1, x̂d_Z̃, û0, d0, model. p)
1452+ else
1453+ k̇1 .= @views K̇[(1 + nk* (j- 1 )- nx): (nk* (j- 1 ))] # k2 of of the last iter. j-1
1454+ end
1455+ if h < 1
1456+ model. f! (k̇2, x̂dnext_Z̃, û0, d0next, model. p)
1457+ else
1458+ # special case: û0(k+p) ≈ û0(k+p-1), since û0(k+p) is not available at k!
1459+ û0next = @views j ≥ Nk ? û0 : Û0[(1 + nu* j): (nu* (j+ 1 ))]
1460+ model. f! (k̇2, x̂dnext_Z̃, û0next, d0next, model. p)
1461+ end
1462+ sdnext .= @. x̂d_Z̃ - x̂dnext_Z̃ + 0.5 * Ts* (k̇1 + k̇2)
1463+ sdnext .+ = ŵd
1464+ end
1465+ Nk < He && (geq[nx̂* Nk+ 1 : end ] .= 0 )
1466+ return geq
1467+ end
1468+
13881469" No nonlinear eq. const. for other cases e.g. [`SingleShooting`](@ref), returns `geq` unchanged."
13891470con_nonlinprogeq_mhe! (geq,_,_,_,:: MovingHorizonEstimator , :: SimModel , :: TranscriptionMethod , _,_,_) = geq
0 commit comments