/* * Main authors: * Guido Tack * Christian Schulte * * Contributing authors: * Gabor Szokoli * * Copyright: * Guido Tack, 2004 * Christian Schulte, 2004 * Gabor Szokoli, 2004 * * Last modified: * $Date: 2005-11-17 23:57:02 +0100 (Thu, 17 Nov 2005) $ by $Author: tack $ * $Revision: 2601 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ 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 ExecStatus Intersection::propagate(Space* home) { bool x0ass=x0.assigned(); bool x1ass=x1.assigned(); bool x2ass=x2.assigned(); { GlbRanges x2lb(x2); GECODE_ME_CHECK( x0.includeI(home,x2lb) ); } { GlbRanges x2lb(x2); GECODE_ME_CHECK( x1.includeI(home,x2lb) ); } { GlbRanges x0lb(x0); LubRanges x2ub(x2); Iter::Ranges::Diff,LubRanges > m1(x0lb,x2ub); GECODE_ME_CHECK( x1.excludeI(home,m1) ); } { GlbRanges x1lb(x1); LubRanges x2ub(x2); Iter::Ranges::Diff,LubRanges > m2(x1lb,x2ub); GECODE_ME_CHECK( x0.excludeI(home,m2) ); } { GlbRanges x0lb(x0); GlbRanges x1lb(x1); Iter::Ranges::Inter,GlbRanges > i1(x0lb,x1lb); GECODE_ME_CHECK( x2.includeI(home,i1) ); } { LubRanges x0ub(x0); LubRanges x1ub(x1); Iter::Ranges::Inter,LubRanges > i2(x0ub,x1ub); GECODE_ME_CHECK( x2.intersectI(home,i2) ); } unsigned int m, n; { LubRanges x0ub(x0); LubRanges x1ub(x1); Iter::Ranges::Union,LubRanges > u1(x0ub,x1ub); m = Iter::Ranges::size(u1); n = x0.cardMin()+x1.cardMin(); if (n>m) GECODE_ME_CHECK( x2.cardMin(home,n-m) ); } unsigned int s2; { LubRanges x0ub(x0); GlbRanges x1lb(x1); Iter::Ranges::Diff,LubRanges > d2(x1lb,x0ub); s2 = Iter::Ranges::size(d2); assert (s2 <= x1.cardMax()); GECODE_ME_CHECK( x2.cardMax(home,x1.cardMax()-s2) ); } { LubRanges x1ub(x1); GlbRanges x0lb(x0); Iter::Ranges::Diff,LubRanges > d1 (x0lb,x1ub); unsigned int s1 = Iter::Ranges::size(d1); assert (s1 <= x0.cardMax()); GECODE_ME_CHECK( x2.cardMax(home,x0.cardMax()-s1) ); if (m+x2.cardMax() > x1.cardMin()) GECODE_ME_CHECK( x0.cardMax(home,m+x2.cardMax()-x1.cardMin()) ); //I might have to recaluclate m here? if (m+x2.cardMax() > x0.cardMin()) GECODE_ME_CHECK( x1.cardMax(home,m+x2.cardMax()-x0.cardMin()) ); //s1 and s2 could be outdated too... GECODE_ME_CHECK( x0.cardMin(home,s1+x2.cardMin()) ); GECODE_ME_CHECK( x1.cardMin(home,s2+x2.cardMin()) ); } if ( x0ass + x1ass + x2ass >= 2 ) return ES_SUBSUMED; return ES_NOFIX; } template forceinline Intersection::Intersection(Space* home, View0 y0,View1 y1,View2 y2) : InhomTernaryPropagator(home,y0,y1,y2) {} template forceinline Intersection::Intersection(Space* home, bool share, Intersection& p) : InhomTernaryPropagator(home,share,p) {} /* * "Nary intersection" propagator * */ template forceinline IntersectionN::IntersectionN(Space* home, ViewArray& x, View1 y) : InhomNaryOnePropagator(home,x,y), intOfDets(home) { shared = x.shared() || viewarrayshared(x,y); } template forceinline IntersectionN::IntersectionN(Space* home, bool share, IntersectionN& p) : InhomNaryOnePropagator(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::Set::card_max)); 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 Actor* IntersectionN::copy(Space* home, bool share) { return new (home) IntersectionN(home,share,*this); } template PropCost IntersectionN::cost(void) const { return PC_QUADRATIC_LO; } template ExecStatus IntersectionN::propagate(Space* home) { bool repeat = false; do { repeat = false; int xsize = x.size(); 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.exclude(home, Limits::Set::int_min, Limits::Set::int_max) ); intOfDets.dispose(home); return ES_SUBSUMED; } } { 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); rightSet[xsize-1].init(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); rightSet[i].init(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.exclude(home,Limits::Set::int_min, Limits::Set::int_max) ); intOfDets.dispose(home); return ES_SUBSUMED; } } } 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; } } while (repeat); return shared ? ES_NOFIX : ES_FIX; } }}} // STATISTICS: set-prop