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
10 #include <isl_ctx_private.h>
11 #include <isl_map_private.h>
13 #include "isl_lp_piplib.h"
16 #include <isl_options_private.h>
18 enum isl_lp_result isl_tab_solve_lp(struct isl_basic_map *bmap, int maximize,
19 isl_int *f, isl_int denom, isl_int *opt,
24 enum isl_lp_result res;
25 unsigned dim = isl_basic_map_total_dim(bmap);
28 isl_seq_neg(f, f, 1 + dim);
30 bmap = isl_basic_map_gauss(bmap, NULL);
31 tab = isl_tab_from_basic_map(bmap, 0);
32 res = isl_tab_min(tab, f, denom, opt, opt_denom, 0);
33 if (res == isl_lp_ok && sol) {
34 *sol = isl_tab_get_sample_value(tab);
41 isl_seq_neg(f, f, 1 + dim);
43 isl_int_neg(*opt, *opt);
48 /* Given a basic map "bmap" and an affine combination of the variables "f"
49 * with denominator "denom", set *opt / *opt_denom to the minimal
50 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
51 * assuming the basic map is not empty and the expression cannot attain
52 * arbitrarily small (or large) values.
53 * If opt_denom is NULL, then *opt is rounded up (or down)
54 * to the nearest integer.
55 * The return value reflects the nature of the result (empty, unbounded,
56 * minmimal or maximal value returned in *opt).
58 enum isl_lp_result isl_basic_map_solve_lp(struct isl_basic_map *bmap, int max,
59 isl_int *f, isl_int d, isl_int *opt,
69 switch (bmap->ctx->opt->lp_solver) {
71 return isl_pip_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
73 return isl_tab_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
79 enum isl_lp_result isl_basic_set_solve_lp(struct isl_basic_set *bset, int max,
80 isl_int *f, isl_int d, isl_int *opt,
84 return isl_basic_map_solve_lp((struct isl_basic_map *)bset, max,
85 f, d, opt, opt_denom, sol);
88 enum isl_lp_result isl_map_solve_lp(__isl_keep isl_map *map, int max,
89 isl_int *f, isl_int d, isl_int *opt,
98 enum isl_lp_result res;
108 for (i = 0; i < map->n; ++i)
109 if (map->p[i]->n_div > max_div)
110 max_div = map->p[i]->n_div;
112 unsigned total = isl_space_dim(map->dim, isl_dim_all);
113 v = isl_vec_alloc(map->ctx, 1 + total + max_div);
116 isl_seq_cpy(v->el, f, 1 + total);
117 isl_seq_clr(v->el + 1 + total, max_div);
121 if (!opt && map->n > 1 && sol) {
127 if (map->n > 0 && opt_denom) {
128 isl_int_init(opt_denom_i);
132 res = isl_basic_map_solve_lp(map->p[0], max, f, d,
133 opt, opt_denom, sol);
134 if (res == isl_lp_error || res == isl_lp_unbounded)
140 for (i = 1; i < map->n; ++i) {
141 isl_vec *sol_i = NULL;
142 enum isl_lp_result res_i;
145 res_i = isl_basic_map_solve_lp(map->p[i], max, f, d,
147 opt_denom ? &opt_denom_i : NULL,
148 sol ? &sol_i : NULL);
149 if (res_i == isl_lp_error || res_i == isl_lp_unbounded) {
153 if (res_i == isl_lp_empty)
155 if (res == isl_lp_empty) {
157 } else if (!opt_denom) {
159 better = isl_int_gt(opt_i, *opt);
161 better = isl_int_lt(opt_i, *opt);
163 isl_int_mul(t, opt_i, *opt_denom);
164 isl_int_submul(t, *opt, opt_denom_i);
166 better = isl_int_is_pos(t);
168 better = isl_int_is_neg(t);
173 isl_int_set(*opt, opt_i);
175 isl_int_set(*opt_denom, opt_denom_i);
186 if (map->n > 0 && opt_denom) {
187 isl_int_clear(opt_denom_i);
191 isl_int_clear(opt_i);
197 enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max,
198 isl_int *f, isl_int d, isl_int *opt,
200 struct isl_vec **sol)
202 return isl_map_solve_lp((struct isl_map *)set, max,
203 f, d, opt, opt_denom, sol);