Generated on Sat Feb 7 2015 02:01:12 for Gecode by doxygen 1.8.9.1
queens.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2001
8  *
9  * Last modified:
10  * $Date: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 #include <gecode/minimodel.hh>
41 
42 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
43 #include <QtGui>
44 #if QT_VERSION >= 0x050000
45 #include <QtWidgets>
46 #endif
47 #endif
48 
49 using namespace Gecode;
50 
60 class Queens : public Script {
61 public:
65  enum {
68  PROP_DISTINCT
69  };
72  : q(*this,opt.size(),0,opt.size()-1) {
73  const int n = q.size();
74  switch (opt.propagation()) {
75  case PROP_BINARY:
76  for (int i = 0; i<n; i++)
77  for (int j = i+1; j<n; j++) {
78  rel(*this, q[i] != q[j]);
79  rel(*this, q[i]+i != q[j]+j);
80  rel(*this, q[i]-i != q[j]-j);
81  }
82  break;
83  case PROP_MIXED:
84  for (int i = 0; i<n; i++)
85  for (int j = i+1; j<n; j++) {
86  rel(*this, q[i]+i != q[j]+j);
87  rel(*this, q[i]-i != q[j]-j);
88  }
89  distinct(*this, q, opt.icl());
90  break;
91  case PROP_DISTINCT:
92  distinct(*this, IntArgs::create(n,0,1), q, opt.icl());
93  distinct(*this, IntArgs::create(n,0,-1), q, opt.icl());
94  distinct(*this, q, opt.icl());
95  break;
96  }
97  branch(*this, q, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
98  }
99 
101  Queens(bool share, Queens& s) : Script(share,s) {
102  q.update(*this, share, s.q);
103  }
104 
106  virtual Space*
107  copy(bool share) {
108  return new Queens(share,*this);
109  }
110 
112  virtual void
113  print(std::ostream& os) const {
114  os << "queens\t";
115  for (int i = 0; i < q.size(); i++) {
116  os << q[i] << ", ";
117  if ((i+1) % 10 == 0)
118  os << std::endl << "\t";
119  }
120  os << std::endl;
121  }
122 };
123 
124 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
125 class QueensInspector : public Gist::Inspector {
127 protected:
129  QGraphicsScene* scene;
131  QMainWindow* mw;
133  static const int unit = 20;
134 public:
136  QueensInspector(void) : scene(NULL), mw(NULL) {}
138  virtual void inspect(const Space& s) {
139  const Queens& q = static_cast<const Queens&>(s);
140 
141  if (!scene)
142  initialize();
143  QList <QGraphicsItem*> itemList = scene->items();
144  foreach (QGraphicsItem* i, scene->items()) {
145  scene->removeItem(i);
146  delete i;
147  }
148 
149  for (int i=0; i<q.q.size(); i++) {
150  for (int j=0; j<q.q.size(); j++) {
151  scene->addRect(i*unit,j*unit,unit,unit);
152  }
153  QBrush b(q.q[i].assigned() ? Qt::black : Qt::red);
154  QPen p(q.q[i].assigned() ? Qt::black : Qt::white);
155  for (IntVarValues xv(q.q[i]); xv(); ++xv) {
156  scene->addEllipse(QRectF(i*unit+unit/4,xv.val()*unit+unit/4,
157  unit/2,unit/2), p, b);
158  }
159  }
160  mw->show();
161  }
162 
164  void initialize(void) {
165  mw = new QMainWindow();
166  scene = new QGraphicsScene();
167  QGraphicsView* view = new QGraphicsView(scene);
168  view->setRenderHints(QPainter::Antialiasing);
169  mw->setCentralWidget(view);
170  mw->setAttribute(Qt::WA_QuitOnClose, false);
171  mw->setAttribute(Qt::WA_DeleteOnClose, false);
172  QAction* closeWindow = new QAction("Close window", mw);
173  closeWindow->setShortcut(QKeySequence("Ctrl+W"));
174  mw->connect(closeWindow, SIGNAL(triggered()),
175  mw, SLOT(close()));
176  mw->addAction(closeWindow);
177  }
178 
180  virtual std::string name(void) { return "Board"; }
182  virtual void finalize(void) {
183  delete mw;
184  mw = NULL;
185  }
186 };
187 
188 #endif /* GECODE_HAS_GIST */
189 
193 int
194 main(int argc, char* argv[]) {
195  SizeOptions opt("Queens");
196  opt.iterations(500);
197  opt.size(100);
199  opt.propagation(Queens::PROP_BINARY, "binary",
200  "only binary disequality constraints");
201  opt.propagation(Queens::PROP_MIXED, "mixed",
202  "single distinct and binary disequality constraints");
203  opt.propagation(Queens::PROP_DISTINCT, "distinct",
204  "three distinct constraints");
205 
206 #if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
207  QueensInspector ki;
208  opt.inspect.click(&ki);
209 #endif
210 
211  opt.parse(argc,argv);
212  Script::run<Queens,DFS,SizeOptions>(opt);
213  return 0;
214 }
215 
216 // STATISTICS: example-any
217 
Value iterator for integer variables.
Definition: int.hh:469
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition: array.hpp:72
Options for scripts with additional size parameter
Definition: driver.hh:567
void propagation(int v)
Set default propagation value.
Definition: options.hpp:181
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
IntVarArray q
Position of queens on boards.
Definition: queens.cpp:63
Integer variable array.
Definition: int.hh:741
Use only binary disequality constraints.
Definition: queens.cpp:66
Computation spaces.
Definition: core.hpp:1362
Abstract base class for inspectors.
Definition: gist.hh:103
Parametric base-class for scripts.
Definition: driver.hh:622
void iterations(unsigned int i)
Set default number of iterations.
Definition: options.hpp:385
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:236
Use three distinct constraints.
Definition: queens.cpp:68
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Options opt
The options.
Definition: test.cpp:101
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
unsigned int size(I &i)
Size of all ranges of range iterator i.
int main(int argc, char *argv[])
Main-function.
Definition: queens.cpp:194
virtual void print(std::ostream &os) const
Print solution.
Definition: queens.cpp:113
Use single distinct and binary disequality constraints.
Definition: queens.cpp:67
virtual Space * copy(bool share)
Perform copying during cloning.
Definition: queens.cpp:107
Queens(bool share, Queens &s)
Constructor for cloning s.
Definition: queens.cpp:101
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Queens(const SizeOptions &opt)
The actual problem.
Definition: queens.cpp:71
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
bool assigned(void) const
Test if all variables are assigned.
Definition: array.hpp:1085
BrancherHandle 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.
Definition: branch.cpp:43
Example: n-Queens puzzle
Definition: queens.cpp:60
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194