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

# Callbacks

Examples

callback

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

    double nodecnt = getDoubleInfo(GRB_CB_MIP_NODCNT);
    if (nodecnt > limit)
      abort();

    double nodecnt = GetDoubleInfo(GRB.Callback.MIP_NODCNT);
    if (nodecnt > limit)
      Abort();

    double nodecnt = getDoubleInfo(GRB.CB_MIP_NODCNT);
    if (nodecnt > limit)
      abort();

    nodecnt = model.cbGet(GRB.Callback.MIP_NODCNT)
    if nodecnt > model._mynodelimit:
        model.terminate()

    Dim nodecnt As Double = GetDoubleInfo(GRB.Callback.MIP_NODCNT)
    If nodecnt > limit Then
        Abort()
    End If

Our callback example also prints progress information.

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

    double nodecnt = getDoubleInfo(GRB_CB_MIP_NODCNT);
    if (nodecnt - lastnode >= 100) {
      lastnode = nodecnt;
      ...
      cout << nodecnt << " " << ...;
    }

    double nodecnt = GetDoubleInfo(GRB.Callback.MIP_NODCNT);
    if (nodecnt - lastnode >= 100) {
      lastnode = nodecnt;
      ...
      Console.WriteLine(nodecnt + " " ...);
    }

    double nodecnt = getDoubleInfo(GRB.CB_MIP_NODCNT);
    if (nodecnt - lastnode >= 100) {
      lastnode = nodecnt;
      ...
      System.out.println(nodecnt + " " ...);
    }

    nodecnt = model.cbGet(GRB.Callback.MIP_NODCNT)
    if nodecnt % 100 == 0:
        print(f"{nodecnt} ...")

    Dim nodecnt As Double = GetDoubleInfo(GRB.Callback.MIP_NODCNT)
    If nodecnt - lastnode >= 100 Then
        lastnode = nodecnt
        ...
        Console.WriteLine(nodecnt & " "  ...)
    End If

Help and Feedback

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