sorted.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Guido Tack <tack@gecode.org>
* Christian Schulte <schulte@gecode.org>
* Gabor Szokoli <szokoli@gecode.org>
*
* Copyright:
* Guido Tack, 2004
* Christian Schulte, 2004
* Gabor Szokoli, 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/set.hh>
#include <gecode/int.hh>
#include <gecode/set/rel.hh>
namespace Gecode { namespace Set { namespace Channel {
template<class View>
forceinline
ChannelSorted<View>::ChannelSorted(Home home, View y0,
ViewArray< Gecode::Int::IntView >& ys)
: Propagator(home), x0(y0), xs(ys) {
x0.subscribe(home,*this, PC_SET_ANY);
xs.subscribe(home,*this, Gecode::Int::PC_INT_BND);
}
template<class View>
forceinline
ChannelSorted<View>::ChannelSorted(Space& home, ChannelSorted& p)
: Propagator(home,p) {
x0.update(home,p.x0);
xs.update(home,p.xs);
}
template<class View>
forceinline ExecStatus
ChannelSorted<View>::post(Home home, View x0,
ViewArray<Gecode::Int::IntView>& xs) {
unsigned int xs_size = static_cast<unsigned int>(xs.size());
GECODE_ME_CHECK(x0.cardMin(home,xs_size));
GECODE_ME_CHECK(x0.cardMax(home,xs_size));
if (xs_size == 1) {
SingletonView sv(xs[0]);
GECODE_ES_CHECK((Rel::Eq<View,
SingletonView>::post(home,x0, sv)));
} else {
// Sharing in xs is handled correctly in the propagator:
// if two views in xs are shared, this leads to failure.
(void) new (home) ChannelSorted(home,x0,xs);
}
return ES_OK;
}
template<class View>
PropCost
ChannelSorted<View>::cost(const Space&, const ModEventDelta&) const {
return PropCost::linear(PropCost::LO, xs.size()+1);
}
template<class View>
void
ChannelSorted<View>::reschedule(Space& home) {
x0.reschedule(home,*this, PC_SET_ANY);
xs.reschedule(home,*this, Gecode::Int::PC_INT_BND);
}
template<class View>
forceinline size_t
ChannelSorted<View>::dispose(Space& home) {
x0.cancel(home,*this, PC_SET_ANY);
xs.cancel(home,*this, Gecode::Int::PC_INT_BND);
(void) Propagator::dispose(home);
return sizeof(*this);
}
template<class View>
Actor*
ChannelSorted<View>::copy(Space& home) {
return new (home) ChannelSorted(home,*this);
}
template<class View>
ExecStatus
ChannelSorted<View>::propagate(Space& home, const ModEventDelta&) {
int xs_size = xs.size();
bool loopFlag;
do {
loopFlag = false;
// Order int vars in xs
GECODE_ME_CHECK(xs[0].gq(home,x0.lubMin()));
for (int i=xs_size-1; i--; ) {
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i+1].gq(home,xs[i].min() + 1));
}
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[xs_size-1].lq(home,x0.lubMax()));
for (int i=xs_size-2; i--; ) {
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].lq(home,xs[i+1].max() - 1));
}
// if y from xs is assigned, add to glb(x0)
for (int i=xs_size; i--; ) {
if (xs[i].assigned()) {
GECODE_ME_CHECK_MODIFIED(loopFlag, x0.include(home,xs[i].val()));
}
}
// intersect every y in xs with lub(x0)
for (int i=xs_size; i--; ) {
LubRanges<View> ub(x0);
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].inter_r(home,ub,false));
}
// remove gaps between vars in xs from lub(x0)
GECODE_ME_CHECK_MODIFIED(loopFlag,
x0.exclude(home,Limits::min,xs[0].min()-1));
GECODE_ME_CHECK_MODIFIED(loopFlag,
x0.exclude(home,xs[xs_size-1].max()+1,
Limits::max));
for (int i=xs_size-1; i--; ) {
int start = xs[i].max() + 1;
int end = xs[i+1].min() - 1;
if (start<=end) {
GECODE_ME_CHECK_MODIFIED(loopFlag, x0.exclude(home,start,end));
}
}
// try to assign vars in xs from glb(x0)
if (x0.glbSize()>0) {
LubRanges<View> ub(x0);
Iter::Ranges::ToValues<LubRanges<View> > ubv(ub);
GlbRanges<View> lb(x0);
Iter::Ranges::ToValues<GlbRanges<View> > lbv(lb);
int i=0;
for (; ubv() && lbv() && ubv.val()==lbv.val();
++ubv, ++lbv, i++) {
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[i].eq(home,lbv.val()));
}
if (i<xs_size-1 && x0.lubMax()==x0.glbMax()) {
LubRanges<View> lbx0(x0);
GlbRanges<View> ubx0(x0);
Iter::Ranges::Inter<LubRanges<View>,GlbRanges<View> >
inter(lbx0, ubx0);
int to = x0.glbMax();
int from = to;
while (inter()) {
from = inter.min();
++inter;
}
int k=xs_size-1;
for (int j=to; j>=from;j--,k--) {
GECODE_ME_CHECK_MODIFIED(loopFlag, xs[k].eq(home,j));
}
}
}
} while (loopFlag);
for (int i=xs_size; i--; )
if (!xs[i].assigned())
return ES_FIX;
return home.ES_SUBSUMED(*this);
}
}}}
// STATISTICS: set-prop
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::Iter::Ranges::Inter
Range iterator for computing intersection (binary).
Definition ranges-inter.hpp:44
Gecode::Iter::Ranges::ToValues
Value iterator from range iterator.
Definition ranges-values.hpp:42
Gecode::Iter::Ranges::ToValues::val
int val(void) const
Return current value.
Definition ranges-values.hpp:129
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::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Propagator::Propagator
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3498
Gecode::Set::Channel::ChannelSorted::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition sorted.hpp:90
Gecode::Set::Channel::ChannelSorted::ChannelSorted
ChannelSorted(Space &home, ChannelSorted &p)
Constructor for cloning p.
Definition sorted.hpp:57
Gecode::Set::Channel::ChannelSorted::post
static ExecStatus post(Home home, View s, ViewArray< Gecode::Int::IntView > &x)
Post propagator that propagates that s contains the , which are sorted in non-descending order.
Definition sorted.hpp:65
Gecode::Set::Channel::ChannelSorted::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition sorted.hpp:106
Gecode::Set::Channel::ChannelSorted::dispose
virtual size_t dispose(Space &home)
Delete Propagator.
Definition sorted.hpp:97
Gecode::Set::Channel::ChannelSorted::x0
View x0
SetView for the match.
Definition channel.hh:63
Gecode::Set::Channel::ChannelSorted::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition sorted.hpp:112
Gecode::Set::Channel::ChannelSorted::xs
ViewArray< Gecode::Int::IntView > xs
IntViews that together form the set x0.
Definition channel.hh:65
Gecode::Set::Channel::ChannelSorted::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as PC_LINEAR_LO).
Definition sorted.hpp:84
Gecode::Set::GlbRanges
Range iterator for the greatest lower bound.
Definition var-imp.hpp:359
Gecode::Set::LubRanges
Range iterator for the least upper bound.
Definition var-imp.hpp:317
Gecode::Set::Rel::Eq
Propagator for set equality
Definition rel.hh:150
Gecode::Set::SingletonView
Singleton set view.
Definition view.hpp:594
Gecode::View
Gecode::ViewArray< Gecode::Int::IntView >
int.hh
set.hh
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_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_CHECK_MODIFIED
#define GECODE_ME_CHECK_MODIFIED(modified, me)
Check whether me is failed or modified, and forward failure.
Definition macros.hpp:64
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::Set::Channel
Channeling propagators for set variables.
Definition channel.hh:43
Gecode::Set::Limits::min
const int min
Smallest allowed integer in integer set.
Definition set.hh:99
Gecode::Set::Limits::max
const int max
Largest allowed integer in integer set.
Definition set.hh:97
Gecode::Set
Finite integer sets.
Definition var-imp.hpp:137
Gecode::Set::PC_SET_ANY
const Gecode::PropCond PC_SET_ANY
Propagate when any bound or the cardinality of a view changes.
Definition var-type.hpp:248
Gecode
Gecode toplevel namespace
Gecode::min
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:67
Gecode::inter
SetExpr inter(const SetVarArgs &)
Intersection of set variables.
Definition set-expr.cpp:696
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::max
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition arithmetic.cpp:49
rel.hh
forceinline
#define forceinline
Definition config.hpp:187