@@ -34,15 +34,16 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
3434 add_data_windows! (estim:: MovingHorizonEstimator , u, d, ym)
3535 initpred! (estim, model)
3636 linconstraint! (estim, model)
37- nx̂, nym, nŵ, Nk = estim. nx̂, estim. nym, estim. nx̂, estim. Nk[]
37+ nŷ, nx̂, nym, nŵ, Nk = model . ny, estim. nx̂, estim. nym, estim. nx̂, estim. Nk[]
3838 nx̃ = ! isinf (estim. C) + nx̂
3939 Z̃var:: Vector{VariableRef} = optim[:Z̃var ]
4040 V̂ = Vector {NT} (undef, nym* Nk)
4141 X̂ = Vector {NT} (undef, nx̂* Nk)
42+ ŷ = Vector {NT} (undef, nŷ)
4243 x̄ = Vector {NT} (undef, nx̂)
4344 ϵ0 = isinf (estim. C) ? empty (estim. Z̃) : estim. Z̃[begin ]
4445 Z̃0 = [ϵ0; estim. x̂arr_old; estim. Ŵ]
45- V̂, X̂ = predict! (V̂, X̂, estim, model, Z̃0)
46+ V̂, X̂ = predict! (V̂, X̂, ŷ, estim, model, Z̃0)
4647 J0 = obj_nonlinprog! (x̄, estim, model, V̂, Z̃0)
4748 # initial Z̃0 with Ŵ=0 if objective or constraint function not finite :
4849 isfinite (J0) || (Z̃0 = [ϵ0; estim. x̂arr_old; zeros (NT, nŵ* estim. He)])
@@ -75,7 +76,7 @@ function update_estimate!(estim::MovingHorizonEstimator{NT}, u, ym, d) where NT<
7576 estim. Z̃ .= iserror (optim) ? Z̃last : Z̃curr
7677 # --------- update estimate -----------------------
7778 estim. Ŵ[1 : nŵ* Nk] .= @views estim. Z̃[nx̃+ 1 : nx̃+ nŵ* Nk] # update Ŵ with optimum for warm-start
78- V̂, X̂ = predict! (V̂, X̂, estim, model, estim. Z̃)
79+ V̂, X̂ = predict! (V̂, X̂, ŷ, estim, model, estim. Z̃)
7980 x̂ .= X̂[end - nx̂+ 1 : end ]
8081 if Nk == estim. He
8182 uarr, ymarr, darr = estim. U[1 : model. nu], estim. Ym[1 : nym], estim. D[1 : model. nd]
@@ -129,7 +130,8 @@ function getinfo(estim::MovingHorizonEstimator{NT}) where NT<:Real
129130 MyTypes = Union{JuMP. _SolutionSummary, Hermitian{NT, Matrix{NT}}, Vector{NT}, NT}
130131 info = Dict {Symbol, MyTypes} ()
131132 V̂, X̂ = similar (estim. Ym[1 : nym* Nk]), similar (estim. X̂[1 : nx̂* Nk])
132- V̂, X̂ = predict! (V̂, X̂, estim, model, estim. Z̃)
133+ ŷ = similar (model. yop)
134+ V̂, X̂ = predict! (V̂, X̂, ŷ, estim, model, estim. Z̃)
133135 x̂arr = estim. Z̃[nx̃- nx̂+ 1 : nx̃]
134136 x̄ = estim. x̂arr_old - x̂arr
135137 X̂ = [x̂arr; X̂]
@@ -372,16 +374,16 @@ function obj_nonlinprog!(
372374end
373375
374376"""
375- predict!(V̂, X̂, estim::MovingHorizonEstimator, model::LinModel, Z̃) -> V̂, X̂
377+ predict!(V̂, X̂, ŷ, estim::MovingHorizonEstimator, model::LinModel, Z̃) -> V̂, X̂
376378
377379Compute the `V̂` vector and `X̂` vectors for the `MovingHorizonEstimator` and `LinModel`.
378380
379- The method mutates `V̂` and `X̂` vector arguments. The vector `V̂` is the estimated sensor
381+ The function mutates `V̂`, `X̂` and ŷ vector arguments. The vector `V̂` is the estimated sensor
380382noises `V̂` from ``k-N_k+1`` to ``k``. The `X̂` vector is estimated states from ``k-N_k+2`` to
381383``k+1``.
382384"""
383385function predict! (
384- V̂, X̂, estim:: MovingHorizonEstimator , :: LinModel , Z̃:: Vector{T}
386+ V̂, X̂, _ , estim:: MovingHorizonEstimator , :: LinModel , Z̃:: Vector{T}
385387) where {T<: Real }
386388 Nk, nϵ = estim. Nk[], ! isinf (estim. C)
387389 nX̂, nŴ, nYm = estim. nx̂* Nk, estim. nx̂* Nk, estim. nym* Nk
@@ -393,13 +395,11 @@ end
393395
394396" Compute the two vectors when `model` is not a `LinModel`."
395397function predict! (
396- V̂, X̂, estim:: MovingHorizonEstimator , model:: SimModel , Z̃:: Vector{T}
398+ V̂, X̂, ŷ, estim:: MovingHorizonEstimator , model:: SimModel , Z̃:: Vector{T}
397399) where {T<: Real }
398400 Nk = estim. Nk[]
399401 nu, nd, ny, nx̂, nŵ, nym = model. nu, model. nd, model. ny, estim. nx̂, estim. nx̂, estim. nym
400402 nx̃ = ! isinf (estim. C) + nx̂
401- # TODO : avoid these two allocations if possible:
402- ŷ, x̂next = Vector {T} (undef, ny), Vector {T} (undef, nx̂)
403403 x̂ = @views Z̃[nx̃- nx̂+ 1 : nx̃]
404404 for j= 1 : Nk
405405 u = @views estim. U[ (1 + nu * (j- 1 )): (nu* j)]
@@ -409,10 +409,10 @@ function predict!(
409409 ĥ! (ŷ, estim, model, x̂, d)
410410 ŷm = @views ŷ[estim. i_ym]
411411 V̂[(1 + nym* (j- 1 )): (nym* j)] .= ym .- ŷm
412+ x̂next = @views X̂[(1 + nx̂ * (j- 1 )): (nx̂ * j)]
412413 f̂! (x̂next, estim, model, x̂, u, d)
413414 x̂next .+ = ŵ
414- X̂[(1 + nx̂ * (j- 1 )): (nx̂ * j)] = x̂next
415- x̂ = @views X̂[(1 + nx̂* (j- 1 )): (nx̂* j)]
415+ x̂ = x̂next
416416 end
417417 return V̂, X̂
418418end
0 commit comments