ExecStatus ReEq<View0,View1>::propagate(Space& home, const ModEventDelta&) { if (b.one()) GECODE_REWRITE(*this,(Eq<View0,View1>::post(home(*this),x0,x1))); if (b.zero()) GECODE_REWRITE(*this,(Distinct<View0,View1>::post(home(*this),x0,x1))); if (x0.assigned() && x1.assigned()) { // directly test x0==x1 GlbRanges<View0> x0lb(x0); GlbRanges<View1> x1lb(x1); for (; x0lb() && x1lb(); ++x0lb, ++x1lb) { if (x0lb.min() != x1lb.min() || x0lb.max() != x1lb.max()) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } } if (!x0lb() && !x1lb()) { GECODE_ME_CHECK(b.one_none(home)); return home.ES_SUBSUMED(*this); } else { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } } // check whether cardinalities still allow equality if (x0.cardMin() > x1.cardMax() || x1.cardMin() > x0.cardMax()) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } // check glb(x0) subset lub(x1) GlbRanges<View0> x0lb(x0); LubRanges<View1> x1ub(x1); Iter::Ranges::Diff<GlbRanges<View0>, LubRanges<View1> > diff1(x0lb, x1ub); if ( diff1() ) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } // check glb(x1) subset lub(x0) GlbRanges<View1> x1lb(x1); LubRanges<View0> x0ub(x0); Iter::Ranges::Diff<GlbRanges<View1>, LubRanges<View0> > diff2(x1lb, x0ub); if ( diff2() ) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } return ES_FIX; }
ExecStatus ReSubset<View0,View1>::propagate(Space& home, const ModEventDelta&) { if (b.one()) GECODE_REWRITE(*this,(Subset<View0,View1>::post(home(*this),x0,x1))); if (b.zero()) GECODE_REWRITE(*this,(NoSubset<View0,View1>::post(home(*this),x0,x1))); // check whether cardinalities still allow subset if (x0.cardMin() > x1.cardMax()) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } // check lub(x0) subset glb(x1) { LubRanges<View0> x0ub(x0); GlbRanges<View1> x1lb(x1); Iter::Ranges::Diff<LubRanges<View0>,GlbRanges<View1> > d(x0ub,x1lb); if (!d()) { GECODE_ME_CHECK(b.one_none(home)); return home.ES_SUBSUMED(*this); } } // check glb(x0) subset lub(x1) { GlbRanges<View0> x0lb(x0); LubRanges<View1> x1ub(x1); Iter::Ranges::Diff<GlbRanges<View0>,LubRanges<View1> > d(x0lb,x1ub); if (d()) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } else if (x0.assigned() && x1.assigned()) { GECODE_ME_CHECK(b.one_none(home)); return home.ES_SUBSUMED(*this); } } if (x0.cardMin() > 0) { LubRanges<View0> x0ub(x0); LubRanges<View1> x1ub(x1); Iter::Ranges::Inter<LubRanges<View0>,LubRanges<View1> > i(x0ub,x1ub); if (!i()) { GECODE_ME_CHECK(b.zero_none(home)); return home.ES_SUBSUMED(*this); } } return ES_FIX; }
ExecStatus Eq<View0,View1>::propagate(Space& home, const ModEventDelta& med) { ModEvent me0 = View0::me(med); ModEvent me1 = View1::me(med); Region r(home); if (testSetEventLB(me0,me1)) { GlbRanges<View0> x0lb(x0); GlbRanges<View1> x1lb(x1); Iter::Ranges::Union<GlbRanges<View0>,GlbRanges<View1> > lbu(x0lb,x1lb); Iter::Ranges::Cache<Iter::Ranges::Union <GlbRanges<View0>, GlbRanges<View1> > > lbuc(r,lbu); GECODE_ME_CHECK(x0.includeI(home,lbuc)); lbuc.reset(); GECODE_ME_CHECK(x1.includeI(home,lbuc)); } if (testSetEventUB(me0,me1)) { LubRanges<View0> x0ub(x0); LubRanges<View1> x1ub(x1); Iter::Ranges::Inter<LubRanges<View0>,LubRanges<View1> > ubi(x0ub,x1ub); Iter::Ranges::Cache<Iter::Ranges::Inter <LubRanges<View0>,LubRanges<View1> > > ubic(r,ubi); GECODE_ME_CHECK(x0.intersectI(home,ubic)); ubic.reset(); GECODE_ME_CHECK(x1.intersectI(home,ubic)); } if (testSetEventCard(me0,me1) ) { unsigned int max = std::min(x0.cardMax(),x1.cardMax()); unsigned int min = std::max(x0.cardMin(),x1.cardMin()); GECODE_ME_CHECK ( x0.cardMax(home,max) ); GECODE_ME_CHECK ( x1.cardMax(home,max) ); GECODE_ME_CHECK ( x0.cardMin(home,min) ); GECODE_ME_CHECK ( x1.cardMin(home,min) ); } if (x0.assigned()) { assert (x1.assigned()); return home.ES_SUBSUMED(*this); } return shared(x0,x1) ? ES_NOFIX : ES_FIX; }
ExecStatus Subset<View0,View1>::propagate(Space& home, const ModEventDelta&) { bool oneassigned = x0.assigned() || x1.assigned(); unsigned int x0glbsize; do { GlbRanges<View0> x0lb(x0); GECODE_ME_CHECK ( x1.includeI(home,x0lb) ); GECODE_ME_CHECK ( x1.cardMin(home,x0.cardMin()) ); LubRanges<View1> x1ub(x1); x0glbsize = x0.glbSize(); GECODE_ME_CHECK ( x0.intersectI(home,x1ub) ); GECODE_ME_CHECK ( x0.cardMax(home,x1.cardMax()) ); } while (x0.glbSize() > x0glbsize); if (x0.cardMin() == x1.cardMax()) GECODE_REWRITE(*this,(Eq<View0,View1>::post(home(*this),x0,x1))); if (shared(x0,x1)) { return oneassigned ? home.ES_SUBSUMED(*this) : ES_NOFIX; } return (x0.assigned() || x1.assigned()) ? home.ES_SUBSUMED(*this) : ES_FIX; }
ExecStatus Union<View0,View1,View2>::propagate(Space& home, const 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<View0> x0ub(x0); LubRanges<View1> x1ub(x1); Iter::Ranges::Union<LubRanges<View0>, LubRanges<View1> > u2(x0ub,x1ub); GECODE_ME_CHECK_MODIFIED(modified, x2.intersectI(home,u2) ); } if (modified || Rel::testSetEventUB(me2) ) { modified = false; // 5) x0 <= x2 LubRanges<View2> x2ub1(x2); GECODE_ME_CHECK_MODIFIED(modified, x0.intersectI(home,x2ub1) ); x0ubmod |= modified; // 6) x1 <= x2 bool modified2=false; LubRanges<View2> 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<View2> x2lb(x2); LubRanges<View0> x0ub(x0); Iter::Ranges::Diff<GlbRanges<View2>, LubRanges<View0> > 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<View2> x2lb(x2); LubRanges<View1> x1ub(x1); Iter::Ranges::Diff<GlbRanges<View2>, LubRanges<View1> > 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<View0> x0lb(x0); GlbRanges<View1> x1lb(x1); Iter::Ranges::Union<GlbRanges<View0>, GlbRanges<View1> > 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 home.ES_SUBSUMED(*this); } if (x0.cardMax() == 0) GECODE_REWRITE(*this,(Rel::Eq<View1,View2>::post(home(*this),x1,x2))); if (x1.cardMax() == 0) GECODE_REWRITE(*this,(Rel::Eq<View0,View2>::post(home(*this),x0,x2))); } while(modified); if (shared(x0,x1,x2)) { return (x0ass && x1ass && x2ass) ? home.ES_SUBSUMED(*this) : ES_NOFIX; } else { if (x0ass && x1ass && x2ass) return home.ES_SUBSUMED(*this); if (x0ass != x0.assigned() || x1ass != x1.assigned() || x2ass != x2.assigned()) { return ES_NOFIX; } else { return ES_FIX; } } }
ExecStatus ElementUnion<SView,RView>::propagate(Space& home, const ModEventDelta&) { Region r(home); 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<RView> x1ub(x1); Iter::Ranges::Cache<LubRanges<RView> > x1ubc(r,x1ub); Iter::Ranges::ToValues<Iter::Ranges::Cache<LubRanges<RView> > > vx1ub(x1ubc); GlbRanges<RView> x1lb(x1); Iter::Ranges::Cache<GlbRanges<RView> > x1lbc(r,x1lb); Iter::Ranges::ToValues<Iter::Ranges::Cache<GlbRanges<RView> > > 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)); GLBndSet* before = static_cast<GLBndSet*>(r.ralloc(sizeof(GLBndSet)*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].view.cancel(home,*this, PC_SET_ANY); ++i; continue; } assert(iv[i].idx == vx1ub.val()); iv[j] = iv[i]; SView candidate = iv[j].view; int candidateInd = iv[j].idx; // inter = glb(candidate) & complement(lub(x0)) GlbRanges<SView> candlb(candidate); LubRanges<SView> x0ub(x0); Iter::Ranges::Diff<GlbRanges<SView>, LubRanges<SView> > diff(candlb, x0ub); bool selectSingleInconsistent = false; if (x1.cardMax() <= 1) { GlbRanges<SView> x0lb(x0); LubRanges<SView> candub(candidate); Iter::Ranges::Diff<GlbRanges<SView>, LubRanges<SView> > 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].view.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<SView> candlb(candidate); ModEvent me = x0.includeI(home,candlb); loopVar |= me_modified(me); GECODE_ME_CHECK(me); LubRanges<SView> 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<SView> cub(candidate); sofarBefore.includeI(home,cub); GlbRanges<SView> 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<n; k++) { iv[k].view.cancel(home,*this, PC_SET_ANY); } n = j; iv.size(n); if (x1.cardMax()==0) { // Selector is empty, hence the result must be empty { GECODE_ME_CHECK(x0.cardMax(home,0)); } for (int i=n; i--;) before[i].dispose(home); return home.ES_SUBSUMED(*this); } if (x1.cardMin() > 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<SView> x0lb(x0); Iter::Ranges::Union<BndSetRanges, BndSetRanges> inter(b,s); Iter::Ranges::Diff<GlbRanges<SView>, Iter::Ranges::Union<BndSetRanges,BndSetRanges> > 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].view.includeI(home,diff); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } LubRanges<SView> iviub(iv[i].view); 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 = static_cast<int>(x1.lubSize()); if (ubsize > 2) { assert(ubsize==n); ViewArray<SView> is(home,ubsize); for (int i=n; i--;) is[i]=iv[i].view; GECODE_REWRITE(*this,(RelOp::UnionN<SView, SView> ::post(home(*this),is,x0))); } else if (ubsize == 2) { assert(n==2); SView a = iv[0].view; SView b = iv[1].view; GECODE_REWRITE(*this,(RelOp::Union<SView, SView, SView> ::post(home(*this),a,b,x0))); } else if (ubsize == 1) { assert(n==1); GECODE_REWRITE(*this,(Rel::Eq<SView,SView>::post(home(*this),x0,iv[0].view))); } else { GECODE_ME_CHECK(x0.cardMax(home, 0)); return home.ES_SUBSUMED(*this); } } bool allAssigned = true; for (int i=iv.size(); i--;) { if (!iv[i].view.assigned()) { allAssigned = false; break; } } if (x0.assigned() && x1.assigned() && allAssigned) { return home.ES_SUBSUMED(*this); } return ES_FIX; }
ExecStatus ElementUnionConst<SView,RView>::propagate(Space& home, const ModEventDelta&) { Region r(home); bool* stillSelected = r.alloc<bool>(n_iv); bool loopVar; do { loopVar = false; for (int i=n_iv; i--;) stillSelected[i] = false; // Cache the upper bound iterator, as we have to // modify the upper bound while iterating LubRanges<RView> x1ub(x1); Iter::Ranges::Cache x1ubc(r,x1ub); Iter::Ranges::ToValues<Iter::Ranges::Cache> vx1ub(x1ubc); GlbRanges<RView> x1lb(x1); Iter::Ranges::Cache x1lbc(r,x1lb); Iter::Ranges::ToValues<Iter::Ranges::Cache> 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. GLBndSet sofarBefore(home); LUBndSet selectedInter(home, IntSet (Limits::min, Limits::max)); GLBndSet* before = static_cast<GLBndSet*>(r.ralloc(sizeof(GLBndSet)*n_iv)); unsigned int maxCard = 0; unsigned int minCard = Limits::card; while (vx1ub()) { int i = vx1ub.val(); IntSetRanges candCardR(iv[i]); unsigned int candidateCard = Iter::Ranges::size(candCardR); IntSetRanges candlb(iv[i]); LubRanges<SView> x0ub(x0); Iter::Ranges::Diff<IntSetRanges, LubRanges<SView> > diff(candlb, x0ub); bool selectSingleInconsistent = false; if (x1.cardMax() <= 1) { GlbRanges<SView> x0lb(x0); IntSetRanges candub(iv[i]); Iter::Ranges::Diff<GlbRanges<SView>, IntSetRanges > diff2(x0lb, candub); selectSingleInconsistent = diff2() || candidateCard < 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 || candidateCard > x0.cardMax() || diff()) { ModEvent me = (x1.exclude(home,i)); loopVar |= me_modified(me); GECODE_ME_CHECK(me); } else { stillSelected[i] = true; // if x_i is consistent, check whether we know // that its index is in x1 if (vx1() && vx1.val()==i) { // x0 >= candidate, candidate <= x0 // GlbRanges<SView> candlb(candidate); IntSetRanges candlb(iv[i]); ModEvent me = x0.includeI(home,candlb); loopVar |= me_modified(me); GECODE_ME_CHECK(me); ++vx1; } new (&before[i]) GLBndSet(home); before[i].update(home,sofarBefore); IntSetRanges cub(iv[i]); sofarBefore.includeI(home,cub); IntSetRanges clb(iv[i]); selectedInter.intersectI(home,clb); maxCard = std::max(maxCard, candidateCard); minCard = std::min(minCard, candidateCard); } ++vx1ub; } if (x1.cardMax()==0) { // Selector is empty, hence the result must be empty { GECODE_ME_CHECK(x0.cardMax(home,0)); } for (int i=n_iv; i--;) if (stillSelected[i]) before[i].dispose(home); selectedInter.dispose(home); sofarBefore.dispose(home); return home.ES_SUBSUMED(*this); } if (x1.cardMin() > 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_iv; i--;) { if (!stillSelected[i]) continue; BndSetRanges b(before[i]); BndSetRanges s(sofarAfter); GlbRanges<SView> x0lb(x0); Iter::Ranges::Union<BndSetRanges, BndSetRanges> inter(b,s); Iter::Ranges::Diff<GlbRanges<SView>, Iter::Ranges::Union<BndSetRanges,BndSetRanges> > diff(x0lb, inter); if (diff()) { ModEvent me = (x1.include(home,i)); loopVar |= me_modified(me); GECODE_ME_CHECK(me); // candidate != extra IntSetRanges ivi(iv[i]); if (!Iter::Ranges::subset(diff, ivi)) GECODE_ME_CHECK(ME_SET_FAILED); } IntSetRanges iviub(iv[i]); sofarAfter.includeI(home,iviub); before[i].dispose(home); } sofarAfter.dispose(home); } while (loopVar); if (x1.assigned()) { assert(x0.assigned()); return home.ES_SUBSUMED(*this); } return ES_FIX; }