Hi,
I would like to make use of glpk library in my C programme. I have
made some tests of it in Octave and it works great, giving an
expected results. However using "raw" glpk via C gives different,
wrong, results. I suspect, that it's a matter of proper
parametrisation of the library, but I am a novice and a real problem
may be hiding somewhere else. I have tried to set the same defaults
that glpk has in octave but it turned out to be a lot of fiddling
around and the way to success seems vague. Can you provide me steps
I should perform to make glpk work well in C as it does in Octave?
Bellow I am providing my sample code in Octave and its
output(correct) followed by C code and its output(wrong).
Best regards,
Karol
OCTAVE CODE:
c=[1,0,0,0,0];
A=[1,
0, 1, 1, 0;
0,1,1,1,0;
0,1,0,0,1;
1,1,1,0,1];
b=[1.0969,0.8416,0.2007,0.5986]';
lb=[0;0;0;0;0];
s=-1;
ctype=["S","S","S","S"];
vartype=["C","C","C","C","C"];
param.msglev=3;
[xmin,fmin,status,extra]=glpk(c,A,b,lb,ub,ctype,vartype,s,param);
OCTAVE OUTPUT:
GLPK Simplex Optimizer, v4.55
4
rows, 5 columns, 12 non-zeros
Preprocessing...
4
rows, 5 columns, 12 non-zeros
Scaling...
A:
min|aij| = 1.000e+000 max|aij| = 1.000e+000 ratio =
1.000e+000
Problem
data seem to be well scaled
Constructing
initial basis...
Size
of triangular part is 3
0: obj = 3.979000000e-001 infeas = 1.426e-001 (1)
*
1: obj = 3.979000000e-001 infeas = 0.000e+000 (0)
OPTIMAL
LP SOLUTION FOUND
fmin=
0.25530
xmin=[
0.25530; 0.00000; 0.14260; 0.69900; 0.20070 ]
##############################
C CODE:
int i,j;
int X[13],Y[13];
double *pathPerf, coeff[13];
double o1,o2,o3,o4,o5;
glp_smcp smcp;
glp_init_smcp(&smcp);
smcp.msg_lev=GLP_MSG_ALL;
//glp problem structure
glp_prob *problem;
//init problem structure
problem=glp_create_prob();
glp_set_obj_dir(problem, GLP_MIN);
//define number of rows and columns in the matrix
glp_add_rows(problem, 4);
glp_add_cols(problem,5);
//seting bounds
for(i=1;i<5;i++)
glp_set_col_bnds(problem,i,GLP_LO,0.0,0);
//right-hand values of each equation
glp_set_row_bnds(problem,1,GLP_FX,1.0969,0);
glp_set_row_bnds(problem,2,GLP_FX,0.8416,0);
glp_set_row_bnds(problem,3,GLP_FX,0.2007,0);
glp_set_row_bnds(problem,4,GLP_FX,0.5986,0);
//objective func. coefficients
glp_set_obj_coef(problem, 1, 1.0);
glp_set_obj_coef(problem, 2, 0.0);
glp_set_obj_coef(problem, 3, 0.0);
glp_set_obj_coef(problem, 4, 0.0);
glp_set_obj_coef(problem, 5, 0.0);
X[1]=1,Y[1]=1,coeff[1]=1.0;
X[2]=1,Y[2]=3,coeff[2]=1.0;
X[3]=1,Y[3]=4,coeff[3]=1.0;
X[4]=2,Y[4]=2,coeff[4]=1.0;
X[5]=2,Y[5]=3,coeff[5]=1.0;
X[6]=2,Y[6]=4,coeff[6]=1.0;
X[7]=3,Y[7]=2,coeff[7]=1.0;
X[8]=3,Y[8]=5,coeff[8]=1.0;
X[9]=4,Y[9]=1,coeff[9]=1.0;
X[10]=4,Y[10]=2,coeff[10]=1.0;
X[11]=4,Y[11]=3,coeff[11]=1.0;
X[12]=4,Y[12]=5,coeff[12]=1.0;
glp_load_matrix(problem, 12, X, Y, coeff);
glp_simplex(problem, &smcp);
o1=glp_get_col_prim(problem,1);
o2=glp_get_col_prim(problem,2);
o3=glp_get_col_prim(problem,3);
o4=glp_get_col_prim(problem,4);
o5=glp_get_col_prim(problem,5);
printf("fmin:%f\n",glp_get_row_prim(problem, 1));
printf("xmin:%f,%f,%f,%f,%f\n",o1,o2,o3,o4,o5);
//cleanup
glp_delete_prob(problem);
glp_free_env();
C OUTPUT:
GLPK Simplex Optimizer, v4.55
4
rows, 5 columns, 12 non-zeros
0: obj = 0.000000000e+00 infeas = 2.738e+00 (4)
4: obj = 3.979000000e-01 infeas = 5.810e-02 (1)
LP
HAS NO PRIMAL FEASIBLE SOLUTION
fmin:1.038800
xmin:0.397900,0.200700,0.000000,0.640900,0.000000
|