Search code examples
glpkmathprog

GLPK Variable seems to change after solve


I have been experiencing a strange behaviour of a glpsol, more precisely one of its variables. I run the command using glpsol -m sol.mod

Input, in file sol.mod:

set Points := (1..3);
var a{i in Points}, >= 0;
var x1{i in Points};
var x2{i in Points};
maximize obj: sum{i in Points} a[i];
px1: x1[1] = 0;
py1: x2[1] = 0;
px2: x1[2] = 2;
py2: x2[2] = 1;
px3: x1[3] = 3;
py3: x2[3] = 3;
p1x2: x1[1] + a[1] <= x1[2] - a[2];
p1x3: x1[1] + a[1] <= x1[3] - a[3];
p2x3: x2[2] + a[2] <= x2[3] - a[3];
solve;
printf "#OUTPUT:\n";
#printf{i in Points} "a_%d = %d\n", i, a[i];
printf "a[1]: %d\n", a[1];
printf "-a[1]: %d\n", -a[1];
printf "a[3]: %d\n", a[3];
printf "#OUTPUT END:\n";
end;

Output:

    GLPSOL: GLPK LP/MIP Solver, v4.52
Parameter(s) specified in the command line:
 -m sol.mod
Reading model section from sol.mod...
22 lines were read
Generating obj...
Generating px1...
Generating py1...
Generating px2...
Generating py2...
Generating px3...
Generating py3...
Generating p1x2...
Generating p1x3...
Generating p2x3...
Model has been successfully generated
GLPK Simplex Optimizer, v4.52
10 rows, 9 columns, 21 non-zeros
Preprocessing...
3 rows, 3 columns, 6 non-zeros
Scaling...
 A: min|aij| =  1.000e+00  max|aij| =  1.000e+00  ratio =  1.000e+00
Problem data seem to be well scaled
Constructing initial basis...
Size of triangular part is 3
*     0: obj =   0.000000000e+00  infeas =  0.000e+00 (0)
*     3: obj =   3.500000000e+00  infeas =  0.000e+00 (0)
OPTIMAL LP SOLUTION FOUND
Time used:   0.0 secs
Memory used: 0.1 Mb (126476 bytes)
#OUTPUT:
a[1]: 2
-a[1]: -1
a[3]: 2
#OUTPUT END:
Model has been successfully processed

The issue seems to be that a[1] is evaluated to 2, while -a[1] is evaluated to -1. Also the a[3] equals 2 as well, so the constraint p1x3 is not fulfilled.

Currently I have no idea how to fix this or even what caused it.


Solution

  • Change %d format specifier to %g and see what happens. Note that a{i} are continuous variables that may have fractional values.