[gecode-users] my first propagator - Nq

Kilian Sprotte ml13 at onlinehome.de
Sat Feb 10 14:33:45 CET 2007


Hi,

I have written my first propagator... :)

Its basically a translation of Nq in the java Queens example.
I hope it is correct, I am posting it here, in case anyone might
find it useful as a starting point.

The class is below - you can post it with:

if (Nq::post(home, view1, view2) < ES_OK)
   home->fail();

or (if you are inside a void function)

GECODE_ES_FAIL(home, Nq::post(s, view1, view2));

Cheers,
   Kilian

--

using namespace Gecode;

class Nq : public BinaryPropagator<Int::IntView, Int::PC_INT_VAL> {
public:
   Nq(Space* home, Int::IntView x0, Int::IntView x1)
     : BinaryPropagator<Int::IntView,Int::PC_INT_VAL>(home,x0,x1) {
   }
   Nq(Space* home, bool share, Nq& p)
     : BinaryPropagator<Int::IntView,Int::PC_INT_VAL>(home,share,p) {
   }
   Actor* copy(Space* home, bool share) {
     return new (home) Nq(home, share,*this);
   }
   PropCost cost () {
     return PC_BINARY_HI;
   }
   ExecStatus propagate (Space* home) {
   std::cout << "propagate head: "<< x0 << " " << x1 << "\n";
   if (x0.assigned()) {
      GECODE_ME_CHECK(x1.nq(home,x0.min()));
      std::cout << "propagate subsumed: "<< x0 << " " << x1 << "\n";
      return ES_SUBSUMED;
   } else { // x1 is assigned
      GECODE_ME_CHECK(x0.nq(home,x1.min()));
      std::cout << "propagate subsumed: "<< x0 << " " << x1 << "\n";
      return ES_SUBSUMED;
     }
   }
   static ExecStatus post (Space* home, Int::IntView x0, Int::IntView  
x1) {
     if (x0.assigned() && x1.assigned() && (x0.min()==x1.min())) {
       return ES_FAILED;
     }
     (void) new (home) Nq(home, x0, x1);
     return ES_OK;
   }
};






More information about the gecode-users mailing list