8.6 Progress and data callback

Callbacks are a very useful mechanism that allow the caller to track the progress of the MOSEK optimizer. A callback function provided by the user is regularly called during the optimization and can be used to

  • obtain a customized log of the solver execution,
  • collect information for debugging purposes or
  • ask the solver to terminate.

Fusion API for Matlab has the following callback mechanisms:

  • progress callback, which provides only the basic status of the solver.
  • data callback, which provides the solver status and a complete set of information items that describe the progress of the optimizer in detail.

Warning

The callbacks functions must not invoke any functions of the solver, environment or task. Otherwise the state of the solver and its outcome are undefined.

8.6.1 Data callback

In the data callback MOSEK passes a callback code and values of all information items to a user-defined function. The callback function is called, in particular, at the beginning of each iteration of the interior-point optimizer. For the simplex optimizers logSimFreq controls how frequently the call-back is called. Note that the callback is done quite frequently, which can lead to degraded performance. If the information items are not required, the simpler progress callback may be a better choice.

The data callback is set by calling the method Model.setDataCallbackHandler.

The callback function must be implemented in Java by extending the abstract class mosek.DataCallback and implementing the method

public int callback(mosek.callbackcode      caller,
                    double[]                douinf,
                    int[]                   intinf,
                    long[]                 lintinf)

Arguments:

  • caller - the status of the optimizer.
  • douinf - values of double information items.
  • intinf - values of integer information items.
  • lintinf - values of long information items.

Return value: Non-zero return value of the callback function indicates that the optimizer should be terminated.

8.6.2 Progress callback

In the progress callback MOSEK provides a single code indicating the current stage of the optimization process.

The callback is set by calling the method Model.setCallbackHandler.

The callback function must be implemented in Java by extending the abstract class mosek.Progress and implementing the method

public int progress(mosek.callbackcode caller)

Arguments:

  • caller - the status of the optimizer.

Return value: Non-zero return value of the callback function indicates that the optimizer should be terminated.

8.6.3 Working example: Data callback

The following example defines a data callback function that prints out some of the information items. It interrupts the solver after a certain time limit.

Note that the time limit refers to time spent in the solver and does not include setting up the model in Fusion.

Listing 8.3 An example of a data callback class and a function accessing information items. Click here to download.
public class MatlabCallback extends mosek.DataCallback
{
  
  private double maxtime;
  private Model M;

  public MatlabCallback(Model M_, double maxtime_) {
    M = M_;
    maxtime = maxtime_;
  }

  public int callback(callbackcode caller,
                      double[]     douinf,
                      int[]        intinf,
                      long[]       lintinf)
  {

    double opttime = 0.0;
    int itrn;
    double pobj, dobj, stime;

    Formatter f = new Formatter(System.out);
    switch (caller)
    {
      case begin_intpnt:
          f.format("Starting interior-point optimizer\n");
          break;
      case intpnt:
          itrn    = intinf[iinfitem.intpnt_iter.value      ];
          pobj    = douinf[dinfitem.intpnt_primal_obj.value];
          dobj    = douinf[dinfitem.intpnt_dual_obj.value  ];
          stime   = douinf[dinfitem.intpnt_time.value      ];
          opttime = douinf[dinfitem.optimizer_time.value   ];

          f.format("Iterations: %-3d\n",itrn);
          f.format("  Elapsed time: %6.2f(%.2f)\n",opttime,stime);
          f.format("  Primal obj.: %-18.6e  Dual obj.: %-18.6e\n",pobj,dobj);
          break;
      case end_intpnt:
          f.format("Interior-point optimizer finished.\n");
          break;
      case begin_primal_simplex:
          f.format("Primal simplex optimizer started.\n");
          break;
      case update_primal_simplex:
          itrn    = intinf[iinfitem.sim_primal_iter.value  ];
          pobj    = douinf[dinfitem.sim_obj.value          ];
          stime   = douinf[dinfitem.sim_time.value         ];
          opttime = douinf[dinfitem.optimizer_time.value   ];
    
          f.format("Iterations: %-3d\n", itrn);
          f.format("  Elapsed time: %6.2f(%.2f)\n",opttime,stime);
          f.format("  Obj.: %-18.6e\n", pobj );
          break;
      case end_primal_simplex:
          f.format("Primal simplex optimizer finished.\n");
          break;
      case begin_dual_simplex:
          f.format("Dual simplex optimizer started.\n");
          break;
      case update_dual_simplex:
          itrn    = intinf[iinfitem.sim_dual_iter.value    ];
          pobj    = douinf[dinfitem.sim_obj.value          ];
          stime   = douinf[dinfitem.sim_time.value         ];
          opttime = douinf[dinfitem.optimizer_time.value   ];
          f.format("Iterations: %-3d\n", itrn);
          f.format("  Elapsed time: %6.2f(%.2f)\n",opttime,stime);
          f.format("  Obj.: %-18.6e\n", pobj);
          break;
      case end_dual_simplex:
          f.format("Dual simplex optimizer finished.\n");
          break;
      case begin_bi:
          f.format("Basis identification started.\n");
          break;
      case end_bi:
          f.format("Basis identification finished.\n");
          break;
      default:
    }
    System.out.flush();
    if (opttime >= maxtime)
    {
      f.format("MOSEK is spending too much time. Terminate it.\n");
      System.out.flush();
      return 1;
    }
    return 0;
  }
}

Assuming that we have defined a model M and a time limit maxtime, and that the compiled class MatlabCallback is available under MATLAB‘s Java class path, the callback function is attached as follows:

Listing 8.4 Attaching the data callback function to the model. Click here to download.
    userCallback = com.mosek.fusion.examples.MatlabCallback(M, maxtime);
    M.setDataCallbackHandler( userCallback );