@@ -536,7 +536,7 @@ function setconstraint!(
536
536
JuMP. delete (optim, optim[:linconstraint ])
537
537
JuMP. unregister (optim, :linconstraint )
538
538
@constraint (optim, linconstraint, A* Z̃var .≤ b)
539
- setnonlincon! (estim, model)
539
+ setnonlincon! (estim, model, optim )
540
540
else
541
541
i_b, i_g = init_matconstraint_mhe (model,
542
542
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},
598
598
end
599
599
600
600
" By default, no nonlinear constraints in the MHE, thus return nothing."
601
- setnonlincon! (:: MovingHorizonEstimator , :: SimModel ) = nothing
601
+ setnonlincon! (:: MovingHorizonEstimator , :: SimModel , :: JuMP.GenericModel ) = nothing
602
602
603
603
" 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
605
607
optim, con = estim. optim, estim. con
606
608
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)
608
611
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 )
611
614
end
612
615
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 )
615
618
end
616
619
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 )
619
622
end
620
623
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 )
623
626
end
624
627
return nothing
625
628
end
@@ -1073,28 +1076,36 @@ function init_optimization!(
1073
1076
end
1074
1077
end
1075
1078
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... ))
1078
1081
nV̂, nX̂ = estim. He* estim. nym, estim. He* estim. nx̂
1079
1082
if length (con. i_g) ≠ 0
1080
1083
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
+ )
1083
1088
end
1084
1089
i_end_X̂min = nX̂
1085
1090
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
+ )
1088
1095
end
1089
1096
i_end_X̂max = 2 * nX̂
1090
1097
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
+ )
1093
1102
end
1094
1103
i_end_V̂min = 2 * nX̂ + nV̂
1095
1104
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
+ )
1098
1109
end
1099
1110
end
1100
1111
return nothing
0 commit comments