Skip to content

Commit 5c2b0cf

Browse files
committed
wip: all field in struct should be concrete type
`ControllerConstraint{NT}` is no longer concrete `ControllerConstraint{NT, GCfunc}` is
1 parent bbb131b commit 5c2b0cf

File tree

2 files changed

+24
-15
lines changed

2 files changed

+24
-15
lines changed

src/controller/execute.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -371,11 +371,10 @@ The function mutates `Ue`, `Ŷe` and `Ū` in arguments, without assuming any i
371371
function extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
372372
ny, nu = model.ny, model.nu
373373
# --- extended manipulated inputs Ue = [U; u(k+Hp-1)] ---
374-
U0 =
375-
U0 .= mul!(U0, mpc.S̃, ΔŨ) .+ mpc.T_lastu0
376-
Ue[1:end-nu] .= U0 .+ mpc.Uop
374+
U =
375+
U .= mul!(U, mpc.S̃, ΔŨ) .+ mpc.T_lastu0 .+ mpc.Uop
377376
# u(k + Hp) = u(k + Hp - 1) since Δu(k+Hp) = 0 (because Hc ≤ Hp):
378-
Ue[end-nu+1:end] .= @views Ue[end-2nu+1:end-nu]
377+
Ue[end-nu+1:end] .= @views U[end-nu+1:end]
379378
# --- extended output predictions Ŷe = [ŷ(k); Ŷ] ---
380379
Ŷe[1:ny] .= mpc.
381380
Ŷe[ny+1:end] .= Ŷ0 .+ mpc.Yop

src/controller/nonlinmpc.jl

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,17 @@ const DEFAULT_NONLINMPC_OPTIMIZER = optimizer_with_attributes(Ipopt.Optimizer,"s
22

33
struct NonLinMPC{
44
NT<:Real,
5-
SE<:StateEstimator,
5+
SE<:StateEstimator,
66
JM<:JuMP.GenericModel,
77
JEfunc<:Function,
8+
GCfunc<:Function,
89
P<:Any
910
} <: PredictiveController{NT}
1011
estim::SE
1112
# note: `NT` and the number type `JNT` in `JuMP.GenericModel{JNT}` can be
1213
# different since solvers that support non-Float64 are scarce.
1314
optim::JM
14-
con::ControllerConstraint{NT}
15+
con::ControllerConstraint{NT, GCfunc}
1516
ΔŨ::Vector{NT}
1617
::Vector{NT}
1718
Hp::Int
@@ -48,9 +49,17 @@ struct NonLinMPC{
4849
Yop::Vector{NT}
4950
Dop::Vector{NT}
5051
buffer::PredictiveControllerBuffer{NT}
51-
function NonLinMPC{NT, SE, JM, JEfunc, P}(
52-
estim::SE, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE::JEfunc, gc, nc, p::P, optim::JM
53-
) where {NT<:Real, SE<:StateEstimator, JM<:JuMP.GenericModel, JEfunc<:Function, P<:Any}
52+
function NonLinMPC{NT, SE, JM, JEfunc, GCfunc, P}(
53+
estim::SE,
54+
Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE::JEfunc, gc::GCfunc, nc, p::P, optim::JM
55+
) where {
56+
NT<:Real,
57+
SE<:StateEstimator,
58+
JM<:JuMP.GenericModel,
59+
JEfunc<:Function,
60+
GCfunc<:Function,
61+
P<:Any
62+
}
5463
model = estim.model
5564
nu, ny, nd, nx̂ = model.nu, model.ny, model.nd, estim.nx̂
5665
= copy(model.yop) # dummy vals (updated just before optimization)
@@ -82,7 +91,7 @@ struct NonLinMPC{
8291
nΔŨ = size(Ẽ, 2)
8392
ΔŨ = zeros(NT, nΔŨ)
8493
buffer = PredictiveControllerBuffer{NT}(nu, ny, nd, Hp)
85-
mpc = new{NT, SE, JM, JEfunc, P}(
94+
mpc = new{NT, SE, JM, JEfunc, GCfunc, P}(
8695
estim, optim, con,
8796
ΔŨ, ŷ,
8897
Hp, Hc, nϵ,
@@ -318,9 +327,9 @@ function NonLinMPC(
318327
L_Hp = diagm(repeat(Lwt, Hp)),
319328
Cwt = DEFAULT_CWT,
320329
Ewt = DEFAULT_EWT,
321-
JE ::JEfunc = (_,_,_,_) -> 0.0,
330+
JE ::JEfunc = (_,_,_,_) -> 0.0,
322331
gc!::Function = (_,_,_,_,_,_) -> nothing,
323-
gc ::Function = gc!,
332+
gc ::GCfunc = gc!,
324333
nc = 0,
325334
p::P = estim.model.p,
326335
optim::JM = JuMP.Model(DEFAULT_NONLINMPC_OPTIMIZER, add_bridges=false),
@@ -329,14 +338,15 @@ function NonLinMPC(
329338
SE<:StateEstimator{NT},
330339
JM<:JuMP.GenericModel,
331340
JEfunc<:Function,
341+
GCfunc<:Function,
332342
P<:Any
333343
}
334344
nk = estimate_delays(estim.model)
335345
if Hp nk
336346
@warn("prediction horizon Hp ($Hp) ≤ estimated number of delays in model "*
337347
"($nk), the closed-loop system may be unstable or zero-gain (unresponsive)")
338348
end
339-
return NonLinMPC{NT, SE, JM, JEfunc, P}(
349+
return NonLinMPC{NT, SE, JM, JEfunc, GCfunc, P}(
340350
estim, Hp, Hc, M_Hp, N_Hc, L_Hp, Cwt, Ewt, JE, gc, nc, p, optim
341351
)
342352
end
@@ -520,7 +530,7 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
520530
gc = get_tmp(gc_cache, ΔŨ1)
521531
Ŷ0, x̂0end = predict!(Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ)
522532
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
523-
ϵ = (nϵ 0) ? ΔŨ[end] : zero(T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
533+
ϵ = (nϵ 0) ? ΔŨ[end] : 0 # ϵ = 0 if nϵ == 0 (meaning no relaxation)
524534
mpc.con.gc!(gc, Ue, Ŷe, mpc.D̂e, mpc.p, ϵ)
525535
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
526536
return obj_nonlinprog!(Ȳ, Ū, mpc, model, Ue, Ŷe, ΔŨ)::T
@@ -539,7 +549,7 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
539549
gc = get_tmp(gc_cache, ΔŨ1)
540550
Ŷ0, x̂0end = predict!(Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ)
541551
Ue, Ŷe = extended_predictions!(Ue, Ŷe, Ū, mpc, model, Ŷ0, ΔŨ)
542-
ϵ = (nϵ 0) ? ΔŨ[end] : zero(T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
552+
ϵ = (nϵ 0) ? ΔŨ[end] : 0 # ϵ = 0 if nϵ == 0 (meaning no relaxation)
543553
mpc.con.gc!(gc, Ue, Ŷe, mpc.D̂e, mpc.p, ϵ)
544554
g = con_nonlinprog!(g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
545555
end

0 commit comments

Comments
 (0)