# How to reference JuMP constraint from solver index?

**URL:** <https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865>\
**Category:** Optimization (Mathematical)\
**Tags:** question, jump\
**Created:** [March 24, 2021, 12:19pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865 "2021-03-24T12:19:20Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![claytonpbarrows](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/claytonpbarrows/32/14233_2.png) [@claytonpbarrows](https://discourse.julialang.org/u/claytonpbarrows)\
**Post date:** [March 24, 2021, 12:19pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/1 "2021-03-24T12:19:20Z")

</div>

Some solvers will indicate the constraints causing infeasibilities (e.g. Gurobi, Xpress, …) like this:

```julia
User MIP start violates constraint R33048 by 0.493000000

```

Is there a way to retrieve the JuMP constraint ref corresponding to “R33048”?  
I know that I can use `write_to_file` and inspect the JSON, but in this case that file is quite large and I’m looking for a more direct way to debug.

---

<div class="post-metadata">

**Author:** ![Henrique\_Becker](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/henrique_becker/32/15443_2.png) [@Henrique\_Becker](https://discourse.julialang.org/u/Henrique_Becker)\
**Post date:** [March 24, 2021, 9:04pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/2 "2021-03-24T21:04:36Z")

</div>

You can build a dictionary using [`JuMP.optimizer_index`](https://jump.dev/JuMP.jl/dev/reference/constraints/#JuMP.optimizer_index-Tuple%7BConstraintRef%7BModel,C,Shape%7D%20where%20Shape%3C:AbstractShape%20where%20C%7D) to get the optimizer index of all your constraints and then search it for specific constraints.

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 25, 2022, 3:42pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/3 "2022-01-25T15:42:08Z")

</div>

Thanks, but doesn’t seem very straightforward…

I tried this, but am not having much luck.

I get the following output from the CPLEX log:  
`Infeasibility row 'c47180': 0 = -1.`

So, I did the following to find it (NOTE: since CPLEX is 0-indexed, I increase the index value by 1 from 47180 to 47181):

```julia
julia> cons=vcat([all_constraints(model, i...) for i in list_of_constraint_types(model)]...);

julia> con_dict=Dict(con => optimizer_index(con).value for con in cons);

julia> findall(i -> i==47181, con_dict)
1-element Vector{ConstraintRef{Model, MathOptInterface.ConstraintIndex{MathOptInterface.SingleVariable, MathOptInterface.EqualTo{Float64}}, ScalarShape}}:
 OS[-19,5,20] = 0.0

```

I noticed this is a variable fixing constraint in my model (using `fix`), so I removed it from the model. I reran the problem and get the same CPLEX output  
`Infeasibility row 'c47180': 0 = -1.`

Any thoughts??

---

<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:** [January 25, 2022, 7:55pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/4 "2022-01-25T19:55:34Z")

</div>

There is no good way to do this, because the `.value` of a variable or constraint may not correspond to its row in the matrix.

You could call `set_name` on your constraints to pass a meaningful name to the inner CPLEX optimizer?

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 25, 2022, 7:58pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/5 "2022-01-25T19:58:55Z")

</div>

I see. All of my constraints are named (`name(ConstraintRef)` returns a valid string name), but these don’t seem to be passed to CPLEX. Is there something I need to do to pass these over?

---

<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:** [January 25, 2022, 9:49pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/6 "2022-01-25T21:49:51Z")

</div>

Use `direct_model(CPLEX.Optimizer())`. Upcoming releases of JuMP and MOI will push more information to the solver. At present they sometimes aren’t if bridges and caches are used.

---

<div class="post-metadata">

**Author:** ![hdavid16](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/hdavid16/32/11531_2.png) [@hdavid16](https://discourse.julialang.org/u/hdavid16)\
**Post date:** [January 25, 2022, 10:29pm UTC](https://discourse.julialang.org/t/how-to-reference-jump-constraint-from-solver-index/57865/7 "2022-01-25T22:29:35Z")

</div>

Got it. Thanks @odow !
