Skip to content

Commit c29e29c

Browse files
committed
changed nomenclature for extended vectors
1 parent 406d18f commit c29e29c

File tree

7 files changed

+69
-58
lines changed

7 files changed

+69
-58
lines changed

src/controller/execute.jl

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ See also [`LinMPC`](@ref), [`ExplicitMPC`](@ref), [`NonLinMPC`](@ref).
3131
- `mpc::PredictiveController` : solve optimization problem of `mpc`.
3232
- `ry=mpc.estim.model.yop` : current output setpoints ``\mathbf{r_y}(k)``.
3333
- `d=[]` : current measured disturbances ``\mathbf{d}(k)``.
34-
- `D̂=repeat(d, mpc.Hp)` or *`Dhat`* : predicted measured disturbances ``\mathbf{D̂}``, constant
35-
in the future by default or ``\mathbf{d̂}(k+j)=\mathbf{d}(k)`` for ``j=1`` to ``H_p``.
36-
- `R̂y=repeat(ry, mpc.Hp)` or *`Rhaty`* : predicted output setpoints ``\mathbf{R̂_y}``, constant
37-
in the future by default or ``\mathbf{r̂_y}(k+j)=\mathbf{r_y}(k)`` for ``j=1`` to ``H_p``.
38-
- `R̂u=mpc.Uop` or *`Rhatu`* : predicted manipulated input setpoints, constant in the future
39-
by default or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for ``j=0`` to ``H_p-1``.
34+
- `D̂=repeat(d, mpc.Hp)` or *`Dhat`* : predicted measured disturbances ``\mathbf{D̂}`` (constant
35+
in the future by default, or ``\mathbf{d̂}(k+j)=\mathbf{d}(k)`` for ``j=1`` to ``H_p``).
36+
- `R̂y=repeat(ry, mpc.Hp)` or *`Rhaty`* : predicted output setpoints ``\mathbf{R̂_y}`` (constant
37+
in the future by default, or ``\mathbf{r̂_y}(k+j)=\mathbf{r_y}(k)`` for ``j=1`` to ``H_p``).
38+
- `R̂u=mpc.Uop` or *`Rhatu`* : predicted manipulated input setpoints ``\mathbf{R̂_u}`` (constant
39+
in the future by default, or ``\mathbf{r̂_u}(k+j)=\mathbf{u_{op}}`` for ``j=0`` to ``H_p-1``).
4040
4141
# Examples
4242
```jldoctest
@@ -193,8 +193,8 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, R̂y, R̂
193193
if model.nd 0
194194
mpc.d0 .= d .- model.dop
195195
mpc.D̂0 .=.- mpc.Dop
196-
mpc.D̂E[1:model.nd] .= d
197-
mpc.D̂E[model.nd+1:end] .=
196+
mpc.D̂e[1:model.nd] .= d
197+
mpc.D̂e[model.nd+1:end] .=
198198
mul!(F, mpc.G, mpc.d0, 1, 1)
199199
mul!(F, mpc.J, mpc.D̂0, 1, 1)
200200
end
@@ -217,7 +217,7 @@ end
217217
@doc raw"""
218218
initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
219219
220-
Init `ŷ, F, d0, D̂0, D̂E, R̂y0, R̂u0` vectors when model is not a [`LinModel`](@ref).
220+
Init `ŷ, F, d0, D̂0, D̂e, R̂y0, R̂u0` vectors when model is not a [`LinModel`](@ref).
221221
"""
222222
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂u)
223223
mul!(mpc.T_lastu0, mpc.T, mpc.estim.lastu0)
@@ -226,8 +226,8 @@ function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, R̂y, R̂
226226
if model.nd 0
227227
mpc.d0 .= d .- model.dop
228228
mpc.D̂0 .=.- mpc.Dop
229-
mpc.D̂E[1:model.nd] .= d
230-
mpc.D̂E[model.nd+1:end] .=
229+
mpc.D̂e[1:model.nd] .= d
230+
mpc.D̂e[model.nd+1:end] .=
231231
end
232232
mpc.R̂y0 .= (R̂y .- mpc.Yop)
233233
if ~mpc.noR̂u

src/controller/explicitmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
3030
Ps::Matrix{NT}
3131
d0::Vector{NT}
3232
D̂0::Vector{NT}
33-
D̂E::Vector{NT}
33+
D̂e::Vector{NT}
3434
Uop::Vector{NT}
3535
Yop::Vector{NT}
3636
Dop::Vector{NT}
@@ -62,7 +62,7 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
6262
H̃_chol = cholesky(H̃)
6363
Ks, Ps = init_stochpred(estim, Hp)
6464
# dummy vals (updated just before optimization):
65-
d0, D̂0, D̂E = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
65+
d0, D̂0, D̂e = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
6666
Uop, Yop, Dop = repeat(model.uop, Hp), repeat(model.yop, Hp), repeat(model.dop, Hp)
6767
nΔŨ = size(Ẽ, 2)
6868
ΔŨ = zeros(NT, nΔŨ)
@@ -78,7 +78,7 @@ struct ExplicitMPC{NT<:Real, SE<:StateEstimator} <: PredictiveController{NT}
7878
H̃, q̃, r,
7979
H̃_chol,
8080
Ks, Ps,
81-
d0, D̂0, D̂E,
81+
d0, D̂0, D̂e,
8282
Uop, Yop, Dop,
8383
buffer
8484
)

src/controller/linmpc.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ struct LinMPC{
3939
Ps::Matrix{NT}
4040
d0::Vector{NT}
4141
D̂0::Vector{NT}
42-
D̂E::Vector{NT}
42+
D̂e::Vector{NT}
4343
Uop::Vector{NT}
4444
Yop::Vector{NT}
4545
Dop::Vector{NT}
@@ -71,7 +71,7 @@ struct LinMPC{
7171
q̃, r = zeros(NT, size(H̃, 1)), zeros(NT, 1)
7272
Ks, Ps = init_stochpred(estim, Hp)
7373
# dummy vals (updated just before optimization):
74-
d0, D̂0, D̂E = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
74+
d0, D̂0, D̂e = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
7575
Uop, Yop, Dop = repeat(model.uop, Hp), repeat(model.yop, Hp), repeat(model.dop, Hp)
7676
nΔŨ = size(Ẽ, 2)
7777
ΔŨ = zeros(NT, nΔŨ)
@@ -86,7 +86,7 @@ struct LinMPC{
8686
Ẽ, F, G, J, K, V, B,
8787
H̃, q̃, r,
8888
Ks, Ps,
89-
d0, D̂0, D̂E,
89+
d0, D̂0, D̂e,
9090
Uop, Yop, Dop,
9191
buffer
9292
)

src/controller/nonlinmpc.jl

Lines changed: 40 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ struct NonLinMPC{
4343
Ps::Matrix{NT}
4444
d0::Vector{NT}
4545
D̂0::Vector{NT}
46-
D̂E::Vector{NT}
46+
D̂e::Vector{NT}
4747
Uop::Vector{NT}
4848
Yop::Vector{NT}
4949
Dop::Vector{NT}
@@ -76,7 +76,7 @@ struct NonLinMPC{
7676
q̃, r = zeros(NT, size(H̃, 1)), zeros(NT, 1)
7777
Ks, Ps = init_stochpred(estim, Hp)
7878
# dummy vals (updated just before optimization):
79-
d0, D̂0, D̂E = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
79+
d0, D̂0, D̂e = zeros(NT, nd), zeros(NT, nd*Hp), zeros(NT, nd + nd*Hp)
8080
Uop, Yop, Dop = repeat(model.uop, Hp), repeat(model.yop, Hp), repeat(model.dop, Hp)
8181
nΔŨ = size(Ẽ, 2)
8282
ΔŨ = zeros(NT, nΔŨ)
@@ -91,7 +91,7 @@ struct NonLinMPC{
9191
Ẽ, F, G, J, K, V, B,
9292
H̃, q̃, r,
9393
Ks, Ps,
94-
d0, D̂0, D̂E,
94+
d0, D̂0, D̂e,
9595
Uop, Yop, Dop,
9696
buffer
9797
)
@@ -165,8 +165,8 @@ state estimator :
165165
- `JE=(_,_,_,_)->0.0` : economic or custom cost function ``J_E(\mathbf{U_e}, \mathbf{Ŷ_e},
166166
\mathbf{D̂_e}, \mathbf{p})``.
167167
- `gc=(_,_,_,_,_,_)->nothing` or `gc!` : custom inequality constraint function
168-
``\mathbf{g_c}(\mathbf{U_e}, \mathbf{Ŷ_e}, \mathbf{D̂_e}, \mathbf{p}, ϵ)`` (mutating or
169-
not, details in Extended Help).
168+
``\mathbf{g_c}(\mathbf{U_e}, \mathbf{Ŷ_e}, \mathbf{D̂_e}, \mathbf{p}, ϵ)``, mutating or
169+
not (details in Extended Help).
170170
- `nc=0` : number of custom inequality constraints.
171171
- `p=model.p` : ``J_E`` and ``\mathbf{g_c}`` functions parameter ``\mathbf{p}`` (any type).
172172
- `optim=JuMP.Model(Ipopt.Optimizer)` : nonlinear optimizer used in the predictive
@@ -346,20 +346,6 @@ function validate_JE(NT, JE)
346346
return nothing
347347
end
348348

349-
"Get mutating custom constraint function `gc!` from the provided function in argument."
350-
function get_mutating_gc(NT, gc)
351-
ismutating_gc = validate_gc(NT, gc)
352-
gc! = if ismutating_gc
353-
gc
354-
else
355-
function gc!(LHS, Ue, Ŷe, D̂e, p, ϵ)
356-
LHS .= gc(Ue, Ŷe, D̂e, p, ϵ)
357-
return nothing
358-
end
359-
end
360-
return gc!
361-
end
362-
363349
"""
364350
validate_gc(NT, gc) -> ismutating
365351
@@ -371,30 +357,55 @@ function validate_gc(NT, gc)
371357
# LHS, Ue, Ŷe, D̂e, p, ϵ
372358
Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, Any, NT}
373359
)
374-
println(ismutating)
375360
# Ue, Ŷe, D̂e, p, ϵ
376361
if !(ismutating || hasmethod(gc, Tuple{Vector{NT}, Vector{NT}, Vector{NT}, Any, NT}))
377362
error(
378363
"the custom constraint function has no method with type signature "*
379-
"gc(UE::Vector{$(NT)}, ŶE::Vector{$(NT)}, D̂E::Vector{$(NT)}, p::Any, ϵ::$(NT)) "*
364+
"gc(Ue::Vector{$(NT)}, Ŷe::Vector{$(NT)}, D̂e::Vector{$(NT)}, p::Any, ϵ::$(NT)) "*
380365
"or mutating form gc!(LHS::Vector{$(NT)}, Ue::Vector{$(NT)}, Ŷe::Vector{$(NT)}, "*
381366
"D̂e::Vector{$(NT)}, p::Any, ϵ::$(NT))"
382367
)
383368
end
384369
return ismutating
385370
end
386371

372+
"Get mutating custom constraint function `gc!` from the provided function in argument."
373+
function get_mutating_gc(NT, gc)
374+
ismutating_gc = validate_gc(NT, gc)
375+
gc! = if ismutating_gc
376+
gc
377+
else
378+
println("YO!")
379+
function gc!(LHS, Ue, Ŷe, D̂e, p, ϵ)
380+
LHS .= gc(Ue, Ŷe, D̂e, p, ϵ)
381+
return nothing
382+
end
383+
end
384+
return gc!
385+
end
386+
387+
function test_custom_functions(JE, gc!, uop; Uop, dop, Dop, ΔŨ, p)
388+
Ue = [Uop; uop]
389+
D̂e = [dop; Dop]
390+
Ŷ0, x̂0next =
391+
Ŷ0, x̂0end = predict!(Ŷ0, x̂0, x̂0next, u0, û0, mpc, model, mpc.ΔŨ)
392+
393+
394+
JE = JE(Uop, Uop, Dop, p)
395+
396+
end
397+
387398
"""
388399
addinfo!(info, mpc::NonLinMPC) -> info
389400
390401
For [`NonLinMPC`](@ref), add `:sol` and the optimal economic cost `:JE`.
391402
"""
392403
function addinfo!(info, mpc::NonLinMPC)
393404
U, Ŷ, D̂, ŷ, d = info[:U], info[:Ŷ], info[:D̂], info[:ŷ], info[:d]
394-
UE = [U; U[(end - mpc.estim.model.nu + 1):end]]
395-
ŶE = [ŷ; Ŷ]
396-
D̂E = [d; D̂]
397-
info[:JE] = mpc.JE(UE, ŶE, D̂E, mpc.p)
405+
Ue = [U; U[(end - mpc.estim.model.nu + 1):end]]
406+
Ŷe = [ŷ; Ŷ]
407+
D̂e = [d; D̂]
408+
info[:JE] = mpc.JE(Ue, Ŷe, D̂e, mpc.p)
398409
info[:sol] = JuMP.solution_summary(mpc.optim, verbose=true)
399410
return info
400411
end
@@ -571,15 +582,15 @@ con_nonlinprog!(g, ::NonLinMPC, ::LinModel, _ , _ , _ ) = g
571582
"Evaluate the economic term of the objective function for [`NonLinMPC`](@ref)."
572583
function obj_econ!(U0, Ȳ, mpc::NonLinMPC, model::SimModel, Ŷ0, ΔŨ)
573584
if !iszero(mpc.E)
574-
ny, Hp, ŷ, D̂E = model.ny, mpc.Hp, mpc.ŷ, mpc.D̂E
585+
ny, Hp, ŷ, D̂e = model.ny, mpc.Hp, mpc.ŷ, mpc.D̂e
575586
U = U0
576587
U .+= mpc.Uop
577588
uend = @views U[(end-model.nu+1):end]
578589
=
579590
Ŷ .= Ŷ0 .+ mpc.Yop
580-
UE = [U; uend]
581-
ŶE = [ŷ; Ŷ]
582-
E_JE = mpc.E*mpc.JE(UE, ŶE, D̂E, mpc.p)
591+
Ue = [U; uend]
592+
Ŷe = [ŷ; Ŷ]
593+
E_JE = mpc.E*mpc.JE(Ue, Ŷe, D̂e, mpc.p)
583594
else
584595
E_JE = 0.0
585596
end

src/model/nonlinmodel.jl

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -179,13 +179,6 @@ function get_mutating_functions(NT, f, h)
179179
return f!, h!
180180
end
181181

182-
"Get the types of `f!`, `h!` and `solver` to construct a `NonLinModel`."
183-
function get_types(
184-
::F, ::H, ::P, ::DS
185-
) where {F<:Function, H<:Function, P<:Any, DS<:DiffSolver}
186-
return F, H, P, DS
187-
end
188-
189182
"""
190183
validate_f(NT, f) -> ismutating
191184
@@ -194,7 +187,7 @@ Validate `f` function argument signature and return `true` if it is mutating.
194187
function validate_f(NT, f)
195188
ismutating = hasmethod(
196189
f,
197-
#/xnext, x, u, d, p
190+
# or xnext, x, u, d, p
198191
Tuple{ Vector{NT}, Vector{NT}, Vector{NT}, Vector{NT}, Any}
199192
)
200193
# x, u, d, p
@@ -231,6 +224,13 @@ function validate_h(NT, h)
231224
return ismutating
232225
end
233226

227+
"Get the types of `f!`, `h!` and `solver` to construct a `NonLinModel`."
228+
function get_types(
229+
::F, ::H, ::P, ::DS
230+
) where {F<:Function, H<:Function, P<:Any, DS<:DiffSolver}
231+
return F, H, P, DS
232+
end
233+
234234
"Do nothing if `model` is a [`NonLinModel`](@ref)."
235235
steadystate!(::SimModel, _ , _ ) = nothing
236236

src/precompile.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,8 @@ preparestate!(nmpc_mhe, [55, 30])
112112
u = nmpc_mhe([55, 30])
113113
sim!(nmpc_mhe, 3, [55, 30])
114114

115-
function JE( _ , ŶE, _ , R̂y)
116-
= ŶE[3:end]
115+
function JE( _ , Ŷe, _ , R̂y)
116+
= Ŷe[3:end]
117117
= R̂y -
118118
return dot(Ȳ, Ȳ)
119119
end

test/test_predictive_control.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -491,7 +491,7 @@ end
491491
@test nmpc5.Ñ_Hc Diagonal(diagm([repeat(Float64[3, 4], 5); [1e3]]))
492492
nmpc6 = NonLinMPC(nonlinmodel, Hp=15, Lwt=[0,1])
493493
@test nmpc6.L_Hp Diagonal(diagm(repeat(Float64[0, 1], 15)))
494-
nmpc7 = NonLinMPC(nonlinmodel, Hp=15, Ewt=1e-3, JE=(UE,ŶE,D̂E,p) -> p*UE.*ŶE.*D̂E, p=2)
494+
nmpc7 = NonLinMPC(nonlinmodel, Hp=15, Ewt=1e-3, JE=(Ue,Ŷe,D̂e,p) -> p*Ue.*Ŷe.*D̂e, p=2)
495495
@test nmpc7.E == 1e-3
496496
@test nmpc7.JE([1,2],[3,4],[4,6],2) == 2*[1,2].*[3,4].*[4,6]
497497
optim = JuMP.Model(optimizer_with_attributes(Ipopt.Optimizer, "nlp_scaling_max_gradient"=>1.0))
@@ -538,7 +538,7 @@ end
538538
@test info[:Ŷ][end] r[1] atol=5e-2
539539
Hp = 1000
540540
R̂y = fill(r[1], Hp)
541-
JE = (_ , ŶE, _ , R̂y) -> sum((ŶE[2:end] - R̂y).^2)
541+
JE = (_ , Ŷe, _ , R̂y) -> sum((Ŷe[2:end] - R̂y).^2)
542542
nmpc = NonLinMPC(linmodel, Mwt=[0], Nwt=[0], Cwt=Inf, Ewt=1, JE=JE, p=R̂y, Hp=Hp, Hc=1)
543543
preparestate!(nmpc, [10])
544544
u = moveinput!(nmpc)

0 commit comments

Comments
 (0)