introduce signed_cone struct
[barvinok.git] / basis_reduction_glpk.c
bloba647ee8518b7f16816c7b0949568f229e70ec955
1 #include <math.h>
2 #include <glpk.h>
3 #include <barvinok/basis_reduction.h>
5 static LPX *init_lp(Polyhedron *P);
6 static void set_lp_obj(LPX *lp, Value *row, int dim);
7 static void solve_lp(LPX *lp);
8 static void get_obj_val(LPX* lp, double *F);
9 static int add_lp_row(LPX *lp, Value *row, int dim);
10 static void get_alpha(LPX* lp, int row, double *alpha);
11 static void del_lp_row(LPX *lp);
13 #define GBR_LP LPX
14 #define GBR_type double
15 #define GBR_init(v) do { } while(0)
16 #define GBR_clear(v) do { } while(0)
17 #define GBR_set(a,b) a = b
18 #define GBR_set_ui(a,b) a = b
19 #define GBR_mul(a,b,c) a = b * c
20 #define GBR_lt(a,b) (a < b)
21 #define GBR_floor(a,b) value_set_si(a,(int)floor(b+1e-10))
22 #define GBR_ceil(a,b) value_set_si(a,(int)ceil(b-1e-10))
23 #define GBR_lp_init(P) init_lp(P)
24 #define GBR_lp_set_obj(lp, obj, dim) set_lp_obj(lp, obj, dim)
25 #define GBR_lp_solve(lp) solve_lp(lp)
26 #define GBR_lp_get_obj_val(lp, F) get_obj_val(lp, F)
27 #define GBR_lp_delete(lp) lpx_delete_prob(lp)
28 #define GBR_lp_next_row(lp) lpx_get_num_rows(lp)+1
29 #define GBR_lp_add_row(lp, row, dim) add_lp_row(lp, row, dim)
30 #define GBR_lp_get_alpha(lp, row, alpha) get_alpha(lp, row, alpha)
31 #define GBR_lp_del_row(lp) del_lp_row(lp);
32 #define Polyhedron_Reduced_Basis glpk_Polyhedron_Reduced_Basis
33 #include "basis_reduction_templ.c"
35 static LPX *init_lp(Polyhedron *P)
37 LPX *lp;
38 int *ind;
39 double *val;
40 int i, j, k, l;
42 ind = ALLOCN(int, 1+P->Dimension);
43 val = ALLOCN(double, 1+P->Dimension);
44 lp = lpx_create_prob();
45 lpx_set_obj_dir(lp, LPX_MAX);
46 lpx_add_rows(lp, 2*P->NbConstraints);
47 lpx_add_cols(lp, 2*P->Dimension);
48 for (i = 0; i < 2; ++i) {
49 for (j = 0; j < P->NbConstraints; ++j) {
50 for (k = 0, l = 0; k < P->Dimension; ++k) {
51 if (value_zero_p(P->Constraint[j][1+k]))
52 continue;
53 ind[1+l] = 1+i*P->Dimension+k;
54 val[1+l] = VALUE_TO_DOUBLE(P->Constraint[j][1+k]);
55 ++l;
57 lpx_set_mat_row(lp, 1+i*P->NbConstraints+j, l, ind, val);
58 lpx_set_row_bnds(lp, 1+i*P->NbConstraints+j, LPX_LO,
59 -VALUE_TO_DOUBLE(P->Constraint[j][1+P->Dimension]), 0);
61 for (k = 0, l = 0; k < P->Dimension; ++k) {
62 lpx_set_col_bnds(lp, 1+i*P->Dimension+k, LPX_FR, 0, 0);
65 free(ind);
66 free(val);
67 lpx_set_int_parm(lp, LPX_K_MSGLEV, 0);
68 return lp;
71 static void set_lp_obj(LPX *lp, Value *row, int dim)
73 int j;
74 for (j = 0; j < dim; ++j) {
75 lpx_set_obj_coef(lp, 1+j, VALUE_TO_DOUBLE(row[j]));
76 lpx_set_obj_coef(lp, 1+dim+j, -VALUE_TO_DOUBLE(row[j]));
80 static void solve_lp(LPX *lp)
82 lpx_adv_basis(lp);
83 lpx_simplex(lp);
86 static void get_obj_val(LPX* lp, double *F)
88 *F = lpx_get_obj_val(lp);
89 assert(*F > -1e-10);
90 if (*F < 0)
91 *F = 0;
94 static int add_lp_row(LPX *lp, Value *row, int dim)
96 int j, l;
97 int nr = lpx_add_rows(lp, 1);
98 int *ind;
99 double *val;
101 ind = ALLOCN(int, 1+2*dim);
102 val = ALLOCN(double, 1+2*dim);
103 for (j = 0, l = 0; j < dim; ++j) {
104 if (value_zero_p(row[j]))
105 continue;
106 ind[1+l] = 1+j;
107 val[1+l] = VALUE_TO_DOUBLE(row[j]);
108 ind[1+l+1] = 1+dim+j;
109 val[1+l+1] = -VALUE_TO_DOUBLE(row[j]);
110 l += 2;
112 lpx_set_mat_row(lp, nr, l, ind, val);
113 lpx_set_row_bnds(lp, nr, LPX_FX, 0, 0);
114 free(ind);
115 free(val);
117 return nr;
120 static void get_alpha(LPX* lp, int row, double *alpha)
122 *alpha = -lpx_get_row_dual(lp, row);
125 static void del_lp_row(LPX *lp)
127 int rows[2];
128 rows[1] = lpx_get_num_rows(lp);
129 lpx_del_rows(lp, 1, rows);