Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
38using namespace Gecode;
39
54protected:
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 }
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 }
84public:
87 : IntMinimizeScript(opt),
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
156int
157main(int argc, char* argv[]) {
158 SizeOptions opt("DominatingQueens");
159 opt.size(7);
160 opt.solutions(0);
161 opt.parse(argc,argv);
163 return 0;
164}
165
166// STATISTICS: example-any
167
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Example: Dominating Queens
IntVar q
Number of queens.
virtual Space * copy(void)
Perform copying during cloning.
int main(int argc, char *argv[])
Main-function.
int y(int xy) const
Compute y coordinate from pair xy.
DominatingQueens(const SizeOptions &opt)
The actual problem.
IntVarArray b
Fields on the board.
const int n
Size of the board.
int x(int xy) const
Compute x coordinate from pair xy.
DominatingQueens(DominatingQueens &s)
Constructor for cloning s.
virtual void print(std::ostream &os) const
Print solution.
int xy(int x, int y) const
Compute coordinate pair from x and y.
virtual IntVar cost(void) const
Return cost.
IntSet attacked(int xy)
Compute set of fields that can be attacked by xy.
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
Passing integer arguments.
Definition int.hh:628
Integer sets.
Definition int.hh:174
Integer variable array.
Definition int.hh:763
Integer variables.
Definition int.hh:371
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1742
bool assigned(void) const
Test if all variables are assigned.
Definition array.hpp:1026
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition array.hpp:1013
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition var.hpp:116
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
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
@ IRT_LQ
Less or equal ( )
Definition int.hh:928
Gecode toplevel namespace
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition dom.cpp:40
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
IntValBranch INT_VAL_MAX(void)
Select largest value.
Definition val.hpp:65
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:767
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition val.hpp:55
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition nvalues.cpp:40
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206
Post propagator for SetVar x
Definition set.hh:767