help-glpk
[Top][All Lists]

## [Help-glpk] Re: How to solve a simple MIP problem ?

 From: Andrew Makhorin Subject: [Help-glpk] Re: How to solve a simple MIP problem ? Date: Tue, 12 Aug 2003 02:56:47 +0400

```>I want to solve a simple problem MIP (I guess) using GLPK API, but
> I couldn't find some samples to help me.
>My problem is:
>
>    Minimize: x0
>
>Where:
>    x0 -p + b >= 0
>    x0 -p -f >= -1
>    b = 1
>    f = 1
>
>x0, b, f and p are all binary variables.
>
>I've tried to change te code that cames with manual but didn't work...
>More I change, more problems show up.
>Someone could send me a simple C code that solve it?

Here is an example below.

#include "glpk.h"

#define row_1     1
#define row_2     2

#define col_x0    1
#define col_p     2
#define col_b     3
#define col_f     4

int main(void)
{     LPX *prob;
int ndx;
double val;
prob = lpx_create_prob();
lpx_set_class(prob, LPX_MIP);
/* x0 - p + b >= 0 */
ndx = col_x0, val = 1;
ndx = col_p, val = -1;
ndx = col_b, val = 1;
lpx_set_mat_row(prob, row_1, 3, ndx, val);
lpx_set_row_bnds(prob, row_1, LPX_LO, 0, 0);
/* x0 - p - f >= -1 */
ndx = col_x0, val = 1;
ndx = col_p, val = -1;
ndx = col_f, val = -1;
lpx_set_mat_row(prob, row_2, 3, ndx, val);
lpx_set_row_bnds(prob, row_2, LPX_LO, -1, 0);
/* all variables are binary */
lpx_set_col_bnds(prob, col_x0, LPX_DB, 0, 1);
lpx_set_col_kind(prob, col_x0, LPX_IV);
lpx_set_col_bnds(prob, col_p, LPX_DB, 0, 1);
lpx_set_col_kind(prob, col_p, LPX_IV);
lpx_set_col_bnds(prob, col_b, LPX_DB, 0, 1);
lpx_set_col_kind(prob, col_b, LPX_IV);
lpx_set_col_bnds(prob, col_f, LPX_DB, 0, 1);
lpx_set_col_kind(prob, col_f, LPX_IV);
/* minimize x0 */
lpx_set_obj_dir(prob, LPX_MIN);
lpx_set_col_coef(prob, col_x0, 1);
/* solve lp relaxation */
lpx_simplex(prob);
/* solve mip */
lpx_integer(prob);
/* print mip solution */
lpx_print_mip(prob, "sol.txt");
lpx_delete_prob(prob);
return 0;
}

```