@@ -196,8 +196,10 @@ function NonLinMPC(
196
196
return NonLinMPC {S, JEFunc} (estim, Hp, Hc, Mwt, Nwt, Lwt, Cwt, Ewt, JE, ru, optim)
197
197
end
198
198
199
- setnontlincon! (mpc:: NonLinMPC , model:: LinModel ) = nothing
199
+ " No nonlinear constraint for [`NonLinMPC`](@ref) based on [`LinModel`](@ref)."
200
+ setnontlincon! (:: NonLinMPC , :: LinModel ) = nothing
200
201
202
+ " Set the nonlinear constraints on the output predictions `Ŷ`."
201
203
function setnonlincon! (mpc:: NonLinMPC , model:: NonLinModel )
202
204
optim = mpc. optim
203
205
ΔŨ = mpc. optim[:ΔŨ ]
@@ -214,8 +216,15 @@ function setnonlincon!(mpc::NonLinMPC, model::NonLinModel)
214
216
return nothing
215
217
end
216
218
219
+ " Nonlinear programming objective does not require any initialization."
217
220
init_objective! (mpc:: NonLinMPC , _ ) = nothing
218
221
222
+
223
+ """
224
+ obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) where {N, T}
225
+
226
+ TBW
227
+ """
219
228
function obj_nonlinprog (mpc:: NonLinMPC , model:: LinModel , ΔŨ:: NTuple{N, T} ) where {N, T}
220
229
ΔŨ = collect (ΔŨ) # convert NTuple to Vector
221
230
Jqp = obj_quadprog (ΔŨ, mpc. P̃, mpc. q̃)
@@ -226,6 +235,11 @@ function obj_nonlinprog(mpc::NonLinMPC, model::LinModel, ΔŨ::NTuple{N, T}) wh
226
235
return Jqp + mpc. E* mpc. JE (UE, ŶE, D̂E)
227
236
end
228
237
238
+ """
239
+ obj_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
240
+
241
+ TBW
242
+ """
229
243
function obj_nonlinprog (mpc:: NonLinMPC , model:: SimModel , ΔŨ:: NTuple{N, T} ) where {N, T}
230
244
ΔŨ = collect (ΔŨ) # convert NTuple to Vector
231
245
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
252
266
return JR̂y + JΔŨ + JR̂u + Jϵ + mpc. E* mpc. JE (UE, ŶE, D̂E)
253
267
end
254
268
269
+ """
270
+ con_nonlinprog(mpc::NonLinMPC, model::SimModel, ΔŨ::NTuple{N, T}) where {N, T}
271
+
272
+ TBW
273
+ """
255
274
function con_nonlinprog (mpc:: NonLinMPC , model:: SimModel , ΔŨ:: NTuple{N, T} ) where {N, T}
256
275
ΔŨ = collect (ΔŨ) # convert NTuple to Vector
257
276
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}
280
299
end
281
300
282
301
"""
283
- memoize(myfunc ::Function, n_outputs::Int)
302
+ memoize(f ::Function, n_outputs::Int)
284
303
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.
287
305
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`.
291
310
"""
292
311
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
298
317
end
299
318
return last_f[i]
300
319
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
306
325
end
307
- return last_dfdΔŨ [i]
326
+ return last_dfdx [i]
308
327
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]
310
329
end
0 commit comments