/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2003 * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * Permission is hereby granted, free of charge, to any person obtaining * a copy of this software and associated documentation files (the * "Software"), to deal in the Software without restriction, including * without limitation the rights to use, copy, modify, merge, publish, * distribute, sublicense, and/or sell copies of the Software, and to * permit persons to whom the Software is furnished to do so, subject to * the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. * */ namespace Gecode { namespace Int { namespace Distinct { template forceinline Bnd::Bnd(Home 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); int min = x[0].min(), max = x[0].max(); for (int i=1; i forceinline size_t Bnd::dispose(Space& home) { y.cancel(home,*this,PC_INT_BND); (void) Propagator::dispose(home); return sizeof(*this); } template forceinline Bnd::Bnd(Space& home, Bnd& p) : Propagator(home,p), min_x(p.min_x), max_x(p.max_x) { x.update(home,p.x); y.update(home,p.y); } template Actor* Bnd::copy(Space& home) { return new (home) Bnd(home,*this); } template PropCost Bnd::cost(const Space&, const ModEventDelta& med) const { if (View::me(med) == ME_INT_VAL) return PropCost::linear(PropCost::LO, y.size()); else return PropCost::quadratic(PropCost::LO, x.size()); } template void Bnd::reschedule(Space& home) { y.reschedule(home,*this,PC_INT_BND); } /// %Rank information class Rank { public: int min, max; }; /// Sort-order by increasing maximum (by index) template class MaxIncIdx { protected: ViewArray x; public: forceinline MaxIncIdx(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 (by index) template class MinIncIdx { protected: ViewArray x; public: forceinline MinIncIdx(const ViewArray& x0) : x(x0) {} forceinline bool operator ()(const int i, const int j) { return x[i].min() < x[j].min(); } }; /// Sort-order by increasing minimum (direct) template class MinInc { public: forceinline bool operator ()(const View& x, const View& y) { return x.min() < y.min(); } }; /// Information on Hall intervals template class HallInfo { public: IntType bounds, t, d, h; }; template forceinline void pathset_t(HallInfo hall[], IntType start, IntType end, IntType to) { IntType k, l; for (l=start; (k=l) != end; hall[k].t=to) { l = hall[k].t; } } template forceinline void pathset_h(HallInfo hall[], IntType start, IntType end, IntType to) { IntType k, l; for (l=start; (k=l) != end; hall[k].h=to) { l = hall[k].h; } } template forceinline IntType pathmin_h(const HallInfo hall[], IntType i) { while (hall[i].h < i) i = hall[i].h; return i; } template forceinline IntType pathmin_t(const HallInfo hall[], IntType i) { while (hall[i].t < i) i = hall[i].t; return i; } template forceinline IntType pathmax_h(const HallInfo hall[], IntType i) { while (hall[i].h > i) i = hall[i].h; return i; } template forceinline IntType pathmax_t(const HallInfo hall[], IntType i) { while (hall[i].t > i) i = hall[i].t; return i; } template forceinline ExecStatus prop_bnd(Space& home, ViewArray& x, int* minsorted, int* maxsorted) { const int n = x.size(); Region r; // Setup rank and bounds info HallInfo* hall = r.alloc >(2*n+2); Rank* rank = r.alloc(n); int nb = 0; { IntType min = x[minsorted[0]].min(); IntType max = x[maxsorted[0]].max() + 1; IntType 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[minsorted[i]].min = nb; if (++i < n) min = x[minsorted[i]].min(); } else { if (max != last) hall[++nb].bounds = last = max; rank[maxsorted[j]].max = nb; if (++j == n) break; max = x[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) { IntType w = pathmax_h(hall, hall[x0].h); IntType m = hall[w].bounds; ModEvent me = x[maxsorted[i]].gq(home,m); if (me_failed(me)) return ES_FAILED; if ((me == ME_INT_VAL) || ((me == ME_INT_BND) && (m != x[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 IntType x0 = rank[minsorted[i]].max; IntType z = pathmin_t(hall, x0-1); IntType 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); IntType y = rank[minsorted[i]].min; if (hall[z].d < hall[y].bounds-hall[z].bounds) return ES_FAILED; if (hall[x0].h < x0) { IntType w = pathmin_h(hall, hall[x0].h); IntType m = hall[w].bounds - 1; ModEvent me = x[minsorted[i]].lq(home,m); if (me_failed(me)) return ES_FAILED; if ((me == ME_INT_VAL) || ((me == ME_INT_BND) && (m != x[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; } } return es; } template forceinline ExecStatus prop_bnd(Space& home, ViewArray& x, int& min_x, int& max_x) { const int n = x.size(); Region r; int* minsorted = r.alloc(n); int* maxsorted = r.alloc(n); unsigned int d = static_cast(max_x - min_x) + 1; if (d < static_cast(n)) return ES_FAILED; if (d > 2*static_cast(n)) { for (int i=0; i min_inc(x); Support::quicksort >(minsorted, n, min_inc); MaxIncIdx max_inc(x); Support::quicksort >(maxsorted, n, max_inc); } else { int* minbucket = r.alloc(d); int* maxbucket = r.alloc(d); for (unsigned int i=0; i (static_cast(Int::Limits::max) / 2U)) return prop_bnd(home,x,minsorted,maxsorted); else return prop_bnd(home,x,minsorted,maxsorted); } template ExecStatus prop_bnd(Space& home, ViewArray& x) { int min = x[0].min(), max = x[0].max(); for (int i=1; i ExecStatus Bnd::propagate(Space& home, const ModEventDelta& med) { assert(x.size() > 1); if (View::me(med) == ME_INT_VAL) { ExecStatus es = prop_val(home,y); GECODE_ES_CHECK(es); if (y.size() < 2) return home.ES_SUBSUMED(*this); if (es == ES_FIX) return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_BND)); } if (y.size() == 2) GECODE_REWRITE(*this,(Rel::Nq::post(home(*this),y[0],y[1]))); ExecStatus es = prop_bnd(home,x,min_x,max_x); GECODE_ES_CHECK(es); if (es == ES_NOFIX && View::me(modeventdelta()) == ME_INT_VAL) return es; const int n = x.size(); if ((n > 2*y.size()) && (n > 6)) { // If there are many assigned views, try to eliminate them unsigned int d = static_cast(max_x - min_x) + 1; if (d > 2*static_cast(n)) { MinInc min_inc; Support::quicksort >(&x[0], n, min_inc); } else { Region r; int* minbucket = r.alloc(d); View* minsorted = r.alloc(n); for (unsigned int i=0; i 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 (x.size() < 2) return home.ES_SUBSUMED(*this); if (x.size() == 2) GECODE_REWRITE(*this,(Rel::Nq::post(home(*this),x[0],x[1]))); return es; } #ifdef GECODE_HAS_CBS template void Bnd::solndistrib(Space& home, Propagator::SendMarginal send) const { cbsdistinct(home,this->id(),x,send); } template void Bnd::domainsizesum(Propagator::InDecision in, unsigned int& size, unsigned int& size_b) const { cbssize(x,in,size,size_b); } #endif template ExecStatus Bnd::post(Home 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