7.3 Errors and exceptions

Exceptions

Almost every function in Optimizer API for Julia 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 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.

try
    putdouparam(task,MSK_DPAR_INTPNT_CO_TOL_REL_GAP, -1.0e-7)
catch e
    println("Response code $(e.rcode)")
    println("Message       $(e.msg)")
end

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 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 MSK_IPAR_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.
using Mosek


cqo1_ptf = "
Task ''
Objective obj
    Minimize + x4 + x5 + x6
Constraints
    c1 [1] + x1 + x2 + 2 x3
    k1 [QUAD(3)]
        @ac1: + x4
        @ac2: + x1
        @ac3: + x2
    k2 [RQUAD(3)]
        @ac4: + x5
        @ac5: + x6
        @ac6: + x3
Variables
    x4
    x1 [0;+inf]
    x2 [0;+inf]
    x5
    x6
    x3 [0;+inf]
"

try
    maketask() do task
        # Use remote server: putoptserverhost(task,"http://solve.mosek.com:30080")
        putstreamfunc(task,MSK_STREAM_LOG,msg -> print(msg))

        if length(ARGS) < 1
            readptfstring(task,cqo1_ptf)
        else
            readdata(task,ARGS[1])
        end

        # (Optional) uncomment to see what happens when solution status is unknown
        # putintparam(task,MSK_IPAR_INTPNT_MAX_ITERATIONS, 1)


        # Optimize
        trmcode = optimize(task)
        solutionsummary(task,MSK_STREAM_LOG)

        # We expect solution status OPTIMAL
        solsta = getsolsta(task,MSK_SOL_ITR)

        if solsta == MSK_SOL_STA_OPTIMAL
            # Optimal solution. Fetch and print it.
            println("An optimal interior-point solution is located.")
            numvar = getnumvar(task)
            xx = getxx(task,MSK_SOL_ITR)
            println("x = $xx")
        elseif solsta == MSK_SOL_STA_DUAL_INFEAS_CER
            println("Dual infeasibility certificate found.")
        elseif solsta == MSK_SOL_STA_PRIM_INFEAS_CER
            println("Primal infeasibility certificate found.")
        elseif solsta == MSK_SOL_STA_UNKNOWN
            # The solutions status is unknown. The termination code
            # indicates why the optimizer terminated prematurely.
            println("The solution status is unknown.")
            (symname, desc) = getcodedesc(trmcode)
            println("   Termination code: $symname $desc")
        else
            println("An unexpected solution status $solsta is obtained.")
        end
    end
catch e
    println("En error occurred: $(e.rcode), $(e.msg)")
end