Skip to content

Commit f06caf9

Browse files
committed
remove useless splatting in memoize
1 parent 4785eb8 commit f06caf9

File tree

3 files changed

+42
-20
lines changed

3 files changed

+42
-20
lines changed

src/controller/linmpc.jl

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,8 +198,10 @@ function LinMPC(
198198
return LinMPC{S}(estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, ru, optim)
199199
end
200200

201-
setnontlincon!(mpc::LinMPC, model::LinModel) = nothing
201+
"No nonlinear constraint for [`LinMPC`](@ref) controller."
202+
setnonlincon!(::LinMPC, ::LinModel) = nothing
202203

204+
"Init quadratic programming `q̃` vector just before optimization."
203205
function init_objective!(mpc::LinMPC, ΔŨ)
204206
set_objective_function(mpc.optim, obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃))
205207
return nothing

src/controller/nonlinmpc.jl

Lines changed: 38 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,10 @@ function NonLinMPC(
196196
return NonLinMPC{S, JEFunc}(estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, Ewt, JE, ru, optim)
197197
end
198198

199-
setnontlincon!(mpc::NonLinMPC, model::LinModel) = nothing
199+
"No nonlinear constraint for [`NonLinMPC`](@ref) based on [`LinModel`](@ref)."
200+
setnontlincon!(::NonLinMPC, ::LinModel) = nothing
200201

202+
"Set the nonlinear constraints on the output predictions `Ŷ`."
201203
function setnonlincon!(mpc::NonLinMPC, model::NonLinModel)
202204
optim = mpc.optim
203205
ΔŨ = mpc.optim[:ΔŨ]
@@ -214,8 +216,15 @@ function setnonlincon!(mpc::NonLinMPC, model::NonLinModel)
214216
return nothing
215217
end
216218

219+
"Nonlinear programming objective does not require any initialization."
217220
init_objective!(mpc::NonLinMPC, _ ) = nothing
218221

222+
223+
"""
224+
obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) where {N, T}
225+
226+
TBW
227+
"""
219228
function obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) where {N, T}
220229
ΔŨ = collect(ΔŨ) # convert NTuple to Vector
221230
Jqp = obj_quadprog(ΔŨ, mpc.P̃, mpc.q̃)
@@ -226,6 +235,11 @@ function obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) wh
226235
return Jqp + mpc.E*mpc.JE(UE, ŶE, D̂E)
227236
end
228237

238+
"""
239+
obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
240+
241+
TBW
242+
"""
229243
function obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
230244
ΔŨ = collect(ΔŨ) # convert NTuple to Vector
231245
U0 = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0)
@@ -252,6 +266,11 @@ function obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) wh
252266
return JR̂y + JΔŨ + JR̂u ++ mpc.E*mpc.JE(UE, ŶE, D̂E)
253267
end
254268

269+
"""
270+
con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
271+
272+
TBW
273+
"""
255274
function con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
256275
ΔŨ = collect(ΔŨ) # convert NTuple to Vector
257276
U0 = mpc.S̃_Hp*ΔŨ + mpc.T_Hp*(mpc.estim.lastu0)
@@ -280,31 +299,31 @@ function evalŶ(mpc, model, x̂d, d0, D̂0, U0::Vector{T}) where {T}
280299
end
281300

282301
"""
283-
memoize(myfunc::Function, n_outputs::Int)
302+
memoize(f::Function, n_outputs::Int)
284303
285-
Take a function `myfunc` and return a vector of length `n_outputs`, where element
286-
`i` is a function that returns the equivalent of `myfunc(x...)[i]`.
304+
Memoize `f` to reduce the computational cost of [`NonLinMPC`](@ref) controllers.
287305
288-
To avoid duplication of work, cache the most-recent evaluations of `myfunc`.
289-
Because `myfunc_i` is auto-differentiated with ForwardDiff, our cache needs to
290-
work when `x` is a `Float64` and a `ForwardDiff.Dual`.
306+
Take a function `f` and return a vector of length `n_outputs`, where element `i` is a
307+
function that returns the equivalent of `f(x...)[i]`. To avoid duplication of work, cache
308+
the most-recent evaluations of `f`. Because `f_i` is auto-differentiated with ForwardDiff,
309+
our cache needs to work when `x` is a `Float64` and a `ForwardDiff.Dual`.
291310
"""
292311
function memoize(f::Function, n_outputs::Int)
293-
last_ΔŨ , last_f = nothing, nothing
294-
function f_i(i, ΔŨ::Float64...)
295-
if ΔŨ !== last_ΔŨ
296-
last_f = f(ΔŨ...)
297-
last_ΔŨ = ΔŨ
312+
last_x , last_f = nothing, nothing
313+
function f_i(i, x::NTuple{N, Float64}) where {N}
314+
if x !== last_x
315+
last_f = f(x...)
316+
last_x = x
298317
end
299318
return last_f[i]
300319
end
301-
last_dΔŨ, last_dfdΔŨ = nothing, nothing
302-
function f_i(i, dΔŨ::T...) where {T<:Real}
303-
if dΔŨ !== last_dΔŨ
304-
last_dfdΔŨ = f(dΔŨ...)
305-
last_dΔŨ = dΔŨ
320+
last_dx, last_dfdx = nothing, nothing
321+
function f_i(i, dx::NTuple{N, T}) where {N, T<:Real}
322+
if dx !== last_dx
323+
last_dfdx = f(dx...)
324+
last_dx = dx
306325
end
307-
return last_dfdΔŨ[i]
326+
return last_dfdx[i]
308327
end
309-
return [(x...) -> f_i(i, x...) for i in 1:n_outputs]
328+
return [(x...) -> f_i(i, x) for i in 1:n_outputs]
310329
end

src/predictive_control.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -188,6 +188,7 @@ function setconstraint!(
188188
return mpc
189189
end
190190

191+
"By default, there is no nonlinear constraint"
191192
setnonlincon!(::PredictiveController, ::SimModel) = nothing
192193

193194
@doc raw"""

0 commit comments

Comments
 (0)