Callbacks#

The final example we consider is the callback example, which demonstrates the use of Gurobi callbacks. Callbacks are used to report on the progress of the optimization or to modify the behavior of the Gurobi solver. To use a callback, the user writes a routine that implements the desired behavior. The routine is passed to the Gurobi optimizer when optimization begins, and the routine is called regularly during the optimization process. One argument of the user routine is a where value, which indicates from where in the optimization process the callback is invoked. The user callback routine can call the optimization library to query certain values. We refer the reader to the callback section of the Gurobi Reference Manual for more precise details.

Our callback example implements a simple termination scheme: the user passes a node count (limit) into the callback, and the callback asks the optimizer to terminate when that node count is reached. This is implemented as follows:

double nodecnt;
GRBcbget(cbdata, where, GRB_CB_MIP_NODCNT, &nodecnt);
if (nodecnt > limit)
  GRBterminate(model);

Our callback example also prints progress information.

GRBcbget(cbdata, where, GRB_CB_MIP_NODCNT, &nodecnt);
if (nodecnt - mydata->lastmsg >= 100) {
  ...
  printf("%7.0f ...", nodecnt, ...);
}