@@ -418,63 +418,65 @@ function setconstraint!(
418418 if ! isnothing (X̂min)
419419 size (X̂min) == (nX̂con,) || throw (ArgumentError (" X̂min size must be $((nX̂con,)) " ))
420420 con. x̃min[end - nx̂+ 1 : end ] = X̂min[1 : nx̂] # if C is finite : x̃ = [ϵ; x̂]
421- con. X̂min[:] = X̂min[nx̂+ 1 : end ]
421+ con. X̂min . = X̂min[nx̂+ 1 : end ]
422422 end
423423 if ! isnothing (X̂max)
424424 size (X̂max) == (nX̂con,) || throw (ArgumentError (" X̂max size must be $((nX̂con,)) " ))
425425 con. x̃max[end - nx̂+ 1 : end ] = X̂max[1 : nx̂] # if C is finite : x̃ = [ϵ; x̂]
426- con. X̂max[:] = X̂max[nx̂+ 1 : end ]
426+ con. X̂max . = X̂max[nx̂+ 1 : end ]
427427 end
428428 if ! isnothing (Ŵmin)
429429 size (Ŵmin) == (nŵ* He,) || throw (ArgumentError (" Ŵmin size must be $((nŵ* He,)) " ))
430- con. Ŵmin[:] = Ŵmin
430+ con. Ŵmin . = Ŵmin
431431 end
432432 if ! isnothing (Ŵmax)
433433 size (Ŵmax) == (nŵ* He,) || throw (ArgumentError (" Ŵmax size must be $((nŵ* He,)) " ))
434- con. Ŵmax[:] = Ŵmax
434+ con. Ŵmax . = Ŵmax
435435 end
436436 if ! isnothing (V̂min)
437437 size (V̂min) == (nym* He,) || throw (ArgumentError (" V̂min size must be $((nym* He,)) " ))
438- con. V̂min[:] = V̂min
438+ con. V̂min . = V̂min
439439 end
440440 if ! isnothing (V̂max)
441441 size (V̂max) == (nym* He,) || throw (ArgumentError (" V̂max size must be $((nym* He,)) " ))
442- con. V̂max[:] = V̂max
442+ con. V̂max . = V̂max
443443 end
444444 if ! isnothing (C_x̂min)
445445 size (C_x̂min) == (nX̂con,) || throw (ArgumentError (" C_x̂min size must be $((nX̂con,)) " ))
446446 any (C_x̂min .< 0 ) && error (" C_x̂min weights should be non-negative" )
447- con. A_x̃min[end - nx̂+ 1 : end , end ] = - C_x̂min[1 : nx̂] # if C is finite : x̃ = [ϵ; x̂]
448- con. C_x̂min[:] = C_x̂min[nx̂+ 1 : end ]
447+ # if C is finite : x̃ = [ϵ; x̂]
448+ con. A_x̃min[end - nx̂+ 1 : end , end ] .= @views - C_x̂min[1 : nx̂]
449+ con. C_x̂min .= @views C_x̂min[nx̂+ 1 : end ]
449450 size (con. A_X̂min, 1 ) ≠ 0 && (con. A_X̂min[:, end ] = - con. C_x̂min) # for LinModel
450451 end
451452 if ! isnothing (C_x̂max)
452453 size (C_x̂max) == (nX̂con,) || throw (ArgumentError (" C_x̂max size must be $((nX̂con,)) " ))
453454 any (C_x̂max .< 0 ) && error (" C_x̂max weights should be non-negative" )
454- con. A_x̃max[end - nx̂+ 1 : end , end ] = - C_x̂max[1 : nx̂] # if C is finite : x̃ = [ϵ; x̂]
455- con. C_x̂max[:] = C_x̂max[nx̂+ 1 : end ]
455+ # if C is finite : x̃ = [ϵ; x̂] :
456+ con. A_x̃max[end - nx̂+ 1 : end , end ] .= @views - C_x̂max[1 : nx̂]
457+ con. C_x̂max .= @views C_x̂max[nx̂+ 1 : end ]
456458 size (con. A_X̂max, 1 ) ≠ 0 && (con. A_X̂max[:, end ] = - con. C_x̂max) # for LinModel
457459 end
458460 if ! isnothing (C_ŵmin)
459461 size (C_ŵmin) == (nŵ* He,) || throw (ArgumentError (" C_ŵmin size must be $((nŵ* He,)) " ))
460462 any (C_ŵmin .< 0 ) && error (" C_ŵmin weights should be non-negative" )
461- con. A_Ŵmin[:, end ] = - C_ŵmin
463+ con. A_Ŵmin[:, end ] . = - C_ŵmin
462464 end
463465 if ! isnothing (C_ŵmax)
464466 size (C_ŵmax) == (nŵ* He,) || throw (ArgumentError (" C_ŵmax size must be $((nŵ* He,)) " ))
465467 any (C_ŵmax .< 0 ) && error (" C_ŵmax weights should be non-negative" )
466- con. A_Ŵmax[:, end ] = - C_ŵmax
468+ con. A_Ŵmax[:, end ] . = - C_ŵmax
467469 end
468470 if ! isnothing (C_v̂min)
469471 size (C_v̂min) == (nym* He,) || throw (ArgumentError (" C_v̂min size must be $((nym* He,)) " ))
470472 any (C_v̂min .< 0 ) && error (" C_v̂min weights should be non-negative" )
471- con. C_v̂min[:] = C_v̂min
473+ con. C_v̂min . = C_v̂min
472474 size (con. A_V̂min, 1 ) ≠ 0 && (con. A_V̂min[:, end ] = - con. C_v̂min) # for LinModel
473475 end
474476 if ! isnothing (C_v̂max)
475477 size (C_v̂max) == (nym* He,) || throw (ArgumentError (" C_v̂max size must be $((nym* He,)) " ))
476478 any (C_v̂max .< 0 ) && error (" C_v̂max weights should be non-negative" )
477- con. C_v̂max[:] = C_v̂max
479+ con. C_v̂max . = C_v̂max
478480 size (con. A_V̂max, 1 ) ≠ 0 && (con. A_V̂max[:, end ] = - con. C_v̂max) # for LinModel
479481 end
480482 i_x̃min, i_x̃max = .! isinf .(con. x̃min) , .! isinf .(con. x̃max)
@@ -989,40 +991,46 @@ function init_optimization!(
989991 He = estim. He
990992 nV̂, nX̂, ng = He* estim. nym, He* estim. nx̂, length (con. i_g)
991993 # see init_optimization!(mpc::NonLinMPC, optim) for details on the inspiration
992- Jfunc, gfunc = let estim= estim, model= model, nZ̃= nZ̃ , nV̂= nV̂, nX̂= nX̂, ng= ng
994+ Jfunc, gfunc = let estim= estim, model= model, nZ̃= nZ̃ , nV̂= nV̂, nX̂= nX̂, ng= ng, nx̂ = estim . nx̂
993995 last_Z̃tup_float, last_Z̃tup_dual = nothing , nothing
994996 V̂_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nV̂), nZ̃ + 3 )
995997 g_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, ng), nZ̃ + 3 )
996998 X̂_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nX̂), nZ̃ + 3 )
999+ x̄_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nx̂), nZ̃ + 3 )
9971000 function Jfunc (Z̃tup:: JNT... )
998- V̂ = get_tmp (V̂_cache, Z̃tup[1 ])
1001+ Z̃1 = Z̃tup[begin ]
1002+ V̂ = get_tmp (V̂_cache, Z̃1)
9991003 Z̃ = collect (Z̃tup)
10001004 if Z̃tup != = last_Z̃tup_float
1001- g = get_tmp (g_cache, Z̃tup[ 1 ] )
1002- X̂ = get_tmp (X̂_cache, Z̃tup[ 1 ] )
1005+ g = get_tmp (g_cache, Z̃1 )
1006+ X̂ = get_tmp (X̂_cache, Z̃1 )
10031007 V̂, X̂ = predict! (V̂, X̂, estim, model, Z̃)
10041008 g = con_nonlinprog! (g, estim, model, X̂, V̂, Z̃)
10051009 last_Z̃tup_float = Z̃tup
10061010 end
1007- return obj_nonlinprog (estim, model, V̂, Z̃)
1011+ x̄ = get_tmp (x̄_cache, Z̃1)
1012+ return obj_nonlinprog! (x̄, estim, model, V̂, Z̃)
10081013 end
10091014 function Jfunc (Z̃tup:: ForwardDiff.Dual... )
1010- V̂ = get_tmp (V̂_cache, Z̃tup[1 ])
1015+ Z̃1 = Z̃tup[begin ]
1016+ V̂ = get_tmp (V̂_cache, Z̃1)
10111017 Z̃ = collect (Z̃tup)
10121018 if Z̃tup != = last_Z̃tup_dual
1013- g = get_tmp (g_cache, Z̃tup[ 1 ] )
1014- X̂ = get_tmp (X̂_cache, Z̃tup[ 1 ] )
1019+ g = get_tmp (g_cache, Z̃1 )
1020+ X̂ = get_tmp (X̂_cache, Z̃1 )
10151021 V̂, X̂ = predict! (V̂, X̂, estim, model, Z̃)
10161022 g = con_nonlinprog! (g, estim, model, X̂, V̂, Z̃)
10171023 last_Z̃tup_dual = Z̃tup
10181024 end
1019- return obj_nonlinprog (estim, model, V̂, Z̃)
1025+ x̄ = get_tmp (x̄_cache, Z̃1)
1026+ return obj_nonlinprog! (x̄, estim, model, V̂, Z̃)
10201027 end
10211028 function gfunc_i (i, Z̃tup:: NTuple{N, JNT} ) where N
1022- g = get_tmp (g_cache, Z̃tup[1 ])
1029+ Z̃1 = Z̃tup[begin ]
1030+ g = get_tmp (g_cache, Z̃1)
10231031 if Z̃tup != = last_Z̃tup_float
1024- V̂ = get_tmp (V̂_cache, Z̃tup[ 1 ] )
1025- X̂ = get_tmp (X̂_cache, Z̃tup[ 1 ] )
1032+ V̂ = get_tmp (V̂_cache, Z̃1 )
1033+ X̂ = get_tmp (X̂_cache, Z̃1 )
10261034 Z̃ = collect (Z̃tup)
10271035 V̂, X̂ = predict! (V̂, X̂, estim, model, Z̃)
10281036 g = con_nonlinprog! (g, estim, model, X̂, V̂, Z̃)
@@ -1031,10 +1039,11 @@ function init_optimization!(
10311039 return g[i]
10321040 end
10331041 function gfunc_i (i, Z̃tup:: NTuple{N, ForwardDiff.Dual} ) where N
1034- g = get_tmp (g_cache, Z̃tup[1 ])
1042+ Z̃1 = Z̃tup[begin ]
1043+ g = get_tmp (g_cache, Z̃1)
10351044 if Z̃tup != = last_Z̃tup_dual
1036- V̂ = get_tmp (V̂_cache, Z̃tup[ 1 ] )
1037- X̂ = get_tmp (X̂_cache, Z̃tup[ 1 ] )
1045+ V̂ = get_tmp (V̂_cache, Z̃1 )
1046+ X̂ = get_tmp (X̂_cache, Z̃1 )
10381047 Z̃ = collect (Z̃tup)
10391048 V̂, X̂ = predict! (V̂, X̂, estim, model, Z̃)
10401049 g = con_nonlinprog! (g, estim, model, X̂, V̂, Z̃)
0 commit comments