Skip to content

Commit 94f86e7

Browse files
committed
debug: also warm-start the collocation points
1 parent 17c1f71 commit 94f86e7

1 file changed

Lines changed: 46 additions & 31 deletions

File tree

src/controller/transcription.jl

Lines changed: 46 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ where ``\mathbf{K}`` encompasses all the intermediate stages of the deterministi
146146
\mathbf{k}_{1}(k+1) \\
147147
\mathbf{k}_{2}(k+1) \\
148148
\vdots \\
149-
\mathbf{k}_{n_o}(k+H_p) \end{bmatrix}
149+
\mathbf{k}_{n_o}(k+H_p-1) \end{bmatrix}
150150
```
151151
and ``\mathbf{k}_o(k+j)`` is the deterministic state prediction for the ``o``th collocation
152152
point at the ``j``th stage/interval/finite element (details in Extended Help). The `roots`
@@ -974,9 +974,10 @@ last control period ``k-1``, and ``ϵ(k-1)``, the slack variable of the last con
974974
"""
975975
function set_warmstart!(mpc::PredictiveController, ::SingleShooting, Z̃var)
976976
nu, Hc, Z̃s = mpc.estim.model.nu, mpc.Hc, mpc.buffer.
977+
nΔU = nu*Hc
977978
# --- input increments ΔU ---
978-
Z̃s[1:(Hc*nu-nu)] .= @views mpc.Z̃[nu+1:Hc*nu]
979-
Z̃s[(Hc*nu-nu+1):(Hc*nu)] .= 0
979+
Z̃s[1:(nΔU-nu)] .= @views mpc.Z̃[nu+1:nΔU]
980+
Z̃s[(nΔU-nu+1):(nΔU)] .= 0
980981
# --- slack variable ϵ ---
981982
mpc.== 1 && (Z̃s[end] = mpc.Z̃[end])
982983
JuMP.set_start_value.(Z̃var, Z̃s)
@@ -1001,25 +1002,35 @@ It warm-starts the solver at:
10011002
\vdots \\
10021003
\mathbf{x̂_0}(k+H_p-1|k-1) \\
10031004
\mathbf{x̂_0}(k+H_p-1|k-1) \\
1004-
\mathbf{k}_{1}(k+1|k-1) \\
1005-
\mathbf{k}_{2}(k+1|k-1) \\
1005+
\mathbf{k}(k+0|k-1) \\
1006+
\mathbf{k}(k+1|k-1) \\
10061007
\vdots \\
1007-
\mathbf{k}_{n_o}(k+1|k-1) \\
1008+
\mathbf{k}(k+H_p-2|k-1) \\
1009+
\mathbf{k}(k+H_p-2|k-1) \\
10081010
ϵ(k-1)
10091011
\end{bmatrix}
10101012
```
10111013
where ``\mathbf{x̂_0}(k+j|k-1)`` is the predicted state for time ``k+j`` computed at the
10121014
last control period ``k-1``, expressed as a deviation from the operating point
1013-
``\mathbf{x̂_{op}}``.
1015+
``\mathbf{x̂_{op}}``. The vector ``\mathbf{k}(k+j|k-1) include the ``n_o`` intermediate
1016+
stage predictions for the interval ``k+j``, and is also computed at the last control period.
10141017
"""
1015-
function set_warmstart!(mpc::PredictiveController, ::OrthogonalCollocation, Z̃var)
1016-
nu, nx̂, Hp, Hc, Z̃s = mpc.estim.model.nu, mpc.estim.nx̂, mpc.Hp, mpc.Hc, mpc.buffer.
1018+
function set_warmstart!(
1019+
mpc::PredictiveController, transcription::OrthogonalCollocation, Z̃var
1020+
)
1021+
nu, nx̂ = mpc.estim.model.nu, mpc.estim.nx̂
1022+
Hp, Hc, Z̃s = mpc.Hp, mpc.Hc, mpc.buffer.
1023+
nk = get_nk(mpc.estim.model, transcription)
1024+
nΔU, nX̂, nK = nu*Hc, nx̂*Hp, nk*Hp
10171025
# --- input increments ΔU ---
1018-
Z̃s[1:(Hc*nu-nu)] .= @views mpc.Z̃[nu+1:Hc*nu]
1019-
Z̃s[(Hc*nu-nu+1):(Hc*nu)] .= 0
1026+
Z̃s[1:(nΔU-nu)] .= @views mpc.Z̃[(nu+1):(nΔU)]
1027+
Z̃s[(nΔU-nu+1):(nΔU)] .= 0
10201028
# --- predicted states X̂0 ---
1021-
Z̃s[(Hc*nu+1):(Hc*nu+Hp*nx̂-nx̂)] .= @views mpc.Z̃[(Hc*nu+nx̂+1):(Hc*nu+Hp*nx̂)]
1022-
Z̃s[(Hc*nu+Hp*nx̂-nx̂+1):(Hc*nu+Hp*nx̂)] .= @views mpc.Z̃[(Hc*nu+Hp*nx̂-nx̂+1):(Hc*nu+Hp*nx̂)]
1029+
Z̃s[(nΔU+1):(nΔU+nX̂-nx̂)] .= @views mpc.Z̃[(nΔU+nx̂+1):(nΔU+nX̂)]
1030+
Z̃s[(nΔU+nX̂-nx̂+1):(nΔU+nX̂)] .= @views mpc.Z̃[(nΔU+nX̂-nx̂+1):(nΔU+nX̂)]
1031+
# --- collocation points K ---
1032+
Z̃s[(nΔU+nX̂+1):(nΔU+nX̂+nK-nk)] .= @views mpc.Z̃[(nΔU+nX̂+nk+1):(nΔU+nX̂+nK)]
1033+
Z̃s[(nΔU+nX̂+nK-nk+1):(nΔU+nX̂+nK)] .= @views mpc.Z̃[(nΔU+nX̂+nK-nk+1):(nΔU+nX̂+nK)]
10231034
# --- slack variable ϵ ---
10241035
mpc.== 1 && (Z̃s[end] = mpc.Z̃[end])
10251036
JuMP.set_start_value.(Z̃var, Z̃s)
@@ -1053,12 +1064,13 @@ last control period ``k-1``, expressed as a deviation from the operating point
10531064
"""
10541065
function set_warmstart!(mpc::PredictiveController, ::TranscriptionMethod, Z̃var)
10551066
nu, nx̂, Hp, Hc, Z̃s = mpc.estim.model.nu, mpc.estim.nx̂, mpc.Hp, mpc.Hc, mpc.buffer.
1067+
nΔU, nX̂ = nu*Hc, nx̂*Hp
10561068
# --- input increments ΔU ---
1057-
Z̃s[1:(Hc*nu-nu)] .= @views mpc.Z̃[nu+1:Hc*nu]
1058-
Z̃s[(Hc*nu-nu+1):(Hc*nu)] .= 0
1069+
Z̃s[1:(nΔU-nu)] .= @views mpc.Z̃[nu+1:nΔU]
1070+
Z̃s[(nΔU-nu+1):(nΔU)] .= 0
10591071
# --- predicted states X̂0 ---
1060-
Z̃s[(Hc*nu+1):(Hc*nu+Hp*nx̂-nx̂)] .= @views mpc.Z̃[(Hc*nu+nx̂+1):(Hc*nu+Hp*nx̂)]
1061-
Z̃s[(Hc*nu+Hp*nx̂-nx̂+1):(Hc*nu+Hp*nx̂)] .= @views mpc.Z̃[(Hc*nu+Hp*nx̂-nx̂+1):(Hc*nu+Hp*nx̂)]
1072+
Z̃s[(nΔU+1):(nΔU+nX̂-nx̂)] .= @views mpc.Z̃[(nΔU+nx̂+1):(nΔU+nX̂)]
1073+
Z̃s[(nΔU+nX̂-nx̂+1):(nΔU+nX̂)] .= @views mpc.Z̃[(nΔU+nX̂-nx̂+1):(nΔU+nX̂)]
10621074
# --- slack variable ϵ ---
10631075
mpc.== 1 && (Z̃s[end] = mpc.Z̃[end])
10641076
JuMP.set_start_value.(Z̃var, Z̃s)
@@ -1427,24 +1439,27 @@ The method mutates the `geq`, `X̂0`, `Û0` and `K̇` vectors in argument. The
14271439
the deterministic state derivative at the ``n_o`` collocation points and the model dynamics
14281440
are computed by:
14291441
```math
1430-
\begin{aligned}
1431-
\mathbf{s_k}(k+j) &
1442+
\mathbf{s_k}(k+j)
14321443
= \mathbf{M_o} \begin{bmatrix}
1433-
\mathbf{k}_1(k+j) - \mathbf{x_0}(k+j) \\
1434-
\mathbf{k}_2(k+j) - \mathbf{x_0}(k+j) \\
1435-
\vdots \\
1436-
\mathbf{k}_{n_o}(k+j) - \mathbf{x_0}(k+j) \\ \end{bmatrix} \\ &\quad
1444+
\mathbf{k}_1(k+j) - \mathbf{x_0}(k+j) \\
1445+
\mathbf{k}_2(k+j) - \mathbf{x_0}(k+j) \\
1446+
\vdots \\
1447+
\mathbf{k}_{n_o}(k+j) - \mathbf{x_0}(k+j) \\ \end{bmatrix}
14371448
- \begin{bmatrix}
1438-
\mathbf{f}\Big(\mathbf{k}_1(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j), \mathbf{p}\Big) \\
1439-
\mathbf{f}\Big(\mathbf{k}_2(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j), \mathbf{p}\Big) \\
1440-
\vdots \\
1441-
\mathbf{f}\Big(\mathbf{k}_{n_o}(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j), \mathbf{p}\Big) \end{bmatrix}
1442-
\end{aligned}
1449+
\mathbf{k̇}_1(k+j) \\
1450+
\mathbf{k̇}_2(k+j) \\
1451+
\vdots \\
1452+
\mathbf{k̇}_{n_o}(k+j) \end{bmatrix}
14431453
```
14441454
for ``j = 0, 1, ... , H_p-1``, and knowing that the ``\mathbf{k}_o(k+j)`` vectors are
14451455
extracted from the decision variable `Z̃`. The ``\mathbf{x_0}`` vectors are the
1446-
deterministic state extracted from `Z̃`. The disturbed input ``\mathbf{û_0}(k+j)`` is defined
1447-
in [`f̂_input!`](@ref). The defects for the stochastic states ``\mathbf{s_s}`` are computed
1456+
deterministic state extracted from `Z̃`. The ``\mathbf{k̇}_o`` vector for the ``o``th
1457+
collocation point is computed from the continuous-time function `model.f!` and:
1458+
```math
1459+
\mathbf{k̇}_o(k+j) = \mathbf{f}\Big(\mathbf{k_o}(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j), \mathbf{p}\Big)
1460+
```
1461+
The disturbed input ``\mathbf{û_0}(k+j)`` is defined in [`f̂_input!`](@ref). The defects for
1462+
the stochastic states ``\mathbf{s_s}`` are computed
14481463
as in the [`TrapezoidalCollocation`](@ref) method, and the ones for the continuity
14491464
constraint of the deterministic state trajectories are given by:
14501465
```math
@@ -1500,7 +1515,7 @@ function con_nonlinprogeq!(
15001515
k̇o = @views k̇[(1 + (o-1)*nx):(o*nx)]
15011516
ko_Z̃ = @views k_Z̃[(1 + (o-1)*nx):(o*nx)]
15021517
Δko = @views Δk[(1 + (o-1)*nx):(o*nx)]
1503-
Δko .= ko_Z̃ .- x0_Z̃
1518+
Δko .= @. ko_Z̃ - x0_Z̃
15041519
model.f!(k̇o, ko_Z̃, û0, d̂0, p)
15051520
end
15061521
# TODO: remove the following allocations

0 commit comments

Comments
 (0)