7.3 Errors and exceptions

Exceptions

Almost every function in Optimizer API for Java can throw an exception informing that the requested operation was not performed correctly, and indicating the type of error that occurred. This is the case in situations such as for instance:

  • referencing a nonexisting variable (for example with too large index),

  • defining an invalid value for a parameter,

  • accessing an undefined solution,

  • repeating a variable name, etc.

It is therefore a good idea to catch exceptions of type Error. The one case where it is extremely important to do so is when Task.optimize is invoked. We will say more about this in Sec. 7.2 (Accessing the solution).

The exception contains a response code (element of the enum rescode) and short diagnostic messages. They can be accessed as in the following example.

  try {
    task.putdouparam(mosek.dparam.intpnt_co_tol_rel_gap, -1.0e-7);
  }
  catch (mosek.Exception e) {
    mosek.rescode res = e.code;
    System.out.println("Response code " + res + "\nMessage       " + e.msg);
  }

It will produce as output:

Response code rescode.err_param_is_too_small
Message       The parameter value -1e-07 is too small for parameter 'MSK_DPAR_INTPNT_CO_TOL_REL_GAP'.

Another way to obtain a human-readable string corresponding to a response code is the method Env.getcodedesc. A full list of exceptions, as well as response codes, can be found in the API reference.

Optimizer errors and warnings

The optimizer may also produce warning messages. They indicate non-critical but important events, that will not prevent solver execution, but may be an indication that something in the optimization problem might be improved. Warning messages are normally printed to a log stream (see Sec. 7.4 (Input/Output)). A typical warning is, for example:

MOSEK warning 53: A numerically large upper bound value  6.6e+09 is specified for constraint 'C69200' (46020).

Warnings can also be suppressed by setting the iparam.max_num_warnings parameter to zero, if they are well-understood.

Error and solution status handling example

Below is a source code example with a simple framework for handling major errors when assessing and retrieving the solution to a conic optimization problem.

Listing 7.2 Sample framework for checking optimization result. Click here to download.
package com.mosek.example;
import mosek.*;

public class response {
  public static void main(String[] argv) {
    StringBuffer symname = new StringBuffer();
    StringBuffer desc = new StringBuffer();

    String filename;
    if (argv.length >=1) filename = argv[0];
    else                 filename = "../data/cqo1.mps";

    // Create the task and environment
    try (Env  env  = new Env();
         Task task = new Task(env, 0, 0)) {
      // (Optionally) attach the log handler to receive log information

      // (Optionally) uncomment this line to experience solution status Unknown
      // task.putintparam(iparam.intpnt_max_iterations, 1);

      // On this example we read an optimization problem from a file
      task.readdata(filename);

      // Perform optimization.
      rescode trm = task.optimize();
      task.solutionsummary(streamtype.log);

      // Handle solution status. We expect Optimal
      solsta solsta = task.getsolsta(soltype.itr);

      switch ( solsta ) {
        case optimal:
          // Fetch and print the solution
          System.out.println("An optimal interior point solution is located.");
          int numvar = task.getnumvar();
          double[] xx = new double[numvar];
          task.getxx(soltype.itr, xx);
          for(int i = 0; i < numvar; i++)
            System.out.println("x[" + i + "] = " + xx[i]);
          break;

        case dual_infeas_cer:
          System.out.println("Dual infeasibility certificate found.");
          break;

        case prim_infeas_cer:
          System.out.println("Primal infeasibility certificate found.");
          break;

        case unknown:
          // The solutions status is unknown. The termination code
          // indicates why the optimizer terminated prematurely.
          System.out.println("The solution status is unknown.");
          Env.getcodedesc(trm, symname, desc);
          System.out.printf("   Termination code: %s %s\n", symname, desc);
          break;

        default:
          System.out.println("Unexpected solution status " + solsta + "\n");
          break;
      }
    }
    catch (mosek.Error e) {
      System.out.println("Unexpected error (" + e.code + ") " + e.msg);
    }
  }
}