# How to get ordering of constraints when calling eval\_constraint?

**URL:** https://discourse.julialang.org/t/how-to-get-ordering-of-constraints-when-calling-eval-constraint/93772
**Category:** Optimization (Mathematical)
**Tags:** jump
**Created:** [January 30, 2023, 1:25pm UTC](https://discourse.julialang.org/t/how-to-get-ordering-of-constraints-when-calling-eval-constraint/93772 "2023-01-30T13:25:48Z")
**Posts on this page:** 3
**Page:** 1

<div class="post-metadata">

### Author: ![hugh\_man](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hugh\_man](https://discourse.julialang.org/u/hugh_man)
#### Post date: [January 30, 2023, 1:25pm UTC](https://discourse.julialang.org/t/how-to-get-ordering-of-constraints-when-calling-eval-constraint/93772/1 "2023-01-30T13:25:48Z")

</div>

Hello,

I have a large JuMP model constructed programmatically as a function of some input data. My ultimate goal is to plug in arbitrary values into the variables and evaluate the values of the constraints. To accompish this, I’ve followed the initial approach from this thread [here](https://discourse.julialang.org/t/evaluating-constraints-at-fixed-points-and-extracting-their-variables-in-jump/47227).

But here is my code:

```julia
using JuMP
using MathOptInterface
const MOI = MathOptInterface

model = complicated_model_builder()
variables = all_variables(model)

d = JuMP.NLPEvaluator(model)
MOI.initialize(d, [:Jac])
# (total_num_constraints = 61 in my case)
total_num_constraints = sum(num_constraints(model, F, S) for (F, S) in list_of_constraint_types(model))
constraint_values = zeros(total_num_constraints)
inp = ones(size(variables))*19 # Something arbitrary but noticeable
MOI.eval_constraint(d, constraint_values, inp)

```

This works as intended: `constraint_values` is populated with reasonable-looking values (e.g. some of them are 19 which I’d expect on constraints like `my_var == 0`)… However, it does not seem like the ordering of constraint\_values matches what I get when I output `print(model)`. Is there a way to link the output of `eval_constraint`/`constraint_values` to constraints in the original model?

Thank you.

**NOTE** : As I mentioned above, my ultimate goal is to get the values of constraints evaluated at arbitrary values of the variables. If there is a better way to do this, I’m also happy to hear about that. The only caveat worth mentioning is that I don’t have easy access to the internals of `complicated_model_builder()` to pass through any expressions that are defined during the constraint definitions.

---

<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 30, 2023, 8:50pm UTC](https://discourse.julialang.org/t/how-to-get-ordering-of-constraints-when-calling-eval-constraint/93772/2 "2023-01-30T20:50:28Z")

</div>

No need to use `eval_constraint`. This should point you in the right direction:

```julia
julia> using JuMP

julia> model = Model();

julia> @variable(model, x >= 0)
x

julia> @constraint(model, c, 2x <= 1)
c : 2 x ≤ 1.0

julia> @NLconstraint(model, nl_con, 0 <= sin(x) <= 0.5)
0 ≤ sin(x) ≤ 0.5

julia> variable_values = Dict(v => rand() for v in all_variables(model))
Dict{VariableRef, Float64} with 1 entry:
  x => 0.379509

julia> cons = all_constraints(model; include_variable_in_set_constraints = true)
3-element Vector{ConstraintRef}:
 c : 2 x ≤ 1.0
 x ≥ 0.0
 0 ≤ sin(x) ≤ 0.5

julia> sol = Dict(c => value(xi -> variable_values[xi], c) for c in cons)
Dict{ConstraintRef{Model, C, ScalarShape} where C, Float64} with 3 entries:
  0 ≤ sin(x) ≤ 0.5 => 0.370465
  x ≥ 0.0 => 0.379509
  c : 2 x ≤ 1.0 => 0.759019

julia> sol[c]
0.759018812310488

julia> sol[nl_con]
0.37046482764020316

julia> sol[LowerBoundRef(x)]
0.379509406155244

```

---

<div class="post-metadata">

### Author: ![hugh\_man](https://avatars.discourse-cdn.com/v4/letter/h/51bf81/32.png) [@hugh\_man](https://discourse.julialang.org/u/hugh_man)
#### Post date: [January 31, 2023, 4:00pm UTC](https://discourse.julialang.org/t/how-to-get-ordering-of-constraints-when-calling-eval-constraint/93772/3 "2023-01-31T16:00:09Z")

</div>

That works great, thank you!
