/* -*- 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 power * */ template inline ExecStatus prop_pow_plus_bnd(Space& home, VA x0, VB x1, const Ops& ops) { bool mod; do { mod = false; { ModEvent me = x0.lq(home,ops.fnroot(x1.max())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x0.gq(home,ops.cnroot(x1.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.lq(home,ops.pow(x0.max())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } { ModEvent me = x1.gq(home,ops.pow(x0.min())); if (me_failed(me)) return ES_FAILED; mod |= me_modified(me); } } while (mod); return ES_OK; } template forceinline PowPlusBnd::PowPlusBnd(Home home, VA x0, VB x1, const Ops& o) : MixBinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus PowPlusBnd::post(Home home, VA x0, VB x1, Ops ops) { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); GECODE_ES_CHECK(prop_pow_plus_bnd(home,x0,x1,ops)); if (!x0.assigned()) { assert(!x1.assigned()); (void) new (home) PowPlusBnd(home,x0,x1,ops); } return ES_OK; } template forceinline PowPlusBnd::PowPlusBnd(Space& home, PowPlusBnd& p) : MixBinaryPropagator(home,p), ops(p.ops) {} template Actor* PowPlusBnd::copy(Space& home) { return new (home) PowPlusBnd(home,*this); } template ExecStatus PowPlusBnd::propagate(Space& home, const ModEventDelta&) { GECODE_ES_CHECK(prop_pow_plus_bnd(home,x0,x1,ops)); return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Bounds consistent power * */ template inline ExecStatus prop_pow_bnd(Space& home, IntView x0, IntView x1, const Ops& ops) { assert((x0.min() < 0) && (0 < x0.max())); if (ops.even()) { assert(x1.min() >= 0); int u = ops.fnroot(x1.max()); GECODE_ME_CHECK(x0.lq(home,u)); GECODE_ME_CHECK(x0.gq(home,-u)); GECODE_ME_CHECK(x1.lq(home,std::max(ops.pow(x0.max()), ops.pow(-x0.min())))); } else { assert((x1.min() < 0) && (0 < x1.max())); GECODE_ME_CHECK(x0.lq(home,ops.fnroot(x1.max()))); GECODE_ME_CHECK(x0.gq(home,-ops.fnroot(-x1.min()))); GECODE_ME_CHECK(x1.lq(home,ops.pow(x0.max()))); GECODE_ME_CHECK(x1.gq(home,ops.pow(x0.min()))); } return ES_OK; } template forceinline PowBnd::PowBnd(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template inline ExecStatus PowBnd::post(Home home, IntView x0, IntView x1, Ops ops) { if (static_cast(ops.exp()) >= sizeof(int) * CHAR_BIT) { // The integer limits allow only -1, 0, 1 for x0 GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,-1)); // Just rewrite to values that can be handeled without overflow ops.exp(ops.even() ? 2 : 1); } 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() != 0); GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -1)); return ES_OK; } // Limits values such that no overflow can occur assert(Limits::max == -Limits::min); { int l = ops.fnroot(Limits::max); GECODE_ME_CHECK(x0.lq(home,l)); GECODE_ME_CHECK(x0.gq(home,-l)); } if ((x0.min() >= 0) || ((x1.min() >= 0) && !ops.even())) return PowPlusBnd::post(home,x0,x1,ops); if (ops.even() && (x0.max() <= 0)) return PowPlusBnd ::post(home,MinusView(x0),x1,ops); if (!ops.even() && ((x0.max() <= 0) || (x1.max() <= 0))) return PowPlusBnd ::post(home,MinusView(x0),MinusView(x1),ops); if (ops.even()) GECODE_ME_CHECK(x1.gq(home,0)); assert((x0.min() < 0) && (x0.max() > 0)); if (ops.even()) { GECODE_ME_CHECK(x1.lq(home,std::max(ops.pow(x0.min()), ops.pow(x0.max())))); } else { GECODE_ME_CHECK(x1.lq(home,ops.pow(x0.max()))); GECODE_ME_CHECK(x1.gq(home,ops.pow(x0.min()))); } (void) new (home) PowBnd(home,x0,x1,ops); return ES_OK; } template forceinline PowBnd::PowBnd(Space& home, PowBnd& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* PowBnd::copy(Space& home) { return new (home) PowBnd(home,*this); } template ExecStatus PowBnd::propagate(Space& home, const ModEventDelta&) { if ((x0.min() >= 0) || ((x1.min() >= 0) && !ops.even())) GECODE_REWRITE(*this,(PowPlusBnd ::post(home(*this),x0,x1,ops))); if (ops.even() && (x0.max() <= 0)) GECODE_REWRITE(*this,(PowPlusBnd ::post(home(*this),MinusView(x0),x1,ops))); if (!ops.even() && ((x0.max() <= 0) || (x1.max() <= 0))) GECODE_REWRITE(*this,(PowPlusBnd ::post(home(*this),MinusView(x0), MinusView(x1),ops))); GECODE_ES_CHECK(prop_pow_bnd(home,x0,x1,ops)); if (x0.assigned() && x1.assigned()) return (ops.pow(x0.val()) == x1.val()) ? home.ES_SUBSUMED(*this) : ES_FAILED; return ES_NOFIX; } /* * Value mappings for power and nroot * */ /// Mapping integer to power template class ValuesMapPow { protected: /// Operations Ops ops; public: /// Initialize with operations \a o forceinline ValuesMapPow(const Ops& o) : ops(o) {} /// Perform mapping forceinline int val(int x) const { return ops.pow(x); } }; /// Mapping integer (must be an n-th power) to n-th root template class ValuesMapNroot { protected: /// Operations Ops ops; public: /// Initialize with operations \a o forceinline ValuesMapNroot(const Ops& o) : ops(o) {} /// Perform mapping forceinline int val(int x) const { return ops.fnroot(x); } }; /// Mapping integer (must be an n-th power) to n-th root (signed) template class ValuesMapNrootSigned { protected: /// Operations Ops ops; public: /// Initialize with operations \a o forceinline ValuesMapNrootSigned(const Ops& o) : ops(o) {} /// Perform mapping forceinline int val(int x) const { if (x < 0) return -ops.fnroot(-x); else return ops.fnroot(x); } }; /* * Positive domain consistent power * */ template forceinline PowPlusDom::PowPlusDom(Home home, VA x0, VB x1, const Ops& o) : MixBinaryPropagator(home,x0,x1), ops(o) {} template forceinline ExecStatus PowPlusDom::post(Home home, VA x0, VB x1, Ops ops) { GECODE_ME_CHECK(x0.gq(home,0)); GECODE_ME_CHECK(x1.gq(home,0)); GECODE_ES_CHECK(prop_pow_plus_bnd(home,x0,x1,ops)); if (!x0.assigned()) { assert(!x1.assigned()); (void) new (home) PowPlusDom(home,x0,x1,ops); } return ES_OK; } template forceinline PowPlusDom::PowPlusDom(Space& home, PowPlusDom& p) : MixBinaryPropagator(home,p), ops(p.ops) {} template Actor* PowPlusDom::copy(Space& home) { return new (home) PowPlusDom(home,*this); } template PropCost PowPlusDom::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 PowPlusDom::propagate(Space& home, const ModEventDelta& med) { if (VA::me(med) != ME_INT_DOM) { GECODE_ES_CHECK(prop_pow_plus_bnd(home,x0,x1,ops)); return x0.assigned() ? home.ES_SUBSUMED(*this) : home.ES_NOFIX_PARTIAL(*this,VA::med(ME_INT_DOM)); } { ViewValues v0(x0); ValuesMapPow vmp(ops); Iter::Values::Map,ValuesMapPow > s0(v0,vmp); GECODE_ME_CHECK(x1.inter_v(home,s0,false)); } { ViewValues v1(x1); ValuesMapNroot vmn(ops); Iter::Values::Map,ValuesMapNroot > s1(v1,vmn); GECODE_ME_CHECK(x0.inter_v(home,s1,false)); } return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } /* * Domain consistent power * */ template forceinline PowDom::PowDom(Home home, IntView x0, IntView x1, const Ops& o) : BinaryPropagator(home,x0,x1), ops(o) {} template inline ExecStatus PowDom::post(Home home, IntView x0, IntView x1, Ops ops) { if (static_cast(ops.exp()) >= sizeof(int) * CHAR_BIT) { // The integer limits allow only -1, 0, 1 for x0 GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,-1)); // Just rewrite to values that can be handeled without overflow ops.exp(ops.even() ? 2 : 1); } 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() != 0); GECODE_ME_CHECK(x0.lq(home,1)); GECODE_ME_CHECK(x0.gq(home,ops.even() ? 0 : -1)); return ES_OK; } // Limits values such that no overflow can occur assert(Limits::max == -Limits::min); { int l = ops.fnroot(Limits::max); GECODE_ME_CHECK(x0.lq(home,l)); GECODE_ME_CHECK(x0.gq(home,-l)); } if ((x0.min() >= 0) || ((x1.min() >= 0) && !ops.even())) return PowPlusDom::post(home,x0,x1,ops); if (ops.even() && (x0.max() <= 0)) return PowPlusDom ::post(home,MinusView(x0),x1,ops); if (!ops.even() && ((x0.max() <= 0) || (x1.max() <= 0))) return PowPlusDom ::post(home,MinusView(x0),MinusView(x1),ops); if (ops.even()) GECODE_ME_CHECK(x1.gq(home,0)); assert((x0.min() < 0) && (x0.max() > 0)); if (ops.even()) { GECODE_ME_CHECK(x1.lq(home,std::max(ops.pow(x0.min()), ops.pow(x0.max())))); } else { GECODE_ME_CHECK(x1.lq(home,ops.pow(x0.max()))); GECODE_ME_CHECK(x1.gq(home,ops.pow(x0.min()))); } (void) new (home) PowDom(home,x0,x1,ops); return ES_OK; } template forceinline PowDom::PowDom(Space& home, PowDom& p) : BinaryPropagator(home,p), ops(p.ops) {} template Actor* PowDom::copy(Space& home) { return new (home) PowDom(home,*this); } template PropCost PowDom::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 PowDom::propagate(Space& home, const ModEventDelta& med) { if ((x0.min() >= 0) || ((x1.min() >= 0) && !ops.even())) GECODE_REWRITE(*this,(PowPlusDom ::post(home(*this),x0,x1,ops))); if (ops.even() && (x0.max() <= 0)) GECODE_REWRITE(*this,(PowPlusDom ::post(home(*this),MinusView(x0),x1,ops))); if (!ops.even() && ((x0.max() <= 0) || (x1.max() <= 0))) GECODE_REWRITE(*this,(PowPlusDom ::post(home(*this),MinusView(x0), MinusView(x1),ops))); if (IntView::me(med) != ME_INT_DOM) { GECODE_ES_CHECK(prop_pow_bnd(home,x0,x1,ops)); if (x0.assigned() && x1.assigned()) return (ops.pow(x0.val()) == x1.val()) ? home.ES_SUBSUMED(*this) : ES_FAILED; return home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM)); } Region r; if (ops.even()) { ViewValues i(x0), j(x0); using namespace Iter::Values; Positive > pos(i); Negative > neg(j); Minus m(r,neg); ValuesMapPow vmp(ops); Map >,ValuesMapPow,true> sp(pos,vmp); Map,true> sm(m,vmp); Union >,ValuesMapPow,true>, Map,true> > u(sp,sm); GECODE_ME_CHECK(x1.inter_v(home,u,false)); } else { ViewValues v0(x0); ValuesMapPow vmp(ops); Iter::Values::Map,ValuesMapPow > s0(v0,vmp); GECODE_ME_CHECK(x1.inter_v(home,s0,false)); } if (ops.even()) { ViewValues i(x1), j(x1); using namespace Iter::Values; ValuesMapNroot vmn(ops); Map,ValuesMapNroot,true> si(i,vmn), sj(j,vmn); Minus mi(r,si); Union,ValuesMapNroot,true> > u(mi,sj); GECODE_ME_CHECK(x0.inter_v(home,u,false)); } else { ViewValues v1(x1); ValuesMapNrootSigned vmn(ops); Iter::Values::Map,ValuesMapNrootSigned > s1(v1,vmn); GECODE_ME_CHECK(x0.inter_v(home,s1,false)); } return x0.assigned() ? home.ES_SUBSUMED(*this) : ES_FIX; } }}} // STATISTICS: int-prop