[gecode-users] my first propagator - Nq
Christian Schulte
cschulte at kth.se
Mon Feb 12 10:05:58 CET 2007
Congratulations!
Maybe some comments. Never do this:
if (Nq::post(home, view1, view2) < ES_OK)
home->fail();
as you should not rely on the order of ES_* (actually, they will change in
Gecode 2.0)! The other way is safe, or test for == ES_FAILED. (No matter
what the macros themselves do, as their implementation will change
consistently with changes to ExecStatus).
The cost you give is way too high: you know that if the propagator is
invoked, it will be subsumed. So, PC_UNARY_LO is more like it.
If you assume that an integer view x is assigned, us x.val() rather than
x.min(); it will have an assertion that it is really assumed.
Posting can be smarter: if you know that just one of the views is assigned,
you can immediately do the propagation.
Cheers
Christian
--
Christian Schulte, http://www.imit.kth.se/~schulte/
-----Original Message-----
From: users-bounces at gecode.org [mailto:users-bounces at gecode.org] On Behalf
Of Kilian Sprotte
Sent: Saturday, February 10, 2007 2:34 PM
To: users at gecode.org
Subject: [gecode-users] my first propagator - Nq
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;
}
};
_______________________________________________
Gecode users mailing list
users at gecode.org https://www.gecode.org/mailman/listinfo/gecode-users
More information about the gecode-users
mailing list