/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Contributing authors: * Mikael Lagerkvist * * Copyright: * Christian Schulte, 2006 * Mikael Lagerkvist, 2006 * * 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 Channel { /** * \brief Combine view with information for domain propagation * */ template class DomInfo { public: /// The view View view; /// Last propagated size unsigned int size; /// Last propagated minimum int min; /// Last propagated maximum int max; /// Initialize void init(View x, int n); /// Update during cloning void update(Space& home, DomInfo& vcb); /// Check whether propagation for assignment is to be done bool doval(void) const; /// Check whether propagation for domain is to be done bool dodom(void) const; /// Record that view got assigned void assigned(void); /// Record that one value got removed void removed(int i); /// Update the size and bounds information after pruning void done(Offset& o); }; template forceinline void DomInfo::init(View x, int n) { view = x; size = static_cast(n); min = 0; max = n-1; } template forceinline void DomInfo::update(Space& home, DomInfo& di) { view.update(home,di.view); size = di.size; min = di.min; max = di.max; } template forceinline bool DomInfo::doval(void) const { return (size != 1) && view.assigned(); } template forceinline bool DomInfo::dodom(void) const { return size != view.size(); } template forceinline void DomInfo::assigned(void) { size = 1; } template forceinline void DomInfo::removed(int i) { size--; if (i == min) min++; else if (i == max) max--; } template forceinline void DomInfo::done(Offset& o) { size = view.size(); min = o(view).min(); max = o(view).max(); } // Propagate domain information from x to y template ExecStatus prop_dom(Space& home, int n, DomInfo* x, Offset& ox, DomInfo* y, Offset& oy, ProcessStack& ya) { for (int i=0; i xir(ox(x[i].view)); Iter::Ranges::ComplVal > xirc(x[i].min,x[i].max,xir); Iter::Ranges::ToValues > > jv(xirc); while (jv()) { // j is not in the domain of x[i], so prune i from y[j] int j = jv.val(); ModEvent me = oy(y[j].view).nq(home,i); if (me_failed(me)) return ES_FAILED; if (me_modified(me)) { if (me == ME_INT_VAL) { // Record that y[j] has been assigned and must be propagated ya.push(j); } else { // Obvious as x[i] is different from j y[j].removed(i); } } ++jv; } // Update which values have been propagated and what are the new bounds x[i].done(ox); } return ES_OK; } /* * The actual propagator * */ template forceinline Dom::Dom(Home home, int n, DomInfo* xy, Offset& ox, Offset& oy) : Base,Offset,PC_INT_DOM>(home,n,xy,ox,oy) {} template forceinline Dom::Dom(Space& home, Dom& p) : Base,Offset,PC_INT_DOM>(home,p) {} template Actor* Dom::copy(Space& home) { return new (home) Dom(home,*this); } template PropCost Dom::cost(const Space&, const ModEventDelta& med) const { if (View::me(med) == ME_INT_VAL) return PropCost::quadratic(PropCost::LO, 2*n); else return PropCost::quadratic(PropCost::HI, 2*n); } template ExecStatus Dom::propagate(Space& home, const ModEventDelta& med) { // MSVC in non-permissive mode needs this, no idea why... const int n = this->n; Region r; ProcessStack xa(r,n); ProcessStack ya(r,n); DomInfo* x = xy; DomInfo* y = xy+n; if (View::me(med) == ME_INT_VAL) { // Scan x and y for assigned but not yet propagated views for (int i=0; i > (home,n,x,ox,y,oy,n_na,xa,ya))); // Propagate assigned views for y GECODE_ES_CHECK((prop_val > (home,n,y,oy,x,ox,n_na,ya,xa))); assert(ya.empty()); } while (!xa.empty() || !ya.empty()); return home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM)); } else { do { // Propagate assigned views for x GECODE_ES_CHECK((prop_val > (home,n,x,ox,y,oy,n_na,xa,ya))); // Propagate assigned views for y GECODE_ES_CHECK((prop_val > (home,n,y,oy,x,ox,n_na,ya,xa))); assert(ya.empty()); } while (!xa.empty()); return home.ES_FIX_PARTIAL(*this,View::med(ME_INT_DOM)); } } // Process changed views for y // This propagates from y to x and possibly records xs that // got assigned GECODE_ES_CHECK(prop_dom(home,n,y,oy,x,ox,xa)); // Process assigned views for x GECODE_ES_CHECK((prop_val > (home,n,x,ox,y,oy,n_na,xa,ya))); // Perform domain consistent propagation for distinct on the x views if (dc.available()) { GECODE_ES_CHECK(dc.sync()); } else { ViewArray xv(r,n); for (int i=0; i > (home,n,x,ox,y,oy,n_na,xa,ya))); // Process assigned views for y GECODE_ES_CHECK((prop_val > (home,n,y,oy,x,ox,n_na,ya,xa))); }; if (shared) { for (int i=0; i<2*n; i++) if (!xy[i].view.assigned()) return ES_NOFIX; return home.ES_SUBSUMED(*this); } else { return (n_na == 0) ? home.ES_SUBSUMED(*this) : ES_FIX; } } template ExecStatus Dom::post(Home home, int n, DomInfo* xy, Offset& ox, Offset& oy) { assert(n > 0); if (n == 1) { GECODE_ME_CHECK(ox(xy[0].view).eq(home,0)); GECODE_ME_CHECK(oy(xy[1].view).eq(home,0)); return ES_OK; } for (int i=0; i(home,n,xy,ox,oy); return ES_OK; } }}} // STATISTICS: int-prop