queens.cpp
Go to the documentation of this file.
/* -*- 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;
class Queens : public Script {
public:
IntVarArray q;
enum {
PROP_BINARY,
PROP_MIXED,
PROP_DISTINCT
};
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());
}
Queens(Queens& s) : Script(s) {
q.update(*this, s.q);
}
virtual Space*
copy(void) {
return new Queens(*this);
}
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)
class QueensInspector : public Gist::Inspector {
protected:
QGraphicsScene* scene;
QMainWindow* mw;
static const int unit = 20;
public:
QueensInspector(void) : scene(NULL), mw(NULL) {}
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();
}
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);
}
virtual std::string name(void) { return "Board"; }
virtual void finalize(void) {
delete mw;
mw = NULL;
}
};
#endif /* GECODE_HAS_GIST */
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
Gecode::Driver::ScriptBase< Driver::IgnoreStepOption< Space > >::run
static void run(const Options &opt, Script *s=NULL)
Gecode::Gist::Inspector
Abstract base class for inspectors.
Definition gist.hh:99
Gecode::Gist::Inspector::inspect
virtual void inspect(const Space &s)=0
Call-back function.
Gecode::Gist::Inspector::finalize
virtual void finalize(void)
Clean up when Gist exits.
Definition gist.cpp:48
Gecode::Gist::Inspector::name
virtual std::string name(void)
Name of the inspector.
Definition gist.cpp:45
Gecode::IntArgs::create
static IntArgs create(int n, int start, int inc=1)
Allocate array with n elements such that for all .
Definition array.hpp:76
Gecode::IntVarArray
Integer variable array.
Definition int.hh:763
Gecode::SizeOptions
Options for scripts with additional size parameter
Definition driver.hh:675
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::VarArray::assigned
bool assigned(void) const
Test if all variables are assigned.
Definition array.hpp:1026
Gecode::VarArray::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:926
Queens::copy
virtual Space * copy(void)
Perform copying during cloning.
Definition queens.cpp:103
Queens::main
int main(int argc, char *argv[])
Main-function.
Definition queens.cpp:190
Queens::Queens
Queens(const SizeOptions &opt)
The actual problem.
Definition queens.cpp:67
Queens::Queens
Queens(Queens &s)
Constructor for cloning s.
Definition queens.cpp:97
Queens::print
virtual void print(std::ostream &os) const
Print solution.
Definition queens.cpp:109
Queens::q
IntVarArray q
Position of queens on boards.
Definition queens.cpp:59
Queens::PROP_DISTINCT
@ PROP_DISTINCT
Use three distinct constraints.
Definition queens.cpp:64
Queens::PROP_BINARY
@ PROP_BINARY
Use only binary disequality constraints.
Definition queens.cpp:62
Queens::PROP_MIXED
@ PROP_MIXED
Use single distinct and binary disequality constraints.
Definition queens.cpp:63
Test::Options::parse
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
driver.hh
int.hh
Gecode::Script
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
Gecode::branch
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
Gecode::rel
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
minimodel.hh
Gecode
Gecode toplevel namespace
Gecode::distinct
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
Gecode::INT_VAL_MIN
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
Gecode::INT_VAR_SIZE_MIN
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206
Test::Float::Arithmetic::b
Gecode::FloatVal b(9, 12)
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})