Skip to content

Commit 78f50ba

Browse files
committed
changed: replaced f̂_input! with disturbedinput!
1 parent 95208ad commit 78f50ba

File tree

5 files changed

+36
-42
lines changed

5 files changed

+36
-42
lines changed

docs/src/internals/state_estim.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ ModelPredictiveControl.get_nonlincon_oracle(::MovingHorizonEstimator, ::ModelPre
2727
```@docs
2828
ModelPredictiveControl.f̂!
2929
ModelPredictiveControl.ĥ!
30-
ModelPredictiveControl.f̂_input!
3130
```
3231

3332
## Update Quadratic Optimization

src/controller/execute.jl

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,36 @@ function predictstoch!(Ŷs, mpc::PredictiveController, estim::InternalModel)
312312
return nothing
313313
end
314314
"Fill `Ŷs` vector with 0 values when `estim` is not an [`InternalModel`](@ref)."
315-
predictstoch!(Ŷs, mpc::PredictiveController, ::StateEstimator) = (Ŷs .= 0; nothing)
315+
predictstoch!(Ŷs, ::PredictiveController, ::StateEstimator) = (Ŷs .= 0; nothing)
316+
317+
"""
318+
disturbedinput!(Û0, mpc::PredictiveController, estim::StateEstimator, U0, X̂0) -> nothing
319+
320+
Fill disturbed inputs of the augmented model `Û0` in-place with stochastic states in `X̂0`
321+
322+
Both `Û0` and `U0` variables include deviation vectors from ``k+0`` to ``k+H_p-1``. The
323+
predicted states `X̂0` include deviation vectors from ``k+1`` to ``k+H_p-1`` (the current one
324+
is stored in `estim.x̂0`).
325+
326+
This function is used for the collocation methods that directly call the state derivative
327+
function `estim.model.f!` with the manipulated inputs augmented with the estimated
328+
disturbances at model input (see [`init_estimstoch`](@ref)). It's also necessary to prefill
329+
the `Û0` vector before anything since both `û0` and `û0next` are needed at each stage with
330+
hold order `h>0`, thus potential race conditions with multi-threading.
331+
"""
332+
function disturbedinput!(Û0, mpc::PredictiveController, estim::StateEstimator, U0, X̂0)
333+
nu, nx, nx̂ = estim.model.nu, estim.model.nx, estim.nx̂
334+
Cs_u = estim.Cs_u
335+
Û0 .= U0
336+
for j=0:mpc.Hp-1
337+
xs = @views j < 1 ? estim.x̂0[(nx+1):(nx̂)] : X̂0[(nx+1+nx̂*(j-1)):(nx̂*j)]
338+
û0 = @views Û0[(1+nu*j):(nu*(j+1))]
339+
mul!(û0, Cs_u, xs, 1, 1) # û0 = u0 + Cs_u*xs
340+
end
341+
return nothing
342+
end
343+
"No input disturbances for [`InternalModel`](@ref), hence do `Û0 .= U0`."
344+
disturbedinput!(Û0, ::PredictiveController, ::InternalModel, U0, _) = (Û0 .= U0; nothing)
316345

317346
@doc raw"""
318347
linconstraint_custom!(mpc::PredictiveController, model::SimModel)

src/controller/transcription.jl

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1397,7 +1397,7 @@ deterministic and stochastic states extracted from the decision variables `Z̃`.
13971397
\end{aligned}
13981398
```
13991399
in which ``h`` is the hold order `transcription.h` and the disturbed input ``\mathbf{û_0}``
1400-
is defined in [`f̂_input!`](@ref).
1400+
is defined in [`!`](@ref) documentation.
14011401
"""
14021402
function con_nonlinprogeq!(
14031403
geq, X̂0, Û0, K̇,
@@ -1412,11 +1412,7 @@ function con_nonlinprogeq!(
14121412
nk = get_nk(model, transcription)
14131413
D̂0 = mpc.D̂0
14141414
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
1415-
for j=0:Hp-1 # prefilling Û0 to avoid race-condition (both û0 and û0next are needed):
1416-
x̂0_Z̃ = @views j < 1 ? mpc.estim.x̂0[1:nx̂] : X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
1417-
u0, û0 = @views U0[(1 + nu*j):(nu*(j+1))], Û0[(1 + nu*j):(nu*(j+1))]
1418-
f̂_input!(û0, mpc.estim, model, x̂0_Z̃, u0)
1419-
end
1415+
disturbedinput!(Û0, mpc, estim, U0, X̂0_Z̃)
14201416
@threadsif f_threads for j=1:Hp
14211417
if j < 2
14221418
x̂0_Z̃ = @views mpc.estim.x̂0[1:nx̂]
@@ -1503,9 +1499,9 @@ and disturbances are piecewise constant or linear:
15031499
\mathbf{d̂}_i(k+j) &= (1-τ_i)\mathbf{d̂_0}(k+j) + τ_i\mathbf{d̂_0}(k+j+1)
15041500
\end{aligned}
15051501
```
1506-
The disturbed input ``\mathbf{û_0}(k+j)`` is defined in [`f̂_input!`](@ref). The defects for
1507-
the stochastic states ``\mathbf{s_s}`` are computed as the [`TrapezoidalCollocation`](@ref)
1508-
method, and the ones for the continuity constraint of the deterministic states are:
1502+
The disturbed input ``\mathbf{û_0}`` is defined in [`!`](@ref). The stochastic state
1503+
defects ``\mathbf{s_s}`` are computed as the [`TrapezoidalCollocation`](@ref) method, and
1504+
the ones for the continuity constraint of the deterministic states are:
15091505
```math
15101506
\mathbf{s_c}(k+j+1)
15111507
= \mathbf{C_o} \begin{bmatrix}
@@ -1535,11 +1531,7 @@ function con_nonlinprogeq!(
15351531
D̂0 = mpc.D̂0
15361532
X̂0_Z̃, K_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)], Z̃[(nΔU+nX̂+1):(nΔU+nX̂+nk*Hp)]
15371533
D̂temp = mpc.buffer.
1538-
for j=0:Hp-1 # prefilling Û0 to avoid race-condition (both û0 and û0next are needed):
1539-
x̂0_Z̃ = @views j < 1 ? mpc.estim.x̂0[1:nx̂] : X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
1540-
u0, û0 = @views U0[(1 + nu*j):(nu*(j+1))], Û0[(1 + nu*j):(nu*(j+1))]
1541-
f̂_input!(û0, mpc.estim, model, x̂0_Z̃, u0)
1542-
end
1534+
disturbedinput!(Û0, mpc, estim, U0, X̂0_Z̃)
15431535
@threadsif f_threads for j=1:Hp
15441536
if j < 2
15451537
x̂0_Z̃ = @views mpc.estim.x̂0[1:nx̂]

src/estimator/execute.jl

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,6 @@ function fs!(x̂0next, estim::StateEstimator, model::SimModel, x̂0)
121121
return nothing
122122
end
123123

124-
@doc raw"""
125-
f̂_input!(û0, estim::StateEstimator, model::SimModel, x̂0, u0) -> nothing
126-
127-
Compute the disturbed input ``\mathbf{û_0}`` of the augmented model from `x̂0` and `u0`.
128-
129-
It mutates `û0` in place with the following equation:
130-
```math
131-
\mathbf{û_0}(k) = \mathbf{u_0}(k) + \mathbf{C_{s_u} x_s}(k)
132-
```
133-
where ``\mathbf{C_{s_u}}`` is defined in [`init_estimstoch`](@ref), and ``\mathbf{x_s}`` is
134-
extracted from `x̂0` as the last `estim.nxs` elements.
135-
"""
136-
function f̂_input!(û0, estim::StateEstimator, model::SimModel, x̂0, u0)
137-
xs = @views x̂0[model.nx+1:end]
138-
mul!(û0, estim.Cs_u, xs) # ys_u = Cs_u*xs
139-
û0 .+= u0 # û0 = u0 + ys_u
140-
return nothing
141-
end
142-
143124
@doc raw"""
144125
ĥ!(ŷ0, estim::StateEstimator, model::SimModel, x̂0, d0) -> nothing
145126

src/estimator/internal_model.jl

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -189,13 +189,6 @@ Does nothing since [`InternalModel`](@ref) does not augment the state vector.
189189
"""
190190
fs!( _ , ::InternalModel, ::SimModel, _ ) = nothing
191191

192-
@doc raw"""
193-
f̂_input!(û0, estim::InternalModel, model::SimModel, x̂0, u0) -> nothing
194-
195-
Compute `û0 .= u0` since [`InternalModel`](@ref) does not augment the state vector.
196-
"""
197-
f̂_input!(û0, ::InternalModel, ::SimModel, _ , u0) = (û0 .= u0; nothing)
198-
199192
@doc raw"""
200193
ĥ!(ŷ0, estim::InternalModel, model::NonLinModel, x̂0, d0)
201194

0 commit comments

Comments
 (0)