Search code examples
c++lpsolve

LPSolve C++ source library and IDE gives different LP task solution results


I have LP task. The problem has appeared suddenly. I use automated code for create the objective function, equations and constrains, from two vectors of nodes and lines, also I use the method for create code of my tas in LPSolve format and write it in file.
For example:

/* Objective function */
max:  +y4 +y5 +y6;

/* Constraints */
n1: +x4 -y4 -z4_5_4 -z4_6_5 +z5_4_8 +z6_4_9 = 0;
n2: +x5 -y5 +z4_5_4 -z5_6_6 +z6_5_7 -z5_4_8 +z6_5_10 -z5_6_11 = 0;
n3: +x6 -y6 +z4_6_5 +z5_6_6 -z6_5_7 -z6_4_9 -z6_5_10 +z5_6_11 = 0;

/* Variable bounds */
x4 <= 16210;
x5 <= 16600;
x6 <= 17950;
y4 <= 15000;
y5 <= 15000;
y6 <= 15000;
z4_5_4 <= 2000;
z4_6_5 <= 2000;
z5_6_6 <= 1000;
z6_5_7 <= 1000;
z5_4_8 <= 2000;
z6_4_9 <= 2000;
z6_5_10 <= 1000;
z5_6_11 <= 1000;

After creating and writing equations in file I execute the solver:

int result = solve(lp);

Usualy for tasks with random values, I've got results like this:

Var-s    Result
         45000
x4       15000
x5       15000
x6       15000
y4       15000
y5       15000
y6       15000
z4_5_4   0
z4_6_5   0
z5_4_8   0
z5_6_11  0
z5_6_6   0
z6_4_9   0
z6_5_10  0
z6_5_7   0

But in the LPSolve IDE I've got results like this (and I think it's rigth results):

Var-s    Result
         45000
x4       16210
x5       16600
x6       12190
y4       15000
y5       15000
y6       15000
z4_5_4   400
z4_6_5   810
z5_4_8   0
z5_6_11  1000
z5_6_6   1000
z6_4_9   0
z6_5_10  0
z6_5_7   0

On some forum I read that's better to use "1E-5" instead "0" and "1000" instead "1.0" as coefficients in my objective function. I tried to do this and i've got the same results in solver IDE and in my programm but the results was not changed for randome values in the task.
So, I need to get results like in LPSolve IDE (wroten in the end) but use my programm. If it's possible, please help me.
Used versions LPSolve v-5.5.2.3, v-5.5.2.0


Solution

  • So, I had conversation with some guy, which used LPSolve many time and knows some pitfalls. In my question I have equations and constraints, if we replace x,y,z unknown values to the resulting solution, the equations will satisfied.

    /* Constraints */
    n1: +x4 -y4 -z4_5_4 -z4_6_5 +z5_4_8 +z6_4_9 = 0;
    n2: +x5 -y5 +z4_5_4 -z5_6_6 +z6_5_7 -z5_4_8 +z6_5_10 -z5_6_11 = 0;
    n3: +x6 -y6 +z4_6_5 +z5_6_6 -z6_5_7 -z6_4_9 -z6_5_10 +z5_6_11 = 0;
    
    /* satisfied equations */
    n1: +15000 -15000 -0 -0 +0 +0 = 0;
    n2: +15000 -15000 +0 -0 +0 -0 +0 -0 = 0;
    n3: +15000 -15000 +0 +0 -0 -0 -0 +0 = 0;
    

    LPSolve will find the optimal solution and will write several different solutions in IDE, programm etc. In this situation when Y is load, X is generation, Z is flows and when less load generation, we needn't flows to balancing system and in programm I have solution like in example, but In IDE have right solution too.

    So, before you will think that solver write wrong solution you need to check the model, objective function, equations and constrains, and try to replace result values with constrains to get satisfied equations or try to solve the dual problem.

    Next time I try to find special constrians in objective function. May be it can help somebody.