# JuMP new nonlinear interface: adding constraint programatically

**URL:** https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [February 10, 2024, 7:12pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041 "2024-02-10T19:12:43Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![franckgaga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/franckgaga/32/218241_2.png) [@franckgaga](https://discourse.julialang.org/u/franckgaga)
#### Post date: [February 10, 2024, 7:12pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041/1 "2024-02-10T19:12:43Z")

</div>

[`ModelPredictiveControl.jl`](https://github.com/JuliaControl/ModelPredictiveControl.jl) is still using the legacy syntax for NLP, but I’m starting the migration right now. The nonlinear inequality constraints are currently added programmatically with (simplified for the purpose of the explanation):

```julia
using JuMP, Ipopt
model = Model(Ipopt.Optimizer)
nx = 2
@variable(model, x[1:nx])
gfunc_i(i, x) = 10x[i]^2 - 10
gfunc = [(x...) -> gfunc_i(i, x) for i in 1:nx]
ymin, ymax = [-1, -1], [1, 1]
for i in eachindex(ymin)
    sym = Symbol("g_ymin_$i")
    register(model, sym, nx, gfunc[i], autodiff=true)
    add_nonlinear_constraint(model, :(-$(sym)($(x...)) <= -$(ymin[i])))
end
for i in eachindex(ymax)
    sym = Symbol("g_ymax_$i")
    register(model, sym, nx, gfunc[i], autodiff=true)
    add_nonlinear_constraint(model, :(+$(sym)($(x...)) <= +$(ymax[i])))
end

```

is there an equivalent for the `add_nonlinear_constraint` method in the new NLP syntax? I’ve tried `add_constraint` but I’m not sure how to use it since the docstring is not very detailed:

```julia-repl
help?> add_constraint
search: add_constraint add_nonlinear_constraint

  add_constraint(model::GenericModel, con::AbstractConstraint, name::String="")

  Add a constraint con to Model model and sets its name.

```

Thanks for the help 🙂

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [February 10, 2024, 8:02pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041/2 "2024-02-10T20:02:32Z")

</div>

```julia
using JuMP
gfunc_i(i, x) = 10x[i]^2 - 10
ymin, ymax = [-1, -1], [1, 1]
nx = length(ymin)
model = Model()
@variable(model, x[1:nx])
# Option 1: function tracing
@constraint(model, [i in 1:nx], ymin[i] <= gfunc_i(i, x) <= ymax[i])

# Option 2: operator
for i in 1:nx
    g = (x...) -> g_func_i(i, x)
    op = add_nonlinear_operator(model, nx, g; name = Symbol("gfunc_$i"))
    @constraint(model, ymin[i] <= op(x...) <= ymax[i])
end

```

For option 1, see: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/dev/manual/nonlinear/#Function-tracing)

For options 2, see: [Nonlinear Modeling · JuMP](https://jump.dev/JuMP.jl/dev/manual/nonlinear/#Add-an-operator-without-macros)

---

<div class="post-metadata">

### Author: ![franckgaga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/franckgaga/32/218241_2.png) [@franckgaga](https://discourse.julialang.org/u/franckgaga)
#### Post date: [February 10, 2024, 8:35pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041/3 "2024-02-10T20:35:21Z")

</div>

Wow, it is way cleaner like that haha. The operator option works well, thanks!

---

<div class="post-metadata">

### Author: ![odow](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/odow/32/28685_2.png) [@odow](https://discourse.julialang.org/u/odow)
#### Post date: [February 10, 2024, 8:39pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041/4 "2024-02-10T20:39:48Z")

</div>

> Wow, it is way cleaner like that haha

Yip! I put a bit of thought into making sure things like this were now well supported 😄

---

<div class="post-metadata">

### Author: ![franckgaga](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/franckgaga/32/218241_2.png) [@franckgaga](https://discourse.julialang.org/u/franckgaga)
#### Post date: [February 10, 2024, 8:41pm UTC](https://discourse.julialang.org/t/jump-new-nonlinear-interface-adding-constraint-programatically/110041/5 "2024-02-10T20:41:15Z")

</div>

It shows, I really like the new syntax.
