-------- Forwarded Message --------
From: Gabriel Gelius-Dietrich <[email protected]>
To: [email protected]
Subject: Difference between glpsol and C-API
Date: Fri, 28 Feb 2014 15:58:34 +0100

Hi,

I have a question concerning the difference between glpsol and the C-API 
of glpk.

The attached C source code file test.c contains a very tiny linear 
optimization problem, the problem gets solved by glp_simplex and the 
value of the objective function and of the primary variables after 
optimization are printed out.  Before the optimization, the problem will 
be written to an lp file test.lp.

Using this file with glpsol, gives another result than the C-API 
(objective value stays the same, but the values of the variables differ 
in some cases).

glpsol --lp test.lp -o test.sol

I am using glpk version 4.45 on debian wheezy (stable).

Is there any explanation for this behavior?


Thanks a lot fir help,

Gabriel Gelius-Dietrich


/*
test.c
gcc -c test.c
gcc test.o -lglpk -lm -o test
*/


#include <stdio.h>
#include <stdlib.h>
#include <glpk.h>

int main(void) {
	glp_prob *lp;
    int ia[1+1000], ja[1+1000];
    double ar[1+1000], z, x1, x2, x3, x4, x5, x6, x7, x8;

    lp = glp_create_prob();
    glp_set_obj_dir(lp, GLP_MIN);
    glp_add_rows(lp, 5);
    glp_add_cols(lp, 8);

    glp_set_row_bnds(lp, 1, GLP_DB, 2.5, 1000);
    glp_set_row_bnds(lp, 2, GLP_DB, -1000, 2.1);
    glp_set_row_bnds(lp, 3, GLP_FX, 4, 4);
    glp_set_row_bnds(lp, 4, GLP_DB, 1.8, 5);
    glp_set_row_bnds(lp, 5, GLP_DB, 3, 15);

    glp_set_col_name(lp, 1, "x_1");
    glp_set_col_name(lp, 2, "x_2");
    glp_set_col_name(lp, 3, "x_3");
    glp_set_col_name(lp, 4, "x_4");
    glp_set_col_name(lp, 5, "x_5");
    glp_set_col_name(lp, 6, "x_6");
    glp_set_col_name(lp, 7, "x_7");
    glp_set_col_name(lp, 8, "x_8");

    glp_set_col_bnds(lp, 1, GLP_DB, 2.5, 1000);
    glp_set_col_bnds(lp, 2, GLP_DB, 0, 4.1);
    glp_set_col_bnds(lp, 3, GLP_DB, 0, 1);
    glp_set_col_bnds(lp, 4, GLP_DB, 0, 1);
    glp_set_col_bnds(lp, 5, GLP_DB, 0.5, 4);
    glp_set_col_bnds(lp, 6, GLP_DB, 0, 1000);
    glp_set_col_bnds(lp, 7, GLP_DB, 0, 1000);
    glp_set_col_bnds(lp, 8, GLP_DB, 0, 4.3);

    glp_set_obj_coef(lp, 1, 1);
    glp_set_obj_coef(lp, 2, 0);
    glp_set_obj_coef(lp, 3, 0);
    glp_set_obj_coef(lp, 4, 0);
    glp_set_obj_coef(lp, 5, 2);
    glp_set_obj_coef(lp, 6, 0);
    glp_set_obj_coef(lp, 7, 0);
    glp_set_obj_coef(lp, 8, -1);

    ia[1]  = 1, ja[1]  = 1, ar[1]  =  3.0;
    ia[2]  = 5, ja[2]  = 1, ar[2]  =  5.6;
    ia[3]  = 1, ja[3]  = 2, ar[3]  =  1.0;
    ia[4]  = 2, ja[4]  = 2, ar[4]  =  2.0;
    ia[5]  = 2, ja[5]  = 3, ar[5]  =  1.1;
    ia[6]  = 3, ja[6]  = 3, ar[6]  =  1.0;
    ia[7]  = 1, ja[7]  = 4, ar[7]  = -2.0;
    ia[8]  = 4, ja[8]  = 4, ar[8]  =  2.8;
    ia[9]  = 1, ja[9]  = 5, ar[9]  = -1.0;
    ia[10] = 5, ja[10] = 5, ar[10] =  1.0;
    ia[11] = 3, ja[11] = 6, ar[11] =  1.0;
    ia[12] = 4, ja[12] = 7, ar[12] = -1.2;
    ia[13] = 1, ja[13] = 8, ar[13] = -1.0;
    ia[14] = 5, ja[14] = 8, ar[14] =  1.9;

    glp_load_matrix(lp, 14, ia, ja, ar);

    glp_write_lp(lp, NULL, "test.lp");

    glp_simplex(lp, NULL);

    z = glp_get_obj_val(lp);
    x1 = glp_get_col_prim(lp, 1);
    x2 = glp_get_col_prim(lp, 2);
    x3 = glp_get_col_prim(lp, 3);
    x4 = glp_get_col_prim(lp, 4);
    x5 = glp_get_col_prim(lp, 5);
    x6 = glp_get_col_prim(lp, 6);
    x7 = glp_get_col_prim(lp, 7);
    x8 = glp_get_col_prim(lp, 8);

    printf("\nz  = %g\nx1 = %g\nx2 = %g\nx3 = %g\nx4 = %g\nx5 = %g\nx6 = %g\nx7 = %g\nx8 = %g\n\n", z, x1, x2, x3, x4, x5, x6, x7, x8);

    glp_delete_prob(lp);
    return 0;

} /* eof */
_______________________________________________
Help-glpk mailing list
[email protected]
https://lists.gnu.org/mailman/listinfo/help-glpk

Reply via email to