/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2004 * * Last modified: * $Date: 2011-08-09 02:04:53 +1000 (Tue, 09 Aug 2011) $ by $Author: schulte $ * $Revision: 12253 $ * * 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. * */ #include namespace Gecode { namespace Int { namespace Arithmetic { /* * Ternary bounds consistent maximum * */ template forceinline ExecStatus prop_max_bnd(Space& home, View x0, View x1, View x2) { bool mod = false; do { mod = false; { ModEvent me = x2.lq(home,std::max(x0.max(),x1.max())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x2.gq(home,std::max(x0.min(),x1.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.lq(home,x2.max()); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.lq(home,x2.max()); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } } while (mod); return ES_OK; } template forceinline MaxBnd::MaxBnd(Home home, View x0, View x1, View x2) : TernaryPropagator(home,x0,x1,x2) {} template ExecStatus MaxBnd::post(Home home, View x0, View x1, View x2) { GECODE_ME_CHECK(x2.gq(home,std::max(x0.min(),x1.min()))); GECODE_ME_CHECK(x2.lq(home,std::max(x0.max(),x1.max()))); if (same(x0,x1)) return Rel::EqBnd::post(home,x0,x2); if (same(x0,x2)) return Rel::Lq::post(home,x1,x2); if (same(x1,x2)) return Rel::Lq::post(home,x0,x2); (void) new (home) MaxBnd(home,x0,x1,x2); return ES_OK; } template forceinline MaxBnd::MaxBnd(Space& home, bool share, MaxBnd& p) : TernaryPropagator(home,share,p) {} template forceinline MaxBnd::MaxBnd(Space& home, bool share, Propagator& p, View x0, View x1, View x2) : TernaryPropagator(home,share,p,x0,x1,x2) {} template Actor* MaxBnd::copy(Space& home, bool share) { return new (home) MaxBnd(home,share,*this); } template ExecStatus MaxBnd::propagate(Space& home, const ModEventDelta&) { GECODE_ES_CHECK(prop_max_bnd(home,x0,x1,x2)); if ((x0.max() <= x1.min()) || (x0.max() < x2.min())) GECODE_REWRITE(*this,(Rel::EqBnd::post(home(*this),x1,x2))); if ((x1.max() <= x0.min()) || (x1.max() < x2.min())) GECODE_REWRITE(*this,(Rel::EqBnd::post(home(*this),x0,x2))); return x0.assigned() && x1.assigned() && x2.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Nary bounds consistent maximum * */ template forceinline NaryMaxBnd::NaryMaxBnd(Home home, ViewArray& x, View y) : NaryOnePropagator(home,x,y) {} template ExecStatus NaryMaxBnd::post(Home home, ViewArray& x, View y) { assert(x.size() > 0); x.unique(home); if (x.size() == 1) return Rel::EqBnd::post(home,x[0],y); if (x.size() == 2) return MaxBnd::post(home,x[0],x[1],y); int l = Int::Limits::min; int u = Int::Limits::min; for (int i=x.size(); i--; ) { l = std::max(l,x[i].min()); u = std::max(u,x[i].max()); } GECODE_ME_CHECK(y.gq(home,l)); GECODE_ME_CHECK(y.lq(home,u)); if (x.same(home,y)) { // Check whether y occurs in x for (int i=x.size(); i--; ) GECODE_ES_CHECK(Rel::Lq::post(home,x[i],y)); } else { (void) new (home) NaryMaxBnd(home,x,y); } return ES_OK; } template forceinline NaryMaxBnd::NaryMaxBnd(Space& home, bool share, NaryMaxBnd& p) : NaryOnePropagator(home,share,p) {} template Actor* NaryMaxBnd::copy(Space& home, bool share) { if (x.size() == 1) return new (home) Rel::EqBnd(home,share,*this,x[0],y); if (x.size() == 2) return new (home) MaxBnd(home,share,*this,x[0],x[1],y); return new (home) NaryMaxBnd(home,share,*this); } /// Status of propagation for nary max enum MaxPropStatus { MPS_ASSIGNED = 1<<0, ///< All views are assigned MPS_REMOVED = 1<<1, ///< A view is removed MPS_NEW_BOUND = 1<<2 ///< Telling has found a new upper bound }; template forceinline ExecStatus prop_nary_max_bnd(Space& home, Propagator& p, ViewArray& x, View y, PropCond pc) { rerun: assert(x.size() > 0); int maxmax = x[x.size()-1].max(); int maxmin = x[x.size()-1].min(); for (int i = x.size()-1; i--; ) { maxmax = std::max(x[i].max(),maxmax); maxmin = std::max(x[i].min(),maxmin); } GECODE_ME_CHECK(y.lq(home,maxmax)); GECODE_ME_CHECK(y.gq(home,maxmin)); maxmin = y.min(); maxmax = y.max(); int status = MPS_ASSIGNED; for (int i = x.size(); i--; ) { ModEvent me = x[i].lq(home,maxmax); if (me == ME_INT_FAILED) return ES_FAILED; if (me_modified(me) && (x[i].max() != maxmax)) status |= MPS_NEW_BOUND; if (x[i].max() < maxmin) { x.move_lst(i,home,p,pc); status |= MPS_REMOVED; } else if (!x[i].assigned()) status &= ~MPS_ASSIGNED; } if (x.size() == 0) return ES_FAILED; if ((status & MPS_REMOVED) != 0) goto rerun; if (((status & MPS_ASSIGNED) != 0) && y.assigned()) return home.ES_SUBSUMED(p); return ((status & MPS_NEW_BOUND) != 0) ? ES_NOFIX : ES_FIX; } template ExecStatus NaryMaxBnd::propagate(Space& home, const ModEventDelta&) { return prop_nary_max_bnd(home,*this,x,y,PC_INT_BND); } /* * Ternary domain consistent maximum * */ template forceinline MaxDom::MaxDom(Home home, View x0, View x1, View x2) : TernaryPropagator(home,x0,x1,x2) {} template ExecStatus MaxDom::post(Home home, View x0, View x1, View x2) { GECODE_ME_CHECK(x2.gq(home,std::max(x0.min(),x1.min()))); GECODE_ME_CHECK(x2.lq(home,std::max(x0.max(),x1.max()))); if (same(x0,x1)) return Rel::EqDom::post(home,x0,x2); if (same(x0,x2)) return Rel::Lq::post(home,x1,x2); if (same(x1,x2)) return Rel::Lq::post(home,x0,x2); (void) new (home) MaxDom(home,x0,x1,x2); return ES_OK; } template forceinline MaxDom::MaxDom(Space& home, bool share, MaxDom& p) : TernaryPropagator(home,share,p) {} template forceinline MaxDom::MaxDom(Space& home, bool share, Propagator& p, View x0, View x1, View x2) : TernaryPropagator(home,share,p,x0,x1,x2) {} template Actor* MaxDom::copy(Space& home, bool share) { return new (home) MaxDom(home,share,*this); } template PropCost MaxDom::cost(const Space&, const ModEventDelta& med) const { return PropCost::ternary((View::me(med) == ME_INT_DOM) ? PropCost::HI : PropCost::LO); } template ExecStatus MaxDom::propagate(Space& home, const ModEventDelta& med) { if (View::me(med) != ME_INT_DOM) { GECODE_ES_CHECK(prop_max_bnd(home,x0,x1,x2)); if ((x0.max() <= x1.min()) || (x0.max() < x2.min())) GECODE_REWRITE(*this,(Rel::EqDom::post(home(*this),x1,x2))); if ((x1.max() <= x0.min()) || (x1.max() < x2.min())) GECODE_REWRITE(*this,(Rel::EqDom::post(home(*this),x0,x2))); return x0.assigned() && x1.assigned() && x2.assigned() ? home.ES_SUBSUMED(*this) : home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM)); } ViewRanges r0(x0), r1(x1); Iter::Ranges::Union,ViewRanges > u(r0,r1); GECODE_ME_CHECK(x2.inter_r(home,u,false)); if (rtest_nq_dom(x0,x2) == RT_TRUE) { GECODE_ES_CHECK(Rel::Lq::post(home,x0,x2)); GECODE_REWRITE(*this,(Rel::EqDom::post(home(*this),x1,x2))); } if (rtest_nq_dom(x1,x2) == RT_TRUE) { GECODE_ES_CHECK(Rel::Lq::post(home,x1,x2)); GECODE_REWRITE(*this,(Rel::EqDom::post(home(*this),x0,x2))); } return ES_FIX; } /* * Nary domain consistent maximum * */ template forceinline NaryMaxDom::NaryMaxDom(Home home, ViewArray& x, View y) : NaryOnePropagator(home,x,y) {} template ExecStatus NaryMaxDom::post(Home home, ViewArray& x, View y) { assert(x.size() > 0); x.unique(home); if (x.size() == 1) return Rel::EqDom::post(home,x[0],y); if (x.size() == 2) return MaxDom::post(home,x[0],x[1],y); int l = Int::Limits::min; int u = Int::Limits::min; for (int i=x.size(); i--; ) { l = std::max(l,x[i].min()); u = std::max(u,x[i].max()); } GECODE_ME_CHECK(y.gq(home,l)); GECODE_ME_CHECK(y.lq(home,u)); if (x.same(home,y)) { // Check whether y occurs in x for (int i=x.size(); i--; ) GECODE_ES_CHECK(Rel::Lq::post(home,x[i],y)); } else { (void) new (home) NaryMaxDom(home,x,y); } return ES_OK; } template forceinline NaryMaxDom::NaryMaxDom(Space& home, bool share, NaryMaxDom& p) : NaryOnePropagator(home,share,p) {} template Actor* NaryMaxDom::copy(Space& home, bool share) { if (x.size() == 1) return new (home) Rel::EqDom(home,share,*this,x[0],y); if (x.size() == 2) return new (home) MaxDom(home,share,*this,x[0],x[1],y); return new (home) NaryMaxDom(home,share,*this); } template PropCost NaryMaxDom::cost(const Space&, const ModEventDelta& med) const { if (View::me(med) == ME_INT_DOM) return PropCost::linear(PropCost::HI, y.size()); else return PropCost::linear(PropCost::LO, x.size()); } template ExecStatus NaryMaxDom::propagate(Space& home, const ModEventDelta& med) { if (View::me(med) != ME_INT_DOM) { ExecStatus es = prop_nary_max_bnd(home,*this,x,y,PC_INT_DOM); GECODE_ES_CHECK(es); return (es == ES_FIX) ? home.ES_FIX_PARTIAL(*this,View::med(ME_INT_DOM)) : home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM)); } Region r(home); ViewRanges* i_x = r.alloc >(x.size()); for (int i = x.size(); i--; ) { ViewRanges i_xi(x[i]); i_x[i]=i_xi; } Iter::Ranges::NaryUnion u(r, i_x, x.size()); GECODE_ME_CHECK(y.inter_r(home,u,false)); for (int i = x.size(); i--; ) if (rtest_nq_dom(x[i],y) == RT_TRUE) { GECODE_ES_CHECK(Rel::Lq::post(home,x[i],y)); x.move_lst(i,home,*this,PC_INT_DOM); } assert(x.size() > 0); if (x.size() == 1) GECODE_REWRITE(*this,(Rel::EqDom::post(home(*this),x[0],y))); return ES_FIX; } }}} // STATISTICS: int-prop