Skip to content

Commit 3d883eb

Browse files
committed
call f, h function outside NonLinModel objects
1 parent f5d1116 commit 3d883eb

File tree

4 files changed

+22
-27
lines changed

4 files changed

+22
-27
lines changed

.vscode/settings.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
"bmatrix",
2121
"bumpless",
2222
"csum",
23+
"defaultcon",
2324
"deterpred",
2425
"Discretize",
2526
"EMPC",
@@ -43,6 +44,7 @@
4344
"mathrm",
4445
"moveinput",
4546
"nonlinmpc",
47+
"nonlinprog",
4648
"optim",
4749
"OSQP",
4850
"Penrose",

example/juMPC.jl

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
# spell-checker: disable
22

3-
using Pkg
4-
using Revise
5-
Pkg.activate(".")
3+
#using Pkg
4+
#using Revise
5+
#Pkg.activate(".")
66

77

88
using ModelPredictiveControl
9-
using Preferences
10-
set_preferences!(ModelPredictiveControl, "precompile_workload" => false; force=true)
9+
#using Preferences
10+
#set_preferences!(ModelPredictiveControl, "precompile_workload" => false; force=true)
1111

1212

1313
#using JuMP, DAQP

src/controller/nonlinmpc.jl

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -270,8 +270,12 @@ function obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) wh
270270
return JR̂y + JΔŨ + JR̂u ++ mpc.E*mpc.JE(UE, ŶE, D̂E)
271271
end
272272

273+
274+
function con_nonlinprog(mpc::NonLinMPC, ::LinModel, ΔŨ::NTuple{N, T}) where {N, T}
275+
return zeros(T, 2*mpc.ny*mpc.Hp)
276+
end
273277
"""
274-
con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
278+
con_nonlinprog(mpc::NonLinMPC, model::NonLinModel, ΔŨ::NTuple{N, T}) where {N, T}
275279
276280
TBW
277281
"""
@@ -294,14 +298,14 @@ function con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) wh
294298
return C
295299
end
296300

297-
function evalŶ(mpc, model, x̂d, d0, D̂0, U0::Vector{T}) where {T}
301+
function evalŶ(mpc::NonLinMPC, model::SimModel, x̂d, d0, D̂0, U0::Vector{T}) where {T}
298302
Ŷd0 = Vector{T}(undef, model.ny*mpc.Hp)
299303
x̂d::Vector{T} = copy(x̂d)
300304
for j=1:mpc.Hp
301305
u0 = U0[(1 + model.nu*(j-1)):(model.nu*j)]
302-
x̂d[:] = model.f(x̂d, u0, d0)
306+
x̂d[:] = f(model, x̂d, u0, d0)
303307
d0 = D̂0[(1 + model.nd*(j-1)):(model.nd*j)]
304-
Ŷd0[(1 + model.ny*(j-1)):(model.ny*j)] = model.h(x̂d, d0)
308+
Ŷd0[(1 + model.ny*(j-1)):(model.ny*j)] = h(model, x̂d, d0)
305309
end
306310
return Ŷd0 + mpc.F # mpc.F = Yop + Ŷs
307311
end

src/predictive_control.jl

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -356,16 +356,16 @@ function initpred!(mpc::PredictiveController, model::LinModel, d, D̂, Ŷs, R̂
356356
end
357357

358358
@doc raw"""
359-
initpred!(mpc::PredictiveController, model::NonLinModel, d, D̂, Ŷs, R̂y )
359+
initpred!(mpc::PredictiveController, model::SimModel, d, D̂, Ŷs, R̂y )
360360
361-
Init `F`, `d0` and `D̂0` prediction matrices for [`NonLinModel`](@ref).
361+
Init `F`, `d0` and `D̂0` prediction matrices when model is not a [`LinModel`](@ref).
362362
363-
For [`NonLinModel`](@ref), the constant matrix ``\mathbf{F = Ŷ_s + Y_{op}}``, thus it
363+
In such a case, the constant matrix is ``\mathbf{F = Ŷ_s + Y_{op}}``, thus it
364364
incorporates the stochastic predictions and the output operating point ``\mathbf{y_{op}}``
365365
repeated over ``H_p``. `d0` and `D̂0` are the measured disturbances and its predictions
366366
without the operating points ``\mathbf{d_{op}}``.
367367
"""
368-
function initpred!(mpc::PredictiveController, model::NonLinModel, d, D̂, Ŷs , R̂y )
368+
function initpred!(mpc::PredictiveController, model::SimModel, d, D̂, Ŷs , R̂y )
369369
mpc.F[:] = Ŷs + mpc.Yop
370370
if model.nd 0
371371
mpc.d0[:], mpc.D̂0[:] = d - model.dop, D̂ - mpc.Dop
@@ -392,12 +392,8 @@ function linconstraint!(mpc::PredictiveController, model::LinModel)
392392
set_normalized_rhs.(mpc.optim[:linconstraint], mpc.con.b[mpc.con.i_b])
393393
end
394394

395-
@doc raw"""
396-
linconstraint!(mpc::PredictiveController, model::NonLinModel)
397-
398-
Set `b` that excludes predicted output ``\mathbf{Ŷ}`` constraints for [`NonLinModel`](@ref).
399-
"""
400-
function linconstraint!(mpc::PredictiveController, model::NonLinModel)
395+
"Set `b` excluding predicted output constraints when `model` is not a [`LinModel`](@ref)."
396+
function linconstraint!(mpc::PredictiveController, model::SimModel)
401397
mpc.con.b[:] = [
402398
-mpc.con.Umin + mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
403399
+mpc.con.Umax - mpc.T_Hc*(mpc.estim.lastu0 + model.uop)
@@ -870,14 +866,7 @@ function init_linconstraint(::LinModel,
870866
return A, i_b, b
871867
end
872868

873-
@doc raw"""
874-
init_linconstraint(model::NonLinModel,
875-
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, A_Ŷmin, A_Ŷmax,
876-
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, i_Ŷmin, i_Ŷmax
877-
)
878-
879-
Init the values without predicted output constraints if `model` is not a [`LinModel`](@ref).
880-
"""
869+
"Init values without predicted output constraints if `model` is not a [`LinModel`](@ref)."
881870
function init_linconstraint(::SimModel,
882871
A_Umin, A_Umax, A_ΔŨmin, A_ΔŨmax, _ , _ ,
883872
i_Umin, i_Umax, i_ΔŨmin, i_ΔŨmax, _ , _

0 commit comments

Comments
 (0)