@@ -196,8 +196,10 @@ function NonLinMPC(
196196 return NonLinMPC {S, JEFunc} (estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, Ewt, JE, ru, optim)
197197end
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 `Ŷ`."
201203function 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
215217end
216218
219+ " Nonlinear programming objective does not require any initialization."
217220init_objective! (mpc:: NonLinMPC , _ ) = nothing
218221
222+
223+ """
224+ obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) where {N, T}
225+
226+ TBW
227+ """
219228function 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)
227236end
228237
238+ """
239+ obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
240+
241+ TBW
242+ """
229243function 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 + Jϵ + mpc. E* mpc. JE (UE, ŶE, D̂E)
253267end
254268
269+ """
270+ con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
271+
272+ TBW
273+ """
255274function 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}
280299end
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"""
292311function 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]
310329end
0 commit comments