mult.cpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* 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 <gecode/int/arithmetic.hh>
namespace Gecode { namespace Int { namespace Arithmetic {
/*
* Bounds consistent multiplication
*
*/
Actor*
MultBnd::copy(Space& home) {
return new (home) MultBnd(home,*this);
}
ExecStatus
MultBnd::propagate(Space& home, const ModEventDelta&) {
if (pos(x0)) {
if (pos(x1) || pos(x2)) goto rewrite_ppp;
if (neg(x1) || neg(x2)) goto rewrite_pnn;
goto prop_pxx;
}
if (neg(x0)) {
if (neg(x1) || pos(x2)) goto rewrite_nnp;
if (pos(x1) || neg(x2)) goto rewrite_npn;
goto prop_nxx;
}
if (pos(x1)) {
if (pos(x2)) goto rewrite_ppp;
if (neg(x2)) goto rewrite_npn;
goto prop_xpx;
}
if (neg(x1)) {
if (pos(x2)) goto rewrite_nnp;
if (neg(x2)) goto rewrite_pnn;
goto prop_xnx;
}
assert(any(x0) && any(x1));
GECODE_ME_CHECK(x2.lq(home,std::max(mll(x0.max(),x1.max()),
mll(x0.min(),x1.min()))));
GECODE_ME_CHECK(x2.gq(home,std::min(mll(x0.min(),x1.max()),
mll(x0.max(),x1.min()))));
if (x0.assigned()) {
assert((x0.val() == 0) && (x2.val() == 0));
return home.ES_SUBSUMED(*this);
}
if (x1.assigned()) {
assert((x1.val() == 0) && (x2.val() == 0));
return home.ES_SUBSUMED(*this);
}
return ES_NOFIX;
prop_xpx:
std::swap(x0,x1);
prop_pxx:
assert(pos(x0) && any(x1) && any(x2));
GECODE_ME_CHECK(x2.lq(home,mll(x0.max(),x1.max())));
GECODE_ME_CHECK(x2.gq(home,mll(x0.max(),x1.min())));
if (pos(x2)) goto rewrite_ppp;
if (neg(x2)) goto rewrite_pnn;
GECODE_ME_CHECK(x1.lq(home,floor_div_xp(x2.max(),x0.min())));
GECODE_ME_CHECK(x1.gq(home,ceil_div_xp(x2.min(),x0.min())));
if (x0.assigned() && x1.assigned()) {
GECODE_ME_CHECK(x2.eq(home,mll(x0.val(),x1.val())));
return home.ES_SUBSUMED(*this);
}
return ES_NOFIX;
prop_xnx:
std::swap(x0,x1);
prop_nxx:
assert(neg(x0) && any(x1) && any(x2));
GECODE_ME_CHECK(x2.lq(home,mll(x0.min(),x1.min())));
GECODE_ME_CHECK(x2.gq(home,mll(x0.min(),x1.max())));
if (pos(x2)) goto rewrite_nnp;
if (neg(x2)) goto rewrite_npn;
GECODE_ME_CHECK(x1.lq(home,floor_div_xx(x2.min(),x0.max())));
GECODE_ME_CHECK(x1.gq(home,ceil_div_xx(x2.max(),x0.max())));
if (x0.assigned() && x1.assigned()) {
GECODE_ME_CHECK(x2.eq(home,mll(x0.val(),x1.val())));
return home.ES_SUBSUMED(*this);
}
return ES_NOFIX;
rewrite_ppp:
GECODE_REWRITE(*this,(MultPlusBnd<IntView,IntView,IntView>
::post(home(*this),x0,x1,x2)));
rewrite_nnp:
GECODE_REWRITE(*this,(MultPlusBnd<MinusView,MinusView,IntView>
::post(home(*this),MinusView(x0),MinusView(x1),x2)));
rewrite_pnn:
std::swap(x0,x1);
rewrite_npn:
GECODE_REWRITE(*this,(MultPlusBnd<MinusView,IntView,MinusView>
::post(home(*this),MinusView(x0),x1,MinusView(x2))));
}
ExecStatus
MultBnd::post(Home home, IntView x0, IntView x1, IntView x2) {
if (x0 == x1) {
SqrOps ops;
return PowBnd<SqrOps>::post(home,x0,x2,ops);
}
if (x0 == x2)
return MultZeroOne<IntView,PC_INT_BND>::post(home,x0,x1);
if (x1 == x2)
return MultZeroOne<IntView,PC_INT_BND>::post(home,x1,x0);
if (pos(x0)) {
if (pos(x1) || pos(x2)) goto post_ppp;
if (neg(x1) || neg(x2)) goto post_pnn;
} else if (neg(x0)) {
if (neg(x1) || pos(x2)) goto post_nnp;
if (pos(x1) || neg(x2)) goto post_npn;
} else if (pos(x1)) {
if (pos(x2)) goto post_ppp;
if (neg(x2)) goto post_npn;
} else if (neg(x1)) {
if (pos(x2)) goto post_nnp;
if (neg(x2)) goto post_pnn;
}
{
long long int a = mll(x0.min(),x1.min());
long long int b = mll(x0.min(),x1.max());
long long int c = mll(x0.max(),x1.min());
long long int d = mll(x0.max(),x1.max());
GECODE_ME_CHECK(x2.gq(home,std::min(std::min(a,b),std::min(c,d))));
GECODE_ME_CHECK(x2.lq(home,std::max(std::max(a,b),std::max(c,d))));
(void) new (home) MultBnd(home,x0,x1,x2);
}
return ES_OK;
post_ppp:
return MultPlusBnd<IntView,IntView,IntView>
::post(home,x0,x1,x2);
post_nnp:
return MultPlusBnd<MinusView,MinusView,IntView>
::post(home,MinusView(x0),MinusView(x1),x2);
post_pnn:
std::swap(x0,x1);
post_npn:
return MultPlusBnd<MinusView,IntView,MinusView>
::post(home,MinusView(x0),x1,MinusView(x2));
}
/*
* Domain consistent multiplication
*
*/
Actor*
MultDom::copy(Space& home) {
return new (home) MultDom(home,*this);
}
PropCost
MultDom::cost(const Space&, const ModEventDelta& med) const {
if (IntView::me(med) == ME_INT_DOM)
return PropCost::ternary(PropCost::HI);
else
return PropCost::ternary(PropCost::LO);
}
ExecStatus
MultDom::propagate(Space& home, const ModEventDelta& med) {
if (IntView::me(med) != ME_INT_DOM) {
if (pos(x0)) {
if (pos(x1) || pos(x2)) goto rewrite_ppp;
if (neg(x1) || neg(x2)) goto rewrite_pnn;
goto prop_pxx;
}
if (neg(x0)) {
if (neg(x1) || pos(x2)) goto rewrite_nnp;
if (pos(x1) || neg(x2)) goto rewrite_npn;
goto prop_nxx;
}
if (pos(x1)) {
if (pos(x2)) goto rewrite_ppp;
if (neg(x2)) goto rewrite_npn;
goto prop_xpx;
}
if (neg(x1)) {
if (pos(x2)) goto rewrite_nnp;
if (neg(x2)) goto rewrite_pnn;
goto prop_xnx;
}
assert(any(x0) && any(x1));
GECODE_ME_CHECK(x2.lq(home,std::max(mll(x0.max(),x1.max()),
mll(x0.min(),x1.min()))));
GECODE_ME_CHECK(x2.gq(home,std::min(mll(x0.min(),x1.max()),
mll(x0.max(),x1.min()))));
if (x0.assigned()) {
assert((x0.val() == 0) && (x2.val() == 0));
return home.ES_SUBSUMED(*this);
}
if (x1.assigned()) {
assert((x1.val() == 0) && (x2.val() == 0));
return home.ES_SUBSUMED(*this);
}
return home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM));
prop_xpx:
std::swap(x0,x1);
prop_pxx:
assert(pos(x0) && any(x1) && any(x2));
GECODE_ME_CHECK(x2.lq(home,mll(x0.max(),x1.max())));
GECODE_ME_CHECK(x2.gq(home,mll(x0.max(),x1.min())));
if (pos(x2)) goto rewrite_ppp;
if (neg(x2)) goto rewrite_pnn;
GECODE_ME_CHECK(x1.lq(home,floor_div_xp(x2.max(),x0.min())));
GECODE_ME_CHECK(x1.gq(home,ceil_div_xp(x2.min(),x0.min())));
if (x0.assigned() && x1.assigned()) {
GECODE_ME_CHECK(x2.eq(home,mll(x0.val(),x1.val())));
return home.ES_SUBSUMED(*this);
}
return home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM));
prop_xnx:
std::swap(x0,x1);
prop_nxx:
assert(neg(x0) && any(x1) && any(x2));
GECODE_ME_CHECK(x2.lq(home,mll(x0.min(),x1.min())));
GECODE_ME_CHECK(x2.gq(home,mll(x0.min(),x1.max())));
if (pos(x2)) goto rewrite_nnp;
if (neg(x2)) goto rewrite_npn;
GECODE_ME_CHECK(x1.lq(home,floor_div_xx(x2.min(),x0.max())));
GECODE_ME_CHECK(x1.gq(home,ceil_div_xx(x2.max(),x0.max())));
if (x0.assigned() && x1.assigned()) {
GECODE_ME_CHECK(x2.eq(home,mll(x0.val(),x1.val())));
return home.ES_SUBSUMED(*this);
}
return home.ES_NOFIX_PARTIAL(*this,IntView::med(ME_INT_DOM));
rewrite_ppp:
GECODE_REWRITE(*this,(MultPlusDom<IntView,IntView,IntView>
::post(home(*this),x0,x1,x2)));
rewrite_nnp:
GECODE_REWRITE(*this,(MultPlusDom<MinusView,MinusView,IntView>
::post(home(*this),
MinusView(x0),MinusView(x1),x2)));
rewrite_pnn:
std::swap(x0,x1);
rewrite_npn:
GECODE_REWRITE(*this,(MultPlusDom<MinusView,IntView,MinusView>
::post(home(*this),
MinusView(x0),x1,MinusView(x2))));
}
return prop_mult_dom<IntView>(home,*this,x0,x1,x2);
}
ExecStatus
MultDom::post(Home home, IntView x0, IntView x1, IntView x2) {
if (x0 == x1) {
SqrOps ops;
return PowDom<SqrOps>::post(home,x0,x2,ops);
}
if (x0 == x2)
return MultZeroOne<IntView,PC_INT_DOM>::post(home,x0,x1);
if (x1 == x2)
return MultZeroOne<IntView,PC_INT_DOM>::post(home,x1,x0);
if (pos(x0)) {
if (pos(x1) || pos(x2)) goto post_ppp;
if (neg(x1) || neg(x2)) goto post_pnn;
} else if (neg(x0)) {
if (neg(x1) || pos(x2)) goto post_nnp;
if (pos(x1) || neg(x2)) goto post_npn;
} else if (pos(x1)) {
if (pos(x2)) goto post_ppp;
if (neg(x2)) goto post_npn;
} else if (neg(x1)) {
if (pos(x2)) goto post_nnp;
if (neg(x2)) goto post_pnn;
}
{
long long int a = mll(x0.min(),x1.min());
long long int b = mll(x0.min(),x1.max());
long long int c = mll(x0.max(),x1.min());
long long int d = mll(x0.max(),x1.max());
GECODE_ME_CHECK(x2.gq(home,std::min(std::min(a,b),std::min(c,d))));
GECODE_ME_CHECK(x2.lq(home,std::max(std::max(a,b),std::max(c,d))));
(void) new (home) MultDom(home,x0,x1,x2);
}
return ES_OK;
post_ppp:
return MultPlusDom<IntView,IntView,IntView>
::post(home,x0,x1,x2);
post_nnp:
return MultPlusDom<MinusView,MinusView,IntView>
::post(home,MinusView(x0),MinusView(x1),x2);
post_pnn:
std::swap(x0,x1);
post_npn:
return MultPlusDom<MinusView,IntView,MinusView>
::post(home,MinusView(x0),x1,MinusView(x2));
}
}}}
// STATISTICS: int-prop
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::Arithmetic::MultBnd::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition mult.cpp:48
Gecode::Int::Arithmetic::MultBnd::MultBnd
MultBnd(Space &home, MultBnd &p)
Constructor for cloning p.
Definition mult.hpp:263
Gecode::Int::Arithmetic::MultBnd::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition mult.cpp:43
Gecode::Int::Arithmetic::MultBnd::post
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition mult.cpp:144
Gecode::Int::Arithmetic::MultDom::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition mult.cpp:197
Gecode::Int::Arithmetic::MultDom::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition mult.cpp:202
Gecode::Int::Arithmetic::MultDom::post
static ExecStatus post(Home home, IntView x0, IntView x1, IntView x2)
Post propagator .
Definition mult.cpp:311
Gecode::Int::Arithmetic::MultDom::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition mult.cpp:210
Gecode::Int::Arithmetic::MultDom::MultDom
MultDom(Space &home, MultDom &p)
Constructor for cloning p.
Definition mult.hpp:350
Gecode::Int::Arithmetic::MultPlusBnd
Bounds consistent positive multiplication propagator.
Definition arithmetic.hh:649
Gecode::Int::Arithmetic::MultPlusDom
Domain consistent positive multiplication propagator.
Definition arithmetic.hh:704
Gecode::Int::Arithmetic::MultZeroOne::post
static ExecStatus post(Home home, View x0, View x1)
Post propagator .
Definition mult.hpp:109
Gecode::Int::Arithmetic::PowBnd::post
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition pow.hpp:149
Gecode::Int::Arithmetic::PowDom::post
static ExecStatus post(Home home, IntView x0, IntView x1, Ops ops)
Post propagator.
Definition pow.hpp:386
Gecode::Int::Arithmetic::SqrOps
Operations for square and square-root propagators.
Definition arithmetic.hh:302
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::IntView::med
int med(void) const
Return median of domain (greatest element not greater than the median).
Definition int.hpp:66
Gecode::Int::MinusView
Minus integer view.
Definition view.hpp:282
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
Gecode::PropCost::ternary
static PropCost ternary(PropCost::Mod m)
Three variables for modifier pcm.
Definition core.hpp:4805
Gecode::PropCost::LO
@ LO
Cheap.
Definition core.hpp:513
Gecode::PropCost::HI
@ HI
Expensive.
Definition core.hpp:514
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Propagator::med
ModEventDelta med
A set of modification events (used during propagation).
Definition core.hpp:1075
Gecode::TernaryPropagator< IntView, PC_INT_BND >::x2
IntView x2
Definition pattern.hpp:116
Gecode::TernaryPropagator< IntView, PC_INT_BND >::x1
IntView x1
Definition pattern.hpp:116
Gecode::TernaryPropagator< IntView, PC_INT_BND >::x0
IntView x0
Definition pattern.hpp:116
Gecode::VarImpView< IntVar >::me
static ModEvent me(const ModEventDelta &med)
Definition view.hpp:552
Gecode::Space::ES_NOFIX_PARTIAL
ExecStatus ES_NOFIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has not computed partial fixpoint
Definition core.hpp:3576
Gecode::Space::ES_SUBSUMED
ExecStatus ES_SUBSUMED(Propagator &p)
Definition core.hpp:3563
Gecode::ModEventDelta
int ModEventDelta
Modification event deltas.
Definition core.hpp:89
GECODE_ME_CHECK
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
GECODE_REWRITE
#define GECODE_REWRITE(prop, post)
Rewrite propagator by executing post function.
Definition macros.hpp:116
arithmetic.hh
Gecode::Int::Arithmetic
Numerical (arithmetic) propagators.
Gecode::Int::Arithmetic::pos
bool pos(const View &x)
Test whether x is postive.
Definition mult.hpp:70
Gecode::Int::Arithmetic::any
bool any(const View &x)
Test whether x is neither positive nor negative.
Definition mult.hpp:82
Gecode::Int::Arithmetic::mll
long long int mll(long long int x, long long int y)
Multiply x and \y.
Definition mult.hpp:48
Gecode::Int::Arithmetic::prop_mult_dom
ExecStatus prop_mult_dom(Space &home, Propagator &p, View x0, View x1, View x2)
Definition mult.hpp:272
Gecode::Int::Arithmetic::neg
bool neg(const View &x)
Test whether x is negative.
Definition mult.hpp:76
Gecode::Int
Finite domain integers.
Gecode::Int::ceil_div_xp
IntType ceil_div_xp(IntType x, IntType y)
Compute where y is non-negative.
Definition div.hpp:69
Gecode::Int::floor_div_xp
IntType floor_div_xp(IntType x, IntType y)
Compute where y is non-negative.
Definition div.hpp:75
Gecode::Int::floor_div_xx
IntType floor_div_xx(IntType x, IntType y)
Compute .
Definition div.hpp:87
Gecode::Int::ME_INT_DOM
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition var-type.hpp:72
Gecode::Int::ceil_div_xx
IntType ceil_div_xx(IntType x, IntType y)
Compute .
Definition div.hpp:82
Gecode
Gecode toplevel namespace
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475