single.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christopher Mears <Chris.Mears@monash.edu>
*
* Contributing authors:
* Christian Schulte <schulte@gecode.org>
* Guido Tack <tack@gecode.org>
*
* Copyright:
* Christopher Mears, 2011
* Christian Schulte, 2011
* Guido Tack, 2011
*
* 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.
*
*/
namespace Gecode { namespace Set { namespace Precede {
template<class View>
forceinline
Single<View>::Index::Index(Space& home, Propagator& p,
Council<Index>& c, int i0)
: Advisor(home,p,c), i(i0) {}
template<class View>
forceinline
Single<View>::Index::Index(Space& home, Index& a)
: Advisor(home,a), i(a.i) {}
template<class View>
forceinline ExecStatus
Single<View>::updateAlpha(Space& home) {
int n = x.size();
while (alpha < n) {
if (x[alpha].notContains(s)) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
} else if (x[alpha].contains(t)) {
GECODE_ME_CHECK(x[alpha].include(home, s));
} else {
break;
}
alpha++;
}
return ES_OK;
}
template<class View>
forceinline ExecStatus
Single<View>::updateBeta(Space& home) {
int n = x.size();
do {
beta++;
} while ((beta < n) &&
(x[beta].notContains(s) || x[beta].contains(t)));
if (beta > gamma) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
GECODE_ME_CHECK(x[alpha].include(home, s));
}
return ES_OK;
}
template<class View>
forceinline
Single<View>::Single(Home home, ViewArray<View>& x0,
int s0, int t0, int b, int g)
: NaryPropagator<View, PC_SET_NONE>(home,x0),
c(home), s(s0), t(t0), alpha(0), beta(b), gamma(g) {
for (int i=x.size(); i--; )
if (!x[i].assigned())
x[i].subscribe(home,*new (home) Index(home,*this,c,i));
View::schedule(home, *this, ME_SET_BB);
}
template<class View>
inline ExecStatus
Single<View>::post(Home home, ViewArray<View>& x, int s, int t) {
{
int alpha = 0;
while (alpha < x.size()) {
if (x[alpha].notContains(s)) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
} else if (x[alpha].contains(t)) {
GECODE_ME_CHECK(x[alpha].include(home, s));
} else {
break;
}
alpha++;
}
x.drop_fst(alpha);
if (x.size() == 0)
return ES_OK;
}
// alpha has been normalized to 0
int beta = 0, gamma = 0;
do {
gamma++;
} while ((gamma < x.size()) &&
(!x[gamma].notContains(s) || !x[gamma].contains(t)));
do {
beta++;
} while ((beta < x.size()) &&
(x[beta].notContains(s) || x[beta].contains(t)));
if (beta > gamma) {
GECODE_ME_CHECK(x[0].exclude(home, t));
GECODE_ME_CHECK(x[0].include(home, s));
return ES_OK;
}
if (gamma < x.size())
x.drop_lst(gamma);
(void) new (home) Single<View>(home, x, s, t, beta, gamma);
return ES_OK;
}
template<class View>
forceinline
Single<View>::Single(Space& home, Single& p)
: NaryPropagator<View,PC_SET_NONE>(home, p),
s(p.s), t(p.t),
alpha(p.alpha), beta(p.beta), gamma(p.gamma) {
c.update(home, p.c);
}
template<class View>
Propagator*
Single<View>::copy(Space& home) {
// Try to eliminate assigned views at the beginning
if (alpha > 0) {
int i = 0;
while ((i < alpha) && x[i].assigned())
i++;
x.drop_fst(i);
for (Advisors<Index> as(c); as(); ++as)
as.advisor().i -= i;
alpha -= i; beta -= i; gamma -= i;
}
// Try to eliminate assigned views at the end
if (gamma < x.size()) {
int i = x.size()-1;
while ((i > gamma) && x[i].assigned())
i--;
x.drop_lst(i);
}
return new (home) Single<View>(home, *this);
}
template<class View>
inline size_t
Single<View>::dispose(Space& home) {
// Cancel remaining advisors
for (Advisors<Index> as(c); as(); ++as)
x[as.advisor().i].cancel(home,as.advisor());
c.dispose(home);
(void) NaryPropagator<View,PC_SET_NONE>::dispose(home);
return sizeof(*this);
}
template<class View>
PropCost
Single<View>::cost(const Space&, const ModEventDelta&) const {
return PropCost::linear(PropCost::LO, x.size());
}
template<class View>
void
Single<View>::reschedule(Space& home) {
View::schedule(home, *this, ME_SET_BB);
}
template<class View>
ExecStatus
Single<View>::advise(Space& home, Advisor& a0, const Delta&) {
Index& a(static_cast<Index&>(a0));
int i = a.i;
// Check for gamma
if ((beta <= gamma) && (i < gamma) &&
x[i].notContains(s) && x[i].contains(t))
gamma = i;
if (x[i].assigned()) {
a.dispose(home,c);
if (c.empty())
return ES_NOFIX;
} else if ((i < alpha) || (i > gamma)) {
x[i].cancel(home,a);
a.dispose(home,c);
return (c.empty()) ? ES_NOFIX : ES_FIX;
}
if (beta > gamma)
return ES_NOFIX;
if ((alpha == i) || (beta == i))
return ES_NOFIX;
return ES_FIX;
}
template<class View>
ExecStatus
Single<View>::propagate(Space& home, const ModEventDelta&) {
int n = x.size();
if (beta > gamma) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
GECODE_ME_CHECK(x[alpha].include(home, s));
return home.ES_SUBSUMED(*this);
}
if (alpha < n && (x[alpha].notContains(s) || x[alpha].contains(t))) {
if (x[alpha].notContains(s)) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
} else {
GECODE_ME_CHECK(x[alpha].include(home, s));
}
alpha++;
while (alpha < beta) {
if (x[alpha].notContains(s)) {
GECODE_ME_CHECK(x[alpha].exclude(home, t));
} else {
GECODE_ME_CHECK(x[alpha].include(home, s));
}
alpha++;
}
GECODE_ES_CHECK(updateAlpha(home));
beta = alpha;
if (alpha < n)
GECODE_ES_CHECK(updateBeta(home));
} else if ((beta < n) && (x[beta].notContains(s) || x[beta].contains(t))) {
GECODE_ES_CHECK(updateBeta(home));
}
return (c.empty()) ? home.ES_SUBSUMED(*this) : ES_FIX;
}
}}}
// STATISTICS: set-prop
Gecode::ActorLink::Space
friend class Space
Definition core.hpp:595
Gecode::ActorLink::Propagator
friend class Propagator
Definition core.hpp:591
Gecode::Advisor::Advisor
Advisor(Space &home, Propagator &p, Council< A > &c)
Constructor for creation.
Definition core.hpp:3831
Gecode::Advisor::Council
friend class Council
Definition core.hpp:1294
Gecode::Advisors
Class to iterate over advisors of a council.
Definition core.hpp:1266
Gecode::Delta
Generic domain change information to be supplied to advisors.
Definition core.hpp:204
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::Precede::Single::Index::Index
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
Definition single.hpp:49
Gecode::Int::Precede::Single::advise
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition single.hpp:183
Gecode::Int::Precede::Single::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition single.hpp:211
Gecode::Int::Precede::Single::copy
virtual Propagator * copy(Space &home)
Copy propagator during cloning.
Definition single.hpp:136
Gecode::Int::Precede::Single::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition single.hpp:177
Gecode::Int::Precede::Single::Single
Single(Home home, ViewArray< View > &x, int s, int t, int beta, int gamma)
Constructor for posting.
Definition single.hpp:84
Gecode::Int::Precede::Single::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition single.hpp:160
Gecode::Int::Precede::Single::cost
virtual PropCost cost(const Space &, const ModEventDelta &) const
Cost function.
Definition single.hpp:171
Gecode::Int::Precede::Single::post
static ExecStatus post(Home home, ViewArray< View > &x, int s, int t)
Post propagator that s precedes t in x.
Definition single.hpp:96
Gecode::NaryPropagator
n-ary propagator
Definition pattern.hpp:142
Gecode::NaryPropagator::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition pattern.hpp:513
Gecode::NaryPropagator< View, PC_SET_NONE >::x
ViewArray< View > x
Definition pattern.hpp:145
Gecode::NaryPropagator< View, PC_SET_NONE >::NaryPropagator
NaryPropagator(Space &home, NaryPropagator &p)
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
Base-class for propagators.
Definition core.hpp:1064
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Propagator::Advisor
friend class Advisor
Definition core.hpp:1068
Gecode::Set::Precede::Single::Index
Advisors for views (by position in array)
Definition precede.hh:67
Gecode::Set::Precede::Single::Index::Index
Index(Space &home, Propagator &p, Council< Index > &c, int i)
Create index advisor.
Definition single.hpp:44
Gecode::Set::Precede::Single::Index::i
int i
The position of the view in the view array.
Definition precede.hh:70
Gecode::Set::Precede::Single
Single value precedence propagator.
Definition precede.hh:63
Gecode::Set::Precede::Single::alpha
int alpha
Pointers updated during propagation.
Definition precede.hh:81
Gecode::Set::Precede::Single::gamma
int gamma
Definition precede.hh:81
Gecode::Set::Precede::Single::updateBeta
ExecStatus updateBeta(Space &home)
Update the beta pointer.
Definition single.hpp:73
Gecode::Set::Precede::Single::t
int t
Definition precede.hh:79
Gecode::Set::Precede::Single::c
Council< Index > c
The advisor council.
Definition precede.hh:77
Gecode::Set::Precede::Single::s
int s
The value s must precede t.
Definition precede.hh:79
Gecode::Set::Precede::Single::Single
Single(Home home, ViewArray< View > &x, int s, int t, int beta, int gamma)
Constructor for posting.
Definition single.hpp:88
Gecode::Set::Precede::Single::updateAlpha
ExecStatus updateAlpha(Space &home)
Update the alpha pointer.
Definition single.hpp:56
Gecode::Set::Precede::Single::beta
int beta
Definition precede.hh:81
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::View
Gecode::ViewArray
View arrays.
Definition array.hpp:253
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::Int::Precede::assigned
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition single.hpp:43
Gecode::Set::Precede
Value precedence propagators.
Gecode::Set
Finite integer sets.
Definition var-imp.hpp:137
Gecode::Set::ME_SET_BB
const Gecode::ModEvent ME_SET_BB
Domain operation has changed both greatest lower and least upper bound.
Definition var-type.hpp:172
Gecode::Set::PC_SET_NONE
const Gecode::PropCond PC_SET_NONE
Propagation condition to be ignored (convenience).
Definition var-type.hpp:199
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_FIX
@ ES_FIX
Propagation has computed fixpoint.
Definition core.hpp:477
Gecode::ES_NOFIX
@ ES_NOFIX
Propagation has not computed fixpoint.
Definition core.hpp:475
forceinline
#define forceinline
Definition config.hpp:187