Try our new documentation site (beta).
Model.cbLazy()
cbLazy ( lhs, sense, rhs )
Add a new lazy constraint to a MIP model from within a callback function.
Note that this method can only be invoked when the
where
value on the callback function is
GRB.Callback.MIPNODE
or GRB.Callback.MIPSOL
(see the
Callback Codes section
for more information).
Lazy constraints are typically used when the full set of constraints for a MIP model is too large to represent explicitly. By only including the constraints that are actually violated by solutions found during the branch-and-cut search, it is sometimes possible to find a proven optimal solution while only adding a fraction of the full set of constraints.
You would typically add a lazy constraint by first querying the
current node solution (by calling
cbGetSolution from a
GRB.Callback.MIPSOL
callback, or
cbGetNodeRel from a
GRB.Callback.MIPNODE
callback), and then calling cbLazy()
to add a
constraint that cuts off the solution. Gurobi guarantees that you
will have the opportunity to cut off any solutions that would
otherwise be considered feasible.
MIP solutions may be generated outside of a MIP node. Thus, generating
lazy constraints is optional when the where
value in the callback
function equals GRB.Callback.MIPNODE
. To avoid this, we recommend to
always check when the where
value equals GRB.Callback.MIPSOL
.
Your callback should be prepared to cut off solutions that violate any of your lazy constraints, including those that have already been added. Node solutions will usually respect previously added lazy constraints, but not always.
Note that you must set the LazyConstraints parameter if you want to use lazy constraints.
Note that this method also accepts a TempConstr as its first argument. This allows you to use operator overloading to create constraints. See TempConstr for more information.
Arguments:
lhs: Left-hand side for new lazy constraint. Can be a constant, a Var, or a LinExpr.
sense: Sense for new lazy constraint (GRB.LESS_EQUAL, GRB.EQUAL, or GRB.GREATER_EQUAL).
rhs: Right-hand side for new lazy constraint. Can be a constant, a Var, or a LinExpr.
Example usage:
def mycallback(model, where): if where == GRB.Callback.MIPSOL: sol = model.cbGetSolution([model._vars[0], model._vars[1]]) if sol[0] + sol[1] > 1.1: model.cbLazy(model._vars[0] + model._vars[1] <= 1) model._vars = model.getVars() model.optimize(mycallback)