@@ -555,6 +555,9 @@ function init_optimization!(
555555 gc:: Vector{JNT} , g:: Vector{JNT} = zeros (JNT, nc), zeros (JNT, ng)
556556 geq:: Vector{JNT} = zeros (JNT, neq)
557557
558+
559+
560+
558561 function gfunc! (g, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq)
559562 update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
560563 return nothing
@@ -572,17 +575,23 @@ function init_optimization!(
572575 ∇g = init_diffmat (JNT, jac, ∇g_prep, nZ̃, ng)
573576
574577
575- function gfunc_set! (g, Z̃)
576- return gfunc! (g, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, geq)
578+ function update_con! (g, ∇g, Z̃_∇g, Z̃_arg)
579+ if isdifferent (Z̃_arg, Z̃_∇g)
580+ Z̃_∇g .= Z̃_arg
581+ value_and_jacobian! (gfunc!, g, ∇g, ∇g_prep, jac, Z̃_∇g, ∇g_context... )
582+ end
577583 end
578- function ∇gfunc_set! (∇g_vec, Z̃)
579- value_and_jacobian! (gfunc!, g, ∇g, ∇g_prep, jac, Z̃, ∇g_context... )
580- ∇g_vec .= nonzeros (∇g)
581- return nothing
584+ function gfunc_set! (g_arg, Z̃_arg)
585+ update_con! (g, ∇g, Z̃_∇g, Z̃_arg)
586+ return g_arg .= g
587+ end
588+ function ∇gfunc_set! (∇g_arg, Z̃_arg)
589+ update_con! (g, ∇g, Z̃_∇g, Z̃_arg)
590+ return ∇g_arg .= nonzeros (∇g)
582591 end
583592
584593 g_min = fill (- myInf, ng)
585- g_max = zeros (JNT , ng)
594+ g_max = fill ( + myInf , ng)
586595
587596 I_∇g, J_∇g = SparseArrays. findnz (∇g)
588597 ∇g_structure = collect (zip (I_∇g, J_∇g))
@@ -597,6 +606,10 @@ function init_optimization!(
597606 )
598607 @constraint (optim, Z̃var in g_set)
599608
609+
610+
611+
612+
600613 function geqfunc! (geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g)
601614 update_predictions! (ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g, geq, mpc, Z̃)
602615 return nothing
@@ -610,15 +623,21 @@ function init_optimization!(
610623 ∇geq_prep = prepare_jacobian (geqfunc!, geq, jac, Z̃_∇geq, ∇geq_context... ; strict)
611624 ∇geq = init_diffmat (JNT, jac, ∇geq_prep, nZ̃, neq)
612625
613- function geqfunc_set! (geq, Z̃)
614- return geqfunc! (geq, Z̃, ΔŨ, x̂0end, Ue, Ŷe, U0, Ŷ0, Û0, K0, X̂0, gc, g)
626+
627+ function update_con_eq! (geq, ∇geq, Z̃_∇geq, Z̃_arg)
628+ if isdifferent (Z̃_arg, Z̃_∇geq)
629+ Z̃_∇geq .= Z̃_arg
630+ value_and_jacobian! (geqfunc!, geq, ∇geq, ∇geq_prep, jac, Z̃_∇geq, ∇geq_context... )
631+ end
615632 end
616- function ∇geqfunc_set! (∇geq_vec, Z̃)
617- value_and_jacobian! (geqfunc!, geq, ∇geq, ∇geq_prep, jac, Z̃, ∇geq_context... )
618- ∇geq_vec .= nonzeros (∇geq)
619- return nothing
633+ function geqfunc_set! (geq_arg, Z̃_arg)
634+ update_con_eq! (geq, ∇geq, Z̃_∇geq, Z̃_arg)
635+ return geq_arg .= geq
636+ end
637+ function ∇geqfunc_set! (∇geq_arg, Z̃_arg)
638+ update_con_eq! (geq, ∇geq, Z̃_∇geq, Z̃_arg)
639+ return ∇geq_arg .= nonzeros (∇geq)
620640 end
621-
622641
623642 geq_min = zeros (JNT, mpc. con. neq)
624643 geq_max = zeros (JNT, mpc. con. neq)
0 commit comments