Generated on for Gecode by doxygen 1.17.0
queen-armies.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2006
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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
35#include <gecode/driver.hh>
36#include <gecode/int.hh>
37#include <gecode/minimodel.hh>
38#include <gecode/set.hh>
39
40using namespace Gecode;
41
47
68public:
69 const int n;
71 W;
73 b;
75
77 enum {
80 };
81
85 n(opt.size()),
86 U(*this, IntSet::empty, IntSet(0, n*n)),
87 W(*this, IntSet::empty, IntSet(0, n*n)),
88 w(*this, n*n, 0, 1),
89 b(*this, n*n, 0, 1),
90 q(*this, 0, n*n)
91 {
92 // Basic rules of the model
93 for (int i = n*n; i--; ) {
94 // w[i] means that no blacks are allowed on A[i]
95 rel(*this, w[i] == (U || A[i]));
96 // Make sure blacks and whites are disjoint.
97 rel(*this, !w[i] || !b[i]);
98 // If i in U, then b[i] has a piece.
99 rel(*this, b[i] == (singleton(i) <= U));
100 }
101
102 // Connect optimization variable to number of pieces
103 linear(*this, w, IRT_EQ, q);
104 linear(*this, b, IRT_GQ, q);
105
106 // Connect cardinality of U to the number of black pieces.
107 IntVar unknowns = expr(*this, cardinality(U));
108 rel(*this, q <= unknowns);
109 linear(*this, b, IRT_EQ, unknowns);
110
111 if (opt.branching() == BRANCH_NAIVE) {
112 branch(*this, w, BOOL_VAR_NONE(), BOOL_VAL_MAX());
113 branch(*this, b, BOOL_VAR_NONE(), BOOL_VAL_MAX());
114 } else {
115 QueenBranch::post(*this);
116 assign(*this, b, BOOL_ASSIGN_MAX());
117 }
118 }
119
121 : IntMaximizeScript(s), n(s.n) {
122 U.update(*this, s.U);
123 W.update(*this, s.W);
124 w.update(*this, s.w);
125 b.update(*this, s.b);
126 q.update(*this, s.q);
127 }
128
129 virtual Space*
130 copy(void) {
131 return new QueenArmies(*this);
132 }
133
134 virtual IntVar cost(void) const {
135 return q;
136 }
137
138 virtual void
139 print(std::ostream& os) const {
140 os << '\t';
141 for (int i = 0; i < n*n; ++i) {
142 if (w[i].assigned() && w[i].val()) os << "W";
143 else if (b[i].assigned() && b[i].val()) os << "B";
144 else if (!w[i].assigned() && !b[i].assigned()) os << " ";
145 else os << ".";
146 if ((i+1)%n == 0) os << std::endl << (i!=(n*n-1)?"\t":"");
147 }
148 os << "Number of white queens: " << q << std::endl << std::endl;
149 }
150
158 class QueenBranch : public Brancher {
159 private:
161 mutable int start;
163 class Choice : public Gecode::Choice {
164 public:
166 int pos;
168 bool val;
172 Choice(const Brancher& b, int pos0, bool val0)
173 : Gecode::Choice(b,2), pos(pos0), val(val0) {}
175 virtual void archive(Archive& e) const {
177 e << pos << val;
178 }
179 };
180
182 QueenBranch(Home home)
183 : Brancher(home), start(0) {}
185 QueenBranch(Space& home, QueenBranch& b)
186 : Brancher(home, b), start(b.start) {}
187
188 public:
190 virtual bool status(const Space& home) const {
191 const QueenArmies& q = static_cast<const QueenArmies&>(home);
192 for (int i = start; i < q.n*q.n; ++i)
193 if (!q.w[i].assigned()) {
194 start = i;
195 return true;
196 }
197 // No non-assigned orders left
198 return false;
199 }
200
201 virtual Gecode::Choice* choice(Space& home) {
202 const QueenArmies& q = static_cast<const QueenArmies&>(home);
203 int maxsize = -1;
204 int pos = -1;
205
206 for (int i = start; i < q.n*q.n; ++i) {
207 if (q.w[i].assigned()) continue;
208 IntSetRanges ai(A[i]);
211 int size = Iter::Ranges::size(r);
212 if (size > maxsize) {
213 maxsize = size;
214 pos = i;
215 }
216 }
217
218 assert(pos != -1);
219 return new Choice(*this, pos, true);
220 }
221
222 virtual Choice* choice(const Space&, Archive& e) {
223 int pos, val;
224 e >> pos >> val;
225 return new Choice(*this, pos, val);
226 }
227
230 virtual ExecStatus commit(Space& home, const Gecode::Choice& _c,
231 unsigned int a) {
232 QueenArmies& q = static_cast<QueenArmies&>(home);
233 const Choice& c = static_cast<const Choice&>(_c);
234 bool val = (a == 0) ? c.val : !c.val;
235 return me_failed(Int::BoolView(q.w[c.pos]).eq(q, val))
236 ? ES_FAILED
237 : ES_OK;
238 }
239
240 virtual void print(const Space&, const Gecode::Choice& _c,
241 unsigned int a,
242 std::ostream& o) const {
243 const Choice& c = static_cast<const Choice&>(_c);
244 bool val = (a == 0) ? c.val : !c.val;
245 o << "w[" << c.pos << "] = " << val;
246 }
247
248 virtual Actor* copy(Space& home) {
249 return new (home) QueenBranch(home, *this);
250 }
251
252 static void post(QueenArmies& home) {
253 (void) new (home) QueenBranch(home);
254 }
255
256 virtual size_t dispose(Space&) {
257 return sizeof(*this);
258 }
259 };
260};
261
263int pos(int i, int j, int n) {
264 return i*n + j;
265}
266
268int
269main(int argc, char* argv[]) {
270 SizeOptions opt("QueenArmies");
271 opt.size(6);
272 opt.branching(QueenArmies::BRANCH_SPECIFIC);
273 opt.branching(QueenArmies::BRANCH_NAIVE, "naive");
274 opt.branching(QueenArmies::BRANCH_SPECIFIC, "specific");
275 opt.solutions(0);
276 opt.parse(argc,argv);
277
278 // Set up the A-sets
279 // A[i] will contain the values attacked by a queen at position i
280 int n = opt.size();
281 A = new IntSet[n*n];
282 int *p = new int[std::max(n*n, 25)];
283 int pn = 0;
284 for (int i = n; i--; ) {
285 for (int j = n; j--; ) {
286 int dir[][2] = {
287 { 0, 1},
288 { 1, 1},
289 { 1, 0},
290 { 0, -1},
291 {-1, -1},
292 {-1, 0},
293 { 1, -1},
294 {-1, 1}
295 };
296 p[pn++] = pos(i, j, n);
297 for (int k = 8; k--; ) {
298 for (int l = 0; l < n
299 && 0 <= (i+l*dir[k][0]) && (i+l*dir[k][0]) < n
300 && 0 <= (j+l*dir[k][1]) && (j+l*dir[k][1]) < n; ++l) {
301 p[pn++] = pos(i+l*dir[k][0], j+l*dir[k][1], n);
302 }
303 }
304
305 A[pos(i, j, n)] = IntSet(p, pn);
306
307 pn = 0;
308 }
309 }
310 delete [] p;
311
313 return 0;
314}
315
316// STATISTICS: example-any
friend class Brancher
Definition core.hpp:640
Archive representation
Definition archive.hpp:42
Boolean variable array.
Definition int.hh:839
friend class Space
Definition core.hpp:1455
Brancher(Home home)
Constructor for creation.
Definition core.hpp:3714
friend class Choice
Definition core.hpp:1456
Choice for performing commit
Definition core.hpp:1423
virtual void archive(Archive &e) const
Archive into e.
static void run(const Options &opt, Script *s=nullptr)
Home class for posting propagators
Definition core.hpp:863
Range iterator for integer sets.
Definition int.hh:310
Integer sets.
Definition int.hh:178
Integer variables.
Definition int.hh:389
Boolean view for Boolean variables.
Definition view.hpp:1378
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition bool.hpp:170
Range iterator for computing intersection (binary).
Iterator for the unknown ranges of a set variable.
Definition set.hh:337
Set variables
Definition set.hh:127
Options for scripts with additional size parameter
Definition driver.hh:723
Computation spaces.
Definition core.hpp:1775
virtual Gecode::Choice * choice(Space &home)
Return choice.
virtual void print(const Space &, const Gecode::Choice &_c, unsigned int a, std::ostream &o) const
Print explanation.
virtual ExecStatus commit(Space &home, const Gecode::Choice &_c, unsigned int a)
Perform commit for choice _c and alternative a.
virtual size_t dispose(Space &)
Delete brancher and return its size.
virtual bool status(const Space &home) const
Check status of brancher, return true if alternatives left.
static void post(QueenArmies &home)
Post brancher.
virtual Choice * choice(const Space &, Archive &e)
Return choice.
virtual Actor * copy(Space &home)
Copy brancher during cloning.
QueenArmies(const SizeOptions &opt)
Constructor.
int main(int argc, char *argv[])
Main-function.
int pos(int i, int j, int n)
Position of a piece in a square board.
virtual void print(std::ostream &os) const
Print solution.
BoolVarArray w
The placement of the white queens.
BoolVarArray b
The placement of the black queens.
QueenArmies(QueenArmies &s)
Constructor for cloning.
virtual IntVar cost(void) const
Return solution cost.
IntVar q
The number of white queens placed.
@ BRANCH_NAIVE
Choose variables left to right.
@ BRANCH_SPECIFIC
Choose variable with problem specific strategy.
SetVar W
Set of squares occupied by white queens.
virtual Space * copy(void)
Return copy during cloning.
const int n
SetVar U
Set of un-attacked squares.
IntSet * A
Position of a piece in a square board.
void parse(int argc, char *argv[])
Parse commandline arguments.
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
Driver::ScriptBase< Driver::IgnoreStepOption< IntMaximizeSpace > > IntMaximizeScript
Base-class for scripts for finding solution of highest integer cost.
Definition driver.hh:862
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Assign all x with variable selection vars and value selection vals.
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.
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
@ IRT_EQ
Equality ( ).
Definition int.hh:960
@ IRT_GQ
Greater or equal ( ).
Definition int.hh:964
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode toplevel namespace
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Definition assign.hpp:105
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
BoolVarBranch BOOL_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:364
BoolValBranch BOOL_VAL_MAX(void)
Select largest value.
Definition val.hpp:135
SetExpr singleton(const LinIntExpr &)
Singleton expression.
ExecStatus
Definition core.hpp:479
@ ES_OK
Execution is okay.
Definition core.hpp:483
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:481
LinIntExpr cardinality(const SetExpr &)
Cardinality of set expression.