Hide navigation sidebar
Hide table of contents sidebar
Back to top
Toggle table of contents sidebar

# 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);
    }

    if (where == GRB_CB_MIP) {
        double nodecnt = getDoubleInfo(GRB_CB_MIP_NODCNT);
        int solcnt = getIntInfo(GRB_CB_MIP_SOLCNT);
        std::cout << "node count: " << nodecnt
                  << " solution count: " << solcnt << std::endl;
    }

    if (where == GRB.Callback.MIP) {
        double nodecnt = GetDoubleInfo(GRB.Callback.MIP_NODCNT);
        int    solcnt  = GetIntInfo(GRB.Callback.MIP_SOLCNT);
        Console.WriteLine("node count: " nodecnt
                          + " solution count: " + solcnt);
    }

    if (where == GRB.CB_MIP) {
        double nodecnt = getDoubleInfo(GRB.CB_MIP_NODCNT);
        int    solcnt  = getIntInfo(GRB.CB_MIP_SOLCNT);
        System.out.println("node count: " + nodecnt
                           + " solution count: " + solcnt);
    }

    if where == GRB.Callback.MIP:
        nodecnt = model.cbGet(GRB.Callback.MIP_NODCNT)
        solcnt = model.cbGet(GRB.Callback.MIP_SOLCNT)
        print(f"node count: {nodecnt} solution count: {solcnt}")

    If where = GRB.Callback.MIP Then
        Dim nodecnt As Double = GetDoubleInfo(GRB.Callback.MIP_NODCNT)
        Dim solcnt As Integer = GetIntInfo(GRB.Callback.MIP_SOLCNT)
        Console.WriteLine("node count: " & nodecnt &_
                          " solution count: " & solcnt)
    End If

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.

Help and Feedback

Press `/` or click to search this documentation | Gurobi website | Gurobi community forum
Go to other Gurobi documentation | Gurobi download | Contact support