# 6.13 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. 8.3 (Debugging infeasibility) has instructions on how to deal with this situation and debug it **by hand**. We recommend Sec. 8.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 Optimizer API for Rust, 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 Optimizer API for Rust 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. The names of duals corresponding to various parts of the problem are defined in Sec. 12.1.2 (Infeasibility for Linear Optimization). 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.

## 6.13.1 Example PINFEAS¶

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

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). All these steps are demonstrated in the snippet below:

```
// Check problem status, we use the interior point solution
if task.get_pro_sta(Soltype::ITR)? == Prosta::PRIM_INFEAS {
// Set the tolerance at which we consider a dual value as essential
let eps = 1e-7;
```

Going through the certificate for a single item

We can define a fairly generic function which takes an array of lower and upper 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 contributing elements
// sl - dual values for lower bounds
// su - dual values for upper bounds
// eps - tolerance for when a nunzero dual value is significant
fn analyze_certificate(sl : &[f64], su : &[f64], eps : f64) {
for (i,(&sli,&sui)) in sl.iter().zip(su.iter()).enumerate() {
if sli > eps {
println!("#{}, lower, dual = {:e}\n", i, sli);
}
if sui > eps {
println!("#{}, upper, dual = {:e}\n", i, sui);
}
}
}
```

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:

```
extern crate mosek;
use mosek::{Streamtype,Boundkey,Soltype,Prosta};
const INF : f64 = 0.0;
fn test_problem() -> Result<mosek::Task,String> {
let mut task = mosek::Task::new().unwrap();
task.append_vars(7)?;
task.append_cons(7)?;
task.put_c_list(&[0,1,2,3,4,5,6],
&[1.0,2.0,5.0,2.0,1.0,2.0,1.0])?;
task.put_aij_list(&[0,0,1,1,2,2,2,3,3,4,5,5,6,6],
&[0,1,2,3,4,5,6,0,4,1,2,5,3,6],
&[1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0,1.0])?;
task.put_con_bound_slice(0, 7,
&[Boundkey::UP,Boundkey::UP,Boundkey::UP,Boundkey::FX,Boundkey::FX,Boundkey::FX,Boundkey::FX],
&[-INF, -INF, -INF, 1100.0, 200.0, 500.0, 500.0],
&[200.0, 1000.0, 1000.0, 1100.0, 200.0, 500.0, 500.0])?;
task.put_var_bound_slice_const(0, 7, Boundkey::UP, 0.0, INF)?;
Ok(task)
}
// Analyzes and prints infeasibility contributing elements
// sl - dual values for lower bounds
// su - dual values for upper bounds
// eps - tolerance for when a nunzero dual value is significant
fn analyze_certificate(sl : &[f64], su : &[f64], eps : f64) {
for (i,(&sli,&sui)) in sl.iter().zip(su.iter()).enumerate() {
if sli > eps {
println!("#{}, lower, dual = {:e}\n", i, sli);
}
if sui > eps {
println!("#{}, upper, dual = {:e}\n", i, sui);
}
}
}
fn main() -> Result<(),String> {
// In this example we set up a simple problem
// One could use any task or a task read from a file
let mut task = test_problem()?.with_callbacks();
let n = task.get_num_var()?;
let m = task.get_num_con()?;
// Useful for debugging
task.write_data("pinfeas.ptf")?; // Write file in human-readable format
// Attach a log stream printer to the task
task.put_stream_callback(Streamtype::LOG, |msg| print!("{}",msg))?;
// Perform the optimization.
task.optimize()?;
task.solution_summary(Streamtype::LOG)?;
// Check problem status, we use the interior point solution
if task.get_pro_sta(Soltype::ITR)? == Prosta::PRIM_INFEAS {
// Set the tolerance at which we consider a dual value as essential
let eps = 1e-7;
println!("Variable bounds important for infeasibility: ");
let mut slx = vec![0.0; n as usize]; task.get_slx(Soltype::ITR, slx.as_mut_slice())?;
let mut sux = vec![0.0; n as usize]; task.get_sux(Soltype::ITR, sux.as_mut_slice())?;
analyze_certificate(slx.as_slice(), sux.as_slice(), eps);
println!("Constraint bounds important for infeasibility: ");
let mut slc = vec![0.0; m as usize]; task.get_slc(Soltype::ITR, slc.as_mut_slice())?;
let mut suc = vec![0.0; m as usize]; task.get_suc(Soltype::ITR, suc.as_mut_slice())?;
analyze_certificate(slc.as_mut_slice(), suc.as_mut_slice(), eps);
}
else {
println!("The problem is not primal infeasible, no certificate to show");
}
Ok(())
}
```

Running this code will produce the following output:

```
Variable bounds important for infeasibility:
#6: lower, dual = 1.000000e+00
#7: lower, dual = 1.000000e+00
Constraint bounds important for infeasibility:
#1: upper, dual = 1.000000e+00
#3: upper, dual = 1.000000e+00
#4: lower, dual = 1.000000e+00
#5: lower, dual = 1.000000e+00
```

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