Skip to content

Commit 694a49b

Browse files
committed
added init_nonlinprog! function
1 parent 21189c7 commit 694a49b

File tree

1 file changed

+62
-55
lines changed

1 file changed

+62
-55
lines changed

src/controller/nonlinmpc.jl

Lines changed: 62 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -73,59 +73,7 @@ struct NonLinMPC{S<:StateEstimator, JEFunc<:Function} <: PredictiveController
7373
A = con.A[con.i_b, :]
7474
b = con.b[con.i_b]
7575
@constraint(optim, linconstraint, A*ΔŨ .≤ b)
76-
77-
last_ΔŨtup, last_C, last_Ŷ = nothing, nothing, nothing
78-
function Jfunc(ΔŨtup::Float64...)
79-
ΔŨvec = collect(ΔŨtup)
80-
if ΔŨtup !== last_ΔŨtup
81-
last_Ŷ = predict(mpc, model, ΔŨvec)
82-
last_C = con_nonlinprog(mpc, model, last_Ŷ, ΔŨvec)
83-
last_ΔŨtup = ΔŨtup
84-
end
85-
return obj_nonlinprog(mpc, model, last_Ŷ, ΔŨvec)
86-
end
87-
last_dΔŨtup, last_dC, last_dŶ = nothing, nothing, nothing
88-
function Jfunc(dΔŨtup::T...) where {T<:Real}
89-
dΔŨvec = collect(dΔŨtup)
90-
if dΔŨtup !== last_dΔŨtup
91-
last_dŶ = predict(mpc, model, dΔŨvec)
92-
last_dC = con_nonlinprog(mpc, model, last_dŶ, dΔŨvec)
93-
last_dΔŨtup = dΔŨtup
94-
end
95-
return obj_nonlinprog(mpc, model, last_dŶ, dΔŨvec)
96-
end
97-
register(optim, :Jfunc, nvar, Jfunc, autodiff=true)
98-
@NLobjective(optim, Min, Jfunc(ΔŨ...))
99-
ncon = length(mpc.con.Ŷmin) + length(mpc.con.Ŷmax)
100-
function con_nonlinprog_i(i, ΔŨtup::NTuple{N, Float64}) where {N}
101-
if ΔŨtup !== last_ΔŨtup
102-
ΔŨvec = collect(ΔŨtup)
103-
last_Ŷ = predict(mpc, model, ΔŨvec)
104-
last_C = con_nonlinprog(mpc, model, last_Ŷ, ΔŨvec)
105-
last_ΔŨtup = ΔŨtup
106-
end
107-
return last_C[i]
108-
end
109-
function con_nonlinprog_i(i, dΔŨtup::NTuple{N, T}) where {N, T<:Real}
110-
if dΔŨtup !== last_dΔŨtup
111-
dΔŨvec = collect(dΔŨtup)
112-
last_dŶ = predict(mpc, model, dΔŨvec)
113-
last_dC = con_nonlinprog(mpc, model, last_dŶ, dΔŨvec)
114-
last_dΔŨtup = dΔŨtup
115-
end
116-
return last_dC[i]
117-
end
118-
Cfunc = [(ΔŨ...) -> con_nonlinprog_i(i, ΔŨ) for i in 1:ncon]
119-
n = 0
120-
for i in eachindex(con.Ŷmin)
121-
sym = Symbol("C_Ŷmin_$i")
122-
register(optim, sym, nvar, Cfunc[n + i], autodiff=true)
123-
end
124-
n = lastindex(con.Ŷmin)
125-
for i in eachindex(con.Ŷmax)
126-
sym = Symbol("C_Ŷmax_$i")
127-
register(optim, sym, nvar, Cfunc[n + i], autodiff=true)
128-
end
76+
init_nonlinprog!(mpc, model)
12977
set_silent(optim)
13078
return mpc
13179
end
@@ -245,6 +193,66 @@ function NonLinMPC(
245193
return NonLinMPC{S, JEFunc}(estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, Ewt, JE, ru, optim)
246194
end
247195

196+
197+
function init_nonlinprog!(mpc::NonLinMPC, model::SimModel)
198+
optim, con = mpc.optim, mpc.con
199+
ΔŨvar = optim[:ΔŨ]
200+
nvar = length(ΔŨvar)
201+
last_ΔŨtup, C, Ŷ = nothing, nothing, nothing
202+
last_dΔŨtup, dC, dŶ = nothing, nothing, nothing
203+
function Jfunc(ΔŨtup::Float64...)
204+
ΔŨ = collect(ΔŨtup)
205+
if ΔŨtup !== last_ΔŨtup
206+
= predict(mpc, model, ΔŨ)
207+
C = con_nonlinprog(mpc, model, Ŷ, ΔŨ)
208+
last_ΔŨtup = ΔŨtup
209+
end
210+
return obj_nonlinprog(mpc, model, Ŷ, ΔŨ)
211+
end
212+
function Jfunc(dΔŨtup::T...) where {T<:Real}
213+
dΔŨ = collect(dΔŨtup)
214+
if dΔŨtup !== last_dΔŨtup
215+
dŶ = predict(mpc, model, dΔŨ)
216+
dC = con_nonlinprog(mpc, model, dŶ, dΔŨ)
217+
last_dΔŨtup = dΔŨtup
218+
end
219+
return obj_nonlinprog(mpc, model, dŶ, dΔŨ)
220+
end
221+
function con_nonlinprog_i(i, ΔŨtup::NTuple{N, Float64}) where {N}
222+
if ΔŨtup !== last_ΔŨtup
223+
ΔŨ = collect(ΔŨtup)
224+
= predict(mpc, model, ΔŨ)
225+
C = con_nonlinprog(mpc, model, Ŷ, ΔŨ)
226+
last_ΔŨtup = ΔŨtup
227+
end
228+
return C[i]
229+
end
230+
function con_nonlinprog_i(i, dΔŨtup::NTuple{N, T}) where {N, T<:Real}
231+
if dΔŨtup !== last_dΔŨtup
232+
dΔŨ = collect(dΔŨtup)
233+
dŶ = predict(mpc, model, dΔŨ)
234+
dC = con_nonlinprog(mpc, model, dŶ, dΔŨ)
235+
last_dΔŨtup = dΔŨtup
236+
end
237+
return dC[i]
238+
end
239+
ncon = length(mpc.con.Ŷmin) + length(mpc.con.Ŷmax)
240+
Cfunc = [(ΔŨ...) -> con_nonlinprog_i(i, ΔŨ) for i in 1:ncon]
241+
register(optim, :Jfunc, nvar, Jfunc, autodiff=true)
242+
@NLobjective(optim, Min, Jfunc(ΔŨvar...))
243+
n = 0
244+
for i in eachindex(con.Ŷmin)
245+
sym = Symbol("C_Ŷmin_$i")
246+
register(optim, sym, nvar, Cfunc[n + i], autodiff=true)
247+
end
248+
n = lastindex(con.Ŷmin)
249+
for i in eachindex(con.Ŷmax)
250+
sym = Symbol("C_Ŷmax_$i")
251+
register(optim, sym, nvar, Cfunc[n + i], autodiff=true)
252+
end
253+
end
254+
255+
248256
"No nonlinear constraint for [`NonLinMPC`](@ref) based on [`LinModel`](@ref)."
249257
setnontlincon!(::NonLinMPC, ::LinModel) = nothing
250258

@@ -289,14 +297,13 @@ end
289297
Objective function for [`NonLinMPC`] when `model` is not a [`LinModel`](@ref).
290298
"""
291299
function obj_nonlinprog(mpc::NonLinMPC, model::SimModel, Ŷ, ΔŨ::Vector{T}) where {T<:Real}
292-
U0 = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0)
293300
# --- output setpoint tracking term ---
294301
êy = mpc.R̂y -
295302
JR̂y = êy'*mpc.M_Hp*êy
296303
# --- move suppression term ---
297304
JΔŨ = ΔŨ'*mpc.Ñ_Hc*ΔŨ
298305
# --- input setpoint tracking term ---
299-
U = U0 + mpc.Uop
306+
U = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0 + model.uop)
300307
if !isempty(mpc.R̂u)
301308
êu = mpc.R̂u - U
302309
JR̂u = êu'*mpc.L_Hp*

0 commit comments

Comments
 (0)