152a25237STobias Grosser /*
252a25237STobias Grosser * Copyright 2008-2009 Katholieke Universiteit Leuven
352a25237STobias Grosser *
452a25237STobias Grosser * Use of this software is governed by the MIT license
552a25237STobias Grosser *
652a25237STobias Grosser * Written by Sven Verdoolaege, K.U.Leuven, Departement
752a25237STobias Grosser * Computerwetenschappen, Celestijnenlaan 200A, B-3001 Leuven, Belgium
852a25237STobias Grosser */
952a25237STobias Grosser
1052a25237STobias Grosser #include <assert.h>
1152a25237STobias Grosser #include <isl_map_private.h>
1252a25237STobias Grosser #include <isl_seq.h>
1352a25237STobias Grosser #include "isl_tab.h"
1452a25237STobias Grosser #include <isl_int.h>
1552a25237STobias Grosser #include <isl_config.h>
1652a25237STobias Grosser
1752a25237STobias Grosser struct tab_lp {
1852a25237STobias Grosser struct isl_ctx *ctx;
1952a25237STobias Grosser struct isl_vec *row;
2052a25237STobias Grosser struct isl_tab *tab;
2152a25237STobias Grosser struct isl_tab_undo **stack;
2252a25237STobias Grosser isl_int *obj;
2352a25237STobias Grosser isl_int opt;
2452a25237STobias Grosser isl_int opt_denom;
2552a25237STobias Grosser isl_int tmp;
2652a25237STobias Grosser isl_int tmp2;
2752a25237STobias Grosser int neq;
2852a25237STobias Grosser unsigned dim;
2952a25237STobias Grosser /* number of constraints in initial product tableau */
3052a25237STobias Grosser int con_offset;
3152a25237STobias Grosser /* objective function has fixed or no integer value */
3252a25237STobias Grosser int is_fixed;
3352a25237STobias Grosser };
3452a25237STobias Grosser
3552a25237STobias Grosser #ifdef USE_GMP_FOR_MP
3652a25237STobias Grosser #define GBR_type mpq_t
3752a25237STobias Grosser #define GBR_init(v) mpq_init(v)
3852a25237STobias Grosser #define GBR_clear(v) mpq_clear(v)
3952a25237STobias Grosser #define GBR_set(a,b) mpq_set(a,b)
4052a25237STobias Grosser #define GBR_set_ui(a,b) mpq_set_ui(a,b,1)
4152a25237STobias Grosser #define GBR_mul(a,b,c) mpq_mul(a,b,c)
4252a25237STobias Grosser #define GBR_lt(a,b) (mpq_cmp(a,b) < 0)
4352a25237STobias Grosser #define GBR_is_zero(a) (mpq_sgn(a) == 0)
4452a25237STobias Grosser #define GBR_numref(a) mpq_numref(a)
4552a25237STobias Grosser #define GBR_denref(a) mpq_denref(a)
4652a25237STobias Grosser #define GBR_floor(a,b) mpz_fdiv_q(a,GBR_numref(b),GBR_denref(b))
4752a25237STobias Grosser #define GBR_ceil(a,b) mpz_cdiv_q(a,GBR_numref(b),GBR_denref(b))
48c59f22c5SMichael Kruse #define GBR_set_num_neg(a, b) mpz_neg(GBR_numref(*a), b);
49c59f22c5SMichael Kruse #define GBR_set_den(a, b) mpz_set(GBR_denref(*a), b);
5052a25237STobias Grosser #endif /* USE_GMP_FOR_MP */
5152a25237STobias Grosser
5252a25237STobias Grosser #ifdef USE_IMATH_FOR_MP
5352a25237STobias Grosser #include <imrat.h>
5452a25237STobias Grosser
5552a25237STobias Grosser #define GBR_type mp_rat
5652a25237STobias Grosser #define GBR_init(v) v = mp_rat_alloc()
5752a25237STobias Grosser #define GBR_clear(v) mp_rat_free(v)
5852a25237STobias Grosser #define GBR_set(a,b) mp_rat_copy(b,a)
5952a25237STobias Grosser #define GBR_set_ui(a,b) mp_rat_set_uvalue(a,b,1)
6052a25237STobias Grosser #define GBR_mul(a,b,c) mp_rat_mul(b,c,a)
6152a25237STobias Grosser #define GBR_lt(a,b) (mp_rat_compare(a,b) < 0)
6252a25237STobias Grosser #define GBR_is_zero(a) (mp_rat_compare_zero(a) == 0)
63c59f22c5SMichael Kruse #ifdef USE_SMALL_INT_OPT
64c59f22c5SMichael Kruse #define GBR_numref(a) isl_sioimath_encode_big(mp_rat_numer_ref(a))
65c59f22c5SMichael Kruse #define GBR_denref(a) isl_sioimath_encode_big(mp_rat_denom_ref(a))
66*f560ca90STobias Grosser #define GBR_floor(a, b) isl_sioimath_fdiv_q((a), GBR_numref(b), GBR_denref(b))
67*f560ca90STobias Grosser #define GBR_ceil(a, b) isl_sioimath_cdiv_q((a), GBR_numref(b), GBR_denref(b))
68c59f22c5SMichael Kruse #define GBR_set_num_neg(a, b) \
69c59f22c5SMichael Kruse do { \
70c59f22c5SMichael Kruse isl_sioimath_scratchspace_t scratch; \
71c59f22c5SMichael Kruse impz_neg(mp_rat_numer_ref(*a), \
72*f560ca90STobias Grosser isl_sioimath_bigarg_src(*b, &scratch));\
73c59f22c5SMichael Kruse } while (0)
74c59f22c5SMichael Kruse #define GBR_set_den(a, b) \
75c59f22c5SMichael Kruse do { \
76c59f22c5SMichael Kruse isl_sioimath_scratchspace_t scratch; \
77c59f22c5SMichael Kruse impz_set(mp_rat_denom_ref(*a), \
78*f560ca90STobias Grosser isl_sioimath_bigarg_src(*b, &scratch));\
79c59f22c5SMichael Kruse } while (0)
80c59f22c5SMichael Kruse #else /* USE_SMALL_INT_OPT */
8152a25237STobias Grosser #define GBR_numref(a) mp_rat_numer_ref(a)
8252a25237STobias Grosser #define GBR_denref(a) mp_rat_denom_ref(a)
8352a25237STobias Grosser #define GBR_floor(a,b) impz_fdiv_q(a,GBR_numref(b),GBR_denref(b))
8452a25237STobias Grosser #define GBR_ceil(a,b) impz_cdiv_q(a,GBR_numref(b),GBR_denref(b))
85c59f22c5SMichael Kruse #define GBR_set_num_neg(a, b) impz_neg(GBR_numref(*a), b)
86c59f22c5SMichael Kruse #define GBR_set_den(a, b) impz_set(GBR_denref(*a), b)
87c59f22c5SMichael Kruse #endif /* USE_SMALL_INT_OPT */
8852a25237STobias Grosser #endif /* USE_IMATH_FOR_MP */
8952a25237STobias Grosser
9052a25237STobias Grosser static struct tab_lp *init_lp(struct isl_tab *tab);
9152a25237STobias Grosser static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim);
9252a25237STobias Grosser static int solve_lp(struct tab_lp *lp);
9352a25237STobias Grosser static void get_obj_val(struct tab_lp* lp, GBR_type *F);
9452a25237STobias Grosser static void delete_lp(struct tab_lp *lp);
9552a25237STobias Grosser static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim);
9652a25237STobias Grosser static void get_alpha(struct tab_lp* lp, int row, GBR_type *alpha);
9752a25237STobias Grosser static int del_lp_row(struct tab_lp *lp) WARN_UNUSED;
9852a25237STobias Grosser static int cut_lp_to_hyperplane(struct tab_lp *lp, isl_int *row);
9952a25237STobias Grosser
10052a25237STobias Grosser #define GBR_LP struct tab_lp
10152a25237STobias Grosser #define GBR_lp_init(P) init_lp(P)
10252a25237STobias Grosser #define GBR_lp_set_obj(lp, obj, dim) set_lp_obj(lp, obj, dim)
10352a25237STobias Grosser #define GBR_lp_solve(lp) solve_lp(lp)
10452a25237STobias Grosser #define GBR_lp_get_obj_val(lp, F) get_obj_val(lp, F)
10552a25237STobias Grosser #define GBR_lp_delete(lp) delete_lp(lp)
10652a25237STobias Grosser #define GBR_lp_next_row(lp) lp->neq
10752a25237STobias Grosser #define GBR_lp_add_row(lp, row, dim) add_lp_row(lp, row, dim)
10852a25237STobias Grosser #define GBR_lp_get_alpha(lp, row, alpha) get_alpha(lp, row, alpha)
10952a25237STobias Grosser #define GBR_lp_del_row(lp) del_lp_row(lp)
11052a25237STobias Grosser #define GBR_lp_is_fixed(lp) (lp)->is_fixed
11152a25237STobias Grosser #define GBR_lp_cut(lp, obj) cut_lp_to_hyperplane(lp, obj)
11252a25237STobias Grosser #include "basis_reduction_templ.c"
11352a25237STobias Grosser
11452a25237STobias Grosser /* Set up a tableau for the Cartesian product of bset with itself.
11552a25237STobias Grosser * This could be optimized by first setting up a tableau for bset
11652a25237STobias Grosser * and then performing the Cartesian product on the tableau.
11752a25237STobias Grosser */
gbr_tab(struct isl_tab * tab,struct isl_vec * row)11852a25237STobias Grosser static struct isl_tab *gbr_tab(struct isl_tab *tab, struct isl_vec *row)
11952a25237STobias Grosser {
12052a25237STobias Grosser unsigned dim;
12152a25237STobias Grosser struct isl_tab *prod;
12252a25237STobias Grosser
12352a25237STobias Grosser if (!tab || !row)
12452a25237STobias Grosser return NULL;
12552a25237STobias Grosser
12652a25237STobias Grosser dim = tab->n_var;
12752a25237STobias Grosser prod = isl_tab_product(tab, tab);
12852a25237STobias Grosser if (isl_tab_extend_cons(prod, 3 * dim + 1) < 0) {
12952a25237STobias Grosser isl_tab_free(prod);
13052a25237STobias Grosser return NULL;
13152a25237STobias Grosser }
13252a25237STobias Grosser return prod;
13352a25237STobias Grosser }
13452a25237STobias Grosser
init_lp(struct isl_tab * tab)13552a25237STobias Grosser static struct tab_lp *init_lp(struct isl_tab *tab)
13652a25237STobias Grosser {
13752a25237STobias Grosser struct tab_lp *lp = NULL;
13852a25237STobias Grosser
13952a25237STobias Grosser if (!tab)
14052a25237STobias Grosser return NULL;
14152a25237STobias Grosser
14252a25237STobias Grosser lp = isl_calloc_type(tab->mat->ctx, struct tab_lp);
14352a25237STobias Grosser if (!lp)
14452a25237STobias Grosser return NULL;
14552a25237STobias Grosser
14652a25237STobias Grosser isl_int_init(lp->opt);
14752a25237STobias Grosser isl_int_init(lp->opt_denom);
14852a25237STobias Grosser isl_int_init(lp->tmp);
14952a25237STobias Grosser isl_int_init(lp->tmp2);
15052a25237STobias Grosser
15152a25237STobias Grosser lp->dim = tab->n_var;
15252a25237STobias Grosser
15352a25237STobias Grosser lp->ctx = tab->mat->ctx;
15452a25237STobias Grosser isl_ctx_ref(lp->ctx);
15552a25237STobias Grosser
15652a25237STobias Grosser lp->stack = isl_alloc_array(lp->ctx, struct isl_tab_undo *, lp->dim);
15752a25237STobias Grosser
15852a25237STobias Grosser lp->row = isl_vec_alloc(lp->ctx, 1 + 2 * lp->dim);
15952a25237STobias Grosser if (!lp->row)
16052a25237STobias Grosser goto error;
16152a25237STobias Grosser lp->tab = gbr_tab(tab, lp->row);
16252a25237STobias Grosser if (!lp->tab)
16352a25237STobias Grosser goto error;
16452a25237STobias Grosser lp->con_offset = lp->tab->n_con;
16552a25237STobias Grosser lp->obj = NULL;
16652a25237STobias Grosser lp->neq = 0;
16752a25237STobias Grosser
16852a25237STobias Grosser return lp;
16952a25237STobias Grosser error:
17052a25237STobias Grosser delete_lp(lp);
17152a25237STobias Grosser return NULL;
17252a25237STobias Grosser }
17352a25237STobias Grosser
set_lp_obj(struct tab_lp * lp,isl_int * row,int dim)17452a25237STobias Grosser static void set_lp_obj(struct tab_lp *lp, isl_int *row, int dim)
17552a25237STobias Grosser {
17652a25237STobias Grosser lp->obj = row;
17752a25237STobias Grosser }
17852a25237STobias Grosser
solve_lp(struct tab_lp * lp)17952a25237STobias Grosser static int solve_lp(struct tab_lp *lp)
18052a25237STobias Grosser {
18152a25237STobias Grosser enum isl_lp_result res;
18252a25237STobias Grosser unsigned flags = 0;
18352a25237STobias Grosser
18452a25237STobias Grosser lp->is_fixed = 0;
18552a25237STobias Grosser
18652a25237STobias Grosser isl_int_set_si(lp->row->el[0], 0);
18752a25237STobias Grosser isl_seq_cpy(lp->row->el + 1, lp->obj, lp->dim);
18852a25237STobias Grosser isl_seq_neg(lp->row->el + 1 + lp->dim, lp->obj, lp->dim);
18952a25237STobias Grosser if (lp->neq)
19052a25237STobias Grosser flags = ISL_TAB_SAVE_DUAL;
19152a25237STobias Grosser res = isl_tab_min(lp->tab, lp->row->el, lp->ctx->one,
19252a25237STobias Grosser &lp->opt, &lp->opt_denom, flags);
19352a25237STobias Grosser isl_int_mul_ui(lp->opt_denom, lp->opt_denom, 2);
19452a25237STobias Grosser if (isl_int_abs_lt(lp->opt, lp->opt_denom)) {
19552a25237STobias Grosser struct isl_vec *sample = isl_tab_get_sample_value(lp->tab);
19652a25237STobias Grosser if (!sample)
19752a25237STobias Grosser return -1;
19852a25237STobias Grosser isl_seq_inner_product(lp->obj, sample->el + 1, lp->dim, &lp->tmp);
19952a25237STobias Grosser isl_seq_inner_product(lp->obj, sample->el + 1 + lp->dim, lp->dim, &lp->tmp2);
20052a25237STobias Grosser isl_int_cdiv_q(lp->tmp, lp->tmp, sample->el[0]);
20152a25237STobias Grosser isl_int_fdiv_q(lp->tmp2, lp->tmp2, sample->el[0]);
20252a25237STobias Grosser if (isl_int_ge(lp->tmp, lp->tmp2))
20352a25237STobias Grosser lp->is_fixed = 1;
20452a25237STobias Grosser isl_vec_free(sample);
20552a25237STobias Grosser }
20652a25237STobias Grosser isl_int_divexact_ui(lp->opt_denom, lp->opt_denom, 2);
2073e6070efSTobias Grosser if (res < 0)
20852a25237STobias Grosser return -1;
2093e6070efSTobias Grosser if (res != isl_lp_ok)
2103e6070efSTobias Grosser isl_die(lp->ctx, isl_error_internal,
2113e6070efSTobias Grosser "unexpected missing (bounded) solution", return -1);
21252a25237STobias Grosser return 0;
21352a25237STobias Grosser }
21452a25237STobias Grosser
21552a25237STobias Grosser /* The current objective function has a fixed (or no) integer value.
21652a25237STobias Grosser * Cut the tableau to the hyperplane that fixes this value in
21752a25237STobias Grosser * both halves of the tableau.
21852a25237STobias Grosser * Return 1 if the resulting tableau is empty.
21952a25237STobias Grosser */
cut_lp_to_hyperplane(struct tab_lp * lp,isl_int * row)22052a25237STobias Grosser static int cut_lp_to_hyperplane(struct tab_lp *lp, isl_int *row)
22152a25237STobias Grosser {
22252a25237STobias Grosser enum isl_lp_result res;
22352a25237STobias Grosser
22452a25237STobias Grosser isl_int_set_si(lp->row->el[0], 0);
22552a25237STobias Grosser isl_seq_cpy(lp->row->el + 1, row, lp->dim);
22652a25237STobias Grosser isl_seq_clr(lp->row->el + 1 + lp->dim, lp->dim);
22752a25237STobias Grosser res = isl_tab_min(lp->tab, lp->row->el, lp->ctx->one,
22852a25237STobias Grosser &lp->tmp, NULL, 0);
22952a25237STobias Grosser if (res != isl_lp_ok)
23052a25237STobias Grosser return -1;
23152a25237STobias Grosser
23252a25237STobias Grosser isl_int_neg(lp->row->el[0], lp->tmp);
23352a25237STobias Grosser if (isl_tab_add_eq(lp->tab, lp->row->el) < 0)
23452a25237STobias Grosser return -1;
23552a25237STobias Grosser
23652a25237STobias Grosser isl_seq_cpy(lp->row->el + 1 + lp->dim, row, lp->dim);
23752a25237STobias Grosser isl_seq_clr(lp->row->el + 1, lp->dim);
23852a25237STobias Grosser if (isl_tab_add_eq(lp->tab, lp->row->el) < 0)
23952a25237STobias Grosser return -1;
24052a25237STobias Grosser
24152a25237STobias Grosser lp->con_offset += 2;
24252a25237STobias Grosser
24352a25237STobias Grosser return lp->tab->empty;
24452a25237STobias Grosser }
24552a25237STobias Grosser
get_obj_val(struct tab_lp * lp,GBR_type * F)24652a25237STobias Grosser static void get_obj_val(struct tab_lp* lp, GBR_type *F)
24752a25237STobias Grosser {
248c59f22c5SMichael Kruse GBR_set_num_neg(F, lp->opt);
249c59f22c5SMichael Kruse GBR_set_den(F, lp->opt_denom);
25052a25237STobias Grosser }
25152a25237STobias Grosser
delete_lp(struct tab_lp * lp)25252a25237STobias Grosser static void delete_lp(struct tab_lp *lp)
25352a25237STobias Grosser {
25452a25237STobias Grosser if (!lp)
25552a25237STobias Grosser return;
25652a25237STobias Grosser
25752a25237STobias Grosser isl_int_clear(lp->opt);
25852a25237STobias Grosser isl_int_clear(lp->opt_denom);
25952a25237STobias Grosser isl_int_clear(lp->tmp);
26052a25237STobias Grosser isl_int_clear(lp->tmp2);
26152a25237STobias Grosser isl_vec_free(lp->row);
26252a25237STobias Grosser free(lp->stack);
26352a25237STobias Grosser isl_tab_free(lp->tab);
26452a25237STobias Grosser isl_ctx_deref(lp->ctx);
26552a25237STobias Grosser free(lp);
26652a25237STobias Grosser }
26752a25237STobias Grosser
add_lp_row(struct tab_lp * lp,isl_int * row,int dim)26852a25237STobias Grosser static int add_lp_row(struct tab_lp *lp, isl_int *row, int dim)
26952a25237STobias Grosser {
27052a25237STobias Grosser lp->stack[lp->neq] = isl_tab_snap(lp->tab);
27152a25237STobias Grosser
27252a25237STobias Grosser isl_int_set_si(lp->row->el[0], 0);
27352a25237STobias Grosser isl_seq_cpy(lp->row->el + 1, row, lp->dim);
27452a25237STobias Grosser isl_seq_neg(lp->row->el + 1 + lp->dim, row, lp->dim);
27552a25237STobias Grosser
27652a25237STobias Grosser if (isl_tab_add_valid_eq(lp->tab, lp->row->el) < 0)
27752a25237STobias Grosser return -1;
27852a25237STobias Grosser
27952a25237STobias Grosser return lp->neq++;
28052a25237STobias Grosser }
28152a25237STobias Grosser
get_alpha(struct tab_lp * lp,int row,GBR_type * alpha)28252a25237STobias Grosser static void get_alpha(struct tab_lp* lp, int row, GBR_type *alpha)
28352a25237STobias Grosser {
28452a25237STobias Grosser row += lp->con_offset;
285c59f22c5SMichael Kruse GBR_set_num_neg(alpha, lp->tab->dual->el[1 + row]);
286c59f22c5SMichael Kruse GBR_set_den(alpha, lp->tab->dual->el[0]);
28752a25237STobias Grosser }
28852a25237STobias Grosser
del_lp_row(struct tab_lp * lp)28952a25237STobias Grosser static int del_lp_row(struct tab_lp *lp)
29052a25237STobias Grosser {
29152a25237STobias Grosser lp->neq--;
29252a25237STobias Grosser return isl_tab_rollback(lp->tab, lp->stack[lp->neq]);
29352a25237STobias Grosser }
294