/* -*- 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-07-11 11:03:40 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $ * $Revision: 7356 $ * * 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 intersection" propagator * */ template ExecStatus Intersection::post(Space* home, View0 x0,View1 x1,View2 x2) { (void) new (home) Intersection(home,x0,x1,x2); return ES_OK; } template Actor* Intersection::copy(Space* home, bool share) { return new (home) Intersection(home,share,*this); } template Support::Symbol Intersection::ati(void) { return Reflection::mangle("Gecode::Set::RelOp::Intersection"); } template Reflection::ActorSpec Intersection::spec(const Space* home, Reflection::VarMap& m) const { return MixTernaryPropagator::spec(home, m, ati()); } template void Intersection::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) Intersection(home,x0,x1,x2); } template ExecStatus Intersection::propagate(Space* home, ModEventDelta med) { // This propagator implements the constraint // x2 = x0 \cap 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 x0lbmod = false; bool x1lbmod = false; bool modified = false; do { modified = false; do { // 4) glb(x2) <= glb(x0) \cap glb(x1) { GlbRanges x0lb(x0); GlbRanges x1lb(x1); Iter::Ranges::Inter, GlbRanges > i2(x0lb,x1lb); GECODE_ME_CHECK_MODIFIED(modified, x2.includeI(home,i2) ); } if (modified || Rel::testSetEventLB(me2) ) { modified = false; // 5) x2 <= x0 GlbRanges x2lb1(x2); GECODE_ME_CHECK_MODIFIED(modified, x0.includeI(home,x2lb1) ); x0lbmod |= modified; // 6) x2 <= x1 bool modified2=false; GlbRanges x2lb2(x2); GECODE_ME_CHECK_MODIFIED(modified2, x1.includeI(home,x2lb2) ); x1lbmod |= modified2; modified |= modified2; } } while (modified); modified = false; do { bool modifiedOld = modified; modified = false; if (Rel::testSetEventUB(me2) || Rel::testSetEventLB(me0) || x0lbmod || modifiedOld) { // 1) lub(x2) \ glb(x0) => lub(x1) GlbRanges x0lb(x0); LubRanges x2ub(x2); Iter::Ranges::Diff,LubRanges > diff(x0lb, x2ub); GECODE_ME_CHECK_MODIFIED(modified, x1.excludeI(home,diff) ); } if (Rel::testSetEventUB(me2) || Rel::testSetEventLB(me1) || x1lbmod || modifiedOld) { // 2) lub(x2) \ glb(x1) => lub(x0) GlbRanges x1lb(x1); LubRanges x2ub(x2); Iter::Ranges::Diff, LubRanges > diff(x1lb, x2ub); GECODE_ME_CHECK_MODIFIED(modified, x0.excludeI(home,diff) ); } if (Rel::testSetEventUB(me0,me1) || modified) { // modified = false; // 3) lub(x0) \cap lub(x1) <= lub(x2) LubRanges x0ub(x0); LubRanges x1ub(x1); Iter::Ranges::Inter, LubRanges > i1(x0ub,x1ub); GECODE_ME_CHECK_MODIFIED(modified, x2.intersectI(home,i1) ); } } while(modified); modified = false; { // cardinality ExecStatus ret = interCard(home,modified, x0, x1, x2); GECODE_ES_CHECK(ret); } if (x2.cardMin() == Set::Limits::card) { GECODE_ME_CHECK( x0.cardMin(home, Set::Limits::card) ); GECODE_ME_CHECK( x1.cardMin(home, Set::Limits::card) ); return ES_SUBSUMED(this,home); } if (x0.cardMin() == Set::Limits::card) GECODE_REWRITE(this,(Rel::Eq::post(home,x1,x2))); if (x1.cardMin() == Set::Limits::card) 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; } } } template forceinline Intersection::Intersection(Space* home, View0 y0,View1 y1,View2 y2) : MixTernaryPropagator(home,y0,y1,y2) {} template forceinline Intersection::Intersection(Space* home, bool share, Intersection& p) : MixTernaryPropagator(home,share,p) {} /* * "Nary intersection" propagator * */ template forceinline IntersectionN::IntersectionN(Space* home, ViewArray& x, View1 y) : MixNaryOnePropagator(home,x,y), intOfDets(home) { shared = x.shared() || viewarrayshared(x,y); } template forceinline IntersectionN::IntersectionN(Space* home, ViewArray& x, const IntSet& z, View1 y) : MixNaryOnePropagator(home,x,y), intOfDets(home) { shared = x.shared() || viewarrayshared(x,y); IntSetRanges rz(z); intOfDets.intersectI(home, rz); } template forceinline IntersectionN::IntersectionN(Space* home, bool share, IntersectionN& p) : MixNaryOnePropagator(home,share,p), shared(p.shared), intOfDets() { intOfDets.update(home, p.intOfDets); } template ExecStatus IntersectionN::post(Space* home, ViewArray& x, View1 y) { switch (x.size()) { case 0: GECODE_ME_CHECK(y.cardMin(home, Limits::card)); return ES_OK; case 1: return Rel::Eq::post(home, x[0], y); case 2: return Intersection::post(home, x[0], x[1], y); default: (void) new (home) IntersectionN(home,x,y); return ES_OK; } } template ExecStatus IntersectionN::post(Space* home, ViewArray& x, const IntSet& z, View1 y) { (void) new (home) IntersectionN(home,x,z,y); return ES_OK; } template Actor* IntersectionN::copy(Space* home, bool share) { return new (home) IntersectionN(home,share,*this); } template PropCost IntersectionN::cost(ModEventDelta) const { return PC_QUADRATIC_LO; } template Support::Symbol IntersectionN::ati(void) { return Reflection::mangle("Gecode::Set::RelOp::IntersectionN"); } template Reflection::ActorSpec IntersectionN::spec(const Space* home, Reflection::VarMap& m) const { Reflection::ActorSpec s = MixNaryOnePropagator ::spec(home, m, ati()); int count = 0; for (BndSetRanges iod(intOfDets); iod(); ++iod) count++; Reflection::IntArrayArg* a = Reflection::Arg::newIntArray(count*2); count = 0; for (BndSetRanges iod(intOfDets); iod(); ++iod) { (*a)[count++] = iod.min(); (*a)[count++] = iod.max(); } return s << a; } template void IntersectionN::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) IntersectionN(home,x0,z,x1); } template ExecStatus IntersectionN::propagate(Space* home, ModEventDelta) { bool repeat = false; do { repeat = false; int xsize = x.size(); if (xsize == 0) goto size0; for (int i = xsize; i--; ) { GECODE_ME_CHECK( y.cardMax(home,x[i].cardMax()) ); GECODE_ME_CHECK( x[i].cardMin(home,y.cardMin()) ); if (x[i].cardMax()==0) { GECODE_ME_CHECK( y.cardMax(home, 0)); intOfDets.dispose(home); return ES_SUBSUMED(this,home); } } { GECODE_AUTOARRAY(GlbRanges,xLBs,xsize); GECODE_AUTOARRAY(LubRanges,xUBs,xsize); for (int i = xsize; i--; ) { GlbRanges lb(x[i]); LubRanges ub(x[i]); xLBs[i]=lb; xUBs[i]=ub; } Iter::Ranges::NaryInter > lbi(xLBs,xsize); BndSetRanges dets1(intOfDets); Iter::Ranges::Inter< Iter::Ranges::NaryInter >, BndSetRanges > lbiAll(lbi,dets1); GECODE_ME_CHECK( y.includeI(home,lbiAll) ); Iter::Ranges::NaryInter > ubi(xUBs,xsize); BndSetRanges dets2(intOfDets); Iter::Ranges::Inter< Iter::Ranges::NaryInter >, BndSetRanges > ubiAll(ubi,dets2); GECODE_ME_CHECK( y.intersectI(home,ubiAll) ); } for (int i = xsize; i--; ) { GlbRanges ylb(y); GECODE_ME_CHECK( x[i].includeI(home,ylb) ); } // xi.exclude (Intersection(xj.lb) - y.ub) { GECODE_AUTOARRAY(GLBndSet, rightSet, xsize); new (&rightSet[xsize-1]) GLBndSet(home); rightSet[xsize-1].update(home,intOfDets); for (int i=xsize-1;i--;) { GlbRanges xilb(x[i+1]); BndSetRanges prev(rightSet[i+1]); Iter::Ranges::Inter, BndSetRanges> inter(xilb,prev); new (&rightSet[i]) GLBndSet(home); rightSet[i].includeI(home,inter); } LUBndSet leftAcc(home); for (int i=0;i inter(left, right); LubRanges yub(y); Iter::Ranges::Diff, LubRanges > forbidden(inter, yub); GECODE_ME_CHECK( x[i].excludeI(home,forbidden) ); GlbRanges xlb(x[i]); leftAcc.intersectI(home,xlb); } for (int i=xsize; i--;) rightSet[i].dispose(home); leftAcc.dispose(home); } for(int i=0;i det(x[i]); if (intOfDets.intersectI(home,det)) {repeat = true;} x.move_lst(i); if (intOfDets.size()==0) { GECODE_ME_CHECK( y.cardMax(home,0) ); intOfDets.dispose(home); return ES_SUBSUMED(this,home); } } } size0: if (x.size()==0) { BndSetRanges all1(intOfDets); GECODE_ME_CHECK( y.intersectI(home,all1) ); BndSetRanges all2(intOfDets); GECODE_ME_CHECK( y.includeI(home,all2) ); intOfDets.dispose(home); return ES_SUBSUMED(this,home); } } while (repeat); return shared ? ES_NOFIX : ES_FIX; } }}} // STATISTICS: set-prop