[gecode-users] A simple propagator

amina kemmar kemmami at hotmail.com
Sun Sep 6 14:13:46 CEST 2009


Hi everyone,

thank you Tias for the propagator's example that you make available in the mailing list,  it is very interesting, but i don't understand 

why you add the following statement, in the propagate method : 



x[i]=x[--n];


can we delete it?


if anyone have an idea could he give me an answer, thank you.


greetings,

Amina.

////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include <gecode/kernel.hh>
#include <gecode/int.hh>

namespace myprop {
  using namespace ::Gecode;
  using namespace ::Gecode::Int;

  /**
   * \brief Base-class for simple cleaner
   */
  template <class V>
  class CleanerBool : public Propagator {
  protected:
    /// Boolean views
    ViewArray<V> x;

    /// Constructor for creation
    CleanerBool(Space& home, ViewArray<V>& x);
    /// Constructor for cloning \a p
    CleanerBool(Space& home, bool share, CleanerBool& p);
  public:
    /// Cost function (defined as low linear)
    virtual PropCost cost(const Space& home, const ModEventDelta& med) const;
    /// Delete propagator and return its size
//   virtual size_t dispose(Space& home);
    /// Create copy during cloning
    virtual Actor* copy(Space& home, bool share);
    /// Perform propagation
    virtual ExecStatus propagate(Space& home, const ModEventDelta& med);
    /// Post propagator
    static ExecStatus post(Space& home, ViewArray<V>& x);
  };

  /*
   * propagator implementation
   */
  template <class V>
  CleanerBool<V>::CleanerBool(Space& home, ViewArray<V>& x0)
    :  Propagator(home), x(x0) {
    x.subscribe(home,*this,PC_INT_VAL);
  }

  template <class V>
  forceinline size_t
  CleanerBool<V>::dispose(Space& home) {
    assert(!home->failed());
    x.cancel(home,*this,PC_INT_VAL);
    (void) Propagator::dispose(home);
    return sizeof(*this);
  }

  template <class V>
  forceinline
  CleanerBool<V>::CleanerBool(Space& home, bool share, CleanerBool& p)
    : Propagator(home,share,p) {
    x.update(home,share,p.x);
  }

  template <class V>
  PropCost
  CleanerBool<V>::cost(const Space&, const ModEventDelta&) const {
    // make sure this propagator is run as last before the fixpoint
    return PropCost::crazy(PropCost::HI, 100000);
    // in a real propagator, this would be something more sensible,
    // for example: PropCost::linear(PropCost::LO, x.size());
  }

  template <class V>
  ExecStatus
  CleanerBool<V>::post(Space& home, ViewArray<V>& x) {
    (void) new (home) CleanerBool<V>(home,x);
    return ES_OK;
  }

  template <class V>
  Actor*
  CleanerBool<V>::copy(Space& home, bool share) {
    return new (home) CleanerBool<V>(home,share,*this);
  }

  template <class V>
  ExecStatus
  CleanerBool<V>::propagate(Space& home, const ModEventDelta&) {
    int n = x.size();
    for (int i = n; i--; ) {
      if (x[i].zero()) {
        x[i]=x[--n];
      } else if (x[i].one()) {
        x[i]=x[--n];
      } else {
        GECODE_ME_CHECK( x[i].one(home) );
        x[i]=x[--n];
      } // all cases covered, so every var assigned and removed
    }
    x.size(n);

    if (n == 0) {
      return ES_SUBSUMED(*this,home);
    }
    return ES_FIX;
  }


  /** \brief Post propagator for cleanup of unassigned variables
   * Is run as last propagator (crazy_hi), sets unassigned variables to 1
   */
  void cleaner(Space& home, const BoolVarArgs& x) {
    if (home.failed()) return;

    ViewArray<BoolView> Vx(home, x);

    GECODE_ES_FAIL(home,(CleanerBool<BoolView>::post(home, Vx)));
  }

}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////



_________________________________________________________________
Tchattez en direct en en vidéo avec vos amis !
http://www.windowslive.fr/messenger/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.ps.uni-sb.de/pipermail/users/attachments/20090906/c9e2c1ea/attachment.htm>


More information about the gecode-users mailing list