# @NLconstraint and String Name

**URL:** <https://discourse.julialang.org/t/nlconstraint-and-string-name/98550>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [May 9, 2023, 12:58pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550 "2023-05-09T12:58:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [May 9, 2023, 12:58pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550/1 "2023-05-09T12:58:12Z")

</div>

Can the string be included to represent the constraint name in non-linear constraints using @NLconstraint, akin to @constraint’s base\_name functionality?

---

<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:** [May 9, 2023, 6:49pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550/2 "2023-05-09T18:49:22Z")

</div>

Unfortunately, no. This is a known limitation.

---

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [May 9, 2023, 7:23pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550/3 "2023-05-09T19:23:39Z")

</div>

So, it appears that using the `base_name` keyword or a similar option is not possible, and the `set_name` function is not a viable solution.

Does this imply that if I need to create a non-linear constraint within the for loop, I am unable to specify distinct names? For example, if I require something like:

```julia
model = Model()
@variable(model, x[i=1:5])
for i = 1:3
    @NLconstraint(model, con[i], x[i]^3 <= 1)
end

```

I understand that the solution could be as follows:

```julia
model = Model()
@variable(model, x[i=1:5])
@NLconstraint(model, con[i = 1:3], x[i]^3 <= 1)

```

However, I require the ability to invoke the @NLconstraint statement within the for loop, as demonstrated in the initial example.

---

<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:** [May 9, 2023, 8:36pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550/4 "2023-05-09T20:36:12Z")

</div>

> So, it appears that using the `base_name` keyword or a similar option is not possible, and the `set_name` function is not a viable solution.

Correct. Nonlinear constraints are not like normal `@constraints`. They do not have names.

It’s always worth remembering that you can add anonymous constraints, and that you can create your own data structures to store a constraint:

Option 1

```Julia
model = Model()
@variable(model, x[i=1:5])
for i = 1:3
    @NLconstraint(model, x[i]^3 <= 1)
end

```

Option 2

```Julia
model = Model()
@variable(model, x[i=1:5])
con = Any[]
for i = 1:3
    c = @NLconstraint(model, x[i]^3 <= 1)
    push!(con, c)
end
con

```

---

<div class="post-metadata">

**Author:** ![Mirsad\_Cosovic](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mirsad_cosovic/32/9887_2.png) [@Mirsad\_Cosovic](https://discourse.julialang.org/u/Mirsad_Cosovic)\
**Post date:** [May 9, 2023, 8:59pm UTC](https://discourse.julialang.org/t/nlconstraint-and-string-name/98550/5 "2023-05-09T20:59:38Z")

</div>

In actuality, option 2 appears to be a satisfactory solution as I can remove specific constraints, such as:

```julia
delete(model, con[2])

```

Although this solution results in a slightly higher number of allocations, it appears to be acceptable. Thank you, this should be sufficient.
