Skip to content

Commit 20d20d9

Browse files
committed
added: parse Hc argument for move blocking
1 parent 025d847 commit 20d20d9

File tree

5 files changed

+24
-17
lines changed

5 files changed

+24
-17
lines changed

src/controller/construct.jl

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -453,6 +453,12 @@ end
453453
"Return `0` when model is not a [`LinModel`](@ref)."
454454
estimate_delays(::SimModel) = 0
455455

456+
457+
"Get move blocking vector `nb` and actual `Hc` value from provided `Hc_arg` argument"
458+
move_blocking(Hc_arg::AbstractVector{Int}) = (Hc_arg, length(nb))
459+
move_blocking(Hc_arg::Int) = (fill(1, length(Hc_arg)), Hc_arg)
460+
461+
456462
"""
457463
validate_args(mpc::PredictiveController, ry, d, D̂, R̂y, R̂u)
458464

src/controller/explicitmpc.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct ExplicitMPC{
3939
Dop::Vector{NT}
4040
buffer::PredictiveControllerBuffer{NT}
4141
function ExplicitMPC{NT}(
42-
estim::SE, Hp, Hc, weights::CW
42+
estim::SE, Hp, Hc, nb, weights::CW
4343
) where {NT<:Real, SE<:StateEstimator, CW<:ControllerWeights}
4444
model = estim.model
4545
nu, ny, nd, nx̂ = model.nu, model.ny, model.nd, estim.nx̂
@@ -48,7 +48,6 @@ struct ExplicitMPC{
4848
# dummy vals (updated just before optimization):
4949
R̂y, R̂u, Tu_lastu0 = zeros(NT, ny*Hp), zeros(NT, nu*Hp), zeros(NT, nu*Hp)
5050
transcription = SingleShooting() # explicit MPC only supports SingleShooting
51-
nb = ones(Hc)
5251
PΔu = init_ZtoΔU(estim, transcription, Hp, Hc)
5352
Pu, Tu = init_ZtoU(estim, transcription, Hp, Hc)
5453
E, G, J, K, V, B = init_predmat(model, estim, transcription, Hp, Hc)
@@ -128,7 +127,7 @@ ExplicitMPC controller with a sample time Ts = 4.0 s, SteadyKalmanFilter estimat
128127
function ExplicitMPC(
129128
model::LinModel;
130129
Hp::Int = default_Hp(model),
131-
Hc::Int = DEFAULT_HC,
130+
Hc::IntVectorOrInt = DEFAULT_HC,
132131
Mwt = fill(DEFAULT_MWT, model.ny),
133132
Nwt = fill(DEFAULT_NWT, model.nu),
134133
Lwt = fill(DEFAULT_LWT, model.nu),
@@ -151,7 +150,7 @@ Use custom state estimator `estim` to construct `ExplicitMPC`.
151150
function ExplicitMPC(
152151
estim::SE;
153152
Hp::Int = default_Hp(estim.model),
154-
Hc::Int = DEFAULT_HC,
153+
Hc::IntVectorOrInt = DEFAULT_HC,
155154
Mwt = fill(DEFAULT_MWT, estim.model.ny),
156155
Nwt = fill(DEFAULT_NWT, estim.model.nu),
157156
Lwt = fill(DEFAULT_LWT, estim.model.nu),
@@ -165,8 +164,9 @@ function ExplicitMPC(
165164
@warn("prediction horizon Hp ($Hp) ≤ estimated number of delays in model "*
166165
"($nk), the closed-loop system may be unstable or zero-gain (unresponsive)")
167166
end
167+
nb, Hc = move_blocking(Hc)
168168
weights = ControllerWeights{NT}(estim.model, Hp, Hc, M_Hp, N_Hc, L_Hp)
169-
return ExplicitMPC{NT}(estim, Hp, Hc, weights)
169+
return ExplicitMPC{NT}(estim, Hp, Hc, nb, weights)
170170
end
171171

172172
setconstraint!(::ExplicitMPC; kwargs...) = error("ExplicitMPC does not support constraints.")

src/controller/linmpc.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ struct LinMPC{
4747
Dop::Vector{NT}
4848
buffer::PredictiveControllerBuffer{NT}
4949
function LinMPC{NT}(
50-
estim::SE, Hp, Hc, weights::CW,
50+
estim::SE, Hp, Hc, nb, weights::CW,
5151
transcription::TM, optim::JM
5252
) where {
5353
NT<:Real,
@@ -61,7 +61,6 @@ struct LinMPC{
6161
= copy(model.yop) # dummy vals (updated just before optimization)
6262
# dummy vals (updated just before optimization):
6363
R̂y, R̂u, Tu_lastu0 = zeros(NT, ny*Hp), zeros(NT, nu*Hp), zeros(NT, nu*Hp)
64-
nb = ones(Hc)
6564
PΔu = init_ZtoΔU(estim, transcription, Hp, Hc)
6665
Pu, Tu = init_ZtoU(estim, transcription, Hp, Hc)
6766
E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = init_predmat(
@@ -204,7 +203,7 @@ LinMPC controller with a sample time Ts = 4.0 s, OSQP optimizer, SteadyKalmanFil
204203
function LinMPC(
205204
model::LinModel;
206205
Hp::Int = default_Hp(model),
207-
Hc::Int = DEFAULT_HC,
206+
Hc::IntVectorOrInt = DEFAULT_HC,
208207
Mwt = fill(DEFAULT_MWT, model.ny),
209208
Nwt = fill(DEFAULT_NWT, model.nu),
210209
Lwt = fill(DEFAULT_LWT, model.nu),
@@ -247,7 +246,7 @@ LinMPC controller with a sample time Ts = 4.0 s, OSQP optimizer, KalmanFilter es
247246
function LinMPC(
248247
estim::SE;
249248
Hp::Int = default_Hp(estim.model),
250-
Hc::Int = DEFAULT_HC,
249+
Hc::IntVectorOrInt = DEFAULT_HC,
251250
Mwt = fill(DEFAULT_MWT, estim.model.ny),
252251
Nwt = fill(DEFAULT_NWT, estim.model.nu),
253252
Lwt = fill(DEFAULT_LWT, estim.model.nu),
@@ -264,8 +263,9 @@ function LinMPC(
264263
@warn("prediction horizon Hp ($Hp) ≤ estimated number of delays in model "*
265264
"($nk), the closed-loop system may be unstable or zero-gain (unresponsive)")
266265
end
266+
nb, Hc = move_blocking(Hc)
267267
weights = ControllerWeights{NT}(estim.model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt)
268-
return LinMPC{NT}(estim, Hp, Hc, weights, transcription, optim)
268+
return LinMPC{NT}(estim, Hp, Hc, nb, weights, transcription, optim)
269269
end
270270

271271
"""

src/controller/nonlinmpc.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ struct NonLinMPC{
6363
Dop::Vector{NT}
6464
buffer::PredictiveControllerBuffer{NT}
6565
function NonLinMPC{NT}(
66-
estim::SE, Hp, Hc, weights::CW,
66+
estim::SE, Hp, Hc, nb, weights::CW,
6767
JE::JEfunc, gc!::GCfunc, nc, p::PT,
6868
transcription::TM, optim::JM,
6969
gradient::GB, jacobian::JB
@@ -84,7 +84,6 @@ struct NonLinMPC{
8484
= copy(model.yop) # dummy vals (updated just before optimization)
8585
# dummy vals (updated just before optimization):
8686
R̂y, R̂u, Tu_lastu0 = zeros(NT, ny*Hp), zeros(NT, nu*Hp), zeros(NT, nu*Hp)
87-
nb = ones(Hc)
8887
PΔu = init_ZtoΔU(estim, transcription, Hp, Hc)
8988
Pu, Tu = init_ZtoU(estim, transcription, Hp, Hc)
9089
E, G, J, K, V, B, ex̂, gx̂, jx̂, kx̂, vx̂, bx̂ = init_predmat(
@@ -280,7 +279,7 @@ NonLinMPC controller with a sample time Ts = 10.0 s, Ipopt optimizer, UnscentedK
280279
function NonLinMPC(
281280
model::SimModel;
282281
Hp::Int = default_Hp(model),
283-
Hc::Int = DEFAULT_HC,
282+
Hc::IntVectorOrInt = DEFAULT_HC,
284283
Mwt = fill(DEFAULT_MWT, model.ny),
285284
Nwt = fill(DEFAULT_NWT, model.nu),
286285
Lwt = fill(DEFAULT_LWT, model.nu),
@@ -337,7 +336,7 @@ NonLinMPC controller with a sample time Ts = 10.0 s, Ipopt optimizer, UnscentedK
337336
function NonLinMPC(
338337
estim::SE;
339338
Hp::Int = default_Hp(estim.model),
340-
Hc::Int = DEFAULT_HC,
339+
Hc::IntVectorOrInt = DEFAULT_HC,
341340
Mwt = fill(DEFAULT_MWT, estim.model.ny),
342341
Nwt = fill(DEFAULT_NWT, estim.model.nu),
343342
Lwt = fill(DEFAULT_LWT, estim.model.nu),
@@ -364,11 +363,12 @@ function NonLinMPC(
364363
@warn("prediction horizon Hp ($Hp) ≤ estimated number of delays in model "*
365364
"($nk), the closed-loop system may be unstable or zero-gain (unresponsive)")
366365
end
366+
nb, Hc = move_blocking(Hc)
367367
validate_JE(NT, JE)
368368
gc! = get_mutating_gc(NT, gc)
369369
weights = ControllerWeights{NT}(estim.model, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt)
370370
return NonLinMPC{NT}(
371-
estim, Hp, Hc, weights, JE, gc!, nc, p, transcription, optim, gradient, jacobian
371+
estim, Hp, Hc, nb, weights, JE, gc!, nc, p, transcription, optim, gradient, jacobian
372372
)
373373
end
374374

src/controller/transcription.jl

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ abstract type TranscriptionMethod end
1010
1111
Construct a direct single shooting [`TranscriptionMethod`](@ref).
1212
13-
The decision variable in the optimization problem is (excluding the slack ``ϵ``):
13+
The decision variable in the optimization problem is (excluding the slack ``ϵ`` and without
14+
any custom move blocking):
1415
```math
1516
\mathbf{Z} = \mathbf{ΔU} = \begin{bmatrix}
1617
\mathbf{Δu}(k+0) \\
@@ -66,7 +67,7 @@ end
6667
Init decision variables to input increments over ``H_c`` conversion matrix `PΔu`.
6768
6869
Introducing the move blocking vector that specifies the length of each step in the
69-
``\mathbf{ΔU}`` vector to customize the pattern (strictly positive integers):
70+
``\mathbf{ΔU}`` vector, to customize the pattern (strictly positive integers):
7071
7172
```math
7273
\mathbf{n_b} = \begin{bmatrix} n_1 & n_2 & \cdots & n_{H_c} \end{bmatrix}'

0 commit comments

Comments
 (0)