help-glpk
[Top][All Lists]
Advanced

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

[Help-glpk] How to reuse the problem object to solve another MIP problem


From: Fernando H. Pratti
Subject: [Help-glpk] How to reuse the problem object to solve another MIP problem without deleting it?
Date: Sun, 6 Feb 2005 10:33:39 -0200

Hi all,

 

I’m trying to reuse a problem object (not deleting) to solve two different MIP problems.

The columns and objective function are the same for both problems, that’s why I don’t want to delete the problem after solve the first one.

If I solve each one separately I got the correct objective values with status: “INTEGER OPTIMAL”.

However, when the second problem after the first I get the wrong answer for the second problem with status: “INTEGER UNDEFINED”.

All I’m doing is to remove all rows from the problem object after solving the first problem and before solving the second one.

Is there anything else I should do to “clean up” the problem object before I reuse it?

 

Thanks in advance.

 

Here my sample code:

 

#include "glpk.h"

 

#define col_x 1

#define col_y 2

 

LPX *prob;

 

void createAndInitProb()

{

    prob = lpx_create_prob();

    lpx_set_class(prob, LPX_MIP);

    lpx_set_obj_dir(prob, LPX_MIN);

 

    lpx_add_cols(prob, 2);

   

    lpx_set_col_name(prob, col_x, "x");

    lpx_set_col_bnds(prob, col_x, LPX_DB, 0, 1);

    lpx_set_col_kind(prob, col_x, LPX_IV);

 

    lpx_set_col_name(prob, col_y, "y");

    lpx_set_col_bnds(prob, col_y, LPX_DB, 0, 1);

    lpx_set_col_kind(prob, col_y, LPX_IV);

   

    /* always minimize x */

    lpx_set_col_coef(prob, col_x, 1);

}

 

void addRow()

{

    int ind[3];

    double val[3];

   

    // x - y >= 0

    ind[1] = col_x, val[1] = 1.0;

    ind[2] = col_y, val[2] = -1.0;

    lpx_add_rows(prob, 1);

    lpx_set_row_name(prob, 1, "x-y>=0");

    lpx_set_mat_row(prob, 1, 2, ind, val);

    lpx_set_row_bnds(prob, 1, LPX_LO, 0, 0);

}

 

void solveProb(char* outputFileName)

{

    // solve lp relaxation

    lpx_simplex(prob);

 

    // solve mip

    lpx_integer(prob);

 

    // print mip solution

    lpx_print_mip(prob, outputFileName);

}

 

void clearProb()

{

    int num[2] = {0, 1};

   

    // Removing the row from prob

    lpx_del_rows(prob, 1, num);

}

 

void createAndSolveProb1(char* outputFileName)

{

    /*

     min x:

        x - y >= 0

        y = 1

     */

 

    /* y = 1 */

    lpx_set_col_bnds(prob, col_y, LPX_FX, 1, 1);

   

    addRow();

   

    solveProb(outputFileName);

}

 

void createAndSolveProb2(char* outputFileName)

{

    /*

     min x:

        x - y >= 0

        y = 0

     */

 

    /* y = 0 */

    lpx_set_col_bnds(prob, col_y, LPX_FX, 0, 0);

   

    addRow();

   

    solveProb(outputFileName);

}

 

int main()

{

    createAndInitProb();

 

    // This will give an objective value = 0 (that's the correct answer)

    createAndSolveProb1("prob1.txt");

   

    // Start a new one

    lpx_delete_prob(prob);

    createAndInitProb();

   

    // This will give an objective value = 1 (that's the correct answer)

    createAndSolveProb2("prob2.txt");

   

    // Start a new one

    lpx_delete_prob(prob);

    createAndInitProb();

   

    // This will give an objective value = 1 (that's the correct answer)

    createAndSolveProb1("prob1_before_prob2.txt");

    // Now I'll try to delete the rows instead the prob

    clearProb();

    // This will give an objective value = 1 (that's the **WRONG** answer)

    createAndSolveProb2("prob2_after_prob1.txt");

 

    lpx_delete_prob(prob);

    return 0;

}

 


reply via email to

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