Generated on Tue May 31 2016 10:19:11 for Gecode by doxygen 1.8.11
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  * Last modified:
10  * $Date: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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 using namespace Gecode;
43 
58 protected:
60  const int n;
66  int xy(int x, int y) const {
67  return y*n + x;
68  }
70  int x(int xy) const {
71  return xy % n;
72  }
74  int y(int xy) const {
75  return xy / n;
76  }
78  IntSet attacked(int xy) {
79  IntArgs a;
80  for (int i=0; i<n; i++)
81  for (int j=0; j<n; j++)
82  if ((i == x(xy)) || // Same row
83  (j == y(xy)) || // Same column
84  (abs(i-x(xy)) == abs(j-y(xy)))) // Same diagonal
85  a << DominatingQueens::xy(i,j);
86  return IntSet(a);
87  }
88 public:
91  : IntMinimizeScript(opt),
92  n(opt.size()), b(*this,n*n,0,n*n-1), q(*this,1,n) {
93 
94  // Constrain field to the fields that can attack a field
95  for (int i=0; i<n*n; i++)
96  dom(*this, b[i], attacked(i));
97 
98  // At most q queens can be placed
99  nvalues(*this, b, IRT_LQ, q);
100 
101  /*
102  * According to: P. R. J. Östergard and W. D. Weakley, Values
103  * of Domination Numbers of the Queen's Graph, Electronic Journal
104  * of Combinatorics, 8:1-19, 2001, for n <= 120, the minimal number
105  * of queens is either ceil(n/2) or ceil(n/2 + 1).
106  */
107  if (n <= 120)
108  dom(*this, q, (n+1)/2, (n+1)/2 + 1);
109 
110  branch(*this, b, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
111  // Try the easier solution first
112  branch(*this, q, INT_VAL_MAX());
113  }
114 
116  virtual IntVar cost(void) const {
117  return q;
118  }
119 
122  : IntMinimizeScript(share,s), n(s.n) {
123  b.update(*this, share, s.b);
124  q.update(*this, share, s.q);
125  }
126 
128  virtual Space*
129  copy(bool share) {
130  return new DominatingQueens(share,*this);
131  }
132 
134  virtual void
135  print(std::ostream& os) const {
136  os << "\tNumber of Queens: " << q << std::endl;
137  os << "\tBoard: " << b << std::endl;
138  if (b.assigned()) {
139  // Print a nice board
140  bool* placed = new bool[n*n];
141  for (int i=0; i<n*n; i++)
142  placed[i] = false;
143  for (int i=0; i<n*n; i++)
144  placed[b[i].val()] = true;
145  for (int j=0; j<n; j++) {
146  std::cout << "\t\t";
147  for (int i=0; i<n; i++)
148  std::cout << (placed[xy(i,j)] ? 'Q' : '.') << ' ';
149  std::cout << std::endl;
150  }
151  delete [] placed;
152  }
153  os << std::endl;
154  }
155 };
156 
160 int
161 main(int argc, char* argv[]) {
162  SizeOptions opt("DominatingQueens");
163  opt.size(7);
164  opt.solutions(0);
165  opt.parse(argc,argv);
166  IntMinimizeScript::run<DominatingQueens,BAB,SizeOptions>(opt);
167  return 0;
168 }
169 
170 // STATISTICS: example-any
171 
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
DominatingQueens(const SizeOptions &opt)
The actual problem.
Options for scripts with additional size parameter
Definition: driver.hh:579
int x(int xy) const
Compute x coordinate from pair xy.
virtual IntVar cost(void) const
Return cost.
void update(Space &home, bool share, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:128
IntVar q
Number of queens.
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:49
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:437
Less or equal ( )
Definition: int.hh:906
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:44
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
const int n
Size of the board.
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:633
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
IntVarArray b
Fields on the board.
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
virtual Space * copy(bool share)
Perform copying during cloning.
DominatingQueens(bool share, DominatingQueens &s)
Constructor for cloning s.
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.
Example: Dominating Queens
virtual void print(std::ostream &os) const
Print solution.
struct Gecode::@519::NNF::@60::@62 a
For atomic nodes.
Integer sets.
Definition: int.hh:171
Passing integer arguments.
Definition: int.hh:607
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.
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition: val.hpp:78
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Integer variables.
Definition: int.hh:350
int xy(int x, int y) const
Compute coordinate pair from x and y.
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:261
int main(int argc, char *argv[])
Main-function.
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
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntConLevel)
Post propagator for .
Definition: nvalues.cpp:44