Generated on Wed Jan 1 2020 10:37:59 for Gecode by doxygen 1.8.16
dominating-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, 2011
8  *
9  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 #include <gecode/minimodel.hh>
37 
38 using namespace Gecode;
39 
54 protected:
56  const int n;
62  int xy(int x, int y) const {
63  return y*n + x;
64  }
66  int x(int xy) const {
67  return xy % n;
68  }
70  int y(int xy) const {
71  return xy / n;
72  }
74  IntSet attacked(int xy) {
75  IntArgs a;
76  for (int i=0; i<n; i++)
77  for (int j=0; j<n; j++)
78  if ((i == x(xy)) || // Same row
79  (j == y(xy)) || // Same column
80  (abs(i-x(xy)) == abs(j-y(xy)))) // Same diagonal
81  a << DominatingQueens::xy(i,j);
82  return IntSet(a);
83  }
84 public:
88  n(opt.size()), b(*this,n*n,0,n*n-1), q(*this,1,n) {
89 
90  // Constrain field to the fields that can attack a field
91  for (int i=0; i<n*n; i++)
92  dom(*this, b[i], attacked(i));
93 
94  // At most q queens can be placed
95  nvalues(*this, b, IRT_LQ, q);
96 
97  /*
98  * According to: P. R. J. Östergard and W. D. Weakley, Values
99  * of Domination Numbers of the Queen's Graph, Electronic Journal
100  * of Combinatorics, 8:1-19, 2001, for n <= 120, the minimal number
101  * of queens is either ceil(n/2) or ceil(n/2 + 1).
102  */
103  if (n <= 120)
104  dom(*this, q, (n+1)/2, (n+1)/2 + 1);
105 
106  branch(*this, b, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
107  // Try the easier solution first
108  branch(*this, q, INT_VAL_MAX());
109  }
110 
112  virtual IntVar cost(void) const {
113  return q;
114  }
115 
118  : IntMinimizeScript(s), n(s.n) {
119  b.update(*this, s.b);
120  q.update(*this, s.q);
121  }
122 
124  virtual Space*
125  copy(void) {
126  return new DominatingQueens(*this);
127  }
128 
130  virtual void
131  print(std::ostream& os) const {
132  os << "\tNumber of Queens: " << q << std::endl;
133  os << "\tBoard: " << b << std::endl;
134  if (b.assigned()) {
135  // Print a nice board
136  bool* placed = new bool[n*n];
137  for (int i=0; i<n*n; i++)
138  placed[i] = false;
139  for (int i=0; i<n*n; i++)
140  placed[b[i].val()] = true;
141  for (int j=0; j<n; j++) {
142  std::cout << "\t\t";
143  for (int i=0; i<n; i++)
144  std::cout << (placed[xy(i,j)] ? 'Q' : '.') << ' ';
145  std::cout << std::endl;
146  }
147  delete [] placed;
148  }
149  os << std::endl;
150  }
151 };
152 
156 int
157 main(int argc, char* argv[]) {
158  SizeOptions opt("DominatingQueens");
159  opt.size(7);
160  opt.solutions(0);
161  opt.parse(argc,argv);
162  IntMinimizeScript::run<DominatingQueens,BAB,SizeOptions>(opt);
163  return 0;
164 }
165 
166 // STATISTICS: example-any
167 
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
Post propagator for SetVar x
Definition: set.hh:767
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
int x(int xy) const
Compute x coordinate from pair xy.
unsigned int size(I &i)
Size of all ranges of range iterator i.
DominatingQueens(const SizeOptions &opt)
The actual problem.
int main(int argc, char *argv[])
Main-function.
virtual Space * copy(void)
Perform copying during cloning.
virtual IntVar cost(void) const
Return cost.
const int n
Size of the board.
Computation spaces.
Definition: core.hpp:1742
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.
Definition: branch.cpp:39
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:55
Integer variable array.
Definition: int.hh:763
IntVarArray b
Fields on the board.
IntVar q
Number of queens.
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel)
Post propagator for .
Definition: nvalues.cpp:40
Gecode toplevel namespace
virtual void print(std::ostream &os) const
Print solution.
Integer sets.
Definition: int.hh:174
Example: Dominating Queens
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
Options opt
The options.
Definition: test.cpp:97
Parametric base-class for scripts.
Definition: driver.hh:729
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:40
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
Integer variables.
Definition: int.hh:371
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:41
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:206
IntSet attacked(int xy)
Compute set of fields that can be attacked by xy.
int y(int xy) const
Compute y coordinate from pair xy.
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:116
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
int xy(int x, int y) const
Compute coordinate pair from x and y.
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Passing integer arguments.
Definition: int.hh:628
DominatingQueens(DominatingQueens &s)
Constructor for cloning s.
Gecode::IntArgs i({1, 2, 3, 4})
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:65
Less or equal ( )
Definition: int.hh:928
Options for scripts with additional size parameter
Definition: driver.hh:675