/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2008 * * 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 { /* * Positive bounds consistent squaring * */ template forceinline ExecStatus prop_sqr_plus_bnd(Space& home, VA x0, VB x1) { bool mod; do { mod = false; { ModEvent me = x0.lq(home,floor(::sqrt(static_cast(x1.max())))); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.gq(home,ceil(::sqrt(static_cast(x1.min())))); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.lq(home,x0.max()*x0.max()); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.gq(home,x0.min()*x0.min()); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } } while (mod); return ES_OK; } template forceinline SqrPlusBnd::SqrPlusBnd(Home home, VA x0, VB x1) : MixBinaryPropagator(home,x0,x1) {} template forceinline ExecStatus SqrPlusBnd::post(Home home, VA x0, VB x1) { GECODE_ES_CHECK(prop_sqr_plus_bnd(home,x0,x1)); (void) new (home) SqrPlusBnd(home,x0,x1); return ES_OK; } template forceinline SqrPlusBnd::SqrPlusBnd(Space& home, bool share, SqrPlusBnd& p) : MixBinaryPropagator(home,share,p) {} template Actor* SqrPlusBnd::copy(Space& home, bool share) { return new (home) SqrPlusBnd(home,share,*this); } template ExecStatus SqrPlusBnd::propagate(Space& home, const ModEventDelta&) { GECODE_ES_CHECK(prop_sqr_plus_bnd(home,x0,x1)); return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Bounds consistent squaring * */ template forceinline SqrBnd::SqrBnd(Home home, View x0, View x1) : BinaryPropagator(home,x0,x1) {} template forceinline ExecStatus SqrBnd::post(Home home, View x0, View x1) { GECODE_ME_CHECK(x1.gq(home,0)); if (same(x0,x1)) { GECODE_ME_CHECK(x1.lq(home,1)); } else { GECODE_ME_CHECK(x0.lq(home,floor(::sqrt(static_cast (Limits::max))))); GECODE_ME_CHECK(x0.gq(home,-floor(::sqrt(static_cast (-Limits::min))))); if (x0.min() >= 0) return SqrPlusBnd::post(home,x0,x1); if (x0.max() <= 0) return SqrPlusBnd::post(home,MinusView(x0),x1); GECODE_ME_CHECK(x1.lq(home, std::max(x0.min()*x0.min(),x0.max()*x0.max()))); (void) new (home) SqrBnd(home,x0,x1); } return ES_OK; } template forceinline SqrBnd::SqrBnd(Space& home, bool share, SqrBnd& p) : BinaryPropagator(home,share,p) {} template Actor* SqrBnd::copy(Space& home, bool share) { return new (home) SqrBnd(home,share,*this); } template ExecStatus SqrBnd::propagate(Space& home, const ModEventDelta&) { assert(x1.min() >= 0); if (x0.min() >= 0) GECODE_REWRITE(*this,(SqrPlusBnd::post(home(*this),x0,x1))); if (x0.max() <= 0) GECODE_REWRITE(*this,(SqrPlusBnd::post(home(*this), MinusView(x0),x1))); GECODE_ME_CHECK(x1.lq(home,std::max(x0.min()*x0.min(), x0.max()*x0.max()))); int s = static_cast(floor(::sqrt(static_cast(x1.max())))); GECODE_ME_CHECK(x0.gq(home,-s)); GECODE_ME_CHECK(x0.lq(home,s)); if (x0.assigned() && x1.assigned()) return (x0.val()*x0.val() == x1.val()) ? home.ES_SUBSUMED(*this) : ES_FAILED; return ES_NOFIX; } /* * Value mappings for squaring and square root * */ /// Mapping integer to square class ValuesMapSqr { public: /// Perform mapping forceinline int val(int n) const { return n*n; } }; /// Mapping integer to square root class ValuesMapSqrt { public: /// Perform mapping forceinline int val(int n) const { return static_cast(floor(::sqrt(static_cast(n)))); } }; /* * Positive domain consistent squaring * */ template forceinline SqrPlusDom::SqrPlusDom(Home home, VA x0, VB x1) : MixBinaryPropagator(home,x0,x1) {} template forceinline ExecStatus SqrPlusDom::post(Home home, VA x0, VB x1) { GECODE_ES_CHECK(prop_sqr_plus_bnd(home,x0,x1)); (void) new (home) SqrPlusDom(home,x0,x1); return ES_OK; } template forceinline SqrPlusDom::SqrPlusDom(Space& home, bool share, SqrPlusDom& p) : MixBinaryPropagator(home,share,p) {} template Actor* SqrPlusDom::copy(Space& home, bool share) { return new (home) SqrPlusDom(home,share,*this); } template PropCost SqrPlusDom::cost(const Space&, const ModEventDelta& med) const { if (VA::me(med) == ME_INT_VAL) return PropCost::unary(PropCost::LO); else if (VA::me(med) == ME_INT_DOM) return PropCost::binary(PropCost::HI); else return PropCost::binary(PropCost::LO); } template ExecStatus SqrPlusDom::propagate(Space& home, const ModEventDelta& med) { if (VA::me(med) != ME_INT_DOM) { GECODE_ES_CHECK(prop_sqr_plus_bnd(home,x0,x1)); return x0.assigned() ? home.ES_SUBSUMED(*this) : home.ES_NOFIX_PARTIAL(*this,VA::med(ME_INT_DOM)); } { ViewValues v0(x0); Iter::Values::Map,ValuesMapSqr> s0(v0); GECODE_ME_CHECK(x1.inter_v(home,s0,false)); } { ViewValues v1(x1); Iter::Values::Map,ValuesMapSqrt> s1(v1); GECODE_ME_CHECK(x0.inter_v(home,s1,false)); } return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Domain consistent squaring * */ template forceinline SqrDom::SqrDom(Home home, View x0, View x1) : BinaryPropagator(home,x0,x1) {} template forceinline ExecStatus SqrDom::post(Home home, View x0, View x1) { GECODE_ME_CHECK(x1.gq(home,0)); if (same(x0,x1)) { GECODE_ME_CHECK(x1.lq(home,1)); } else { GECODE_ME_CHECK(x0.lq(home,floor(::sqrt(static_cast (Limits::max))))); GECODE_ME_CHECK(x0.gq(home,-floor(::sqrt(static_cast (-Limits::min))))); if (x0.min() >= 0) return SqrPlusDom::post(home,x0,x1); if (x0.max() <= 0) return SqrPlusDom::post(home,MinusView(x0),x1); GECODE_ME_CHECK(x1.lq(home, std::max(x0.min()*x0.min(),x0.max()*x0.max()))); (void) new (home) SqrDom(home,x0,x1); } return ES_OK; } template forceinline SqrDom::SqrDom(Space& home, bool share, SqrDom& p) : BinaryPropagator(home,share,p) {} template Actor* SqrDom::copy(Space& home, bool share) { return new (home) SqrDom(home,share,*this); } template PropCost SqrDom::cost(const Space&, const ModEventDelta& med) const { if (View::me(med) == ME_INT_VAL) return PropCost::unary(PropCost::LO); else if (View::me(med) == ME_INT_DOM) return PropCost::binary(PropCost::HI); else return PropCost::binary(PropCost::LO); } template ExecStatus SqrDom::propagate(Space& home, const ModEventDelta& med) { assert(x1.min() >= 0); if (View::me(med) != ME_INT_DOM) { if (x0.min() >= 0) GECODE_REWRITE(*this,(SqrPlusDom::post(home(*this),x0,x1))); if (x0.max() <= 0) GECODE_REWRITE(*this,(SqrPlusDom::post(home(*this), MinusView(x0),x1))); GECODE_ME_CHECK(x1.lq(home,std::max(x0.min()*x0.min(), x0.max()*x0.max()))); int s = static_cast(floor(::sqrt(static_cast(x1.max())))); GECODE_ME_CHECK(x0.gq(home,-s)); GECODE_ME_CHECK(x0.lq(home,s)); if (x0.assigned() && x1.assigned()) return (x0.val()*x0.val() == x1.val()) ? home.ES_SUBSUMED(*this) : ES_FAILED; return home.ES_NOFIX_PARTIAL(*this,View::med(ME_INT_DOM)); } Region r(home); { ViewValues i(x0), j(x0); using namespace Iter::Values; Positive > p(i); Negative > n(j); Minus m(r,n); Map >,ValuesMapSqr,true> sp(p); Map sm(m); Union >,ValuesMapSqr,true>, Map > u(sp,sm); GECODE_ME_CHECK(x1.inter_v(home,u,false)); } { ViewValues i(x1), j(x1); using namespace Iter::Values; Map,ValuesMapSqrt,true> si(i), sj(j); Minus mi(r,si); Union,ValuesMapSqrt,true> > u(mi,sj); GECODE_ME_CHECK(x0.inter_v(home,u,false)); } return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } }}} // STATISTICS: int-prop