/* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2003 * * Last modified: * $Date: 2006-10-23 16:16:09 +0200 (Mon, 23 Oct 2006) $ by $Author: schulte $ * $Revision: 3768 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ #include "gecode/support/sort.hh" namespace Gecode { namespace Int { namespace Distinct { template inline Bnd::Bnd(Space* home, ViewArray& x0) : Propagator(home), x(x0), y(home,x0) { // Both x and y initially contain the same variables // - x is used for bounds propagation // - y is used for performing singleton propagation // They can not be shared as singleton propagation removes // determined variables still required for bounds propagation. y.subscribe(home,this,PC_INT_BND); } template size_t Bnd::dispose(Space* home) { assert(!home->failed()); y.cancel(home,this,PC_INT_BND); (void) Propagator::dispose(home); return sizeof(*this); } template forceinline Bnd::Bnd(Space* home, bool share, Bnd& p) : Propagator(home,share,p) { x.update(home,share,p.x); y.update(home,share,p.y); } template Actor* Bnd::copy(Space* home, bool share) { return new (home) Bnd(home,share,*this); } template PropCost Bnd::cost(void) const { return (View::pme(this) == ME_INT_VAL) ? cost_lo(y.size(),PC_LINEAR_LO) : cost_hi(x.size(),PC_LINEAR_HI); } /// Rank information class Rank { public: int min, max; }; /// Sort-order by increasing maximum template class MaxInc { protected: ViewArray x; public: MaxInc(const ViewArray& x0) : x(x0) {} forceinline bool operator()(const int i, const int j) { return x[i].max() < x[j].max(); } }; /// Sort-order by increasing minimum template class MinInc { public: forceinline bool operator()(const View& x, const View& y) { return x.min() < y.min(); } }; /// Information on Hall intervals class HallInfo { public: int bounds, t, d, h; }; inline void pathset_t(HallInfo hall[], int start, int end, int to) { int k, l; for (l=start; (k=l) != end; hall[k].t=to) { l = hall[k].t; } } inline void pathset_h(HallInfo hall[], int start, int end, int to) { int k, l; for (l=start; (k=l) != end; hall[k].h=to) { l = hall[k].h; } } forceinline int pathmin_h(const HallInfo hall[], int i) { while (hall[i].h < i) i = hall[i].h; return i; } forceinline int pathmin_t(const HallInfo hall[], int i) { while (hall[i].t < i) i = hall[i].t; return i; } forceinline int pathmax_h(const HallInfo hall[], int i) { while (hall[i].h > i) i = hall[i].h; return i; } forceinline int pathmax_t(const HallInfo hall[], int i) { while (hall[i].t > i) i = hall[i].t; return i; } #define GECODE_INT_MINSORTED(i) (i) #define GECODE_INT_MAXSORTED(i) (_maxsorted[i]) template ExecStatus prop_bnd(Space* home, ViewArray& x, int m) { const int n = x.size(); // Sort variable array for minimum directly { MinInc min_inc; Support::insertion >(&x[0], n, min_inc); } GECODE_AUTOARRAY(int, _maxsorted, n); for (int i = n; i--; ) _maxsorted[i]=i; { MaxInc max_inc(x); Support::insertion >(_maxsorted, n, max_inc); } // Setup rank and bounds info GECODE_AUTOARRAY(HallInfo, hall, 2*n+2); GECODE_AUTOARRAY(Rank, rank, n); int nb = 0; { int min = x[GECODE_INT_MINSORTED(0)].min(); int max = x[GECODE_INT_MAXSORTED(0)].max() + 1; int last = min - 2; hall[0].bounds = last; int i = 0; int j = 0; while (true) { if (i < n && min < max) { if (min != last) hall[++nb].bounds = last = min; rank[GECODE_INT_MINSORTED(i)].min = nb; if (++i < n) min = x[GECODE_INT_MINSORTED(i)].min(); } else { if (max != last) hall[++nb].bounds = last = max; rank[GECODE_INT_MAXSORTED(j)].max = nb; if (++j == n) break; max = x[GECODE_INT_MAXSORTED(j)].max() + 1; } } hall[nb+1].bounds = hall[nb].bounds + 2; } // If tells cross holes, we do not compute a fixpoint ExecStatus es = ES_FIX; // Propagate lower bounds for (int i=nb+2; --i;) { hall[i].t = hall[i].h = i-1; hall[i].d = hall[i].bounds - hall[i-1].bounds; } for (int i=0; i x0) { int w = pathmax_h(hall, hall[x0].h); int m = hall[w].bounds; ModEvent me = x[GECODE_INT_MAXSORTED(i)].gq(home,m); if (me_failed(me)) return ES_FAILED; if ((me == ME_INT_VAL) || ((me == ME_INT_BND) && (m != x[GECODE_INT_MAXSORTED(i)].min()))) es = ES_NOFIX; pathset_h(hall, x0, w, w); // path compression } if (hall[z].d == hall[z].bounds-hall[y].bounds) { pathset_h(hall, hall[y].h, j-1, y); // mark hall interval hall[y].h = j-1; } } // Propagate upper bounds for (int i=nb+1; i--;) { hall[i].t = hall[i].h = i+1; hall[i].d = hall[i+1].bounds - hall[i].bounds; } for (int i=n; --i>=0; ) { // visit intervals in decreasing min order int x0 = rank[GECODE_INT_MINSORTED(i)].max; int z = pathmin_t(hall, x0-1); int j = hall[z].t; if (--hall[z].d == 0) hall[z = pathmin_t(hall, hall[z].t=z-1)].t = j; pathset_t(hall, x0-1, z, z); int y = rank[GECODE_INT_MINSORTED(i)].min; if (hall[z].d < hall[y].bounds-hall[z].bounds) return ES_FAILED; if (hall[x0].h < x0) { int w = pathmin_h(hall, hall[x0].h); int m = hall[w].bounds - 1; ModEvent me = x[GECODE_INT_MINSORTED(i)].lq(home,m); if (me_failed(me)) return ES_FAILED; if ((me == ME_INT_VAL) || ((me == ME_INT_BND) && (m != x[GECODE_INT_MAXSORTED(i)].min()))) es = ES_NOFIX; pathset_h(hall, x0, w, w); } if (hall[z].d == hall[y].bounds-hall[z].bounds) { pathset_h(hall, hall[y].h, j+1, y); hall[y].h = j+1; } } if ((n > 2*m) && (n > 6)) { // If there are many assigned views, try to eliminate them MinInc min_inc; Support::insertion >(&x[0], n, min_inc); int i = 0; int j = 0; int max = x[0].max()-1; while (i < n-1) { if (!x[i].assigned() || (x[i].val() <= max) || (x[i].val() > x[i+1].min())) { // Keep view x[i] max = std::max(max,x[i].max()); x[j++]=x[i]; } i++; } if (!x[i].assigned() || (x[i].val() <= max)) x[j++]=x[i]; x.size(j); if (j < 2) return ES_SUBSUMED; } return es; } #undef GECODE_INT_MINSORTED #undef GECODE_INT_MAXSORTED template ExecStatus Bnd::propagate(Space* home) { assert(x.size() > 1); if (View::pme(this) == ME_INT_VAL) { ExecStatus es = prop_val(home,y); if ((es == ES_FAILED) || (es == ES_SUBSUMED)) return es; if (es == ES_FIX) return ES_FIX_PARTIAL(View::pme(ME_INT_BND)); } if (y.size() == 2) { GECODE_ES_CHECK(Rel::Nq::post(home,y[0],y[1])); return ES_SUBSUMED; } return prop_bnd(home,x,y.size()); } template ExecStatus Bnd::post(Space* home, ViewArray& x){ if (x.size() == 2) return Rel::Nq::post(home,x[0],x[1]); if (x.size() > 2) (void) new (home) Bnd(home,x); return ES_OK; } }}} // STATISTICS: int-prop