Skip to content

Commit 704846d

Browse files
committed
moving obj_nonlinprog to another file
1 parent 6272cc5 commit 704846d

File tree

3 files changed

+91
-92
lines changed

3 files changed

+91
-92
lines changed

src/controller/execute.jl

Lines changed: 90 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,46 @@ end
211211
"Separate stochastic predictions are not needed if `estim` is not [`InternalModel`](@ref)."
212212
predictstoch!(::PredictiveController, ::StateEstimator, _ , _ ) = nothing
213213

214+
@doc raw"""
215+
linconstraint!(mpc::PredictiveController, model::LinModel)
216+
217+
Set `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
218+
219+
Also init ``\mathbf{f_x̂}`` vector for the terminal constraints, see [`init_predmat`](@ref).
220+
"""
221+
function linconstraint!(mpc::PredictiveController, model::LinModel)
222+
mpc.con.fx̂[:] = mpc.con.kx̂ * mpc.estim.+ mpc.con.vx̂ * mpc.estim.lastu0
223+
if model.nd 0
224+
mpc.con.fx̂[:] = mpc.con.fx̂ + mpc.con.gx̂ * mpc.d0 + mpc.con.jx̂ * mpc.D̂0
225+
end
226+
lastu = mpc.estim.lastu0 + model.uop
227+
mpc.con.b[:] = [
228+
-mpc.con.Umin + mpc.T*lastu
229+
+mpc.con.Umax - mpc.T*lastu
230+
-mpc.con.ΔŨmin
231+
+mpc.con.ΔŨmax
232+
-mpc.con.Ymin + mpc.F
233+
+mpc.con.Ymax - mpc.F
234+
-mpc.con.x̂min + mpc.con.fx̂
235+
+mpc.con.x̂max - mpc.con.fx̂
236+
]
237+
lincon = mpc.optim[:linconstraint]
238+
set_normalized_rhs.(lincon, mpc.con.b[mpc.con.i_b])
239+
end
240+
241+
"Set `b` excluding predicted output constraints when `model` is not a [`LinModel`](@ref)."
242+
function linconstraint!(mpc::PredictiveController, model::SimModel)
243+
lastu = mpc.estim.lastu0 + model.uop
244+
mpc.con.b[:] = [
245+
-mpc.con.Umin + mpc.T*lastu
246+
+mpc.con.Umax - mpc.T*lastu
247+
-mpc.con.ΔŨmin
248+
+mpc.con.ΔŨmax
249+
]
250+
lincon = mpc.optim[:linconstraint]
251+
set_normalized_rhs.(lincon, mpc.con.b[mpc.con.i_b])
252+
end
253+
214254
@doc raw"""
215255
predict!(Ŷ, x̂, mpc::PredictiveController, model::LinModel, ΔŨ) -> Ŷ, x̂end
216256
@@ -250,44 +290,62 @@ function predict!(Ŷ, x̂, mpc::PredictiveController, model::SimModel, ΔŨ::V
250290
return Ŷ, x̂end
251291
end
252292

253-
@doc raw"""
254-
linconstraint!(mpc::PredictiveController, model::LinModel)
293+
"""
294+
obj_nonlinprog(mpc::PredictiveController, model::LinModel, Ŷ, ΔŨ)
255295
256-
Set `b` vector for the linear model inequality constraints (``\mathbf{A ΔŨ ≤ b}``).
296+
Nonlinear programming objective function when `model` is a [`LinModel`](@ref).
257297
258-
Also init ``\mathbf{f_x̂}`` vector for the terminal constraints, see [`init_predmat`](@ref).
298+
The function is called by the nonlinear optimizer of [`NonLinMPC`](@ref) controllers. It can
299+
also be called on any [`PredictiveController`](@ref)s to evaluate the objective function `J`
300+
at specific input increments `ΔŨ` and predictions `Ŷ` values.
259301
"""
260-
function linconstraint!(mpc::PredictiveController, model::LinModel)
261-
mpc.con.fx̂[:] = mpc.con.kx̂ * mpc.estim.+ mpc.con.vx̂ * mpc.estim.lastu0
262-
if model.nd 0
263-
mpc.con.fx̂[:] = mpc.con.fx̂ + mpc.con.gx̂ * mpc.d0 + mpc.con.jx̂ * mpc.D̂0
302+
function obj_nonlinprog(
303+
mpc::PredictiveController, model::LinModel, Ŷ, ΔŨ::Vector{NT}
304+
) where {NT<:Real}
305+
J = obj_quadprog(ΔŨ, mpc.H̃, mpc.q̃) + mpc.p[]
306+
if !iszero(mpc.E)
307+
U = mpc.*ΔŨ + mpc.T*(mpc.estim.lastu0 + model.uop)
308+
UE = [U; U[(end - model.nu + 1):end]]
309+
ŶE = [mpc.ŷ; Ŷ]
310+
J += mpc.E*mpc.JE(UE, ŶE, mpc.D̂E)
264311
end
265-
lastu = mpc.estim.lastu0 + model.uop
266-
mpc.con.b[:] = [
267-
-mpc.con.Umin + mpc.T*lastu
268-
+mpc.con.Umax - mpc.T*lastu
269-
-mpc.con.ΔŨmin
270-
+mpc.con.ΔŨmax
271-
-mpc.con.Ymin + mpc.F
272-
+mpc.con.Ymax - mpc.F
273-
-mpc.con.x̂min + mpc.con.fx̂
274-
+mpc.con.x̂max - mpc.con.fx̂
275-
]
276-
lincon = mpc.optim[:linconstraint]
277-
set_normalized_rhs.(lincon, mpc.con.b[mpc.con.i_b])
312+
return J
278313
end
279314

280-
"Set `b` excluding predicted output constraints when `model` is not a [`LinModel`](@ref)."
281-
function linconstraint!(mpc::PredictiveController, model::SimModel)
282-
lastu = mpc.estim.lastu0 + model.uop
283-
mpc.con.b[:] = [
284-
-mpc.con.Umin + mpc.T*lastu
285-
+mpc.con.Umax - mpc.T*lastu
286-
-mpc.con.ΔŨmin
287-
+mpc.con.ΔŨmax
288-
]
289-
lincon = mpc.optim[:linconstraint]
290-
set_normalized_rhs.(lincon, mpc.con.b[mpc.con.i_b])
315+
"""
316+
obj_nonlinprog(mpc::PredictiveController, model::SimModel, Ŷ, ΔŨ)
317+
318+
Nonlinear programming objective function when `model` is not a [`LinModel`](@ref). The
319+
function `dot(x, A, x)` is a performant way of calculating `x'*A*x`.
320+
"""
321+
function obj_nonlinprog(
322+
mpc::PredictiveController, model::SimModel, Ŷ, ΔŨ::Vector{NT}
323+
) where {NT<:Real}
324+
# --- output setpoint tracking term ---
325+
êy = mpc.R̂y -
326+
JR̂y = dot(êy, mpc.M_Hp, êy)
327+
# --- move suppression and slack variable term ---
328+
JΔŨ = dot(ΔŨ, mpc.Ñ_Hc, ΔŨ)
329+
# --- input over prediction horizon ---
330+
if !mpc.noR̂u || !iszero(mpc.E)
331+
U = mpc.*ΔŨ + mpc.T*(mpc.estim.lastu0 + model.uop)
332+
end
333+
# --- input setpoint tracking term ---
334+
if !mpc.noR̂u
335+
êu = mpc.R̂u - U
336+
JR̂u = dot(êu, mpc.L_Hp, êu)
337+
else
338+
JR̂u = 0.0
339+
end
340+
# --- economic term ---
341+
if !iszero(mpc.E)
342+
UE = [U; U[(end - model.nu + 1):end]]
343+
ŶE = [mpc.ŷ; Ŷ]
344+
E_JE = mpc.E*mpc.JE(UE, ŶE, mpc.D̂E)
345+
else
346+
E_JE = 0.0
347+
end
348+
return JR̂y + JΔŨ + JR̂u + E_JE
291349
end
292350

293351
"""

src/estimator/mhe/construct.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ end
229229

230230
@doc raw"""
231231
init_predmat_mhe(
232-
model::LinModel{NT}, He, i_ym, Â, B̂u, Ĉ, B̂d, D̂d
232+
model::LinModel, He, i_ym, Â, B̂u, Ĉ, B̂d, D̂d
233233
) -> E, F, G, J, ex̄, fx̄, Ex̂, Fx̂, Gx̂, Jx̂
234234
235235
Construct the MHE prediction matrices for [`LinModel`](@ref) `model`.

src/predictive_control.jl

Lines changed: 0 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -68,65 +68,6 @@ Set the estimate at `mpc.estim.x̂`.
6868
"""
6969
setstate!(mpc::PredictiveController, x̂) = (setstate!(mpc.estim, x̂); return mpc)
7070

71-
72-
"""
73-
obj_nonlinprog(mpc::PredictiveController, model::LinModel, Ŷ, ΔŨ)
74-
75-
Nonlinear programming objective function when `model` is a [`LinModel`](@ref).
76-
77-
The function is called by the nonlinear optimizer of [`NonLinMPC`](@ref) controllers. It can
78-
also be called on any [`PredictiveController`](@ref)s to evaluate the objective function `J`
79-
at specific input increments `ΔŨ` and predictions `Ŷ` values.
80-
"""
81-
function obj_nonlinprog(
82-
mpc::PredictiveController, model::LinModel, Ŷ, ΔŨ::Vector{NT}
83-
) where {NT<:Real}
84-
J = obj_quadprog(ΔŨ, mpc.H̃, mpc.q̃) + mpc.p[]
85-
if !iszero(mpc.E)
86-
U = mpc.*ΔŨ + mpc.T*(mpc.estim.lastu0 + model.uop)
87-
UE = [U; U[(end - model.nu + 1):end]]
88-
ŶE = [mpc.ŷ; Ŷ]
89-
J += mpc.E*mpc.JE(UE, ŶE, mpc.D̂E)
90-
end
91-
return J
92-
end
93-
94-
"""
95-
obj_nonlinprog(mpc::PredictiveController, model::SimModel, Ŷ, ΔŨ)
96-
97-
Nonlinear programming objective function when `model` is not a [`LinModel`](@ref). The
98-
function `dot(x, A, x)` is a performant way of calculating `x'*A*x`.
99-
"""
100-
function obj_nonlinprog(
101-
mpc::PredictiveController, model::SimModel, Ŷ, ΔŨ::Vector{NT}
102-
) where {NT<:Real}
103-
# --- output setpoint tracking term ---
104-
êy = mpc.R̂y -
105-
JR̂y = dot(êy, mpc.M_Hp, êy)
106-
# --- move suppression and slack variable term ---
107-
JΔŨ = dot(ΔŨ, mpc.Ñ_Hc, ΔŨ)
108-
# --- input over prediction horizon ---
109-
if !mpc.noR̂u || !iszero(mpc.E)
110-
U = mpc.*ΔŨ + mpc.T*(mpc.estim.lastu0 + model.uop)
111-
end
112-
# --- input setpoint tracking term ---
113-
if !mpc.noR̂u
114-
êu = mpc.R̂u - U
115-
JR̂u = dot(êu, mpc.L_Hp, êu)
116-
else
117-
JR̂u = 0.0
118-
end
119-
# --- economic term ---
120-
if !iszero(mpc.E)
121-
UE = [U; U[(end - model.nu + 1):end]]
122-
ŶE = [mpc.ŷ; Ŷ]
123-
E_JE = mpc.E*mpc.JE(UE, ŶE, mpc.D̂E)
124-
else
125-
E_JE = 0.0
126-
end
127-
return JR̂y + JΔŨ + JR̂u + E_JE
128-
end
129-
13071
function Base.show(io::IO, mpc::PredictiveController)
13172
Hp, Hc = mpc.Hp, mpc.Hc
13273
nu, nd = mpc.estim.model.nu, mpc.estim.model.nd

0 commit comments

Comments
 (0)