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
);
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
)
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
]))
55 ind
[1+l
] = 1+i
*P
->Dimension
+k
;
56 val
[1+l
] = VALUE_TO_DOUBLE(P
->Constraint
[j
][1+k
]);
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);
69 lpx_set_int_parm(lp
, LPX_K_MSGLEV
, 0);
73 static void set_lp_obj(LPX
*lp
, Value
*row
, int dim
)
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
)
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
);
98 static int add_lp_row(LPX
*lp
, Value
*row
, int dim
)
101 int nr
= lpx_add_rows(lp
, 1);
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
]))
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
]);
116 lpx_set_mat_row(lp
, nr
, l
, ind
, val
);
117 lpx_set_row_bnds(lp
, nr
, LPX_FX
, 0, 0);
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
)
132 rows
[1] = lpx_get_num_rows(lp
);
133 lpx_del_rows(lp
, 1, rows
);