Skip to content

Commit 4137880

Browse files
committed
added: adapt K0 size to number of collocation points
1 parent 356e573 commit 4137880

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

src/controller/nonlinmpc.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -562,8 +562,10 @@ Inspired from: [User-defined operators with vector outputs](@extref JuMP User-de
562562
function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT<:Real
563563
# ----------- common cache for Jfunc, gfuncs and geqfuncs ----------------------------
564564
model = mpc.estim.model
565+
transcription = mpc.transcription
565566
grad, jac = mpc.gradient, mpc.jacobian
566-
nu, ny, nx̂, nϵ, nk = model.nu, model.ny, mpc.estim.nx̂, mpc.nϵ, model.nk
567+
nu, ny, nx̂, nϵ = model.nu, model.ny, mpc.estim.nx̂, mpc.
568+
nk = get_nk(model, transcription)
567569
Hp, Hc = mpc.Hp, mpc.Hc
568570
ng, nc, neq = length(mpc.con.i_g), mpc.con.nc, mpc.con.neq
569571
nZ̃, nU, nŶ, nX̂, nK = length(mpc.Z̃), Hp*nu, Hp*ny, Hp*nx̂, Hp*nk

src/controller/transcription.jl

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -78,12 +78,18 @@ equality constraint function and by using the implicit trapezoidal rule. It can
7878
moderately stiff systems and is A-stable. However, it may not be as efficient as more
7979
advanced collocation methods for highly stiff systems. Note that the stochastic model of the
8080
unmeasured disturbances is strictly discrete-time, it is thus transcribed separately using
81-
[`MultipleShooting`](@ref).
81+
[`MultipleShooting`](@ref). Also note that the state
8282
8383
Sparse optimizers like `Ipopt` and sparse Jacobian computations are recommended for this
8484
transcription method.
8585
"""
86-
struct TrapezoidalCollocation <: CollocationMethod end
86+
struct TrapezoidalCollocation <: CollocationMethod
87+
nc::Int
88+
function TrapezoidalCollocation()
89+
nc = 2 # 2 collocation points per interval for trapezoidal rule
90+
return new(nc)
91+
end
92+
end
8793

8894
function validate_transcription(::LinModel, ::CollocationMethod)
8995
throw(ArgumentError("Collocation methods are not supported for LinModel."))
@@ -103,6 +109,10 @@ function get_nZ(estim::StateEstimator, ::TranscriptionMethod, Hp, Hc)
103109
return estim.model.nu*Hc + estim.nx̂*Hp
104110
end
105111

112+
"Get length of the `k` vector with all the solver intermediate steps or all the collocation pts."
113+
get_nk(model::SimModel, ::ShootingMethod) = model.nk
114+
get_nk(model::SimModel, transcription::CollocationMethod) = model.nx*transcription.nc
115+
106116
@doc raw"""
107117
init_ZtoΔU(estim::StateEstimator, transcription::TranscriptionMethod, Hp, Hc) -> PΔu
108118
@@ -1271,7 +1281,8 @@ end
12711281
"""
12721282
con_nonlinprogeq!(
12731283
geq, X̂0, Û0, K0
1274-
mpc::PredictiveController, model::NonLinModel, ::MultipleShooting, U0, Z̃
1284+
mpc::PredictiveController, model::NonLinModel, transcription::MultipleShooting,
1285+
U0, Z̃
12751286
)
12761287
12771288
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`MultipleShooting`](@ref).
@@ -1311,7 +1322,8 @@ end
13111322
@doc raw"""
13121323
con_nonlinprogeq!(
13131324
geq, X̂0, Û0, K0
1314-
mpc::PredictiveController, model::NonLinModel, ::TrapezoidalCollocation, U0, Z̃
1325+
mpc::PredictiveController, model::NonLinModel, transcription::TrapezoidalCollocation,
1326+
U0, Z̃
13151327
)
13161328
13171329
Nonlinear equality constrains for [`NonLinModel`](@ref) and [`TrapezoidalCollocation`](@ref).
@@ -1334,16 +1346,15 @@ hence no need to add `model.fop` and subtract `model.xop` in the collocation equ
13341346
"""
13351347
function con_nonlinprogeq!(
13361348
geq, X̂0, Û0, K0,
1337-
mpc::PredictiveController, model::NonLinModel, ::TrapezoidalCollocation, U0, Z̃
1349+
mpc::PredictiveController, model::NonLinModel, transcription::TrapezoidalCollocation,
1350+
U0, Z̃
13381351
)
13391352
nu, nx̂, nd, nx = model.nu, mpc.estim.nx̂, model.nd, model.nx
13401353
Hp, Hc = mpc.Hp, mpc.Hc
13411354
nΔU, nX̂ = nu*Hc, nx̂*Hp
1342-
13431355
Ts, p = model.Ts, model.p
13441356
As, Cs_u = mpc.estim.As, mpc.estim.Cs_u
1345-
# K0 stores the state derivatives at each collocation point for all intervals
1346-
nk = model.nk # TODO: initialize K0 to the adequate size for a given collocation method
1357+
nk = get_nk(model, transcription)
13471358
D̂0 = mpc.D̂0
13481359
X̂0_Z̃ = @views Z̃[(nΔU+1):(nΔU+nX̂)]
13491360
x̂0 = @views mpc.estim.x̂0[1:nx̂]

0 commit comments

Comments
 (0)