mult.hpp
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 <cmath>
#include <climits>
#include <gecode/int/div.hh>
#include <gecode/int/support-values.hh>
namespace Gecode { namespace Int { namespace Arithmetic {
/*
* Arithmetic help functions
*
*/
forceinline long long int
mll(long long int x, long long int y) {
return x*y;
}
forceinline long long int
ll(int x) {
return static_cast<long long int>(x);
}
forceinline long long int
ill(int x) {
return static_cast<long long int>(x) + 1;
}
forceinline long long int
dll(int x) {
return static_cast<long long int>(x) - 1;
}
template<class View>
forceinline bool
pos(const View& x) {
return x.min() > 0;
}
template<class View>
forceinline bool
neg(const View& x) {
return x.max() < 0;
}
template<class View>
forceinline bool
any(const View& x) {
return (x.min() <= 0) && (x.max() >= 0);
}
/*
* Propagator for x * y = x
*
*/
template<class View, PropCond pc>
forceinline
MultZeroOne<View,pc>::MultZeroOne(Home home, View x0, View x1)
: BinaryPropagator<View,pc>(home,x0,x1) {}
template<class View, PropCond pc>
forceinline RelTest
MultZeroOne<View,pc>::equal(View x, int n) {
if (pc == PC_INT_DOM) {
return rtest_eq_dom(x,n);
} else {
return rtest_eq_bnd(x,n);
}
}
template<class View, PropCond pc>
forceinline ExecStatus
MultZeroOne<View,pc>::post(Home home, View x0, View x1) {
switch (equal(x0,0)) {
case RT_FALSE:
GECODE_ME_CHECK(x1.eq(home,1));
break;
case RT_TRUE:
break;
case RT_MAYBE:
switch (equal(x1,1)) {
case RT_FALSE:
GECODE_ME_CHECK(x0.eq(home,0));
break;
case RT_TRUE:
break;
case RT_MAYBE:
(void) new (home) MultZeroOne<View,pc>(home,x0,x1);
break;
default: GECODE_NEVER;
}
break;
default: GECODE_NEVER;
}
return ES_OK;
}
template<class View, PropCond pc>
forceinline
MultZeroOne<View,pc>::MultZeroOne(Space& home, MultZeroOne<View,pc>& p)
: BinaryPropagator<View,pc>(home,p) {}
template<class View, PropCond pc>
Actor*
MultZeroOne<View,pc>::copy(Space& home) {
return new (home) MultZeroOne<View,pc>(home,*this);
}
template<class View, PropCond pc>
ExecStatus
MultZeroOne<View,pc>::propagate(Space& home, const ModEventDelta&) {
switch (equal(x0,0)) {
case RT_FALSE:
GECODE_ME_CHECK(x1.eq(home,1));
break;
case RT_TRUE:
break;
case RT_MAYBE:
switch (equal(x1,1)) {
case RT_FALSE:
GECODE_ME_CHECK(x0.eq(home,0));
break;
case RT_TRUE:
break;
case RT_MAYBE:
return ES_FIX;
default: GECODE_NEVER;
}
break;
default: GECODE_NEVER;
}
return home.ES_SUBSUMED(*this);
}
/*
* Positive bounds consistent multiplication
*
*/
template<class VA, class VB, class VC>
forceinline ExecStatus
prop_mult_plus_bnd(Space& home, Propagator& p, VA x0, VB x1, VC x2) {
assert(pos(x0) && pos(x1) && pos(x2));
bool mod;
do {
mod = false;
{
ModEvent me = x2.lq(home,mll(x0.max(),x1.max()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
{
ModEvent me = x2.gq(home,mll(x0.min(),x1.min()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
{
ModEvent me = x0.lq(home,floor_div_pp(x2.max(),x1.min()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
{
ModEvent me = x0.gq(home,ceil_div_pp(x2.min(),x1.max()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
{
ModEvent me = x1.lq(home,floor_div_pp(x2.max(),x0.min()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
{
ModEvent me = x1.gq(home,ceil_div_pp(x2.min(),x0.max()));
if (me_failed(me)) return ES_FAILED;
mod |= me_modified(me);
}
} while (mod);
return x0.assigned() && x1.assigned() ?
home.ES_SUBSUMED(p) : ES_FIX;
}
template<class VA, class VB, class VC>
forceinline
MultPlusBnd<VA,VB,VC>::MultPlusBnd(Home home, VA x0, VB x1, VC x2)
: MixTernaryPropagator<VA,PC_INT_BND,VB,PC_INT_BND,VC,PC_INT_BND>
(home,x0,x1,x2) {}
template<class VA, class VB, class VC>
forceinline
MultPlusBnd<VA,VB,VC>::MultPlusBnd(Space& home, MultPlusBnd<VA,VB,VC>& p)
: MixTernaryPropagator<VA,PC_INT_BND,VB,PC_INT_BND,VC,PC_INT_BND>
(home,p) {}
template<class VA, class VB, class VC>
Actor*
MultPlusBnd<VA,VB,VC>::copy(Space& home) {
return new (home) MultPlusBnd<VA,VB,VC>(home,*this);
}
template<class VA, class VB, class VC>
ExecStatus
MultPlusBnd<VA,VB,VC>::propagate(Space& home, const ModEventDelta&) {
return prop_mult_plus_bnd<VA,VB,VC>(home,*this,x0,x1,x2);
}
template<class VA, class VB, class VC>
forceinline ExecStatus
MultPlusBnd<VA,VB,VC>::post(Home home, VA x0, VB x1, VC x2) {
GECODE_ME_CHECK(x0.gr(home,0));
GECODE_ME_CHECK(x1.gr(home,0));
GECODE_ME_CHECK(x2.gq(home,mll(x0.min(),x1.min())));
GECODE_ME_CHECK(x2.lq(home,mll(x0.max(),x1.max())));
(void) new (home) MultPlusBnd<VA,VB,VC>(home,x0,x1,x2);
return ES_OK;
}
/*
* Bounds consistent multiplication
*
*/
forceinline
MultBnd::MultBnd(Home home, IntView x0, IntView x1, IntView x2)
: TernaryPropagator<IntView,PC_INT_BND>(home,x0,x1,x2) {}
forceinline
MultBnd::MultBnd(Space& home, MultBnd& p)
: TernaryPropagator<IntView,PC_INT_BND>(home,p) {}
/*
* Positive domain consistent multiplication
*
*/
template<class View>
forceinline ExecStatus
prop_mult_dom(Space& home, Propagator& p, View x0, View x1, View x2) {
Region r;
SupportValues<View,Region> s0(r,x0), s1(r,x1), s2(r,x2);
while (s0()) {
while (s1()) {
if (s2.support(mll(s0.val(),s1.val()))) {
s0.support(); s1.support();
}
++s1;
}
s1.reset(); ++s0;
}
GECODE_ME_CHECK(s0.tell(home));
GECODE_ME_CHECK(s1.tell(home));
GECODE_ME_CHECK(s2.tell(home));
return x0.assigned() && x1.assigned() ? home.ES_SUBSUMED(p) : ES_FIX;
}
template<class VA, class VB, class VC>
forceinline
MultPlusDom<VA,VB,VC>::MultPlusDom(Home home, VA x0, VB x1, VC x2)
: MixTernaryPropagator<VA,PC_INT_DOM,VB,PC_INT_DOM,VC,PC_INT_DOM>
(home,x0,x1,x2) {}
template<class VA, class VB, class VC>
forceinline
MultPlusDom<VA,VB,VC>::MultPlusDom(Space& home, MultPlusDom<VA,VB,VC>& p)
: MixTernaryPropagator<VA,PC_INT_DOM,VB,PC_INT_DOM,VC,PC_INT_DOM>
(home,p) {}
template<class VA, class VB, class VC>
Actor*
MultPlusDom<VA,VB,VC>::copy(Space& home) {
return new (home) MultPlusDom<VA,VB,VC>(home,*this);
}
template<class VA, class VB, class VC>
PropCost
MultPlusDom<VA,VB,VC>::cost(const Space&,
const ModEventDelta& med) const {
if (VA::me(med) == ME_INT_DOM)
return PropCost::ternary(PropCost::HI);
else
return PropCost::ternary(PropCost::LO);
}
template<class VA, class VB, class VC>
ExecStatus
MultPlusDom<VA,VB,VC>::propagate(Space& home, const ModEventDelta& med) {
if (VA::me(med) != ME_INT_DOM) {
GECODE_ES_CHECK((prop_mult_plus_bnd<VA,VB,VC>(home,*this,x0,x1,x2)));
return home.ES_FIX_PARTIAL(*this,VA::med(ME_INT_DOM));
}
IntView y0(x0.varimp()), y1(x1.varimp()), y2(x2.varimp());
return prop_mult_dom<IntView>(home,*this,y0,y1,y2);
}
template<class VA, class VB, class VC>
forceinline ExecStatus
MultPlusDom<VA,VB,VC>::post(Home home, VA x0, VB x1, VC x2) {
GECODE_ME_CHECK(x0.gr(home,0));
GECODE_ME_CHECK(x1.gr(home,0));
GECODE_ME_CHECK(x2.gq(home,mll(x0.min(),x1.min())));
GECODE_ME_CHECK(x2.lq(home,mll(x0.max(),x1.max())));
(void) new (home) MultPlusDom<VA,VB,VC>(home,x0,x1,x2);
return ES_OK;
}
/*
* Domain consistent multiplication
*
*/
forceinline
MultDom::MultDom(Home home, IntView x0, IntView x1, IntView x2)
: TernaryPropagator<IntView,PC_INT_DOM>(home,x0,x1,x2) {}
forceinline
MultDom::MultDom(Space& home, MultDom& p)
: TernaryPropagator<IntView,PC_INT_DOM>(home,p) {}
}}}
// STATISTICS: int-prop
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::BinaryPropagator::x0
View x0
Two views.
Definition pattern.hpp:87
Gecode::BinaryPropagator::BinaryPropagator
BinaryPropagator(Space &home, BinaryPropagator &p)
Constructor for cloning p.
Gecode::BinaryPropagator::x1
View x1
Definition pattern.hpp:87
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::Arithmetic::MultBnd::MultBnd
MultBnd(Space &home, MultBnd &p)
Constructor for cloning p.
Definition mult.hpp:263
Gecode::Int::Arithmetic::MultDom::MultDom
MultDom(Space &home, MultDom &p)
Constructor for cloning p.
Definition mult.hpp:350
Gecode::Int::Arithmetic::MultPlusBnd::post
static ExecStatus post(Home home, VA x0, VB x1, VC x2)
Post propagator .
Definition mult.hpp:244
Gecode::Int::Arithmetic::MultPlusBnd::MultPlusBnd
MultPlusBnd(Home home, VA x0, VB x1, VC x2)
Constructor for posting.
Definition mult.hpp:220
Gecode::Int::Arithmetic::MultPlusBnd::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition mult.hpp:232
Gecode::Int::Arithmetic::MultPlusBnd::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition mult.hpp:238
Gecode::Int::Arithmetic::MultPlusDom::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition mult.hpp:304
Gecode::Int::Arithmetic::MultPlusDom::post
static ExecStatus post(Home home, VA x0, VB x1, VC x2)
Post propagator .
Definition mult.hpp:331
Gecode::Int::Arithmetic::MultPlusDom::MultPlusDom
MultPlusDom(Home home, VA x0, VB x1, VC x2)
Constructor for posting.
Definition mult.hpp:292
Gecode::Int::Arithmetic::MultPlusDom::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition mult.hpp:310
Gecode::Int::Arithmetic::MultPlusDom::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition mult.hpp:320
Gecode::Int::Arithmetic::MultZeroOne::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition mult.hpp:141
Gecode::Int::Arithmetic::MultZeroOne::MultZeroOne
MultZeroOne(Space &home, MultZeroOne< View, pc > &p)
Constructor for cloning p.
Definition mult.hpp:136
Gecode::Int::Arithmetic::MultZeroOne::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition mult.hpp:147
Gecode::Int::Arithmetic::MultZeroOne::post
static ExecStatus post(Home home, View x0, View x1)
Post propagator .
Definition mult.hpp:109
Gecode::Int::Arithmetic::MultZeroOne::equal
static RelTest equal(View x, int n)
Test whether x is equal to n.
Definition mult.hpp:99
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::SupportValues
Support value iterator and recorder
Definition support-values.hh:48
Gecode::Int::SupportValues::support
void support(void)
Mark current (iterator) value as supported.
Definition support-values.hpp:94
Gecode::Int::SupportValues::tell
ModEvent tell(Space &home)
Remove all unsupported values.
Definition support-values.hpp:175
Gecode::MixTernaryPropagator< VA, PC_INT_BND, VB, PC_INT_BND, VC, PC_INT_BND >::x0
VA x0
Definition pattern.hpp:240
Gecode::MixTernaryPropagator< VA, PC_INT_BND, VB, PC_INT_BND, VC, PC_INT_BND >::x1
VB x1
Definition pattern.hpp:242
Gecode::MixTernaryPropagator< VA, PC_INT_BND, VB, PC_INT_BND, VC, PC_INT_BND >::x2
VC x2
Definition pattern.hpp:244
Gecode::MixTernaryPropagator< VA, PC_INT_BND, VB, PC_INT_BND, VC, PC_INT_BND >::MixTernaryPropagator
MixTernaryPropagator(Space &home, MixTernaryPropagator &p)
Definition pattern.hpp:661
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
Base-class for propagators.
Definition core.hpp:1064
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::Region
Handle to region.
Definition region.hpp:55
Gecode::Space
Computation spaces.
Definition core.hpp:1742
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::TernaryPropagator< IntView, PC_INT_BND >::TernaryPropagator
TernaryPropagator(Space &home, TernaryPropagator &p)
Gecode::View
div.hh
Gecode::Space::ES_FIX_PARTIAL
ExecStatus ES_FIX_PARTIAL(Propagator &p, const ModEventDelta &med)
Propagator p has computed partial fixpoint
Definition core.hpp:3569
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::me_failed
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
GECODE_ES_CHECK
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
Gecode::me_modified
bool me_modified(ModEvent me)
Check whether modification event me describes variable modification.
Definition modevent.hpp:59
Gecode::Int::Arithmetic
Numerical (arithmetic) propagators.
Gecode::Int::Arithmetic::dll
long long int dll(int x)
Decrement x by one.
Definition mult.hpp:63
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_plus_bnd
ExecStatus prop_mult_plus_bnd(Space &home, Propagator &p, VA x0, VB x1, VC x2)
Definition mult.hpp:178
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::ill
long long int ill(int x)
Increment x by one.
Definition mult.hpp:58
Gecode::Int::Arithmetic::neg
bool neg(const View &x)
Test whether x is negative.
Definition mult.hpp:76
Gecode::Int::Arithmetic::ll
long long int ll(int x)
Cast x into a long long int.
Definition mult.hpp:53
Gecode::Int
Finite domain integers.
Gecode::Int::ceil_div_pp
IntType ceil_div_pp(IntType x, IntType y)
Compute where x and y are non-negative.
Definition div.hpp:38
Gecode::Int::rtest_eq_dom
RelTest rtest_eq_dom(VX x, VY y)
Test whether views x and y are equal (use full domain information).
Definition rel-test.hpp:65
Gecode::Int::PC_INT_BND
const Gecode::PropCond PC_INT_BND
Propagate when minimum or maximum of a view changes.
Definition var-type.hpp:91
Gecode::Int::rtest_eq_bnd
RelTest rtest_eq_bnd(VX x, VY y)
Test whether views x and y are equal (use bounds information).
Definition rel-test.hpp:43
Gecode::Int::PC_INT_DOM
const Gecode::PropCond PC_INT_DOM
Propagate when domain changes.
Definition var-type.hpp:100
Gecode::Int::ME_INT_DOM
const Gecode::ModEvent ME_INT_DOM
Domain operation has changed the domain.
Definition var-type.hpp:72
Gecode::Int::floor_div_pp
IntType floor_div_pp(IntType x, IntType y)
Compute where x and y are non-negative.
Definition div.hpp:49
Gecode::Int::RelTest
RelTest
Result of testing relation.
Definition view.hpp:1734
Gecode::Int::RT_TRUE
@ RT_TRUE
Relation does hold.
Definition view.hpp:1737
Gecode::Int::RT_MAYBE
@ RT_MAYBE
Relation may hold or not.
Definition view.hpp:1736
Gecode::Int::RT_FALSE
@ RT_FALSE
Relation does not hold.
Definition view.hpp:1735
Gecode
Gecode toplevel namespace
Gecode::mod
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition arithmetic.cpp:360
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::ES_FIX
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
Gecode::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
support-values.hh
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56