@@ -524,10 +524,10 @@ Inspired from: [User-defined operators with vector outputs](https://jump.dev/JuM
524524function get_optim_functions (mpc:: NonLinMPC , :: JuMP.GenericModel{JNT} ) where JNT<: Real
525525 model = mpc. estim. model
526526 nu, ny, nx̂, nϵ, Hp = model. nu, model. ny, mpc. estim. nx̂, mpc. nϵ, mpc. Hp
527- ng, nc, nΔŨ , nU, nŶ = length (mpc. con. i_g), mpc. con. nc, length (mpc. ΔŨ ), Hp* nu, Hp* ny
527+ ng, nc, nZ̃ , nU, nŶ = length (mpc. con. i_g), mpc. con. nc, length (mpc. Z̃ ), Hp* nu, Hp* ny
528528 nUe, nŶe = nU + nu, nŶ + ny
529- Ncache = nΔŨ + 3
530- ΔŨ_cache :: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nΔŨ), Ncache)
529+ Ncache = nZ̃ + 3
530+ Z̃_cache :: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nZ̃), Ncache)
531531 Ŷe_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nŶe), Ncache)
532532 Ue_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nUe), Ncache)
533533 Ȳ_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nŶ), Ncache)
@@ -538,91 +538,91 @@ function get_optim_functions(mpc::NonLinMPC, ::JuMP.GenericModel{JNT}) where JNT
538538 û0_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nu), Ncache)
539539 g_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, ng), Ncache)
540540 gc_cache:: DiffCache{Vector{JNT}, Vector{JNT}} = DiffCache (zeros (JNT, nc), Ncache)
541- function update_simulations! (ΔŨ, ΔŨtup :: NTuple{N, T} ) where {N, T<: Real }
542- if any (new != = old for (new, old) in zip (ΔŨtup, ΔŨ )) # new ΔŨtup , update predictions:
543- ΔŨ1 = ΔŨtup [begin ]
544- for i in eachindex (ΔŨtup )
545- ΔŨ [i] = ΔŨtup [i] # ΔŨ .= ΔŨtup seems to produce a type instability
541+ function update_simulations! (Z̃, Z̃tup :: NTuple{N, T} ) where {N, T<: Real }
542+ if any (new != = old for (new, old) in zip (Z̃tup, Z̃ )) # new Z̃tup , update predictions:
543+ Z̃1 = Z̃tup [begin ]
544+ for i in eachindex (Z̃tup )
545+ Z̃ [i] = Z̃tup [i] # Z̃ .= Z̃tup seems to produce a type instability
546546 end
547- Ŷe, Ue = get_tmp (Ŷe_cache, ΔŨ1 ), get_tmp (Ue_cache, ΔŨ1 )
548- Ȳ, Ū = get_tmp (Ȳ_cache, ΔŨ1 ), get_tmp (Ū_cache, ΔŨ1 )
549- x̂0, x̂0next = get_tmp (x̂0_cache, ΔŨ1 ), get_tmp (x̂0next_cache, ΔŨ1 )
550- u0, û0 = get_tmp (u0_cache, ΔŨ1 ), get_tmp (û0_cache, ΔŨ1 )
551- gc, g = get_tmp (gc_cache, ΔŨ1 ), get_tmp (g_cache, ΔŨ1 )
552- Ŷ0, x̂0end = predict! (Ȳ, x̂0, x̂0next, u0, û0, mpc, model, ΔŨ )
553- Ŷe, Ue = extended_predictions! (Ŷe, Ue, Ū, mpc, model, Ŷ0, ΔŨ )
554- ϵ = (nϵ ≠ 0 ) ? ΔŨ [end ] : zero (T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
547+ Ŷe, Ue = get_tmp (Ŷe_cache, Z̃1 ), get_tmp (Ue_cache, Z̃1 )
548+ Ȳ, Ū = get_tmp (Ȳ_cache, Z̃1 ), get_tmp (Ū_cache, Z̃1 )
549+ x̂0, x̂0next = get_tmp (x̂0_cache, Z̃1 ), get_tmp (x̂0next_cache, Z̃1 )
550+ u0, û0 = get_tmp (u0_cache, Z̃1 ), get_tmp (û0_cache, Z̃1 )
551+ gc, g = get_tmp (gc_cache, Z̃1 ), get_tmp (g_cache, Z̃1 )
552+ Ŷ0, x̂0end = predict! (Ȳ, x̂0, x̂0next, u0, û0, mpc, model, Z̃ )
553+ Ŷe, Ue = extended_predictions! (Ŷe, Ue, Ū, mpc, model, Ŷ0, Z̃ )
554+ ϵ = (nϵ ≠ 0 ) ? Z̃ [end ] : zero (T) # ϵ = 0 if nϵ == 0 (meaning no relaxation)
555555 gc = con_custom! (gc, mpc, Ue, Ŷe, ϵ)
556556 g = con_nonlinprog! (g, mpc, model, x̂0end, Ŷ0, gc, ϵ)
557557 end
558558 return nothing
559559 end
560- function Jfunc (ΔŨtup :: Vararg{T, N} ) where {N, T<: Real }
561- ΔŨ1 = ΔŨtup [begin ]
562- ΔŨ = get_tmp (ΔŨ_cache, ΔŨ1 )
563- update_simulations! (ΔŨ, ΔŨtup )
564- Ȳ, Ū = get_tmp (Ȳ_cache, ΔŨ1 ), get_tmp (Ū_cache, ΔŨ1 )
565- Ŷe, Ue = get_tmp (Ŷe_cache, ΔŨ1 ), get_tmp (Ue_cache, ΔŨ1 )
566- return obj_nonlinprog! (Ȳ, Ū, mpc, model, Ue, Ŷe, ΔŨ ):: T
560+ function Jfunc (Z̃tup :: Vararg{T, N} ) where {N, T<: Real }
561+ Z̃1 = Z̃tup [begin ]
562+ Z̃ = get_tmp (Z̃_cache, Z̃1 )
563+ update_simulations! (Z̃, Z̃tup )
564+ Ȳ, Ū = get_tmp (Ȳ_cache, Z̃1 ), get_tmp (Ū_cache, Z̃1 )
565+ Ŷe, Ue = get_tmp (Ŷe_cache, Z̃1 ), get_tmp (Ue_cache, Z̃1 )
566+ return obj_nonlinprog! (Ȳ, Ū, mpc, model, Ue, Ŷe, Z̃ ):: T
567567 end
568- function gfunc_i (i, ΔŨtup :: NTuple{N, T} ) where {N, T<: Real }
569- ΔŨ1 = ΔŨtup [begin ]
570- ΔŨ = get_tmp (ΔŨ_cache, ΔŨ1 )
571- update_simulations! (ΔŨ, ΔŨtup )
572- g = get_tmp (g_cache, ΔŨ1 )
568+ function gfunc_i (i, Z̃tup :: NTuple{N, T} ) where {N, T<: Real }
569+ Z̃1 = Z̃tup [begin ]
570+ Z̃ = get_tmp (Z̃_cache, Z̃1 )
571+ update_simulations! (Z̃, Z̃tup )
572+ g = get_tmp (g_cache, Z̃1 )
573573 return g[i]:: T
574574 end
575575 gfuncs = Vector {Function} (undef, ng)
576576 for i in 1 : ng
577577 # this is another syntax for anonymous function, allowing parameters T and N:
578- gfuncs[i] = function (ΔŨtup :: Vararg{T, N} ) where {N, T<: Real }
579- return gfunc_i (i, ΔŨtup )
578+ gfuncs[i] = function (Z̃tup :: Vararg{T, N} ) where {N, T<: Real }
579+ return gfunc_i (i, Z̃tup )
580580 end
581581 end
582582 return Jfunc, gfuncs
583583end
584584
585585function init_nonlincon! (mpc:: NonLinMPC , :: LinModel , gfuncs:: Vector{<:Function} )
586586 optim, con = mpc. optim, mpc. con
587- nΔŨ = length (mpc. ΔŨ )
587+ nZ̃ = length (mpc. Z̃ )
588588 if length (con. i_g) ≠ 0
589589 i_base = 0
590590 for i in 1 : con. nc
591591 name = Symbol (" g_c_$i " )
592- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
592+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
593593 end
594594 end
595595 return nothing
596596end
597597
598598function init_nonlincon! (mpc:: NonLinMPC , :: NonLinModel , gfuncs:: Vector{<:Function} )
599599 optim, con = mpc. optim, mpc. con
600- ny, nx̂, Hp, nΔŨ = mpc. estim. model. ny, mpc. estim. nx̂, mpc. Hp, length (mpc. ΔŨ )
600+ ny, nx̂, Hp, nZ̃ = mpc. estim. model. ny, mpc. estim. nx̂, mpc. Hp, length (mpc. Z̃ )
601601 if length (con. i_g) ≠ 0
602602 i_base = 0
603603 for i in eachindex (con. Y0min)
604604 name = Symbol (" g_Y0min_$i " )
605- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
605+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
606606 end
607607 i_base = 1 Hp* ny
608608 for i in eachindex (con. Y0max)
609609 name = Symbol (" g_Y0max_$i " )
610- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
610+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
611611 end
612612 i_base = 2 Hp* ny
613613 for i in eachindex (con. x̂0min)
614614 name = Symbol (" g_x̂0min_$i " )
615- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
615+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
616616 end
617617 i_base = 2 Hp* ny + nx̂
618618 for i in eachindex (con. x̂0max)
619619 name = Symbol (" g_x̂0max_$i " )
620- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
620+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
621621 end
622622 i_base = 2 Hp* ny + 2 nx̂
623623 for i in 1 : con. nc
624624 name = Symbol (" g_c_$i " )
625- optim[name] = JuMP. add_nonlinear_operator (optim, nΔŨ , gfuncs[i_base+ i]; name)
625+ optim[name] = JuMP. add_nonlinear_operator (optim, nZ̃ , gfuncs[i_base+ i]; name)
626626 end
627627 end
628628 return nothing
@@ -636,13 +636,13 @@ Set the custom nonlinear inequality constraints for `LinModel`.
636636function set_nonlincon! (
637637 mpc:: NonLinMPC , :: LinModel , optim:: JuMP.GenericModel{JNT}
638638) where JNT<: Real
639- ΔŨvar = optim[:ΔŨvar ]
639+ Z̃var = optim[:Z̃var ]
640640 con = mpc. con
641641 nonlin_constraints = JuMP. all_constraints (optim, JuMP. NonlinearExpr, MOI. LessThan{JNT})
642642 map (con_ref -> JuMP. delete (optim, con_ref), nonlin_constraints)
643643 for i in 1 : con. nc
644644 gfunc_i = optim[Symbol (" g_c_$i " )]
645- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
645+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
646646 end
647647 return nothing
648648end
@@ -655,29 +655,29 @@ Also set output prediction `Ŷ` and terminal state `x̂end` constraints when no
655655function set_nonlincon! (
656656 mpc:: NonLinMPC , :: SimModel , optim:: JuMP.GenericModel{JNT}
657657) where JNT<: Real
658- ΔŨvar = optim[:ΔŨvar ]
658+ Z̃var = optim[:Z̃var ]
659659 con = mpc. con
660660 nonlin_constraints = JuMP. all_constraints (optim, JuMP. NonlinearExpr, MOI. LessThan{JNT})
661661 map (con_ref -> JuMP. delete (optim, con_ref), nonlin_constraints)
662662 for i in findall (.! isinf .(con. Y0min))
663663 gfunc_i = optim[Symbol (" g_Y0min_$(i) " )]
664- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
664+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
665665 end
666666 for i in findall (.! isinf .(con. Y0max))
667667 gfunc_i = optim[Symbol (" g_Y0max_$(i) " )]
668- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
668+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
669669 end
670670 for i in findall (.! isinf .(con. x̂0min))
671671 gfunc_i = optim[Symbol (" g_x̂0min_$(i) " )]
672- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
672+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
673673 end
674674 for i in findall (.! isinf .(con. x̂0max))
675675 gfunc_i = optim[Symbol (" g_x̂0max_$(i) " )]
676- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
676+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
677677 end
678678 for i in 1 : con. nc
679679 gfunc_i = optim[Symbol (" g_c_$i " )]
680- @constraint (optim, gfunc_i (ΔŨvar ... ) <= 0 )
680+ @constraint (optim, gfunc_i (Z̃var ... ) <= 0 )
681681 end
682682 return nothing
683683end
0 commit comments