Exemplo n.º 1
0
static struct isl_vec *interval_sample(struct isl_basic_set *bset)
{
	int i;
	isl_int t;
	struct isl_vec *sample;

	bset = isl_basic_set_simplify(bset);
	if (!bset)
		return NULL;
	if (isl_basic_set_plain_is_empty(bset))
		return empty_sample(bset);
	if (bset->n_eq == 0 && bset->n_ineq == 0)
		return zero_sample(bset);

	sample = isl_vec_alloc(bset->ctx, 2);
	if (!sample)
		goto error;
	if (!bset)
		return NULL;
	isl_int_set_si(sample->block.data[0], 1);

	if (bset->n_eq > 0) {
		isl_assert(bset->ctx, bset->n_eq == 1, goto error);
		isl_assert(bset->ctx, bset->n_ineq == 0, goto error);
		if (isl_int_is_one(bset->eq[0][1]))
			isl_int_neg(sample->el[1], bset->eq[0][0]);
		else {
			isl_assert(bset->ctx, isl_int_is_negone(bset->eq[0][1]),
				   goto error);
			isl_int_set(sample->el[1], bset->eq[0][0]);
		}
		isl_basic_set_free(bset);
		return sample;
	}
Exemplo n.º 2
0
/* Given a set of equalities
 *
 *		B(y) + A x = 0						(*)
 *
 * compute and return an affine transformation T,
 *
 *		y = T y'
 *
 * that bijectively maps the integer vectors y' to integer
 * vectors y that satisfy the modulo constraints for some value of x.
 *
 * Let [H 0] be the Hermite Normal Form of A, i.e.,
 *
 *		A = [H 0] Q
 *
 * Then y is a solution of (*) iff
 *
 *		H^-1 B(y) (= - [I 0] Q x)
 *
 * is an integer vector.  Let d be the common denominator of H^-1.
 * We impose
 *
 *		d H^-1 B(y) = 0 mod d
 *
 * and compute the solution using isl_mat_parameter_compression.
 */
__isl_give isl_mat *isl_mat_parameter_compression_ext(__isl_take isl_mat *B,
	__isl_take isl_mat *A)
{
	isl_ctx *ctx;
	isl_vec *d;
	int n_row, n_col;

	if (!A)
		return isl_mat_free(B);

	ctx = isl_mat_get_ctx(A);
	n_row = A->n_row;
	n_col = A->n_col;
	A = isl_mat_left_hermite(A, 0, NULL, NULL);
	A = isl_mat_drop_cols(A, n_row, n_col - n_row);
	A = isl_mat_lin_to_aff(A);
	A = isl_mat_right_inverse(A);
	d = isl_vec_alloc(ctx, n_row);
	if (A)
		d = isl_vec_set(d, A->row[0][0]);
	A = isl_mat_drop_rows(A, 0, 1);
	A = isl_mat_drop_cols(A, 0, 1);
	B = isl_mat_product(A, B);

	return isl_mat_parameter_compression(B, d);
}
Exemplo n.º 3
0
/* Return 1 if "bmap" contains the point "point".
 * "bmap" is assumed to have known divs.
 * The point is first extended with the divs and then passed
 * to basic_map_contains.
 */
int isl_basic_map_contains_point(__isl_keep isl_basic_map *bmap,
	__isl_keep isl_point *point)
{
	int i;
	struct isl_vec *vec;
	unsigned dim;
	int contains;

	if (!bmap || !point)
		return -1;
	isl_assert(bmap->ctx, isl_dim_equal(bmap->dim, point->dim), return -1);
	if (bmap->n_div == 0)
		return isl_basic_map_contains(bmap, point->vec);

	dim = isl_basic_map_total_dim(bmap) - bmap->n_div;
	vec = isl_vec_alloc(bmap->ctx, 1 + dim + bmap->n_div);
	if (!vec)
		return -1;

	isl_seq_cpy(vec->el, point->vec->el, point->vec->size);
	for (i = 0; i < bmap->n_div; ++i) {
		isl_seq_inner_product(bmap->div[i] + 1, vec->el,
					1 + dim + i, &vec->el[1+dim+i]);
		isl_int_fdiv_q(vec->el[1+dim+i], vec->el[1+dim+i],
				bmap->div[i][0]);
	}

	contains = isl_basic_map_contains(bmap, vec);

	isl_vec_free(vec);
	return contains;
}
Exemplo n.º 4
0
__isl_give isl_point *isl_point_void(__isl_take isl_dim *dim)
{
	if (!dim)
		return NULL;

	return isl_point_alloc(dim, isl_vec_alloc(dim->ctx, 0));
}
Exemplo n.º 5
0
static struct isl_vec *empty_sample(struct isl_basic_set *bset)
{
	struct isl_vec *vec;

	vec = isl_vec_alloc(bset->ctx, 0);
	isl_basic_set_free(bset);
	return vec;
}
Exemplo n.º 6
0
/* Construct a parameter compression for "bset".
 * We basically just call isl_mat_parameter_compression with the right input
 * and then extend the resulting matrix to include the variables.
 *
 * Let the equalities be given as
 *
 *	B(p) + A x = 0
 *
 * and let [H 0] be the Hermite Normal Form of A, then
 *
 *	H^-1 B(p)
 *
 * needs to be integer, so we impose that each row is divisible by
 * the denominator.
 */
__isl_give isl_morph *isl_basic_set_parameter_compression(
	__isl_keep isl_basic_set *bset)
{
	unsigned nparam;
	unsigned nvar;
	int n_eq;
	isl_mat *H, *B;
	isl_vec *d;
	isl_mat *map, *inv;
	isl_basic_set *dom, *ran;

	if (!bset)
		return NULL;

	if (isl_basic_set_plain_is_empty(bset))
		return isl_morph_empty(bset);
	if (bset->n_eq == 0)
		return isl_morph_identity(bset);

	isl_assert(bset->ctx, bset->n_div == 0, return NULL);

	n_eq = bset->n_eq;
	nparam = isl_basic_set_dim(bset, isl_dim_param);
	nvar = isl_basic_set_dim(bset, isl_dim_set);

	isl_assert(bset->ctx, n_eq <= nvar, return NULL);

	d = isl_vec_alloc(bset->ctx, n_eq);
	B = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 0, 1 + nparam);
	H = isl_mat_sub_alloc6(bset->ctx, bset->eq, 0, n_eq, 1 + nparam, nvar);
	H = isl_mat_left_hermite(H, 0, NULL, NULL);
	H = isl_mat_drop_cols(H, n_eq, nvar - n_eq);
	H = isl_mat_lin_to_aff(H);
	H = isl_mat_right_inverse(H);
	if (!H || !d)
		goto error;
	isl_seq_set(d->el, H->row[0][0], d->size);
	H = isl_mat_drop_rows(H, 0, 1);
	H = isl_mat_drop_cols(H, 0, 1);
	B = isl_mat_product(H, B);
	inv = isl_mat_parameter_compression(B, d);
	inv = isl_mat_diagonal(inv, isl_mat_identity(bset->ctx, nvar));
	map = isl_mat_right_inverse(isl_mat_copy(inv));

	dom = isl_basic_set_universe(isl_space_copy(bset->dim));
	ran = isl_basic_set_universe(isl_space_copy(bset->dim));

	return isl_morph_alloc(dom, ran, map, inv);
error:
	isl_mat_free(H);
	isl_mat_free(B);
	isl_vec_free(d);
	return NULL;
}
Exemplo n.º 7
0
/* Add a specific constraint of bmap (or its opposite) to tab.
 * The position of the constraint is specified by "c", where
 * the equalities of bmap are counted twice, once for the inequality
 * that is equal to the equality, and once for its negation.
 *
 * Each of these constraints has been added to "tab" before by
 * tab_add_constraints (and later removed again), so there should
 * already be a row available for the constraint.
 */
static int tab_add_constraint(struct isl_tab *tab,
	__isl_keep isl_basic_map *bmap, int *div_map, int c, int oppose)
{
	unsigned dim;
	unsigned tab_total;
	unsigned bmap_total;
	isl_vec *v;
	int r;

	if (!tab || !bmap)
		return -1;

	tab_total = isl_basic_map_total_dim(tab->bmap);
	bmap_total = isl_basic_map_total_dim(bmap);
	dim = isl_space_dim(tab->bmap->dim, isl_dim_all);

	v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
	if (!v)
		return -1;

	if (c < 2 * bmap->n_eq) {
		if ((c % 2) != oppose)
			isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
					1 + bmap_total);
		if (oppose)
			isl_int_sub_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
		expand_constraint(v, dim, bmap->eq[c/2], div_map, bmap->n_div);
		r = isl_tab_add_ineq(tab, v->el);
		if (oppose)
			isl_int_add_ui(bmap->eq[c/2][0], bmap->eq[c/2][0], 1);
		if ((c % 2) != oppose)
			isl_seq_neg(bmap->eq[c/2], bmap->eq[c/2],
					1 + bmap_total);
	} else {
		c -= 2 * bmap->n_eq;
		if (oppose) {
			isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
					1 + bmap_total);
			isl_int_sub_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
		}
		expand_constraint(v, dim, bmap->ineq[c], div_map, bmap->n_div);
		r = isl_tab_add_ineq(tab, v->el);
		if (oppose) {
			isl_int_add_ui(bmap->ineq[c][0], bmap->ineq[c][0], 1);
			isl_seq_neg(bmap->ineq[c], bmap->ineq[c],
					1 + bmap_total);
		}
	}

	isl_vec_free(v);
	return r;
}
Exemplo n.º 8
0
enum isl_lp_result isl_pip_solve_lp(struct isl_basic_map *bmap, int maximize,
				      isl_int *f, isl_int denom, isl_int *opt,
				      isl_int *opt_denom,
				      struct isl_vec **vec)
{
	enum isl_lp_result res = isl_lp_ok;
	PipMatrix	*domain = NULL;
	PipOptions	*options;
	PipQuast   	*sol;
	unsigned	 total;

	total = isl_basic_map_total_dim(bmap);
	domain = isl_basic_map_to_pip(bmap, 0, 1, 0);
	if (!domain)
		goto error;
	entier_set_si(domain->p[0][1], -1);
	isl_int_set(domain->p[0][domain->NbColumns - 1], f[0]);
	isl_seq_cpy_to_pip(domain->p[0]+2, f+1, total);

	options = pip_options_init();
	if (!options)
		goto error;
	options->Urs_unknowns = -1;
	options->Maximize = maximize;
	options->Nq = 0;
	sol = pip_solve(domain, NULL, -1, options);
	pip_options_free(options);
	if (!sol)
		goto error;

	if (vec) {
		isl_ctx *ctx = isl_basic_map_get_ctx(bmap);
		*vec = isl_vec_alloc(ctx, 1 + total);
	}
	if (vec && !*vec)
		res = isl_lp_error;
	else if (!sol->list)
		res = isl_lp_empty;
	else if (entier_zero_p(sol->list->vector->the_deno[0]))
		res = isl_lp_unbounded;
	else
		copy_solution(*vec, maximize, opt, opt_denom, sol);
	pip_matrix_free(domain);
	pip_quast_free(sol);
	return res;
error:
	if (domain)
		pip_matrix_free(domain);
	return isl_lp_error;
}
Exemplo n.º 9
0
/* Add all constraints of bmap to tab.  The equalities of bmap
 * are added as a pair of inequalities.
 */
static int tab_add_constraints(struct isl_tab *tab,
	__isl_keep isl_basic_map *bmap, int *div_map)
{
	int i;
	unsigned dim;
	unsigned tab_total;
	unsigned bmap_total;
	isl_vec *v;

	if (!tab || !bmap)
		return -1;

	tab_total = isl_basic_map_total_dim(tab->bmap);
	bmap_total = isl_basic_map_total_dim(bmap);
	dim = isl_space_dim(tab->bmap->dim, isl_dim_all);

	if (isl_tab_extend_cons(tab, 2 * bmap->n_eq + bmap->n_ineq) < 0)
		return -1;

	v = isl_vec_alloc(bmap->ctx, 1 + tab_total);
	if (!v)
		return -1;

	for (i = 0; i < bmap->n_eq; ++i) {
		expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
		if (isl_tab_add_ineq(tab, v->el) < 0)
			goto error;
		isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
		expand_constraint(v, dim, bmap->eq[i], div_map, bmap->n_div);
		if (isl_tab_add_ineq(tab, v->el) < 0)
			goto error;
		isl_seq_neg(bmap->eq[i], bmap->eq[i], 1 + bmap_total);
		if (tab->empty)
			break;
	}

	for (i = 0; i < bmap->n_ineq; ++i) {
		expand_constraint(v, dim, bmap->ineq[i], div_map, bmap->n_div);
		if (isl_tab_add_ineq(tab, v->el) < 0)
			goto error;
		if (tab->empty)
			break;
	}

	isl_vec_free(v);
	return 0;
error:
	isl_vec_free(v);
	return -1;
}
Exemplo n.º 10
0
/* Construct a zero sample of the same dimension as bset.
 * As a special case, if bset is zero-dimensional, this
 * function creates a zero-dimensional sample point.
 */
static struct isl_vec *zero_sample(struct isl_basic_set *bset)
{
	unsigned dim;
	struct isl_vec *sample;

	dim = isl_basic_set_total_dim(bset);
	sample = isl_vec_alloc(bset->ctx, 1 + dim);
	if (sample) {
		isl_int_set_si(sample->el[0], 1);
		isl_seq_clr(sample->el + 1, dim);
	}
	isl_basic_set_free(bset);
	return sample;
}
Exemplo n.º 11
0
static int tab_add_divs(struct isl_tab *tab, __isl_keep isl_basic_map *bmap,
	int **div_map)
{
	int i, j;
	struct isl_vec *vec;
	unsigned total;
	unsigned dim;

	if (!bmap)
		return -1;
	if (!bmap->n_div)
		return 0;

	if (!*div_map)
		*div_map = isl_alloc_array(bmap->ctx, int, bmap->n_div);
	if (!*div_map)
		return -1;

	total = isl_basic_map_total_dim(tab->bmap);
	dim = total - tab->bmap->n_div;
	vec = isl_vec_alloc(bmap->ctx, 2 + total + bmap->n_div);
	if (!vec)
		return -1;

	for (i = 0; i < bmap->n_div; ++i) {
		isl_seq_cpy(vec->el, bmap->div[i], 2 + dim);
		isl_seq_clr(vec->el + 2 + dim, tab->bmap->n_div);
		for (j = 0; j < i; ++j)
			isl_int_set(vec->el[2 + dim + (*div_map)[j]],
					bmap->div[i][2 + dim + j]);
		for (j = 0; j < tab->bmap->n_div; ++j)
			if (isl_seq_eq(tab->bmap->div[j],
					vec->el, 2 + dim + tab->bmap->n_div))
				break;
		(*div_map)[i] = j;
		if (j == tab->bmap->n_div) {
			vec->size = 2 + dim + tab->bmap->n_div;
			if (isl_tab_add_div(tab, vec) < 0)
				goto error;
		}
	}

	isl_vec_free(vec);

	return 0;
error:
	isl_vec_free(vec);

	return -1;
}
Exemplo n.º 12
0
/* Find an integer point in the set represented by "tab"
 * that lies outside of the equality "eq" e(x) = 0.
 * If "up" is true, look for a point satisfying e(x) - 1 >= 0.
 * Otherwise, look for a point satisfying -e(x) - 1 >= 0 (i.e., e(x) <= -1).
 * The point, if found, is returned.
 * If no point can be found, a zero-length vector is returned.
 *
 * Before solving an ILP problem, we first check if simply
 * adding the normal of the constraint to one of the known
 * integer points in the basic set represented by "tab"
 * yields another point inside the basic set.
 *
 * The caller of this function ensures that the tableau is bounded or
 * that tab->basis and tab->n_unbounded have been set appropriately.
 */
static struct isl_vec *outside_point(struct isl_tab *tab, isl_int *eq, int up)
{
	struct isl_ctx *ctx;
	struct isl_vec *sample = NULL;
	struct isl_tab_undo *snap;
	unsigned dim;

	if (!tab)
		return NULL;
	ctx = tab->mat->ctx;

	dim = tab->n_var;
	sample = isl_vec_alloc(ctx, 1 + dim);
	if (!sample)
		return NULL;
	isl_int_set_si(sample->el[0], 1);
	isl_seq_combine(sample->el + 1,
		ctx->one, tab->bmap->sample->el + 1,
		up ? ctx->one : ctx->negone, eq + 1, dim);
	if (isl_basic_map_contains(tab->bmap, sample))
		return sample;
	isl_vec_free(sample);
	sample = NULL;

	snap = isl_tab_snap(tab);

	if (!up)
		isl_seq_neg(eq, eq, 1 + dim);
	isl_int_sub_ui(eq[0], eq[0], 1);

	if (isl_tab_extend_cons(tab, 1) < 0)
		goto error;
	if (isl_tab_add_ineq(tab, eq) < 0)
		goto error;

	sample = isl_tab_sample(tab);

	isl_int_add_ui(eq[0], eq[0], 1);
	if (!up)
		isl_seq_neg(eq, eq, 1 + dim);

	if (sample && isl_tab_rollback(tab, snap) < 0)
		goto error;

	return sample;
error:
	isl_vec_free(sample);
	return NULL;
}
Exemplo n.º 13
0
static int scan_0D(struct isl_basic_set *bset,
	struct isl_scan_callback *callback)
{
	struct isl_vec *sample;

	sample = isl_vec_alloc(bset->ctx, 1);
	isl_basic_set_free(bset);

	if (!sample)
		return -1;

	isl_int_set_si(sample->el[0], 1);

	return callback->add(callback, sample);
}
Exemplo n.º 14
0
__isl_give isl_point *isl_point_zero(__isl_take isl_dim *dim)
{
	isl_vec *vec;

	if (!dim)
		return NULL;
	vec = isl_vec_alloc(dim->ctx, 1 + isl_dim_total(dim));
	if (!vec)
		goto error;
	isl_int_set_si(vec->el[0], 1);
	isl_seq_clr(vec->el + 1, vec->size - 1);
	return isl_point_alloc(dim, vec);
error:
	isl_dim_free(dim);
	return NULL;
}
static struct isl_vec *isl_vec_lin_to_aff(struct isl_vec *vec)
{
	struct isl_vec *aff;

	if (!vec)
		return NULL;
	aff = isl_vec_alloc(vec->ctx, 1 + vec->size);
	if (!aff)
		goto error;
	isl_int_set_si(aff->el[0], 0);
	isl_seq_cpy(aff->el + 1, vec->el, vec->size);
	isl_vec_free(vec);
	return aff;
error:
	isl_vec_free(vec);
	return NULL;
}
Exemplo n.º 16
0
/* Look for all integer points in "bset", which is assumed to be bounded,
 * and call callback->add on each of them.
 *
 * We first compute a reduced basis for the set and then scan
 * the set in the directions of this basis.
 * We basically perform a depth first search, where in each level i
 * we compute the range in the i-th basis vector direction, given
 * fixed values in the directions of the previous basis vector.
 * We then add an equality to the tableau fixing the value in the
 * direction of the current basis vector to each value in the range
 * in turn and then continue to the next level.
 *
 * The search is implemented iteratively.  "level" identifies the current
 * basis vector.  "init" is true if we want the first value at the current
 * level and false if we want the next value.
 * Solutions are added in the leaves of the search tree, i.e., after
 * we have fixed a value in each direction of the basis.
 */
int isl_basic_set_scan(struct isl_basic_set *bset,
	struct isl_scan_callback *callback)
{
	unsigned dim;
	struct isl_mat *B = NULL;
	struct isl_tab *tab = NULL;
	struct isl_vec *min;
	struct isl_vec *max;
	struct isl_tab_undo **snap;
	int level;
	int init;
	enum isl_lp_result res;

	if (!bset)
		return -1;

	dim = isl_basic_set_total_dim(bset);
	if (dim == 0)
		return scan_0D(bset, callback);

	min = isl_vec_alloc(bset->ctx, dim);
	max = isl_vec_alloc(bset->ctx, dim);
	snap = isl_alloc_array(bset->ctx, struct isl_tab_undo *, dim);

	if (!min || !max || !snap)
		goto error;

	tab = isl_tab_from_basic_set(bset, 0);
	if (!tab)
		goto error;
	if (isl_tab_extend_cons(tab, dim + 1) < 0)
		goto error;

	tab->basis = isl_mat_identity(bset->ctx, 1 + dim);
	if (1)
		tab = isl_tab_compute_reduced_basis(tab);
	if (!tab)
		goto error;
	B = isl_mat_copy(tab->basis);
	if (!B)
		goto error;

	level = 0;
	init = 1;

	while (level >= 0) {
		int empty = 0;
		if (init) {
			res = isl_tab_min(tab, B->row[1 + level],
				    bset->ctx->one, &min->el[level], NULL, 0);
			if (res == isl_lp_empty)
				empty = 1;
			if (res == isl_lp_error || res == isl_lp_unbounded)
				goto error;
			isl_seq_neg(B->row[1 + level] + 1,
				    B->row[1 + level] + 1, dim);
			res = isl_tab_min(tab, B->row[1 + level],
				    bset->ctx->one, &max->el[level], NULL, 0);
			isl_seq_neg(B->row[1 + level] + 1,
				    B->row[1 + level] + 1, dim);
			isl_int_neg(max->el[level], max->el[level]);
			if (res == isl_lp_empty)
				empty = 1;
			if (res == isl_lp_error || res == isl_lp_unbounded)
				goto error;
			snap[level] = isl_tab_snap(tab);
		} else
			isl_int_add_ui(min->el[level], min->el[level], 1);

		if (empty || isl_int_gt(min->el[level], max->el[level])) {
			level--;
			init = 0;
			if (level >= 0)
				if (isl_tab_rollback(tab, snap[level]) < 0)
					goto error;
			continue;
		}
		if (level == dim - 1 && callback->add == increment_counter) {
			if (increment_range(callback,
					    min->el[level], max->el[level]))
				goto error;
			level--;
			init = 0;
			if (level >= 0)
				if (isl_tab_rollback(tab, snap[level]) < 0)
					goto error;
			continue;
		}
		isl_int_neg(B->row[1 + level][0], min->el[level]);
		if (isl_tab_add_valid_eq(tab, B->row[1 + level]) < 0)
			goto error;
		isl_int_set_si(B->row[1 + level][0], 0);
		if (level < dim - 1) {
			++level;
			init = 1;
			continue;
		}
		if (add_solution(tab, callback) < 0)
			goto error;
		init = 0;
		if (isl_tab_rollback(tab, snap[level]) < 0)
			goto error;
	}

	isl_tab_free(tab);
	free(snap);
	isl_vec_free(min);
	isl_vec_free(max);
	isl_basic_set_free(bset);
	isl_mat_free(B);
	return 0;
error:
	isl_tab_free(tab);
	free(snap);
	isl_vec_free(min);
	isl_vec_free(max);
	isl_basic_set_free(bset);
	isl_mat_free(B);
	return -1;
}
Exemplo n.º 17
0
/* Given a tableau of a set and a tableau of the corresponding
 * recession cone, detect and add all equalities to the tableau.
 * If the tableau is bounded, then we can simply keep the
 * tableau in its state after the return from extend_affine_hull.
 * However, if the tableau is unbounded, then
 * isl_tab_set_initial_basis_with_cone will add some additional
 * constraints to the tableau that have to be removed again.
 * In this case, we therefore rollback to the state before
 * any constraints were added and then add the equalities back in.
 */
struct isl_tab *isl_tab_detect_equalities(struct isl_tab *tab,
	struct isl_tab *tab_cone)
{
	int j;
	struct isl_vec *sample;
	struct isl_basic_set *hull;
	struct isl_tab_undo *snap;

	if (!tab || !tab_cone)
		goto error;

	snap = isl_tab_snap(tab);

	isl_mat_free(tab->basis);
	tab->basis = NULL;

	isl_assert(tab->mat->ctx, tab->bmap, goto error);
	isl_assert(tab->mat->ctx, tab->samples, goto error);
	isl_assert(tab->mat->ctx, tab->samples->n_col == 1 + tab->n_var, goto error);
	isl_assert(tab->mat->ctx, tab->n_sample > tab->n_outside, goto error);

	if (isl_tab_set_initial_basis_with_cone(tab, tab_cone) < 0)
		goto error;

	sample = isl_vec_alloc(tab->mat->ctx, 1 + tab->n_var);
	if (!sample)
		goto error;

	isl_seq_cpy(sample->el, tab->samples->row[tab->n_outside], sample->size);

	isl_vec_free(tab->bmap->sample);
	tab->bmap->sample = isl_vec_copy(sample);

	if (tab->n_unbounded == 0)
		hull = isl_basic_set_from_vec(isl_vec_copy(sample));
	else
		hull = initial_hull(tab, isl_vec_copy(sample));

	for (j = tab->n_outside + 1; j < tab->n_sample; ++j) {
		isl_seq_cpy(sample->el, tab->samples->row[j], sample->size);
		hull = affine_hull(hull,
				isl_basic_set_from_vec(isl_vec_copy(sample)));
	}

	isl_vec_free(sample);

	hull = extend_affine_hull(tab, hull);
	if (!hull)
		goto error;

	if (tab->n_unbounded == 0) {
		isl_basic_set_free(hull);
		return tab;
	}

	if (isl_tab_rollback(tab, snap) < 0)
		goto error;

	if (hull->n_eq > tab->n_zero) {
		for (j = 0; j < hull->n_eq; ++j) {
			isl_seq_normalize(tab->mat->ctx, hull->eq[j], 1 + tab->n_var);
			if (isl_tab_add_eq(tab, hull->eq[j]) < 0)
				goto error;
		}
	}

	isl_basic_set_free(hull);

	return tab;
error:
	isl_tab_free(tab);
	return NULL;
}
Exemplo n.º 18
0
/* Compute a reduced basis for the set represented by the tableau "tab".
 * tab->basis, which must be initialized by the calling function to an affine
 * unimodular basis, is updated to reflect the reduced basis.
 * The first tab->n_zero rows of the basis (ignoring the constant row)
 * are assumed to correspond to equalities and are left untouched.
 * tab->n_zero is updated to reflect any additional equalities that
 * have been detected in the first rows of the new basis.
 * The final tab->n_unbounded rows of the basis are assumed to correspond
 * to unbounded directions and are also left untouched.
 * In particular this means that the remaining rows are assumed to
 * correspond to bounded directions.
 *
 * This function implements the algorithm described in
 * "An Implementation of the Generalized Basis Reduction Algorithm
 *  for Integer Programming" of Cook el al. to compute a reduced basis.
 * We use \epsilon = 1/4.
 *
 * If ctx->opt->gbr_only_first is set, the user is only interested
 * in the first direction.  In this case we stop the basis reduction when
 * the width in the first direction becomes smaller than 2.
 */
struct isl_tab *isl_tab_compute_reduced_basis(struct isl_tab *tab)
{
    unsigned dim;
    struct isl_ctx *ctx;
    struct isl_mat *B;
    int unbounded;
    int i;
    GBR_LP *lp = NULL;
    GBR_type F_old, alpha, F_new;
    int row;
    isl_int tmp;
    struct isl_vec *b_tmp;
    GBR_type *F = NULL;
    GBR_type *alpha_buffer[2] = { NULL, NULL };
    GBR_type *alpha_saved;
    GBR_type F_saved;
    int use_saved = 0;
    isl_int mu[2];
    GBR_type mu_F[2];
    GBR_type two;
    GBR_type one;
    int empty = 0;
    int fixed = 0;
    int fixed_saved = 0;
    int mu_fixed[2];
    int n_bounded;
    int gbr_only_first;

    if (!tab)
        return NULL;

    if (tab->empty)
        return tab;

    ctx = tab->mat->ctx;
    gbr_only_first = ctx->opt->gbr_only_first;
    dim = tab->n_var;
    B = tab->basis;
    if (!B)
        return tab;

    n_bounded = dim - tab->n_unbounded;
    if (n_bounded <= tab->n_zero + 1)
        return tab;

    isl_int_init(tmp);
    isl_int_init(mu[0]);
    isl_int_init(mu[1]);

    GBR_init(alpha);
    GBR_init(F_old);
    GBR_init(F_new);
    GBR_init(F_saved);
    GBR_init(mu_F[0]);
    GBR_init(mu_F[1]);
    GBR_init(two);
    GBR_init(one);

    b_tmp = isl_vec_alloc(ctx, dim);
    if (!b_tmp)
        goto error;

    F = isl_alloc_array(ctx, GBR_type, n_bounded);
    alpha_buffer[0] = isl_alloc_array(ctx, GBR_type, n_bounded);
    alpha_buffer[1] = isl_alloc_array(ctx, GBR_type, n_bounded);
    alpha_saved = alpha_buffer[0];

    if (!F || !alpha_buffer[0] || !alpha_buffer[1])
        goto error;

    for (i = 0; i < n_bounded; ++i) {
        GBR_init(F[i]);
        GBR_init(alpha_buffer[0][i]);
        GBR_init(alpha_buffer[1][i]);
    }

    GBR_set_ui(two, 2);
    GBR_set_ui(one, 1);

    lp = GBR_lp_init(tab);
    if (!lp)
        goto error;

    i = tab->n_zero;

    GBR_lp_set_obj(lp, B->row[1+i]+1, dim);
    ctx->stats->gbr_solved_lps++;
    unbounded = GBR_lp_solve(lp);
    isl_assert(ctx, !unbounded, goto error);
    GBR_lp_get_obj_val(lp, &F[i]);

    if (GBR_lt(F[i], one)) {
        if (!GBR_is_zero(F[i])) {
            empty = GBR_lp_cut(lp, B->row[1+i]+1);
            if (empty)
                goto done;
            GBR_set_ui(F[i], 0);
        }
        tab->n_zero++;
    }

    do {
        if (i+1 == tab->n_zero) {
            GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
            ctx->stats->gbr_solved_lps++;
            unbounded = GBR_lp_solve(lp);
            isl_assert(ctx, !unbounded, goto error);
            GBR_lp_get_obj_val(lp, &F_new);
            fixed = GBR_lp_is_fixed(lp);
            GBR_set_ui(alpha, 0);
        } else if (use_saved) {
            row = GBR_lp_next_row(lp);
            GBR_set(F_new, F_saved);
            fixed = fixed_saved;
            GBR_set(alpha, alpha_saved[i]);
        } else {
            row = GBR_lp_add_row(lp, B->row[1+i]+1, dim);
            GBR_lp_set_obj(lp, B->row[1+i+1]+1, dim);
            ctx->stats->gbr_solved_lps++;
            unbounded = GBR_lp_solve(lp);
            isl_assert(ctx, !unbounded, goto error);
            GBR_lp_get_obj_val(lp, &F_new);
            fixed = GBR_lp_is_fixed(lp);

            GBR_lp_get_alpha(lp, row, &alpha);

            if (i > 0)
                save_alpha(lp, row-i, i, alpha_saved);

            if (GBR_lp_del_row(lp) < 0)
                goto error;
        }
        GBR_set(F[i+1], F_new);

        GBR_floor(mu[0], alpha);
        GBR_ceil(mu[1], alpha);

        if (isl_int_eq(mu[0], mu[1]))
            isl_int_set(tmp, mu[0]);
        else {
            int j;

            for (j = 0; j <= 1; ++j) {
                isl_int_set(tmp, mu[j]);
                isl_seq_combine(b_tmp->el,
                                ctx->one, B->row[1+i+1]+1,
                                tmp, B->row[1+i]+1, dim);
                GBR_lp_set_obj(lp, b_tmp->el, dim);
                ctx->stats->gbr_solved_lps++;
                unbounded = GBR_lp_solve(lp);
                isl_assert(ctx, !unbounded, goto error);
                GBR_lp_get_obj_val(lp, &mu_F[j]);
                mu_fixed[j] = GBR_lp_is_fixed(lp);
                if (i > 0)
                    save_alpha(lp, row-i, i, alpha_buffer[j]);
            }

            if (GBR_lt(mu_F[0], mu_F[1]))
                j = 0;
            else
                j = 1;

            isl_int_set(tmp, mu[j]);
            GBR_set(F_new, mu_F[j]);
            fixed = mu_fixed[j];
            alpha_saved = alpha_buffer[j];
        }
        isl_seq_combine(B->row[1+i+1]+1, ctx->one, B->row[1+i+1]+1,
                        tmp, B->row[1+i]+1, dim);

        if (i+1 == tab->n_zero && fixed) {
            if (!GBR_is_zero(F[i+1])) {
                empty = GBR_lp_cut(lp, B->row[1+i+1]+1);
                if (empty)
                    goto done;
                GBR_set_ui(F[i+1], 0);
            }
            tab->n_zero++;
        }

        GBR_set(F_old, F[i]);

        use_saved = 0;
        /* mu_F[0] = 4 * F_new; mu_F[1] = 3 * F_old */
        GBR_set_ui(mu_F[0], 4);
        GBR_mul(mu_F[0], mu_F[0], F_new);
        GBR_set_ui(mu_F[1], 3);
        GBR_mul(mu_F[1], mu_F[1], F_old);
        if (GBR_lt(mu_F[0], mu_F[1])) {
            B = isl_mat_swap_rows(B, 1 + i, 1 + i + 1);
            if (i > tab->n_zero) {
                use_saved = 1;
                GBR_set(F_saved, F_new);
                fixed_saved = fixed;
                if (GBR_lp_del_row(lp) < 0)
                    goto error;
                --i;
            } else {
                GBR_set(F[tab->n_zero], F_new);
                if (gbr_only_first && GBR_lt(F[tab->n_zero], two))
                    break;

                if (fixed) {
                    if (!GBR_is_zero(F[tab->n_zero])) {
                        empty = GBR_lp_cut(lp, B->row[1+tab->n_zero]+1);
                        if (empty)
                            goto done;
                        GBR_set_ui(F[tab->n_zero], 0);
                    }
                    tab->n_zero++;
                }
            }
        } else {
            GBR_lp_add_row(lp, B->row[1+i]+1, dim);
            ++i;
        }
    } while (i < n_bounded - 1);