/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2004 * * 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 (x0 == x1) return Rel::EqBnd::post(home,x0,x2); if (x0 == x2) return Rel::Lq::post(home,x1,x2); if (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, MaxBnd& p) : TernaryPropagator(home,p) {} template forceinline MaxBnd::MaxBnd(Space& home, Propagator& p, View x0, View x1, View x2) : TernaryPropagator(home,p,x0,x1,x2) {} template Actor* MaxBnd::copy(Space& home) { return new (home) MaxBnd(home,*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(); 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 = x[0].min(); int u = x[0].max(); for (int i=1; i::post(home,x[i],y))); } else { (void) new (home) NaryMaxBnd(home,x,y); } return ES_OK; } template forceinline NaryMaxBnd::NaryMaxBnd(Space& home, NaryMaxBnd& p) : NaryOnePropagator(home,p) {} template Actor* NaryMaxBnd::copy(Space& home) { if (x.size() == 1) return new (home) Rel::EqBnd(home,*this,x[0],y); if (x.size() == 2) return new (home) MaxBnd(home,*this,x[0],x[1],y); return new (home) NaryMaxBnd(home,*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[0].max(); int maxmin = x[0].min(); for (int i=1; i ExecStatus NaryMaxBnd::propagate(Space& home, const ModEventDelta&) { ExecStatus es = prop_nary_max_bnd(home,*this,x,y,PC_INT_BND); GECODE_ES_CHECK(es); if (x.size() == 1) GECODE_REWRITE(*this,(Rel::EqBnd::post(home(*this),x[0],y))); return es; } /* * 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 (x0 == x1) return Rel::EqDom::post(home,x0,x2); if (x0 == x2) return Rel::Lq::post(home,x1,x2); if (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, MaxDom& p) : TernaryPropagator(home,p) {} template forceinline MaxDom::MaxDom(Space& home, Propagator& p, View x0, View x1, View x2) : TernaryPropagator(home,p,x0,x1,x2) {} template Actor* MaxDom::copy(Space& home) { return new (home) MaxDom(home,*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(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(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(); 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 = x[0].min(); int u = x[0].max(); for (int i=0; i::post(home,x[i],y))); } else { (void) new (home) NaryMaxDom(home,x,y); } return ES_OK; } template forceinline NaryMaxDom::NaryMaxDom(Space& home, NaryMaxDom& p) : NaryOnePropagator(home,p) {} template Actor* NaryMaxDom::copy(Space& home) { if (x.size() == 1) return new (home) Rel::EqDom(home,*this,x[0],y); if (x.size() == 2) return new (home) MaxDom(home,*this,x[0],x[1],y); return new (home) NaryMaxDom(home,*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; ViewRanges* i_x = r.alloc >(x.size()); for (int i=0; i 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(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