/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * Christian Schulte * * Copyright: * Guido Tack, 2004,2006 * Christian Schulte, 2004 * * Last modified: * $Date: 2008-02-06 18:48:22 +0100 (Wed, 06 Feb 2008) $ by $Author: schulte $ * $Revision: 6102 $ * * 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 Set { namespace Select { template forceinline SelectUnion:: SelectUnion(Space* home, SView y0, IdxViewArray& iv0, RView y1) : Propagator(home), x0(y0), iv(iv0), x1(y1) { force(home); x0.subscribe(home,this, PC_SET_ANY); x1.subscribe(home,this, PC_SET_ANY); iv.subscribe(home,this, PC_SET_ANY); } template forceinline SelectUnion:: SelectUnion(Space* home, bool share, SelectUnion& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); iv.update(home,share,p.iv); } template PropCost SelectUnion::cost(ModEventDelta) const { return PC_LINEAR_HI; } template Support::Symbol SelectUnion::ati(void) { return Reflection::mangle("Gecode::Set::Select::Union"); } template Reflection::ActorSpec SelectUnion::spec(const Space* home, Reflection::VarMap& m) const { Reflection::ActorSpec s(ati()); return s << x0.spec(home, m) << iv.spec(home, m) << x1.spec(home, m); } template void SelectUnion::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(3); SView x0(home, vars, spec[0]); IdxViewArray iv(home, vars, spec[1]); RView x1(home, vars, spec[2]); (void) new (home) SelectUnion(home, x0, iv, x1); } template size_t SelectUnion::dispose(Space* home) { unforce(home); if (!home->failed()) { x0.cancel(home,this, PC_SET_ANY); x1.cancel(home,this, PC_SET_ANY); iv.cancel(home,this,PC_SET_ANY); } (void) Propagator::dispose(home); return sizeof(*this); } template ExecStatus SelectUnion:: post(Space* home, SView x0, IdxViewArray& xs, RView x1) { int n = xs.size(); // s2 \subseteq {1,...,n} Iter::Ranges::Singleton s(0, n-1); GECODE_ME_CHECK(x1.intersectI(home,s)); (void) new (home) SelectUnion(home,x0,xs,x1); return ES_OK; } template Actor* SelectUnion::copy(Space* home, bool share) { return new (home) SelectUnion(home,share,*this); } template ExecStatus SelectUnion::propagate(Space* home, ModEventDelta) { int n = iv.size(); bool loopVar; do { loopVar = false; // Cache the upper bound iterator, as we have to // modify the upper bound while iterating LubRanges x1ub(x1); Iter::Ranges::Cache > x1ubc(x1ub); Iter::Ranges::ToValues > > vx1ub(x1ubc); GlbRanges x1lb(x1); Iter::Ranges::Cache > x1lbc(x1lb); Iter::Ranges::ToValues > > vx1(x1lbc); // In the first iteration, compute in before[i] the union // of all the upper bounds of the x_i. At the same time, // exclude inconsistent x_i from x1 and remove them from // the list, cancel their dependencies. GLBndSet sofarBefore(home); LUBndSet selectedInter(home, IntSet (Limits::min, Limits::max)); GECODE_AUTOARRAY(GLBndSet, before, n); int j = 0; int i = 0; unsigned int maxCard = 0; unsigned int minCard = Limits::card; while ( vx1ub() ) { // Remove vars at indices not in the upper bound if (iv[i].idx < vx1ub.val()) { iv[i].var.cancel(home,this, PC_SET_ANY); ++i; continue; } assert(iv[i].idx == vx1ub.val()); iv[j] = iv[i]; SView candidate = iv[j].var; int candidateInd = iv[j].idx; // inter = glb(candidate) & complement(lub(x0)) GlbRanges candlb(candidate); LubRanges x0ub(x0); Iter::Ranges::Diff, LubRanges > diff(candlb, x0ub); bool selectSingleInconsistent = false; if (x1.cardMax() <= 1) { GlbRanges x0lb(x0); LubRanges candub(candidate); Iter::Ranges::Diff, LubRanges > diff2(x0lb, candub); selectSingleInconsistent = diff2() || candidate.cardMax() < x0.cardMin(); } // exclude inconsistent x_i // an x_i is inconsistent if // * at most one x_i can be selected and there are // elements in x_0 that can't be in x_i // (selectSingleInconsistent) // * its min cardinality is greater than maxCard of x0 // * inter is not empty (there are elements in x_i // that can't be in x_0) if (selectSingleInconsistent || candidate.cardMin() > x0.cardMax() || diff()) { ModEvent me = (x1.exclude(home,candidateInd)); loopVar |= me_modified(me); GECODE_ME_CHECK(me); iv[j].var.cancel(home,this, PC_SET_ANY); ++i; ++vx1ub; continue; } else { // if x_i is consistent, check whether we know // that its index is in x1 if (vx1() && vx1.val()==candidateInd) { // x0 >= candidate, candidate <= x0 GlbRanges candlb(candidate); ModEvent me = x0.includeI(home,candlb); loopVar |= me_modified(me); GECODE_ME_CHECK(me); LubRanges x0ub(x0); me = candidate.intersectI(home,x0ub); loopVar |= me_modified(me); GECODE_ME_CHECK(me); ++vx1; } new (&before[j]) GLBndSet(home); before[j].update(home,sofarBefore); LubRanges cub(candidate); sofarBefore.includeI(home,cub); GlbRanges clb(candidate); selectedInter.intersectI(home,clb); maxCard = std::max(maxCard, candidate.cardMax()); minCard = std::min(minCard, candidate.cardMin()); } ++vx1ub; ++i; ++j; } // cancel the variables with index greater than // max of lub(x1) for (int k=i; k 0) { // Selector is not empty, hence the intersection of the // possibly selected lower bounds is contained in x0 BndSetRanges si(selectedInter); ModEvent me = x0.includeI(home, si); loopVar |= me_modified(me); GECODE_ME_CHECK(me); me = x0.cardMin(home, minCard); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } selectedInter.dispose(home); if (x1.cardMax() <= 1) { ModEvent me = x0.cardMax(home, maxCard); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } { // x0 <= sofarBefore BndSetRanges sfB(sofarBefore); ModEvent me = x0.intersectI(home,sfB); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } sofarBefore.dispose(home); GLBndSet sofarAfter(home); // In the second iteration, this time backwards, compute // sofarAfter as the union of all lub(x_j) with j>i for (int i=n; i--;) { // TODO: check for size of universe here? // if (sofarAfter.size() == 0) break; // extra = inter(before[i], sofarAfter) - lub(x0) BndSetRanges b(before[i]); BndSetRanges s(sofarAfter); GlbRanges x0lb(x0); Iter::Ranges::Union inter(b,s); Iter::Ranges::Diff, Iter::Ranges::Union > diff(x0lb, inter); if (diff()) { ModEvent me = (x1.include(home,iv[i].idx)); loopVar |= me_modified(me); GECODE_ME_CHECK(me); // candidate != extra me = iv[i].var.includeI(home,diff); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } LubRanges iviub(iv[i].var); sofarAfter.includeI(home,iviub); before[i].dispose(home); } sofarAfter.dispose(home); } while (loopVar); // Test whether we determined x1 without determining x0 if (x1.assigned() && !x0.assigned()) { int ubsize = x1.lubSize(); if (ubsize > 2) { assert(ubsize==n); ViewArray is(home,ubsize); for (int i=n; i--;) is[i]=iv[i].var; GECODE_REWRITE(this,(RelOp::UnionN ::post(home,is,x0))); } else if (ubsize == 2) { assert(n==2); SView a = iv[0].var; SView b = iv[1].var; GECODE_REWRITE(this,(RelOp::Union ::post(home,a,b,x0))); } else if (ubsize == 1) { assert(n==1); GECODE_REWRITE(this,(Rel::Eq::post(home,x0,iv[0].var))); } else { GECODE_ME_CHECK(x0.cardMax(home, 0)); return ES_SUBSUMED(this,home); } } bool allAssigned = true; for (int i=iv.size(); i--;) { if (!iv[i].var.assigned()) { allAssigned = false; break; } } if (x0.assigned() && x1.assigned() && allAssigned) { return ES_SUBSUMED(this,home); } return ES_FIX; } }}} // STATISTICS: set-prop