I am new to LP and Clp, but I came up with the following code (with help of the documentation and the examples). I have some strange behaviour in my code, I would like to know why and how I can improve my code so it actually works.

```
/* First try to use the Clp library. I use the following example:
# short example
maximize obj: 0.6 * x1 + 0.5 * x2;
s.t. c_1: x1 + 2 * x2 <= 1;
s.t. c_2: 3 * x1 + x2 <= 2;
# solution
x_1 = 0.6
x_2 = 0.2
*/
#include "ClpSimplex.hpp"
#include "CoinBuild.hpp"
#include "CoinModel.hpp"
#include < iostream>
using namespace std;
int main(int argc, const char *argv[]) {
ClpSimplex model; // child of ClpModel
int n_cols = 2;
model.resize(0,2);
// define objective coefficients
// define upper- and lowerbounds
double objec[] = { -0.6, -0.5}; // I used minus because I want to maximize, is this correct?
double lower[] = {-10.0, -10.0};
double upper[] = { 10.0, 10.0};
// set objective coefficients
// set upper- and lowerbounds
for(int i = 0; i < n_cols; i++) {
model.setObjectiveCoefficient(i, objec[i]);
model.setColumnLower(i, lower[i]);
model.setColumnUpper(i, upper[i]);
}
// define coefficients of the constraints
int cols_1[] = {0, 1};
int cols_2[] = {0, 1};
double c_1[] = {1.0, 2.0};
double c_2[] = {3.0, 1.0};
// set coefficients of the constraints
// some strange behaviour when I swap the 1 and 2 (i.e. I get different results this shouldn't happen, should it?
model.addRow(0, cols_1, c_1, -100.0, 1.0);
model.addRow(1, cols_2, c_2, -100.0, 2.0);
// solve
// model.initialSolve();
// model.dual();
model.primal(); // which should I use?
// get solution
int numberCols = model.numberColumns();
const double* sol1 = model.primalColumnSolution();
const double* sol2 = model.dualColumnSolution();
// print solution
cout << endl << "### SOLUTION ###" << endl;
cout << "status = " << model.status() << endl;
cout << "2nd-status = " << model.secondaryStatus() << endl;
for(int i = 0; i < numberCols; i++) {
cout << "x_" << (i+1) << " = " << sol1[i] << "\t\t";
cout << "x_" << (i+1) << " = " << sol2[i] << endl;
}
cout << endl;
// some checks
if(model.isAbandoned())
cout << "Numerical problems found" << endl;
if(model.isProvenPrimalInfeasible())
cout << "Primal Infeasible" << endl;
if(model.isProvenDualInfeasible())
cout << "Dual Infeasible" << endl;
return 0;
}
```

The result is the following:

```
Clp0006I 0 Obj 11 Dual inf 1000000.6 (2)
Clp0006I 2 Obj -5.4
Clp0000I Optimal - objective value -5.4
### SOLUTION ###
status = 0
2nd-status = 0
x_1 = 0.666667 x_1 = 0
x_2 = 10 x_2 = -0.5
```

**Edit**

Oke i have changed the code ( `addRow(1,...)`

and `addRow(2,...)`

have become `addRow(0,...)`

and `addRow(1,...)`

. But still the solution given by Clp is infeasible (primal) and not optimal (dual). And still swapping the two rows give different solutions (both not the one I am looking for)

asked
**19 May '14, 05:45**

martijnn2008

32●6

accept rate:
0%

a) Switching the position of the two rows, should give you same solution in terms of objective, but may or may not end up in the same solution in terms of variable values. There often exists many optimal solutions. b) You can use either dual or primal both should give you an optimal solution. c) I don't think you have to explicitly negate the objective to get a max problem, probably clp has a setting for it.

But the solution is not the optimal solution, because it violates the constraint

`3 * x1 + x2 <= 2;`

. And in this simple example there is only one solution, isn't it or maybe I understand LP wrong?Obviously the solution reported by CLP is infeasible to the original problem. I quickly looked at the code, but can't spot the error.

Try to print out the model before solving it, you can print to a .lp file an see what CLP has stored internally.

Now I see it, you need to use index starting at zero :model.addRow(1, cols_2, c_2, -100.0, 2.0);

You mean I have to do:

`addRow(0,....)`

and`addRow(1,....)`

I have some difficulty to print it to an .lp file I can print it to MPS, but that is less human readibleYes to 0 and 1.

Ok I changed the code, but still didn't gave me the solution I am looking for (see edit)

My bad, take a look here https://projects.coin-or.org/Clp/browser/trunk/Clp/examples/addRows.cpp?rev=755. Then you will see first argument is number of non zeros of the row and it will then automatically append the new constraint. You then need to use 2 and 2, since both rows has 2 non zeros.

Thanks very much, maybe post your last comment as answer and get the karma you deserve :)