Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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  : n(opt.size()), b(*this,n*n,0,n*n-1), q(*this,1,n) {
92 
93  // Constrain field to the fields that can attack a field
94  for (int i=0; i<n*n; i++)
95  dom(*this, b[i], attacked(i));
96 
97  // At most q queens can be placed
98  nvalues(*this, b, IRT_LQ, q);
99 
100  /*
101  * According to: P. R. J. Östergard and W. D. Weakley, Values
102  * of Domination Numbers of the Queen's Graph, Electronic Journal
103  * of Combinatorics, 8:1-19, 2001, for n <= 120, the minimal number
104  * of queens is either ceil(n/2) or ceil(n/2 + 1).
105  */
106  if (n <= 120)
107  dom(*this, q, (n+1)/2, (n+1)/2 + 1);
108 
109  branch(*this, b, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
110  // Try the easier solution first
111  branch(*this, q, INT_VAL_MAX());
112  }
113 
115  virtual IntVar cost(void) const {
116  return q;
117  }
118 
121  : IntMinimizeScript(share,s), n(s.n) {
122  b.update(*this, share, s.b);
123  q.update(*this, share, s.q);
124  }
125 
127  virtual Space*
128  copy(bool share) {
129  return new DominatingQueens(share,*this);
130  }
131 
133  virtual void
134  print(std::ostream& os) const {
135  os << "\tNumber of Queens: " << q << std::endl;
136  os << "\tBoard: " << b << std::endl;
137  if (b.assigned()) {
138  // Print a nice board
139  bool* placed = new bool[n*n];
140  for (int i=0; i<n*n; i++)
141  placed[i] = false;
142  for (int i=0; i<n*n; i++)
143  placed[b[i].val()] = true;
144  for (int j=0; j<n; j++) {
145  std::cout << "\t\t";
146  for (int i=0; i<n; i++)
147  std::cout << (placed[xy(i,j)] ? 'Q' : '.') << ' ';
148  std::cout << std::endl;
149  }
150  delete [] placed;
151  }
152  os << std::endl;
153  }
154 };
155 
159 int
160 main(int argc, char* argv[]) {
161  SizeOptions opt("DominatingQueens");
162  opt.size(7);
163  opt.solutions(0);
164  opt.parse(argc,argv);
165  IntMinimizeScript::run<DominatingQueens,BAB,SizeOptions>(opt);
166  return 0;
167 }
168 
169 // STATISTICS: example-any
170 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
DominatingQueens(const SizeOptions &opt)
The actual problem.
Options for scripts with additional size parameter
Definition: driver.hh:567
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:435
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:622
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.
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:243
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
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.