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
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
13 #include "isl_sample.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>
21 #include <isl_lp_private.h>
22 #include <isl_ilp_private.h>
23 #include <isl/deprecated/ilp_int.h>
25 /* Given a basic set "bset", construct a basic set U such that for
26 * each element x in U, the whole unit box positioned at x is inside
27 * the given basic set.
28 * Note that U may not contain all points that satisfy this property.
30 * We simply add the sum of all negative coefficients to the constant
31 * term. This ensures that if x satisfies the resulting constraints,
32 * then x plus any sum of unit vectors satisfies the original constraints.
34 static struct isl_basic_set
*unit_box_base_points(struct isl_basic_set
*bset
)
37 struct isl_basic_set
*unit_box
= NULL
;
43 if (bset
->n_eq
!= 0) {
44 unit_box
= isl_basic_set_empty_like(bset
);
45 isl_basic_set_free(bset
);
49 total
= isl_basic_set_total_dim(bset
);
50 unit_box
= isl_basic_set_alloc_space(isl_basic_set_get_space(bset
),
53 for (i
= 0; i
< bset
->n_ineq
; ++i
) {
54 k
= isl_basic_set_alloc_inequality(unit_box
);
57 isl_seq_cpy(unit_box
->ineq
[k
], bset
->ineq
[i
], 1 + total
);
58 for (j
= 0; j
< total
; ++j
) {
59 if (isl_int_is_nonneg(unit_box
->ineq
[k
][1 + j
]))
61 isl_int_add(unit_box
->ineq
[k
][0],
62 unit_box
->ineq
[k
][0], unit_box
->ineq
[k
][1 + j
]);
66 isl_basic_set_free(bset
);
69 isl_basic_set_free(bset
);
70 isl_basic_set_free(unit_box
);
74 /* Find an integer point in "bset", preferably one that is
75 * close to minimizing "f".
77 * We first check if we can easily put unit boxes inside bset.
78 * If so, we take the best base point of any of the unit boxes we can find
79 * and round it up to the nearest integer.
80 * If not, we simply pick any integer point in "bset".
82 static struct isl_vec
*initial_solution(struct isl_basic_set
*bset
, isl_int
*f
)
84 enum isl_lp_result res
;
85 struct isl_basic_set
*unit_box
;
88 unit_box
= unit_box_base_points(isl_basic_set_copy(bset
));
90 res
= isl_basic_set_solve_lp(unit_box
, 0, f
, bset
->ctx
->one
,
92 if (res
== isl_lp_ok
) {
93 isl_basic_set_free(unit_box
);
94 return isl_vec_ceil(sol
);
97 isl_basic_set_free(unit_box
);
99 return isl_basic_set_sample_vec(isl_basic_set_copy(bset
));
102 /* Restrict "bset" to those points with values for f in the interval [l, u].
104 static struct isl_basic_set
*add_bounds(struct isl_basic_set
*bset
,
105 isl_int
*f
, isl_int l
, isl_int u
)
110 total
= isl_basic_set_total_dim(bset
);
111 bset
= isl_basic_set_extend_constraints(bset
, 0, 2);
113 k
= isl_basic_set_alloc_inequality(bset
);
116 isl_seq_cpy(bset
->ineq
[k
], f
, 1 + total
);
117 isl_int_sub(bset
->ineq
[k
][0], bset
->ineq
[k
][0], l
);
119 k
= isl_basic_set_alloc_inequality(bset
);
122 isl_seq_neg(bset
->ineq
[k
], f
, 1 + total
);
123 isl_int_add(bset
->ineq
[k
][0], bset
->ineq
[k
][0], u
);
127 isl_basic_set_free(bset
);
131 /* Find an integer point in "bset" that minimizes f (in any) such that
132 * the value of f lies inside the interval [l, u].
133 * Return this integer point if it can be found.
134 * Otherwise, return sol.
136 * We perform a number of steps until l > u.
137 * In each step, we look for an integer point with value in either
138 * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
139 * The choice depends on whether we have found an integer point in the
140 * previous step. If so, we look for the next point in half of the remaining
142 * If we find a point, the current solution is updated and u is set
143 * to its value minus 1.
144 * If no point can be found, we update l to the upper bound of the interval
145 * we checked (u or l+floor(u-l-1/2)) plus 1.
147 static struct isl_vec
*solve_ilp_search(struct isl_basic_set
*bset
,
148 isl_int
*f
, isl_int
*opt
, struct isl_vec
*sol
, isl_int l
, isl_int u
)
155 while (isl_int_le(l
, u
)) {
156 struct isl_basic_set
*slice
;
157 struct isl_vec
*sample
;
162 isl_int_sub(tmp
, u
, l
);
163 isl_int_fdiv_q_ui(tmp
, tmp
, 2);
164 isl_int_add(tmp
, tmp
, l
);
166 slice
= add_bounds(isl_basic_set_copy(bset
), f
, l
, tmp
);
167 sample
= isl_basic_set_sample_vec(slice
);
173 if (sample
->size
> 0) {
176 isl_seq_inner_product(f
, sol
->el
, sol
->size
, opt
);
177 isl_int_sub_ui(u
, *opt
, 1);
180 isl_vec_free(sample
);
183 isl_int_add_ui(l
, tmp
, 1);
193 /* Find an integer point in "bset" that minimizes f (if any).
194 * If sol_p is not NULL then the integer point is returned in *sol_p.
195 * The optimal value of f is returned in *opt.
197 * The algorithm maintains a currently best solution and an interval [l, u]
198 * of values of f for which integer solutions could potentially still be found.
199 * The initial value of the best solution so far is any solution.
200 * The initial value of l is minimal value of f over the rationals
201 * (rounded up to the nearest integer).
202 * The initial value of u is the value of f at the initial solution minus 1.
204 * We then call solve_ilp_search to perform a binary search on the interval.
206 static enum isl_lp_result
solve_ilp(struct isl_basic_set
*bset
,
207 isl_int
*f
, isl_int
*opt
,
208 struct isl_vec
**sol_p
)
210 enum isl_lp_result res
;
214 res
= isl_basic_set_solve_lp(bset
, 0, f
, bset
->ctx
->one
,
216 if (res
== isl_lp_ok
&& isl_int_is_one(sol
->el
[0])) {
224 if (res
== isl_lp_error
|| res
== isl_lp_empty
)
227 sol
= initial_solution(bset
, f
);
230 if (sol
->size
== 0) {
234 if (res
== isl_lp_unbounded
) {
236 return isl_lp_unbounded
;
242 isl_int_set(l
, *opt
);
244 isl_seq_inner_product(f
, sol
->el
, sol
->size
, opt
);
245 isl_int_sub_ui(u
, *opt
, 1);
247 sol
= solve_ilp_search(bset
, f
, opt
, sol
, l
, u
);
262 static enum isl_lp_result
solve_ilp_with_eq(struct isl_basic_set
*bset
, int max
,
263 isl_int
*f
, isl_int
*opt
,
264 struct isl_vec
**sol_p
)
267 enum isl_lp_result res
;
268 struct isl_mat
*T
= NULL
;
271 bset
= isl_basic_set_copy(bset
);
272 dim
= isl_basic_set_total_dim(bset
);
273 v
= isl_vec_alloc(bset
->ctx
, 1 + dim
);
276 isl_seq_cpy(v
->el
, f
, 1 + dim
);
277 bset
= isl_basic_set_remove_equalities(bset
, &T
, NULL
);
278 v
= isl_vec_mat_product(v
, isl_mat_copy(T
));
281 res
= isl_basic_set_solve_ilp(bset
, max
, v
->el
, opt
, sol_p
);
283 if (res
== isl_lp_ok
&& sol_p
) {
284 *sol_p
= isl_mat_vec_product(T
, *sol_p
);
289 isl_basic_set_free(bset
);
293 isl_basic_set_free(bset
);
297 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
299 * If sol_p is not NULL then the integer point is returned in *sol_p.
300 * The optimal value of f is returned in *opt.
302 * If there is any equality among the points in "bset", then we first
303 * project it out. Otherwise, we continue with solve_ilp above.
305 enum isl_lp_result
isl_basic_set_solve_ilp(struct isl_basic_set
*bset
, int max
,
306 isl_int
*f
, isl_int
*opt
,
307 struct isl_vec
**sol_p
)
310 enum isl_lp_result res
;
317 isl_assert(bset
->ctx
, isl_basic_set_n_param(bset
) == 0, goto error
);
319 if (isl_basic_set_plain_is_empty(bset
))
323 return solve_ilp_with_eq(bset
, max
, f
, opt
, sol_p
);
325 dim
= isl_basic_set_total_dim(bset
);
328 isl_seq_neg(f
, f
, 1 + dim
);
330 res
= solve_ilp(bset
, f
, opt
, sol_p
);
333 isl_seq_neg(f
, f
, 1 + dim
);
334 isl_int_neg(*opt
, *opt
);
339 isl_basic_set_free(bset
);
343 static enum isl_lp_result
basic_set_opt(__isl_keep isl_basic_set
*bset
, int max
,
344 __isl_keep isl_aff
*obj
, isl_int
*opt
)
346 enum isl_lp_result res
;
350 bset
= isl_basic_set_copy(bset
);
351 bset
= isl_basic_set_underlying_set(bset
);
352 res
= isl_basic_set_solve_ilp(bset
, max
, obj
->v
->el
+ 1, opt
, NULL
);
353 isl_basic_set_free(bset
);
357 static __isl_give isl_mat
*extract_divs(__isl_keep isl_basic_set
*bset
)
360 isl_ctx
*ctx
= isl_basic_set_get_ctx(bset
);
363 div
= isl_mat_alloc(ctx
, bset
->n_div
,
364 1 + 1 + isl_basic_set_total_dim(bset
));
368 for (i
= 0; i
< bset
->n_div
; ++i
)
369 isl_seq_cpy(div
->row
[i
], bset
->div
[i
], div
->n_col
);
374 enum isl_lp_result
isl_basic_set_opt(__isl_keep isl_basic_set
*bset
, int max
,
375 __isl_keep isl_aff
*obj
, isl_int
*opt
)
380 isl_mat
*bset_div
= NULL
;
382 enum isl_lp_result res
;
388 ctx
= isl_aff_get_ctx(obj
);
389 if (!isl_space_is_equal(bset
->dim
, obj
->ls
->dim
))
390 isl_die(ctx
, isl_error_invalid
,
391 "spaces don't match", return isl_lp_error
);
392 if (!isl_int_is_one(obj
->v
->el
[0]))
393 isl_die(ctx
, isl_error_unsupported
,
394 "expecting integer affine expression",
395 return isl_lp_error
);
397 bset_n_div
= isl_basic_set_dim(bset
, isl_dim_div
);
398 if (bset_n_div
== 0 && obj
->ls
->div
->n_row
== 0)
399 return basic_set_opt(bset
, max
, obj
, opt
);
401 bset
= isl_basic_set_copy(bset
);
402 obj
= isl_aff_copy(obj
);
404 bset_div
= extract_divs(bset
);
405 exp1
= isl_alloc_array(ctx
, int, bset_n_div
);
406 exp2
= isl_alloc_array(ctx
, int, obj
->ls
->div
->n_row
);
407 if (!bset_div
|| !exp1
|| !exp2
)
410 div
= isl_merge_divs(bset_div
, obj
->ls
->div
, exp1
, exp2
);
412 bset
= isl_basic_set_expand_divs(bset
, isl_mat_copy(div
), exp1
);
413 obj
= isl_aff_expand_divs(obj
, isl_mat_copy(div
), exp2
);
415 res
= basic_set_opt(bset
, max
, obj
, opt
);
417 isl_mat_free(bset_div
);
421 isl_basic_set_free(bset
);
427 isl_mat_free(bset_div
);
430 isl_basic_set_free(bset
);
435 /* Compute the minimum (maximum if max is set) of the integer affine
436 * expression obj over the points in set and put the result in *opt.
438 * The parameters are assumed to have been aligned.
440 static enum isl_lp_result
isl_set_opt_aligned(__isl_keep isl_set
*set
, int max
,
441 __isl_keep isl_aff
*obj
, isl_int
*opt
)
444 enum isl_lp_result res
;
453 res
= isl_basic_set_opt(set
->p
[0], max
, obj
, opt
);
454 if (res
== isl_lp_error
|| res
== isl_lp_unbounded
)
458 if (res
== isl_lp_ok
)
462 for (i
= 1; i
< set
->n
; ++i
) {
463 res
= isl_basic_set_opt(set
->p
[i
], max
, obj
, &opt_i
);
464 if (res
== isl_lp_error
|| res
== isl_lp_unbounded
) {
465 isl_int_clear(opt_i
);
468 if (res
== isl_lp_ok
)
470 if (isl_int_gt(opt_i
, *opt
))
471 isl_int_set(*opt
, opt_i
);
473 isl_int_clear(opt_i
);
475 return empty
? isl_lp_empty
: isl_lp_ok
;
478 /* Compute the minimum (maximum if max is set) of the integer affine
479 * expression obj over the points in set and put the result in *opt.
481 enum isl_lp_result
isl_set_opt(__isl_keep isl_set
*set
, int max
,
482 __isl_keep isl_aff
*obj
, isl_int
*opt
)
484 enum isl_lp_result res
;
489 if (isl_space_match(set
->dim
, isl_dim_param
,
490 obj
->ls
->dim
, isl_dim_param
))
491 return isl_set_opt_aligned(set
, max
, obj
, opt
);
493 set
= isl_set_copy(set
);
494 obj
= isl_aff_copy(obj
);
495 set
= isl_set_align_params(set
, isl_aff_get_domain_space(obj
));
496 obj
= isl_aff_align_params(obj
, isl_set_get_space(set
));
498 res
= isl_set_opt_aligned(set
, max
, obj
, opt
);
506 enum isl_lp_result
isl_basic_set_max(__isl_keep isl_basic_set
*bset
,
507 __isl_keep isl_aff
*obj
, isl_int
*opt
)
509 return isl_basic_set_opt(bset
, 1, obj
, opt
);
512 enum isl_lp_result
isl_set_max(__isl_keep isl_set
*set
,
513 __isl_keep isl_aff
*obj
, isl_int
*opt
)
515 return isl_set_opt(set
, 1, obj
, opt
);
518 enum isl_lp_result
isl_set_min(__isl_keep isl_set
*set
,
519 __isl_keep isl_aff
*obj
, isl_int
*opt
)
521 return isl_set_opt(set
, 0, obj
, opt
);
524 /* Convert the result of a function that returns an isl_lp_result
525 * to an isl_val. The numerator of "v" is set to the optimal value
526 * if lp_res is isl_lp_ok. "max" is set if a maximum was computed.
528 * Return "v" with denominator set to 1 if lp_res is isl_lp_ok.
529 * Return NULL on error.
530 * Return a NaN if lp_res is isl_lp_empty.
531 * Return infinity or negative infinity if lp_res is isl_lp_unbounded,
532 * depending on "max".
534 static __isl_give isl_val
*convert_lp_result(enum isl_lp_result lp_res
,
535 __isl_take isl_val
*v
, int max
)
539 if (lp_res
== isl_lp_ok
) {
540 isl_int_set_si(v
->d
, 1);
541 return isl_val_normalize(v
);
543 ctx
= isl_val_get_ctx(v
);
545 if (lp_res
== isl_lp_error
)
547 if (lp_res
== isl_lp_empty
)
548 return isl_val_nan(ctx
);
550 return isl_val_infty(ctx
);
552 return isl_val_neginfty(ctx
);
555 /* Return the minimum (maximum if max is set) of the integer affine
556 * expression "obj" over the points in "bset".
558 * Return infinity or negative infinity if the optimal value is unbounded and
559 * NaN if "bset" is empty.
561 * Call isl_basic_set_opt and translate the results.
563 __isl_give isl_val
*isl_basic_set_opt_val(__isl_keep isl_basic_set
*bset
,
564 int max
, __isl_keep isl_aff
*obj
)
568 enum isl_lp_result lp_res
;
573 ctx
= isl_aff_get_ctx(obj
);
574 res
= isl_val_alloc(ctx
);
577 lp_res
= isl_basic_set_opt(bset
, max
, obj
, &res
->n
);
578 return convert_lp_result(lp_res
, res
, max
);
581 /* Return the maximum of the integer affine
582 * expression "obj" over the points in "bset".
584 * Return infinity or negative infinity if the optimal value is unbounded and
585 * NaN if "bset" is empty.
587 __isl_give isl_val
*isl_basic_set_max_val(__isl_keep isl_basic_set
*bset
,
588 __isl_keep isl_aff
*obj
)
590 return isl_basic_set_opt_val(bset
, 1, obj
);
593 /* Return the minimum (maximum if max is set) of the integer affine
594 * expression "obj" over the points in "set".
596 * Return infinity or negative infinity if the optimal value is unbounded and
597 * NaN if "bset" is empty.
599 * Call isl_set_opt and translate the results.
601 __isl_give isl_val
*isl_set_opt_val(__isl_keep isl_set
*set
, int max
,
602 __isl_keep isl_aff
*obj
)
606 enum isl_lp_result lp_res
;
611 ctx
= isl_aff_get_ctx(obj
);
612 res
= isl_val_alloc(ctx
);
615 lp_res
= isl_set_opt(set
, max
, obj
, &res
->n
);
616 return convert_lp_result(lp_res
, res
, max
);
619 /* Return the minimum of the integer affine
620 * expression "obj" over the points in "set".
622 * Return infinity or negative infinity if the optimal value is unbounded and
623 * NaN if "bset" is empty.
625 __isl_give isl_val
*isl_set_min_val(__isl_keep isl_set
*set
,
626 __isl_keep isl_aff
*obj
)
628 return isl_set_opt_val(set
, 0, obj
);
631 /* Return the maximum of the integer affine
632 * expression "obj" over the points in "set".
634 * Return infinity or negative infinity if the optimal value is unbounded and
635 * NaN if "bset" is empty.
637 __isl_give isl_val
*isl_set_max_val(__isl_keep isl_set
*set
,
638 __isl_keep isl_aff
*obj
)
640 return isl_set_opt_val(set
, 1, obj
);