@@ -258,21 +258,21 @@ function init_optimization!(mpc::NonLinMPC)
258258 @constraint (optim, linconstraint, A* ΔŨvar .≤ b)
259259 # --- nonlinear optimization init ---
260260 model = mpc. estim. model
261- ny, nx̂, Hp, nC = model. ny, mpc. estim. nx̂, mpc. Hp, length (con. i_C )
261+ ny, nx̂, Hp, ng = model. ny, mpc. estim. nx̂, mpc. Hp, length (con. i_g )
262262 # inspired from https://jump.dev/JuMP.jl/stable/tutorials/nonlinear/tips_and_tricks/#User-defined-operators-with-vector-outputs
263- Jfunc, Cfunc = let mpc= mpc, model= model, nC = nC , nvar= nvar , nŶ= Hp* ny, nx̂= nx̂
263+ Jfunc, gfunc = let mpc= mpc, model= model, ng = ng , nvar= nvar , nŶ= Hp* ny, nx̂= nx̂
264264 last_ΔŨtup_float, last_ΔŨtup_dual = nothing , nothing
265265 Ŷ_cache:: DiffCacheType = DiffCache (zeros (nŶ), nvar + 3 )
266- C_cache :: DiffCacheType = DiffCache (zeros (nC ), nvar + 3 )
266+ g_cache :: DiffCacheType = DiffCache (zeros (ng ), nvar + 3 )
267267 x̂_cache:: DiffCacheType = DiffCache (zeros (nx̂), nvar + 3 )
268268 function Jfunc (ΔŨtup:: Float64... )
269269 Ŷ = get_tmp (Ŷ_cache, ΔŨtup[1 ])
270270 ΔŨ = collect (ΔŨtup)
271271 if ΔŨtup != last_ΔŨtup_float
272272 x̂ = get_tmp (x̂_cache, ΔŨtup[1 ])
273- C = get_tmp (C_cache , ΔŨtup[1 ])
273+ g = get_tmp (g_cache , ΔŨtup[1 ])
274274 Ŷ, x̂end = predict! (Ŷ, x̂, mpc, model, ΔŨ)
275- con_nonlinprog! (C , mpc, model, x̂end , Ŷ, ΔŨ)
275+ con_nonlinprog! (g , mpc, model, x̂end , Ŷ, ΔŨ)
276276 last_ΔŨtup_float = ΔŨtup
277277 end
278278 return obj_nonlinprog (mpc, model, Ŷ, ΔŨ)
@@ -282,59 +282,59 @@ function init_optimization!(mpc::NonLinMPC)
282282 ΔŨ = collect (ΔŨtup)
283283 if ΔŨtup != last_ΔŨtup_dual
284284 x̂ = get_tmp (x̂_cache, ΔŨtup[1 ])
285- C = get_tmp (C_cache , ΔŨtup[1 ])
285+ g = get_tmp (g_cache , ΔŨtup[1 ])
286286 Ŷ, x̂end = predict! (Ŷ, x̂, mpc, model, ΔŨ)
287- con_nonlinprog! (C , mpc, model, x̂end , Ŷ, ΔŨ)
287+ con_nonlinprog! (g , mpc, model, x̂end , Ŷ, ΔŨ)
288288 last_ΔŨtup_dual = ΔŨtup
289289 end
290290 return obj_nonlinprog (mpc, model, Ŷ, ΔŨ)
291291 end
292- function con_nonlinprog_i (i, ΔŨtup:: NTuple{N, Float64} ) where {N}
293- C = get_tmp (C_cache , ΔŨtup[1 ])
292+ function gfunc_i (i, ΔŨtup:: NTuple{N, Float64} ) where {N}
293+ g = get_tmp (g_cache , ΔŨtup[1 ])
294294 if ΔŨtup != last_ΔŨtup_float
295295 x̂ = get_tmp (x̂_cache, ΔŨtup[1 ])
296296 Ŷ = get_tmp (Ŷ_cache, ΔŨtup[1 ])
297297 ΔŨ = collect (ΔŨtup)
298298 Ŷ, x̂end = predict! (Ŷ, x̂, mpc, model, ΔŨ)
299- C = con_nonlinprog! (C , mpc, model, x̂end , Ŷ, ΔŨ)
299+ g = con_nonlinprog! (g , mpc, model, x̂end , Ŷ, ΔŨ)
300300 last_ΔŨtup_float = ΔŨtup
301301 end
302- return C [i]
302+ return g [i]
303303 end
304- function con_nonlinprog_i (i, ΔŨtup:: NTuple{N, Real} ) where {N}
305- C = get_tmp (C_cache , ΔŨtup[1 ])
304+ function gfunc_i (i, ΔŨtup:: NTuple{N, Real} ) where {N}
305+ g = get_tmp (g_cache , ΔŨtup[1 ])
306306 if ΔŨtup != last_ΔŨtup_dual
307307 x̂ = get_tmp (x̂_cache, ΔŨtup[1 ])
308308 Ŷ = get_tmp (Ŷ_cache, ΔŨtup[1 ])
309309 ΔŨ = collect (ΔŨtup)
310310 Ŷ, x̂end = predict! (Ŷ, x̂, mpc, model, ΔŨ)
311- C = con_nonlinprog! (C , mpc, model, x̂end , Ŷ, ΔŨ)
311+ g = con_nonlinprog! (g , mpc, model, x̂end , Ŷ, ΔŨ)
312312 last_ΔŨtup_dual = ΔŨtup
313313 end
314- return C [i]
314+ return g [i]
315315 end
316- Cfunc = [(ΔŨ... ) -> con_nonlinprog_i (i, ΔŨ) for i in 1 : nC ]
317- (Jfunc, Cfunc )
316+ gfunc = [(ΔŨ... ) -> gfunc_i (i, ΔŨ) for i in 1 : ng ]
317+ (Jfunc, gfunc )
318318 end
319319 register (optim, :Jfunc , nvar, Jfunc, autodiff= true )
320320 @NLobjective (optim, Min, Jfunc (ΔŨvar... ))
321- if nC ≠ 0
321+ if ng ≠ 0
322322 i_end_Ymin, i_end_Ymax, i_end_x̂min = 1 Hp* ny, 2 Hp* ny, 2 Hp* ny + nx̂
323323 for i in eachindex (con. Ymin)
324- sym = Symbol (" C_Ymin_ $i " )
325- register (optim, sym, nvar, Cfunc [i], autodiff= true )
324+ sym = Symbol (" g_Ymin_ $i " )
325+ register (optim, sym, nvar, gfunc [i], autodiff= true )
326326 end
327327 for i in eachindex (con. Ymax)
328- sym = Symbol (" C_Ymax_ $i " )
329- register (optim, sym, nvar, Cfunc [i_end_Ymin+ i], autodiff= true )
328+ sym = Symbol (" g_Ymax_ $i " )
329+ register (optim, sym, nvar, gfunc [i_end_Ymin+ i], autodiff= true )
330330 end
331331 for i in eachindex (con. x̂min)
332- sym = Symbol (" C_x̂min_ $i " )
333- register (optim, sym, nvar, Cfunc [i_end_Ymax+ i], autodiff= true )
332+ sym = Symbol (" g_x̂min_ $i " )
333+ register (optim, sym, nvar, gfunc [i_end_Ymax+ i], autodiff= true )
334334 end
335335 for i in eachindex (con. x̂max)
336- sym = Symbol (" C_x̂max_ $i " )
337- register (optim, sym, nvar, Cfunc [i_end_x̂min+ i], autodiff= true )
336+ sym = Symbol (" g_x̂max_ $i " )
337+ register (optim, sym, nvar, gfunc [i_end_x̂min+ i], autodiff= true )
338338 end
339339 end
340340 return nothing
@@ -347,52 +347,52 @@ function setnonlincon!(mpc::NonLinMPC, ::NonLinModel)
347347 con = mpc. con
348348 map (con -> delete (optim, con), all_nonlinear_constraints (optim))
349349 for i in findall (.! isinf .(con. Ymin))
350- f_sym = Symbol (" C_Ymin_ $(i) " )
350+ f_sym = Symbol (" g_Ymin_ $(i) " )
351351 add_nonlinear_constraint (optim, :($ (f_sym)($ (ΔŨvar... )) <= 0 ))
352352 end
353353 for i in findall (.! isinf .(con. Ymax))
354- f_sym = Symbol (" C_Ymax_ $(i) " )
354+ f_sym = Symbol (" g_Ymax_ $(i) " )
355355 add_nonlinear_constraint (optim, :($ (f_sym)($ (ΔŨvar... )) <= 0 ))
356356 end
357357 for i in findall (.! isinf .(con. x̂min))
358- f_sym = Symbol (" C_x̂min_ $(i) " )
358+ f_sym = Symbol (" g_x̂min_ $(i) " )
359359 add_nonlinear_constraint (optim, :($ (f_sym)($ (ΔŨvar... )) <= 0 ))
360360 end
361361 for i in findall (.! isinf .(con. x̂max))
362- f_sym = Symbol (" C_x̂max_ $(i) " )
362+ f_sym = Symbol (" g_x̂max_ $(i) " )
363363 add_nonlinear_constraint (optim, :($ (f_sym)($ (ΔŨvar... )) <= 0 ))
364364 end
365365 return nothing
366366end
367367
368368"""
369- con_nonlinprog!(C , mpc::NonLinMPC, model::SimModel, x̂end, Ŷ, ΔŨ) -> C
369+ con_nonlinprog!(g , mpc::NonLinMPC, model::SimModel, x̂end, Ŷ, ΔŨ) -> g
370370
371371Nonlinear constrains for [`NonLinMPC`](@ref) when `model` is not a [`LinModel`](@ref).
372372
373- The method mutates the `C ` vector in argument and returns it.
373+ The method mutates the `g ` vector in argument and returns it.
374374"""
375- function con_nonlinprog! (C , mpc:: NonLinMPC , :: SimModel , x̂end , Ŷ, ΔŨ)
375+ function con_nonlinprog! (g , mpc:: NonLinMPC , :: SimModel , x̂end , Ŷ, ΔŨ)
376376 nx̂, nŶ = mpc. estim. nx̂, length (Ŷ)
377377 ϵ = ! isinf (mpc. C) ? ΔŨ[end ] : 0.0 # ϵ = 0.0 if Cwt=Inf (meaning: no relaxation)
378- for i in eachindex (C )
379- mpc. con. i_C [i] || continue
378+ for i in eachindex (g )
379+ mpc. con. i_g [i] || continue
380380 if i ≤ nŶ
381381 j = i
382- C [i] = (mpc. con. Ymin[j] - Ŷ[j]) - ϵ* mpc. con. C_ymin[j]
382+ g [i] = (mpc. con. Ymin[j] - Ŷ[j]) - ϵ* mpc. con. C_ymin[j]
383383 elseif i ≤ 2 nŶ
384384 j = i - nŶ
385- C [i] = (Ŷ[j] - mpc. con. Ymax[j]) - ϵ* mpc. con. C_ymax[j]
385+ g [i] = (Ŷ[j] - mpc. con. Ymax[j]) - ϵ* mpc. con. C_ymax[j]
386386 elseif i ≤ 2 nŶ + nx̂
387387 j = i - 2 nŶ
388- C [i] = (mpc. con. x̂min[j] - x̂end [j]) - ϵ* mpc. con. c_x̂min[j]
388+ g [i] = (mpc. con. x̂min[j] - x̂end [j]) - ϵ* mpc. con. c_x̂min[j]
389389 else
390390 j = i - 2 nŶ - nx̂
391- C [i] = (x̂end [j] - mpc. con. x̂max[j]) - ϵ* mpc. con. c_x̂max[j]
391+ g [i] = (x̂end [j] - mpc. con. x̂max[j]) - ϵ* mpc. con. c_x̂max[j]
392392 end
393393 end
394- return C
394+ return g
395395end
396396
397- " No nonlinear constraints if `model` is a [`LinModel`](@ref), return `C ` unchanged."
398- con_nonlinprog! (C , :: NonLinMPC , :: LinModel , _ , _ , _ ) = C
397+ " No nonlinear constraints if `model` is a [`LinModel`](@ref), return `g ` unchanged."
398+ con_nonlinprog! (g , :: NonLinMPC , :: LinModel , _ , _ , _ ) = g
0 commit comments