# 6.7 Optimizer Termination Handling¶

After solving an optimization problem with **MOSEK** an approriate action must be taken depending on the outcome. Usually the expected outcome is an optimal solution, but there may be several situations where this is not the result. E.g., if the problem is infeasible or nearly so or if the solver ran out of memory or stalled while optimizing, the result may not be as expected.

This section discusses what should be considered when an optimization has ended unsuccessfully.

Before continuing, let us consider the four status codes available in **MOSEK** that is relevant for the error handing:

**Termination code**: It provides information about why the optimizer terminated. For instance if a time limit has been specfied (this is common for mixed integer problems), the termination code will tell if this termination limit was the cause of the termination. Note that reaching a prespecfied time limit is not considered an exceptional case. It must be expected that this occurs occasionally.**Response code**: It is an information about the system status and the outcome of the call to a**MOSEK**functionalities. This code is used to report the unexpected failures such as out of space.

The response code is the returned value of most functions of the API, and its type is `rescode`

. See 17.5 for a list of possible return codes.

**Solution status**: It contains information about the status of the solution, e.g., whether the solution is optimal or a certificate of infeasibility.**Problem status**: It describes what**MOSEK**knows about the feasibility of the problem, i.e., if the is problem feasible or infeasible.

The problem status is mostly used for integer problems. For continuous problems a problem status of, say, *infeasible* will always mean that the solution is a certificate of infeasibility. For integer problems it is not possible to provide a certificate, and thus a separate problem status is useful.

Note that if we want to report, e.g., that the optimizer terminated due to a time limit or because it stalled but with a feasible solution, we have to consider *both* the termination code, *and* the solution status.

The following pseudo code demonstrates a best practice way of dealing with the status codes.

`if ( the solution status is as expected )`

**The normal case**:Do whatever that was planned. Note the response code is ignored because the solution has the expected status. Of course we may check the response anyway if we like.

`else`

**Exceptional case**:Based on solution status, response and termination codes take appropriate action.

In Listing 6.11 the pseudo code is implemented. The idea of the example is to read an optimization problem from a file, e.g., an MPS file and optimize it. Based on status codes an appropriate action is taken, which in this case is to print a suitable message.

```
function response(inputfile, solfile)
cmd = sprintf('read(%s)', inputfile)
% Read the problem from file
[r, res] = mosekopt(cmd)
if strcmp( res.rcodestr , 'MSK_RES_OK')
% Perform the optimization.
[r,res] = mosekopt('minimize', res.prob);
r
res
%Expected result: The solution status of the basic solution is optimal.
if strcmp(res.rcodestr, 'MSK_RES_OK')
solsta = strcat('MSK_SOL_STA_', res.sol.itr.solsta)
if strcmp( solsta , 'MSK_SOL_STA_OPTIMAL') || ...
strcmp( solsta , 'MSK_SOL_STA_NEAR_OPTIMAL')
fprintf('An optimal basic solution is located.');
elseif strcmp( solsta , 'MSK_SOL_STA_DUAL_INFEAS_CER') || ...
strcmp( solsta , 'MSK_SOL_STA_NEAR_DUAL_INFEAS_CER')
fprintf('Dual infeasibility certificate found.');
elseif strcmp( solsta , 'MSK_SOL_STA_PRIM_INFEAS_CER') || ...
strcmp( soslta , 'MSK_SOL_STA_NEAR_PRIM_INFEAS_CER')
fprintf('Primal infeasibility certificate found.');
elseif strcmp( solsta , 'MSK_SOL_STA_UNKNOWN')
% The solutions status is unknown. The termination code
% indicates why the optimizer terminated prematurely.
fprintf('The solution status is unknown.');
if ~strcmp(res.rcodestr, 'MSK_RES_OK' )
% A system failure e.g. out of space.
fprintf(' Response code: %s\n', res);
else
%No system failure e.g. an iteration limit is reached.
printf(' Termination code: %s\n', res);
end
else
fprintf('An unexpected solution status is obtained.');
end
else
fprintf('Could not obtain the solution status for the requested solution.');
end
fprintf('Return code: %d (0 means no error occurred.)\n',r);
end
```