8.2 Errors and exceptions

Exceptions

Almost every method in Fusion API for .NET 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:

  • incompatible dimensions in a linear expression,

  • 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 FusionException and its specific subclasses. The one case where it is extremely important to do so is when Model.Solve is invoked. We will say more about this in Sec. 8.1 (Accessing the solution).

The exception contains a short diagnostic message. They can be accessed as in the following example.

             try {
               M.SetSolverParam("intpntCoTolRelGap", 1.01);
             } catch (mosek.fusion.ParameterError e) {
               Console.WriteLine("Error: {0}", e.ToString());
             }

It will produce as output:

Error: Invalid value for parameter (intpntCoTolRelGap)

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. 8.3 (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).

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 8.2 Sample framework for checking optimization result. Click here to download.
    public static void Main(String[] argv) {
      Model M = new Model();

      // (Optional) set a log stream
      // M.SetLogHandler(Console.Out);
      
      // (Optional) uncomment to see what happens when solution status is unknown
      // M.SetSolverParam("intpntMaxIterations", 1);

      // In this example we set up a small conic problem
      SetupExample(M);

      // Optimize
      try 
      {
        M.Solve();

        // We expect solution status OPTIMAL (this is also default)
        M.AcceptedSolutionStatus(AccSolutionStatus.Optimal);

        Variable x = M.GetVariable("x");
        long xsize = x.GetSize();
        double[] xVal = x.Level();
        Console.Write("Optimal value of x = ");
        for(int i = 0; i < xsize; ++i)
          Console.Write(xVal[i] + " ");
        Console.WriteLine("\nOptimal primal objective: " + M.PrimalObjValue());
        // .. continue analyzing the solution

      }
      catch (OptimizeError e)
      {
        Console.WriteLine("Optimization failed. Error: " + e.ToString());
      }
      catch (SolutionError)
      {
        // The solution with at least the expected status was not available.
        // We try to diagnoze why.
        Console.WriteLine("Requested solution was not available.");
        ProblemStatus prosta = M.GetProblemStatus();
        switch(prosta)
        {
          case ProblemStatus.DualInfeasible:
            Console.WriteLine("Dual infeasibility certificate found.");
            break;

          case ProblemStatus.PrimalInfeasible:
            Console.WriteLine("Primal infeasibility certificate found.");
            break;

          case ProblemStatus.Unknown:
            // The solutions status is unknown. The termination code
            // indicates why the optimizer terminated prematurely.
            Console.WriteLine("The solution status is unknown.");
            StringBuilder symname = new StringBuilder();
            StringBuilder desc = new StringBuilder();
            Env.getcodedesc((rescode)M.GetSolverIntInfo("optimizeResponse"), symname, desc);
            Console.WriteLine("  Termination code: {0} {1}", symname, desc);
            break;

          default:
            Console.WriteLine("Another unexpected problem status: " + prosta);
            break;
        }
      }
      catch (Exception e)
      {
        Console.WriteLine("Unexpected error: " + e.ToString());
      }

      M.Dispose();
    }