iscc: use cloog_union_domain_from_isl_set
[barvinok.git] / basis_reduction_glpk.c
blob136bfdea20796454339fb5a9a3290dafd8f962d8
1 #include <assert.h>
2 #include <math.h>
3 #include <glpk.h>
4 #include <barvinok/basis_reduction.h>
6 static LPX *init_lp(Polyhedron *P);
7 static void set_lp_obj(LPX *lp, Value *row, int dim);
8 static int solve_lp(LPX *lp);
9 static void get_obj_val(LPX* lp, double *F);
10 static int add_lp_row(LPX *lp, Value *row, int dim);
11 static void get_alpha(LPX* lp, int row, double *alpha);
12 static void del_lp_row(LPX *lp);
14 #define GBR_LP LPX
15 #define GBR_type double
16 #define GBR_init(v) do { } while(0)
17 #define GBR_clear(v) do { } while(0)
18 #define GBR_set(a,b) a = b
19 #define GBR_set_ui(a,b) a = b
20 #define GBR_mul(a,b,c) a = b * c
21 #define GBR_lt(a,b) (a < b)
22 #define GBR_floor(a,b) value_set_si(a,(int)floor(b+1e-10))
23 #define GBR_ceil(a,b) value_set_si(a,(int)ceil(b-1e-10))
24 #define GBR_lp_init(P) init_lp(P)
25 #define GBR_lp_set_obj(lp, obj, dim) set_lp_obj(lp, obj, dim)
26 #define GBR_lp_solve(lp) solve_lp(lp)
27 #define GBR_lp_get_obj_val(lp, F) get_obj_val(lp, F)
28 #define GBR_lp_delete(lp) lpx_delete_prob(lp)
29 #define GBR_lp_next_row(lp) lpx_get_num_rows(lp)+1
30 #define GBR_lp_add_row(lp, row, dim) add_lp_row(lp, row, dim)
31 #define GBR_lp_get_alpha(lp, row, alpha) get_alpha(lp, row, alpha)
32 #define GBR_lp_del_row(lp) del_lp_row(lp);
33 #define Polyhedron_Reduced_Basis glpk_Polyhedron_Reduced_Basis
34 #include "basis_reduction_templ.c"
36 static LPX *init_lp(Polyhedron *P)
38 LPX *lp;
39 int *ind;
40 double *val;
41 int i, j, k, l;
43 ind = ALLOCN(int, 1+P->Dimension);
44 val = ALLOCN(double, 1+P->Dimension);
45 lp = lpx_create_prob();
46 lpx_set_obj_dir(lp, LPX_MAX);
47 lpx_add_rows(lp, 2*P->NbConstraints);
48 lpx_add_cols(lp, 2*P->Dimension);
49 for (i = 0; i < 2; ++i) {
50 for (j = 0; j < P->NbConstraints; ++j) {
51 int type = j < P->NbEq ? LPX_FX : LPX_LO;
52 for (k = 0, l = 0; k < P->Dimension; ++k) {
53 if (value_zero_p(P->Constraint[j][1+k]))
54 continue;
55 ind[1+l] = 1+i*P->Dimension+k;
56 val[1+l] = VALUE_TO_DOUBLE(P->Constraint[j][1+k]);
57 ++l;
59 lpx_set_mat_row(lp, 1+i*P->NbConstraints+j, l, ind, val);
60 lpx_set_row_bnds(lp, 1+i*P->NbConstraints+j, type,
61 -VALUE_TO_DOUBLE(P->Constraint[j][1+P->Dimension]), 0);
63 for (k = 0, l = 0; k < P->Dimension; ++k) {
64 lpx_set_col_bnds(lp, 1+i*P->Dimension+k, LPX_FR, 0, 0);
67 free(ind);
68 free(val);
69 lpx_set_int_parm(lp, LPX_K_MSGLEV, 0);
70 return lp;
73 static void set_lp_obj(LPX *lp, Value *row, int dim)
75 int j;
76 for (j = 0; j < dim; ++j) {
77 lpx_set_obj_coef(lp, 1+j, VALUE_TO_DOUBLE(row[j]));
78 lpx_set_obj_coef(lp, 1+dim+j, -VALUE_TO_DOUBLE(row[j]));
82 static int solve_lp(LPX *lp)
84 lpx_adv_basis(lp);
85 lpx_simplex(lp);
86 return lpx_get_status(lp) == LPX_UNBND;
89 static void get_obj_val(LPX* lp, double *F)
91 assert(lpx_get_status(lp) == LPX_OPT);
92 *F = lpx_get_obj_val(lp);
93 assert(*F > -1e-10);
94 if (*F < 0)
95 *F = 0;
98 static int add_lp_row(LPX *lp, Value *row, int dim)
100 int j, l;
101 int nr = lpx_add_rows(lp, 1);
102 int *ind;
103 double *val;
105 ind = ALLOCN(int, 1+2*dim);
106 val = ALLOCN(double, 1+2*dim);
107 for (j = 0, l = 0; j < dim; ++j) {
108 if (value_zero_p(row[j]))
109 continue;
110 ind[1+l] = 1+j;
111 val[1+l] = VALUE_TO_DOUBLE(row[j]);
112 ind[1+l+1] = 1+dim+j;
113 val[1+l+1] = -VALUE_TO_DOUBLE(row[j]);
114 l += 2;
116 lpx_set_mat_row(lp, nr, l, ind, val);
117 lpx_set_row_bnds(lp, nr, LPX_FX, 0, 0);
118 free(ind);
119 free(val);
121 return nr;
124 static void get_alpha(LPX* lp, int row, double *alpha)
126 *alpha = -lpx_get_row_dual(lp, row);
129 static void del_lp_row(LPX *lp)
131 int rows[2];
132 rows[1] = lpx_get_num_rows(lp);
133 lpx_del_rows(lp, 1, rows);