How to pass a constraint as Lazy with Gurobi?

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:

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

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


# 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.

I tried the following thanks to Gurobi.jl:

 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.

I have not tried this, but your approach does not look similar to the example given in the Gurobi.jl documentation.. As far as I understand it, you should give your constraint a name (c in the given example), and set the attribute with:

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

@mike_k is correct. See the example in the documentation: GitHub - jump-dev/Gurobi.jl: Julia interface for Gurobi Optimizer

Another approach that works for most cases is:

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> 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.
1 Like

Thank you for your answer @odow and @mike_k

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

1 Like

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

1 Like

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

1 Like