2 #include "isl_map_private.h"
3 #include "isl_sample.h"
5 #include "isl_equalities.h"
7 /* Given a basic set "bset", construct a basic set U such that for
8 * each element x in U, the whole unit box positioned at x is inside
10 * Note that U may not contain all points that satisfy this property.
12 * We simply add the sum of all negative coefficients to the constant
13 * term. This ensures that if x satisfies the resulting constraints,
14 * then x plus any sum of unit vectors satisfies the original constraints.
16 static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
19 struct isl_basic_set *unit_box = NULL;
25 if (bset->n_eq != 0) {
26 unit_box = isl_basic_set_empty_like(bset);
27 isl_basic_set_free(bset);
31 total = isl_basic_set_total_dim(bset);
32 unit_box = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset),
35 for (i = 0; i < bset->n_ineq; ++i) {
36 k = isl_basic_set_alloc_inequality(unit_box);
39 isl_seq_cpy(unit_box->ineq[k], bset->ineq[i], 1 + total);
40 for (j = 0; j < total; ++j) {
41 if (isl_int_is_nonneg(unit_box->ineq[k][1 + j]))
43 isl_int_add(unit_box->ineq[k][0],
44 unit_box->ineq[k][0], unit_box->ineq[k][1 + j]);
48 isl_basic_set_free(bset);
51 isl_basic_set_free(bset);
52 isl_basic_set_free(unit_box);
56 /* Find an integer point in "bset", preferably one that is
57 * close to minimizing "f".
59 * We first check if we can easily put unit boxes inside bset.
60 * If so, we take the best base point of any of the unit boxes we can find
61 * and round it up to the nearest integer.
62 * If not, we simply pick any integer point in "bset".
64 static struct isl_vec *initial_solution(struct isl_basic_set *bset, isl_int *f)
66 enum isl_lp_result res;
67 struct isl_basic_set *unit_box;
70 unit_box = unit_box_base_points(isl_basic_set_copy(bset));
72 res = isl_basic_set_solve_lp(unit_box, 0, f, bset->ctx->one,
74 if (res == isl_lp_ok) {
75 isl_basic_set_free(unit_box);
76 return isl_vec_ceil(sol);
79 isl_basic_set_free(unit_box);
81 return isl_basic_set_sample_vec(isl_basic_set_copy(bset));
84 /* Restrict "bset" to those points with values for f in the interval [l, u].
86 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
87 isl_int *f, isl_int l, isl_int u)
92 total = isl_basic_set_total_dim(bset);
93 bset = isl_basic_set_extend_constraints(bset, 0, 2);
95 k = isl_basic_set_alloc_inequality(bset);
98 isl_seq_cpy(bset->ineq[k], f, 1 + total);
99 isl_int_sub(bset->ineq[k][0], bset->ineq[k][0], l);
101 k = isl_basic_set_alloc_inequality(bset);
104 isl_seq_neg(bset->ineq[k], f, 1 + total);
105 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], u);
109 isl_basic_set_free(bset);
113 /* Find an integer point in "bset" that minimizes f (if any).
114 * If sol_p is not NULL then the integer point is returned in *sol_p.
115 * The optimal value of f is returned in *opt.
117 * The algorithm maintains a currently best solution and an interval [l, u]
118 * of values of f for which integer solutions could potentially still be found.
119 * The initial value of the best solution so far is any solution.
120 * The initial value of l is minimal value of f over the rationals
121 * (rounded up to the nearest integer).
122 * The initial value of u is the value of f at the current solution minus 1.
124 * We perform a number of steps until l > u.
125 * In each step, we look for an integer point with value in either
126 * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
127 * The choice depends on whether we have found an integer point in the
128 * previous step. If so, we look for the next point in half of the remaining
130 * If we find a point, the current solution is updated and u is set
131 * to its value minus 1.
132 * If no point can be found, we update l to the upper bound of the interval
133 * we checked (u or l+floor(u-l-1/2)) plus 1.
135 static enum isl_lp_result solve_ilp(struct isl_basic_set *bset,
136 isl_int *f, isl_int *opt,
137 struct isl_vec **sol_p)
139 enum isl_lp_result res;
144 res = isl_basic_set_solve_lp(bset, 0, f, bset->ctx->one,
146 if (res == isl_lp_ok && isl_int_is_one(sol->el[0])) {
154 if (res == isl_lp_error || res == isl_lp_empty)
157 sol = initial_solution(bset, f);
160 if (sol->size == 0) {
164 if (res == isl_lp_unbounded) {
166 return isl_lp_unbounded;
173 isl_int_set(l, *opt);
175 isl_seq_inner_product(f, sol->el, sol->size, opt);
176 isl_int_sub_ui(u, *opt, 1);
178 while (isl_int_le(l, u)) {
179 struct isl_basic_set *slice;
180 struct isl_vec *sample;
185 isl_int_sub(tmp, u, l);
186 isl_int_fdiv_q_ui(tmp, tmp, 2);
187 isl_int_add(tmp, tmp, l);
189 slice = add_bounds(isl_basic_set_copy(bset), f, l, tmp);
190 sample = isl_basic_set_sample_vec(slice);
197 if (sample->size > 0) {
200 isl_seq_inner_product(f, sol->el, sol->size, opt);
201 isl_int_sub_ui(u, *opt, 1);
204 isl_vec_free(sample);
207 isl_int_add_ui(l, tmp, 1);
224 static enum isl_lp_result solve_ilp_with_eq(struct isl_basic_set *bset, int max,
225 isl_int *f, isl_int *opt,
226 struct isl_vec **sol_p)
229 enum isl_lp_result res;
230 struct isl_mat *T = NULL;
233 dim = isl_basic_set_total_dim(bset);
234 v = isl_vec_alloc(bset->ctx, 1 + dim);
237 isl_seq_cpy(v->el, f, 1 + dim);
238 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
239 v = isl_vec_mat_product(v, isl_mat_copy(T));
242 res = isl_basic_set_solve_ilp(bset, max, v->el, opt, sol_p);
244 if (res == isl_lp_ok && *sol_p) {
245 *sol_p = isl_mat_vec_product(T, *sol_p);
253 isl_basic_set_free(bset);
257 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
259 * If sol_p is not NULL then the integer point is returned in *sol_p.
260 * The optimal value of f is returned in *opt.
262 * If there is any equality among the points in "bset", then we first
263 * project it out. Otherwise, we continue with solve_ilp above.
265 enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max,
266 isl_int *f, isl_int *opt,
267 struct isl_vec **sol_p)
270 enum isl_lp_result res;
277 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
280 return solve_ilp_with_eq(bset, max, f, opt, sol_p);
282 dim = isl_basic_set_total_dim(bset);
285 isl_seq_neg(f, f, 1 + dim);
287 res = solve_ilp(bset, f, opt, sol_p);
290 isl_seq_neg(f, f, 1 + dim);
291 isl_int_neg(*opt, *opt);
296 isl_basic_set_free(bset);