/* poly-eq(C, X, y) == min(y) <= sum(C . X) <= max(y) */ static int fd_poly_eq_filter(fd_constraint this) { int ub, lb; int min, max; int terms = this->nconstants; int *mins, *maxs; int i; #ifdef CONSTRAINT_TEMPS int *base; assert(!fd__constraint_data_valid(this)); if (!constraint_memory[this->index]) constraint_memory[this->index] = malloc((2 * terms + 4) * sizeof(int)); base = constraint_memory[this->index]; mins = base + 4; maxs = mins + terms; #else mins = alloca(terms * sizeof(*mins)); maxs = alloca(terms * sizeof(*maxs)); #endif lb = _fd_var_min(VAR(this, this->nvariables - 1)); // lower bound ub = _fd_var_max(VAR(this, this->nvariables - 1)); // upper bound // sum the minima and the maxima of the terms min = max = 0; for (i = 0; i < terms; ++i) { int vl, vh; if (this->constants[i] > 0) { vl = mins[i] = _fd_var_min(VAR(this, i)); vh = maxs[i] = _fd_var_max(VAR(this, i)); } else { vl = maxs[i] = _fd_var_max(VAR(this, i)); vh = mins[i] = _fd_var_min(VAR(this, i)); } min += this->constants[i] * vl; max += this->constants[i] * vh; } if (min > ub || max < lb) return FD_NOSOLUTION; if ((min > lb && _fd_var_del_lt(min, VAR(this, this->nvariables - 1))) | (max < ub && _fd_var_del_gt(max, VAR(this, this->nvariables - 1)))) { if (fd_domain_empty(VAR(this, this->nvariables - 1))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, this->nvariables - 1)); } if (min == max) return FD_OK; // XXX: poor man's propagation if (min == ub) for (i = 0; i < terms; ++i) { int c = this->constants[i]; if (c && mins[i] != maxs[i]) { if (c > 0) { _fd_var_del_gt(mins[i], VAR(this, i)); // check needed for when variables occur twice and // with opposite signs if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } else { _fd_var_del_lt(maxs[i], VAR(this, i)); if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } else if (max == lb) for (i = 0; i < terms; ++i) { int c = this->constants[i]; if (c && mins[i] != maxs[i]) { if (c > 0) { _fd_var_del_lt(maxs[i], VAR(this, i)); if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } else { _fd_var_del_gt(mins[i], VAR(this, i)); if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } else { if (max > ub) for (i = 0; i < terms; ++i) { int c = this->constants[i]; int xmin = mins[i]; int xmax = maxs[i]; if (c > 0) { if ((xmax - xmin) * c > ub - min) { // xmax = floor((ub - min) / c) + xmin xmax = (ub - min) / c + xmin; _fd_var_del_gt(xmax, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } else if (c < 0) { if ((xmin - xmax) * c > ub - min) { // xmin = ceil((ub - min) / c) + xmax xmin = (ub - min) / c + xmax; _fd_var_del_lt(xmin, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } if (min < lb) for (i = 0; i < terms; ++i) { int c = this->constants[i]; int xmin = mins[i]; int xmax = maxs[i]; if (c > 0) { if ((xmax - xmin) * c > max - lb) { // xmin = ceil((lb - max) / c) + xmax xmin = (lb - max) / c + xmax; _fd_var_del_lt(xmin, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) // domains may have holes return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } else if (c < 0) { if ((xmax - xmin) * c < lb - max) { // xmax = floor((lb - max) / c) + xmin xmax = (lb - max) / c + xmin; _fd_var_del_gt(xmax, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) // domains may have holes return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } } #ifdef CONSTRAINT_TEMPS // save values *base = lb; *(base + 1) = ub; *(base + 2) = min; *(base + 3) = max; fd__constraint_remember(this); #endif return FD_OK; } static int fd_poly_eq_propagate2(fd_constraint this, fd_int culprit) { #ifdef CONSTRAINT_TEMPS int ub, lb; int min, max; int terms = this->nconstants; int *mins, *maxs; int i; int *base; int x, c; int nmin, nmin_x, nmax, nmax_x; if (!fd__constraint_data_valid(this)) return fd_poly_eq_filter(this); // ignores culprit // bounds filtering base = constraint_memory[this->index]; mins = base + 4; maxs = mins + terms; lb = *base; ub = *(base + 1); min = *(base + 2); max = *(base + 3); if (culprit == VAR(this, this->nvariables - 1)) { // the culprit is the sum variable int nlb, nub; nlb = _fd_var_min(culprit); nub = _fd_var_max(culprit); if (nlb == lb && nub == ub) return FD_OK; if (nlb != lb) { if (max < nlb) return FD_NOSOLUTION; if (max == nlb) for (i = 0; i < terms; ++i) { if (this->constants[i] > 0) { if (_fd_var_del_lt(maxs[i], VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } else if (this->constants[i] < 0) { if (_fd_var_del_gt(mins[i], VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } if (min < nlb) for (i = 0; i < terms; ++i) { int c = this->constants[i]; int xmin = mins[i]; int xmax = maxs[i]; if (c > 0) { if ((xmax - xmin) * c > max - nlb) { // xmin = ceil((nlb - max) / c) + xmax xmin = (nlb - max) / c + xmax; _fd_var_del_lt(xmin, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) // domains may have holes return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } else if (c < 0) { if ((xmax - xmin) * c < nlb - max) { // xmax = floor((nlb - max) / c) + xmin xmax = (nlb - max) / c + xmin; _fd_var_del_gt(xmax, VAR(this, i)); if (fd_domain_empty(VAR(this, i))) // domains may have holes return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } *base = nlb; } if (nub != ub) { if (min > nub) return FD_NOSOLUTION; if (min == nub) for (i = 0; i < terms; ++i) { if (this->constants[i] > 0) { if (_fd_var_del_gt(mins[i], VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } else if (this->constants[i] < 0) { if (_fd_var_del_lt(maxs[i], VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } if (max > nub) for (i = 0; i < terms; ++i) { int c = this->constants[i]; int xmin = mins[i]; int xmax = maxs[i]; if (c > 0) { if ((xmax - xmin) * c > nub - min) { // xmax = floor((nub - min) / c) + xmin xmax = (nub - min) / c + xmin; if (_fd_var_del_gt(xmax, VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } else if (c < 0) { if ((xmin - xmax) * c > nub - min) { // xmin = ceil((nub - min) / c) + xmax xmin = (nub - min) / c + xmax; if (_fd_var_del_lt(xmin, VAR(this, i))) { if (fd_domain_empty(VAR(this, i))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, i)); } } } } *(base + 1) = nub; } return FD_OK; } // the culprit appears in one of the terms, find out which one(s) for (x = 0; culprit != VAR(this, x); ++x) ; nmin_x = _fd_var_min(VAR(this, x)); nmax_x = _fd_var_max(VAR(this, x)); if (nmin_x == mins[x] && nmax_x == maxs[x]) return FD_OK; nmin = min; nmax = max; do { c = this->constants[x]; if (c > 0) { nmin = nmin + (nmin_x - mins[x]) * c; nmax = nmax - (maxs[x] - nmax_x) * c; } else if (c < 0) { nmin = nmin - (maxs[x] - nmax_x) * c; nmax = nmax + (nmin_x - mins[x]) * c; } if (nmin > ub || nmax < lb) return FD_NOSOLUTION; mins[x] = nmin_x; maxs[x] = nmax_x; while (++x < terms && culprit != VAR(this, x)) ; } while (x < terms); if ((nmin > lb && _fd_var_del_lt(nmin, VAR(this, this->nvariables - 1))) | (nmax < ub && _fd_var_del_gt(nmax, VAR(this, this->nvariables - 1)))) { if (fd_domain_empty(VAR(this, this->nvariables - 1))) return FD_NOSOLUTION; _fd_revise_connected(this, VAR(this, this->nvariables - 1)); } *(base + 2) = nmin; *(base + 3) = nmax; return FD_OK; #else /* CONSTRAINT_TEMPS */ return fd_poly_eq_filter(this); // ignores culprit #endif /* CONSTRAINT_TEMPS */ } fd_constraint fd_poly_eq(int cs[], fd_int xs[], int nterms, fd_int y) { fd_constraint c = _fd_constraint_new(nterms + 1, nterms); int i; if (c) { for (i = 0; i < nterms; ++i) c->variables[i] = FD_INT2C_VAR(xs[i]); c->variables[nterms] = FD_INT2C_VAR(y); for (i = 0; i < nterms; ++i) c->constants[i] = cs[i]; #ifdef CONSTRAINT_CLASS c->kind = FD_CONSTR_POLY_EQ; #else /* CONSTRAINT_CLASS */ c->propagator2 = fd_poly_eq_propagate2; #endif /* CONSTRAINT_CLASS */ for (i = 0; i < c->nvariables; ++i) _fd_var_add_constraint(VAR(c, i), c); _fd_add_constraint(c); } return c; }