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