0
1

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's gravatar image

martijnn2008
326
accept rate: 0%

edited 19 May '14, 06:36

1

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.

(19 May '14, 05:52) Bo Jensen ♦

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?

(19 May '14, 05:57) martijnn2008

Obviously the solution reported by CLP is infeasible to the original problem. I quickly looked at the code, but can't spot the error.

(19 May '14, 05:58) Bo Jensen ♦

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

(19 May '14, 05:59) Bo Jensen ♦
2

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

(19 May '14, 06:02) Bo Jensen ♦

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 readible

(19 May '14, 06:22) martijnn2008

Yes to 0 and 1.

(19 May '14, 06:26) Bo Jensen ♦

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

(19 May '14, 06:36) martijnn2008

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.

(19 May '14, 06:40) Bo Jensen ♦

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

(19 May '14, 06:47) martijnn2008
showing 5 of 10 show 5 more comments

Take a look here. 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.

link

answered 19 May '14, 06:48

Bo%20Jensen's gravatar image

Bo Jensen ♦
5.0k2919
accept rate: 14%

edited 19 May '14, 06:53

I can't upvote so i just gave you all my reputation XD. Change the link the . in it makes the link broken.

(19 May '14, 06:52) martijnn2008
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "Title")
  • image?![alt text](/path/img.jpg "Title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Tags:

×231
×7

Asked: 19 May '14, 05:45

Seen: 1,240 times

Last updated: 19 May '14, 06:53

OR-Exchange! Your site for questions, answers, and announcements about operations research.