view.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>
*
* Contributing authors:
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christian Schulte, 2004
* Guido Tack, 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 <algorithm>
namespace Gecode { namespace Int { namespace Element {
template<class VA, class VC>
class RelTestBnd {
public:
RelTest operator ()(VA,VC);
};
template<class VA>
class RelTestBnd<VA,ConstIntView> {
public:
RelTest operator ()(VA,ConstIntView);
};
template<class VA, class VC>
class RelTestDom {
public:
RelTest operator ()(VA,VC);
};
template<class VA>
class RelTestDom<VA,ConstIntView> {
public:
RelTest operator ()(VA,ConstIntView);
};
template<class VA, class VC>
forceinline RelTest
RelTestBnd<VA,VC>::operator ()(VA x, VC y) {
return rtest_eq_bnd(x,y);
}
template<class VA>
forceinline RelTest
RelTestBnd<VA,ConstIntView>::operator ()(VA x, ConstIntView y) {
return rtest_eq_bnd(x,y.val());
}
template<class VA, class VC>
forceinline RelTest
RelTestDom<VA,VC>::operator ()(VA x, VC y) {
return rtest_eq_dom(x,y);
}
template<class VA>
forceinline RelTest
RelTestDom<VA,ConstIntView>::operator ()(VA x, ConstIntView y) {
return rtest_eq_dom(x,y.val());
}
/*
* Base class
*
*/
template<class VA, class VB, class VC, PropCond pc_ac>
View<VA,VB,VC,pc_ac>::View(Home home, IdxViewArray<VA>& iv0,
VB y0, VC y1)
: Propagator(home), iv(iv0), x0(y0), x1(y1) {
x0.subscribe(home,*this,PC_INT_DOM);
x1.subscribe(home,*this,pc_ac);
iv.subscribe(home,*this,pc_ac);
}
template<class VA, class VB, class VC, PropCond pc_ac>
forceinline
View<VA,VB,VC,pc_ac>::View(Space& home, View& p)
: Propagator(home,p) {
x0.update(home,p.x0);
x1.update(home,p.x1);
iv.update(home,p.iv);
}
template<class VA, class VB, class VC, PropCond pc_ac>
PropCost
View<VA,VB,VC,pc_ac>::cost(const Space&, const ModEventDelta&) const {
// This is required for subscribing to variables in the
// above constructor, but this is then the only time this
// virtual function is ever used!
return PropCost::linear(PropCost::LO,iv.size()+2);
}
template<class VA, class VB, class VC, PropCond pc_ac>
void
View<VA,VB,VC,pc_ac>::reschedule(Space& home) {
x0.reschedule(home,*this,PC_INT_DOM);
x1.reschedule(home,*this,pc_ac);
iv.reschedule(home,*this,pc_ac);
}
template<class VA, class VB, class VC, PropCond pc_ac>
forceinline size_t
View<VA,VB,VC,pc_ac>::dispose(Space& home) {
x0.cancel(home,*this,PC_INT_DOM);
x1.cancel(home,*this,pc_ac);
iv.cancel(home,*this,pc_ac);
(void) Propagator::dispose(home);
return sizeof(*this);
}
template<class View>
class IterIdxView {
private:
const IdxView<View> *cur, *end;
public:
IterIdxView(void);
IterIdxView(const IdxView<View>*, const IdxView<View>*);
void init(const IdxView<View>*, const IdxView<View>*);
bool operator ()(void) const;
void operator ++(void);
int val(void) const;
};
template<class View>
forceinline
IterIdxView<View>::IterIdxView(void) {}
template<class View>
forceinline
IterIdxView<View>::IterIdxView(const IdxView<View>* b,
const IdxView<View>* e)
: cur(b), end(e) {}
template<class View>
forceinline void
IterIdxView<View>::init(const IdxView<View>* b,
const IdxView<View>* e) {
cur=b; end=e;
}
template<class View>
forceinline bool
IterIdxView<View>::operator ()(void) const {
return cur < end;
}
template<class View>
forceinline void
IterIdxView<View>::operator ++(void) {
cur++;
}
template<class View>
forceinline int
IterIdxView<View>::val(void) const {
return cur->idx;
}
/*
* Generic scanning: does all but computing new domain for result
* (which is specific to bounds/domain version)
*
*/
template<class VA, class VB, class VC, PropCond pc_ac, class RelTest>
ExecStatus
scan(Space& home, IdxViewArray<VA>& iv,
VB x0, VC x1, Propagator& p, RelTest rt) {
assert(iv.size() > 1);
/*
* Prunes pairs of index, variable
* - checks for idx value removed
* - checks for disequal variables
*
*/
ViewValues<VB> vx0(x0);
int i = 0;
int j = 0;
while (vx0() && (i < iv.size())) {
if (iv[i].idx < vx0.val()) {
iv[i].view.cancel(home,p,pc_ac);
++i;
} else if (iv[i].idx > vx0.val()) {
++vx0;
} else {
assert(iv[i].idx == vx0.val());
switch (rt(iv[i].view,x1)) {
case RT_FALSE:
iv[i].view.cancel(home,p,pc_ac);
break;
case RT_TRUE:
case RT_MAYBE:
iv[j++] = iv[i];
break;
default: GECODE_NEVER;
}
++vx0; ++i;
}
}
while (i < iv.size())
iv[i++].view.cancel(home,p,pc_ac);
bool adjust = (j<iv.size());
iv.size(j);
if (iv.size() == 0)
return ES_FAILED;
if (iv.size() == 1) {
GECODE_ME_CHECK(x0.eq(home,iv[0].idx));
} else if (adjust) {
IterIdxView<VA> v(&iv[0],&iv[0]+iv.size());
GECODE_ME_CHECK(x0.narrow_v(home,v,false));
assert(x0.size() == static_cast<unsigned int>(iv.size()));
}
return ES_OK;
}
/*
* Bounds consistent propagator
*
*/
template<class VA, class VB, class VC>
forceinline
ViewBnd<VA,VB,VC>::ViewBnd(Home home,
IdxViewArray<VA>& iv, VB x0, VC x1)
: View<VA,VB,VC,PC_INT_BND>(home,iv,x0,x1) {}
template<class VA, class VB, class VC>
ExecStatus
ViewBnd<VA,VB,VC>::post(Home home,
IdxViewArray<VA>& iv, VB x0, VC x1) {
GECODE_ME_CHECK(x0.gq(home,0));
GECODE_ME_CHECK(x0.le(home,iv.size()));
if (x0.assigned()) {
(void) new (home) Rel::EqBnd<VA,VC>(home,iv[x0.val()].view,x1);
return ES_OK;
} else {
assert(iv.size()>1);
(void) new (home) ViewBnd<VA,VB,VC>(home,iv,x0,x1);
}
return ES_OK;
}
template<class VA, class VB, class VC>
forceinline
ViewBnd<VA,VB,VC>::ViewBnd(Space& home, ViewBnd& p)
: View<VA,VB,VC,PC_INT_BND>(home,p) {}
template<class VA, class VB, class VC>
Actor*
ViewBnd<VA,VB,VC>::copy(Space& home) {
return new (home) ViewBnd<VA,VB,VC>(home,*this);
}
template<class VA, class VB, class VC>
ExecStatus
ViewBnd<VA,VB,VC>::propagate(Space& home, const ModEventDelta&) {
assert(iv.size() > 1);
RelTestBnd<VA,VC> rt;
GECODE_ES_CHECK((scan<VA,VB,VC,PC_INT_BND,RelTestBnd<VA,VC> >
(home,iv,x0,x1,*this,rt)));
if (iv.size() == 1) {
ExecStatus es = home.ES_SUBSUMED(*this);
(void) new (home) Rel::EqBnd<VA,VC>(home(*this),iv[0].view,x1);
return es;
}
assert(iv.size() > 1);
// Compute new result
int min = iv[0].view.min();
int max = iv[0].view.max();
for (int i=1; i<iv.size(); i++) {
min = std::min(iv[i].view.min(),min);
max = std::max(iv[i].view.max(),max);
}
ExecStatus es = shared(x0,x1) ? ES_NOFIX : ES_FIX;
{
ModEvent me = x1.lq(home,max);
if (me_failed(me))
return ES_FAILED;
if (me_modified(me) && (x1.max() != max))
es = ES_NOFIX;
}
{
ModEvent me = x1.gq(home,min);
if (me_failed(me))
return ES_FAILED;
if (me_modified(me) && (x1.min() != min))
es = ES_NOFIX;
}
return (x1.assigned() && (min == max)) ?
home.ES_SUBSUMED(*this) : es;
}
/*
* Domain consistent propagator
*
*/
template<class VA, class VB, class VC>
forceinline
ViewDom<VA,VB,VC>::ViewDom(Home home,
IdxViewArray<VA>& iv, VB x0, VC x1)
: View<VA,VB,VC,PC_INT_DOM>(home,iv,x0,x1) {}
template<class VA, class VB, class VC>
ExecStatus
ViewDom<VA,VB,VC>::post(Home home,
IdxViewArray<VA>& iv, VB x0, VC x1){
GECODE_ME_CHECK(x0.gq(home,0));
GECODE_ME_CHECK(x0.le(home,iv.size()));
if (x0.assigned()) {
(void) new (home) Rel::EqDom<VA,VC>(home,iv[x0.val()].view,x1);
return ES_OK;
} else {
assert(iv.size()>1);
(void) new (home) ViewDom<VA,VB,VC>(home,iv,x0,x1);
}
return ES_OK;
}
template<class VA, class VB, class VC>
forceinline
ViewDom<VA,VB,VC>::ViewDom(Space& home, ViewDom& p)
: View<VA,VB,VC,PC_INT_DOM>(home,p) {}
template<class VA, class VB, class VC>
Actor*
ViewDom<VA,VB,VC>::copy(Space& home) {
return new (home) ViewDom<VA,VB,VC>(home,*this);
}
template<class VA, class VB, class VC>
PropCost
ViewDom<VA,VB,VC>::cost(const Space&, const ModEventDelta& med) const {
return PropCost::linear((VA::me(med) != ME_INT_DOM) ?
PropCost::LO : PropCost::HI, iv.size()+2);
}
template<class VA, class VB, class VC>
ExecStatus
ViewDom<VA,VB,VC>::propagate(Space& home, const ModEventDelta& med) {
assert(iv.size() > 1);
if (VA::me(med) != ME_INT_DOM) {
RelTestBnd<VA,VC> rt;
GECODE_ES_CHECK((scan<VA,VB,VC,PC_INT_DOM,RelTestBnd<VA,VC> >
(home,iv,x0,x1,*this,rt)));
if (iv.size() == 1) {
ExecStatus es = home.ES_SUBSUMED(*this);
(void) new (home) Rel::EqDom<VA,VC>(home(*this),iv[0].view,x1);
return es;
}
// Compute new result
int min = iv[0].view.min();
int max = iv[0].view.max();
for (int i=1; i<iv.size(); i++) {
min = std::min(iv[i].view.min(),min);
max = std::max(iv[i].view.max(),max);
}
GECODE_ME_CHECK(x1.lq(home,max));
GECODE_ME_CHECK(x1.gq(home,min));
return (x1.assigned() && (min == max)) ?
home.ES_SUBSUMED(*this) :
home.ES_NOFIX_PARTIAL(*this,VA::med(ME_INT_DOM));
}
RelTestDom<VA,VC> rt;
GECODE_ES_CHECK((scan<VA,VB,VC,PC_INT_DOM,RelTestDom<VA,VC> >
(home,iv,x0,x1,*this,rt)));
if (iv.size() == 1) {
ExecStatus es = home.ES_SUBSUMED(*this);
(void) new (home) Rel::EqDom<VA,VC>(home(*this),iv[0].view,x1);
return es;
}
assert(iv.size() > 1);
if (x1.assigned()) {
for (int i=0; i<iv.size(); i++)
if (iv[i].view.in(x1.val()))
return shared(x0,x1) ? ES_NOFIX : ES_FIX;
return ES_FAILED;
} else {
Region r;
ViewRanges<VA>* i_view = r.alloc<ViewRanges<VA> >(iv.size());
for (int i=0; i<iv.size(); i++)
i_view[i].init(iv[i].view);
Iter::Ranges::NaryUnion i_val(r, i_view, iv.size());
ModEvent me = x1.inter_r(home,i_val);
r.free<ViewRanges<VA> >(i_view,iv.size());
GECODE_ME_CHECK(me);
return (shared(x0,x1) || me_modified(me)) ? ES_NOFIX : ES_FIX;
}
}
}}}
// STATISTICS: int-prop
Gecode::ActorLink::init
void init(void)
Initialize links (self-linked).
Definition core.hpp:3154
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Actor::dispose
virtual size_t dispose(Space &home)
Delete actor and return its size.
Definition core.hpp:3252
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::ConstIntView
Constant integer view.
Definition view.hpp:851
Gecode::Int::ConstIntView::val
int val(void) const
Return assigned value (only if assigned).
Definition constint.hpp:62
Gecode::Int::Element::IterIdxView
Value iterator for indices in index-view map.
Definition view.hpp:164
Gecode::Int::Element::IterIdxView::operator++
void operator++(void)
Definition view.hpp:197
Gecode::Int::Element::IterIdxView::IterIdxView
IterIdxView(void)
Definition view.hpp:178
Gecode::Int::Element::IterIdxView::init
void init(const IdxView< View > *, const IdxView< View > *)
Definition view.hpp:186
Gecode::Int::Element::IterIdxView::operator()
bool operator()(void) const
Definition view.hpp:192
Gecode::Int::Element::IterIdxView::val
int val(void) const
Definition view.hpp:202
Gecode::Int::Element::RelTestBnd
Class for bounds-equality test.
Definition view.hpp:47
Gecode::Int::Element::RelTestBnd::operator()
RelTest operator()(VA, VC)
Definition view.hpp:83
Gecode::Int::Element::RelTestDom
Class for domain-equality test.
Definition view.hpp:66
Gecode::Int::Element::RelTestDom::operator()
RelTest operator()(VA, VC)
Definition view.hpp:94
Gecode::Int::Element::ViewBnd::ViewBnd
ViewBnd(Space &home, ViewBnd &p)
Constructor for cloning p.
Definition view.hpp:301
Gecode::Int::Element::ViewBnd::copy
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition view.hpp:306
Gecode::Int::Element::ViewBnd::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition view.hpp:312
Gecode::Int::Element::ViewBnd::post
static ExecStatus post(Home home, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition view.hpp:284
Gecode::Int::Element::ViewDom::copy
virtual Actor * copy(Space &home)
Perform copying during cloning.
Definition view.hpp:388
Gecode::Int::Element::ViewDom::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition view.hpp:402
Gecode::Int::Element::ViewDom::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition view.hpp:395
Gecode::Int::Element::ViewDom::post
static ExecStatus post(Home home, IdxViewArray< VA > &iv, VB x0, VC x1)
Post propagator for .
Definition view.hpp:366
Gecode::Int::Element::ViewDom::ViewDom
ViewDom(Space &home, ViewDom &p)
Constructor for cloning p.
Definition view.hpp:383
Gecode::Int::Element::View::iv
IdxViewArray< VA > iv
Current index-view map.
Definition element.hh:206
Gecode::Int::Element::View::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function.
Definition view.hpp:131
Gecode::Int::Element::View::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition view.hpp:140
Gecode::Int::Element::View::View
View(Space &home, View &p)
Constructor for cloning p.
Definition view.hpp:122
Gecode::Int::Element::View::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition view.hpp:148
Gecode::Int::Element::View::x1
VC x1
View for result.
Definition element.hh:210
Gecode::Int::Element::View::x0
VB x0
View for index.
Definition element.hh:208
Gecode::Int::IdxViewArray
An array of IdxView pairs.
Definition idx-view.hh:67
Gecode::Int::IdxViewArray::cancel
void cancel(Space &home, Propagator &p, PropCond pc)
Definition idx-view.hpp:139
Gecode::Int::IdxViewArray::size
int size(void) const
Return the current size.
Definition idx-view.hpp:105
Gecode::Int::IdxView
Class for pair of index and view.
Definition idx-view.hh:48
Gecode::Int::Rel::EqBnd
Binary bounds consistent equality propagator.
Definition rel.hh:134
Gecode::Int::Rel::EqDom
Binary domain consistent equality propagator.
Definition rel.hh:68
Gecode::Int::ViewRanges
Range iterator for integer views.
Definition view.hpp:54
Gecode::Int::ViewValues
Value iterator for integer views.
Definition view.hpp:94
Gecode::Iter::Ranges::NaryUnion
Range iterator for union of iterators.
Definition ranges-union.hpp:74
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
Gecode::PropCost::linear
static PropCost linear(PropCost::Mod m, unsigned int n)
Linear complexity for modifier pcm and size measure n.
Definition core.hpp:4796
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::Propagator::Propagator
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3498
Gecode::Region
Handle to region.
Definition region.hpp:55
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::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::Element
Element propagators
Gecode::Int::Element::scan
ExecStatus scan(Space &home, IdxViewArray< VA > &iv, VB x0, VC x1, Propagator &p, RelTest rt)
Definition view.hpp:217
Gecode::Int
Finite domain integers.
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::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::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
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::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
Gecode::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
Gecode::shared
bool shared(ViewArray< ViewX > x, ViewArray< ViewY > y)
Definition array.hpp:1466
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
Test::Int::Distinct::v
const int v[7]
Definition distinct.cpp:259
forceinline
#define forceinline
Definition config.hpp:187
GECODE_NEVER
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56