GRBCallback#
-
class GRBCallback#
Gurobi callback class. This is an abstract class. To implement a callback, you should create a subclass of this class and implement a
callback()
method. If you pass an object of this subclass to methodGRBModel::setCallback
before callingGRBModel::optimize
orGRBModel::computeIIS
, thecallback()
method of the class will be called periodically. Depending on where the callback is called from, you can obtain various information about the progress of the optimization.Note that this class contains one protected int member variable:
where
. You can query this variable from yourcallback()
method to determine where the callback was called from.Gurobi callbacks can be used both to monitor the progress of the optimization and to modify the behavior of the Gurobi optimizer. A simple user callback function might call the
GRBCallback::getIntInfo
orGRBCallback::getDoubleInfo
methods to produce a custom display, or perhaps to terminate optimization early (usingGRBCallback::abort
) or to proceed to the next phase of the computation (usingGRBCallback::proceed
). More sophisticated MIP callbacks might useGRBCallback::getNodeRel
orGRBCallback::getSolution
to retrieve values from the solution to the current node, and then useGRBCallback::addCut
orGRBCallback::addLazy
to add a constraint to cut off that solution, orGRBCallback::setSolution
to import a heuristic solution built from that solution. For multi-objective problems, you might useGRBCallback::stopOneMultiObj
to interrupt the optimization process of one of the optimization steps in a multi-objective MIP problem without stopping the hierarchical optimization process.When solving a model using multiple threads, the user callback is only ever called from a single thread, so you don’t need to worry about the thread-safety of your callback.
Note that changing parameters from within a callback is not supported, doing so may lead to undefined behavior.
You can look at the callback_c++.cpp example for details of how to use Gurobi callbacks.
-
GRBCallback GRBCallback()#
Callback constructor.
- Returns:
A callback object.
-
void abort()#
Abort optimization. When the optimization stops, the Status attribute will be equal to
GRB_INTERRUPTED
.
-
void addCut(const GRBLinExpr &lhsExpr, char sense, double rhsVal)#
Add a cutting plane to the MIP model from within a callback function. Note that this method can only be invoked when the
where
member variable is equal toGRB_CB_MIPNODE
(see the Callback Codes section for more information).Cutting planes can be added at any node of the branch-and-cut tree. However, they should be added sparingly, since they increase the size of the relaxation model that is solved at each node and can significantly degrade node processing speed.
Cutting planes are typically used to cut off the current relaxation solution. To retrieve the relaxation solution at the current node, you should first call
getNodeRel
.You should consider setting parameter PreCrush to value 1 when adding your own cuts. This setting shuts off a few presolve reductions that can sometimes prevent your cut from being applied to the presolved model (which would result in your cut being silently ignored).
Note that cutting planes added through this method must truly be cutting planes – they can cut off continuous solutions, but they may not cut off integer solutions that respect the original constraints of the model. Ignoring this restriction will lead to incorrect solutions.
- Parameters:
lhsExpr – Left-hand side expression for new cutting plane.
sense – Sense for new cutting plane (
GRB_LESS_EQUAL
,GRB_EQUAL
, orGRB_GREATER_EQUAL
).rhsVal – Right-hand side value for new cutting plane.
-
void addCut(GRBTempConstr &tc)#
Add a cutting plane to the MIP model from within a callback function. Note that this method can only be invoked when the
where
member variable is equal toGRB_CB_MIPNODE
(see the Callback Codes section for more information).Cutting planes can be added at any node of the branch-and-cut tree. However, they should be added sparingly, since they increase the size of the relaxation model that is solved at each node and can significantly degrade node processing speed.
Cutting planes are typically used to cut off the current relaxation solution. To retrieve the relaxation solution at the current node, you should first call
getNodeRel
.You should consider setting parameter PreCrush to value 1 when adding your own cuts. This setting shuts off a few presolve reductions that can sometimes prevent your cut from being applied to the presolved model (which would result in your cut being silently ignored).
Note that cutting planes added through this method must truly be cutting planes – they can cut off continuous solutions, but they may not cut off integer solutions that respect the original constraints of the model. Ignoring this restriction will lead to incorrect solutions.
- Parameters:
tc – Temporary constraint object, created using an overloaded comparison operator. See
GRBTempConstr
for more information.
-
void addLazy(const GRBLinExpr &lhsExpr, char sense, double rhsVal)#
Add a lazy constraint to the MIP model from within a callback function. Note that this method can only be invoked when the
where
member variable is equal toGRB_CB_MIPNODE
orGRB_CB_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
getSolution
from aGRB_CB_MIPSOL
callback, orgetNodeRel
from aGRB_CB_MIPNODE
callback), and then callingaddLazy()
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 equalsGRB_CB_MIPNODE
. To avoid this, we recommend to always check when thewhere
value equalsGRB_CB_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.
- Parameters:
lhsExpr – Left-hand side expression for new lazy constraint.
sense – Sense for new lazy constraint (
GRB_LESS_EQUAL
,GRB_EQUAL
, orGRB_GREATER_EQUAL
).rhsVal – Right-hand side value for new lazy constraint.
-
void addLazy(GRBTempConstr &tc)#
Add a lazy constraint to the MIP model from within a callback function. Note that this method can only be invoked when the
where
member variable is equal toGRB_CB_MIPNODE
orGRB_CB_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
getSolution
from aGRB_CB_MIPSOL
callback, orgetNodeRel
from aGRB_CB_MIPNODE
callback), and then callingaddLazy()
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 equalsGRB_CB_MIPNODE
. To avoid this, we recommend to always check when thewhere
value equalsGRB_CB_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.
- Parameters:
tc – Temporary constraint object, created using an overloaded comparison operator. See
GRBTempConstr
for more information.
-
double getDoubleInfo(int what)#
Request double-valued callback information. The available information depends on the value of the
where
member. For information on possible values ofwhere
, and the double-valued information that can be queried for different values ofwhere
, please refer to the Callback section.- Parameters:
what – Information requested (refer the list of Gurobi Callback Codes for possible values).
- Returns:
Value of requested callback information.
-
int getIntInfo(int what)#
Request int-valued callback information. The available information depends on the value of the
where
member. For information on possible values ofwhere
, and the int-valued information that can be queried for different values ofwhere
, please refer to the Callback section.- Parameters:
what – Information requested (refer to the list of Gurobi Callback Codes for possible values).
- Returns:
Value of requested callback information.
-
double getNodeRel(GRBVar v)#
Retrieve values from the node relaxation solution at the current node. Only available when the
where
member variable is equal toGRB_CB_MIPNODE
, andGRB_CB_MIPNODE_STATUS
is equal toGRB_OPTIMAL
.- Parameters:
v – The variable whose value is desired.
- Returns:
The value of the specified variable in the node relaxation for the current node.
-
double *getNodeRel(const GRBVar *xvars, int len)#
Retrieve values from the node relaxation solution at the current node. Only available when the
where
member variable is equal toGRB_CB_MIPNODE
, andGRB_CB_MIPNODE_STATUS
is equal toGRB_OPTIMAL
.- Parameters:
xvars – The list of variables whose values are desired.
len – The number of variables in the list.
- Returns:
The values of the specified variables in the node relaxation for the current node. Note that the result is heap-allocated, and must be returned to the heap by the user.
-
double getSolution(GRBVar v)#
Retrieve values from the current solution vector. Only available when the
where
member variable is equal toGRB_CB_MIPSOL
orGRB_CB_MULTIOBJ
.- Parameters:
v – The variable whose value is desired.
- Returns:
The value of the specified variable in the current solution vector.
-
double *getSolution(const GRBVar *xvars, int len)#
Retrieve values from the current solution vector. Only available when the
where
member variable is equal toGRB_CB_MIPSOL
orGRB_CB_MULTIOBJ
.- Parameters:
xvars – The list of variables whose values are desired.
len – The number of variables in the list.
- Returns:
The values of the specified variables in the current solution. Note that the result is heap-allocated, and must be returned to the heap by the user.
-
string getStringInfo(int what)#
Request string-valued callback information. The available information depends on the value of the
where
member. For information on possible values ofwhere
, and the string-valued information that can be queried for different values ofwhere
, please refer to the Callback section.- Parameters:
what – Information requested (refer to the list of Gurobi Callback Codes for possible values).
- Returns:
Value of requested callback information.
-
void proceed()#
Generate a request to proceed to the next phase of the computation. Note that the request is only accepted in a few phases of the algorithm, and it won’t be acted upon immediately.
In the current Gurobi version, this callback allows you to proceed from the NoRel heuristic to the standard MIP search. You can determine the current algorithm phase using
MIP_PHASE
,MIPNODE_PHASE
, orMIPSOL_PHASE
queries from a callback.
-
void setSolution(GRBVar v, double val)#
Import solution values for a heuristic solution. Only available when the
where
member variable is equal toGRB_CB_MIP
,GRB_CB_MIPNODE
, orGRB_CB_MIPSOL
(see the Callback Codes section for more information).When you specify a heuristic solution from a callback, variables initially take undefined values. You should use this method to specify variable values. You can make multiple calls to
setSolution
from one callback invocation to specify values for multiple sets of variables. After the callback, if values have been specified for any variables, the Gurobi optimizer will try to compute a feasible solution from the specified values, possibly filling in values for variables whose values were left undefined. You can also optionally calluseSolution
within your callback function to try to immediately compute a feasible solution from the specified values.Note that this method is not supported in a Compute Server environment.
- Parameters:
v – The variable whose values is being set.
val – The value of the variable in the new solution.
-
void setSolution(const GRBVar *xvars, const double *sol, int len)#
Import solution values for a heuristic solution. Only available when the
where
member variable is equal toGRB_CB_MIP
,GRB_CB_MIPNODE
, orGRB_CB_MIPSOL
(see the Callback Codes section for more information).When you specify a heuristic solution from a callback, variables initially take undefined values. You should use this method to specify variable values. You can make multiple calls to
setSolution
from one callback invocation to specify values for multiple sets of variables. After the callback, if values have been specified for any variables, the Gurobi optimizer will try to compute a feasible solution from the specified values, possibly filling in values for variables whose values were left undefined. You can also optionally calluseSolution
within your callback function to try to immediately compute a feasible solution from the specified values.Note that this method is not supported in a Compute Server environment.
- Parameters:
xvars – The variables whose values are being set.
sol – The values of the variables in the new solution.
len – The number of variables.
-
void stopOneMultiObj(int objcnt)#
Interrupt the optimization process of one of the optimization steps in a multi-objective MIP problem without stopping the hierarchical optimization process. Only available for multi-objective MIP models and when the where member variable is not equal to GRB_CB_MULTIOBJ (see the Callback Codes section for more information).
You would typically stop a multi-objective optimization step by querying the last finished number of multi-objectives steps, and using that number to stop the current step and move on to the next hierarchical objective (if any) as shown in the following example:
#include <ctime> class mycallback: public GDBCallback { public: int objcnt = 0; time_t starttime = time(); protected: void callback () { if (where == GRB_CB_MULTIOBJ) { /* get current objective number */ objcnt = getIntInfo(GRB_CB_MULTIOBJ_OBJCNT); /* reset start time to current time */ starttime = time(); } else if (time() - startime > BIG || /* takes too long or good enough */) { /* stop only this optimization step */ stopOneMultiObj(objcnt); } } }
You should refer to the section on Multiple Objectives for information on how to specify multiple objective functions and control the trade-off between them.
- Parameters:
objnum – The number of the multi-objective optimization step to interrupt. For processes running locally, this argument can have the special value -1, meaning to stop the current step.
-
double useSolution()#
Once you have imported solution values using
setSolution
, you can optionally calluseSolution
in aGRB_CB_MIPNODE
callback to immediately use these values to try to compute a heuristic solution. Alternatively, you can calluseSolution
in aGRB_CB_MIP
orGRB_CB_MIPSOL
callback, which will store the solution until it can be processed internally.- Returns:
The objective value for the solution obtained from your solution values. It equals
GRB_INFINITY
if no improved solution is found or the method has been called from a callback other thanGRB_CB_MIPNODE
.
-
GRBCallback GRBCallback()#