/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * Copyright: * Christian Schulte, 2012 * * 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 #include #include namespace Gecode { namespace Int { namespace Arithmetic { /* * Positive bounds consistent nth root * */ template forceinline ExecStatus prop_nroot_plus_bnd(Space& home, IntView x0, IntView x1, const Ops& ops) { if (minus) { bool mod; do { mod = false; { ModEvent me = x1.gq(home,-ops.cnroot(-x0.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.lq(home,-ops.cnroot(-x0.max())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.gq(home,-ops.tpow(-x1.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.lq(home,-(ops.tpow(-x1.max()-1)+1)); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } } while (mod); } else { bool mod; do { mod = false; { ModEvent me = x1.lq(home,ops.fnroot(x0.max())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.gq(home,ops.fnroot(x0.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.le(home,ops.tpow(x1.max()+1)); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.gq(home,ops.tpow(x1.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } } while (mod); } return ES_OK; } template forceinline NrootPlusBnd::NrootPlusBnd(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus NrootPlusBnd::post(Home home, IntView x0, IntView x1, Ops ops) { if (minus) { GECODE_ME_CHECK(x0.lq(home,0)); GECODE_ME_CHECK(x1.lq(home,0)); } else { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); } (void) new (home) NrootPlusBnd(home,x0,x1,ops); return ES_OK; } template forceinline NrootPlusBnd::NrootPlusBnd(Space& home, NrootPlusBnd& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* NrootPlusBnd::copy(Space& home) { return new (home) NrootPlusBnd(home,*this); } template ExecStatus NrootPlusBnd::propagate(Space& home, const ModEventDelta&) { GECODE_ES_CHECK((prop_nroot_plus_bnd(home,x0,x1,ops))); return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Bounds consistent nth root * */ template forceinline ExecStatus prop_nroot_bnd(Space& home, IntView x0, IntView x1, const Ops& ops) { assert((x0.min() < 0) && (x0.max() > 0)); assert((x1.min() < 0) && (x1.max() > 0)); GECODE_ME_CHECK(x1.lq(home,ops.fnroot(x0.max()))); GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-x0.min()))); GECODE_ME_CHECK(x0.le(home,ops.tpow(x1.max()+1))); GECODE_ME_CHECK(x0.gq(home,ops.tpow(x1.min()-1)+1)); return ES_OK; } template forceinline NrootBnd::NrootBnd(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus NrootBnd::post(Home home, IntView x0, IntView x1, Ops ops) { if (static_cast(ops.exp()) >= sizeof(int) * CHAR_BIT) { // The integer limits allow only -2, -1, 0, 1 for x1 GECODE_ME_CHECK(x1.lq(home,1)); GECODE_ME_CHECK(x1.gq(home,-2)); // Just rewrite to values that can be handeled without overflow ops.exp(ops.even() ? 30 : 31); } if (ops.exp() == 0) { GECODE_ME_CHECK(x1.eq(home,1)); return ES_OK; } else if (ops.exp() == 1) { return Rel::EqBnd::post(home,x0,x1); } if (x0 == x1) { assert(ops.exp() > 1); GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -2)); return ES_OK; } // Limits values such that no overflow can occur GECODE_ME_CHECK(x1.lq(home,ops.fnroot(Limits::max))); GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-Limits::min))); if (ops.even()) { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); } if ((x0.min() >= 0) || (x1.min() >= 0)) return NrootPlusBnd::post(home,x0,x1,ops); if ((x0.max() <= 0) || (x1.max() <= 0)) return NrootPlusBnd::post(home,x0,x1,ops); assert((x0.min() < 0) && (x0.max() > 0)); assert((x1.min() < 0) && (x1.max() > 0)); GECODE_ES_CHECK(prop_nroot_bnd(home,x0,x1,ops)); (void) new (home) NrootBnd(home,x0,x1,ops); return ES_OK; } template forceinline NrootBnd::NrootBnd(Space& home, NrootBnd& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* NrootBnd::copy(Space& home) { return new (home) NrootBnd(home,*this); } template ExecStatus NrootBnd::propagate(Space& home, const ModEventDelta&) { assert(!ops.even()); if ((x0.min() >= 0) || (x1.min() >= 0)) GECODE_REWRITE(*this,(NrootPlusBnd::post(home(*this),x0,x1,ops))); if ((x0.max() <= 0) || (x1.max() <= 0)) GECODE_REWRITE(*this,(NrootPlusBnd::post(home(*this),x0,x1,ops))); GECODE_ES_CHECK(prop_nroot_bnd(home,x0,x1,ops)); return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this) : ES_NOFIX; } /* * Domain consistent nth root * */ /// Mapping ranges to powers template class RangesMapPow { protected: /// Power operations Ops ops; public: /// Initialize with operations \a o forceinline RangesMapPow(const Ops& o) : ops(o) {} /// Perform mapping of minimum forceinline int min(int x) const { return ops.tpow(x); } /// Perform mapping of maximum forceinline int max(int x) const { return ops.tpow(x+1)-1; } }; /// Mapping integer to n-th root template class RangesMapNroot { protected: /// Power operations Ops ops; public: /// Initialize with operations \a o forceinline RangesMapNroot(const Ops& o) : ops(o) {} /// Perform mapping of minimum forceinline int min(int x) const { return (x < 0) ? -ops.cnroot(-x) : ops.fnroot(x); } /// Perform mapping of maximum forceinline int max(int x) const { return (x < 0) ? -ops.cnroot(-x) : ops.fnroot(x); } }; template forceinline NrootPlusDom::NrootPlusDom(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus NrootPlusDom::post(Home home, IntView x0, IntView x1, Ops ops) { if (minus) { GECODE_ME_CHECK(x0.lq(home,0)); GECODE_ME_CHECK(x1.lq(home,0)); } else { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); } GECODE_ES_CHECK((prop_nroot_plus_bnd(home,x0,x1,ops))); (void) new (home) NrootPlusDom(home,x0,x1,ops); return ES_OK; } template forceinline NrootPlusDom::NrootPlusDom(Space& home, NrootPlusDom& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* NrootPlusDom::copy(Space& home) { return new (home) NrootPlusDom(home,*this); } template PropCost NrootPlusDom::cost(const Space&, const ModEventDelta& med) const { if (IntView::me(med) == ME_INT_VAL) return PropCost::unary(PropCost::LO); else if (IntView::me(med) == ME_INT_DOM) return PropCost::binary(PropCost::HI); else return PropCost::binary(PropCost::LO); } template ExecStatus NrootPlusDom::propagate(Space& home, const ModEventDelta& med) { if (IntView::me(med) != ME_INT_DOM) { GECODE_ES_CHECK((prop_nroot_plus_bnd(home,x0,x1,ops))); return x1.assigned() ? home.ES_SUBSUMED(*this) : home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM)); } { ViewRanges r(x0); RangesMapNroot rmn(ops); Iter::Ranges::Map,RangesMapNroot,false> m(r,rmn); GECODE_ME_CHECK(x1.inter_r(home,m,false)); } { ViewRanges r(x1); RangesMapPow rmp(ops); Iter::Ranges::Map,RangesMapPow,true> m(r,rmp); GECODE_ME_CHECK(x0.inter_r(home,m,false)); } return x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } template forceinline NrootDom::NrootDom(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus NrootDom::post(Home home, IntView x0, IntView x1, Ops ops) { if (static_cast(ops.exp()) >= sizeof(int) * CHAR_BIT) { // The integer limits allow only -2, -1, 0, 1 for x1 GECODE_ME_CHECK(x1.lq(home,1)); GECODE_ME_CHECK(x1.gq(home,-2)); // Just rewrite to values that can be handeled without overflow ops.exp(ops.even() ? 30 : 31); } if (ops.exp() == 0) { GECODE_ME_CHECK(x1.eq(home,1)); return ES_OK; } else if (ops.exp() == 1) { return Rel::EqDom::post(home,x0,x1); } if (x0 == x1) { assert(ops.exp() > 1); GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -2)); return ES_OK; } // Limits values such that no overflow can occur GECODE_ME_CHECK(x1.lq(home,ops.fnroot(Limits::max))); GECODE_ME_CHECK(x1.gq(home,-ops.cnroot(-Limits::min))); if (ops.even()) { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); } if ((x0.min() >= 0) || (x1.min() >= 0)) return NrootPlusDom::post(home,x0,x1,ops); if ((x0.max() <= 0) || (x1.max() <= 0)) return NrootPlusDom::post(home,x0,x1,ops); assert((x0.min() < 0) && (x0.max() > 0)); assert((x1.min() < 0) && (x1.max() > 0)); GECODE_ES_CHECK(prop_nroot_bnd(home,x0,x1,ops)); (void) new (home) NrootDom(home,x0,x1,ops); return ES_OK; } template forceinline NrootDom::NrootDom(Space& home, NrootDom& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* NrootDom::copy(Space& home) { return new (home) NrootDom(home,*this); } template PropCost NrootDom::cost(const Space&, const ModEventDelta& med) const { if (IntView::me(med) == ME_INT_VAL) return PropCost::unary(PropCost::LO); else if (IntView::me(med) == ME_INT_DOM) return PropCost::binary(PropCost::HI); else return PropCost::binary(PropCost::LO); } template ExecStatus NrootDom::propagate(Space& home, const ModEventDelta& med) { assert(!ops.even()); if ((x0.min() >= 0) || (x1.min() >= 0)) GECODE_REWRITE(*this,(NrootPlusDom::post(home(*this),x0,x1,ops))); if ((x0.max() <= 0) || (x1.max() <= 0)) GECODE_REWRITE(*this,(NrootPlusDom::post(home(*this),x0,x1,ops))); if (IntView::me(med) != ME_INT_DOM) { GECODE_ES_CHECK(prop_nroot_bnd(home,x0,x1,ops)); return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this) : home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM)); } { ViewRanges r(x0); RangesMapNroot rmn(ops); Iter::Ranges::Map,RangesMapNroot,false> m(r,rmn); GECODE_ME_CHECK(x1.inter_r(home,m,false)); } { ViewRanges r(x1); RangesMapPow rmp(ops); Iter::Ranges::Map,RangesMapPow,true> m(r,rmp); GECODE_ME_CHECK(x0.inter_r(home,m,false)); } return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } }}} // STATISTICS: int-prop