# Setting constraints as lazy with JuMP 0.19 and Gurobi

**URL:** <https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516>\
**Category:** Optimization (Mathematical)\
**Created:** [May 23, 2019, 1:38pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516 "2019-05-23T13:38:14Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![fa-bien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fa-bien/32/5559_2.png) [@fa-bien](https://discourse.julialang.org/u/fa-bien)\
**Post date:** [May 23, 2019, 1:38pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/1 "2019-05-23T13:38:14Z")

</div>

I build a mixed-integer model with JuMP and Gurobi. I want to set certain constraints as lazy. With JuMP 0.18 I was using the solution described here: [Lazy constraints at root node? · Issue #1226 · jump-dev/JuMP.jl · GitHub](https://github.com/JuliaOpt/JuMP.jl/issues/1226)

Now I am using JuMP 0.19 and I cannot find information on how to achieve the same thing. Is it currently possible? If not, what would it take?

---

<div class="post-metadata">

**Author:** ![blegat](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/blegat/32/217090_2.png) [@blegat](https://discourse.julialang.org/u/blegat)\
**Post date:** [May 23, 2019, 8:25pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/2 "2019-05-23T20:25:34Z")

</div>

See [Gurobi.jl/MOI\_wrapper.jl at f7332cbd2881fa0f776776de2cceabfb7b97f750 · jump-dev/Gurobi.jl · GitHub](https://github.com/JuliaOpt/Gurobi.jl/blob/f7332cbd2881fa0f776776de2cceabfb7b97f750/test/MOI_wrapper.jl#L106-L198) for examples and [https://github.com/JuliaOpt/Gurobi.jl/pull/194](https://github.com/JuliaOpt/Gurobi.jl/pull/194), [DNMY: add solver-independent(-ish) callbacks by odow · Pull Request #670 · jump-dev/MathOptInterface.jl · GitHub](https://github.com/JuliaOpt/MathOptInterface.jl/pull/670) for future directions

---

<div class="post-metadata">

**Author:** ![fa-bien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fa-bien/32/5559_2.png) [@fa-bien](https://discourse.julialang.org/u/fa-bien)\
**Post date:** [May 27, 2019, 10:21am UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/3 "2019-05-27T10:21:26Z")

</div>

Thank you. This seems to be about adding lazy cuts from a callback. I was referring to adding lazy constraints when creating a model, which is a different (although similar) concept, and which does not rely on the concept of callback.

Edit for clarity: by that I mean that lazy constraints do not require to write an additional callback, Gurobi handles them. Ideally, adding `Lazy=true` when creating a constraint could tell Gurobi that this constraint is lazy.

---

<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:** [May 27, 2019, 8:06pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/4 "2019-05-27T20:06:59Z")

</div>

Since you know that you’re using Gurobi, you should use JuMP in direct-mode. The new syntax would be:

```julia
model = JuMP.direct_model(Gurobi.Optimizer())
@variable(model, x >= 0)
@constraint(model, c, 2x >= 1)
grb = JuMP.backend(model)
row = grb[JuMP.index(c)]
Gurobi.set_intattrelement!(grb.inner, "Lazy", row, 3)

```

If you use this a lot, it’d be a nice contribution to add this as a MOI attribute, similar to the in-progress IIS: [Compute IIS by dourouc05 · Pull Request #208 · jump-dev/Gurobi.jl · GitHub](https://github.com/JuliaOpt/Gurobi.jl/pull/208)

Then the syntax would be

```nohighlight
model = Modle(with_optimizer(Gurobi.Optimizer))
@variable(model, x >= 0)
@constraint(model, c, 2x >= 1)
MOI.set(model, Gurobi.Lazy(), c, 3)

```

or even

```nohighlight
model = Modle(with_optimizer(Gurobi.Optimizer))
@variable(model, x >= 0)
@constraint(model, c, 2x >= 1)
MOI.set(model, Gurobi.ConstraintAttribute("lazy"), c, 3)

```

Let me know, and we can point you in the right direction.

---

<div class="post-metadata">

**Author:** ![fa-bien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/fa-bien/32/5559_2.png) [@fa-bien](https://discourse.julialang.org/u/fa-bien)\
**Post date:** [May 28, 2019, 5:46am UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/5 "2019-05-28T05:46:28Z")

</div>

Thank you, this works!

I would like to add this as a MOI attribute, where do I start?

---

<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:** [May 28, 2019, 1:35pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/6 "2019-05-28T13:35:51Z")

</div>

I’ve opened an issue with some pointers: [[MOI] Set Gurobi-specific attributes · Issue #211 · jump-dev/Gurobi.jl · GitHub](https://github.com/JuliaOpt/Gurobi.jl/issues/211)

Let’s continue the discussion there.

---

<div class="post-metadata">

**Author:** ![Prashant\_Chintapalli](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/prashant_chintapalli/32/9539_2.png) [@Prashant\_Chintapalli](https://discourse.julialang.org/u/Prashant_Chintapalli)\
**Post date:** [July 27, 2019, 5:39am UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/7 "2019-07-27T05:39:06Z")

</div>

What are the equivalent steps for the Lazy constraints generation that is described in the “Lazy Constraints” section of [Solver Callbacks — JuMP -- Julia for Mathematical Optimization 0.18 documentation](http://www.juliaopt.org/JuMP.jl/0.18/callbacks.html#lazy-constraints)?

---

<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:** [July 27, 2019, 7:15pm UTC](https://discourse.julialang.org/t/setting-constraints-as-lazy-with-jump-0-19-and-gurobi/24516/8 "2019-07-27T19:15:04Z")

</div>

We’re still in the process of re-introducing callbacks into JuMP 0.19. For now, if you need callbacks, stick with JuMP 0.18.
