isl_basic_map_remove_redundancies: sort constraints
[isl.git] / isl_ilp.c
blob1897035602c325ae32321e0547c2c7c36b333a24
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/union_set.h>
14 #include "isl_sample.h"
15 #include <isl_seq.h>
16 #include "isl_equalities.h"
17 #include <isl_aff_private.h>
18 #include <isl_local_space_private.h>
19 #include <isl_mat_private.h>
20 #include <isl_val_private.h>
21 #include <isl_vec_private.h>
22 #include <isl_lp_private.h>
23 #include <isl_ilp_private.h>
24 #include <isl/deprecated/ilp_int.h>
26 /* Given a basic set "bset", construct a basic set U such that for
27 * each element x in U, the whole unit box positioned at x is inside
28 * the given basic set.
29 * Note that U may not contain all points that satisfy this property.
31 * We simply add the sum of all negative coefficients to the constant
32 * term. This ensures that if x satisfies the resulting constraints,
33 * then x plus any sum of unit vectors satisfies the original constraints.
35 static struct isl_basic_set *unit_box_base_points(struct isl_basic_set *bset)
37 int i, j, k;
38 struct isl_basic_set *unit_box = NULL;
39 unsigned total;
41 if (!bset)
42 goto error;
44 if (bset->n_eq != 0) {
45 isl_space *space = isl_basic_set_get_space(bset);
46 isl_basic_set_free(bset);
47 return isl_basic_set_empty(space);
50 total = isl_basic_set_total_dim(bset);
51 unit_box = isl_basic_set_alloc_space(isl_basic_set_get_space(bset),
52 0, 0, bset->n_ineq);
54 for (i = 0; i < bset->n_ineq; ++i) {
55 k = isl_basic_set_alloc_inequality(unit_box);
56 if (k < 0)
57 goto error;
58 isl_seq_cpy(unit_box->ineq[k], bset->ineq[i], 1 + total);
59 for (j = 0; j < total; ++j) {
60 if (isl_int_is_nonneg(unit_box->ineq[k][1 + j]))
61 continue;
62 isl_int_add(unit_box->ineq[k][0],
63 unit_box->ineq[k][0], unit_box->ineq[k][1 + j]);
67 isl_basic_set_free(bset);
68 return unit_box;
69 error:
70 isl_basic_set_free(bset);
71 isl_basic_set_free(unit_box);
72 return NULL;
75 /* Find an integer point in "bset", preferably one that is
76 * close to minimizing "f".
78 * We first check if we can easily put unit boxes inside bset.
79 * If so, we take the best base point of any of the unit boxes we can find
80 * and round it up to the nearest integer.
81 * If not, we simply pick any integer point in "bset".
83 static struct isl_vec *initial_solution(struct isl_basic_set *bset, isl_int *f)
85 enum isl_lp_result res;
86 struct isl_basic_set *unit_box;
87 struct isl_vec *sol;
89 unit_box = unit_box_base_points(isl_basic_set_copy(bset));
91 res = isl_basic_set_solve_lp(unit_box, 0, f, bset->ctx->one,
92 NULL, NULL, &sol);
93 if (res == isl_lp_ok) {
94 isl_basic_set_free(unit_box);
95 return isl_vec_ceil(sol);
98 isl_basic_set_free(unit_box);
100 return isl_basic_set_sample_vec(isl_basic_set_copy(bset));
103 /* Restrict "bset" to those points with values for f in the interval [l, u].
105 static struct isl_basic_set *add_bounds(struct isl_basic_set *bset,
106 isl_int *f, isl_int l, isl_int u)
108 int k;
109 unsigned total;
111 total = isl_basic_set_total_dim(bset);
112 bset = isl_basic_set_extend_constraints(bset, 0, 2);
114 k = isl_basic_set_alloc_inequality(bset);
115 if (k < 0)
116 goto error;
117 isl_seq_cpy(bset->ineq[k], f, 1 + total);
118 isl_int_sub(bset->ineq[k][0], bset->ineq[k][0], l);
120 k = isl_basic_set_alloc_inequality(bset);
121 if (k < 0)
122 goto error;
123 isl_seq_neg(bset->ineq[k], f, 1 + total);
124 isl_int_add(bset->ineq[k][0], bset->ineq[k][0], u);
126 return bset;
127 error:
128 isl_basic_set_free(bset);
129 return NULL;
132 /* Find an integer point in "bset" that minimizes f (in any) such that
133 * the value of f lies inside the interval [l, u].
134 * Return this integer point if it can be found.
135 * Otherwise, return sol.
137 * We perform a number of steps until l > u.
138 * In each step, we look for an integer point with value in either
139 * the whole interval [l, u] or half of the interval [l, l+floor(u-l-1/2)].
140 * The choice depends on whether we have found an integer point in the
141 * previous step. If so, we look for the next point in half of the remaining
142 * interval.
143 * If we find a point, the current solution is updated and u is set
144 * to its value minus 1.
145 * If no point can be found, we update l to the upper bound of the interval
146 * we checked (u or l+floor(u-l-1/2)) plus 1.
148 static struct isl_vec *solve_ilp_search(struct isl_basic_set *bset,
149 isl_int *f, isl_int *opt, struct isl_vec *sol, isl_int l, isl_int u)
151 isl_int tmp;
152 int divide = 1;
154 isl_int_init(tmp);
156 while (isl_int_le(l, u)) {
157 struct isl_basic_set *slice;
158 struct isl_vec *sample;
160 if (!divide)
161 isl_int_set(tmp, u);
162 else {
163 isl_int_sub(tmp, u, l);
164 isl_int_fdiv_q_ui(tmp, tmp, 2);
165 isl_int_add(tmp, tmp, l);
167 slice = add_bounds(isl_basic_set_copy(bset), f, l, tmp);
168 sample = isl_basic_set_sample_vec(slice);
169 if (!sample) {
170 isl_vec_free(sol);
171 sol = NULL;
172 break;
174 if (sample->size > 0) {
175 isl_vec_free(sol);
176 sol = sample;
177 isl_seq_inner_product(f, sol->el, sol->size, opt);
178 isl_int_sub_ui(u, *opt, 1);
179 divide = 1;
180 } else {
181 isl_vec_free(sample);
182 if (!divide)
183 break;
184 isl_int_add_ui(l, tmp, 1);
185 divide = 0;
189 isl_int_clear(tmp);
191 return sol;
194 /* Find an integer point in "bset" that minimizes f (if any).
195 * If sol_p is not NULL then the integer point is returned in *sol_p.
196 * The optimal value of f is returned in *opt.
198 * The algorithm maintains a currently best solution and an interval [l, u]
199 * of values of f for which integer solutions could potentially still be found.
200 * The initial value of the best solution so far is any solution.
201 * The initial value of l is minimal value of f over the rationals
202 * (rounded up to the nearest integer).
203 * The initial value of u is the value of f at the initial solution minus 1.
205 * We then call solve_ilp_search to perform a binary search on the interval.
207 static enum isl_lp_result solve_ilp(struct isl_basic_set *bset,
208 isl_int *f, isl_int *opt,
209 struct isl_vec **sol_p)
211 enum isl_lp_result res;
212 isl_int l, u;
213 struct isl_vec *sol;
215 res = isl_basic_set_solve_lp(bset, 0, f, bset->ctx->one,
216 opt, NULL, &sol);
217 if (res == isl_lp_ok && isl_int_is_one(sol->el[0])) {
218 if (sol_p)
219 *sol_p = sol;
220 else
221 isl_vec_free(sol);
222 return isl_lp_ok;
224 isl_vec_free(sol);
225 if (res == isl_lp_error || res == isl_lp_empty)
226 return res;
228 sol = initial_solution(bset, f);
229 if (!sol)
230 return isl_lp_error;
231 if (sol->size == 0) {
232 isl_vec_free(sol);
233 return isl_lp_empty;
235 if (res == isl_lp_unbounded) {
236 isl_vec_free(sol);
237 return isl_lp_unbounded;
240 isl_int_init(l);
241 isl_int_init(u);
243 isl_int_set(l, *opt);
245 isl_seq_inner_product(f, sol->el, sol->size, opt);
246 isl_int_sub_ui(u, *opt, 1);
248 sol = solve_ilp_search(bset, f, opt, sol, l, u);
249 if (!sol)
250 res = isl_lp_error;
252 isl_int_clear(l);
253 isl_int_clear(u);
255 if (sol_p)
256 *sol_p = sol;
257 else
258 isl_vec_free(sol);
260 return res;
263 static enum isl_lp_result solve_ilp_with_eq(struct isl_basic_set *bset, int max,
264 isl_int *f, isl_int *opt,
265 struct isl_vec **sol_p)
267 unsigned dim;
268 enum isl_lp_result res;
269 struct isl_mat *T = NULL;
270 struct isl_vec *v;
272 bset = isl_basic_set_copy(bset);
273 dim = isl_basic_set_total_dim(bset);
274 v = isl_vec_alloc(bset->ctx, 1 + dim);
275 if (!v)
276 goto error;
277 isl_seq_cpy(v->el, f, 1 + dim);
278 bset = isl_basic_set_remove_equalities(bset, &T, NULL);
279 v = isl_vec_mat_product(v, isl_mat_copy(T));
280 if (!v)
281 goto error;
282 res = isl_basic_set_solve_ilp(bset, max, v->el, opt, sol_p);
283 isl_vec_free(v);
284 if (res == isl_lp_ok && sol_p) {
285 *sol_p = isl_mat_vec_product(T, *sol_p);
286 if (!*sol_p)
287 res = isl_lp_error;
288 } else
289 isl_mat_free(T);
290 isl_basic_set_free(bset);
291 return res;
292 error:
293 isl_mat_free(T);
294 isl_basic_set_free(bset);
295 return isl_lp_error;
298 /* Find an integer point in "bset" that minimizes (or maximizes if max is set)
299 * f (if any).
300 * If sol_p is not NULL then the integer point is returned in *sol_p.
301 * The optimal value of f is returned in *opt.
303 * If there is any equality among the points in "bset", then we first
304 * project it out. Otherwise, we continue with solve_ilp above.
306 enum isl_lp_result isl_basic_set_solve_ilp(struct isl_basic_set *bset, int max,
307 isl_int *f, isl_int *opt,
308 struct isl_vec **sol_p)
310 unsigned dim;
311 enum isl_lp_result res;
313 if (!bset)
314 return isl_lp_error;
315 if (sol_p)
316 *sol_p = NULL;
318 isl_assert(bset->ctx, isl_basic_set_n_param(bset) == 0, goto error);
320 if (isl_basic_set_plain_is_empty(bset))
321 return isl_lp_empty;
323 if (bset->n_eq)
324 return solve_ilp_with_eq(bset, max, f, opt, sol_p);
326 dim = isl_basic_set_total_dim(bset);
328 if (max)
329 isl_seq_neg(f, f, 1 + dim);
331 res = solve_ilp(bset, f, opt, sol_p);
333 if (max) {
334 isl_seq_neg(f, f, 1 + dim);
335 isl_int_neg(*opt, *opt);
338 return res;
339 error:
340 isl_basic_set_free(bset);
341 return isl_lp_error;
344 static enum isl_lp_result basic_set_opt(__isl_keep isl_basic_set *bset, int max,
345 __isl_keep isl_aff *obj, isl_int *opt)
347 enum isl_lp_result res;
349 if (!obj)
350 return isl_lp_error;
351 bset = isl_basic_set_copy(bset);
352 bset = isl_basic_set_underlying_set(bset);
353 res = isl_basic_set_solve_ilp(bset, max, obj->v->el + 1, opt, NULL);
354 isl_basic_set_free(bset);
355 return res;
358 static __isl_give isl_mat *extract_divs(__isl_keep isl_basic_set *bset)
360 int i;
361 isl_ctx *ctx = isl_basic_set_get_ctx(bset);
362 isl_mat *div;
364 div = isl_mat_alloc(ctx, bset->n_div,
365 1 + 1 + isl_basic_set_total_dim(bset));
366 if (!div)
367 return NULL;
369 for (i = 0; i < bset->n_div; ++i)
370 isl_seq_cpy(div->row[i], bset->div[i], div->n_col);
372 return div;
375 enum isl_lp_result isl_basic_set_opt(__isl_keep isl_basic_set *bset, int max,
376 __isl_keep isl_aff *obj, isl_int *opt)
378 int *exp1 = NULL;
379 int *exp2 = NULL;
380 isl_ctx *ctx;
381 isl_mat *bset_div = NULL;
382 isl_mat *div = NULL;
383 enum isl_lp_result res;
384 int bset_n_div, obj_n_div;
386 if (!bset || !obj)
387 return isl_lp_error;
389 ctx = isl_aff_get_ctx(obj);
390 if (!isl_space_is_equal(bset->dim, obj->ls->dim))
391 isl_die(ctx, isl_error_invalid,
392 "spaces don't match", return isl_lp_error);
393 if (!isl_int_is_one(obj->v->el[0]))
394 isl_die(ctx, isl_error_unsupported,
395 "expecting integer affine expression",
396 return isl_lp_error);
398 bset_n_div = isl_basic_set_dim(bset, isl_dim_div);
399 obj_n_div = isl_aff_dim(obj, isl_dim_div);
400 if (bset_n_div == 0 && obj_n_div == 0)
401 return basic_set_opt(bset, max, obj, opt);
403 bset = isl_basic_set_copy(bset);
404 obj = isl_aff_copy(obj);
406 bset_div = extract_divs(bset);
407 exp1 = isl_alloc_array(ctx, int, bset_n_div);
408 exp2 = isl_alloc_array(ctx, int, obj_n_div);
409 if (!bset_div || (bset_n_div && !exp1) || (obj_n_div && !exp2))
410 goto error;
412 div = isl_merge_divs(bset_div, obj->ls->div, exp1, exp2);
414 bset = isl_basic_set_expand_divs(bset, isl_mat_copy(div), exp1);
415 obj = isl_aff_expand_divs(obj, isl_mat_copy(div), exp2);
417 res = basic_set_opt(bset, max, obj, opt);
419 isl_mat_free(bset_div);
420 isl_mat_free(div);
421 free(exp1);
422 free(exp2);
423 isl_basic_set_free(bset);
424 isl_aff_free(obj);
426 return res;
427 error:
428 isl_mat_free(div);
429 isl_mat_free(bset_div);
430 free(exp1);
431 free(exp2);
432 isl_basic_set_free(bset);
433 isl_aff_free(obj);
434 return isl_lp_error;
437 /* Compute the minimum (maximum if max is set) of the integer affine
438 * expression obj over the points in set and put the result in *opt.
440 * The parameters are assumed to have been aligned.
442 static enum isl_lp_result isl_set_opt_aligned(__isl_keep isl_set *set, int max,
443 __isl_keep isl_aff *obj, isl_int *opt)
445 int i;
446 enum isl_lp_result res;
447 int empty = 1;
448 isl_int opt_i;
450 if (!set || !obj)
451 return isl_lp_error;
452 if (set->n == 0)
453 return isl_lp_empty;
455 res = isl_basic_set_opt(set->p[0], max, obj, opt);
456 if (res == isl_lp_error || res == isl_lp_unbounded)
457 return res;
458 if (set->n == 1)
459 return res;
460 if (res == isl_lp_ok)
461 empty = 0;
463 isl_int_init(opt_i);
464 for (i = 1; i < set->n; ++i) {
465 res = isl_basic_set_opt(set->p[i], max, obj, &opt_i);
466 if (res == isl_lp_error || res == isl_lp_unbounded) {
467 isl_int_clear(opt_i);
468 return res;
470 if (res == isl_lp_ok)
471 empty = 0;
472 if (max ? isl_int_gt(opt_i, *opt) : isl_int_lt(opt_i, *opt))
473 isl_int_set(*opt, opt_i);
475 isl_int_clear(opt_i);
477 return empty ? isl_lp_empty : isl_lp_ok;
480 /* Compute the minimum (maximum if max is set) of the integer affine
481 * expression obj over the points in set and put the result in *opt.
483 enum isl_lp_result isl_set_opt(__isl_keep isl_set *set, int max,
484 __isl_keep isl_aff *obj, isl_int *opt)
486 enum isl_lp_result res;
488 if (!set || !obj)
489 return isl_lp_error;
491 if (isl_space_match(set->dim, isl_dim_param,
492 obj->ls->dim, isl_dim_param))
493 return isl_set_opt_aligned(set, max, obj, opt);
495 set = isl_set_copy(set);
496 obj = isl_aff_copy(obj);
497 set = isl_set_align_params(set, isl_aff_get_domain_space(obj));
498 obj = isl_aff_align_params(obj, isl_set_get_space(set));
500 res = isl_set_opt_aligned(set, max, obj, opt);
502 isl_set_free(set);
503 isl_aff_free(obj);
505 return res;
508 enum isl_lp_result isl_basic_set_max(__isl_keep isl_basic_set *bset,
509 __isl_keep isl_aff *obj, isl_int *opt)
511 return isl_basic_set_opt(bset, 1, obj, opt);
514 enum isl_lp_result isl_set_max(__isl_keep isl_set *set,
515 __isl_keep isl_aff *obj, isl_int *opt)
517 return isl_set_opt(set, 1, obj, opt);
520 enum isl_lp_result isl_set_min(__isl_keep isl_set *set,
521 __isl_keep isl_aff *obj, isl_int *opt)
523 return isl_set_opt(set, 0, obj, opt);
526 /* Convert the result of a function that returns an isl_lp_result
527 * to an isl_val. The numerator of "v" is set to the optimal value
528 * if lp_res is isl_lp_ok. "max" is set if a maximum was computed.
530 * Return "v" with denominator set to 1 if lp_res is isl_lp_ok.
531 * Return NULL on error.
532 * Return a NaN if lp_res is isl_lp_empty.
533 * Return infinity or negative infinity if lp_res is isl_lp_unbounded,
534 * depending on "max".
536 static __isl_give isl_val *convert_lp_result(enum isl_lp_result lp_res,
537 __isl_take isl_val *v, int max)
539 isl_ctx *ctx;
541 if (lp_res == isl_lp_ok) {
542 isl_int_set_si(v->d, 1);
543 return isl_val_normalize(v);
545 ctx = isl_val_get_ctx(v);
546 isl_val_free(v);
547 if (lp_res == isl_lp_error)
548 return NULL;
549 if (lp_res == isl_lp_empty)
550 return isl_val_nan(ctx);
551 if (max)
552 return isl_val_infty(ctx);
553 else
554 return isl_val_neginfty(ctx);
557 /* Return the minimum (maximum if max is set) of the integer affine
558 * expression "obj" over the points in "bset".
560 * Return infinity or negative infinity if the optimal value is unbounded and
561 * NaN if "bset" is empty.
563 * Call isl_basic_set_opt and translate the results.
565 __isl_give isl_val *isl_basic_set_opt_val(__isl_keep isl_basic_set *bset,
566 int max, __isl_keep isl_aff *obj)
568 isl_ctx *ctx;
569 isl_val *res;
570 enum isl_lp_result lp_res;
572 if (!bset || !obj)
573 return NULL;
575 ctx = isl_aff_get_ctx(obj);
576 res = isl_val_alloc(ctx);
577 if (!res)
578 return NULL;
579 lp_res = isl_basic_set_opt(bset, max, obj, &res->n);
580 return convert_lp_result(lp_res, res, max);
583 /* Return the maximum of the integer affine
584 * expression "obj" over the points in "bset".
586 * Return infinity or negative infinity if the optimal value is unbounded and
587 * NaN if "bset" is empty.
589 __isl_give isl_val *isl_basic_set_max_val(__isl_keep isl_basic_set *bset,
590 __isl_keep isl_aff *obj)
592 return isl_basic_set_opt_val(bset, 1, obj);
595 /* Return the minimum (maximum if max is set) of the integer affine
596 * expression "obj" over the points in "set".
598 * Return infinity or negative infinity if the optimal value is unbounded and
599 * NaN if "set" is empty.
601 * Call isl_set_opt and translate the results.
603 __isl_give isl_val *isl_set_opt_val(__isl_keep isl_set *set, int max,
604 __isl_keep isl_aff *obj)
606 isl_ctx *ctx;
607 isl_val *res;
608 enum isl_lp_result lp_res;
610 if (!set || !obj)
611 return NULL;
613 ctx = isl_aff_get_ctx(obj);
614 res = isl_val_alloc(ctx);
615 if (!res)
616 return NULL;
617 lp_res = isl_set_opt(set, max, obj, &res->n);
618 return convert_lp_result(lp_res, res, max);
621 /* Return the minimum of the integer affine
622 * expression "obj" over the points in "set".
624 * Return infinity or negative infinity if the optimal value is unbounded and
625 * NaN if "set" is empty.
627 __isl_give isl_val *isl_set_min_val(__isl_keep isl_set *set,
628 __isl_keep isl_aff *obj)
630 return isl_set_opt_val(set, 0, obj);
633 /* Return the maximum of the integer affine
634 * expression "obj" over the points in "set".
636 * Return infinity or negative infinity if the optimal value is unbounded and
637 * NaN if "set" is empty.
639 __isl_give isl_val *isl_set_max_val(__isl_keep isl_set *set,
640 __isl_keep isl_aff *obj)
642 return isl_set_opt_val(set, 1, obj);
645 /* Return the optimum (min or max depending on "max") of "v1" and "v2",
646 * where either may be NaN, signifying an uninitialized value.
647 * That is, if either is NaN, then return the other one.
649 static __isl_give isl_val *val_opt(__isl_take isl_val *v1,
650 __isl_take isl_val *v2, int max)
652 if (!v1 || !v2)
653 goto error;
654 if (isl_val_is_nan(v1)) {
655 isl_val_free(v1);
656 return v2;
658 if (isl_val_is_nan(v2)) {
659 isl_val_free(v2);
660 return v1;
662 if (max)
663 return isl_val_max(v1, v2);
664 else
665 return isl_val_min(v1, v2);
666 error:
667 isl_val_free(v1);
668 isl_val_free(v2);
669 return NULL;
672 /* Internal data structure for isl_set_opt_pw_aff.
674 * "max" is set if the maximum should be computed.
675 * "set" is the set over which the optimum should be computed.
676 * "res" contains the current optimum and is initialized to NaN.
678 struct isl_set_opt_data {
679 int max;
680 isl_set *set;
682 isl_val *res;
685 /* Update the optimum in data->res with respect to the affine function
686 * "aff" defined over "set".
688 static isl_stat piece_opt(__isl_take isl_set *set, __isl_take isl_aff *aff,
689 void *user)
691 struct isl_set_opt_data *data = user;
692 isl_val *opt;
694 set = isl_set_intersect(set, isl_set_copy(data->set));
695 opt = isl_set_opt_val(set, data->max, aff);
696 isl_set_free(set);
697 isl_aff_free(aff);
699 data->res = val_opt(data->res, opt, data->max);
700 if (!data->res)
701 return isl_stat_error;
703 return isl_stat_ok;
706 /* Return the minimum (maximum if "max" is set) of the integer piecewise affine
707 * expression "obj" over the points in "set".
709 * Return infinity or negative infinity if the optimal value is unbounded and
710 * NaN if the intersection of "set" with the domain of "obj" is empty.
712 * Initialize the result to NaN and then update it for each of the pieces
713 * in "obj".
715 static __isl_give isl_val *isl_set_opt_pw_aff(__isl_keep isl_set *set, int max,
716 __isl_keep isl_pw_aff *obj)
718 struct isl_set_opt_data data = { max, set };
720 data.res = isl_val_nan(isl_set_get_ctx(set));
721 if (isl_pw_aff_foreach_piece(obj, &piece_opt, &data) < 0)
722 return isl_val_free(data.res);
724 return data.res;
727 /* Internal data structure for isl_union_set_opt_union_pw_aff.
729 * "max" is set if the maximum should be computed.
730 * "obj" is the objective function that needs to be optimized.
731 * "res" contains the current optimum and is initialized to NaN.
733 struct isl_union_set_opt_data {
734 int max;
735 isl_union_pw_aff *obj;
737 isl_val *res;
740 /* Update the optimum in data->res with the optimum over "set".
741 * Do so by first extracting the matching objective function
742 * from data->obj.
744 static isl_stat set_opt(__isl_take isl_set *set, void *user)
746 struct isl_union_set_opt_data *data = user;
747 isl_space *space;
748 isl_pw_aff *pa;
749 isl_val *opt;
751 space = isl_set_get_space(set);
752 space = isl_space_from_domain(space);
753 space = isl_space_add_dims(space, isl_dim_out, 1);
754 pa = isl_union_pw_aff_extract_pw_aff(data->obj, space);
755 opt = isl_set_opt_pw_aff(set, data->max, pa);
756 isl_pw_aff_free(pa);
757 isl_set_free(set);
759 data->res = val_opt(data->res, opt, data->max);
760 if (!data->res)
761 return isl_stat_error;
763 return isl_stat_ok;
766 /* Return the minimum (maximum if "max" is set) of the integer piecewise affine
767 * expression "obj" over the points in "uset".
769 * Return infinity or negative infinity if the optimal value is unbounded and
770 * NaN if the intersection of "uset" with the domain of "obj" is empty.
772 * Initialize the result to NaN and then update it for each of the sets
773 * in "uset".
775 static __isl_give isl_val *isl_union_set_opt_union_pw_aff(
776 __isl_keep isl_union_set *uset, int max,
777 __isl_keep isl_union_pw_aff *obj)
779 struct isl_union_set_opt_data data = { max, obj };
781 data.res = isl_val_nan(isl_union_set_get_ctx(uset));
782 if (isl_union_set_foreach_set(uset, &set_opt, &data) < 0)
783 return isl_val_free(data.res);
785 return data.res;
788 /* Return a list of minima (maxima if "max" is set) over the points in "uset"
789 * for each of the expressions in "obj".
791 * An element in the list is infinity or negative infinity if the optimal
792 * value of the corresponding expression is unbounded and
793 * NaN if the intersection of "uset" with the domain of the expression
794 * is empty.
796 * Iterate over all the expressions in "obj" and collect the results.
798 static __isl_give isl_multi_val *isl_union_set_opt_multi_union_pw_aff(
799 __isl_keep isl_union_set *uset, int max,
800 __isl_keep isl_multi_union_pw_aff *obj)
802 int i, n;
803 isl_multi_val *mv;
805 if (!uset || !obj)
806 return NULL;
808 n = isl_multi_union_pw_aff_dim(obj, isl_dim_set);
809 mv = isl_multi_val_zero(isl_multi_union_pw_aff_get_space(obj));
811 for (i = 0; i < n; ++i) {
812 isl_val *v;
813 isl_union_pw_aff *upa;
815 upa = isl_multi_union_pw_aff_get_union_pw_aff(obj, i);
816 v = isl_union_set_opt_union_pw_aff(uset, max, upa);
817 isl_union_pw_aff_free(upa);
818 mv = isl_multi_val_set_val(mv, i, v);
821 return mv;
824 /* Return a list of minima over the points in "uset"
825 * for each of the expressions in "obj".
827 * An element in the list is infinity or negative infinity if the optimal
828 * value of the corresponding expression is unbounded and
829 * NaN if the intersection of "uset" with the domain of the expression
830 * is empty.
832 __isl_give isl_multi_val *isl_union_set_min_multi_union_pw_aff(
833 __isl_keep isl_union_set *uset, __isl_keep isl_multi_union_pw_aff *obj)
835 return isl_union_set_opt_multi_union_pw_aff(uset, 0, obj);