[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [Help-glpk] binary variable
From: |
Andrew Makhorin |
Subject: |
Re: [Help-glpk] binary variable |
Date: |
Thu, 13 Mar 2008 18:56:12 +0300 |
> i have this example to do and here my code
> maximize x1 + x2 + 3* x3 + 4* x4;
> x4 <= x2
> x3 <= x1
> x1 + x2 + x3 + x4 <= 3
> xi : binary variable so xi = 0 or xi =1
> i do my code as follow :
> /* myexp.c */
> #include <stdio.h>
> #include <stdlib.h>
> //extern "C" {
> #include <glpk.h>
> //}
> int main(void)
> { glp_prob *lp;
> double Z, x1, x2, x3,x4;
> s1: lp = glp_create_prob();
> s2: glp_set_prob_name(lp, "sample");
> s3: glp_set_obj_dir(lp, GLP_MAX);
> s4: glp_add_rows(lp, 3);
> s5: glp_set_row_name(lp, 1, "p");
> s6: glp_set_row_bnds(lp, 1, GLP_UP, 0, 0);
> s7: glp_set_row_name(lp, 2, "q");
> s8: glp_set_row_bnds(lp, 2, GLP_UP, 0, 0);
> s9: glp_set_row_name(lp, 3, "r");
> s10: glp_set_row_bnds(lp, 3, GLP_UP, 0, 3);
> s11: glp_add_cols(lp, 4);
> glp_set_col_kind(lp,1,GLP_BV);
> s12: glp_set_col_name(lp, 1, "x1");
> s13: glp_set_col_bnds(lp, 1, GLP_DB, 0, 1);
> s14: glp_set_obj_coef(lp, 1, 1);
> glp_set_col_kind(lp,2,GLP_BV);
> s15: glp_set_col_name(lp, 2, "x2");
> s16: glp_set_col_bnds(lp, 2, GLP_DB, 0, 1);
> s17: glp_set_obj_coef(lp, 2, 1);
> glp_set_col_kind(lp,3,GLP_BV);
> s18: glp_set_col_name(lp, 3, "x3");
> s19: glp_set_col_bnds(lp, 3, GLP_DB, 0,1);
> s20: glp_set_obj_coef(lp, 3, 3);
> glp_set_col_kind(lp,4,GLP_BV);
> glp_set_col_name(lp, 4, "x4");
> glp_set_col_bnds(lp, 4, GLP_DB, 0,1);
> glp_set_obj_coef(lp, 4, 4);
> int ia[9]={0,1,1,2,2,3,3,3,3};
> int ja[9]={0,2,4,1,3,1,2,3,4};
> double ar[9]={0,-1,1,-1,1,1,1,1,1};
> s30: glp_load_matrix(lp, 8, ia, ja, ar);
> glp_simplex(lp,NULL);
> s31: glp_intopt(lp,NULL);
> s32: Z = glp_get_obj_val(lp);
> s33: x1 = glp_get_col_prim(lp, 1);
> s34: x2 = glp_get_col_prim(lp, 2);
> s35: x3 = glp_get_col_prim(lp, 3);
> x4 = glp_get_col_prim(lp, 4);
> s36: printf("\nZ = %g; x1 = %g; x2 = %g; x3 = %g; x4
> = %g\n", Z, x1, x2, x3, x4);
> s37: glp_delete_prob(lp);
> return 0;
> }
> /* eof */
> my problem is that i have an inexpected result :
> * 0: objval = 0.000000000e+00 infeas =
> 0.000000000e+00 (0)
> * 4: objval = 7.000000000e+00 infeas =
> 0.000000000e+00 (0)
> OPTIMAL SOLUTION FOUND
> Integer optimization begins...
> + 4: mip = not found yet <= +inf
> (1; 0)
+ 6: >>>>>> 6.000000000e+00 <= 7.000000000e+00
> 16.7% (3; 0)
> + 6: mip = 6.000000000e+00 <= tree is empty
> 0.0% (0; 5)
> INTEGER OPTIMAL SOLUTION FOUND
> Z = 7; x1 = 0.5; x2 = 1; x3 = 0.5; x4 = 1
====>> we can see here than xi even if are binary we
> have x3 = 0.5 and also for x1 = 0.5 so i wonder that
> there is something wrong with binary variable or may
> be i did a mistake in my code.
You obtained components of lp relaxation, not of mip solution.
You need to call glp_mip_obj_val and glp_mip_col_val rather than
glp_get_obj_val and glp_get_col_prim in statements s32-s36.