Skip to content

Commit a54f384

Browse files
committed
added: MultipleShooting stochastic defects as linear eq. constraints
1 parent 1772b94 commit a54f384

3 files changed

Lines changed: 27 additions & 29 deletions

File tree

docs/src/internals/predictive_control.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ ModelPredictiveControl.linconstrainteq!
4545
ModelPredictiveControl.optim_objective!(::PredictiveController)
4646
ModelPredictiveControl.set_warmstart!
4747
ModelPredictiveControl.predict!
48+
ModelPredictiveControl.disturbedinput!
4849
ModelPredictiveControl.con_nonlinprog!
4950
ModelPredictiveControl.con_nonlinprogeq!
5051
ModelPredictiveControl.getinput!

src/controller/execute.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -323,11 +323,11 @@ Both `Û0` and `U0` variables include deviation vectors from ``k+0`` to ``k+H_p
323323
predicted states `X̂0` include deviation vectors from ``k+1`` to ``k+H_p-1`` (the current one
324324
is stored in `estim.x̂0`).
325325
326-
This function is used for the collocation methods that directly call the state derivative
326+
This function is needed for the collocation methods that directly call the state derivative
327327
function `estim.model.f!` with the manipulated inputs augmented with the estimated
328328
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.
329+
the `Û0` vector before anything else since both `û0` and `û0next` are needed at each stage
330+
with hold order `h>0`, thus potential race conditions with multi-threading.
331331
"""
332332
function disturbedinput!(Û0, mpc::PredictiveController, estim::StateEstimator, U0, X̂0)
333333
nu, nx, nx̂ = estim.model.nu, estim.model.nx, estim.nx̂

src/controller/transcription.jl

Lines changed: 23 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1403,21 +1403,26 @@ end
14031403
14041404
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`MultipleShooting`](@ref).
14051405
1406-
The method mutates the `geq`, `X̂0`, `Û0` and `K` vectors in argument. The nonlinear
1407-
equality constraints `geq` only includes the augmented state defects, computed with:
1406+
The method mutates the `geq`, `X̂0`, `Û0` and `K` vectors in argument. The defects of the
1407+
stochastic states are linear equality constraints (see [`init_defectmat`](@ref)). The
1408+
defects of the deterministic states are computed with:
1409+
with:
14081410
```math
1409-
\mathbf{}(k+j+1) = \mathbf{}\Big(\mathbf{x̂_0}(k+j), \mathbf{u_0}(k+j), \mathbf{d̂_0}(k+j)\Big)
1410-
- \mathbf{x̂_0}(k+j+1)
1411+
\mathbf{ŝ_d}(k+j+1) = \mathbf{f}\Big(\mathbf{x_0}(k+j), \mathbf{û_0}(k+j), \mathbf{d̂_0}(k+j)\Big)
1412+
- \mathbf{x_0}(k+j+1)
14111413
```
1412-
for ``j = 0, 1, ... , H_p-1``, and in which the augmented state ``\mathbf{x̂_0}`` are
1413-
extracted from the decision variables `Z̃`, and ``\mathbf{f̂}`` is the augmented state
1414-
function defined in [`f̂!`](@ref).
1414+
for ``j = 0, 1, ... , H_p-1``, and in which the deterministic state ``\mathbf{x_0}`` are
1415+
extracted from the decision variables `Z̃`, ``\mathbf{f}`` is the deterministic state update
1416+
function [`f!`](@ref), the disturbed input ``\mathbf{û_0}`` is defined in [`f̂!`](@ref)
1417+
documentation. The defects of the stochastic states are linear equality constraints (see
1418+
[`init_defectmat`](@ref)).
14151419
"""
14161420
function con_nonlinprogeq!(
14171421
geq, X̂0, Û0, K,
1418-
mpc::PredictiveController, model::NonLinModel, transcription::MultipleShooting, U0, Z̃
1422+
mpc::PredictiveController, model::NonLinModel, transcription::MultipleShooting,
1423+
U0, Z̃
14191424
)
1420-
nu, nx̂, nd, nk = model.nu, mpc.estim.nx̂, model.nd, model.nk
1425+
nx̂, nx, nu, nd, nk = mpc.estim.nx̂, model.nx, model.nu, model.nd, model.nk
14211426
Hp, Hc = mpc.Hp, mpc.Hc
14221427
nΔU, nX̂ = nu*Hc, nx̂*Hp
14231428
f_threads = transcription.f_threads
@@ -1426,25 +1431,19 @@ function con_nonlinprogeq!(
14261431
disturbedinput!(Û0, mpc, mpc.estim, U0, X̂0_Z̃)
14271432
@threadsif f_threads for j=1:Hp
14281433
if j < 2
1429-
x̂0_Z̃ = @views mpc.estim.x̂0[1:nx̂]
1434+
x0_Z̃ = @views mpc.estim.x̂0[1:nx]
14301435
d̂0 = @views mpc.d0[1:nd]
14311436
else
1432-
x̂0_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-2)):(nx̂*(j-1))]
1437+
x0_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-2)):(nx + nx̂*(j-2))]
14331438
d̂0 = @views D̂0[(1 + nd*(j-2)):(nd*(j-1))]
14341439
end
14351440
û0 = @views Û0[(1 + nu*(j-1)):(nu*j)]
14361441
k = @views K[(1 + nk*(j-1)):(nk*j)]
1437-
x̂0next = @views X̂0[(1 + nx̂*(j-1)):(nx̂*j)]
1438-
x̂0next_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-1)):(nx̂*j)]
1439-
ŝnext = @views geq[(1 + nx̂*(j-1)):(nx̂*j)]
1440-
f̂!(x̂0next, û0, k, mpc.estim, model, x̂0_Z̃, u0, d̂0)
1441-
1442-
@views xdnext = x̂0next[1:model.nx], x̂0next[model.nx+1:end]
1443-
1444-
f!(xdnext, k, model, xd, û0, d0, model.p)
1445-
1446-
1447-
ŝnext .= @. x̂0next - x̂0next_Z̃
1442+
x0next = @views X̂0[(1 + nx̂*(j-1)):(nx + nx̂*(j-1))]
1443+
x0next_Z̃ = @views X̂0_Z̃[(1 + nx̂*(j-1)):(nx + nx̂*(j-1))]
1444+
sdnext = @views geq[(1 + nx*(j-1)):(nx*j)]
1445+
f!(x0next, k, model, x0_Z̃, û0, d̂0, model.p)
1446+
sdnext .= @. x0next - x0next_Z̃
14481447
end
14491448
return geq
14501449
end
@@ -1458,10 +1457,8 @@ end
14581457
14591458
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`TrapezoidalCollocation`](@ref).
14601459
1461-
The method mutates the `geq`, `X̂0`, `Û0` and `K̇` vectors in argument. The nonlinear equality
1462-
constraints `geq` includes the defects of the deterministic states only. The stochastic
1463-
states are handled seperatly as linear equality constraints, see [`init_defectmat`](@ref).
1464-
The deterministic state defects are computed with:
1460+
The method mutates the `geq`, `X̂0`, `Û0` and `K̇` vectors in argument. The deterministic
1461+
state defects are computed with:
14651462
```math
14661463
\mathbf{s_d}(k+j+1) = \mathbf{x_0}(k+j) + 0.5 T_s [\mathbf{k̇}_1(k+j) + \mathbf{k̇}_2(k+j)]
14671464
- \mathbf{x_0}(k+j+1)

0 commit comments

Comments
 (0)