/* * Main authors: * Christian Schulte * Guido Tack * * Copyright: * Christian Schulte, 2002 * Guido Tack, 2004 * * Last modified: * $Date: 2006-08-04 16:03:05 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $ * $Revision: 3510 $ * * This file is part of Gecode, the generic constraint * development environment: * http://www.gecode.org * * See the file "LICENSE" for information on usage and * redistribution of this file, and for a * DISCLAIMER OF ALL WARRANTIES. * */ namespace Gecode { /** * \brief Dynamic low-propagation cost computation * * If \a n is less than 4, use constant low-costs, otherwise use \a pc. * \ingroup TaskActor */ PropCost cost_lo(int n, PropCost pc); /** * \brief Dynamic high-propagation cost computation * * If \a n is less than 4, use constant hight-costs, otherwise use \a pc. * \ingroup TaskActor */ PropCost cost_hi(int n, PropCost pc); /** * \defgroup TaskPropPat Propagator patterns * * The optional last Boolean argument (\a fd) to the constructor for creation * defines whether disposal must be forced. That is, if \a fd is true * the dispose member function of a propagator is called when the * space containing the propagator is deleted. * \ingroup TaskActor */ //@{ /** * \brief Unary propagator * * Stores single view of type \a View with propagation condition \a pc. */ template class UnaryPropagator : public Propagator { protected: /// Single view View x0; /// Constructor for cloning \a p UnaryPropagator(Space* home, bool share, UnaryPropagator& p); /// Constructor for rewriting \a p during cloning UnaryPropagator(Space* home, bool share, Propagator& p, View x0); /// Constructor for creation UnaryPropagator(Space* home, View x0, bool fd=false); public: /// Cost function (defined as PC_UNARY_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief Binary propagator * * Stores two views of type \a View with propagation condition \a pc. */ template class BinaryPropagator : public Propagator { protected: /// Two views View x0, x1; /// Constructor for cloning \a p BinaryPropagator(Space* home, bool share, BinaryPropagator& p); /// Constructor for creation BinaryPropagator(Space* home, View x0, View x1, bool fd=false); /// Constructor for rewriting \a p during cloning BinaryPropagator(Space* home, bool share, Propagator& p, View x0, View x1); public: /// Cost function (defined as PC_BINARY_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief Ternary propagator * * Stores three views of type \a View with propagation condition \a pc. */ template class TernaryPropagator : public Propagator { protected: /// Three views View x0, x1, x2; /// Constructor for cloning \a p TernaryPropagator(Space* home, bool share, TernaryPropagator& p); /// Constructor for creation TernaryPropagator(Space* home, View x0, View x1, View x2, bool fd=false); /// Constructor for rewriting \a p during cloning TernaryPropagator(Space* home, bool share, Propagator& p, View x0, View x1, View x2); public: /// Cost function (defined as PC_TERNARY_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief n-ary propagator * * Stores array of views of type \a View with propagation condition \a pc. */ template class NaryPropagator : public Propagator { protected: /// Array of views ViewArray x; /// Constructor for cloning \a p NaryPropagator(Space* home, bool share, NaryPropagator& p); /// Constructor for rewriting \a p during cloning NaryPropagator(Space* home, bool share, Propagator& p, ViewArray& x); /// Constructor for creation NaryPropagator(Space* home, ViewArray& x, bool fd=false); public: /// Cost function (defined as dynamic PC_LINEAR_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief (n+1)-ary propagator * * Stores array of views and single view of type \a View with propagation * condition \a pc. */ template class NaryOnePropagator : public Propagator { protected: /// Array of views ViewArray x; /// Single view View y; /// Constructor for cloning \a p NaryOnePropagator(Space* home, bool share, NaryOnePropagator& p); /// Constructor for rewriting \a p during cloning NaryOnePropagator(Space* home, bool share, Propagator& p, ViewArray& x, View y); /// Constructor for creation NaryOnePropagator(Space* home, ViewArray& x, View y, bool fd=false); public: /// Cost function (defined as dynamic PC_LINEAR_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief Inhomogeneous binary propagator * * Stores two views of type \a View0 and \a View1 with propagation * conditions \a pc0 and \a pc1. */ template class InhomBinaryPropagator : public Propagator { protected: View0 x0; View1 x1; /// Constructor for cloning InhomBinaryPropagator(Space* home,bool,InhomBinaryPropagator&); /// Constructor for creation InhomBinaryPropagator(Space* home,View0,View1,bool=false); /// Constructor for rewriting \a p during cloning InhomBinaryPropagator(Space* home, bool share, Propagator& p, View0 x0, View1 x1); public: /// Cost function (defined as PC_BINARY_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief Inhomogeneous ternary propagator * * Stores three views of type \a View0, \a View1, and \a View2 * with propagation conditions \a pc0, \a pc1, and \a pc2. */ template class InhomTernaryPropagator : public Propagator { protected: View0 x0; View1 x1; View2 x2; /// Constructor for cloning InhomTernaryPropagator(Space* home,bool,InhomTernaryPropagator&); /// Constructor for creation InhomTernaryPropagator(Space* home,View0,View1,View2,bool=false); /// Constructor for rewriting \a p during cloning InhomTernaryPropagator(Space* home, bool share, Propagator& p, View0 x0, View1 x1, View2 x2); public: /// Cost function (defined as PC_TERNARY_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; /** * \brief Inhomogeneous (n+1)-ary propagator * * Stores array of views of type \a View0 with propagation condition \a pc0 * and a single view of type \a View1 with propagation condition \a pc1. */ template class InhomNaryOnePropagator : public Propagator { protected: /// Array of views ViewArray x; /// Single view View1 y; /// Constructor for cloning \a p InhomNaryOnePropagator(Space* home, bool share, InhomNaryOnePropagator& p); /// Constructor for creation InhomNaryOnePropagator(Space* home, ViewArray& x, View1 y, bool fd=false); /// Constructor for rewriting \a p during cloning InhomNaryOnePropagator(Space* home, bool share, Propagator& p, ViewArray& x, View1 y); public: /// Cost function (defined as dynamic PC_LINEAR_LO) virtual PropCost cost(void) const; /// Delete propagator and return its size virtual size_t dispose(Space* home); }; //@} /* * Dynamic cost computation * */ forceinline PropCost cost_lo(int n, PropCost c) { if (n > 3) return c; if (n < 2) return PC_UNARY_LO; return (n > 2) ? PC_TERNARY_LO : PC_BINARY_LO; } forceinline PropCost cost_hi(int n, PropCost c) { if (n > 3) return c; if (n < 2) return PC_UNARY_HI; return (n > 2) ? PC_TERNARY_HI : PC_BINARY_HI; } /* * Unary propagators * */ template UnaryPropagator::UnaryPropagator (Space* home, View y0, bool fd) : Propagator(home,fd), x0(y0) { x0.subscribe(home,this,pc); } template forceinline UnaryPropagator::UnaryPropagator (Space* home, bool share, UnaryPropagator& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); } template forceinline UnaryPropagator::UnaryPropagator (Space* home, bool share, Propagator& p, View y0) : Propagator(home,share,p) { x0.update(home,share,y0); } template PropCost UnaryPropagator::cost(void) const { return PC_UNARY_LO; } template size_t UnaryPropagator::dispose(Space* home) { if (!home->failed()) x0.cancel(home,this,pc); (void) Propagator::dispose(home); return sizeof(*this); } /* * Binary propagators * */ template BinaryPropagator::BinaryPropagator (Space* home, View y0, View y1, bool fd) : Propagator(home,fd), x0(y0), x1(y1) { x0.subscribe(home,this,pc); x1.subscribe(home,this,pc); } template forceinline BinaryPropagator::BinaryPropagator (Space* home, bool share, BinaryPropagator& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); } template forceinline BinaryPropagator::BinaryPropagator (Space* home, bool share, Propagator& p, View y0, View y1) : Propagator(home,share,p) { x0.update(home,share,y0); x1.update(home,share,y1); } template PropCost BinaryPropagator::cost(void) const { return PC_BINARY_LO; } template size_t BinaryPropagator::dispose(Space* home) { if (!home->failed()) { x0.cancel(home,this,pc); x1.cancel(home,this,pc); } (void) Propagator::dispose(home); return sizeof(*this); } /* * Ternary propagators * */ template TernaryPropagator::TernaryPropagator (Space* home, View y0, View y1, View y2, bool fd) : Propagator(home,fd), x0(y0), x1(y1), x2(y2) { x0.subscribe(home,this,pc); x1.subscribe(home,this,pc); x2.subscribe(home,this,pc); } template forceinline TernaryPropagator::TernaryPropagator (Space* home, bool share, TernaryPropagator& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); x2.update(home,share,p.x2); } template forceinline TernaryPropagator::TernaryPropagator (Space* home, bool share, Propagator& p, View y0, View y1, View y2) : Propagator(home,share,p) { x0.update(home,share,y0); x1.update(home,share,y1); x2.update(home,share,y2); } template PropCost TernaryPropagator::cost(void) const { return PC_TERNARY_LO; } template size_t TernaryPropagator::dispose(Space* home) { if (!home->failed()) { x0.cancel(home,this,pc); x1.cancel(home,this,pc); x2.cancel(home,this,pc); } (void) Propagator::dispose(home); return sizeof(*this); } /* * Nary propagators * */ template NaryPropagator::NaryPropagator (Space* home, ViewArray& y, bool fd) : Propagator(home,fd), x(y) { x.subscribe(home,this,pc); } template forceinline NaryPropagator::NaryPropagator (Space* home, bool share, NaryPropagator& p) : Propagator(home,share,p) { x.update(home,share,p.x); } template forceinline NaryPropagator::NaryPropagator (Space* home, bool share, Propagator& p, ViewArray& x0) : Propagator(home,share,p) { x.update(home,share,x0); } template PropCost NaryPropagator::cost(void) const { return cost_lo(x.size(), PC_LINEAR_LO); } template size_t NaryPropagator::dispose(Space* home) { if (!home->failed()) x.cancel(home,this,pc); (void) Propagator::dispose(home); return sizeof(*this); } /* * NaryOne (one additional variable) propagators * */ template NaryOnePropagator::NaryOnePropagator (Space* home, ViewArray& x0, View y0, bool fd) : Propagator(home,fd), x(x0), y(y0) { x.subscribe(home,this,pc); y.subscribe(home,this,pc); } template forceinline NaryOnePropagator::NaryOnePropagator (Space* home, bool share, NaryOnePropagator& p) : Propagator(home,share,p) { x.update(home,share,p.x); y.update(home,share,p.y); } template forceinline NaryOnePropagator::NaryOnePropagator (Space* home, bool share, Propagator& p, ViewArray& x0, View y0) : Propagator(home,share,p) { x.update(home,share,x0); y.update(home,share,y0); } template PropCost NaryOnePropagator::cost(void) const { return cost_lo(x.size()+1, PC_LINEAR_LO); } template size_t NaryOnePropagator::dispose(Space* home) { if (!home->failed()) { x.cancel(home,this,pc); y.cancel(home,this,pc); } (void) Propagator::dispose(home); return sizeof(*this); } /* * Inhomogeneous binary propagators * */ template InhomBinaryPropagator::InhomBinaryPropagator (Space* home, View0 y0, View1 y1, bool fd) : Propagator(home,fd), x0(y0), x1(y1) { x0.subscribe(home,this,pc0); x1.subscribe(home,this,pc1); } template forceinline InhomBinaryPropagator::InhomBinaryPropagator (Space* home, bool share, InhomBinaryPropagator& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); } template forceinline InhomBinaryPropagator::InhomBinaryPropagator (Space* home, bool share, Propagator& p, View0 y0, View1 y1) : Propagator(home,share,p) { x0.update(home,share,y0); x1.update(home,share,y1); } template PropCost InhomBinaryPropagator::cost(void) const { return PC_BINARY_LO; } template size_t InhomBinaryPropagator::dispose(Space* home) { if (!home->failed()) { x0.cancel(home,this,pc0); x1.cancel(home,this,pc1); } (void) Propagator::dispose(home); return sizeof(*this); } /* * Inhomogeneous ternary propagators * */ template InhomTernaryPropagator:: InhomTernaryPropagator(Space* home, View0 y0, View1 y1, View2 y2, bool fd) : Propagator(home,fd), x0(y0), x1(y1), x2(y2) { x0.subscribe(home,this,pc0); x1.subscribe(home,this,pc1); x2.subscribe(home,this,pc2); } template forceinline InhomTernaryPropagator:: InhomTernaryPropagator(Space* home, bool share, InhomTernaryPropagator& p) : Propagator(home,share,p) { x0.update(home,share,p.x0); x1.update(home,share,p.x1); x2.update(home,share,p.x2); } template forceinline InhomTernaryPropagator::InhomTernaryPropagator (Space* home, bool share, Propagator& p, View0 y0, View1 y1, View2 y2) : Propagator(home,share,p) { x0.update(home,share,y0); x1.update(home,share,y1); x2.update(home,share,y2); } template PropCost InhomTernaryPropagator::cost(void) const { return PC_BINARY_LO; } template size_t InhomTernaryPropagator:: dispose(Space* home) { if (!home->failed()) { x0.cancel(home,this,pc0); x1.cancel(home,this,pc1); x2.cancel(home,this,pc2); } (void) Propagator::dispose(home); return sizeof(*this); } /* * InhomNaryOne (one additional variable) propagators * */ template InhomNaryOnePropagator::InhomNaryOnePropagator (Space* home, ViewArray& x0, View1 y0, bool fd) : Propagator(home,fd), x(x0), y(y0) { x.subscribe(home,this,pc0); y.subscribe(home,this,pc1); } template forceinline InhomNaryOnePropagator::InhomNaryOnePropagator (Space* home, bool share, InhomNaryOnePropagator& p) : Propagator(home,share,p) { x.update(home,share,p.x); y.update(home,share,p.y); } template forceinline InhomNaryOnePropagator::InhomNaryOnePropagator (Space* home, bool share, Propagator& p, ViewArray& x0, View1 y0) : Propagator(home,share,p) { x.update(home,share,x0); y.update(home,share,y0); } template PropCost InhomNaryOnePropagator::cost(void) const { return cost_lo(x.size()+1, PC_LINEAR_LO); } template size_t InhomNaryOnePropagator::dispose(Space* home) { if (!home->failed()) { x.cancel(home,this,pc0); y.cancel(home,this,pc1); } (void) Propagator::dispose(home); return sizeof(*this); } } // STATISTICS: kernel-other