Skip to content

Commit 11d59a6

Browse files
committed
renamed: nonlin constraint vector C -> g
1 parent 842064f commit 11d59a6

File tree

4 files changed

+63
-63
lines changed

4 files changed

+63
-63
lines changed

src/controller/nonlinmpc.jl

Lines changed: 42 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -258,21 +258,21 @@ function init_optimization!(mpc::NonLinMPC)
258258
@constraint(optim, linconstraint, A*ΔŨvar .≤ b)
259259
# --- nonlinear optimization init ---
260260
model = mpc.estim.model
261-
ny, nx̂, Hp, nC = model.ny, mpc.estim.nx̂, mpc.Hp, length(con.i_C)
261+
ny, nx̂, Hp, ng = model.ny, mpc.estim.nx̂, mpc.Hp, length(con.i_g)
262262
# inspired from https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/tips_and_tricks/#User-defined-operators-with-vector-outputs
263-
Jfunc, Cfunc = let mpc=mpc, model=model, nC=nC, nvar=nvar , nŶ=Hp*ny, nx̂=nx̂
263+
Jfunc, gfunc = let mpc=mpc, model=model, ng=ng, nvar=nvar , nŶ=Hp*ny, nx̂=nx̂
264264
last_ΔŨtup_float, last_ΔŨtup_dual = nothing, nothing
265265
Ŷ_cache::DiffCacheType = DiffCache(zeros(nŶ), nvar + 3)
266-
C_cache::DiffCacheType = DiffCache(zeros(nC), nvar + 3)
266+
g_cache::DiffCacheType = DiffCache(zeros(ng), nvar + 3)
267267
x̂_cache::DiffCacheType = DiffCache(zeros(nx̂), nvar + 3)
268268
function Jfunc(ΔŨtup::Float64...)
269269
= get_tmp(Ŷ_cache, ΔŨtup[1])
270270
ΔŨ = collect(ΔŨtup)
271271
if ΔŨtup != last_ΔŨtup_float
272272
= get_tmp(x̂_cache, ΔŨtup[1])
273-
C = get_tmp(C_cache, ΔŨtup[1])
273+
g = get_tmp(g_cache, ΔŨtup[1])
274274
Ŷ, x̂end = predict!(Ŷ, x̂, mpc, model, ΔŨ)
275-
con_nonlinprog!(C, mpc, model, x̂end, Ŷ, ΔŨ)
275+
con_nonlinprog!(g, mpc, model, x̂end, Ŷ, ΔŨ)
276276
last_ΔŨtup_float = ΔŨtup
277277
end
278278
return obj_nonlinprog(mpc, model, Ŷ, ΔŨ)
@@ -282,59 +282,59 @@ function init_optimization!(mpc::NonLinMPC)
282282
ΔŨ = collect(ΔŨtup)
283283
if ΔŨtup != last_ΔŨtup_dual
284284
= get_tmp(x̂_cache, ΔŨtup[1])
285-
C = get_tmp(C_cache, ΔŨtup[1])
285+
g = get_tmp(g_cache, ΔŨtup[1])
286286
Ŷ, x̂end = predict!(Ŷ, x̂, mpc, model, ΔŨ)
287-
con_nonlinprog!(C, mpc, model, x̂end, Ŷ, ΔŨ)
287+
con_nonlinprog!(g, mpc, model, x̂end, Ŷ, ΔŨ)
288288
last_ΔŨtup_dual = ΔŨtup
289289
end
290290
return obj_nonlinprog(mpc, model, Ŷ, ΔŨ)
291291
end
292-
function con_nonlinprog_i(i, ΔŨtup::NTuple{N, Float64}) where {N}
293-
C = get_tmp(C_cache, ΔŨtup[1])
292+
function gfunc_i(i, ΔŨtup::NTuple{N, Float64}) where {N}
293+
g = get_tmp(g_cache, ΔŨtup[1])
294294
if ΔŨtup != last_ΔŨtup_float
295295
= get_tmp(x̂_cache, ΔŨtup[1])
296296
= get_tmp(Ŷ_cache, ΔŨtup[1])
297297
ΔŨ = collect(ΔŨtup)
298298
Ŷ, x̂end = predict!(Ŷ, x̂, mpc, model, ΔŨ)
299-
C = con_nonlinprog!(C, mpc, model, x̂end, Ŷ, ΔŨ)
299+
g = con_nonlinprog!(g, mpc, model, x̂end, Ŷ, ΔŨ)
300300
last_ΔŨtup_float = ΔŨtup
301301
end
302-
return C[i]
302+
return g[i]
303303
end
304-
function con_nonlinprog_i(i, ΔŨtup::NTuple{N, Real}) where {N}
305-
C = get_tmp(C_cache, ΔŨtup[1])
304+
function gfunc_i(i, ΔŨtup::NTuple{N, Real}) where {N}
305+
g = get_tmp(g_cache, ΔŨtup[1])
306306
if ΔŨtup != last_ΔŨtup_dual
307307
= get_tmp(x̂_cache, ΔŨtup[1])
308308
= get_tmp(Ŷ_cache, ΔŨtup[1])
309309
ΔŨ = collect(ΔŨtup)
310310
Ŷ, x̂end = predict!(Ŷ, x̂, mpc, model, ΔŨ)
311-
C = con_nonlinprog!(C, mpc, model, x̂end, Ŷ, ΔŨ)
311+
g = con_nonlinprog!(g, mpc, model, x̂end, Ŷ, ΔŨ)
312312
last_ΔŨtup_dual = ΔŨtup
313313
end
314-
return C[i]
314+
return g[i]
315315
end
316-
Cfunc = [(ΔŨ...) -> con_nonlinprog_i(i, ΔŨ) for i in 1:nC]
317-
(Jfunc, Cfunc)
316+
gfunc = [(ΔŨ...) -> gfunc_i(i, ΔŨ) for i in 1:ng]
317+
(Jfunc, gfunc)
318318
end
319319
register(optim, :Jfunc, nvar, Jfunc, autodiff=true)
320320
@NLobjective(optim, Min, Jfunc(ΔŨvar...))
321-
if nC 0
321+
if ng 0
322322
i_end_Ymin, i_end_Ymax, i_end_x̂min = 1Hp*ny, 2Hp*ny, 2Hp*ny + nx̂
323323
for i in eachindex(con.Ymin)
324-
sym = Symbol("C_Ymin_$i")
325-
register(optim, sym, nvar, Cfunc[i], autodiff=true)
324+
sym = Symbol("g_Ymin_$i")
325+
register(optim, sym, nvar, gfunc[i], autodiff=true)
326326
end
327327
for i in eachindex(con.Ymax)
328-
sym = Symbol("C_Ymax_$i")
329-
register(optim, sym, nvar, Cfunc[i_end_Ymin+i], autodiff=true)
328+
sym = Symbol("g_Ymax_$i")
329+
register(optim, sym, nvar, gfunc[i_end_Ymin+i], autodiff=true)
330330
end
331331
for i in eachindex(con.x̂min)
332-
sym = Symbol("C_x̂min_$i")
333-
register(optim, sym, nvar, Cfunc[i_end_Ymax+i], autodiff=true)
332+
sym = Symbol("g_x̂min_$i")
333+
register(optim, sym, nvar, gfunc[i_end_Ymax+i], autodiff=true)
334334
end
335335
for i in eachindex(con.x̂max)
336-
sym = Symbol("C_x̂max_$i")
337-
register(optim, sym, nvar, Cfunc[i_end_x̂min+i], autodiff=true)
336+
sym = Symbol("g_x̂max_$i")
337+
register(optim, sym, nvar, gfunc[i_end_x̂min+i], autodiff=true)
338338
end
339339
end
340340
return nothing
@@ -347,52 +347,52 @@ function setnonlincon!(mpc::NonLinMPC, ::NonLinModel)
347347
con = mpc.con
348348
map(con -> delete(optim, con), all_nonlinear_constraints(optim))
349349
for i in findall(.!isinf.(con.Ymin))
350-
f_sym = Symbol("C_Ymin_$(i)")
350+
f_sym = Symbol("g_Ymin_$(i)")
351351
add_nonlinear_constraint(optim, :($(f_sym)($(ΔŨvar...)) <= 0))
352352
end
353353
for i in findall(.!isinf.(con.Ymax))
354-
f_sym = Symbol("C_Ymax_$(i)")
354+
f_sym = Symbol("g_Ymax_$(i)")
355355
add_nonlinear_constraint(optim, :($(f_sym)($(ΔŨvar...)) <= 0))
356356
end
357357
for i in findall(.!isinf.(con.x̂min))
358-
f_sym = Symbol("C_x̂min_$(i)")
358+
f_sym = Symbol("g_x̂min_$(i)")
359359
add_nonlinear_constraint(optim, :($(f_sym)($(ΔŨvar...)) <= 0))
360360
end
361361
for i in findall(.!isinf.(con.x̂max))
362-
f_sym = Symbol("C_x̂max_$(i)")
362+
f_sym = Symbol("g_x̂max_$(i)")
363363
add_nonlinear_constraint(optim, :($(f_sym)($(ΔŨvar...)) <= 0))
364364
end
365365
return nothing
366366
end
367367

368368
"""
369-
con_nonlinprog!(C, mpc::NonLinMPC, model::SimModel, x̂end, Ŷ, ΔŨ) -> C
369+
con_nonlinprog!(g, mpc::NonLinMPC, model::SimModel, x̂end, Ŷ, ΔŨ) -> g
370370
371371
Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is not a [`LinModel`](@ref).
372372
373-
The method mutates the `C` vector in argument and returns it.
373+
The method mutates the `g` vector in argument and returns it.
374374
"""
375-
function con_nonlinprog!(C, mpc::NonLinMPC, ::SimModel, x̂end, Ŷ, ΔŨ)
375+
function con_nonlinprog!(g, mpc::NonLinMPC, ::SimModel, x̂end, Ŷ, ΔŨ)
376376
nx̂, nŶ = mpc.estim.nx̂, length(Ŷ)
377377
ϵ = !isinf(mpc.C) ? ΔŨ[end] : 0.0 # ϵ = 0.0 if Cwt=Inf (meaning: no relaxation)
378-
for i in eachindex(C)
379-
mpc.con.i_C[i] || continue
378+
for i in eachindex(g)
379+
mpc.con.i_g[i] || continue
380380
if i nŶ
381381
j = i
382-
C[i] = (mpc.con.Ymin[j] - Ŷ[j]) - ϵ*mpc.con.C_ymin[j]
382+
g[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+
g[i] = (Ŷ[j] - mpc.con.Ymax[j]) - ϵ*mpc.con.C_ymax[j]
386386
elseif i 2nŶ + nx̂
387387
j = i - 2nŶ
388-
C[i] = (mpc.con.x̂min[j] -end[j]) - ϵ*mpc.con.c_x̂min[j]
388+
g[i] = (mpc.con.x̂min[j] -end[j]) - ϵ*mpc.con.c_x̂min[j]
389389
else
390390
j = i - 2nŶ - nx̂
391-
C[i] = (x̂end[j] - mpc.con.x̂max[j]) - ϵ*mpc.con.c_x̂max[j]
391+
g[i] = (x̂end[j] - mpc.con.x̂max[j]) - ϵ*mpc.con.c_x̂max[j]
392392
end
393393
end
394-
return C
394+
return g
395395
end
396396

397-
"No nonlinear constraints if `model` is a [`LinModel`](@ref), return `C` unchanged."
398-
con_nonlinprog!(C, ::NonLinMPC, ::LinModel, _ , _ , _ ) = C
397+
"No nonlinear constraints if `model` is a [`LinModel`](@ref), return `g` unchanged."
398+
con_nonlinprog!(g, ::NonLinMPC, ::LinModel, _ , _ , _ ) = g

src/predictive_control.jl

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ struct ControllerConstraint
7474
C_ymax ::Vector{Float64}
7575
c_x̂min ::Vector{Float64}
7676
c_x̂max ::Vector{Float64}
77-
i_C ::BitVector
77+
i_g ::BitVector
7878
end
7979

8080
@doc raw"""
@@ -314,7 +314,7 @@ function setconstraint!(
314314
i_Ymin, i_Ymax = .!isinf.(con.Ymin), .!isinf.(con.Ymax)
315315
i_x̂min, i_x̂max = .!isinf.(con.x̂min), .!isinf.(con.x̂max)
316316
if notSolvedYet
317-
con.i_b[:], con.i_C[:], con.A[:] = init_matconstraint(model,
317+
con.i_b[:], con.i_g[:], con.A[:] = init_matconstraint(model,
318318
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax,
319319
i_Ymin, i_Ymax, i_x̂min, i_x̂max,
320320
con.A_Umin, con.A_Umax, con.A_ΔŨmin, con.A_ΔŨmax,
@@ -328,11 +328,11 @@ function setconstraint!(
328328
@constraint(mpc.optim, linconstraint, A*ΔŨvar .≤ b)
329329
setnonlincon!(mpc, model)
330330
else
331-
i_b, i_C = init_matconstraint(model,
331+
i_b, i_g = init_matconstraint(model,
332332
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax,
333333
i_Ymin, i_Ymax, i_x̂min, i_x̂max
334334
)
335-
if i_b con.i_b || i_C con.i_C
335+
if i_b con.i_b || i_g con.i_g
336336
error("Cannot modify ±Inf constraints after calling moveinput!")
337337
end
338338
end
@@ -1027,7 +1027,7 @@ function init_defaultcon(estim, Hp, Hc, C, S, N_Hc, E, ex̂, fx̂, gx̂, jx̂, k
10271027
i_ΔŨmin, i_ΔŨmax = .!isinf.(ΔŨmin), .!isinf.(ΔŨmax)
10281028
i_Ymin, i_Ymax = .!isinf.(Ymin), .!isinf.(Ymax)
10291029
i_x̂min, i_x̂max = .!isinf.(x̂min), .!isinf.(x̂max)
1030-
i_b, i_C, A = init_matconstraint(
1030+
i_b, i_g, A = init_matconstraint(
10311031
model,
10321032
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max,
10331033
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂max, A_x̂min
@@ -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_g
10411041
)
10421042
return con, S̃, Ñ_Hc, Ẽ
10431043
end
@@ -1245,19 +1245,19 @@ init_stochpred(estim::StateEstimator, _ ) = zeros(0, estim.nxs), zeros(0, estim.
12451245
@doc raw"""
12461246
init_matconstraint(model::LinModel,
12471247
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
1248-
) -> i_b, i_C, A
1248+
) -> i_b, i_g, A
12491249
1250-
Init `i_b`, `i_C` and `A` matrices for the linear and nonlinear inequality constraints.
1250+
Init `i_b`, `i_g` and `A` matrices for the linear and nonlinear inequality constraints.
12511251
12521252
The linear and nonlinear inequality constraints are respectively defined as:
12531253
```math
12541254
\begin{aligned}
12551255
\mathbf{A ΔŨ } &≤ \mathbf{b} \\
1256-
\mathbf{C(ΔŨ)} &≤ \mathbf{0}
1256+
\mathbf{g(ΔŨ)} &≤ \mathbf{0}
12571257
\end{aligned}
12581258
```
12591259
`i_b` is a `BitVector` including the indices of ``\mathbf{b}`` that are finite numbers.
1260-
`i_C` is a similar vector but for the indices of ``\mathbf{C}`` (empty if `model` is a
1260+
`i_g` is a similar vector but for the indices of ``\mathbf{g}`` (empty if `model` is a
12611261
[`LinModel`](@ref)). The method also returns the ``\mathbf{A}`` matrix if `args` is
12621262
provided. In such a case, `args` needs to contain all the inequality constraint matrices:
12631263
`A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max`.
@@ -1266,29 +1266,29 @@ function init_matconstraint(::LinModel,
12661266
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
12671267
)
12681268
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax; i_Ymin; i_Ymax; i_x̂min; i_x̂max]
1269-
i_C = BitVector()
1269+
i_g = BitVector()
12701270
if isempty(args)
12711271
A = zeros(length(i_b), 0)
12721272
else
12731273
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ymin, A_Ymax, A_x̂min, A_x̂max = args
12741274
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax; A_Ymin; A_Ymax; A_x̂min; A_x̂max]
12751275
end
1276-
return i_b, i_C, A
1276+
return i_b, i_g, A
12771277
end
12781278

1279-
"Init `i_b` and `A` without predicted output and terminal constraints if `model` is not a [`LinModel`](@ref)."
1279+
"Init `i_b, A` without outputs and terminal constraints if `model` is not a [`LinModel`](@ref)."
12801280
function init_matconstraint(::SimModel,
12811281
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ymin, i_Ymax, i_x̂min, i_x̂max, args...
12821282
)
12831283
i_b = [i_Umin; i_Umax; i_ΔŨmin; i_ΔŨmax]
1284-
i_C = [i_Ymin; i_Ymax; i_x̂min; i_x̂max]
1284+
i_g = [i_Ymin; i_Ymax; i_x̂min; i_x̂max]
12851285
if isempty(args)
12861286
A = zeros(length(i_b), 0)
12871287
else
12881288
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, _ , _ , _ , _ = args
12891289
A = [A_Umin; A_Umax; A_ΔŨmin; A_ΔŨmax]
12901290
end
1291-
return i_b, i_C, A
1291+
return i_b, i_g, A
12921292
end
12931293

12941294
"Validate predictive controller weight and horizon specified values."

src/state_estim.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -151,15 +151,15 @@ function init_integrators(nint::IntVectorOrInt, ny, varname::String)
151151
nx = sum(nint)
152152
A, C = zeros(nx, nx), zeros(ny, nx)
153153
if nx 0
154-
i_A, i_C = 1, 1
154+
i_A, i_g = 1, 1
155155
for i = 1:ny
156156
nint_i = nint[i]
157157
if nint_i 0
158158
rows_A = (i_A):(i_A + nint_i - 1)
159159
A[rows_A, rows_A] = Bidiagonal(ones(nint_i), ones(nint_i-1), :L)
160-
C[i, i_C+nint_i-1] = 1
160+
C[i, i_g+nint_i-1] = 1
161161
i_A += nint_i
162-
i_C += nint_i
162+
i_g += nint_i
163163
end
164164
end
165165
end

test/test_predictive_control.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -392,9 +392,9 @@ end
392392
u = moveinput!(nmpc4, [0], d, R̂u=fill(12, nmpc4.Hp))
393393
@test u [12] atol=5e-2
394394
nmpc5 = setconstraint!(NonLinMPC(nonlinmodel, Hp=15, Cwt=Inf), ymin=[-1])
395-
C_Ymax_end = nmpc5.optim.nlp_model.operators.registered_multivariate_operators[end].f
396-
@test C_Ymax_end(Float64.((1.0, 1.0))) 0.0 # test con_nonlinprog_i(i,::NTuple{N, Float64})
397-
@test C_Ymax_end(Float32.((1.0, 1.0))) 0.0 # test con_nonlinprog_i(i,::NTuple{N, Real})
395+
g_Ymax_end = nmpc5.optim.nlp_model.operators.registered_multivariate_operators[end].f
396+
@test g_Ymax_end(Float64.((1.0, 1.0))) 0.0 # test gfunc_i(i,::NTuple{N, Float64})
397+
@test g_Ymax_end(Float32.((1.0, 1.0))) 0.0 # test gfunc_i(i,::NTuple{N, Real})
398398
end
399399

400400
@testset "NonLinMPC step disturbance rejection" begin

0 commit comments

Comments
 (0)