@@ -17,6 +17,7 @@ function init_estimate_cov!(estim::MovingHorizonEstimator, y0m, d0, u0)
1717 estim. q̃ .= 0
1818 estim. r .= 0
1919 estim. con. FX̂ .= 0
20+ estim. con. FS .= 0
2021 if estim. direct
2122 # add y0m(-1) to the extended data window (custom NL constraints):
2223 estim. Yem[1 : ny] .= y0m .+ @views yop[estim. i_ym]
@@ -422,7 +423,7 @@ function initpred!(estim::MovingHorizonEstimator{NT}, model::LinModel) where NT<
422423 nYm, nZ = estim. nym* Nk, get_nZ_mhe (estim. transcription, Nk, nx̂, nŵ)
423424 # --- truncate vectors and matrices if Nk < He ---
424425 U0, D0, Y0m = trunc_windows (estim)
425- Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var = trunc_predmat (estim, estim . transcription )
426+ Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var = trunc_predmat (estim)
426427 invQ̂_Nk = trunc_cov (invQ̂_He, nx̂, Nk, estim. He)
427428 invR̂_Nk = trunc_cov (invR̂_He, nym, Nk, estim. He)
428429 # --- update F and fx̄ vectors for MHE predictions ---
@@ -652,6 +653,55 @@ function trunc_windows(estim::MovingHorizonEstimator)
652653 return U0, D0, Y0m
653654end
654655
656+ " Truncate or extract subparts of the prediction matrices if `Nk < He`"
657+ function trunc_predmat (estim:: MovingHorizonEstimator )
658+ model, transcription = estim. model, estim. transcription
659+ nŵ, nym, nε, Nk = estim. nx̂, estim. nym, estim. nε, estim. Nk[]
660+ nU, nYm, nŴ, nD = model. nu* Nk, nym* Nk, nŵ* Nk, model. nd* (Nk+ 1 )
661+ if Nk < estim. He # avoid views since allocations only when Nk < He and we want fast mul!
662+ i_Z̃_Nk = get_i_Z̃_Nk (estim, transcription)
663+ i_Z_Nk = nε < 1 ? i_Z̃_Nk : i_Z̃_Nk[begin + 1 : end ] .- 1
664+ Ẽ = estim. Ẽ[1 : nYm, i_Z̃_Nk]
665+ G, J, B = estim. G[1 : nYm, 1 : nU], estim. J[1 : nYm, 1 : nD], estim. B[1 : nYm]
666+ ẽx̄ = estim. ẽx̄[:, i_Z̃_Nk]
667+ Tŵ = estim. Tŵ[1 : nŴ, i_Z_Nk]
668+ F = @views estim. F[1 : nYm] # views here since they will store results
669+ H̃_data = @views estim. H̃. data[i_Z̃_Nk, i_Z̃_Nk]
670+ H̃ = @views estim. H̃[i_Z̃_Nk, i_Z̃_Nk]
671+ q̃ = @views estim. q̃[i_Z̃_Nk]
672+ Z̃var = @views estim. optim[:Z̃var ][i_Z̃_Nk]
673+ else
674+ Ẽ, F, G, J, B = estim. Ẽ, estim. F, estim. G, estim. J, estim. B
675+ ẽx̄, Tŵ = estim. ẽx̄, estim. Tŵ
676+ H̃, H̃_data, q̃ = estim. H̃, estim. H̃. data, estim. q̃
677+ Z̃var = estim. optim[:Z̃var ]
678+ end
679+ return Ẽ, F, G, J, B, ẽx̄, Tŵ, H̃, H̃_data, q̃, Z̃var
680+ end
681+
682+ " Extract subparts of the defect matrices if `Nk < He`"
683+ function trunc_defectmat (estim:: MovingHorizonEstimator )
684+ model, transcription, con = estim. model, estim. transcription, estim. con
685+ FS = con. FS
686+ nx̂, Nk = estim. nx̂, estim. Nk[]
687+ nU, nX̂, nD = model. nu* Nk, nx̂* Nk, model. nd* (Nk+ 1 )
688+ if Nk < estim. He # avoid views since allocations only when Nk < He and we want fast mul!
689+ i_Z̃_Nk = get_i_Z̃_Nk (estim, transcription)
690+ ẼS = con. ẼS[1 : nX̂, i_Z̃_Nk]
691+ GS, JS, BS = con. GS[1 : nX̂, 1 : nU], con. JS[1 : nX̂, 1 : nD], con. BS[1 : nX̂]
692+ FS = @views con. FS[1 : nX̂] # views here since they will store results
693+ Aeq = @views con. Aeq[1 : nX̂, i_Z̃_Nk]
694+ beq = @views con. beq[1 : nX̂]
695+ Z̃var = @views estim. optim[:Z̃var ][i_Z̃_Nk]
696+ else
697+ ẼS, FS, GS, JS, BS = con. ẼS, con. FS, con. GS, con. JS, con. BS
698+ Aeq = con. Aeq
699+ beq = con. beq
700+ Z̃var = estim. optim[:Z̃var ]
701+ end
702+ return ẼS, FS, GS, JS, BS, Aeq, beq, Z̃var
703+ end
704+
655705" Truncate the inverse covariance `invA_He` to the window size `Nk` if `Nk < He`."
656706function trunc_cov (invA_He:: Hermitian{<:Real, <:AbstractMatrix} , n, Nk, He)
657707 if Nk < He
@@ -931,8 +981,8 @@ function setmodel_estimator!(
931981 con. BX̂ .= BX̂
932982 # --- defect matrices ---
933983 ES, GS, JS, BS = init_defectmat_mhe (
934- model, transcription, He, estim . Â, estim . B̂u, estim . B̂d, estim . x̂op, estim . f̂op,
935- estim. direct
984+ model, transcription, He,
985+ estim. Â, estim . B̂u, estim . B̂d, estim . x̂op, estim . f̂op, estim . As, estim . direct
936986 )
937987 Aeq, ẼS = augmentdefect (ES, nε; slackfirst= true )
938988 con. ẼS .= ẼS
0 commit comments