@@ -495,7 +495,7 @@ function setconstraint!(
495495 delete (optim, optim[:linconstraint ])
496496 unregister (optim, :linconstraint )
497497 @constraint (optim, linconstraint, A* Z̃var .≤ b)
498- setnonlincon! (estim, model)
498+ setnonlincon! (estim, model, optim )
499499 else
500500 i_b, i_g = init_matconstraint_mhe (model,
501501 i_x̃min, i_x̃max, i_X̂min, i_X̂max, i_Ŵmin, i_Ŵmax, i_V̂min, i_V̂max
@@ -557,28 +557,31 @@ function init_matconstraint_mhe(::SimModel{NT},
557557end
558558
559559" By default, no nonlinear constraints in the MHE, thus return nothing."
560- setnonlincon! (:: MovingHorizonEstimator , :: SimModel ) = nothing
560+ setnonlincon! (:: MovingHorizonEstimator , :: SimModel , :: JuMP.GenericModel ) = nothing
561561
562562" Set the nonlinear constraints on the output predictions `Ŷ` and terminal states `x̂end`."
563- function setnonlincon! (estim:: MovingHorizonEstimator , :: NonLinModel )
563+ function setnonlincon! (
564+ estim:: MovingHorizonEstimator , :: NonLinModel , optim:: JuMP.GenericModel{JNT}
565+ ) where JNT<: Real
564566 optim, con = estim. optim, estim. con
565567 Z̃var = optim[:Z̃var ]
566- map (con -> delete (optim, con), all_nonlinear_constraints (optim))
568+ nonlin_constraints = all_constraints (optim, NonlinearExpr, MOI. LessThan{JNT})
569+ map (con_ref -> delete (optim, con_ref), nonlin_constraints)
567570 for i in findall (.! isinf .(con. X̂min))
568- f_sym = Symbol (" g_X̂min_$(i) " )
569- add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
571+ gfunc_i = optim[ Symbol (" g_X̂min_$(i) " )]
572+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
570573 end
571574 for i in findall (.! isinf .(con. X̂max))
572- f_sym = Symbol (" g_X̂max_$(i) " )
573- add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
575+ gfunc_i = optim[ Symbol (" g_X̂max_$(i) " )]
576+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
574577 end
575578 for i in findall (.! isinf .(con. V̂min))
576- f_sym = Symbol (" g_V̂min_$(i) " )
577- add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
579+ gfunc_i = optim[ Symbol (" g_V̂min_$(i) " )]
580+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
578581 end
579582 for i in findall (.! isinf .(con. V̂max))
580- f_sym = Symbol (" g_V̂max_$(i) " )
581- add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
583+ gfunc_i = optim[ Symbol (" g_V̂max_$(i) " )]
584+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
582585 end
583586 return nothing
584587end
@@ -1054,27 +1057,27 @@ function init_optimization!(
10541057 gfunc = [(Z̃... ) -> gfunc_i (i, Z̃) for i in 1 : ng]
10551058 Jfunc, gfunc
10561059 end
1057- register (optim, :Jfunc , nZ̃, Jfunc, autodiff = true )
1058- @NLobjective (optim, Min, Jfunc (Z̃var... ))
1060+ @operator (optim, J , nZ̃, Jfunc)
1061+ @objective (optim, Min, J (Z̃var... ))
10591062 if ng ≠ 0
10601063 for i in eachindex (con. X̂min)
1061- sym = Symbol (" g_X̂min_$i " )
1062- register (optim, sym, nZ̃, gfunc[i], autodiff = true )
1064+ name = Symbol (" g_X̂min_$i " )
1065+ optim[name] = add_nonlinear_operator (optim, nZ̃, gfunc[i]; name )
10631066 end
10641067 i_end_X̂min = nX̂
10651068 for i in eachindex (con. X̂max)
1066- sym = Symbol (" g_X̂max_$i " )
1067- register (optim, sym, nZ̃, gfunc[i_end_X̂min+ i], autodiff = true )
1069+ name = Symbol (" g_X̂max_$i " )
1070+ optim[name] = add_nonlinear_operator (optim, nZ̃, gfunc[i_end_X̂min + i]; name )
10681071 end
10691072 i_end_X̂max = 2 * nX̂
10701073 for i in eachindex (con. V̂min)
1071- sym = Symbol (" g_V̂min_$i " )
1072- register (optim, sym, nZ̃, gfunc[i_end_X̂max+ i], autodiff = true )
1074+ name = Symbol (" g_V̂min_$i " )
1075+ optim[name] = add_nonlinear_operator (optim, nZ̃, gfunc[i_end_X̂max + i]; name )
10731076 end
10741077 i_end_V̂min = 2 * nX̂ + nV̂
10751078 for i in eachindex (con. V̂max)
1076- sym = Symbol (" g_V̂max_$i " )
1077- register (optim, sym, nZ̃, gfunc[i_end_V̂min+ i], autodiff = true )
1079+ name = Symbol (" g_V̂max_$i " )
1080+ optim[name] = add_nonlinear_operator (optim, nZ̃, gfunc[i_end_V̂min + i]; name )
10781081 end
10791082 end
10801083 return nothing
0 commit comments