clause.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, 2008
*
* 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 Int { namespace Bool {
/*
* Boolean clause propagator (disjunctive, true)
*
*/
template<class VX, class VY>
forceinline
ClauseTrue<VX,VY>::ClauseTrue(Home home,
ViewArray<VX>& x0, ViewArray<VY>& y0)
: MixBinaryPropagator<VX,PC_BOOL_VAL,VY,PC_BOOL_VAL>
(home,x0[x0.size()-1],y0[y0.size()-1]), x(x0), y(y0) {
assert((x.size() > 0) && (y.size() > 0));
x.size(x.size()-1); y.size(y.size()-1);
}
template<class VX, class VY>
PropCost
ClauseTrue<VX,VY>::cost(const Space&, const ModEventDelta&) const {
return PropCost::binary(PropCost::LO);
}
template<class VX, class VY>
forceinline
ClauseTrue<VX,VY>::ClauseTrue(Space& home, ClauseTrue<VX,VY>& p)
: MixBinaryPropagator<VX,PC_BOOL_VAL,VY,PC_BOOL_VAL>(home,p) {
x.update(home,p.x);
y.update(home,p.y);
}
template<class VX, class VY>
Actor*
ClauseTrue<VX,VY>::copy(Space& home) {
{
int n = x.size();
if (n > 0) {
// Eliminate all zeros and find a one
for (int i=n; i--; )
if (x[i].one()) {
// Only keep the one
x[0]=x[i]; n=1; break;
} else if (x[i].zero()) {
// Eliminate the zero
x[i]=x[--n];
}
x.size(n);
}
}
{
int n = y.size();
if (n > 0) {
// Eliminate all zeros and find a one
for (int i=n; i--; )
if (y[i].one()) {
// Only keep the one
y[0]=y[i]; n=1; break;
} else if (y[i].zero()) {
// Eliminate the zero
y[i]=y[--n];
}
y.size(n);
}
}
if ((x.size() == 0) && (y.size() == 0))
return new (home) BinOrTrue<VX,VY>(home,*this,x0,x1);
else
return new (home) ClauseTrue<VX,VY>(home,*this);
}
template<class VX, class VY>
inline ExecStatus
ClauseTrue<VX,VY>::post(Home home, ViewArray<VX>& x, ViewArray<VY>& y) {
for (int i=x.size(); i--; )
if (x[i].one())
return ES_OK;
else if (x[i].zero())
x.move_lst(i);
if (x.size() == 0)
return NaryOrTrue<VY>::post(home,y);
for (int i=y.size(); i--; )
if (y[i].one())
return ES_OK;
else if (y[i].zero())
y.move_lst(i);
if (y.size() == 0)
return NaryOrTrue<VX>::post(home,x);
if ((x.size() == 1) && (y.size() == 1)) {
return BinOrTrue<VX,VY>::post(home,x[0],y[0]);
} else if (!shared(x,y)) {
(void) new (home) ClauseTrue(home,x,y);
}
return ES_OK;
}
template<class VX, class VY>
forceinline size_t
ClauseTrue<VX,VY>::dispose(Space& home) {
(void) MixBinaryPropagator<VX,PC_BOOL_VAL,VY,PC_BOOL_VAL>::dispose(home);
return sizeof(*this);
}
template<class VX, class VY>
forceinline ExecStatus
resubscribe(Space& home, Propagator& p,
VX& x0, ViewArray<VX>& x,
VY& x1, ViewArray<VY>& y) {
if (x0.zero()) {
int n = x.size();
for (int i=n; i--; )
if (x[i].one()) {
x.size(n);
return home.ES_SUBSUMED(p);
} else if (x[i].zero()) {
x[i] = x[--n];
} else {
// Rewrite if there is just one view left
if ((i == 0) && (y.size() == 0)) {
VX z = x[0]; x.size(0);
GECODE_REWRITE(p,(BinOrTrue<VX,VY>::post(home(p),z,x1)));
}
// Move to x0 and subscribe
x0=x[i]; x[i]=x[--n];
x.size(n);
x0.subscribe(home,p,PC_BOOL_VAL,false);
return ES_FIX;
}
// All x-views have been assigned!
ViewArray<VY> z(home,y.size()+1);
for (int i=0; i<y.size(); i++)
z[i]=y[i];
z[y.size()] = x1;
GECODE_REWRITE(p,(NaryOrTrue<VY>::post(home(p),z)));
}
return ES_FIX;
}
template<class VX, class VY>
ExecStatus
ClauseTrue<VX,VY>::propagate(Space& home, const ModEventDelta&) {
if (x0.one() || x1.one())
return home.ES_SUBSUMED(*this);
GECODE_ES_CHECK(resubscribe(home,*this,x0,x,x1,y));
GECODE_ES_CHECK(resubscribe(home,*this,x1,y,x0,x));
return ES_FIX;
}
/*
* Boolean clause propagator (disjunctive)
*
*/
/*
* Index advisors
*
*/
template<class VX, class VY>
forceinline
Clause<VX,VY>::Tagged::Tagged(Space& home, Propagator& p,
Council<Tagged>& c, bool x0)
: Advisor(home,p,c), x(x0) {}
template<class VX, class VY>
forceinline
Clause<VX,VY>::Tagged::Tagged(Space& home, Tagged& a)
: Advisor(home,a), x(a.x) {}
template<class VX, class VY>
forceinline
Clause<VX,VY>::Clause(Home home, ViewArray<VX>& x0, ViewArray<VY>& y0,
VX z0)
: Propagator(home), x(x0), y(y0), z(z0), n_zero(0), c(home) {
x.subscribe(home,*new (home) Tagged(home,*this,c,true));
y.subscribe(home,*new (home) Tagged(home,*this,c,false));
z.subscribe(home,*this,PC_BOOL_VAL);
}
template<class VX, class VY>
forceinline
Clause<VX,VY>::Clause(Space& home, Clause<VX,VY>& p)
: Propagator(home,p), n_zero(p.n_zero) {
x.update(home,p.x);
y.update(home,p.y);
z.update(home,p.z);
c.update(home,p.c);
}
template<class VX>
forceinline void
eliminate_zero(ViewArray<VX>& x, int& n_zero) {
if (n_zero > 0) {
int n=x.size();
// Eliminate all zeros
for (int i=n; i--; )
if (x[i].zero()) {
x[i]=x[--n]; n_zero--;
}
x.size(n);
}
}
template<class VX, class VY>
Actor*
Clause<VX,VY>::copy(Space& home) {
eliminate_zero(x,n_zero);
eliminate_zero(y,n_zero);
return new (home) Clause<VX,VY>(home,*this);
}
template<class VX, class VY>
inline ExecStatus
Clause<VX,VY>::post(Home home, ViewArray<VX>& x, ViewArray<VY>& y, VX z) {
assert(!shared(x) && !shared(y));
if (z.one())
return ClauseTrue<VX,VY>::post(home,x,y);
if (z.zero()) {
for (int i=0; i<x.size(); i++)
GECODE_ME_CHECK(x[i].zero(home));
for (int i=0; i<y.size(); i++)
GECODE_ME_CHECK(y[i].zero(home));
return ES_OK;
}
for (int i=x.size(); i--; )
if (x[i].one()) {
GECODE_ME_CHECK(z.one_none(home));
return ES_OK;
} else if (x[i].zero()) {
x.move_lst(i);
}
if (x.size() == 0)
return NaryOr<VY,VX>::post(home,y,z);
for (int i=y.size(); i--; )
if (y[i].one()) {
GECODE_ME_CHECK(z.one_none(home));
return ES_OK;
} else if (y[i].zero()) {
y.move_lst(i);
}
if (y.size() == 0)
return NaryOr<VX,VX>::post(home,x,z);
if ((x.size() == 1) && (y.size() == 1)) {
return Or<VX,VY,VX>::post(home,x[0],y[0],z);
} else if (shared(x,y)) {
GECODE_ME_CHECK(z.one_none(home));
} else {
(void) new (home) Clause<VX,VY>(home,x,y,z);
}
return ES_OK;
}
template<class VX, class VY>
PropCost
Clause<VX,VY>::cost(const Space&, const ModEventDelta&) const {
return PropCost::unary(PropCost::LO);
}
template<class VX, class VY>
forceinline void
Clause<VX,VY>::cancel(Space& home) {
for (Advisors<Tagged> as(c); as(); ++as) {
if (as.advisor().x)
x.cancel(home,as.advisor());
else
y.cancel(home,as.advisor());
as.advisor().dispose(home,c);
}
c.dispose(home);
z.cancel(home,*this,PC_BOOL_VAL);
}
template<class VX, class VY>
forceinline size_t
Clause<VX,VY>::dispose(Space& home) {
cancel(home);
(void) Propagator::dispose(home);
return sizeof(*this);
}
template<class VX, class VY>
ExecStatus
Clause<VX,VY>::advise(Space&, Advisor& _a, const Delta& d) {
Tagged& a = static_cast<Tagged&>(_a);
// Decides whether the propagator must be run
if ((a.x && VX::zero(d)) || (!a.x && VY::zero(d)))
if (++n_zero < x.size() + y.size())
return ES_FIX;
return ES_NOFIX;
}
template<class VX, class VY>
void
Clause<VX,VY>::reschedule(Space& home) {
z.reschedule(home,*this,PC_BOOL_VAL);
if (n_zero == x.size() + y.size())
VX::schedule(home,*this,ME_BOOL_VAL);
for (int i=0; i<x.size(); i++)
if (x[i].one()) {
VX::schedule(home,*this,ME_BOOL_VAL);
return;
}
for (int i=0; i<y.size(); i++)
if (y[i].one()) {
VX::schedule(home,*this,ME_BOOL_VAL);
return;
}
}
template<class VX, class VY>
ExecStatus
Clause<VX,VY>::propagate(Space& home, const ModEventDelta&) {
if (z.one())
GECODE_REWRITE(*this,(ClauseTrue<VX,VY>::post(home(*this),x,y)));
if (z.zero()) {
for (int i=0; i<x.size(); i++)
GECODE_ME_CHECK(x[i].zero(home));
for (int i=0; i<y.size(); i++)
GECODE_ME_CHECK(y[i].zero(home));
c.dispose(home);
} else if (n_zero == x.size() + y.size()) {
GECODE_ME_CHECK(z.zero_none(home));
c.dispose(home);
} else {
// There is at least one view which is one
GECODE_ME_CHECK(z.one_none(home));
}
return home.ES_SUBSUMED(*this);
}
}}}
// STATISTICS: int-prop
Gecode::ActorLink::Space
friend class Space
Definition core.hpp:595
Gecode::ActorLink::Propagator
friend class Propagator
Definition core.hpp:591
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::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::FloatVal::x
FloatValImpType x
Implementation of float value.
Definition float.hh:425
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::Int::Bool::BinOrTrue
Binary Boolean disjunction propagator (true).
Definition bool.hh:237
Gecode::Int::Bool::BinOrTrue::post
static ExecStatus post(Home home, BVA b0, BVB b1)
Post propagator .
Definition or.hpp:125
Gecode::Int::Bool::ClauseTrue::post
static ExecStatus post(Home home, ViewArray< VX > &x, ViewArray< VY > &y)
Post propagator .
Definition clause.hpp:106
Gecode::Int::Bool::ClauseTrue::ClauseTrue
ClauseTrue(Home home, ViewArray< VX > &x, ViewArray< VY > &y)
Constructor for posting.
Definition clause.hpp:43
Gecode::Int::Bool::ClauseTrue::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition clause.hpp:131
Gecode::Int::Bool::ClauseTrue::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low binary).
Definition clause.hpp:53
Gecode::Int::Bool::ClauseTrue::x
ViewArray< VX > x
Views not yet subscribed to.
Definition bool.hh:552
Gecode::Int::Bool::ClauseTrue::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition clause.hpp:67
Gecode::Int::Bool::ClauseTrue::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition clause.hpp:173
Gecode::Int::Bool::ClauseTrue::y
ViewArray< VY > y
Views not yet subscribed to (origin from negative variables).
Definition bool.hh:554
Gecode::Int::Bool::Clause::Tagged
Advisors for views (tagged whether for x or y)
Definition bool.hh:503
Gecode::Int::Bool::Clause::Tagged::Tagged
Tagged(Space &home, Propagator &p, Council< Tagged > &c, bool x)
Create tagged advisor.
Definition clause.hpp:193
Gecode::Int::Bool::Clause::Tagged::x
const bool x
Whether advises a view for x or y.
Definition bool.hh:506
Gecode::Int::Bool::Clause::propagate
virtual ExecStatus propagate(Space &home, const ModEventDelta &med)
Perform propagation.
Definition clause.hpp:345
Gecode::Int::Bool::Clause::z
VX z
Result.
Definition bool.hh:499
Gecode::Int::Bool::Clause::post
static ExecStatus post(Home home, ViewArray< VX > &x, ViewArray< VY > &y, VX z)
Post propagator .
Definition clause.hpp:246
Gecode::Int::Bool::Clause::x
ViewArray< VX > x
Positive views.
Definition bool.hh:495
Gecode::Int::Bool::Clause::advise
virtual ExecStatus advise(Space &home, Advisor &a, const Delta &d)
Give advice to propagator.
Definition clause.hpp:316
Gecode::Int::Bool::Clause::reschedule
virtual void reschedule(Space &home)
Schedule function.
Definition clause.hpp:327
Gecode::Int::Bool::Clause::n_zero
int n_zero
The number of views assigned to zero in x and y.
Definition bool.hh:501
Gecode::Int::Bool::Clause::Clause
Clause(Home home, ViewArray< VX > &x, ViewArray< VY > &y, VX z)
Constructor for posting.
Definition clause.hpp:204
Gecode::Int::Bool::Clause::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition clause.hpp:307
Gecode::Int::Bool::Clause::c
Council< Tagged > c
The advisor council.
Definition bool.hh:513
Gecode::Int::Bool::Clause::y
ViewArray< VY > y
Positive views (origin from negative variables).
Definition bool.hh:497
Gecode::Int::Bool::Clause::copy
virtual Actor * copy(Space &home)
Copy propagator during cloning.
Definition clause.hpp:238
Gecode::Int::Bool::Clause::cost
virtual PropCost cost(const Space &home, const ModEventDelta &med) const
Cost function (defined as low unary).
Definition clause.hpp:287
Gecode::Int::Bool::Clause::cancel
void cancel(Space &home)
Cancel subscriptions.
Definition clause.hpp:293
Gecode::Int::Bool::NaryOrTrue::post
static ExecStatus post(Home home, ViewArray< BV > &b)
Post propagator .
Definition or.hpp:663
Gecode::Int::Bool::NaryOr::post
static ExecStatus post(Home home, ViewArray< VX > &x, VY y)
Post propagator .
Definition or.hpp:769
Gecode::Int::Bool::Or::post
static ExecStatus post(Home home, BVA b0, BVB b1, BVC b2)
Post propagator .
Definition or.hpp:494
Gecode::MixBinaryPropagator< VX, PC_BOOL_VAL, VY, PC_BOOL_VAL >::x1
VY x1
Definition pattern.hpp:209
Gecode::MixBinaryPropagator< VX, PC_BOOL_VAL, VY, PC_BOOL_VAL >::MixBinaryPropagator
MixBinaryPropagator(Space &home, MixBinaryPropagator &p)
Definition pattern.hpp:597
Gecode::MixBinaryPropagator< VX, PC_BOOL_VAL, VY, PC_BOOL_VAL >::x0
VX x0
Definition pattern.hpp:207
Gecode::MixBinaryPropagator::dispose
virtual size_t dispose(Space &home)
Delete propagator and return its size.
Definition pattern.hpp:630
Gecode::PropCost
Propagation cost.
Definition core.hpp:486
Gecode::PropCost::unary
static PropCost unary(PropCost::Mod m)
Single variable for modifier pcm.
Definition core.hpp:4813
Gecode::PropCost::binary
static PropCost binary(PropCost::Mod m)
Two variables for modifier pcm.
Definition core.hpp:4809
Gecode::PropCost::LO
@ LO
Cheap.
Definition core.hpp:513
Gecode::Propagator
Base-class for propagators.
Definition core.hpp:1064
Gecode::Propagator::size
size_t size
The size of the propagator (used during subsumption).
Definition core.hpp:1077
Gecode::Propagator::Space
friend class Space
Definition core.hpp:1066
Gecode::Propagator::Advisor
friend class Advisor
Definition core.hpp:1068
Gecode::Propagator::Propagator
Propagator(Home home)
Constructor for posting.
Definition core.hpp:3498
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1156
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
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::Bool
Boolean propagators.
Gecode::Int::Bool::resubscribe
ExecStatus resubscribe(Space &home, Propagator &p, VX &x0, ViewArray< VX > &x, VY &x1, ViewArray< VY > &y)
Definition clause.hpp:138
Gecode::Int::Bool::eliminate_zero
void eliminate_zero(ViewArray< VX > &x, int &n_zero)
Definition clause.hpp:224
Gecode::Int
Finite domain integers.
Gecode::Int::ME_BOOL_VAL
const Gecode::ModEvent ME_BOOL_VAL
Domain operation has resulted in a value (assigned variable).
Definition var-type.hpp:116
Gecode::Int::PC_BOOL_VAL
const Gecode::PropCond PC_BOOL_VAL
Propagate when a view becomes assigned (single value).
Definition var-type.hpp:126
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
Gecode::shared
bool shared(ViewArray< ViewX > x, ViewArray< ViewY > y)
Definition array.hpp:1466
forceinline
#define forceinline
Definition config.hpp:187