@@ -536,7 +536,7 @@ function setconstraint!(
536536 JuMP. delete (optim, optim[:linconstraint ])
537537 JuMP. unregister (optim, :linconstraint )
538538 @constraint (optim, linconstraint, A* Z̃var .≤ b)
539- setnonlincon! (estim, model)
539+ setnonlincon! (estim, model, optim )
540540 else
541541 i_b, i_g = init_matconstraint_mhe (model,
542542 i_x̃min, i_x̃max, i_X̂min, i_X̂max, i_Ŵmin, i_Ŵmax, i_V̂min, i_V̂max
@@ -598,28 +598,31 @@ function init_matconstraint_mhe(::SimModel{NT},
598598end
599599
600600" By default, no nonlinear constraints in the MHE, thus return nothing."
601- setnonlincon! (:: MovingHorizonEstimator , :: SimModel ) = nothing
601+ setnonlincon! (:: MovingHorizonEstimator , :: SimModel , :: JuMP.GenericModel ) = nothing
602602
603603" Set the nonlinear constraints on the output predictions `Ŷ` and terminal states `x̂end`."
604- function setnonlincon! (estim:: MovingHorizonEstimator , :: NonLinModel )
604+ function setnonlincon! (
605+ estim:: MovingHorizonEstimator , :: NonLinModel , optim:: JuMP.GenericModel{JNT}
606+ ) where JNT<: Real
605607 optim, con = estim. optim, estim. con
606608 Z̃var = optim[:Z̃var ]
607- map (con -> JuMP. delete (optim, con), JuMP. all_nonlinear_constraints (optim))
609+ nonlin_constraints = JuMP. all_constraints (optim, JuMP. NonlinearExpr, MOI. LessThan{JNT})
610+ map (con_ref -> JuMP. delete (optim, con_ref), nonlin_constraints)
608611 for i in findall (.! isinf .(con. X̂0min))
609- f_sym = Symbol (" g_X̂0min_$(i) " )
610- JuMP . add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
612+ gfunc_i = optim[ Symbol (" g_X̂0min_$(i) " )]
613+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
611614 end
612615 for i in findall (.! isinf .(con. X̂0max))
613- f_sym = Symbol (" g_X̂0max_$(i) " )
614- JuMP . add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
616+ gfunc_i = optim[ Symbol (" g_X̂0max_$(i) " )]
617+ @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
615618 end
616619 for i in findall (.! isinf .(con. V̂min))
617- f_sym = Symbol (" g_V̂min_$(i) " )
618- JuMP. add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
620+ gfunc_i = optim[ Symbol (" g_V̂min_$(i) " )]
621+ JuMP. @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
619622 end
620623 for i in findall (.! isinf .(con. V̂max))
621- f_sym = Symbol (" g_V̂max_$(i) " )
622- JuMP. add_nonlinear_constraint (optim, :( $ (f_sym)( $ ( Z̃var... )) <= 0 ) )
624+ gfunc_i = optim[ Symbol (" g_V̂max_$(i) " )]
625+ JuMP. @constraint (optim, gfunc_i ( Z̃var... ) <= 0 )
623626 end
624627 return nothing
625628end
@@ -1073,28 +1076,36 @@ function init_optimization!(
10731076 end
10741077 end
10751078 Jfunc, gfunc = get_optim_functions (estim, optim)
1076- register (optim, :Jfunc , nZ̃, Jfunc, autodiff = true )
1077- @NLobjective (optim, Min, Jfunc (Z̃var... ))
1079+ @operator (optim, J , nZ̃, Jfunc)
1080+ @objective (optim, Min, J (Z̃var... ))
10781081 nV̂, nX̂ = estim. He* estim. nym, estim. He* estim. nx̂
10791082 if length (con. i_g) ≠ 0
10801083 for i in eachindex (con. X̂0min)
1081- sym = Symbol (" g_X̂0min_$i " )
1082- register (optim, sym, nZ̃, gfunc[i], autodiff= true )
1084+ name = Symbol (" g_X̂0min_$i " )
1085+ optim[name] = JuMP. add_nonlinear_operator (
1086+ optim, nZ̃, gfunc[i]; name
1087+ )
10831088 end
10841089 i_end_X̂min = nX̂
10851090 for i in eachindex (con. X̂0max)
1086- sym = Symbol (" g_X̂0max_$i " )
1087- register (optim, sym, nZ̃, gfunc[i_end_X̂min+ i], autodiff= true )
1091+ name = Symbol (" g_X̂0max_$i " )
1092+ optim[name] = JuMP. add_nonlinear_operator (
1093+ optim, nZ̃, gfunc[i_end_X̂min + i]; name
1094+ )
10881095 end
10891096 i_end_X̂max = 2 * nX̂
10901097 for i in eachindex (con. V̂min)
1091- sym = Symbol (" g_V̂min_$i " )
1092- register (optim, sym, nZ̃, gfunc[i_end_X̂max+ i], autodiff= true )
1098+ name = Symbol (" g_V̂min_$i " )
1099+ optim[name] = JuMP. add_nonlinear_operator (
1100+ optim, nZ̃, gfunc[i_end_X̂max + i]; name
1101+ )
10931102 end
10941103 i_end_V̂min = 2 * nX̂ + nV̂
10951104 for i in eachindex (con. V̂max)
1096- sym = Symbol (" g_V̂max_$i " )
1097- register (optim, sym, nZ̃, gfunc[i_end_V̂min+ i], autodiff= true )
1105+ name = Symbol (" g_V̂max_$i " )
1106+ optim[name] = JuMP. add_nonlinear_operator (
1107+ optim, nZ̃, gfunc[i_end_V̂min + i]; name
1108+ )
10981109 end
10991110 end
11001111 return nothing
0 commit comments