/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Guido Tack * Christian Schulte * * Contributing authors: * Gabor Szokoli * * Copyright: * Guido Tack, 2004 * Christian Schulte, 2004 * Gabor Szokoli, 2004 * * 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 Set { namespace RelOp { /* * "Ternary union" propagator * */ template forceinline Union::Union(Space* home, View0 y0,View1 y1,View2 y2) : MixTernaryPropagator(home,y0,y1,y2) {} template forceinline Union::Union(Space* home, bool share, Union& p) : MixTernaryPropagator(home,share,p) {} template ExecStatus Union::post(Space* home, View0 x0, View1 x1, View2 x2) { (void) new (home) Union(home,x0,x1,x2); return ES_OK; } template Actor* Union::copy(Space* home, bool share) { return new (home) Union(home,share,*this); } template Support::Symbol Union::ati(void) { return Reflection::mangle("Gecode::Set::RelOp::Union"); } template Reflection::ActorSpec Union::spec(const Space* home, Reflection::VarMap& m) const { return MixTernaryPropagator::spec(home, m, ati()); } template void Union::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(3); View0 x0(home, vars, spec[0]); View1 x1(home, vars, spec[1]); View2 x2(home, vars, spec[2]); (void) new (home) Union(home,x0,x1,x2); } template ExecStatus Union::propagate(Space* home, ModEventDelta med) { // This propagator implements the constraint // x2 = x0 u x1 bool x0ass = x0.assigned(); bool x1ass = x1.assigned(); bool x2ass = x2.assigned(); ModEvent me0 = View0::me(med); ModEvent me1 = View1::me(med); ModEvent me2 = View2::me(med); bool x0ubmod = false; bool x1ubmod = false; bool modified = false; do { modified = false; do { // 4) lub(x2) <= lub(x0) u lub(x1) { LubRanges x0ub(x0); LubRanges x1ub(x1); Iter::Ranges::Union, LubRanges > u2(x0ub,x1ub); GECODE_ME_CHECK_MODIFIED(modified, x2.intersectI(home,u2) ); } if (modified || Rel::testSetEventUB(me2) ) { modified = false; // 5) x0 <= x2 LubRanges x2ub1(x2); GECODE_ME_CHECK_MODIFIED(modified, x0.intersectI(home,x2ub1) ); x0ubmod |= modified; // 6) x1 <= x2 bool modified2=false; LubRanges x2ub2(x2); GECODE_ME_CHECK_MODIFIED(modified2, x1.intersectI(home,x2ub2) ); x1ubmod |= modified2; modified |= modified2; } } while (modified); modified = false; do { bool modifiedOld = modified; modified = false; if (Rel::testSetEventLB(me2) || Rel::testSetEventUB(me0) || x0ubmod || modifiedOld) { // 1) glb(x0) \ lub(x1) <= glb(x2) GlbRanges x2lb(x2); LubRanges x0ub(x0); Iter::Ranges::Diff, LubRanges > diff(x2lb, x0ub); GECODE_ME_CHECK_MODIFIED(modified, x1.includeI(home,diff) ); } if (Rel::testSetEventLB(me2) || Rel::testSetEventUB(me1) || x1ubmod || modifiedOld) { // 2) glb(x0) \ lub(x2) <= glb(x1) GlbRanges x2lb(x2); LubRanges x1ub(x1); Iter::Ranges::Diff, LubRanges > diff(x2lb, x1ub); GECODE_ME_CHECK_MODIFIED(modified, x0.includeI(home,diff) ); } if (Rel::testSetEventLB(me0,me1) || modified) { // modified = false; // 3) glb(x1) u glb(x2) <= glb(x0) GlbRanges x0lb(x0); GlbRanges x1lb(x1); Iter::Ranges::Union, GlbRanges > u1(x0lb,x1lb); GECODE_ME_CHECK_MODIFIED(modified, x2.includeI(home,u1) ); } } while(modified); modified = false; { // cardinality ExecStatus ret = unionCard(home,modified, x0, x1, x2); GECODE_ES_CHECK(ret); } if (x2.cardMax() == 0) { GECODE_ME_CHECK( x0.cardMax(home, 0) ); GECODE_ME_CHECK( x1.cardMax(home, 0) ); return ES_SUBSUMED(this,home); } if (x0.cardMax() == 0) GECODE_REWRITE(this,(Rel::Eq::post(home,x1,x2))); if (x1.cardMax() == 0) GECODE_REWRITE(this,(Rel::Eq::post(home,x0,x2))); } while(modified); if (shared(x0,x1,x2)) { return (x0ass && x1ass && x2ass) ? ES_SUBSUMED(this,home) : ES_NOFIX; } else { if (x0ass && x1ass && x2ass) return ES_SUBSUMED(this,home); if (x0ass != x0.assigned() || x1ass != x1.assigned() || x2ass != x2.assigned()) { return ES_NOFIX; } else { return ES_FIX; } } } /* * "Nary union" propagator * */ template forceinline UnionN::UnionN(Space* home, ViewArray& x, View1 y) : MixNaryOnePropagator(home,x,y) { shared = x.shared() || viewarrayshared(x,y); } template forceinline UnionN::UnionN(Space* home, ViewArray& x, const IntSet& z, View1 y) : MixNaryOnePropagator(home,x,y) { shared = x.shared() || viewarrayshared(x,y); IntSetRanges rz(z); unionOfDets.includeI(home, rz); } template forceinline UnionN::UnionN(Space* home, bool share, UnionN& p) : MixNaryOnePropagator(home,share,p), shared(p.shared) { unionOfDets.update(home,p.unionOfDets); } template Actor* UnionN::copy(Space* home, bool share) { return new (home) UnionN(home,share,*this); } template ExecStatus UnionN::post(Space* home, ViewArray& x, View1 y) { switch (x.size()) { case 0: GECODE_ME_CHECK(y.cardMax(home, 0)); return ES_OK; case 1: return Rel::Eq::post(home, x[0], y); case 2: return Union::post(home, x[0], x[1], y); default: (void) new (home) UnionN(home,x,y); return ES_OK; } } template ExecStatus UnionN::post(Space* home, ViewArray& x, const IntSet& z, View1 y) { (void) new (home) UnionN(home,x,z,y); return ES_OK; } template PropCost UnionN::cost(ModEventDelta) const { return PC_QUADRATIC_LO; } template Support::Symbol UnionN::ati(void) { return Reflection::mangle("Gecode::Set::RelOp::UnionN"); } template Reflection::ActorSpec UnionN::spec(const Space* home, Reflection::VarMap& m) const { Reflection::ActorSpec s = MixNaryOnePropagator ::spec(home, m, ati()); int count = 0; for (BndSetRanges uod(unionOfDets); uod(); ++uod) count++; Reflection::IntArrayArg* a = Reflection::Arg::newIntArray(count*2); count = 0; for (BndSetRanges uod(unionOfDets); uod(); ++uod) { (*a)[count++] = uod.min(); (*a)[count++] = uod.max(); } return s << a; } template void UnionN::post(Space* home, Reflection::VarMap& vars, const Reflection::ActorSpec& spec) { spec.checkArity(3); ViewArray x0(home, vars, spec[0]); View1 x1(home, vars, spec[1]); Reflection::IntArrayArgRanges zr(spec[2]->toIntArray()); IntSet z(zr); (void) new (home) UnionN(home,x0,z,x1); } template ExecStatus UnionN::propagate(Space* home, ModEventDelta med) { ModEvent me0 = View0::me(med); ModEvent me1 = View1::me(med); bool ubevent = Rel::testSetEventUB(me0, me1); bool lbevent = Rel::testSetEventLB(me0, me1); bool anybevent = Rel::testSetEventAnyB(me0, me1); bool cardevent = Rel::testSetEventCard(me0, me1); bool modified = false; bool oldModified = false; do { oldModified = modified; modified = false; if (modified || oldModified || ubevent) GECODE_ME_CHECK(unionNXiUB(home, modified, x, y,unionOfDets)); if (modified || oldModified || ubevent) GECODE_ME_CHECK(partitionNYUB(home, modified, x, y,unionOfDets)); if (modified || oldModified || anybevent) GECODE_ME_CHECK(partitionNXiLB(home, modified, x, y,unionOfDets)); if (modified || oldModified || lbevent) GECODE_ME_CHECK(partitionNYLB(home, modified, x, y,unionOfDets)); if (modified || oldModified || cardevent || ubevent) GECODE_ME_CHECK(unionNCard(home, modified, x, y, unionOfDets)); } while (modified); for(int i=0;i det(x[i]); unionOfDets.includeI(home,det); x.move_lst(i); } } // When we run out of variables, make a final check and disolve: if (x.size()==0) { BndSetRanges all1(unionOfDets); GECODE_ME_CHECK( y.intersectI(home,all1) ); BndSetRanges all2(unionOfDets); GECODE_ME_CHECK( y.includeI(home,all2) ); unionOfDets.dispose(home); return ES_SUBSUMED(this,home); } return shared ? ES_NOFIX : ES_FIX; } }}} // STATISTICS: set-prop