Polyhedron_Sample: postpone removal of rays until we know domain is unbounded
[barvinok.git] / sample.c
blobd49a941bb5a8ff3e720881931039c704983624ba
1 #include <barvinok/util.h>
2 #include <barvinok/basis_reduction.h>
3 #include <barvinok/sample.h>
4 #include <barvinok/options.h>
5 #include "polysign.h"
7 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
9 /* If P has no rays, then we return NULL.
10 * Otherwise, look for the coordinate axis with the smallest maximal non-zero
11 * coefficient over all rays and a constraint that bounds the values on
12 * this axis to the maximal value over the vertices plus the above maximal
13 * non-zero coefficient times the number of rays minus 1.
14 * Any integer point x outside this region is the sum of a point inside
15 * the region and an integer multiple of the rays.
16 * Write x = \sum_i a_i v_i + \sum_j b_j r_j
17 * with \sum_i a_i = 1.
18 * Then x = \sum_i a_i v_i + \sum_j {b_j} r_j + \sum_j [b_j] r_j
19 * with y = \sum_i a_i v_i + \sum_j {b_j} r_j a point inside the region.
21 static Polyhedron *remove_ray(Polyhedron *P, unsigned MaxRays)
23 int r = 0;
24 Vector *min, *max, *c;
25 int i;
26 Value s, v, tmp;
27 int pos;
28 Polyhedron *R;
29 int rays;
31 POL_ENSURE_VERTICES(P);
32 if (P->NbBid == 0)
33 for (; r < P->NbRays; ++r)
34 if (value_zero_p(P->Ray[r][P->Dimension+1]))
35 break;
36 if (P->NbBid == 0 && r == P->NbRays)
37 return NULL;
39 max = Vector_Alloc(P->Dimension);
40 min = Vector_Alloc(P->Dimension);
41 for (r = 0; r < P->NbBid; ++r)
42 for (i = 0 ; i < P->Dimension; ++i)
43 if (value_abs_gt(P->Ray[r][1+i], max->p[i]))
44 value_absolute(max->p[i], P->Ray[r][1+i]);
46 for (i = 0 ; i < P->Dimension; ++i)
47 value_oppose(min->p[i], max->p[i]);
49 rays = P->NbBid;
50 for (r = P->NbBid; r < P->NbRays; ++r) {
51 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
52 continue;
53 for (i = 0 ; i < P->Dimension; ++i) {
54 if (value_gt(P->Ray[r][1+i], max->p[i]))
55 value_assign(max->p[i], P->Ray[r][1+i]);
56 if (value_lt(P->Ray[r][1+i], min->p[i]))
57 value_assign(min->p[i], P->Ray[r][1+i]);
59 ++rays;
62 value_init(s);
63 value_init(v);
64 value_init(tmp);
66 for (i = 0 ; i < P->Dimension; ++i) {
67 if (value_notzero_p(min->p[i]) &&
68 (value_zero_p(s) || value_abs_lt(min->p[i], s))) {
69 value_assign(s, min->p[i]);
70 pos = i;
72 if (value_notzero_p(max->p[i]) &&
73 (value_zero_p(s) || value_abs_lt(max->p[i], s))) {
74 value_assign(s, max->p[i]);
75 pos = i;
79 for (r = P->NbBid; r < P->NbRays; ++r)
80 if (value_notzero_p(P->Ray[r][P->Dimension+1]))
81 break;
83 if (value_pos_p(s))
84 mpz_cdiv_q(v, P->Ray[r][1+pos], P->Ray[r][P->Dimension+1]);
85 else
86 mpz_fdiv_q(v, P->Ray[r][1+pos], P->Ray[r][P->Dimension+1]);
88 for ( ; r < P->NbRays; ++r) {
89 if (value_zero_p(P->Ray[r][P->Dimension+1]))
90 continue;
92 if (value_pos_p(s)) {
93 mpz_cdiv_q(tmp, P->Ray[r][1+pos], P->Ray[r][P->Dimension+1]);
94 if (value_gt(tmp, v))
95 value_assign(v, tmp);
96 } else {
97 mpz_fdiv_q(tmp, P->Ray[r][1+pos], P->Ray[r][P->Dimension+1]);
98 if (value_lt(tmp, v))
99 value_assign(v, tmp);
103 c = Vector_Alloc(1+P->Dimension+1);
105 value_set_si(tmp, rays);
106 value_addmul(v, tmp, s);
107 value_set_si(c->p[0], 1);
108 if (value_pos_p(s)) {
109 value_set_si(c->p[1+pos], -1);
110 value_assign(c->p[1+P->Dimension], v);
111 } else {
112 value_set_si(c->p[1+pos], 1);
113 value_oppose(c->p[1+P->Dimension], v);
115 value_decrement(c->p[1+P->Dimension], c->p[1+P->Dimension]);
117 R = AddConstraints(c->p, 1, P, MaxRays);
119 Vector_Free(c);
121 Vector_Free(min);
122 Vector_Free(max);
124 value_clear(tmp);
125 value_clear(s);
126 value_clear(v);
128 return R;
131 static void print_minmax(Polyhedron *P)
133 int i, j;
134 POL_ENSURE_VERTICES(P);
135 Polyhedron_Print(stderr, P_VALUE_FMT, P);
136 for (i = 0; i < P->Dimension; ++i) {
137 Value min, max, tmp;
138 value_init(min);
139 value_init(max);
140 value_init(tmp);
142 mpz_cdiv_q(min, P->Ray[0][1+i], P->Ray[0][1+P->Dimension]);
143 mpz_fdiv_q(max, P->Ray[0][1+i], P->Ray[0][1+P->Dimension]);
145 for (j = 1; j < P->NbRays; ++j) {
146 mpz_cdiv_q(tmp, P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
147 if (value_lt(tmp, min))
148 value_assign(min, tmp);
149 mpz_fdiv_q(tmp, P->Ray[j][1+i], P->Ray[j][1+P->Dimension]);
150 if (value_gt(tmp, max))
151 value_assign(max, tmp);
153 fprintf(stderr, "i: %d, min: ", i);
154 value_print(stderr, VALUE_FMT, min);
155 fprintf(stderr, ", max: ");
156 value_print(stderr, VALUE_FMT, max);
157 fprintf(stderr, "\n");
159 value_clear(min);
160 value_clear(max);
161 value_clear(tmp);
165 /* Remove coordinates that have a fixed value and return the matrix
166 * that adds these fixed coordinates again through T.
168 static Polyhedron *Polyhedron_RemoveFixedColumns(Polyhedron *P, Matrix **T)
170 int i, j, k, n;
171 int dim = P->Dimension;
172 int *remove = ALLOCN(int, dim);
173 Polyhedron *Q;
174 int NbEq;
176 assert(POL_HAS(P, POL_INEQUALITIES));
177 for (i = 0; i < dim; ++i)
178 remove[i] = -1;
179 NbEq = 0;
180 for (i = 0; i < P->NbEq; ++i) {
181 int pos = First_Non_Zero(P->Constraint[i]+1, dim);
182 if (First_Non_Zero(P->Constraint[i]+1+pos+1, dim-pos-1) != -1)
183 continue;
184 remove[pos] = i;
185 ++NbEq;
187 assert(NbEq > 0);
188 Q = Polyhedron_Alloc(P->Dimension-NbEq, P->NbConstraints-NbEq, P->NbRays);
189 for (i = 0, k = 0; i < P->NbConstraints; ++i) {
190 if (i < P->NbEq) {
191 int pos = First_Non_Zero(P->Constraint[i]+1, dim);
192 if (First_Non_Zero(P->Constraint[i]+1+pos+1, dim-pos-1) == -1)
193 continue;
195 value_assign(Q->Constraint[k][0], P->Constraint[i][0]);
196 for (j = 0, n = 0; j < P->Dimension; ++j) {
197 if (remove[j] != -1)
198 ++n;
199 else
200 value_assign(Q->Constraint[k][1+j-n], P->Constraint[i][1+j]);
202 value_assign(Q->Constraint[k][1+j-n], P->Constraint[i][1+j]);
203 ++k;
205 for (i = 0; i < Q->NbRays; ++i) {
206 value_assign(Q->Ray[i][0], P->Ray[i][0]);
207 for (j = 0, n = 0; j < P->Dimension; ++j) {
208 if (remove[j] != -1)
209 ++n;
210 else
211 value_assign(Q->Ray[i][1+j-n], P->Ray[i][1+j]);
213 value_assign(Q->Ray[i][1+j-n], P->Ray[i][1+j]);
215 *T = Matrix_Alloc(P->Dimension+1, Q->Dimension+1);
216 for (i = 0, n = 0; i < P->Dimension; ++i) {
217 if (remove[i] != -1) {
218 value_oppose((*T)->p[i][Q->Dimension],
219 P->Constraint[remove[i]][1+P->Dimension]);
220 ++n;
221 } else
222 value_set_si((*T)->p[i][i-n], 1);
224 value_set_si((*T)->p[i][i-n], 1);
225 POL_SET(Q, POL_VALID);
226 if (POL_HAS(P, POL_INEQUALITIES))
227 POL_SET(Q, POL_INEQUALITIES);
228 if (POL_HAS(P, POL_FACETS))
229 POL_SET(Q, POL_FACETS);
230 if (POL_HAS(P, POL_POINTS))
231 POL_SET(Q, POL_POINTS);
232 if (POL_HAS(P, POL_VERTICES))
233 POL_SET(Q, POL_VERTICES);
234 free(remove);
235 return Q;
238 /* This function implements the algorithm described in
239 * "An Implementation of the Generalized Basis Reduction Algorithm
240 * for Integer Programming" of Cook el al. to find an integer point
241 * in a polyhedron.
242 * If the polyhedron is unbounded, we first remove its rays.
244 Vector *Polyhedron_Sample(Polyhedron *P, struct barvinok_options *options)
246 int i, j;
247 Vector *sample = NULL, *obj = NULL;
248 Polyhedron *Q;
249 Matrix *inv = NULL;
250 Value min, max, tmp;
251 Vector *v;
252 int ok;
253 enum lp_result res;
255 if (emptyQ2(P))
256 return NULL;
258 if (P->Dimension == 0) {
259 sample = Vector_Alloc(1);
260 value_set_si(sample->p[0], 1);
261 return sample;
264 for (i = 0; i < P->NbRays; ++i)
265 if (value_one_p(P->Ray[i][1+P->Dimension])) {
266 sample = Vector_Alloc(P->Dimension+1);
267 Vector_Copy(P->Ray[i]+1, sample->p, P->Dimension+1);
268 return sample;
271 value_init(min);
272 value_init(max);
274 obj = Vector_Alloc(P->Dimension+1);
275 value_set_si(obj->p[0], 1);
276 res = polyhedron_range(P, obj->p, obj->p[0], &min, &max, options);
277 if (res == lp_unbounded) {
278 unbounded:
279 value_clear(min);
280 value_clear(max);
281 Vector_Free(obj);
283 Q = remove_ray(P, options->MaxRays);
284 assert(Q);
285 sample = Polyhedron_Sample(Q, options);
286 Polyhedron_Free(Q);
287 return sample;
289 if (res == lp_empty) {
290 value_clear(min);
291 value_clear(max);
292 Vector_Free(obj);
293 return NULL;
295 assert(res == lp_ok);
297 if (value_eq(min, max)) {
298 Q = P;
299 } else {
300 Matrix *M, *T;
301 Matrix *basis = Polyhedron_Reduced_Basis(P, options);
303 if (!basis)
304 goto unbounded;
305 T = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
306 inv = Matrix_Alloc(P->Dimension+1, P->Dimension+1);
307 for (i = 0; i < P->Dimension; ++i)
308 for (j = 0; j < P->Dimension; ++j)
309 value_assign(T->p[i][j], basis->p[i][j]);
310 value_set_si(T->p[P->Dimension][P->Dimension], 1);
311 Matrix_Free(basis);
313 M = Matrix_Copy(T);
314 ok = Matrix_Inverse(M, inv);
315 assert(ok);
316 Matrix_Free(M);
318 Q = Polyhedron_Image(P, T, options->MaxRays);
319 res = polyhedron_range(Q, obj->p, obj->p[0], &min, &max, options);
320 assert(res == lp_ok);
322 Matrix_Free(T);
325 value_init(tmp);
327 v = Vector_Alloc(1+Q->Dimension+1);
328 value_set_si(v->p[1], -1);
330 for (value_assign(tmp, min); value_le(tmp, max); value_increment(tmp, tmp)) {
331 Polyhedron *R, *S;
332 Matrix *T;
333 Vector *S_sample;
334 value_assign(v->p[1+Q->Dimension], tmp);
336 R = AddConstraints(v->p, 1, Q, options->MaxRays);
337 R = DomainConstraintSimplify(R, options->MaxRays);
338 if (emptyQ(R)) {
339 Polyhedron_Free(R);
340 continue;
343 S = Polyhedron_RemoveFixedColumns(R, &T);
344 Polyhedron_Free(R);
345 S_sample = Polyhedron_Sample(S, options);
346 Polyhedron_Free(S);
347 if (S_sample) {
348 Vector *Q_sample = obj;
349 obj = NULL;
350 Matrix_Vector_Product(T, S_sample->p, Q_sample->p);
351 Matrix_Free(T);
352 Vector_Free(S_sample);
353 if (!inv)
354 sample = Q_sample;
355 else {
356 sample = Vector_Alloc(P->Dimension + 1);
357 Matrix_Vector_Product(inv, Q_sample->p, sample->p);
358 Vector_Free(Q_sample);
360 break;
362 Matrix_Free(T);
365 if (obj)
366 Vector_Free(obj);
367 if (inv)
368 Matrix_Free(inv);
369 if (P != Q)
370 Polyhedron_Free(Q);
371 Vector_Free(v);
373 value_clear(min);
374 value_clear(max);
375 value_clear(tmp);
377 return sample;