gurobipy - add specific constraints as "Lazy"
AnsweredI'd like to add specific linear constraints to a model and set them as 'Lazy'. Of course I'd like to use the addConstrs method to build the model fast.
Can anybody suggest an example in gurobipy where this is applied? I cannot find an example in the documentation.
Thank you!
-
addConstrs returns a tupledict of constraints that you can use latter on to set the lazy attribute
0 -
Thank you! Can you link an example where constraints get the "Lazy" attribute set after they get created with ? I can't find any in the documentation and on Google
0 -
Hi Niccolò,
Here is a simple example showing how to set the Lazy attribute after creating the constraints.
# 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 = 1I hope this helps!
Eli
1 -
awesome! exaclty what I was looking for. thanks!
0 -
how does this work in the c++ api?
0 -
Hi,
You can find C++ attribute examples here. C++ is not as elegant as Python, and you need to include the type of the attribute in the identifier. The Lazy attribute is integer-valued, so you would set it on a constraint object like this:
constraint.set(GRB_IntAttr_Lazy, 1);
Silke
0 -
Hey, thanks a lot for your quick reply.
And I get the constraint object by:
auto constraint = model.addConstr(...);
followed by
constraint.set(GRB_IntAttr_Lazy, 2);
right? (just for completeness sake)
Julian
0 -
Yes, exactly!
Of course, you can also iterate over an array of constraints (e.g. one returned by addConstrs) to set the Lazy attribute.
Silke
0
Please sign in to leave a comment.
Comments
8 comments