# How to get/set constraint names?

**URL:** https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411
**Category:** Optimization (Mathematical)
**Created:** [September 10, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411 "2020-09-10T19:22:46Z")
**Posts on this page:** 5
**Page:** 1

<div class="post-metadata">

### Author: ![vattyamv](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vattyamv](https://discourse.julialang.org/u/vattyamv)
#### Post date: [September 10, 2020, 7:22pm UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411/1 "2020-09-10T19:22:46Z")

</div>

Hi, I am trying to set and get constraint names but am running into some errors. I originally built the model without naming the constraints but now I want to name them. Take for example,

```julia
for i in 1:timerange
    @constraint(m, cost[i,1] == s[i,1] * price[i])
end

```

Then I tried to name it with

```julia
for i in 1:timerange
    @constraint(m, con[i], cost[i,1] == s[i,1] * price[i])
end

```

but I got

LoadError: An object of name con is already attached to this model. If this is intended, consider using the anonymous construction syntax, e.g., x = @variable(model, [1:N], …) where the name of the object does not appear inside the macro.

This error happens no matter what I name the variable. However when I do constraint\_by\_name(m,“con[1]”) it actually shows the constraint correctly… Is there a way to conveniently name all the constraints without causing the error?

---

<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: [September 10, 2020, 8:29pm UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411/2 "2020-09-10T20:29:11Z")

</div>

See [https://jump.dev/JuMP.jl/stable/constraints/#Constraint-names-1](https://jump.dev/JuMP.jl/stable/constraints/#Constraint-names-1)

```nohighlight
for i in 1:timerange
    c = @constraint(m, cost[i,1] == s[i,1] * price[i])
    set_name(c, "my_constraint_$(i)")
end

# or 

@constraint(m, con[i=1:timerange], cost[i,1] == s[i,1] * price[i])
# effectively equivalent to
con = [
    @constriant(m, cost[i,1] == s[i,1] * price[i])
    for i in 1:timerange
]
for i in 1:timerange
    set_name(con[i], "con[$i]")
end

```

---

<div class="post-metadata">

### Author: ![vattyamv](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vattyamv](https://discourse.julialang.org/u/vattyamv)
#### Post date: [September 10, 2020, 8:44pm UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411/3 "2020-09-10T20:44:29Z")

</div>

Thanks that worked. Is there a way to get all of the constraints in the entire model at once and put them into an array and/or name them?

---

<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: [September 11, 2020, 2:14am UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411/4 "2020-09-11T02:14:43Z")

</div>

See [https://jump.dev/JuMP.jl/stable/constraints/#Accessing-constraints-from-a-model-1](https://jump.dev/JuMP.jl/stable/constraints/#Accessing-constraints-from-a-model-1)

---

<div class="post-metadata">

### Author: ![vattyamv](https://avatars.discourse-cdn.com/v4/letter/v/3bc359/32.png) [@vattyamv](https://discourse.julialang.org/u/vattyamv)
#### Post date: [September 11, 2020, 4:32pm UTC](https://discourse.julialang.org/t/how-to-get-set-constraint-names/46411/5 "2020-09-11T16:32:16Z")

</div>

I did see that before. I guess I could loop through each constraint type then. Another question if you don’t mind: when I get an optimal solution I get as part of the output “User MIP start violates constraint R0 by 10000.000000000” but when I try constraint\_by\_name(m, “R0”) it does nothing. Is there any way to find which constraint R0 is?
