isl_tab_compute_reduced_basis: handle empty tables
[platform/upstream/isl.git] / basis_reduction_templ.c
1 #include <stdlib.h>
2 #include "isl_basis_reduction.h"
3
4 static void save_alpha(GBR_LP *lp, int first, int n, GBR_type *alpha)
5 {
6         int i;
7
8         for (i = 0; i < n; ++i)
9                 GBR_lp_get_alpha(lp, first + i, &alpha[i]);
10 }
11
12 /* Compute a reduced basis for the set represented by the tableau "tab".
13  * tab->basis, must be initialized by the calling function to an affine
14  * unimodular basis, is updated to reflect the reduced basis.
15  * The first tab->n_zero rows of the basis (ignoring the constant row)
16  * are assumed to correspond to equalities and are left untouched.
17  * tab->n_zero is updated to reflect any additional equalities that
18  * have been detected in the first rows of the new basis.
19  * The final tab->n_unbounded rows of the basis are assumed to correspond
20  * to unbounded directions and are also left untouched.
21  * In particular this means that the remaining rows are assumed to
22  * correspond to bounded directions.
23  *
24  * This function implements the algorithm described in
25  * "An Implementation of the Generalized Basis Reduction Algorithm
26  *  for Integer Programming" of Cook el al. to compute a reduced basis.
27  * We use \epsilon = 1/4.
28  *
29  * If ctx->gbr_only_first is set, the user is only interested
30  * in the first direction.  In this case we stop the basis reduction when
31  * the width in the first direction becomes smaller than 2.
32  */
33 struct isl_tab *isl_tab_compute_reduced_basis(struct isl_tab *tab)
34 {
35         unsigned dim;
36         struct isl_ctx *ctx;
37         struct isl_mat *B;
38         int unbounded;
39         int i;
40         GBR_LP *lp = NULL;
41         GBR_type F_old, alpha, F_new;
42         int row;
43         isl_int tmp;
44         struct isl_vec *b_tmp;
45         GBR_type *F = NULL;
46         GBR_type *alpha_buffer[2] = { NULL, NULL };
47         GBR_type *alpha_saved;
48         GBR_type F_saved;
49         int use_saved = 0;
50         isl_int mu[2];
51         GBR_type mu_F[2];
52         GBR_type two;
53         GBR_type one;
54         int empty = 0;
55         int fixed = 0;
56         int fixed_saved = 0;
57         int mu_fixed[2];
58         int n_bounded;
59
60         if (!tab)
61                 return NULL;
62
63         if (tab->empty)
64                 return tab;
65
66         ctx = tab->mat->ctx;
67         dim = tab->n_var;
68         B = tab->basis;
69         if (!B)
70                 return tab;
71
72         n_bounded = dim - tab->n_unbounded;
73         if (n_bounded <= tab->n_zero + 1)
74                 return tab;
75
76         isl_int_init(tmp);
77         isl_int_init(mu[0]);
78         isl_int_init(mu[1]);
79
80         GBR_init(alpha);
81         GBR_init(F_old);
82         GBR_init(F_new);
83         GBR_init(F_saved);
84         GBR_init(mu_F[0]);
85         GBR_init(mu_F[1]);
86         GBR_init(two);
87         GBR_init(one);
88
89         b_tmp = isl_vec_alloc(ctx, dim);
90         if (!b_tmp)
91                 goto error;
92
93         F = isl_alloc_array(ctx, GBR_type, n_bounded);
94         alpha_buffer[0] = isl_alloc_array(ctx, GBR_type, n_bounded);
95         alpha_buffer[1] = isl_alloc_array(ctx, GBR_type, n_bounded);
96         alpha_saved = alpha_buffer[0];
97
98         if (!F || !alpha_buffer[0] || !alpha_buffer[1])
99                 goto error;
100
101         for (i = 0; i < n_bounded; ++i) {
102                 GBR_init(F[i]);
103                 GBR_init(alpha_buffer[0][i]);
104                 GBR_init(alpha_buffer[1][i]);
105         }
106
107         GBR_set_ui(two, 2);
108         GBR_set_ui(one, 1);
109
110         lp = GBR_lp_init(tab);
111         if (!lp)
112                 goto error;
113
114         i = tab->n_zero;
115
116         GBR_lp_set_obj(lp, B->row[1+i]+1, dim);
117         ctx->stats->gbr_solved_lps++;
118         unbounded = GBR_lp_solve(lp);
119         isl_assert(ctx, !unbounded, goto error);
120         GBR_lp_get_obj_val(lp, &F[i]);
121
122         if (GBR_lt(F[i], one)) {
123                 if (!GBR_is_zero(F[i])) {
124                         empty = GBR_lp_cut(lp, B->row[1+i]+1);
125                         if (empty)
126                                 goto done;
127                         GBR_set_ui(F[i], 0);
128                 }
129                 tab->n_zero++;
130         }
131
132         do {
133                 if (i+1 == tab->n_zero) {
134                         GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
135                         ctx->stats->gbr_solved_lps++;
136                         unbounded = GBR_lp_solve(lp);
137                         isl_assert(ctx, !unbounded, goto error);
138                         GBR_lp_get_obj_val(lp, &F_new);
139                         fixed = GBR_lp_is_fixed(lp);
140                         GBR_set_ui(alpha, 0);
141                 } else
142                 if (use_saved) {
143                         row = GBR_lp_next_row(lp);
144                         GBR_set(F_new, F_saved);
145                         fixed = fixed_saved;
146                         GBR_set(alpha, alpha_saved[i]);
147                 } else {
148                         row = GBR_lp_add_row(lp, B->row[1+i]+1, dim);
149                         GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
150                         ctx->stats->gbr_solved_lps++;
151                         unbounded = GBR_lp_solve(lp);
152                         isl_assert(ctx, !unbounded, goto error);
153                         GBR_lp_get_obj_val(lp, &F_new);
154                         fixed = GBR_lp_is_fixed(lp);
155
156                         GBR_lp_get_alpha(lp, row, &alpha);
157
158                         if (i > 0)
159                                 save_alpha(lp, row-i, i, alpha_saved);
160
161                         if (GBR_lp_del_row(lp) < 0)
162                                 goto error;
163                 }
164                 GBR_set(F[i+1], F_new);
165
166                 GBR_floor(mu[0], alpha);
167                 GBR_ceil(mu[1], alpha);
168
169                 if (isl_int_eq(mu[0], mu[1]))
170                         isl_int_set(tmp, mu[0]);
171                 else {
172                         int j;
173
174                         for (j = 0; j <= 1; ++j) {
175                                 isl_int_set(tmp, mu[j]);
176                                 isl_seq_combine(b_tmp->el,
177                                                 ctx->one, B->row[1+i+1]+1,
178                                                 tmp, B->row[1+i]+1, dim);
179                                 GBR_lp_set_obj(lp, b_tmp->el, dim);
180                                 ctx->stats->gbr_solved_lps++;
181                                 unbounded = GBR_lp_solve(lp);
182                                 isl_assert(ctx, !unbounded, goto error);
183                                 GBR_lp_get_obj_val(lp, &mu_F[j]);
184                                 mu_fixed[j] = GBR_lp_is_fixed(lp);
185                                 if (i > 0)
186                                         save_alpha(lp, row-i, i, alpha_buffer[j]);
187                         }
188
189                         if (GBR_lt(mu_F[0], mu_F[1]))
190                                 j = 0;
191                         else
192                                 j = 1;
193
194                         isl_int_set(tmp, mu[j]);
195                         GBR_set(F_new, mu_F[j]);
196                         fixed = mu_fixed[j];
197                         alpha_saved = alpha_buffer[j];
198                 }
199                 isl_seq_combine(B->row[1+i+1]+1, ctx->one, B->row[1+i+1]+1,
200                                 tmp, B->row[1+i]+1, dim);
201
202                 if (i+1 == tab->n_zero && fixed) {
203                         if (!GBR_is_zero(F[i+1])) {
204                                 empty = GBR_lp_cut(lp, B->row[1+i+1]+1);
205                                 if (empty)
206                                         goto done;
207                                 GBR_set_ui(F[i+1], 0);
208                         }
209                         tab->n_zero++;
210                 }
211
212                 GBR_set(F_old, F[i]);
213
214                 use_saved = 0;
215                 /* mu_F[0] = 4 * F_new; mu_F[1] = 3 * F_old */
216                 GBR_set_ui(mu_F[0], 4);
217                 GBR_mul(mu_F[0], mu_F[0], F_new);
218                 GBR_set_ui(mu_F[1], 3);
219                 GBR_mul(mu_F[1], mu_F[1], F_old);
220                 if (GBR_lt(mu_F[0], mu_F[1])) {
221                         B = isl_mat_swap_rows(B, 1 + i, 1 + i + 1);
222                         if (i > tab->n_zero) {
223                                 use_saved = 1;
224                                 GBR_set(F_saved, F_new);
225                                 fixed_saved = fixed;
226                                 if (GBR_lp_del_row(lp) < 0)
227                                         goto error;
228                                 --i;
229                         } else {
230                                 GBR_set(F[tab->n_zero], F_new);
231                                 if (ctx->gbr_only_first && GBR_lt(F[tab->n_zero], two))
232                                         break;
233
234                                 if (fixed) {
235                                         if (!GBR_is_zero(F[tab->n_zero])) {
236                                                 empty = GBR_lp_cut(lp, B->row[1+tab->n_zero]+1);
237                                                 if (empty)
238                                                         goto done;
239                                                 GBR_set_ui(F[tab->n_zero], 0);
240                                         }
241                                         tab->n_zero++;
242                                 }
243                         }
244                 } else {
245                         GBR_lp_add_row(lp, B->row[1+i]+1, dim);
246                         ++i;
247                 }
248         } while (i < n_bounded - 1);
249
250         if (0) {
251 done:
252                 if (empty < 0) {
253 error:
254                         isl_mat_free(B);
255                         B = NULL;
256                 }
257         }
258
259         GBR_lp_delete(lp);
260
261         if (alpha_buffer[1])
262                 for (i = 0; i < n_bounded; ++i) {
263                         GBR_clear(F[i]);
264                         GBR_clear(alpha_buffer[0][i]);
265                         GBR_clear(alpha_buffer[1][i]);
266                 }
267         free(F);
268         free(alpha_buffer[0]);
269         free(alpha_buffer[1]);
270
271         isl_vec_free(b_tmp);
272
273         GBR_clear(alpha);
274         GBR_clear(F_old);
275         GBR_clear(F_new);
276         GBR_clear(F_saved);
277         GBR_clear(mu_F[0]);
278         GBR_clear(mu_F[1]);
279         GBR_clear(two);
280         GBR_clear(one);
281
282         isl_int_clear(tmp);
283         isl_int_clear(mu[0]);
284         isl_int_clear(mu[1]);
285
286         tab->basis = B;
287
288         return tab;
289 }
290
291 struct isl_mat *isl_basic_set_reduced_basis(struct isl_basic_set *bset)
292 {
293         struct isl_mat *basis;
294         struct isl_tab *tab;
295
296         isl_assert(bset->ctx, bset->n_eq == 0, return NULL);
297
298         tab = isl_tab_from_basic_set(bset);
299         tab->basis = isl_mat_identity(bset->ctx, 1 + tab->n_var);
300         tab = isl_tab_compute_reduced_basis(tab);
301         if (!tab)
302                 return NULL;
303
304         basis = isl_mat_copy(tab->basis);
305
306         isl_tab_free(tab);
307
308         return basis;
309 }