# Getting constraint values using JuMP

**URL:** <https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [February 9, 2017, 2:58am UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996 "2017-02-09T02:58:16Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [February 9, 2017, 2:58am UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/1 "2017-02-09T02:58:16Z")

</div>

I am interested in getting the values of my constraints evaluated at a particular spot in the design space. Here is an example to explain what I am trying to do:

```julia
using JuMP
m = Model()
@variable(m, x, start = 0.0)
@variable(m, y, start = 0.0)
@NLobjective(m, Min, (1-x)^2 + 100(y-x^2)^2)
N=@NLconstraint(m, x^2 + y == 10)
solve(m)

```

I know that I can look at the infeasibility of the dual problem using:

```julia
getdual(N)

```

But, I would like to do something like

```julia
julia> getvalue(N)
ERROR: MethodError: no method matching getvalue(::JuMP.ConstraintRef{JuMP.Model,JuMP.GenericRangeConstraint{JuMP.NonlinearExprData}})
Closest candidates are:
  getvalue(::JuMP.NonlinearExpression) at /home/febbo/.julia/v0.5/JuMP/src/nlp.jl:1323
  getvalue(::JuMP.NonlinearParameter) at /home/febbo/.julia/v0.5/JuMP/src/nlp.jl:41
  getvalue(::JuMP.GenericQuadExpr{Float64,JuMP.Variable}) at /home/febbo/.julia/v0.5/JuMP/src/quadexpr.jl:92
  ...

```

So, I want to look at the values of the constraints in my actual problem, not the dual problem.

Is there a way to do this without turning my constraints into @NLexpressions?

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 9, 2017, 12:57pm UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/2 "2017-02-09T12:57:40Z")

</div>

You can use `eval_g` via the [derivative evaluation interface](http://www.juliaopt.org/JuMP.jl/0.15/nlp.html#querying-derivatives-from-a-jump-model). However, JuMP currently normalizes most constraints to have zero on the right-hand side, so the “value” of the constraint will not correspond to the left-hand side that you wrote down.

---

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [February 10, 2017, 7:29pm UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/3 "2017-02-10T19:29:03Z")

</div>

@miles.lubin thanks!

---

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [February 14, 2017, 12:55am UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/4 "2017-02-14T00:55:44Z")

</div>

@miles.lubin I am still trying to figure this out.

Lets say that I have this model:

```julia
m = Model()
@variable(m, x)
@variable(m, y)
con1=@constraint(m, x<=2)
con2=@constraint(m, 2<=y<=4)
@NLobjective(m, Min, sin(x) + sin(y))

```

Then I would like to evaluate the constraints at:

```julia
values = zeros(2)
values[linearindex(x)] = 1.0
values[linearindex(y)] = 5.0

```

Then I do some setup:

```julia
d = JuMP.NLPEvaluator(m)
MathProgBase.initialize(d, [:Grad])
g = zeros(2);
MathProgBase.eval_g(d, g, values)

```

I query:

```julia
julia> g[linearindex(x)]
1.0

julia> g[linearindex(y)]
5.0

```

I know that you said that JuMP normalizes the constraints so that the right hand side is zero, so for

```julia
julia> g[linearindex(x)]

```

I might expect an output like:

```julia
1-2=-2

```

and for

```julia
julia> g[linearindex(x)]

```

I might expect an output like:

```julia
[5-4; 
2-5]

```

For the two constraints the upper and lower constraint values

Can you please help me understand this?

Thanks!

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 14, 2017, 2:12am UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/5 "2017-02-14T02:12:29Z")

</div>

The `g` vector is indexed linearly by constraints, not by variables. Linear and nonlinear constraints are handled differently with respect to their right-hand sides. (Furthermore, although it’s not relevant to this particular example, the ordering between linear and nonlinear constraints is described in the documentation.)

---

<div class="post-metadata">

**Author:** ![huckl3b3rry87](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/huckl3b3rry87/32/3163_2.png) [@huckl3b3rry87](https://discourse.julialang.org/u/huckl3b3rry87)\
**Post date:** [February 14, 2017, 9:27pm UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/7 "2017-02-14T21:27:44Z")

</div>

@miles.lubin thank you for your help, for completeness I will post the solution I obtained,

The functionality can be used to check the feasibility of an `x` vector w.r.t. the actual constraints ( @miles.lubin if there is a better way to do this please comment) .

```julia
m = Model()
@variable(m, x)
@variable(m, y)
con1=@constraint(m, 2*x + 1 <=4)
con2=@NLconstraint(m, y^2<=4)

@NLobjective(m, Min, sin(x) + sin(y))
values = zeros(2)
values[linearindex(x)] = 1.0
values[linearindex(y)] = 5.0

d = JuMP.NLPEvaluator(m)
MathProgBase.initialize(d, [:Grad])
g = zeros(MathProgBase.numconstr(d.m));
MathProgBase.eval_g(d, g, values)

b = JuMP.constraintbounds(m);
if minimum(sign([g-b[1];b[2]-g])) == -1
  pass = false;
else
  pass = true;
end

```

---

<div class="post-metadata">

**Author:** ![miles.lubin](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/miles.lubin/32/279_2.png) [@miles.lubin](https://discourse.julialang.org/u/miles.lubin)\
**Post date:** [February 15, 2017, 1:48am UTC](https://discourse.julialang.org/t/getting-constraint-values-using-jump/1996/8 "2017-02-15T01:48:39Z")

</div>

The JuMP usage seems fine to me. Typically you’d check constraint satisfaction within some numerical tolerance.
