/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2006 * * Last modified: * $Date: 2008-01-31 18:29:16 +0100 (Thu, 31 Jan 2008) $ by $Author: tack $ * $Revision: 6017 $ * * 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 Linear { /** * \brief Set for support information * * Records supported positions of values such that with iteration * the supported values can be reconstructed. * */ class SupportSet { private: /// Number of bits per unsigned integer static const unsigned int bpui = sizeof(unsigned int) * 8; /// Array of bits unsigned int* bits; public: /// Initialize support set with cardinality \arg n void init(unsigned int n); /// Record that there is support at position \arg i void support(unsigned int i); /// Check whether position \arg i has support bool supported(unsigned int i) const; private: /// Support-based iterator: iterates values to be removed class ResultIter : public ViewValues { protected: /// The support set used const SupportSet* s; /// The current position of the value unsigned int p; public: /// Initialize iterator ResultIter(const SupportSet* s0, const IntView& x); /// Increment to next supported value void operator++(void); }; public: /// Perform tell according to recorded support information on \arg x ModEvent tell(Space* home, IntView& x) const; /// Release memory void dispose(void); }; /** * \brief Base-class for support-based iterator * */ template class SupportIter { protected: /// Integer coefficient for view int a; /// Integer view IntView x; /// Set of support for values in x SupportSet s; /// Current value int c; /// Position of current value unsigned int p; /// Lower bound information for value Val l; /// Upper bound information for value Val u; public: /// Initialize view void init(int a, const IntView& x, Val l, Val u); /// Record value at current position as supported void support(void); /// Tell back new variable domain according to support found ModEvent tell(Space* home); /// Finalize memory for support set void dispose(void); }; /** * \brief Support-based iterator for positive view * */ template class PosSupportIter : public SupportIter { private: /// Iterate ranges of integer view in increasing order IntVarImpFwd i; // Using-declarations for dependant names using SupportIter::a; using SupportIter::x; using SupportIter::s; using SupportIter::c; using SupportIter::p; using SupportIter::l; using SupportIter::u; public: /// Reset iterator to beginning and adjust \arg d accordingly bool reset(Val& d); /// Adjust \arg d and return true if next value still works bool adjust(Val& d); }; /** * \brief Support-based iterator for negative view * */ template class NegSupportIter : public SupportIter { private: /// Iterate ranges of integer view in decreasing order IntVarImpBwd i; // Using-declarations for dependant names using SupportIter::a; using SupportIter::x; using SupportIter::s; using SupportIter::c; using SupportIter::p; using SupportIter::l; using SupportIter::u; public: /// Reset iterator to beginning and adjust \arg d accordingly bool reset(Val& d); /// Adjust \arg d and return true if next value still works bool adjust(Val& d); }; /* * Support set * */ forceinline void SupportSet::init(unsigned int n) { bits = Memory::bmalloc((n / bpui) + 1); for (unsigned int i = (n / bpui) + 1; i--; ) bits[i] = 0; } forceinline void SupportSet::support(unsigned int i) { unsigned int p = i / bpui; bits[p] |= 1 << (i-p*bpui); } forceinline bool SupportSet::supported(unsigned int i) const { unsigned int p = i / bpui; return (bits[p] & (1 << (i-p*bpui))) != 0; } forceinline SupportSet::ResultIter::ResultIter(const SupportSet* s0, const IntView& x) : ViewValues(x), s(s0), p(0) { while (ViewValues::operator()() && s->supported(p)) { ViewValues::operator++(); ++p; } } forceinline void SupportSet::ResultIter::operator++(void) { do { ViewValues::operator++(); ++p; } while (ViewValues::operator()() && s->supported(p)); } inline ModEvent SupportSet::tell(Space* home, IntView& x) const { unsigned int n = x.size() / bpui; // Check whether all bits are zero: failure for (unsigned int i=n+1; i--; ) if (bits[i] != 0) goto all; return ME_INT_FAILED; all: // Check whether all bits are one: nothing changed for (unsigned int i=n; i--; ) if (bits[i] != ~0U) goto tell; // Now check the bits in the last word for (unsigned int i=n*bpui; i forceinline void SupportIter::init(int a0, const IntView& x0, Val l0, Val u0) { a=a0; x=x0; l=l0; u=u0; s.init(x.size()); } template forceinline void SupportIter::support(void) { s.support(p); } template forceinline ModEvent SupportIter::tell(Space* home) { return s.tell(home,x); } template forceinline void SupportIter::dispose(void) { s.dispose(); } /* * Support-based iterator for positive view * */ template forceinline bool PosSupportIter::reset(Val& d) { // Way too small, no value can make it big enough if (d + static_cast(a)*x.max() < u) return false; // Restart iterator and position of values i.init(x.var()); p = 0; // Skip all ranges which are too small while (d + static_cast(a)*i.max() < u) { p += i.width(); ++i; } // There is at least one range left (check of max) assert(i()); // Initialize current range and adjust value c = i.min(); // Skip all values which are too small while (d + static_cast(a)*c < u) { p++; c++; } // Adjust to new value d += static_cast(a) * c; return true; } template forceinline bool PosSupportIter::adjust(Val& d) { // Current value Val v = static_cast(a) * c; // Subtract current value from d d -= v; // Move to next position (number of value) p++; // Still in the same range if (c < i.max()) { // Decrement current values c += 1; v += a; } else { // Go to next range ++i; if (!i()) return false; c = i.min(); v = static_cast(a) * c; } // Is d with the current value too large? if (d + v > l) return false; // Update d d += v; return true; } /* * Support-based iterator for negative view * */ template forceinline bool NegSupportIter::reset(Val& d) { // Way too small, no value can make it big enough if (d + static_cast(a)*x.min() < u) return false; // Restart iterator and position of values i.init(x.var()); p = x.size()-1; // Skip all ranges which are too small while (d + static_cast(a)*i.min() < u) { p -= i.width(); ++i; } // There is at least one range left (check of max) assert(i()); // Initialize current range c = i.max(); // Skip all values which are too small while (d + static_cast(a)*c < u) { p--; c--; } // Adjust to new value d += static_cast(a) * c; return true; } template forceinline bool NegSupportIter::adjust(Val& d) { // Current value Val v = static_cast(a) * c; // Subtract current value from d d -= v; // Move to next position (number of value) p--; // Still in the same range if (c > i.min()) { // Decrement current values c -= 1; v -= a; } else { // Go to next range ++i; if (!i()) return false; c = i.max(); v = static_cast(a) * c; } // Is d with the current value too large? if (d + v > l) return false; // Update d d += v; return true; } /* * The domain-consisten equality propagator * */ template forceinline DomEq::DomEq(Space* home, ViewArray& x, ViewArray& y, Val c) : Lin(home,x,y,c) {} template ExecStatus DomEq::post(Space* home, ViewArray& x, ViewArray& y, Val c) { (void) new (home) DomEq(home,x,y,c); return ES_OK; } template forceinline DomEq::DomEq(Space* home, bool share, DomEq& p) : Lin(home,share,p) {} template Actor* DomEq::copy(Space* home, bool share) { return new (home) DomEq(home,share,*this); } template PropCost DomEq::cost(ModEventDelta med) const { return (View::me(med) != ME_INT_DOM) ? cost_hi(x.size()+y.size(),PC_LINEAR_LO) : cost_hi(x.size()+y.size(),PC_CRAZY_HI); } template inline Support::Symbol DomEq::ati(void) { return Reflection::mangle("Gecode::Int::Linear::DomEq"); } template Reflection::ActorSpec DomEq::spec(const Space* home, Reflection::VarMap& m) const { return Lin::spec(home, m, ati()); } template void DomEq::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(3); ViewArray x(home, vars, spec[0]); ViewArray y(home, vars, spec[1]); Val c = spec[2]->toInt(); (void) new (home) DomEq(home, x, y, c); } template ExecStatus DomEq::propagate(Space* home, ModEventDelta med) { if (View::me(med) != ME_INT_DOM) { ExecStatus es = prop_bnd(home,med,this,x,y,c); GECODE_ES_CHECK(es); return ES_FIX_PARTIAL(this,View::med(ME_INT_DOM)); } // Value of equation for partial assignment Val d = -c; int n = x.size(); int m = y.size(); // Create support-base iterators GECODE_AUTOARRAY(PosSupportIter, xp, n); GECODE_AUTOARRAY(NegSupportIter, yp, m); // Initialize views for assignments { Val l = 0; Val u = 0; for (int j=m; j--; ) { yp[j].init(-y[j].scale(),y[j].base(),l,u); l += y[j].max(); u += y[j].min(); } for (int i=n; i--; ) { xp[i].init(x[i].scale(),x[i].base(),l,u); l -= x[i].min(); u -= x[i].max(); } } // Collect support information by iterating assignments { // Force reset of all iterators in first round int i = 0; int j = 0; next_i: // Reset all iterators for positive views and update d while (i0) { if (yp[j-1].adjust(d)) goto next_j; j--; } prev_i: // Try iterating to next assignment: positive views while (i>0) { if (xp[i-1].adjust(d)) goto next_i; i--; } } // Tell back new variable domains ExecStatus es = ES_NOFIX; for (int i=n; i--; ) { ModEvent me = xp[i].tell(home); if (me_failed(me)) { es = ES_FAILED; goto dispose; } if (!x[i].assigned()) es = ES_FIX; } for (int j=m; j--; ) { ModEvent me = yp[j].tell(home); if (me_failed(me)) { es = ES_FAILED; goto dispose; } if (!y[j].assigned()) es = ES_FIX; } // Release memory dispose: for (int i=n; i--; ) xp[i].dispose(); for (int j=m; j--; ) yp[j].dispose(); if (es == ES_NOFIX) return ES_SUBSUMED(this,sizeof(*this)); return es; } }}} // STATISTICS: int-prop