GRBCallback#
- 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 will be able to 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.java example for details of how to use Gurobi callbacks.
- GRBCallback GRBCallback()#
Callback constructor.
- Return value:
A callback object.
- void abort()#
Abort optimization. When the optimization stops, the Status attribute will be equal to
GRB.Status.INTERRUPTED
.
- void addCut(GRBLinExpr lhsExpr, char sense, double rhs)#
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 in the Reference Manual 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.
- Arguments:
lhsExpr – Left-hand side expression for new cutting plane.
sense – Sense for new cutting plane (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhs – Right-hand side value for new cutting plane.
- void addLazy(GRBLinExpr lhsExpr, char sense, double rhs)#
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 in the Reference Manual 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.
- Arguments:
lhsExpr – Left-hand side expression for new lazy constraint.
sense – Sense for new lazy constraint (
GRB.LESS_EQUAL
,GRB.EQUAL
, orGRB.GREATER_EQUAL
).rhs – Right-hand side value for new lazy constraint.
- 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 Codes section of the Reference Manual.- Arguments:
what – Information requested. Please refer to the list of Callback Codes in the Reference Manual for possible values.
- Return value:
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 Codes section in the Reference Manual.- Arguments:
what – Information requested. Please refer to the list of Callback Codes in the Reference Manual for possible values.
- Return value:
Value of requested callback information.
- double getNodeRel(GRBVar v)#
Retrieve node relaxation solution values at the current node. Only available when the
where
member variable is equal toGRB.CB_MIPNODE
, andGRB.CB_MIPNODE_STATUS
is equal toGRB.Status.OPTIMAL
.- Arguments:
v – The variable whose value is desired.
- Return value:
The value of the specified variable in the node relaxation for the current node.
- double[] getNodeRel(GRBVar[] xvars)#
Retrieve node relaxation solution values at the current node. Only available when the
where
member variable is equal toGRB.CB_MIPNODE
, andGRB.CB_MIPNODE_STATUS
is equal toGRB.Status.OPTIMAL
.- Arguments:
xvars – The list of variables whose values are desired.
- Return value:
The values of the specified variables in the node relaxation for the current node.
- double[][] getNodeRel(GRBVar[][] xvars)#
Retrieve node relaxation solution values at the current node. Only available when the
where
member variable is equal toGRB.CB_MIPNODE
, andGRB.CB_MIPNODE_STATUS
is equal toGRB.Status.OPTIMAL
.- Arguments:
xvars – The array of variables whose values are desired.
- Return value:
The values of the specified variables in the node relaxation for the current node.
- 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
.- Arguments:
v – The variable whose value is desired.
- Return value:
The value of the specified variable in the current solution vector.
- double[] getSolution(GRBVar[] xvars)#
Retrieve values from the current solution vector. Only available when the
where
member variable is equal toGRB.CB_MIPSOL
orGRB.CB_MULTIOBJ
.- Arguments:
xvars – The list of variables whose values are desired.
- Return value:
The values of the specified variables in the current solution.
- double[][] getSolution(GRBVar[][] xvars)#
Retrieve values from the current solution vector. Only available when the
where
member variable is equal toGRB.CB_MIPSOL
orGRB.CB_MULTIOBJ
.- Arguments:
xvars – The array of variables whose values are desired.
- Return value:
The values of the specified variables in the current solution.
- 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 Codes section of the Reference Manual.- Arguments:
what – Information requested. Please refer to the list of Callback Codes in the Reference Manual for possible values.
- Return value:
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.
- Arguments:
v – The variable whose values is being set.
val – The value of the variable in the new solution.
- void setSolution(GRBVar[] xvars, double[] sol)#
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.
- Arguments:
xvars – The variables whose values are being set.
sol – The desired values of the specified variables in the new solution.
- 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:
import gurobi.*; public class Callback extends GRBCallback { private int objcnt; private long starttime; protected void callback() { try { if (where == GRB.CB_MULTIOBJ) { /* get current objective number */ objcnt = getIntInfo(GRB.CB_MULTIOBJ_OBJCNT); /* reset start time to current time */ starttime = System.currentTimeMillis(); } else if (System.currentTimeMillis() - starttime > 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.
- Arguments:
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.- Return value:
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
.