2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the GNU LGPLv2.1 license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
11 #include "isl_map_private.h"
12 #include "isl_sample.h"
14 #include "isl_equalities.h"
16 /* Given a basic set "bset", construct a basic set U such that for
17 * each element x in U, the whole unit box positioned at x is inside
18 * the given basic set.
19 * Note that U may not contain all points that satisfy this property.
21 * We simply add the sum of all negative coefficients to the constant
22 * term. This ensures that if x satisfies the resulting constraints,
23 * then x plus any sum of unit vectors satisfies the original constraints.
25 static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
28 struct isl_basic_set *unit_box = NULL;
34 if (bset->n_eq != 0) {
35 unit_box = isl_basic_set_empty_like(bset);
36 isl_basic_set_free(bset);
40 total = isl_basic_set_total_dim(bset);
41 unit_box = isl_basic_set_alloc_dim(isl_basic_set_get_dim(bset),
44 for (i = 0; i < bset->n_ineq; ++i) {
45 k = isl_basic_set_alloc_inequality(unit_box);
48 isl_seq_cpy(unit_box->ineq[k], bset->ineq[i], 1 + total);
49 for (j = 0; j < total; ++j) {
50 if (isl_int_is_nonneg(unit_box->ineq[k][1 + j]))
52 isl_int_add(unit_box->ineq[k][0],
53 unit_box->ineq[k][0], unit_box->ineq[k][1 + j]);
57 isl_basic_set_free(bset);
60 isl_basic_set_free(bset);
61 isl_basic_set_free(unit_box);
65 /* Find an integer point in "bset", preferably one that is
66 * close to minimizing "f".
68 * We first check if we can easily put unit boxes inside bset.
69 * If so, we take the best base point of any of the unit boxes we can find
70 * and round it up to the nearest integer.
71 * If not, we simply pick any integer point in "bset".
73 static struct isl_vec *initial_solution(struct isl_basic_set *bset, isl_int *f)
75 enum isl_lp_result res;
76 struct isl_basic_set *unit_box;
79 unit_box = unit_box_base_points(isl_basic_set_copy(bset));
81 res = isl_basic_set_solve_lp(unit_box, 0, f, bset->ctx->one,
83 if (res == isl_lp_ok) {
84 isl_basic_set_free(unit_box);
85 return isl_vec_ceil(sol);
88 isl_basic_set_free(unit_box);
90 return isl_basic_set_sample_vec(isl_basic_set_copy(bset));
93 /* Restrict "bset" to those points with values for f in the interval [l, u].
95 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
96 isl_int *f, isl_int l, isl_int u)
101 total = isl_basic_set_total_dim(bset);
102 bset = isl_basic_set_extend_constraints(bset, 0, 2);
104 k = isl_basic_set_alloc_inequality(bset);
107 isl_seq_cpy(bset->ineq[k], f, 1 + total);
108 isl_int_sub(bset->ineq[k][0], bset->ineq[k][0], l);
110 k = isl_basic_set_alloc_inequality(bset);
113 isl_seq_neg(bset->ineq[k], f, 1 + total);
114 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], u);
118 isl_basic_set_free(bset);
122 /* Find an integer point in "bset" that minimizes f (in any) such that
123 * the value of f lies inside the interval [l, u].
124 * Return this integer point if it can be found.
125 * Otherwise, return sol.
127 * We perform a number of steps until l > u.
128 * In each step, we look for an integer point with value in either
129 * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
130 * The choice depends on whether we have found an integer point in the
131 * previous step. If so, we look for the next point in half of the remaining
133 * If we find a point, the current solution is updated and u is set
134 * to its value minus 1.
135 * If no point can be found, we update l to the upper bound of the interval
136 * we checked (u or l+floor(u-l-1/2)) plus 1.
138 static struct isl_vec *solve_ilp_search(struct isl_basic_set *bset,
139 isl_int *f, isl_int *opt, struct isl_vec *sol, isl_int l, isl_int u)
146 while (isl_int_le(l, u)) {
147 struct isl_basic_set *slice;
148 struct isl_vec *sample;
153 isl_int_sub(tmp, u, l);
154 isl_int_fdiv_q_ui(tmp, tmp, 2);
155 isl_int_add(tmp, tmp, l);
157 slice = add_bounds(isl_basic_set_copy(bset), f, l, tmp);
158 sample = isl_basic_set_sample_vec(slice);
164 if (sample->size > 0) {
167 isl_seq_inner_product(f, sol->el, sol->size, opt);
168 isl_int_sub_ui(u, *opt, 1);
171 isl_vec_free(sample);
174 isl_int_add_ui(l, tmp, 1);
184 /* Find an integer point in "bset" that minimizes f (if any).
185 * If sol_p is not NULL then the integer point is returned in *sol_p.
186 * The optimal value of f is returned in *opt.
188 * The algorithm maintains a currently best solution and an interval [l, u]
189 * of values of f for which integer solutions could potentially still be found.
190 * The initial value of the best solution so far is any solution.
191 * The initial value of l is minimal value of f over the rationals
192 * (rounded up to the nearest integer).
193 * The initial value of u is the value of f at the initial solution minus 1.
195 * We then call solve_ilp_search to perform a binary search on the interval.
197 static enum isl_lp_result solve_ilp(struct isl_basic_set *bset,
198 isl_int *f, isl_int *opt,
199 struct isl_vec **sol_p)
201 enum isl_lp_result res;
205 res = isl_basic_set_solve_lp(bset, 0, f, bset->ctx->one,
207 if (res == isl_lp_ok && isl_int_is_one(sol->el[0])) {
215 if (res == isl_lp_error || res == isl_lp_empty)
218 sol = initial_solution(bset, f);
221 if (sol->size == 0) {
225 if (res == isl_lp_unbounded) {
227 return isl_lp_unbounded;
233 isl_int_set(l, *opt);
235 isl_seq_inner_product(f, sol->el, sol->size, opt);
236 isl_int_sub_ui(u, *opt, 1);
238 sol = solve_ilp_search(bset, f, opt, sol, l, u);
253 static enum isl_lp_result solve_ilp_with_eq(struct isl_basic_set *bset, int max,
254 isl_int *f, isl_int *opt,
255 struct isl_vec **sol_p)
258 enum isl_lp_result res;
259 struct isl_mat *T = NULL;
262 bset = isl_basic_set_copy(bset);
263 dim = isl_basic_set_total_dim(bset);
264 v = isl_vec_alloc(bset->ctx, 1 + dim);
267 isl_seq_cpy(v->el, f, 1 + dim);
268 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
269 v = isl_vec_mat_product(v, isl_mat_copy(T));
272 res = isl_basic_set_solve_ilp(bset, max, v->el, opt, sol_p);
274 if (res == isl_lp_ok && sol_p) {
275 *sol_p = isl_mat_vec_product(T, *sol_p);
280 isl_basic_set_free(bset);
284 isl_basic_set_free(bset);
288 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
290 * If sol_p is not NULL then the integer point is returned in *sol_p.
291 * The optimal value of f is returned in *opt.
293 * If there is any equality among the points in "bset", then we first
294 * project it out. Otherwise, we continue with solve_ilp above.
296 enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max,
297 isl_int *f, isl_int *opt,
298 struct isl_vec **sol_p)
301 enum isl_lp_result res;
308 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
310 if (isl_basic_set_fast_is_empty(bset))
314 return solve_ilp_with_eq(bset, max, f, opt, sol_p);
316 dim = isl_basic_set_total_dim(bset);
319 isl_seq_neg(f, f, 1 + dim);
321 res = solve_ilp(bset, f, opt, sol_p);
324 isl_seq_neg(f, f, 1 + dim);
325 isl_int_neg(*opt, *opt);
330 isl_basic_set_free(bset);