2 #include <barvinok/util.h>
3 #include <barvinok/basis_reduction.h>
4 #include <barvinok/sample.h>
5 #include <barvinok/options.h>
8 #define ALLOCN(type,n) (type*)malloc((n) * sizeof(type))
10 /* If P has no rays, then we return NULL.
11 * Otherwise, look for the coordinate axis with the smallest maximal non-zero
12 * coefficient over all rays and a constraint that bounds the values on
13 * this axis to the maximal value over the vertices plus the above maximal
14 * non-zero coefficient times the number of rays minus 1.
15 * Any integer point x outside this region is the sum of a point inside
16 * the region and an integer multiple of the rays.
17 * Write x = \sum_i a_i v_i + \sum_j b_j r_j
18 * with \sum_i a_i = 1.
19 * Then x = \sum_i a_i v_i + \sum_j {b_j} r_j + \sum_j [b_j] r_j
20 * with y = \sum_i a_i v_i + \sum_j {b_j} r_j a point inside the region.
22 static Polyhedron
*remove_ray(Polyhedron
*P
, unsigned MaxRays
)
25 Vector
*min
, *max
, *c
;
32 POL_ENSURE_VERTICES(P
);
34 for (; r
< P
->NbRays
; ++r
)
35 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
37 if (P
->NbBid
== 0 && r
== P
->NbRays
)
40 max
= Vector_Alloc(P
->Dimension
);
41 min
= Vector_Alloc(P
->Dimension
);
42 for (r
= 0; r
< P
->NbBid
; ++r
)
43 for (i
= 0 ; i
< P
->Dimension
; ++i
)
44 if (value_abs_gt(P
->Ray
[r
][1+i
], max
->p
[i
]))
45 value_absolute(max
->p
[i
], P
->Ray
[r
][1+i
]);
47 for (i
= 0 ; i
< P
->Dimension
; ++i
)
48 value_oppose(min
->p
[i
], max
->p
[i
]);
51 for (r
= P
->NbBid
; r
< P
->NbRays
; ++r
) {
52 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
54 for (i
= 0 ; i
< P
->Dimension
; ++i
) {
55 if (value_gt(P
->Ray
[r
][1+i
], max
->p
[i
]))
56 value_assign(max
->p
[i
], P
->Ray
[r
][1+i
]);
57 if (value_lt(P
->Ray
[r
][1+i
], min
->p
[i
]))
58 value_assign(min
->p
[i
], P
->Ray
[r
][1+i
]);
67 for (i
= 0 ; i
< P
->Dimension
; ++i
) {
68 if (value_notzero_p(min
->p
[i
]) &&
69 (value_zero_p(s
) || value_abs_lt(min
->p
[i
], s
))) {
70 value_assign(s
, min
->p
[i
]);
73 if (value_notzero_p(max
->p
[i
]) &&
74 (value_zero_p(s
) || value_abs_lt(max
->p
[i
], s
))) {
75 value_assign(s
, max
->p
[i
]);
80 for (r
= P
->NbBid
; r
< P
->NbRays
; ++r
)
81 if (value_notzero_p(P
->Ray
[r
][P
->Dimension
+1]))
85 mpz_cdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][P
->Dimension
+1]);
87 mpz_fdiv_q(v
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][P
->Dimension
+1]);
89 for ( ; r
< P
->NbRays
; ++r
) {
90 if (value_zero_p(P
->Ray
[r
][P
->Dimension
+1]))
94 mpz_cdiv_q(tmp
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][P
->Dimension
+1]);
98 mpz_fdiv_q(tmp
, P
->Ray
[r
][1+pos
], P
->Ray
[r
][P
->Dimension
+1]);
100 value_assign(v
, tmp
);
104 c
= Vector_Alloc(1+P
->Dimension
+1);
106 value_set_si(tmp
, rays
);
107 value_addmul(v
, tmp
, s
);
108 value_set_si(c
->p
[0], 1);
109 if (value_pos_p(s
)) {
110 value_set_si(c
->p
[1+pos
], -1);
111 value_assign(c
->p
[1+P
->Dimension
], v
);
113 value_set_si(c
->p
[1+pos
], 1);
114 value_oppose(c
->p
[1+P
->Dimension
], v
);
116 value_decrement(c
->p
[1+P
->Dimension
], c
->p
[1+P
->Dimension
]);
118 R
= AddConstraints(c
->p
, 1, P
, MaxRays
);
132 static void print_minmax(Polyhedron
*P
)
135 POL_ENSURE_VERTICES(P
);
136 Polyhedron_Print(stderr
, P_VALUE_FMT
, P
);
137 for (i
= 0; i
< P
->Dimension
; ++i
) {
143 mpz_cdiv_q(min
, P
->Ray
[0][1+i
], P
->Ray
[0][1+P
->Dimension
]);
144 mpz_fdiv_q(max
, P
->Ray
[0][1+i
], P
->Ray
[0][1+P
->Dimension
]);
146 for (j
= 1; j
< P
->NbRays
; ++j
) {
147 mpz_cdiv_q(tmp
, P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
148 if (value_lt(tmp
, min
))
149 value_assign(min
, tmp
);
150 mpz_fdiv_q(tmp
, P
->Ray
[j
][1+i
], P
->Ray
[j
][1+P
->Dimension
]);
151 if (value_gt(tmp
, max
))
152 value_assign(max
, tmp
);
154 fprintf(stderr
, "i: %d, min: ", i
);
155 value_print(stderr
, VALUE_FMT
, min
);
156 fprintf(stderr
, ", max: ");
157 value_print(stderr
, VALUE_FMT
, max
);
158 fprintf(stderr
, "\n");
166 /* Remove coordinates that have a fixed value and return the matrix
167 * that adds these fixed coordinates again through T.
169 static Polyhedron
*Polyhedron_RemoveFixedColumns(Polyhedron
*P
, Matrix
**T
)
172 int dim
= P
->Dimension
;
173 int *remove
= ALLOCN(int, dim
);
177 assert(POL_HAS(P
, POL_INEQUALITIES
));
178 for (i
= 0; i
< dim
; ++i
)
181 for (i
= 0; i
< P
->NbEq
; ++i
) {
182 int pos
= First_Non_Zero(P
->Constraint
[i
]+1, dim
);
183 if (First_Non_Zero(P
->Constraint
[i
]+1+pos
+1, dim
-pos
-1) != -1)
189 Q
= Polyhedron_Alloc(P
->Dimension
-NbEq
, P
->NbConstraints
-NbEq
, P
->NbRays
);
190 Q
->NbEq
= P
->NbEq
- NbEq
;
191 for (i
= 0, k
= 0; i
< P
->NbConstraints
; ++i
) {
193 int pos
= First_Non_Zero(P
->Constraint
[i
]+1, dim
);
194 if (First_Non_Zero(P
->Constraint
[i
]+1+pos
+1, dim
-pos
-1) == -1)
197 value_assign(Q
->Constraint
[k
][0], P
->Constraint
[i
][0]);
198 for (j
= 0, n
= 0; j
< P
->Dimension
; ++j
) {
202 value_assign(Q
->Constraint
[k
][1+j
-n
], P
->Constraint
[i
][1+j
]);
204 value_assign(Q
->Constraint
[k
][1+j
-n
], P
->Constraint
[i
][1+j
]);
207 for (i
= 0; i
< Q
->NbRays
; ++i
) {
208 value_assign(Q
->Ray
[i
][0], P
->Ray
[i
][0]);
209 for (j
= 0, n
= 0; j
< P
->Dimension
; ++j
) {
213 value_assign(Q
->Ray
[i
][1+j
-n
], P
->Ray
[i
][1+j
]);
215 value_assign(Q
->Ray
[i
][1+j
-n
], P
->Ray
[i
][1+j
]);
217 *T
= Matrix_Alloc(P
->Dimension
+1, Q
->Dimension
+1);
218 for (i
= 0, n
= 0; i
< P
->Dimension
; ++i
) {
219 if (remove
[i
] != -1) {
220 value_oppose((*T
)->p
[i
][Q
->Dimension
],
221 P
->Constraint
[remove
[i
]][1+P
->Dimension
]);
224 value_set_si((*T
)->p
[i
][i
-n
], 1);
226 value_set_si((*T
)->p
[i
][i
-n
], 1);
227 POL_SET(Q
, POL_VALID
);
228 if (POL_HAS(P
, POL_INEQUALITIES
))
229 POL_SET(Q
, POL_INEQUALITIES
);
230 if (POL_HAS(P
, POL_FACETS
))
231 POL_SET(Q
, POL_FACETS
);
232 if (POL_HAS(P
, POL_POINTS
))
233 POL_SET(Q
, POL_POINTS
);
234 if (POL_HAS(P
, POL_VERTICES
))
235 POL_SET(Q
, POL_VERTICES
);
240 static Polyhedron
*remove_all_equalities(Polyhedron
*P
, Matrix
**T
,
244 Polyhedron_Matrix_View(P
, &M
, P
->NbEq
);
246 *T
= compress_variables(&M
, 0);
250 P
= Polyhedron_Preimage(P
, *T
, MaxRays
);
255 static Vector
*product_sample(Polyhedron
*P
, Matrix
*T
,
256 struct barvinok_options
*options
)
259 Vector
*sample
= NULL
;
260 Vector
*tmp
= Vector_Alloc(T
->NbRows
);
262 for (; P
; P
= P
->next
) {
264 Polyhedron
*next
= P
->next
;
266 P_sample
= Polyhedron_Sample(P
, options
);
273 Vector_Copy(P_sample
->p
, tmp
->p
+i
, P
->Dimension
);
274 Vector_Free(P_sample
);
278 sample
= Vector_Alloc(T
->NbRows
+ 1);
279 Matrix_Vector_Product(T
, tmp
->p
, sample
->p
);
280 value_set_si(sample
->p
[T
->NbRows
], 1);
286 /* This function implements the algorithm described in
287 * "An Implementation of the Generalized Basis Reduction Algorithm
288 * for Integer Programming" of Cook el al. to find an integer point
290 * If the polyhedron is unbounded, we first remove its rays.
292 Vector
*Polyhedron_Sample(Polyhedron
*P
, struct barvinok_options
*options
)
295 Vector
*sample
= NULL
, *obj
= NULL
;
307 if (P
->Dimension
== 0) {
308 sample
= Vector_Alloc(1);
309 value_set_si(sample
->p
[0], 1);
313 if (P
->Dimension
== 1)
314 POL_ENSURE_VERTICES(P
);
316 for (i
= 0; i
< P
->NbRays
; ++i
)
317 if (value_one_p(P
->Ray
[i
][1+P
->Dimension
])) {
318 sample
= Vector_Alloc(P
->Dimension
+1);
319 Vector_Copy(P
->Ray
[i
]+1, sample
->p
, P
->Dimension
+1);
325 Polyhedron
*Q
= remove_all_equalities(P
, &T
, options
->MaxRays
);
328 Q_sample
= Polyhedron_Sample(Q
, options
);
331 sample
= Vector_Alloc(P
->Dimension
+ 1);
332 Matrix_Vector_Product(T
, Q_sample
->p
, sample
->p
);
333 Vector_Free(Q_sample
);
339 Q
= Polyhedron_Factor(P
, 0, &T
, options
->MaxRays
);
341 sample
= product_sample(Q
, T
, options
);
350 obj
= Vector_Alloc(P
->Dimension
+1);
351 value_set_si(obj
->p
[0], 1);
352 res
= polyhedron_range(P
, obj
->p
, obj
->p
[0], &min
, &max
, options
);
353 if (res
== lp_unbounded
) {
359 Q
= remove_ray(P
, options
->MaxRays
);
361 sample
= Polyhedron_Sample(Q
, options
);
367 assert(res
== lp_ok
);
369 if (value_eq(min
, max
)) {
375 options
->gbr_only_first
= 1;
376 basis
= Polyhedron_Reduced_Basis(P
, options
);
377 options
->gbr_only_first
= 0;
381 T
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
382 inv
= Matrix_Alloc(P
->Dimension
+1, P
->Dimension
+1);
383 for (i
= 0; i
< P
->Dimension
; ++i
)
384 for (j
= 0; j
< P
->Dimension
; ++j
)
385 value_assign(T
->p
[i
][j
], basis
->p
[i
][j
]);
386 value_set_si(T
->p
[P
->Dimension
][P
->Dimension
], 1);
390 ok
= Matrix_Inverse(M
, inv
);
394 Q
= Polyhedron_Image(P
, T
, options
->MaxRays
);
395 res
= polyhedron_range(Q
, obj
->p
, obj
->p
[0], &min
, &max
, options
);
400 assert(res
== lp_ok
);
405 v
= Vector_Alloc(1+Q
->Dimension
+1);
406 value_set_si(v
->p
[1], -1);
408 for (value_assign(tmp
, min
); value_le(tmp
, max
); value_increment(tmp
, tmp
)) {
412 value_assign(v
->p
[1+Q
->Dimension
], tmp
);
414 R
= AddConstraints(v
->p
, 1, Q
, options
->MaxRays
);
415 R
= DomainConstraintSimplify(R
, options
->MaxRays
);
421 S
= Polyhedron_RemoveFixedColumns(R
, &T
);
423 S_sample
= Polyhedron_Sample(S
, options
);
426 Vector
*Q_sample
= obj
;
428 Matrix_Vector_Product(T
, S_sample
->p
, Q_sample
->p
);
430 Vector_Free(S_sample
);
434 sample
= Vector_Alloc(P
->Dimension
+ 1);
435 Matrix_Vector_Product(inv
, Q_sample
->p
, sample
->p
);
436 Vector_Free(Q_sample
);