1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213
|
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 2001
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
#include <gecode/driver.hh>
#include <gecode/int.hh>
#include <gecode/minimodel.hh>
#if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
#include <QtGui>
#if QT_VERSION >= 0x050000
#include <QtWidgets>
#endif
#endif
using namespace Gecode;
/**
* \brief %Example: n-%Queens puzzle
*
* Place n queens on an n times n chessboard such that they do not
* attack each other.
*
* \ingroup Example
*
*/
class Queens : public Script {
public:
/// Position of queens on boards
IntVarArray q;
/// Propagation to use for model
enum {
PROP_BINARY, ///< Use only binary disequality constraints
PROP_MIXED, ///< Use single distinct and binary disequality constraints
PROP_DISTINCT ///< Use three distinct constraints
};
/// The actual problem
Queens(const SizeOptions& opt)
: Script(opt), q(*this,opt.size(),0,opt.size()-1) {
const int n = q.size();
switch (opt.propagation()) {
case PROP_BINARY:
for (int i = 0; i<n; i++)
for (int j = i+1; j<n; j++) {
rel(*this, q[i] != q[j]);
rel(*this, q[i]+i != q[j]+j);
rel(*this, q[i]-i != q[j]-j);
}
break;
case PROP_MIXED:
for (int i = 0; i<n; i++)
for (int j = i+1; j<n; j++) {
rel(*this, q[i]+i != q[j]+j);
rel(*this, q[i]-i != q[j]-j);
}
distinct(*this, q, opt.ipl());
break;
case PROP_DISTINCT:
distinct(*this, IntArgs::create(n,0,1), q, opt.ipl());
distinct(*this, IntArgs::create(n,0,-1), q, opt.ipl());
distinct(*this, q, opt.ipl());
break;
}
branch(*this, q, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
}
/// Constructor for cloning \a s
Queens(Queens& s) : Script(s) {
q.update(*this, s.q);
}
/// Perform copying during cloning
virtual Space*
copy(void) {
return new Queens(*this);
}
/// Print solution
virtual void
print(std::ostream& os) const {
os << "queens\t";
for (int i = 0; i < q.size(); i++) {
os << q[i] << ", ";
if ((i+1) % 10 == 0)
os << std::endl << "\t";
}
os << std::endl;
}
};
#if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
/// Inspector showing queens on a chess board
class QueensInspector : public Gist::Inspector {
protected:
/// The graphics scene displaying the board
QGraphicsScene* scene;
/// The window containing the graphics scene
QMainWindow* mw;
/// The size of a field on the board
static const int unit = 20;
public:
/// Constructor
QueensInspector(void) : scene(nullptr), mw(nullptr) {}
/// Inspect space \a s
virtual void inspect(const Space& s) {
const Queens& q = static_cast<const Queens&>(s);
if (!scene)
initialize();
QList <QGraphicsItem*> itemList = scene->items();
foreach (QGraphicsItem* i, scene->items()) {
scene->removeItem(i);
delete i;
}
for (int i=0; i<q.q.size(); i++) {
for (int j=0; j<q.q.size(); j++) {
scene->addRect(i*unit,j*unit,unit,unit);
}
QBrush b(q.q[i].assigned() ? Qt::black : Qt::red);
QPen p(q.q[i].assigned() ? Qt::black : Qt::white);
for (IntVarValues xv(q.q[i]); xv(); ++xv) {
scene->addEllipse(QRectF(i*unit+unit/4,xv.val()*unit+unit/4,
unit/2,unit/2), p, b);
}
}
mw->show();
}
/// Set up main window
void initialize(void) {
mw = new QMainWindow();
scene = new QGraphicsScene();
QGraphicsView* view = new QGraphicsView(scene);
view->setRenderHints(QPainter::Antialiasing);
mw->setCentralWidget(view);
mw->setAttribute(Qt::WA_QuitOnClose, false);
mw->setAttribute(Qt::WA_DeleteOnClose, false);
QAction* closeWindow = new QAction("Close window", mw);
closeWindow->setShortcut(QKeySequence("Ctrl+W"));
mw->connect(closeWindow, SIGNAL(triggered()),
mw, SLOT(close()));
mw->addAction(closeWindow);
}
/// Name of the inspector
virtual std::string name(void) { return "Board"; }
/// Finalize inspector
virtual void finalize(void) {
delete mw;
mw = nullptr;
}
};
#endif /* GECODE_HAS_GIST */
/** \brief Main-function
* \relates Queens
*/
int
main(int argc, char* argv[]) {
SizeOptions opt("Queens");
opt.iterations(500);
opt.size(100);
opt.propagation(Queens::PROP_DISTINCT);
opt.propagation(Queens::PROP_BINARY, "binary",
"only binary disequality constraints");
opt.propagation(Queens::PROP_MIXED, "mixed",
"single distinct and binary disequality constraints");
opt.propagation(Queens::PROP_DISTINCT, "distinct",
"three distinct constraints");
#if defined(GECODE_HAS_QT) && defined(GECODE_HAS_GIST)
QueensInspector ki;
opt.inspect.click(&ki);
#endif
opt.parse(argc,argv);
Script::run<Queens,DFS,SizeOptions>(opt);
return 0;
}
// STATISTICS: example-any
|