8.1 Accessing the solution

This section contains important information about the status of the solver and the status of the solution, which must be checked in order to properly interpret the results of the optimization.

8.1.1 Solver termination

If an error occurs during optimization then the method Model.solve will throw an exception of type OptimizeError. The method FusionRuntimeException.toString will produce a description of the error, if available. More about exceptions in Sec. 8.2 (Errors and exceptions).

If a runtime error causes the program to crash during optimization, the first debugging step is to enable logging and check the log output. See Sec. 8.3 (Input/Output).

If the optimization completes successfully, the next step is to check the solution status, as explained below.

8.1.2 Available solutions

MOSEK uses three kinds of optimizers and provides three types of solutions:

  • basic solution from the simplex optimizer,

  • interior-point solution from the interior-point optimizer,

  • integer solution from the mixed-integer optimizer.

Under standard parameters settings the following solutions will be available for various problem types:

Table 8.1 Types of solutions available from MOSEK

Simplex optimizer

Interior-point optimizer

Mixed-integer optimizer

Linear problem

SolutionType.Basic

SolutionType.Interior

Conic (nonlinear) problem

SolutionType.Interior

Problem with integer variables

SolutionType.Integer

For linear problems the user can force a specific optimizer choice making only one of the two solutions available. For example, if the user disables basis identification, then only the interior point solution will be available for a linear problem. Numerical issues may cause one of the solutions to be unknown even if another one is feasible.

Not all components of a solution are always available. For example, there is no dual solution for integer problems and no dual conic variables from the simplex optimizer.

The user will always need to specify which solution should be accessed.

Moreover, the user may be oblivious to the actual solution type by always referring to SolutionType.Default, which will automatically select the best available solution, if there is more than one. Moreover, the method Model.selectedSolution can be used to fix one solution type for all future references.

8.1.3 Problem and solution status

Assuming that the optimization terminated without errors, the next important step is to check the problem and solution status. There is one for every type of solution, as explained above.

Problem status

Problem status (ProblemStatus, retrieved with Model.getProblemStatus) determines whether the problem is certified as feasible. Its values can roughly be divided into the following broad categories:

  • feasible — the problem is feasible. For continuous problems and when the solver is run with default parameters, the feasibility status should ideally be ProblemStatus.PrimalAndDualFeasible.

  • primal/dual infeasible — the problem is infeasible or unbounded or a combination of those. The exact problem status will indicate the type of infeasibility.

  • unknown — the solver was unable to reach a conclusion, most likely due to numerical issues.

Solution status

Solution status (SolutionStatus, retrieved with Model.getPrimalSolutionStatus and Model.getDualSolutionStatus) provides the information about what the solution values actually contain. The most important broad categories of values are:

  • optimal (SolutionStatus.Optimal) — the solution values are feasible and optimal.

  • certificate — the solution is in fact a certificate of infeasibility (primal or dual, depending on the solution).

  • unknown — the status was not determined, typically because of numerical issues, stall etc. Some solution is available, but its quality is not guaranteed.

  • undefined — this type of solution is not available at all.

The solution status determines the action to be taken. For example, in some cases a suboptimal solution may still be valuable and deserve attention. It is the user’s responsibility to check the status and quality of the solution.

Typical status reports

Here are the most typical optimization outcomes described in terms of the problem and solution statuses. Note that these do not cover all possible situations that can occur.

Table 8.2 Continuous problems (solution status for SolutionType.Interior or SolutionType.Basic)

Outcome

Problem status

Solution status (primal)

Solution status (dual)

Optimal

ProblemStatus.PrimalAndDualFeasible

SolutionStatus.Optimal

SolutionStatus.Optimal

Primal infeasible

ProblemStatus.PrimalInfeasible

SolutionStatus.Undefined

SolutionStatus.Certificate

Dual infeasible (unbounded)

ProblemStatus.DualInfeasible

SolutionStatus.Certificate

SolutionStatus.Undefined

Uncertain (stall, numerical issues, etc.)

ProblemStatus.Unknown

SolutionStatus.Unknown

SolutionStatus.Unknown

Table 8.3 Integer problems (solution status for SolutionType.Integer, others undefined)

Outcome

Problem status

Solution status (primal)

Solution status (dual)

Integer optimal

ProblemStatus.PrimalFeasible

SolutionStatus.Optimal

SolutionStatus.Undefined

Infeasible

ProblemStatus.PrimalInfeasible

SolutionStatus.Undefined

SolutionStatus.Undefined

Integer feasible point

ProblemStatus.PrimalFeasible

SolutionStatus.Feasible

SolutionStatus.Undefined

No conclusion

ProblemStatus.Unknown

SolutionStatus.Unknown

SolutionStatus.Undefined

8.1.4 Retrieving solution values

After the meaning and quality of the solution (or certificate) have been established, we can query for the actual numerical values. They can be accessed using:

Remark

By default only optimal solutions are returned. An attempt to access a solution with an incompatible status will result in an exception. This can be changed by choosing another level of acceptable solutions with the method Model.acceptedSolutionStatus. In particular, this method must be called to enable retrieving suboptimal solutions and infeasibility certificates. For instance, one could write

    M.acceptedSolutionStatus(AccSolutionStatus.Feasible)

The current setting of acceptable solutions can be checked with Model.getAcceptedSolutionStatus.

8.1.5 Source code example

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

Listing 8.1 Sample framework for checking optimization result. Click here to download.
with Model() as M:
  # (Optional) set a log stream
  # M.setLoghandler(sys.stdout)

  # (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)

    print("Optimal solution for x: {0}".format(M.getVariable('x').level()))
    print("Optimal primal objective: {0}".format(M.primalObjValue()))
    # .. continue analyzing the solution

  except OptimizeError as e:
    print("Optimization failed. Error: {0}".format(e))

  except SolutionError as e:
    # The solution with at least the expected status was not available.
    # We try to diagnoze why.
    print("Requested solution was not available.")
    prosta = M.getProblemStatus()

    if prosta == ProblemStatus.DualInfeasible:
      print("Dual infeasibility certificate found.")

    elif prosta == ProblemStatus.PrimalInfeasible:
      print("Primal infeasibility certificate found.")
          
    elif prosta == ProblemStatus.Unknown:
      # The solutions status is unknown. The termination code
      # indicates why the optimizer terminated prematurely.
      print("The solution status is unknown.")
      symname, desc = mosek.Env.getcodedesc(mosek.rescode(int(M.getSolverIntInfo("optimizeResponse"))))
      print("   Termination code: {0} {1}".format(symname, desc))

    else:
      print("Another unexpected problem status {0} is obtained.".format(prosta))

  except Exception as e:
    print("Unexpected error: {0}".format(e))