help-glpk
[Top][All Lists]
Advanced

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

Re: [Help-glpk] Simple glpk program giving me wrong maximum value output


From: Andrew Makhorin
Subject: Re: [Help-glpk] Simple glpk program giving me wrong maximum value output?
Date: Tue, 15 Apr 2014 11:02:35 +0400

On Tue, 2014-04-15 at 09:21 +0530, Amit Gurung wrote:
> "I do not know why the maximum value is 0 when it should have been -5.
> Not just this example, but with any other negative value, the output
> is always zero. How can I fix this?"

You incorrectly set lower bounds for 2nd and 4th rows (see below).

> 
> 
> 
> /*
>  * Maximize z = 0*x - 1*y
>  * subject to :
>  *      x<=1
>  *      x>=1
>  *      y<=5
>  *      y>=5
>  */
> 
> #include <iostream>
> #include <stdlib.h>
> 
> #include <glpk.h>
> 
> using namespace std;
> 
> int main(void)
> {
> glp_prob *lp;
> 
> int ia[1+1000], ja[1+1000];
> double ar[1+1000], z, x, y;
> 
> lp = glp_create_prob();
> glp_set_prob_name(lp, "sample");
> glp_set_obj_dir(lp, GLP_MAX);
> glp_add_rows(lp, 4);
> glp_set_row_name(lp, 1, "p");
> glp_set_row_bnds(lp, 1, GLP_UP, 0.0, 1.0);
> glp_set_row_name(lp, 2, "q");
> glp_set_row_bnds(lp, 2, GLP_LO, 0.0, 1.0);

should be glp_set_row_bnds(lp, 2, GLP_LO, 1.0, 0.0);

> glp_set_row_name(lp, 3, "r");
> glp_set_row_bnds(lp, 3, GLP_UP, 0.0, 5.0);
> glp_set_row_name(lp, 4, "s");
> glp_set_row_bnds(lp, 4, GLP_LO, 0.0, 5.0);

should be glp_set_row_bnds(lp, 4, GLP_LO, 5.0, 0.0);

> 
> glp_add_cols(lp, 2);
> glp_set_col_name(lp, 1, "x");
> glp_set_col_bnds(lp, 1, GLP_LO, 0.0, 0.0);  //why GLP_LO here ?
> glp_set_obj_coef(lp, 1, 0.0);
> glp_set_col_name(lp, 2, "x");
> glp_set_col_bnds(lp, 2, GLP_LO, 0.0, 0.0);  //why GLP_LO here ?
> glp_set_obj_coef(lp, 2, -1.0);
> 
> ia[1] = 1, ja[1] = 1, ar[1] = 1.0;
> ia[2] = 1, ja[2] = 2, ar[2] = 0.0;
> 
> ia[3] = 2, ja[3] = 1, ar[3] = 1.0;
> ia[4] = 2, ja[4] = 2, ar[4] = 0.0;
> 
> ia[5] = 3, ja[5] = 1, ar[5] = 0.0;
> ia[6] = 3, ja[6] = 2, ar[6] = 1.0;
> 
> ia[7] = 4, ja[7] = 1, ar[7] = 0.0;
> ia[8] = 4, ja[8] = 2, ar[8] = 1.0;
> 
> glp_load_matrix(lp, 8, ia, ja, ar);
> glp_simplex(lp, NULL);
> z = glp_get_obj_val(lp);
> x = glp_get_col_prim(lp, 1);
> y = glp_get_col_prim(lp, 2);
> 
> //printf("\nz = %g; x1 = %g; x2 = %g; x3 = %g\n",z, x1, x2, x3);
> cout <<"z = " << z << "  x = " << x << " y = " << y;
> cout << endl;
> glp_delete_prob(lp);
> return 0;
> }
> 
> -- 
> Praying for your good health,
> 
> 
> Amit Gurung,
> Contact  : +91 8974008902






reply via email to

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