Callbacks#

Callbacks are a powerful feature in Gurobi that allow users to interact with the optimization process while it’s running. A callback is a user-defined function that is called periodically by the Gurobi Optimizer, enabling you to query or modify the state of the optimization in real-time.

Typical Use Cases#

Gurobi callbacks can be used both to monitor the progress of the optimization and to modify the behavior of the Gurobi Optimizer. Some typical use cases are:

  • Produce a custom display.

  • Define custom specific termination criteria.

  • Proceed to the next phase of the computation.

  • Add user cuts.

  • Add lazy constraints.

  • Implement a custom heuristic.

  • For multi-objective problems, interrupt the optimization process of one of the optimization steps in a multi-objective MIP problem without stopping the hierarchical optimization process.

Retrieving Information#

Associated with the user callback function is a where argument that indicates from where in the optimization process the user callback is being called. It is possible to retrieve several information from Gurobi by using the appropriate get funtions (C, C++, Java, .NET, Python):

if (where == GRB_CB_MIP) {
    double nodecnt;
    int solcnt;
    error = GRBcbget(cbdata, where, GRB_CB_MIP_NODECNT, (void *) &nodecnt);
    if (error) return 0;
    error = GRBcbget(cbdata, where, GRB_CB_MIP_SOLCNT, &solcnt);
    if (error) return 0;
    printf("node count: %d solution count: %d\n", nodecnt, solcnt);
}

The information retrievable through these functions, referred to as what values, ranges from general metrics such as elapsed runtime or number of found solutions, to specific data like the current simplex iteration count. The availability of each type of information depends on where the callback is invoked. Please refer to Callback Codes for available what values w.r.t. the where argument.

The object-oriented interfaces have specialized methods for obtaining the incumbent or relaxation solution. While in C you would use GRBcbget with what=GRB_CB_MIPSOL_SOL for the incumbent and with what = GRB_CB_MIPNODE_REL for the relaxation, you use getSolution (C++, Java, .NET, Python) or getNodeRel (C++, Java, .NET, Python) for the object-oriented interfaces.

Gurobi also offers additional specialized functions to support the uses cases mentioned above. For more information refer to the callback documentation specific to your API: C++, Java, .NET, or Python. For C users, see the Logging and Callbacks and Modifying Solver Behavior sections.

To see how a callback function is implemented across different APIs, consult the callback example, which demonstrates how to monitor optimization progress, implement a custom termination strategy, and output progress data. You may also refer to the tsp example, which uses a callback to generate lazy constraints.

Restrictions#

Note that there are certain restrictions concerning the available callbacks when using the Gurobi Remote Services (Compute Server, Instant Cloud, etc.). Please refer to the Callbacks section of the Gurobi Remote Services Manual for more information.