/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * Christian Schulte * Gabor Szokoli * * Copyright: * Guido Tack, 2004 * Christian Schulte, 2004 * Gabor Szokoli, 2004 * * 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. * */ #include #include namespace Gecode { namespace Set { namespace Int { /// Value Iterator for values above a certain weight template class OverweightValues { private: /// The threshold above which values should be iterated int threshold; /// The value iterator I iter; /// A superset of the elements found in the iterator SharedArray elements; /// Weights for all the possible elements SharedArray weights; /// The current index into the elements and weights int index; /// Move to the next element void next(void); public: /// \name Constructors and initialization //@{ /// Default constructor OverweightValues(void); /// Initialize with elements/weights pairs, threshold \a t and iterator \a i OverweightValues(int t, SharedArray& elements0, SharedArray& weights0, I& i); /// Initialize with elements/weights pairs, threshold \a t and iterator \a i void init(int t, SharedArray& elements0, SharedArray& weights0, I& i); //@} /// \name Iteration control //@{ /// Test whether iterator is still at a value or done bool operator ()(void) const; /// Move iterator to next value (if possible) void operator ++(void); //@} /// \name Value access //@{ /// Return current value int val(void) const; //@} }; template forceinline void OverweightValues::next(void) { while (iter()) { while (elements[index] threshold) { return; } ++iter; } } template forceinline OverweightValues::OverweightValues(void) {} template forceinline OverweightValues::OverweightValues(int t, SharedArray& elements0, SharedArray& weights0, I& i) : threshold(t), iter(i), elements(elements0), weights(weights0), index(0) { next(); } template forceinline void OverweightValues::init(int t, SharedArray& elements0, SharedArray& weights0, I& i) { threshold = t; iter = i; elements = elements0; weights = weights0; index = 0; next(); } template forceinline bool OverweightValues::operator ()(void) const { return iter(); } template forceinline void OverweightValues::operator ++(void) { ++iter; next(); } template forceinline int OverweightValues::val(void) const { return elements[index]; } template forceinline Weights::Weights(Home home, const SharedArray& elements0, const SharedArray& weights0, View x0, Gecode::Int::IntView y0) : Propagator(home), elements(elements0), weights(weights0), x(x0), y(y0) { home.notice(*this,AP_DISPOSE); x.subscribe(home,*this, PC_SET_ANY); y.subscribe(home,*this, Gecode::Int::PC_INT_BND); } template forceinline Weights::Weights(Space& home, Weights& p) : Propagator(home,p), elements(p.elements), weights(p.weights) { x.update(home,p.x); y.update(home,p.y); } template inline ExecStatus Weights::post(Home home, const SharedArray& elements, const SharedArray& weights, View x, Gecode::Int::IntView y) { if (elements.size() != weights.size()) throw ArgumentSizeMismatch("Weights"); Region r; int* els_arr = r.alloc(elements.size()); for (int i=elements.size(); i--;) els_arr[i] = elements[i]; IntSet els(els_arr, elements.size()); IntSetRanges er(els); GECODE_ME_CHECK(x.intersectI(home, er)); (void) new (home) Weights(home,elements,weights,x,y); return ES_OK; } template PropCost Weights::cost(const Space&, const ModEventDelta&) const { return PropCost::linear(PropCost::LO, y.size()+1); } template void Weights::reschedule(Space& home) { x.reschedule(home,*this, PC_SET_ANY); y.reschedule(home,*this, Gecode::Int::PC_INT_BND); } template forceinline size_t Weights::dispose(Space& home) { home.ignore(*this,AP_DISPOSE); x.cancel(home,*this, PC_SET_ANY); y.cancel(home,*this, Gecode::Int::PC_INT_BND); elements.~SharedArray(); weights.~SharedArray(); (void) Propagator::dispose(home); return sizeof(*this); } template Actor* Weights::copy(Space& home) { return new (home) Weights(home,*this); } /// Compute the weight of the elements in the iterator \a I template forceinline int weightI(SharedArray& elements, SharedArray& weights, I& iter) { int sum = 0; int i = 0; Iter::Ranges::ToValues v(iter); for (; v(); ++v) { // Skip all elements below the current while (elements[i] ExecStatus Weights::propagate(Space& home, const ModEventDelta&) { ModEvent me = ME_SET_NONE; if (!x.assigned()) { // Collect the weights of the elements in the unknown set in an array int size = elements.size(); Region r; int* minWeights = r.alloc(size); int* maxWeights = r.alloc(size); UnknownRanges ur(x); Iter::Ranges::ToValues > urv(ur); for (int i=0; i(minWeights, size, il); Support::quicksort(maxWeights, size, il); // The maximum number of elements that can still be added to x int delta = static_cast(std::min(x.unknownSize(), x.cardMax() - x.glbSize())); // The weight of the elements already in x GlbRanges glb(x); int glbWeight = weightI >(elements, weights, glb); // Compute the weight of the current lower bound of x, plus at most // delta-1 further elements with smallest negative weights. This weight // determines which elements in the upper bound cannot possibly be // added to x (those whose weight would exceed the capacity even if // all other elements are minimal) int lowWeight = glbWeight; for (int i=0; i= 0) break; lowWeight+=minWeights[i]; } // Compute the lowest possible weight of x. If there is another element // with negative weight left, then add its weight to lowWeight. // Otherwise lowWeight is already the lowest possible weight. int lowestWeight = lowWeight; if (delta>0 && minWeights[delta-1]<0) lowestWeight+=minWeights[delta-1]; // If after including the minimal number of required elements, // no more element with negative weight is available, then // a tighter lower bound can be computed. if ( (x.cardMin() - x.glbSize() > 0 && minWeights[x.cardMin() - x.glbSize() - 1] >= 0) || minWeights[0] >= 0 ) { int lowestPosWeight = glbWeight; for (unsigned int i=0; i ur2(x); Iter::Ranges::ToValues > urv2(ur2); OverweightValues > > ov(remainingCapacity, elements, weights, urv2); Iter::Values::ToRanges > > > ovr(ov); me = x.excludeI(home, ovr); GECODE_ME_CHECK(me); } if (x.assigned()) { // If x is assigned, just compute its weight and assign y. GlbRanges glb(x); int w = weightI >(elements, weights, glb); GECODE_ME_CHECK(y.eq(home, w)); return home.ES_SUBSUMED(*this); } // return me_modified(me) ? ES_NOFIX : ES_FIX; return ES_NOFIX; } }}} // STATISTICS: set-prop