# 7.12 Retrieving infeasibility certificates¶

When a continuous problem is declared as primal or dual infeasible, **MOSEK** provides a Farkas-type infeasibility certificate. If, as it happens in many cases, the problem is infeasible due to an unintended mistake in the formulation or because some individual constraint is too tight, then it is likely that infeasibility can be isolated to a few linear constraints/bounds that mutually contradict each other. In this case it is easy to identify the source of infeasibility. The tutorial in Sec. 9.3 (Debugging infeasibility) has instructions on how to deal with this situation and debug it **by hand**. We recommend Sec. 9.3 (Debugging infeasibility) as an introduction to infeasibility certificates and how to deal with infeasibilities in general.

Some users, however, would prefer to obtain the infeasibility certificate using Fusion API for C++, for example in order to repair the issue automatically, display the information to the user, or perhaps simply because the infeasibility was one of the intended outcomes that should be analyzed in the code.

In this tutorial we show how to obtain such an infeasibility certificate with Fusion API for C++ in the most typical case, that is when the linear part of a problem is primal infeasible. A Farkas-type primal infeasibility certificate consists of the dual values of linear constraints and bounds. Each of the dual values (multipliers) indicates that a certain multiple of the corresponding constraint should be taken into account when forming the collection of mutually contradictory equalities/inequalities.

## 7.12.1 Example PFEAS¶

For the purpose of this tutorial we use the same example as in Sec. 9.3 (Debugging infeasibility), that is the primal infeasible problem

Creating the model

In order to fetch the infeasibility certificate we must have access to the objects representing both variables and constraints after optimization. We will implement the problem as having two linear constraints `s`

and `d`

of dimensions 3 and 4, respectively.

```
// Construct the sample model from the example in the manual
auto sMat = Matrix::sparse(3, 7, new_array_ptr<int,1>({0,0,1,1,2,2,2}),
new_array_ptr<int,1>({0,1,2,3,4,5,6}),
new_array_ptr<double,1>({1,1,1,1,1,1,1}));
auto sBound = new_array_ptr<double,1>({200, 1000, 1000});
auto dMat = Matrix::sparse(4, 7, new_array_ptr<int,1>({0,0,1,2,2,3,3}),
new_array_ptr<int,1>({0,4,1,2,5,3,6}),
new_array_ptr<double,1>({1,1,1,1,1,1,1}));
auto dBound = new_array_ptr<double,1>({1100, 200, 500, 500});
auto c = new_array_ptr<double,1>({1, 2, 5, 2, 1, 2, 1});
Model::t M = new Model("pinfeas"); auto _M = finally([&]() { M->dispose(); });
Variable::t x = M->variable("x", 7, Domain::greaterThan(0));
Constraint::t s = M->constraint("s", Expr::mul(sMat, x), Domain::lessThan(sBound));
Constraint::t d = M->constraint("d", Expr::mul(dMat, x), Domain::equalsTo(dBound));
M->objective(ObjectiveSense::Minimize, Expr::dot(c,x));
```

Checking infeasible status and adjusting settings

After the model has been solved we check that it is indeed infeasible. If yes, then we choose a threshold for when a certificate value is considered as an important contributor to infeasibility (ideally we would like to list all nonzero duals, but just like an optimal solution, an infeasibility certificate is also subject to floating-point rounding errors). Finally, we declare that we are interested in retrieving certificates and not just optimal solutions by calling `Model.acceptedSolutionStatus`

, see Sec. 8.1.4 (Retrieving solution values). All these steps are demonstrated in the snippet below:

```
// Check problem status
if (M->getProblemStatus() == ProblemStatus::PrimalInfeasible) {
// Set the tolerance at which we consider a dual value as essential
double eps = 1e-7;
// We want to retrieve infeasibility certificates
M->acceptedSolutionStatus(AccSolutionStatus::Certificate);
```

Going through the certificate for a single item

We can define a fairly generic function which takes an array of dual values and all other required data and prints out the positions of those entries whose dual values exceed the given threshold. These are precisely the values we are interested in:

```
//Analyzes and prints infeasibility certificate for a single object,
//which can be a variable or constraint
static void analyzeCertificate(std::string name, // name of the analyzed object
long size, // size of the object
std::shared_ptr<ndarray<double, 1>> duals, // actual dual values
double eps) // tolerance determining when a dual value is considered important
{
for(int i = 0; i < size; i++) {
if (abs((*duals)[i]) > eps)
std::cout << name << "[" << i << "], dual = " << (*duals)[i] << std::endl;
}
}
```

Full source code

All that remains is to call this function for all variable and constraint bounds for which we want to know their contribution to infeasibility. Putting all these pieces together we obtain the following full code:

```
#include <iostream>
#include "fusion.h"
using namespace mosek::fusion;
using namespace monty;
//Analyzes and prints infeasibility certificate for a single object,
//which can be a variable or constraint
static void analyzeCertificate(std::string name, // name of the analyzed object
long size, // size of the object
std::shared_ptr<ndarray<double, 1>> duals, // actual dual values
double eps) // tolerance determining when a dual value is considered important
{
for(int i = 0; i < size; i++) {
if (abs((*duals)[i]) > eps)
std::cout << name << "[" << i << "], dual = " << (*duals)[i] << std::endl;
}
}
int main(int argc, char ** argv)
{
// Construct the sample model from the example in the manual
auto sMat = Matrix::sparse(3, 7, new_array_ptr<int,1>({0,0,1,1,2,2,2}),
new_array_ptr<int,1>({0,1,2,3,4,5,6}),
new_array_ptr<double,1>({1,1,1,1,1,1,1}));
auto sBound = new_array_ptr<double,1>({200, 1000, 1000});
auto dMat = Matrix::sparse(4, 7, new_array_ptr<int,1>({0,0,1,2,2,3,3}),
new_array_ptr<int,1>({0,4,1,2,5,3,6}),
new_array_ptr<double,1>({1,1,1,1,1,1,1}));
auto dBound = new_array_ptr<double,1>({1100, 200, 500, 500});
auto c = new_array_ptr<double,1>({1, 2, 5, 2, 1, 2, 1});
Model::t M = new Model("pinfeas"); auto _M = finally([&]() { M->dispose(); });
Variable::t x = M->variable("x", 7, Domain::greaterThan(0));
Constraint::t s = M->constraint("s", Expr::mul(sMat, x), Domain::lessThan(sBound));
Constraint::t d = M->constraint("d", Expr::mul(dMat, x), Domain::equalsTo(dBound));
M->objective(ObjectiveSense::Minimize, Expr::dot(c,x));
// Useful for debugging
M->writeTask("pinfeas.ptf");
M->setLogHandler([ = ](const std::string & msg) { std::cout << msg << std::flush; } );
// Solve the problem
M->solve();
// Check problem status
if (M->getProblemStatus() == ProblemStatus::PrimalInfeasible) {
// Set the tolerance at which we consider a dual value as essential
double eps = 1e-7;
// We want to retrieve infeasibility certificates
M->acceptedSolutionStatus(AccSolutionStatus::Certificate);
// Go through variable bounds
std::cout << "Variable bounds important for infeasibility: " << std::endl;
analyzeCertificate("x", x->getSize(), x->dual(), eps);
// Go through constraint bounds
std::cout << "Constraint bounds important for infeasibility: " << std::endl;
analyzeCertificate("s", s->getSize(), s->dual(), eps);
analyzeCertificate("d", d->getSize(), d->dual(), eps);
}
else {
std::cout << "The problem is not primal infeasible, no certificate to show" << std::endl;
}
}
```

Running this code will produce the following output:

```
Variable bounds important for infeasibility:
x[5], dual = 1.0
x[6], dual = 1.0
Constraint bounds important for infeasibility:
s[0], dual = -1.0
s[2], dual = -1.0
d[0], dual = 1.0
d[1], dual = 1.0
```

indicating the positions of bounds which appear in the infeasibility certificate with nonzero values.