help-glpk
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Help-glpk] Cannot get integer case to work


From: William Gordon Rutherdale
Subject: [Help-glpk] Cannot get integer case to work
Date: Tue, 25 Aug 2009 08:06:49 -0400
User-agent: Mozilla/5.0 (X11; U; Linux x86_64; en-US; rv:1.9.1b3pre) Gecko/20090513 Fedora/3.0-2.3.beta2.fc11 Lightning/1.0pre Thunderbird/3.0b2

Hi.

As a first-time user of glpk, I tried an example from an old university text book. The model is given in comments in the enclosed C source code below.

I am using a Fedora 11 machine;  uname gives this output:
2.6.29.6-217.2.3.fc11.x86_64 #1 SMP Wed Jul 29 16:02:42 EDT 2009 x86_64 x86_64 x86_64 GNU/Linux The gcc version is 4.4.0, and the glpk version is 4.36 according to the header file.

I don't know if I have found a bug or just need advice on correct usage of the library. When I asked for a regular LP solution, it gave me the correct answer of
z==55.083333333333328596, x==4.25, y==3.1666666666666665186

However when I asked for an integer solution, I got
z==0, x==0, y==0

which is incorrect. The correct optimal integer solution is x=3, y=4, giving an objective value of z=53.

I only went by the API description in the documentation. Did I set up the problem incorrectly? Is there a way to get this to work in the integer case?

Any help would be appreciated.

-Will


// LP problem from Kolman & Beck (1980), p. 236 ff:  Section 4.1, Example 8.
// gcc -std=c99 -Wall -Werror kolman_beck01.c -o kolman_beck01 -lm -lglpk

#include <stdio.h>
#include <stdbool.h>
#include <glpk/glpk.h>

void solveProb( bool integer_constraints );

// Maximize z = 7x+8y s. t.
//     10x + 3y <= 52
//      2x + 3y <= 18
//      x >=0, y >=0, integers


int main( void )
{
    solveProb( false );
    solveProb( true );
    return 0;
}


void solveProb( bool integer_constraints )
{
printf( "Solving problem %s integer constraints.\n", (integer_constraints ? "with" : "without") );

    glp_prob *prob = glp_create_prob();
    glp_set_prob_name( prob, "kolman_beck01" );
    glp_add_rows( prob, 2 );
    glp_add_cols( prob, 2 );

    glp_set_obj_name( prob, "z" );
    glp_set_obj_dir( prob, GLP_MAX );
    glp_set_obj_coef( prob, 1, 7.0 );
    glp_set_obj_coef( prob, 2, 8.0 );

    glp_set_row_bnds( prob, 1, GLP_UP, 0.0, 52.0 );
    glp_set_row_bnds( prob, 2, GLP_UP, 0.0, 18.0 );
    glp_set_col_bnds( prob, 1, GLP_LO, 0.0, 0.0 );
    glp_set_col_bnds( prob, 2, GLP_LO, 0.0, 0.0 );

    if ( integer_constraints )
    {
        glp_set_col_kind( prob, 1, GLP_IV );
        glp_set_col_kind( prob, 2, GLP_IV );
    }

    #define ne 4
    int ia[ne+1] =    { 0,    1,    1,    2,    2 };
    int ja[ne+1] =    { 0,    1,    2,    1,    2 };
    double ar[ne+1] = { 0.0,  10.0, 3.0,  2.0,  3.0 };
    glp_load_matrix( prob, ne, ia, ja, ar );

    int result = 0;
    if ( integer_constraints )
    {
        glp_iocp control_params;
        glp_init_iocp( &control_params );
        control_params.presolve = GLP_ON;
        result = glp_intopt( prob, &control_params );
    }
    else
    {
        glp_smcp control_params;
        glp_init_smcp( &control_params );
        result = glp_simplex( prob, &control_params );
    }

    printf( "result==%d\n", result );
    double z = glp_get_obj_val( prob );
    double x = glp_get_col_prim( prob, 1 );
    double y = glp_get_col_prim( prob, 2 );
    printf( "z==%0.20g, x==%0.20g, y==%0.20g\n\n", z, x, y );

    glp_delete_prob( prob );
}






reply via email to

[Prev in Thread] Current Thread [Next in Thread]