# Finding all binding constraints using JuMP

**URL:** <https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579>\
**Category:** Optimization (Mathematical)\
**Tags:** jump\
**Created:** [June 10, 2022, 7:07pm UTC](https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579 "2022-06-10T19:07:07Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 10, 2022, 7:07pm UTC](https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579/1 "2022-06-10T19:07:07Z")

</div>

Is this the correct way to find all the binding constraints in JuMP?  
I figure it is only binding of it has a nonzero shadow-price.  
However `shadow_price` only works for `<`, `>` and `==` constraints so I am not sure i am going about this the right way.  
Is there a better way that would also find when a Conic constraint was binding?

```julia
function binding_constraints(model, threshold=1e-8)
    binding_cons = ConstraintRef[]
    for (F, S) in list_of_constraint_types(model)  
        # only these relations are supported by shadow_price
        S <: Union{MOI.EqualTo, MOI.LessThan, MOI.GreaterThan} || continue
        
        for con in all_constraints(model, F, S)
            if abs(shadow_price(con)) > threshold || continue
                push!(binding_cons, con)
            end
        end
    end
    return binding_cons
end

```

---

<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:** [June 12, 2022, 7:05am UTC](https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579/2 "2022-06-12T07:05:25Z")

</div>

Use `dual` instead of `shadow_price`. `shadow_price` is just for convenience for linear programs. (JuMP uses a different convention, so a lot of classes will teach “shadow price” instead of conic duality.)

What constraints do you want to check though?

---

<div class="post-metadata">

**Author:** ![oxinabox](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/oxinabox/32/206603_2.png) [@oxinabox](https://discourse.julialang.org/u/oxinabox)\
**Post date:** [June 13, 2022, 11:19am UTC](https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579/3 "2022-06-13T11:19:35Z")

</div>

> [@odow](#):
>
> What constraints do you want to check though?

Not sure yet.  
Really I want to tweak my model until one particular constraint _is_ binding.  
Which probably means finding other ones that are binding and relaxing them.

So

```julia
function binding_constraints(model, threshold=1e-8)
    binding_cons = ConstraintRef[]
    for (F, S) in list_of_constraint_types(model)  
        for con in all_constraints(model, F, S)
            if abs(dual(con)) > threshold || continue
                push!(binding_cons, con)
            end
        end
    end
    return binding_cons
end

```

---

<div class="post-metadata">

**Author:** ![mtanneau](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mtanneau/32/17787_2.png) [@mtanneau](https://discourse.julialang.org/u/mtanneau)\
**Post date:** [June 13, 2022, 1:25pm UTC](https://discourse.julialang.org/t/finding-all-binding-constraints-using-jump/82579/4 "2022-06-13T13:25:35Z")

</div>

Careful with that `abs(dual(con))`: if you have conic constraints, `dual(con)` will be a `Vector`, and `abs` will error. I would suggest you use `norm` instead.

A more mathematical note (for completeness; probably not news to you): one the one hand, if the dual is non-zero, then the constraint will be tight (assuming your solution satisfies complementary slackness). Nothing new here.  
On the other hand, however, there may exist a constraint a^{T}x \geq b such that, at the optimum, a^{T}x^{\*} = b, but the corresponding dual is zero. This is not restricted to linear constraints, and it may happen when you have redundant constraints and/or multiple optimal solutions.
