Skip to content

Commit ea8355c

Browse files
committed
changed: now internally handle the operating points
This is simpler since the users of the function, that is `PredictiveController` and `StateEstimator` objects, no longer need to add `f̂op` subtract `x̂op` after calling `f̂`
1 parent ffc3bda commit ea8355c

File tree

5 files changed

+16
-13
lines changed

5 files changed

+16
-13
lines changed

src/controller/transcription.jl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1159,7 +1159,6 @@ function predict!(
11591159
k0 = @views K0[(1 + nk*(j-1)):(nk*j)]
11601160
x̂0next = @views X̂0[(1 + nx̂*(j-1)):(nx̂*j)]
11611161
f̂!(x̂0next, û0, k0, mpc.estim, model, x̂0, u0, d0)
1162-
x̂0next .+= mpc.estim.f̂op .- mpc.estim.x̂op
11631162
x̂0 = @views X̂0[(1 + nx̂*(j-1)):(nx̂*j)]
11641163
d0 = @views D̂0[(1 + nd*(j-1)):(nd*j)]
11651164
ŷ0 = @views Ŷ0[(1 + ny*(j-1)):(ny*j)]
@@ -1318,8 +1317,6 @@ function con_nonlinprogeq!(
13181317
x̂0next_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
13191318
ŝnext = @views geq[(1 + nx̂*(j-1)):(nx̂*j)]
13201319
f̂!(x̂0next, û0, k0, mpc.estim, model, x̂0, u0, d0)
1321-
# handle operating points (but should be zeros for NonLinModel):
1322-
x̂0next .+= mpc.estim.f̂op .- mpc.estim.x̂op
13231320
ŝnext .= x̂0next .- x̂0next_Z̃
13241321
x̂0 = x̂0next_Z̃ # using states in Z̃ for next iteration (allow parallel for)
13251322
d0 = d0next

src/estimator/execute.jl

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,10 @@ end
1919
Mutating state function ``\mathbf{f̂}`` of the augmented model.
2020
2121
By introducing an augmented state vector ``\mathbf{x̂_0}`` like in [`augment_model`](@ref),
22-
the function returns the next state of the augmented model, defined as:
22+
the function returns the next state of the augmented model, as deviation vectors:
2323
```math
2424
\begin{aligned}
25-
\mathbf{x̂_0}(k+1) &= \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big) \\
25+
\mathbf{x̂_0}(k+1) &= \mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big)
2626
\mathbf{ŷ_0}(k) &= \mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k)\Big)
2727
\end{aligned}
2828
```
@@ -41,7 +41,8 @@ Extended Help for details on ``\mathbf{û_0, f̂}`` and ``\mathbf{ĥ}`` implem
4141
\begin{aligned}
4242
\mathbf{f̂}\Big(\mathbf{x̂_0}(k), \mathbf{u_0}(k), \mathbf{d_0}(k)\Big) &= \begin{bmatrix}
4343
\mathbf{f}\Big(\mathbf{x_0}(k), \mathbf{û_0}(k), \mathbf{d_0}(k), \mathbf{p}\Big) \\
44-
\mathbf{A_s} \mathbf{x_s}(k) \end{bmatrix} \\
44+
\mathbf{A_s} \mathbf{x_s}(k) \end{bmatrix}
45+
+ \mathbf{f̂_{op}} - \mathbf{x̂_{op}} \\
4546
\mathbf{ĥ}\Big(\mathbf{x̂_0}(k), \mathbf{d_0}(k)\Big) &=
4647
\mathbf{h}\Big(\mathbf{x_0}(k), \mathbf{d_0}(k), \mathbf{p}\Big) + \mathbf{y_{s_y}}(k)
4748
\end{aligned}
@@ -55,7 +56,9 @@ Extended Help for details on ``\mathbf{û_0, f̂}`` and ``\mathbf{ĥ}`` implem
5556
\end{aligned}
5657
```
5758
The ``\mathbf{f}`` and ``\mathbf{h}`` functions above are in fact the [`f!`](@ref) and
58-
[`h!`](@ref) methods, respectively.
59+
[`h!`](@ref) methods, respectively. The operating points ``\mathbf{x̂_{op}, f̂_{op}}``
60+
are computed by [`augment_model`](@ref) (almost always zeros in practice for
61+
[`NonLinModel`](@ref)).
5962
"""
6063
function f̂!(x̂0next, û0, k0, estim::StateEstimator, model::SimModel, x̂0, u0, d0)
6164
return f̂!(x̂0next, û0, k0, model, estim.As, estim.Cs_u, x̂0, u0, d0)
@@ -64,12 +67,13 @@ end
6467
"""
6568
f̂!(x̂0next, _ , _ , estim::StateEstimator, model::LinModel, x̂0, u0, d0) -> nothing
6669
67-
Use the augmented model matrices if `model` is a [`LinModel`](@ref).
70+
Use the augmented model matrices and operating points if `model` is a [`LinModel`](@ref).
6871
"""
6972
function f̂!(x̂0next, _ , _ , estim::StateEstimator, ::LinModel, x̂0, u0, d0)
7073
mul!(x̂0next, estim.Â, x̂0)
7174
mul!(x̂0next, estim.B̂u, u0, 1, 1)
7275
mul!(x̂0next, estim.B̂d, d0, 1, 1)
76+
x̂0next .+= estim.f̂op .- estim.x̂op
7377
return nothing
7478
end
7579

@@ -86,6 +90,7 @@ function f̂!(x̂0next, û0, k0, model::SimModel, As, Cs_u, x̂0, u0, d0)
8690
û0 .+= u0 # û0 = u0 + ys_u
8791
f!(xdnext, k0, model, xd, û0, d0, model.p)
8892
mul!(xsnext, As, xs)
93+
x̂0next .+= estim.f̂op .- estim.x̂op
8994
return nothing
9095
end
9196

src/estimator/internal_model.jl

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,8 +172,10 @@ State function ``\mathbf{f̂}`` of [`InternalModel`](@ref) for [`NonLinModel`](@
172172
173173
It calls [`f!`](@ref) directly since this estimator does not augment the states.
174174
"""
175-
function f̂!(x̂0next, _ , k0, ::InternalModel, model::NonLinModel, x̂0, u0, d0)
176-
return f!(x̂0next, k0, model, x̂0, u0, d0, model.p)
175+
function f̂!(x̂0next, _ , k0, estim::InternalModel, model::NonLinModel, x̂0, u0, d0)
176+
f!(x̂0next, k0, model, x̂0, u0, d0, model.p)
177+
x̂0next .+= estim.f̂op .- estim.x̂op
178+
return x̂0next
177179
end
178180

179181
@doc raw"""

src/estimator/kalman.jl

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,6 @@ function update_estimate!(estim::UnscentedKalmanFilter, y0m, d0, u0)
867867
P̂next = estim.buffer.
868868
mul!(P̂next, X̄0next, Ŝ_X̄0nextᵀ)
869869
P̂next .+=
870-
x̂0next .+= estim.f̂op .- estim.x̂op
871870
estim.x̂0 .= x̂0next
872871
estim.cov.P̂ .= Hermitian(P̂next, :L)
873872
return nothing

src/estimator/mhe/execute.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,7 +610,7 @@ function predict!(V̂, X̂0, û0, k0, ŷ0, estim::MovingHorizonEstimator, mode
610610
= @views Z̃[(1 + nx̃ + nŵ*(j-1)):(nx̃ + nŵ*j)]
611611
x̂0next = @views X̂0[(1 + nx̂ *(j-1)):(nx̂ *j)]
612612
f̂!(x̂0next, û0, k0, estim, model, x̂0, u0, d0)
613-
x̂0next .+= .+ estim.f̂op .- estim.x̂op
613+
x̂0next .+=
614614
y0nextm = @views estim.Y0m[(1 + nym * (j-1)):(nym*j)]
615615
d0next = @views estim.D0[(1 + nd*j):(nd*(j+1))]
616616
ĥ!(ŷ0next, estim, model, x̂0next, d0next)
@@ -629,7 +629,7 @@ function predict!(V̂, X̂0, û0, k0, ŷ0, estim::MovingHorizonEstimator, mode
629629
V̂[(1 + nym*(j-1)):(nym*j)] .= y0m .- ŷ0m
630630
x̂0next = @views X̂0[(1 + nx̂ *(j-1)):(nx̂ *j)]
631631
f̂!(x̂0next, û0, k0, estim, model, x̂0, u0, d0)
632-
x̂0next .+= .+ estim.f̂op .- estim.x̂op
632+
x̂0next .+=
633633
x̂0 = x̂0next
634634
end
635635
end

0 commit comments

Comments
 (0)