# 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:

 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/undefined — the solver could not solve the problem or this type of solution is not available for a given problem.

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.Unknown`

`SolutionStatus.Certificate`

Dual infeasible (unbounded)

`ProblemStatus.DualInfeasible`

`SolutionStatus.Certificate`

`SolutionStatus.Unknown`

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.Unknown`

Infeasible

`ProblemStatus.PrimalInfeasible`

`SolutionStatus.Unknown`

`SolutionStatus.Unknown`

Integer feasible point

`ProblemStatus.PrimalFeasible`

`SolutionStatus.Feasible`

`SolutionStatus.Unknown`

No conclusion

`ProblemStatus.Unknown`

`SolutionStatus.Unknown`

`SolutionStatus.Unknown`

## 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 a weaker 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.
```int main(int arc, char** argv)
{
Model::t M = new Model(); auto _M = finally([&]() { M->dispose(); });

// (Optional) set a log stream
// M->setLogHandler([](const std::string & msg) { std::cout << msg << std::flush; });

// (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);

auto x = M->getVariable("x");
auto xsize = x->getSize();
auto xVal = x->level();
std::cout << "Optimal value of x = ";
for(int i = 0; i < xsize; ++i)
std::cout << (*xVal)[i] << " ";
std::cout << "\nOptimal primal objective: " << M->primalObjValue() <<"\n";
// .. continue analyzing the solution

}
catch (const OptimizeError& e)
{
std::cout << "Optimization failed. Error: " << e.what() << "\n";
}
catch (const SolutionError& e)
{
// The solution with at least the expected status was not available.
// We try to diagnoze why.
std::cout << "Requested solution was not available.\n";
auto prosta = M->getProblemStatus();
switch(prosta)
{
case ProblemStatus::DualInfeasible:
std::cout << "Dual infeasibility certificate found.\n";
break;

case ProblemStatus::PrimalInfeasible:
std::cout << "Primal infeasibility certificate found.\n";
break;

case ProblemStatus::Unknown:
// The solutions status is unknown. The termination code
// indicates why the optimizer terminated prematurely.
std::cout << "The solution status is unknown.\n";
char symname[MSK_MAX_STR_LEN];
char desc[MSK_MAX_STR_LEN];
MSK_getcodedesc((MSKrescodee)(M->getSolverIntInfo("optimizeResponse")), symname, desc);
std::cout << "  Termination code: " << symname << " " << desc << "\n";
break;

default:
std::cout << "Another unexpected problem status: " << prosta << "\n";
}
}
catch (const std::exception& e)
{
std::cerr << "Unexpected error: " << e.what() << "\n";
}

M->dispose();
return 0;
}
```
{"ft_lang_label":"__label__en","ft_lang_prob":0.73886406,"math_prob":0.8384734,"size":7582,"snap":"2021-43-2021-49","text_gpt3_token_len":1689,"char_repetition_ratio":0.17418844,"word_repetition_ratio":0.023722628,"special_character_ratio":0.22118175,"punctuation_ratio":0.16512346,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9591576,"pos_list":[0],"im_url_duplicate_count":[null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2021-11-27T17:58:55Z\",\"WARC-Record-ID\":\"<urn:uuid:e7ac4def-9ead-4e8f-acee-0080a44f6098>\",\"Content-Length\":\"42312\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:abda4b99-5cea-4b2c-ba2c-cdc97e377d1a>\",\"WARC-Concurrent-To\":\"<urn:uuid:6ae38b1c-5578-4548-863f-d62c398cea13>\",\"WARC-IP-Address\":\"13.32.182.69\",\"WARC-Target-URI\":\"https://docs.mosek.com/latest/cxxfusion/accessing-solution.html\",\"WARC-Payload-Digest\":\"sha1:BXFEOX7Y5VRT5GRFF3EDEUYPTM6UTCKI\",\"WARC-Block-Digest\":\"sha1:LQFI3LIEJSMTMYMT3WOHNS6TNQOFSMPQ\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2021/CC-MAIN-2021-49/CC-MAIN-2021-49_segments_1637964358208.31_warc_CC-MAIN-20211127163427-20211127193427-00617.warc.gz\"}"}