Skip to content

Commit 842064f

Browse files
committed
changed: time-varying ECR argument with capital C
1 parent 3e35b1e commit 842064f

File tree

3 files changed

+66
-66
lines changed

3 files changed

+66
-66
lines changed

src/controller/nonlinmpc.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,10 +379,10 @@ function con_nonlinprog!(C, mpc::NonLinMPC, ::SimModel, x̂end, Ŷ, ΔŨ)
379379
mpc.con.i_C[i] || continue
380380
if i nŶ
381381
j = i
382-
C[i] = (mpc.con.Ymin[j] - Ŷ[j]) - ϵ*mpc.con.c_Ymin[j]
382+
C[i] = (mpc.con.Ymin[j] - Ŷ[j]) - ϵ*mpc.con.C_ymin[j]
383383
elseif i 2nŶ
384384
j = i - nŶ
385-
C[i] = (Ŷ[j] - mpc.con.Ymax[j]) - ϵ*mpc.con.c_Ymax[j]
385+
C[i] = (Ŷ[j] - mpc.con.Ymax[j]) - ϵ*mpc.con.C_ymax[j]
386386
elseif i 2nŶ + nx̂
387387
j = i - 2nŶ
388388
C[i] = (mpc.con.x̂min[j] -end[j]) - ϵ*mpc.con.c_x̂min[j]

src/predictive_control.jl

Lines changed: 60 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -70,8 +70,8 @@ struct ControllerConstraint
7070
A ::Matrix{Float64}
7171
b ::Vector{Float64}
7272
i_b ::BitVector
73-
c_Ymin ::Vector{Float64}
74-
c_Ymax ::Vector{Float64}
73+
C_ymin ::Vector{Float64}
74+
C_ymax ::Vector{Float64}
7575
c_x̂min ::Vector{Float64}
7676
c_x̂max ::Vector{Float64}
7777
i_C ::BitVector
@@ -121,7 +121,7 @@ constraints are all soft by default. See Extended Help for time-varying constrai
121121
- `c_x̂min = fill(1.0,nx̂)` : `x̂min` softness weights ``\mathbf{c_{x̂_{min}}}``.
122122
- `c_x̂max = fill(1.0,nx̂)` : `x̂max` softness weights ``\mathbf{c_{x̂_{max}}}``.
123123
- all the keyword arguments above but with a capital letter, except for the terminal
124-
constraints, e.g. `Ymax` or `c_ΔUmin` : for time-varying constraints (see Extended Help).
124+
constraints, e.g. `Ymax` or `C_Δumin` : for time-varying constraints (see Extended Help).
125125
126126
# Examples
127127
```jldoctest
@@ -160,15 +160,15 @@ It is also possible to specify time-varying constraints over ``H_p`` and ``H_c``
160160
In such a case, they are defined by:
161161
```math
162162
\begin{alignat*}{3}
163-
\mathbf{U_{min} - c_{U_{min}}} ϵ ≤&&\ \mathbf{U} &≤ \mathbf{U_{max} + c_{U_{max}}} ϵ \\
164-
\mathbf{ΔU_{min} - c_{ΔU_{min}}} ϵ ≤&&\ \mathbf{ΔU} &≤ \mathbf{ΔU_{max} + c_{ΔY_{max}}} ϵ \\
165-
\mathbf{Y_{min} - c_{Y_{min}}} ϵ ≤&&\ \mathbf{Ŷ} &≤ \mathbf{Y_{max} + c_{Y_{max}}} ϵ
163+
\mathbf{U_{min} - C_{u_{min}}} ϵ ≤&&\ \mathbf{U} &≤ \mathbf{U_{max} + C_{u_{max}}} ϵ \\
164+
\mathbf{ΔU_{min} - C_{Δu_{min}}} ϵ ≤&&\ \mathbf{ΔU} &≤ \mathbf{ΔU_{max} + C_{Δu_{max}}} ϵ \\
165+
\mathbf{Y_{min} - C_{y_{min}}} ϵ ≤&&\ \mathbf{Ŷ} &≤ \mathbf{Y_{max} + C_{y_{max}}} ϵ
166166
\end{alignat*}
167167
```
168168
For this, use the same keyword arguments as above but with a capital letter:
169-
- `Umin` / `Umax` / `c_Umin` / `c_Umax` : ``\mathbf{U}`` constraints `(nu*Hp,)`.
170-
- `ΔUmin` / `ΔUmax` / `c_ΔUmin` / `c_ΔUmax` : ``\mathbf{ΔU}`` constraints `(nu*Hc,)`.
171-
- `Ymin` / `Ymax` / `c_Ymin` / `c_Ymax` : ``\mathbf{Ŷ}`` constraints `(ny*Hp,)`.
169+
- `Umin` / `Umax` / `C_umin` / `C_umax` : ``\mathbf{U}`` constraints `(nu*Hp,)`.
170+
- `ΔUmin` / `ΔUmax` / `C_Δumin` / `C_Δumax` : ``\mathbf{ΔU}`` constraints `(nu*Hc,)`.
171+
- `Ymin` / `Ymax` / `C_ymin` / `C_ymax` : ``\mathbf{Ŷ}`` constraints `(ny*Hp,)`.
172172
"""
173173
function setconstraint!(
174174
mpc::PredictiveController;
@@ -183,9 +183,9 @@ function setconstraint!(
183183
Umin = nothing, Umax = nothing,
184184
ΔUmin = nothing, ΔUmax = nothing,
185185
Ymin = nothing, Ymax = nothing,
186-
c_Umax = nothing, c_Umin = nothing,
187-
c_ΔUmax = nothing, c_ΔUmin = nothing,
188-
c_Ymax = nothing, c_Ymin = nothing,
186+
C_umax = nothing, C_umin = nothing,
187+
C_Δumax = nothing, C_Δumin = nothing,
188+
C_ymax = nothing, C_ymin = nothing,
189189
# ------------ will be deleted in the future ---------------
190190
ŷmin = nothing, ŷmax = nothing,
191191
c_ŷmin = nothing, c_ŷmax = nothing,
@@ -219,13 +219,13 @@ function setconstraint!(
219219
isnothing(ΔUmax) && !isnothing(Δumax) && (ΔUmax = repeat(Δumax, Hc))
220220
isnothing(Ymin) && !isnothing(ymin) && (Ymin = repeat(ymin, Hp))
221221
isnothing(Ymax) && !isnothing(ymax) && (Ymax = repeat(ymax, Hp))
222-
isnothing(c_Umin) && !isnothing(c_umin) && (c_Umin = repeat(c_umin, Hp))
223-
isnothing(c_Umax) && !isnothing(c_umax) && (c_Umax = repeat(c_umax, Hp))
224-
isnothing(c_ΔUmin) && !isnothing(c_Δumin) && (c_ΔUmin = repeat(c_Δumin, Hc))
225-
isnothing(c_ΔUmax) && !isnothing(c_Δumax) && (c_ΔUmax = repeat(c_Δumax, Hc))
226-
isnothing(c_Ymin) && !isnothing(c_ymin) && (c_Ymin = repeat(c_ymin, Hp))
227-
isnothing(c_Ymax) && !isnothing(c_ymax) && (c_Ymax = repeat(c_ymax, Hp))
228-
if !all(isnothing.([c_Umin, c_Umax, c_ΔUmin, c_ΔUmax, c_Ymin, c_Ymax, c_x̂min, c_x̂max]))
222+
isnothing(C_umin) && !isnothing(c_umin) && (C_umin = repeat(c_umin, Hp))
223+
isnothing(C_umax) && !isnothing(c_umax) && (C_umax = repeat(c_umax, Hp))
224+
isnothing(C_Δumin) && !isnothing(c_Δumin) && (C_Δumin = repeat(c_Δumin, Hc))
225+
isnothing(C_Δumax) && !isnothing(c_Δumax) && (C_Δumax = repeat(c_Δumax, Hc))
226+
isnothing(C_ymin) && !isnothing(c_ymin) && (C_ymin = repeat(c_ymin, Hp))
227+
isnothing(C_ymax) && !isnothing(c_ymax) && (C_ymax = repeat(c_ymax, Hp))
228+
if !all(isnothing.([C_umin, C_umax, C_Δumin, C_Δumax, C_ymin, C_ymax, c_x̂min, c_x̂max]))
229229
!isinf(C) || throw(ArgumentError("Slack variable weight Cwt must be finite to set softness parameters"))
230230
notSolvedYet || error("Cannot set softness parameters after calling moveinput!")
231231
end
@@ -261,38 +261,38 @@ function setconstraint!(
261261
size(x̂max) == (nx̂,) || throw(ArgumentError("x̂max size must be $((nx̂,))"))
262262
con.x̂max[:] = x̂max
263263
end
264-
if !isnothing(c_Umin)
265-
size(c_Umin) == (nu*Hp,) || throw(ArgumentError("c_Umin size must be $((nu*Hp,))"))
266-
any(c_Umin .< 0) && error("c_Umin weights should be non-negative")
267-
con.A_Umin[:, end] = -c_Umin
264+
if !isnothing(C_umin)
265+
size(C_umin) == (nu*Hp,) || throw(ArgumentError("C_umin size must be $((nu*Hp,))"))
266+
any(C_umin .< 0) && error("C_umin weights should be non-negative")
267+
con.A_Umin[:, end] = -C_umin
268268
end
269-
if !isnothing(c_Umax)
270-
size(c_Umax) == (nu*Hp,) || throw(ArgumentError("c_Umax size must be $((nu*Hp,))"))
271-
any(c_Umax .< 0) && error("c_Umax weights should be non-negative")
272-
con.A_Umax[:, end] = -c_Umax
269+
if !isnothing(C_umax)
270+
size(C_umax) == (nu*Hp,) || throw(ArgumentError("C_umax size must be $((nu*Hp,))"))
271+
any(C_umax .< 0) && error("C_umax weights should be non-negative")
272+
con.A_Umax[:, end] = -C_umax
273273
end
274-
if !isnothing(c_ΔUmin)
275-
size(c_ΔUmin) == (nu*Hc,) || throw(ArgumentError("c_ΔUmin size must be $((nu*Hc,))"))
276-
any(c_ΔUmin .< 0) && error("c_ΔUmin weights should be non-negative")
277-
con.A_ΔŨmin[1:end-1, end] = -c_ΔUmin
274+
if !isnothing(C_Δumin)
275+
size(C_Δumin) == (nu*Hc,) || throw(ArgumentError("C_Δumin size must be $((nu*Hc,))"))
276+
any(C_Δumin .< 0) && error("C_Δumin weights should be non-negative")
277+
con.A_ΔŨmin[1:end-1, end] = -C_Δumin
278278
end
279-
if !isnothing(c_ΔUmax)
280-
size(c_ΔUmax) == (nu*Hc,) || throw(ArgumentError("c_ΔUmax size must be $((nu*Hc,))"))
281-
any(c_ΔUmax .< 0) && error("c_ΔUmax weights should be non-negative")
282-
con.A_ΔŨmax[1:end-1, end] = -c_ΔUmax
279+
if !isnothing(C_Δumax)
280+
size(C_Δumax) == (nu*Hc,) || throw(ArgumentError("C_Δumax size must be $((nu*Hc,))"))
281+
any(C_Δumax .< 0) && error("C_Δumax weights should be non-negative")
282+
con.A_ΔŨmax[1:end-1, end] = -C_Δumax
283283
end
284-
if !isnothing(c_Ymin)
285-
size(c_Ymin) == (ny*Hp,) || throw(ArgumentError("c_Ymin size must be $((ny*Hp,))"))
286-
any(c_Ymin .< 0) && error("c_Ymin weights should be non-negative")
287-
con.c_Ymin[:] = c_Ymin
288-
A_Ymin ,_ = relaxŶ(model, C, con.c_Ymin, con.c_Ymax, E)
284+
if !isnothing(C_ymin)
285+
size(C_ymin) == (ny*Hp,) || throw(ArgumentError("C_ymin size must be $((ny*Hp,))"))
286+
any(C_ymin .< 0) && error("C_ymin weights should be non-negative")
287+
con.C_ymin[:] = C_ymin
288+
A_Ymin ,_ = relaxŶ(model, C, con.C_ymin, con.C_ymax, E)
289289
con.A_Ymin[:] = A_Ymin
290290
end
291-
if !isnothing(c_Ymax)
292-
size(c_Ymax) == (ny*Hp,) || throw(ArgumentError("c_Ymax size must be $((ny*Hp,))"))
293-
any(c_Ymax .< 0) && error("c_Ymax weights should be non-negative")
294-
con.c_Ymax[:] = c_Ymax
295-
_, A_Ymax = relaxŶ(model, C, con.c_Ymin, con.c_Ymax, E)
291+
if !isnothing(C_ymax)
292+
size(C_ymax) == (ny*Hp,) || throw(ArgumentError("C_ymax size must be $((ny*Hp,))"))
293+
any(C_ymax .< 0) && error("C_ymax weights should be non-negative")
294+
con.C_ymax[:] = C_ymax
295+
_, A_Ymax = relaxŶ(model, C, con.C_ymin, con.C_ymax, E)
296296
con.A_Ymax[:] = A_Ymax
297297
end
298298
if !isnothing(c_x̂min)
@@ -1017,11 +1017,11 @@ function init_defaultcon(estim, Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, k
10171017
c_x̂min, c_x̂max = fill(0.0, nx̂), fill(0.0, nx̂)
10181018
Umin, Umax, ΔUmin, ΔUmax, Ymin, Ymax =
10191019
repeat_constraints(Hp, Hc, umin, umax, Δumin, Δumax, ymin, ymax)
1020-
c_Umin, c_Umax, c_ΔUmin, c_ΔUmax, c_Ymin, c_Ymax =
1020+
C_umin, C_umax, C_Δumin, C_Δumax, C_ymin, C_ymax =
10211021
repeat_constraints(Hp, Hc, c_umin, c_umax, c_Δumin, c_Δumax, c_ymin, c_ymax)
1022-
A_Umin, A_Umax, S̃ = relaxU(C, c_Umin, c_Umax, S)
1023-
A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc = relaxΔU(C, c_ΔUmin, c_ΔUmax, ΔUmin, ΔUmax, N_Hc)
1024-
A_Ymin, A_Ymax, Ẽ = relaxŶ(model, C, c_Ymin, c_Ymax, E)
1022+
A_Umin, A_Umax, S̃ = relaxU(C, C_umin, C_umax, S)
1023+
A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc = relaxΔU(C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc)
1024+
A_Ymin, A_Ymax, Ẽ = relaxŶ(model, C, C_ymin, C_ymax, E)
10251025
A_x̂min, A_x̂max, ẽx̂ = relaxterminal(model, C, c_x̂min, c_x̂max, ex̂)
10261026
i_Umin, i_Umax = .!isinf.(Umin), .!isinf.(Umax)
10271027
i_ΔŨmin, i_ΔŨmax = .!isinf.(ΔŨmin), .!isinf.(ΔŨmax)
@@ -1037,7 +1037,7 @@ function init_defaultcon(estim, Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, k
10371037
ẽx̂ , fx̂ , gx̂ , jx̂ , kx̂ , vx̂ ,
10381038
Umin , Umax , ΔŨmin , ΔŨmax , Ymin , Ymax , x̂min , x̂max,
10391039
A_Umin , A_Umax, A_ΔŨmin, A_ΔŨmax , A_Ymin , A_Ymax , A_x̂min , A_x̂max,
1040-
A , b , i_b , c_Ymin , c_Ymax , c_x̂min , c_x̂max , i_C
1040+
A , b , i_b , C_ymin , C_ymax , c_x̂min , c_x̂max , i_C
10411041
)
10421042
return con, S̃, Ñ_Hc, Ẽ
10431043
end
@@ -1054,7 +1054,7 @@ function repeat_constraints(Hp, Hc, umin, umax, Δumin, Δumax, ymin, ymax)
10541054
end
10551055

10561056
@doc raw"""
1057-
relaxU(C, c_Umin, c_Umax, S) -> A_Umin, A_Umax, S̃
1057+
relaxU(C, C_umin, C_umax, S) -> A_Umin, A_Umax, S̃
10581058
10591059
Augment manipulated inputs constraints with slack variable ϵ for softening.
10601060
@@ -1074,10 +1074,10 @@ constraints:
10741074
\end{bmatrix}
10751075
```
10761076
"""
1077-
function relaxU(C, c_Umin, c_Umax, S)
1077+
function relaxU(C, C_umin, C_umax, S)
10781078
if !isinf(C) # ΔŨ = [ΔU; ϵ]
10791079
# ϵ impacts ΔU → U conversion for constraint calculations:
1080-
A_Umin, A_Umax = -[S c_Umin], [S -c_Umax]
1080+
A_Umin, A_Umax = -[S C_umin], [S -C_umax]
10811081
# ϵ has no impact on ΔU → U conversion for prediction calculations:
10821082
= [S falses(size(S, 1))]
10831083
else # ΔŨ = ΔU (only hard constraints)
@@ -1088,7 +1088,7 @@ function relaxU(C, c_Umin, c_Umax, S)
10881088
end
10891089

10901090
@doc raw"""
1091-
relaxΔU(C, c_ΔUmin, c_ΔUmax, ΔUmin, ΔUmax, N_Hc) -> A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc
1091+
relaxΔU(C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc) -> A_ΔŨmin, A_ΔŨmax, ΔŨmin, ΔŨmax, Ñ_Hc
10921092
10931093
Augment input increments constraints with slack variable ϵ for softening.
10941094
@@ -1108,12 +1108,12 @@ returns the augmented constraints ``\mathbf{ΔŨ_{min}}`` and ``\mathbf{ΔŨ_{
11081108
\end{bmatrix}
11091109
```
11101110
"""
1111-
function relaxΔU(C, c_ΔUmin, c_ΔUmax, ΔUmin, ΔUmax, N_Hc)
1111+
function relaxΔU(C, C_Δumin, C_Δumax, ΔUmin, ΔUmax, N_Hc)
11121112
if !isinf(C) # ΔŨ = [ΔU; ϵ]
11131113
# 0 ≤ ϵ ≤ ∞
11141114
ΔŨmin, ΔŨmax = [ΔUmin; 0.0], [ΔUmax; Inf]
11151115
A_ϵ = [zeros(1, length(ΔUmin)) [1]]
1116-
A_ΔŨmin, A_ΔŨmax = -[I c_ΔUmin; A_ϵ], [I -c_ΔUmax; A_ϵ]
1116+
A_ΔŨmin, A_ΔŨmax = -[I C_Δumin; A_ϵ], [I -C_Δumax; A_ϵ]
11171117
Ñ_Hc = Diagonal([diag(N_Hc); C])
11181118
else # ΔŨ = ΔU (only hard constraints)
11191119
ΔŨmin, ΔŨmax = ΔUmin, ΔUmax
@@ -1125,7 +1125,7 @@ function relaxΔU(C, c_ΔUmin, c_ΔUmax, ΔUmin, ΔUmax, N_Hc)
11251125
end
11261126

11271127
@doc raw"""
1128-
relaxŶ(::LinModel, C, c_Ymin, c_Ymax, E) -> A_Ymin, A_Ymax, Ẽ
1128+
relaxŶ(::LinModel, C, C_ymin, C_ymax, E) -> A_Ymin, A_Ymax, Ẽ
11291129
11301130
Augment linear output prediction constraints with slack variable ϵ for softening.
11311131
@@ -1144,10 +1144,10 @@ Denoting the input increments augmented with the slack variable
11441144
\end{bmatrix}
11451145
```
11461146
"""
1147-
function relaxŶ(::LinModel, C, c_Ymin, c_Ymax, E)
1147+
function relaxŶ(::LinModel, C, C_ymin, C_ymax, E)
11481148
if !isinf(C) # ΔŨ = [ΔU; ϵ]
11491149
# ϵ impacts predicted output constraint calculations:
1150-
A_Ymin, A_Ymax = -[E c_Ymin], [E -c_Ymax]
1150+
A_Ymin, A_Ymax = -[E C_ymin], [E -C_ymax]
11511151
# ϵ has no impact on output predictions
11521152
= [E zeros(size(E, 1), 1)]
11531153
else # ΔŨ = ΔU (only hard constraints)
@@ -1158,7 +1158,7 @@ function relaxŶ(::LinModel, C, c_Ymin, c_Ymax, E)
11581158
end
11591159

11601160
"Return empty matrices if model is not a [`LinModel`](@ref)"
1161-
function relaxŶ(::SimModel, C, c_Ymin, c_Ymax, E)
1161+
function relaxŶ(::SimModel, C, C_ymin, C_ymax, E)
11621162
= !isinf(C) ? [E zeros(0, 1)] : E
11631163
A_Ymin, A_Ymax = -Ẽ, Ẽ
11641164
return A_Ymin, A_Ymax, Ẽ

test/test_predictive_control.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -148,11 +148,11 @@ end
148148
setconstraint!(mpc2, Ymin=-1(1:50).-3, Ymax=+1(1:50).+3)
149149
@test all((mpc2.con.Ymin, mpc2.con.Ymax) .≈ (-1(1:50).-3, +1(1:50).+3))
150150

151-
setconstraint!(mpc2, c_Umin=+1(1:50).+4, c_Umax=+1(1:50).+4)
151+
setconstraint!(mpc2, C_umin=+1(1:50).+4, C_umax=+1(1:50).+4)
152152
@test all((-mpc2.con.A_Umin[:, end], -mpc2.con.A_Umax[:, end]) .≈ (+1(1:50).+4, +1(1:50).+4))
153-
setconstraint!(mpc2, c_ΔUmin=+1(1:5).+5, c_ΔUmax=+1(1:5).+5)
153+
setconstraint!(mpc2, C_Δumin=+1(1:5).+5, C_Δumax=+1(1:5).+5)
154154
@test all((-mpc2.con.A_ΔŨmin[1:end-1, end], -mpc2.con.A_ΔŨmax[1:end-1, end]) .≈ (+1(1:5).+5, +1(1:5).+5))
155-
setconstraint!(mpc2, c_Ymin=+1(1:50).+6, c_Ymax=+1(1:50).+6)
155+
setconstraint!(mpc2, C_ymin=+1(1:50).+6, C_ymax=+1(1:50).+6)
156156
@test all((-mpc2.con.A_Ymin[:, end], -mpc2.con.A_Ymax[:, end]) .≈ (+1(1:50).+6, +1(1:50).+6))
157157
setconstraint!(mpc2, c_umin=[0], c_umax=[0], c_Δumin=[0], c_Δumax=[0], c_ymin=[1], c_ymax=[1])
158158

@@ -479,7 +479,7 @@ end
479479
@test all((-nmpc.con.A_ΔŨmin[1:end-1, end], -nmpc.con.A_ΔŨmax[1:end-1, end]) .≈ ([0.05,0.06], [0.07,0.08]))
480480
setconstraint!(nmpc, c_ymin=[1.00,1.01], c_ymax=[1.02,1.03])
481481
@test all((-nmpc.con.A_Ymin, -nmpc.con.A_Ymax) .≈ (zeros(0,3), zeros(0,3)))
482-
@test all((nmpc.con.c_Ymin, nmpc.con.c_Ymax) .≈ ([1.00,1.01], [1.02,1.03]))
482+
@test all((nmpc.con.C_ymin, nmpc.con.C_ymax) .≈ ([1.00,1.01], [1.02,1.03]))
483483
setconstraint!(nmpc, c_x̂min=[0.21,0.22,0.23,0.24,0.25,0.26], c_x̂max=[0.31,0.32,0.33,0.34,0.35,0.36])
484484
@test all((nmpc.con.c_x̂min, nmpc.con.c_x̂max) .≈ ([0.21,0.22,0.23,0.24,0.25,0.26], [0.31,0.32,0.33,0.34,0.35,0.36]))
485485

0 commit comments

Comments
 (0)