5b7f30eb5e3c9ada69ea52ee62adfb26678f3e09
[isl.git] / isl_ilp.c
blob5b7f30eb5e3c9ada69ea52ee62adfb26678f3e09
1 /*
2 * Copyright 2008-2009 Katholieke Universiteit Leuven
4 * Use of this software is governed by the MIT license
6 * Written by Sven Verdoolaege, K.U.Leuven, Departement
7 * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
8 */
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
12 #include <isl/ilp.h>
13 #include "isl_sample.h"
14 #include <isl_seq.h>
15 #include "isl_equalities.h"
16 #include <isl_aff_private.h>
17 #include <isl_local_space_private.h>
18 #include <isl_mat_private.h>
19 #include <isl_val_private.h>
20 #include <isl_vec_private.h>
22 /* Given a basic set "bset", construct a basic set U such that for
23 * each element x in U, the whole unit box positioned at x is inside
24 * the given basic set.
25 * Note that U may not contain all points that satisfy this property.
27 * We simply add the sum of all negative coefficients to the constant
28 * term. This ensures that if x satisfies the resulting constraints,
29 * then x plus any sum of unit vectors satisfies the original constraints.
31 static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
33 int i, j, k;
34 struct isl_basic_set *unit_box = NULL;
35 unsigned total;
37 if (!bset)
38 goto error;
40 if (bset->n_eq != 0) {
41 unit_box = isl_basic_set_empty_like(bset);
42 isl_basic_set_free(bset);
43 return unit_box;
46 total = isl_basic_set_total_dim(bset);
47 unit_box = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
48 0, 0, bset->n_ineq);
50 for (i = 0; i < bset->n_ineq; ++i) {
51 k = isl_basic_set_alloc_inequality(unit_box);
52 if (k < 0)
53 goto error;
54 isl_seq_cpy(unit_box->ineq[k], bset->ineq[i], 1 + total);
55 for (j = 0; j < total; ++j) {
56 if (isl_int_is_nonneg(unit_box->ineq[k][1 + j]))
57 continue;
58 isl_int_add(unit_box->ineq[k][0],
59 unit_box->ineq[k][0], unit_box->ineq[k][1 + j]);
63 isl_basic_set_free(bset);
64 return unit_box;
65 error:
66 isl_basic_set_free(bset);
67 isl_basic_set_free(unit_box);
68 return NULL;
71 /* Find an integer point in "bset", preferably one that is
72 * close to minimizing "f".
74 * We first check if we can easily put unit boxes inside bset.
75 * If so, we take the best base point of any of the unit boxes we can find
76 * and round it up to the nearest integer.
77 * If not, we simply pick any integer point in "bset".
79 static struct isl_vec *initial_solution(struct isl_basic_set *bset, isl_int *f)
81 enum isl_lp_result res;
82 struct isl_basic_set *unit_box;
83 struct isl_vec *sol;
85 unit_box = unit_box_base_points(isl_basic_set_copy(bset));
87 res = isl_basic_set_solve_lp(unit_box, 0, f, bset->ctx->one,
88 NULL, NULL, &sol);
89 if (res == isl_lp_ok) {
90 isl_basic_set_free(unit_box);
91 return isl_vec_ceil(sol);
94 isl_basic_set_free(unit_box);
96 return isl_basic_set_sample_vec(isl_basic_set_copy(bset));
99 /* Restrict "bset" to those points with values for f in the interval [l, u].
101 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
102 isl_int *f, isl_int l, isl_int u)
104 int k;
105 unsigned total;
107 total = isl_basic_set_total_dim(bset);
108 bset = isl_basic_set_extend_constraints(bset, 0, 2);
110 k = isl_basic_set_alloc_inequality(bset);
111 if (k < 0)
112 goto error;
113 isl_seq_cpy(bset->ineq[k], f, 1 + total);
114 isl_int_sub(bset->ineq[k][0], bset->ineq[k][0], l);
116 k = isl_basic_set_alloc_inequality(bset);
117 if (k < 0)
118 goto error;
119 isl_seq_neg(bset->ineq[k], f, 1 + total);
120 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], u);
122 return bset;
123 error:
124 isl_basic_set_free(bset);
125 return NULL;
128 /* Find an integer point in "bset" that minimizes f (in any) such that
129 * the value of f lies inside the interval [l, u].
130 * Return this integer point if it can be found.
131 * Otherwise, return sol.
133 * We perform a number of steps until l > u.
134 * In each step, we look for an integer point with value in either
135 * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
136 * The choice depends on whether we have found an integer point in the
137 * previous step. If so, we look for the next point in half of the remaining
138 * interval.
139 * If we find a point, the current solution is updated and u is set
140 * to its value minus 1.
141 * If no point can be found, we update l to the upper bound of the interval
142 * we checked (u or l+floor(u-l-1/2)) plus 1.
144 static struct isl_vec *solve_ilp_search(struct isl_basic_set *bset,
145 isl_int *f, isl_int *opt, struct isl_vec *sol, isl_int l, isl_int u)
147 isl_int tmp;
148 int divide = 1;
150 isl_int_init(tmp);
152 while (isl_int_le(l, u)) {
153 struct isl_basic_set *slice;
154 struct isl_vec *sample;
156 if (!divide)
157 isl_int_set(tmp, u);
158 else {
159 isl_int_sub(tmp, u, l);
160 isl_int_fdiv_q_ui(tmp, tmp, 2);
161 isl_int_add(tmp, tmp, l);
163 slice = add_bounds(isl_basic_set_copy(bset), f, l, tmp);
164 sample = isl_basic_set_sample_vec(slice);
165 if (!sample) {
166 isl_vec_free(sol);
167 sol = NULL;
168 break;
170 if (sample->size > 0) {
171 isl_vec_free(sol);
172 sol = sample;
173 isl_seq_inner_product(f, sol->el, sol->size, opt);
174 isl_int_sub_ui(u, *opt, 1);
175 divide = 1;
176 } else {
177 isl_vec_free(sample);
178 if (!divide)
179 break;
180 isl_int_add_ui(l, tmp, 1);
181 divide = 0;
185 isl_int_clear(tmp);
187 return sol;
190 /* Find an integer point in "bset" that minimizes f (if any).
191 * If sol_p is not NULL then the integer point is returned in *sol_p.
192 * The optimal value of f is returned in *opt.
194 * The algorithm maintains a currently best solution and an interval [l, u]
195 * of values of f for which integer solutions could potentially still be found.
196 * The initial value of the best solution so far is any solution.
197 * The initial value of l is minimal value of f over the rationals
198 * (rounded up to the nearest integer).
199 * The initial value of u is the value of f at the initial solution minus 1.
201 * We then call solve_ilp_search to perform a binary search on the interval.
203 static enum isl_lp_result solve_ilp(struct isl_basic_set *bset,
204 isl_int *f, isl_int *opt,
205 struct isl_vec **sol_p)
207 enum isl_lp_result res;
208 isl_int l, u;
209 struct isl_vec *sol;
211 res = isl_basic_set_solve_lp(bset, 0, f, bset->ctx->one,
212 opt, NULL, &sol);
213 if (res == isl_lp_ok && isl_int_is_one(sol->el[0])) {
214 if (sol_p)
215 *sol_p = sol;
216 else
217 isl_vec_free(sol);
218 return isl_lp_ok;
220 isl_vec_free(sol);
221 if (res == isl_lp_error || res == isl_lp_empty)
222 return res;
224 sol = initial_solution(bset, f);
225 if (!sol)
226 return isl_lp_error;
227 if (sol->size == 0) {
228 isl_vec_free(sol);
229 return isl_lp_empty;
231 if (res == isl_lp_unbounded) {
232 isl_vec_free(sol);
233 return isl_lp_unbounded;
236 isl_int_init(l);
237 isl_int_init(u);
239 isl_int_set(l, *opt);
241 isl_seq_inner_product(f, sol->el, sol->size, opt);
242 isl_int_sub_ui(u, *opt, 1);
244 sol = solve_ilp_search(bset, f, opt, sol, l, u);
245 if (!sol)
246 res = isl_lp_error;
248 isl_int_clear(l);
249 isl_int_clear(u);
251 if (sol_p)
252 *sol_p = sol;
253 else
254 isl_vec_free(sol);
256 return res;
259 static enum isl_lp_result solve_ilp_with_eq(struct isl_basic_set *bset, int max,
260 isl_int *f, isl_int *opt,
261 struct isl_vec **sol_p)
263 unsigned dim;
264 enum isl_lp_result res;
265 struct isl_mat *T = NULL;
266 struct isl_vec *v;
268 bset = isl_basic_set_copy(bset);
269 dim = isl_basic_set_total_dim(bset);
270 v = isl_vec_alloc(bset->ctx, 1 + dim);
271 if (!v)
272 goto error;
273 isl_seq_cpy(v->el, f, 1 + dim);
274 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
275 v = isl_vec_mat_product(v, isl_mat_copy(T));
276 if (!v)
277 goto error;
278 res = isl_basic_set_solve_ilp(bset, max, v->el, opt, sol_p);
279 isl_vec_free(v);
280 if (res == isl_lp_ok && sol_p) {
281 *sol_p = isl_mat_vec_product(T, *sol_p);
282 if (!*sol_p)
283 res = isl_lp_error;
284 } else
285 isl_mat_free(T);
286 isl_basic_set_free(bset);
287 return res;
288 error:
289 isl_mat_free(T);
290 isl_basic_set_free(bset);
291 return isl_lp_error;
294 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
295 * f (if any).
296 * If sol_p is not NULL then the integer point is returned in *sol_p.
297 * The optimal value of f is returned in *opt.
299 * If there is any equality among the points in "bset", then we first
300 * project it out. Otherwise, we continue with solve_ilp above.
302 enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max,
303 isl_int *f, isl_int *opt,
304 struct isl_vec **sol_p)
306 unsigned dim;
307 enum isl_lp_result res;
309 if (!bset)
310 return isl_lp_error;
311 if (sol_p)
312 *sol_p = NULL;
314 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
316 if (isl_basic_set_plain_is_empty(bset))
317 return isl_lp_empty;
319 if (bset->n_eq)
320 return solve_ilp_with_eq(bset, max, f, opt, sol_p);
322 dim = isl_basic_set_total_dim(bset);
324 if (max)
325 isl_seq_neg(f, f, 1 + dim);
327 res = solve_ilp(bset, f, opt, sol_p);
329 if (max) {
330 isl_seq_neg(f, f, 1 + dim);
331 isl_int_neg(*opt, *opt);
334 return res;
335 error:
336 isl_basic_set_free(bset);
337 return isl_lp_error;
340 static enum isl_lp_result basic_set_opt(__isl_keep isl_basic_set *bset, int max,
341 __isl_keep isl_aff *obj, isl_int *opt)
343 enum isl_lp_result res;
345 if (!obj)
346 return isl_lp_error;
347 bset = isl_basic_set_copy(bset);
348 bset = isl_basic_set_underlying_set(bset);
349 res = isl_basic_set_solve_ilp(bset, max, obj->v->el + 1, opt, NULL);
350 isl_basic_set_free(bset);
351 return res;
354 static __isl_give isl_mat *extract_divs(__isl_keep isl_basic_set *bset)
356 int i;
357 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
358 isl_mat *div;
360 div = isl_mat_alloc(ctx, bset->n_div,
361 1 + 1 + isl_basic_set_total_dim(bset));
362 if (!div)
363 return NULL;
365 for (i = 0; i < bset->n_div; ++i)
366 isl_seq_cpy(div->row[i], bset->div[i], div->n_col);
368 return div;
371 enum isl_lp_result isl_basic_set_opt(__isl_keep isl_basic_set *bset, int max,
372 __isl_keep isl_aff *obj, isl_int *opt)
374 int *exp1 = NULL;
375 int *exp2 = NULL;
376 isl_ctx *ctx;
377 isl_mat *bset_div = NULL;
378 isl_mat *div = NULL;
379 enum isl_lp_result res;
380 int bset_n_div;
382 if (!bset || !obj)
383 return isl_lp_error;
385 ctx = isl_aff_get_ctx(obj);
386 if (!isl_space_is_equal(bset->dim, obj->ls->dim))
387 isl_die(ctx, isl_error_invalid,
388 "spaces don't match", return isl_lp_error);
389 if (!isl_int_is_one(obj->v->el[0]))
390 isl_die(ctx, isl_error_unsupported,
391 "expecting integer affine expression",
392 return isl_lp_error);
394 bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
395 if (bset_n_div == 0 && obj->ls->div->n_row == 0)
396 return basic_set_opt(bset, max, obj, opt);
398 bset = isl_basic_set_copy(bset);
399 obj = isl_aff_copy(obj);
401 bset_div = extract_divs(bset);
402 exp1 = isl_alloc_array(ctx, int, bset_n_div);
403 exp2 = isl_alloc_array(ctx, int, obj->ls->div->n_row);
404 if (!bset_div || !exp1 || !exp2)
405 goto error;
407 div = isl_merge_divs(bset_div, obj->ls->div, exp1, exp2);
409 bset = isl_basic_set_expand_divs(bset, isl_mat_copy(div), exp1);
410 obj = isl_aff_expand_divs(obj, isl_mat_copy(div), exp2);
412 res = basic_set_opt(bset, max, obj, opt);
414 isl_mat_free(bset_div);
415 isl_mat_free(div);
416 free(exp1);
417 free(exp2);
418 isl_basic_set_free(bset);
419 isl_aff_free(obj);
421 return res;
422 error:
423 isl_mat_free(div);
424 isl_mat_free(bset_div);
425 free(exp1);
426 free(exp2);
427 isl_basic_set_free(bset);
428 isl_aff_free(obj);
429 return isl_lp_error;
432 /* Compute the minimum (maximum if max is set) of the integer affine
433 * expression obj over the points in set and put the result in *opt.
435 * The parameters are assumed to have been aligned.
437 static enum isl_lp_result isl_set_opt_aligned(__isl_keep isl_set *set, int max,
438 __isl_keep isl_aff *obj, isl_int *opt)
440 int i;
441 enum isl_lp_result res;
442 int empty = 1;
443 isl_int opt_i;
445 if (!set || !obj)
446 return isl_lp_error;
447 if (set->n == 0)
448 return isl_lp_empty;
450 res = isl_basic_set_opt(set->p[0], max, obj, opt);
451 if (res == isl_lp_error || res == isl_lp_unbounded)
452 return res;
453 if (set->n == 1)
454 return res;
455 if (res == isl_lp_ok)
456 empty = 0;
458 isl_int_init(opt_i);
459 for (i = 1; i < set->n; ++i) {
460 res = isl_basic_set_opt(set->p[i], max, obj, &opt_i);
461 if (res == isl_lp_error || res == isl_lp_unbounded) {
462 isl_int_clear(opt_i);
463 return res;
465 if (res == isl_lp_ok)
466 empty = 0;
467 if (isl_int_gt(opt_i, *opt))
468 isl_int_set(*opt, opt_i);
470 isl_int_clear(opt_i);
472 return empty ? isl_lp_empty : isl_lp_ok;
475 /* Compute the minimum (maximum if max is set) of the integer affine
476 * expression obj over the points in set and put the result in *opt.
478 enum isl_lp_result isl_set_opt(__isl_keep isl_set *set, int max,
479 __isl_keep isl_aff *obj, isl_int *opt)
481 enum isl_lp_result res;
483 if (!set || !obj)
484 return isl_lp_error;
486 if (isl_space_match(set->dim, isl_dim_param,
487 obj->ls->dim, isl_dim_param))
488 return isl_set_opt_aligned(set, max, obj, opt);
490 set = isl_set_copy(set);
491 obj = isl_aff_copy(obj);
492 set = isl_set_align_params(set, isl_aff_get_domain_space(obj));
493 obj = isl_aff_align_params(obj, isl_set_get_space(set));
495 res = isl_set_opt_aligned(set, max, obj, opt);
497 isl_set_free(set);
498 isl_aff_free(obj);
500 return res;
503 enum isl_lp_result isl_basic_set_max(__isl_keep isl_basic_set *bset,
504 __isl_keep isl_aff *obj, isl_int *opt)
506 return isl_basic_set_opt(bset, 1, obj, opt);
509 enum isl_lp_result isl_set_max(__isl_keep isl_set *set,
510 __isl_keep isl_aff *obj, isl_int *opt)
512 return isl_set_opt(set, 1, obj, opt);
515 enum isl_lp_result isl_set_min(__isl_keep isl_set *set,
516 __isl_keep isl_aff *obj, isl_int *opt)
518 return isl_set_opt(set, 0, obj, opt);
521 /* Convert the result of a function that returns an isl_lp_result
522 * to an isl_val. The numerator of "v" is set to the optimal value
523 * if lp_res is isl_lp_ok. "max" is set if a maximum was computed.
525 * Return "v" with denominator set to 1 if lp_res is isl_lp_ok.
526 * Return NULL on error.
527 * Return a NaN if lp_res is isl_lp_empty.
528 * Return infinity or negative infinity if lp_res is isl_lp_unbounded,
529 * depending on "max".
531 static __isl_give isl_val *convert_lp_result(enum isl_lp_result lp_res,
532 __isl_take isl_val *v, int max)
534 isl_ctx *ctx;
536 if (lp_res == isl_lp_ok) {
537 isl_int_set_si(v->d, 1);
538 return isl_val_normalize(v);
540 ctx = isl_val_get_ctx(v);
541 isl_val_free(v);
542 if (lp_res == isl_lp_error)
543 return NULL;
544 if (lp_res == isl_lp_empty)
545 return isl_val_nan(ctx);
546 if (max)
547 return isl_val_infty(ctx);
548 else
549 return isl_val_neginfty(ctx);
552 /* Return the minimum (maximum if max is set) of the integer affine
553 * expression "obj" over the points in "bset".
555 * Return infinity or negative infinity if the optimal value is unbounded and
556 * NaN if "bset" is empty.
558 * Call isl_basic_set_opt and translate the results.
560 __isl_give isl_val *isl_basic_set_opt_val(__isl_keep isl_basic_set *bset,
561 int max, __isl_keep isl_aff *obj)
563 isl_ctx *ctx;
564 isl_val *res;
565 enum isl_lp_result lp_res;
567 if (!bset || !obj)
568 return NULL;
570 ctx = isl_aff_get_ctx(obj);
571 res = isl_val_alloc(ctx);
572 if (!res)
573 return NULL;
574 lp_res = isl_basic_set_opt(bset, max, obj, &res->n);
575 return convert_lp_result(lp_res, res, max);
578 /* Return the maximum of the integer affine
579 * expression "obj" over the points in "bset".
581 * Return infinity or negative infinity if the optimal value is unbounded and
582 * NaN if "bset" is empty.
584 __isl_give isl_val *isl_basic_set_max_val(__isl_keep isl_basic_set *bset,
585 __isl_keep isl_aff *obj)
587 return isl_basic_set_opt_val(bset, 1, obj);
590 /* Return the minimum (maximum if max is set) of the integer affine
591 * expression "obj" over the points in "set".
593 * Return infinity or negative infinity if the optimal value is unbounded and
594 * NaN if "bset" is empty.
596 * Call isl_set_opt and translate the results.
598 __isl_give isl_val *isl_set_opt_val(__isl_keep isl_set *set, int max,
599 __isl_keep isl_aff *obj)
601 isl_ctx *ctx;
602 isl_val *res;
603 enum isl_lp_result lp_res;
605 if (!set || !obj)
606 return NULL;
608 ctx = isl_aff_get_ctx(obj);
609 res = isl_val_alloc(ctx);
610 if (!res)
611 return NULL;
612 lp_res = isl_set_opt(set, max, obj, &res->n);
613 return convert_lp_result(lp_res, res, max);
616 /* Return the minimum of the integer affine
617 * expression "obj" over the points in "set".
619 * Return infinity or negative infinity if the optimal value is unbounded and
620 * NaN if "bset" is empty.
622 __isl_give isl_val *isl_set_min_val(__isl_keep isl_set *set,
623 __isl_keep isl_aff *obj)
625 return isl_set_opt_val(set, 0, obj);
628 /* Return the maximum of the integer affine
629 * expression "obj" over the points in "set".
631 * Return infinity or negative infinity if the optimal value is unbounded and
632 * NaN if "bset" is empty.
634 __isl_give isl_val *isl_set_max_val(__isl_keep isl_set *set,
635 __isl_keep isl_aff *obj)
637 return isl_set_opt_val(set, 1, obj);