Generated on Sat May 25 2013 18:00:32 for Gecode by doxygen 1.8.3.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: 2012-12-21 01:48:30 +0100 (Fri, 21 Dec 2012) $ by $Author: tack $
11  * $Revision: 13214 $
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 
126 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