# How to pass a constraint as Lazy with Gurobi?

**URL:** <https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294>\
**Category:** Optimization (Mathematical)\
**Tags:** jump, gurobi, lazy-constraint\
**Created:** [February 16, 2024, 1:44pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294 "2024-02-16T13:44:09Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [February 16, 2024, 1:44pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/1 "2024-02-16T13:44:09Z")

</div>

I want to pass a constraint as Lazy in the beginning of my JuMP model.  
Note that I do not want it to be a lazy constraint called in a Callback.

How am I supposed to do it?

The corresponding Gurobi parameter is:

> **[Lazy - Gurobi Optimization](https://www.gurobi.com/documentation/current/refman/lazy.html)**
>
> Est. reading time: 1 minute

It is also the first bullet point of this Gurobi post:

> **[How do I implement lazy constraints in Gurobi?](https://support.gurobi.com/hc/en-us/articles/360013197972-How-do-I-implement-lazy-constraints-in-Gurobi)**
>
> There are two ways to implement lazy constraints for a MIP model. 
> 
> If the constraints can be enumerated upfront, set the Lazy attribute on the constraints that should be treated as lazy.
> If the co...

Please note that the equivalent code for what I want in Python is:

```julia

# addConstrs() returns tupledict of constraints
myconstrs = m.addConstrs(x[i] + y[i] <= 1 for i in I)

# Set Lazy attribute using returned tupledict
for i in I:
    myconstrs[i].Lazy = 1

```

[According to here.](https://support.gurobi.com/hc/en-us/community/posts/360047945591-gurobipy-add-specific-constraints-as-Lazy)

I tried the following thanks to Gurobi.jl:

```julia
 m = Model(Gurobi.Optimizer);
 @variable(m, x)
 @constraint(m, 2x <= 1)
julia> GRBsetdblattrelement(m, "Lazy", 1, 1)
ERROR: MethodError: no method matching unsafe_convert(::Type{Ptr{Nothing}}, ::Model)

Closest candidates are:
  unsafe_convert(::Type{Ptr{Nothing}}, ::LibGit2.GitConfigIter)
   @ LibGit2 ~/.julia/juliaup/julia-1.10.1+0.x64.linux.gnu/share/julia/stdlib/v1.10/LibGit2/src/types.jl:1042
  unsafe_convert(::Type{Ptr{Nothing}}, ::LibGit2.GitCommit)
   @ LibGit2 ~/.julia/juliaup/julia-1.10.1+0.x64.linux.gnu/share/julia/stdlib/v1.10/LibGit2/src/types.jl:1057
  unsafe_convert(::Type{Ptr{Nothing}}, ::LibGit2.GitAnnotated)
   @ LibGit2 ~/.julia/juliaup/julia-1.10.1+0.x64.linux.gnu/share/julia/stdlib/v1.10/LibGit2/src/types.jl:1057
  ...

Stacktrace:
 [1] GRBsetdblattrelement(model::Model, attrname::String, element::Int64, newvalue::Int64)
   @ Gurobi ~/juliapkg/packages/Gurobi/yvF0V/src/gen91/libgrb_api.jl:88
 [2] top-level scope

```

I do not understand how I should pursue.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [February 16, 2024, 2:36pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/2 "2024-02-16T14:36:45Z")

</div>

I have not tried this, but your approach does not look similar to the example given in the [Gurobi.jl documentation.](https://github.com/jump-dev/Gurobi.jl?tab=readme-ov-file#accessing-gurobi-specific-attributes). As far as I understand it, you should give your constraint a name (`c` in the given example), and set the attribute with:

```julia
MOI.set(grb, Gurobi.ConstraintAttribute("Lazy"), index(c), 2)

```

---

<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:** [February 16, 2024, 9:27pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/3 "2024-02-16T21:27:47Z")

</div>

@mike_k is correct. See the example in the documentation: [GitHub - jump-dev/Gurobi.jl: Julia interface for Gurobi Optimizer](https://github.com/jump-dev/Gurobi.jl?tab=readme-ov-file#accessing-gurobi-specific-attributes)

Another approach that works for most cases is:

```julia
using JuMP, Gurobi
model = Model(Gurobi.Optimizer)
@variable(model, x >= 1, Int)
@objective(model, Max, x)
c = @constraint(model, 2.0 * x <= 3)
set_attribute(c, Gurobi.ConstraintAttribute("Lazy"), 2)
optimize!(model)

```

I say most, because it won’t work for constraints that require bridging, like this:

```Julia
julia> c = @constraint(model, 1 <= 2.0 * x <= 3)
2 x ∈ [1, 3]

julia> set_attribute(c, Gurobi.ConstraintAttribute("Lazy"), 2)

julia> optimize!(model)
ERROR: MathOptInterface.UnsupportedAttribute{Gurobi.ConstraintAttribute}: Attribute Gurobi.ConstraintAttribute("Lazy") is not supported by the model: Bridge of type `ScalarSlackBridge` does not support setting a value for the attribute `Gurobi.ConstraintAttribute("Lazy")`. If you encountered this error unexpectedly, it probably means your model has been reformulated using the bridge, and you are attempting to query an attribute that we haven't implemented yet for this bridge. Please open an issue at https://github.com/jump-dev/MathOptInterface.jl/issues/new and provide a reproducible example explaining what you were trying to do.

```

---

<div class="post-metadata">

**Author:** ![K5Julien](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/k5julien/32/210626_2.png) [@K5Julien](https://discourse.julialang.org/u/K5Julien)\
**Post date:** [February 19, 2024, 4:20pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/4 "2024-02-19T16:20:29Z")

</div>

Thank you for your answer @odow and @mike_k

What about CPLEX? I did not find it in CPLEX.jl

---

<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:** [February 19, 2024, 5:48pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/5 "2024-02-19T17:48:40Z")

</div>

To the best of my knowledge, CPLEX does not have equivalent functionality.

---

<div class="post-metadata">

**Author:** ![mike\_k](https://sea2.discourse-cdn.com/julialang/user_avatar/discourse.julialang.org/mike_k/32/211864_2.png) [@mike\_k](https://discourse.julialang.org/u/mike_k)\
**Post date:** [February 19, 2024, 5:57pm UTC](https://discourse.julialang.org/t/how-to-pass-a-constraint-as-lazy-with-gurobi/110294/6 "2024-02-19T17:57:50Z")

</div>

I agree with @odow. @K5Julien: why do you seek for such a feature at all? I am puzzeld about the use-case.
