7.3 Errors and exceptions

Exceptions

Almost every function in Optimizer API for Rust 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 errors. 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 error is contains a response code (element of the enum Rescode) and short diagnostic messages. They can be accessed as in the following example.

if let Err(e) = task.put_dou_param(Dparam::INTPNT_CO_TOL_REL_GAP, -1.0e-7) {
    println!("{}", e);
}

It will produce as output:

Error in call to put_dou_param: (1216) "The parameter value -1e-07 is too small for parameter 'MSK_DPAR_INTPNT_CO_TOL_REL_GAP'.\0"

Another way to obtain a human-readable string corresponding to a response code is the method Env.get_code_desc. 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.
extern crate mosek;

use mosek::{Task,Streamtype,Solsta,Soltype};
use std::env;

const CQO1_PTF : &str = "Task 'CQO1 EXAMPLE'
Objective obj
    Minimize + x4 + x5 + x6
Constraints
    c1 [1] + x1 + x2 + 2 x3
Variables
    k1 [QUAD(3)]
        x4
        x1 [0;+inf]
        x2 [0;+inf]
    k2 [RQUAD(3)]
        x5
        x6
        x3 [0;+inf]
";


fn main() -> Result<(),String> {
    let args: Vec<String> = env::args().collect();

    let mut task = Task::new().unwrap().with_callbacks();
    if args.len() < 2 {
        task.read_ptf_string(CQO1_PTF)?;
    }
    else {
        task.read_data(args[1].as_str())?;
    }

    // Perform optimization.
    let trm = task.optimize()?;
    task.solution_summary(Streamtype::LOG)?;

    // Handle solution status. We expect Optimal
    let solsta = task.get_sol_sta(Soltype::ITR)?;

    match solsta {
        Solsta::OPTIMAL => {
            // Fetch and print the solution
            println!("An optimal interior point solution is located.");
            let numvar = task.get_num_var()?;
            let mut xx = vec![0.0; numvar as usize];
            task.get_xx(Soltype::ITR, xx.as_mut_slice())?;
            println!("xx = {:?}",xx)
        },
        Solsta::DUAL_INFEAS_CER =>
          println!("Dual infeasibility certificate found."),
        Solsta::PRIM_INFEAS_CER =>
          println!("Primal infeasibility certificate found."),
        Solsta::UNKNOWN => {
          // The solutions status is unknown. The termination code
          // indicates why the optimizer terminated prematurely.
          println!("The solution status is unknown.");
          let (symname,desc) = mosek::get_code_desc(trm)?;
          println!("   Termination code: {} {}\n", symname, desc)
        },
        _ =>
          println!("Unexpected solution status {}\n",solsta)
    }
    Ok(())
}