8.2 Errors and exceptions¶
Exceptions
Almost every method in Fusion 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:
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) {
System.out.println("Error: " + 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.
public static void main(String[] argv) {
Model M = new Model();
// (Optional) set a log stream
/*
task.set_Stream(
streamtype.log,
new mosek.Stream()
{ public void stream(String msg) { System.out.print(msg); }});
*/
// (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();
System.out.print("Optimal value of x = ");
for(int i = 0; i < xsize; ++i)
System.out.print(xVal[i] + " ");
System.out.println("\nOptimal primal objective: " + M.primalObjValue());
// .. continue analyzing the solution
}
catch (OptimizeError e)
{
System.out.println("Optimization failed. Error: " + e.toString());
}
catch (SolutionError e)
{
// The solution with at least the expected status was not available.
// We try to diagnoze why.
System.out.println("Requested solution was not available.");
ProblemStatus prosta = M.getProblemStatus();
switch(prosta)
{
case DualInfeasible:
System.out.println("Dual infeasibility certificate found.");
break;
case PrimalInfeasible:
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.");
StringBuffer symname = new StringBuffer();
StringBuffer desc = new StringBuffer();
Env.getcodedesc(rescode.fromValue(M.getSolverIntInfo("optimizeResponse")), symname, desc);
System.out.printf(" Termination code: %s %s\n", symname, desc);
break;
default:
System.out.println("Another unexpected problem status: " + prosta);
}
}
catch (java.lang.Exception e)
{
System.out.println("Unexpected error: " + e.toString());
}
M.dispose();
}