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"
17 enum isl_lp_result isl_tab_solve_lp(struct isl_basic_map *bmap, int maximize,
18 isl_int *f, isl_int denom, isl_int *opt,
23 enum isl_lp_result res;
24 unsigned dim = isl_basic_map_total_dim(bmap);
27 isl_seq_neg(f, f, 1 + dim);
29 bmap = isl_basic_map_gauss(bmap, NULL);
30 tab = isl_tab_from_basic_map(bmap);
31 res = isl_tab_min(tab, f, denom, opt, opt_denom, 0);
32 if (res == isl_lp_ok && sol) {
33 *sol = isl_tab_get_sample_value(tab);
40 isl_seq_neg(f, f, 1 + dim);
42 isl_int_neg(*opt, *opt);
47 /* Given a basic map "bmap" and an affine combination of the variables "f"
48 * with denominator "denom", set *opt / *opt_denom to the minimal
49 * (or maximal if "maximize" is true) value attained by f/d over "bmap",
50 * assuming the basic map is not empty and the expression cannot attain
51 * arbitrarily small (or large) values.
52 * If opt_denom is NULL, then *opt is rounded up (or down)
53 * to the nearest integer.
54 * The return value reflects the nature of the result (empty, unbounded,
55 * minmimal or maximal value returned in *opt).
57 enum isl_lp_result isl_basic_map_solve_lp(struct isl_basic_map *bmap, int max,
58 isl_int *f, isl_int d, isl_int *opt,
68 switch (bmap->ctx->opt->lp_solver) {
70 return isl_pip_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
72 return isl_tab_solve_lp(bmap, max, f, d, opt, opt_denom, sol);
78 enum isl_lp_result isl_basic_set_solve_lp(struct isl_basic_set *bset, int max,
79 isl_int *f, isl_int d, isl_int *opt,
83 return isl_basic_map_solve_lp((struct isl_basic_map *)bset, max,
84 f, d, opt, opt_denom, sol);
87 enum isl_lp_result isl_map_solve_lp(__isl_keep isl_map *map, int max,
88 isl_int *f, isl_int d, isl_int *opt,
97 enum isl_lp_result res;
107 for (i = 0; i < map->n; ++i)
108 if (map->p[i]->n_div > max_div)
109 max_div = map->p[i]->n_div;
111 unsigned total = isl_dim_total(map->dim);
112 v = isl_vec_alloc(map->ctx, 1 + total + max_div);
115 isl_seq_cpy(v->el, f, 1 + total);
116 isl_seq_clr(v->el + 1 + total, max_div);
120 if (!opt && map->n > 1 && sol) {
126 if (map->n > 0 && opt_denom) {
127 isl_int_init(opt_denom_i);
131 res = isl_basic_map_solve_lp(map->p[0], max, f, d,
132 opt, opt_denom, sol);
133 if (res == isl_lp_error || res == isl_lp_unbounded)
139 for (i = 1; i < map->n; ++i) {
140 isl_vec *sol_i = NULL;
141 enum isl_lp_result res_i;
144 res_i = isl_basic_map_solve_lp(map->p[i], max, f, d,
146 opt_denom ? &opt_denom_i : NULL,
147 sol ? &sol_i : NULL);
148 if (res_i == isl_lp_error || res_i == isl_lp_unbounded) {
152 if (res_i == isl_lp_empty)
154 if (res == isl_lp_empty) {
156 } else if (!opt_denom) {
158 better = isl_int_gt(opt_i, *opt);
160 better = isl_int_lt(opt_i, *opt);
162 isl_int_mul(t, opt_i, *opt_denom);
163 isl_int_submul(t, *opt, opt_denom_i);
165 better = isl_int_is_pos(t);
167 better = isl_int_is_neg(t);
172 isl_int_set(*opt, opt_i);
174 isl_int_set(*opt_denom, opt_denom_i);
185 if (map->n > 0 && opt_denom) {
186 isl_int_clear(opt_denom_i);
190 isl_int_clear(opt_i);
196 enum isl_lp_result isl_set_solve_lp(__isl_keep isl_set *set, int max,
197 isl_int *f, isl_int d, isl_int *opt,
199 struct isl_vec **sol)
201 return isl_map_solve_lp((struct isl_map *)set, max,
202 f, d, opt, opt_denom, sol);