40 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST) 42 #if QT_VERSION >= 0x050000 88 :
Brancher(home, b), start(b.start) {
95 for (
int n=x.
size();
n--; ) {
99 start = x[start].val();
117 return new Choice(*
this, start, n);
123 return new Choice(*
this, pos, val);
137 std::ostream& o)
const {
139 o <<
"x[" << c.
pos <<
"] " 140 << ((a == 0) ?
"=" :
"!=")
154 return sizeof(*this);
177 int f(
int x,
int y)
const {
190 static const int moves[8][2] = {
191 {-2,-1}, {-2,1}, {-1,-2}, {-1,2}, {1,-2}, {1,2}, {2,-1}, {2,1}
193 int nbs[8];
int n_nbs = 0;
194 for (
int m=0; m<8; m++) {
195 int nx =
x(i) + moves[m][0], ny =
y(i) + moves[m][1];
196 if ((nx >= 0) && (nx < n) && (ny >= 0) && (ny < n))
197 nbs[n_nbs++] =
f(nx,ny);
203 :
Script(opt), n(opt.
size()), succ(*this,n*n,0,n*n-1) {
208 case BRANCH_WARNSDORFF:
220 int* jump =
new int[n*
n];
223 for (
int i=0;
i<n*
n;
i++) {
224 jump[j]=
i; j=succ[j].min();
228 for (
int i = 0;
i <
n;
i++) {
229 for (
int j = 0; j <
n; j++) {
231 os << jump[
f(
i,j)] <<
" ";
233 os << std::endl <<
"\t";
259 for (
int i = nn;
i--; ) {
261 IntVar j(*
this,0,nn-1); jump[
i]=j;
271 for (
int f = 0;
f < nn;
f++) {
275 expr(*
this, (jump[
i.val()]-jump[
f] == 1)),
277 expr(*
this, (jump[
i.val()]-jump[
f] == 1-nn)),
278 expr(*
this, (succ[
f] ==
i.val())));
279 dom(*
this, pred[
f], ds);
280 dom(*
this, succ[f], ds);
311 for (
int f = 0;
f <
n*
n;
f++)
312 dom(*
this, succ[
f], neighbors(f));
329 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST) 334 QGraphicsScene* scene;
338 static const int unit = 30;
341 KnightsInspector(
void) : scene(NULL), mw(NULL) {}
343 virtual void inspect(
const Space& s) {
348 QList <QGraphicsItem*> itemList = scene->items();
349 foreach (QGraphicsItem*
i, scene->items()) {
350 scene->removeItem(i);
354 for (
int i=0; i<k.
n; i++) {
355 for (
int j=0; j<k.
n; j++) {
356 scene->addRect(i*unit,j*unit,unit,unit);
358 QPen pen(Qt::black, 2);
360 pen.setColor(Qt::red);
361 pen.setStyle(Qt::DotLine);
365 int ky = xv.val() % k.
n;
366 int kx = xv.val() / k.
n;
367 scene->addLine(i*unit+unit/2,j*unit+unit/2,
368 kx*unit+unit/2,ky*unit+unit/2,
378 void initialize(
void) {
379 mw =
new QMainWindow();
380 scene =
new QGraphicsScene();
381 QGraphicsView* view =
new QGraphicsView(scene);
382 view->setRenderHints(QPainter::Antialiasing);
383 mw->setCentralWidget(view);
384 mw->setAttribute(Qt::WA_QuitOnClose,
false);
385 mw->setAttribute(Qt::WA_DeleteOnClose,
false);
386 QAction* closeWindow =
new QAction(
"Close window", mw);
387 closeWindow->setShortcut(QKeySequence(
"Ctrl+W"));
388 mw->connect(closeWindow, SIGNAL(triggered()),
390 mw->addAction(closeWindow);
394 virtual std::string name(
void) {
return "Board"; }
396 virtual void finalize(
void) {
418 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST) 423 opt.
parse(argc,argv);
426 Script::run<KnightsReified,DFS,SizeOptions>(
opt);
428 Script::run<KnightsCircuit,DFS,SizeOptions>(
opt);
Value iterator for integer variables.
int y(int f) const
Return y coordinate at field f.
void size(unsigned int s)
Set default size.
Knights(Knights &s)
Constructor for cloning s.
Options for scripts with additional size parameter
Warnsdorff(Home home, ViewArray< Int::IntView > &xv)
Construct brancher.
Use single circuit constraints.
virtual Space * copy(void)
Copy during cloning.
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
void update(Space &home, ViewArray< View > &a)
Update array to be a clone of array a.
const int n
Size of board.
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
virtual size_t dispose(Space &)
Delete brancher and return its size.
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
void propagation(int v)
Set default propagation value.
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Example: n-Knights tour (model using circuit)
bool assigned(void) const
Test if all variables are assigned.
bool pos(const View &x)
Test whether x is postive.
virtual Space * copy(void)
Copy during cloning.
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Multi _c(Gecode::IntArgs(3, 1, 2, 3))
class Gecode::Options::_I inspect
void circuit(Home home, int offset, const IntVarArgs &x, IntPropLevel ipl)
Post propagator such that x forms a circuit.
void ipl(IntPropLevel i)
Set default integer propagation level.
Value iterator for integer views.
int pos
Position of variable.
ViewArray< Int::IntView > x
Views of the brancher.
Abstract base class for inspectors.
Parametric base-class for scripts.
void iterations(unsigned int i)
Set default number of iterations.
int val(void) const
Return current value.
Base-class for both propagators and branchers.
Gecode::FloatVal c(-8, 8)
int val
Value of variable.
int p
Number of positive literals for node type.
int f(int x, int y) const
Return field at position x, y.
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual Gecode::Choice * choice(Space &)
Return choice.
Base-class for branchers.
Example: n-Knight's tour (simple model)
int n
Number of negative literals for node type.
Execution has resulted in failure.
KnightsCircuit(const SizeOptions &opt)
IntValBranch INT_VAL_MIN(void)
Select smallest value.
unsigned int size(I &i)
Size of all ranges of range iterator i.
void click(Gist::Inspector *i)
Add inspector that reacts on node double clicks.
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
static void post(Home home, const IntVarArgs &x)
Post brancher.
Knights(const SizeOptions &opt)
Constructor.
void branching(int v)
Set default branching value.
virtual bool status(const Space &) const
Check status of brancher, return true if alternatives left.
Passing integer variables.
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
struct Gecode::@585::NNF::@62::@63 b
For binary nodes (and, or, eqv)
KnightsReified(const SizeOptions &opt)
Post propagator for f(x \diamond_{\mathit{op}} y) \sim_r z \f$ void rel(Home home
Post propagator for SetVar SetOpType SetVar y
int main(int argc, char *argv[])
Main-function.
int x(int f) const
Return x coordinate at field f.
virtual void archive(Archive &e) const
Archive into e.
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
Choice for performing commit
int start
Next variable to branch on.
Base-class for knight's tour example.
Use Warnsdorff's rule.
bool assigned(View x, int v)
Whether x is assigned to value v.
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
virtual void print(const Space &, const Gecode::Choice &_c, unsigned int a, std::ostream &o) const
Print explanation.
Custom brancher for knight's tours using Warnsdorff's rule.
Value iterator for integer sets.
virtual void print(std::ostream &os) const
Print board.
Post propagator for SetVar x
virtual Actor * copy(Space &home)
Copy brancher.
Use naive, lexicographical branching.
IntSet neighbors(int i)
Compute set of neighbour fields.
Choice(const Brancher &b, int pos0, int val0)
virtual void archive(Archive &e) const
Archive into e.
virtual Choice * choice(const Space &, Archive &e)
Return choice.
Gecode toplevel namespace
int size(void) const
Return size of array (number of elements)
IntVarArray succ
Maps board field to successor field.
Home class for posting propagators
KnightsReified(KnightsReified &s)
Constructor for cloning s.
virtual ExecStatus commit(Space &home, const Gecode::Choice &_c, unsigned int a)
Perform commit for choice _c and alternative a.
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Warnsdorff(Space &home, Warnsdorff &b)
Copy constructor.
KnightsCircuit(KnightsCircuit &s)
Constructor for cloning s.