Generated on Thu Jan 20 2022 00:00:00 for Gecode by doxygen 1.9.1
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 Lagerkvist <lagerkvist@gecode.org>
5  *
6  * Copyright:
7  * Mikael Lagerkvist, 2006
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 
35 #include <gecode/driver.hh>
36 #include <gecode/int.hh>
37 #include <gecode/minimodel.hh>
38 #include <gecode/set.hh>
39 
40 using namespace Gecode;
41 
47 
68 public:
69  const int n;
71  W;
73  b;
75 
77  enum {
79  BRANCH_SPECIFIC
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  }
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  }
129  virtual Space*
130  copy(void) {
131  return new QueenArmies(*this);
132  }
134  virtual IntVar cost(void) const {
135  return q;
136  }
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  }
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]);
209  SetVarUnknownRanges qU(q.U);
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  }
222  virtual Choice* choice(const Space&, Archive& e) {
223  int pos, val;
224  e >> pos >> val;
225  return new Choice(*this, pos, val);
226  }
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  }
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  }
248  virtual Actor* copy(Space& home) {
249  return new (home) QueenBranch(home, *this);
250  }
252  static void post(QueenArmies& home) {
253  (void) new (home) QueenBranch(home);
254  }
256  virtual size_t dispose(Space&) {
257  return sizeof(*this);
258  }
259  };
260 };
261 
266 int pos(int i, int j, int n) {
267  return i*n + j;
268 }
269 
273 int
274 main(int argc, char* argv[]) {
275  SizeOptions opt("QueenArmies");
276  opt.size(6);
280  opt.solutions(0);
281  opt.parse(argc,argv);
282 
283  // Set up the A-sets
284  // A[i] will contain the values attacked by a queen at position i
285  int n = opt.size();
286  A = new IntSet[n*n];
287  int *p = new int[std::max(n*n, 25)];
288  int pn = 0;
289  for (int i = n; i--; ) {
290  for (int j = n; j--; ) {
291  int dir[][2] = {
292  { 0, 1},
293  { 1, 1},
294  { 1, 0},
295  { 0, -1},
296  {-1, -1},
297  {-1, 0},
298  { 1, -1},
299  {-1, 1}
300  };
301  p[pn++] = pos(i, j, n);
302  for (int k = 8; k--; ) {
303  for (int l = 0; l < n
304  && 0 <= (i+l*dir[k][0]) && (i+l*dir[k][0]) < n
305  && 0 <= (j+l*dir[k][1]) && (j+l*dir[k][1]) < n; ++l) {
306  p[pn++] = pos(i+l*dir[k][0], j+l*dir[k][1], n);
307  }
308  }
309 
310  A[pos(i, j, n)] = IntSet(p, pn);
311 
312  pn = 0;
313  }
314  }
315  delete [] p;
316 
317  IntMaximizeScript::run<QueenArmies,BAB,SizeOptions>(opt);
318  return 0;
319 }
320 
321 // STATISTICS: example-any
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
NNF * l
Left subtree.
Definition: bool-expr.cpp:240
int p
Number of positive literals for node type.
Definition: bool-expr.cpp:232
BoolVar expr(Home home, const BoolExpr &e, const IntPropLevels &ipls)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:629
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
struct Gecode::@602::NNF::@65::@67 a
For atomic nodes.
NNF * r
Right subtree.
Definition: bool-expr.cpp:242
Base-class for both propagators and branchers.
Definition: core.hpp:628
Archive representation
Definition: archive.hpp:42
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
Boolean variable array.
Definition: int.hh:808
Base-class for branchers.
Definition: core.hpp:1442
Choice for performing commit
Definition: core.hpp:1412
virtual void archive(Archive &e) const
Archive into e.
Definition: core.cpp:891
Parametric base-class for scripts.
Definition: driver.hh:729
Home class for posting propagators
Definition: core.hpp:856
Range iterator for integer sets.
Definition: int.hh:292
Integer sets.
Definition: int.hh:174
Integer variables.
Definition: int.hh:371
Boolean view for Boolean variables.
Definition: view.hpp:1380
ModEvent eq(Space &home, int n)
Restrict domain values to be equal to n.
Definition: bool.hpp:170
Range iterator for computing intersection (binary)
void branching(int v)
Set default branching value.
Definition: options.hpp:225
void solutions(unsigned int n)
Set default number of solutions to search for.
Definition: options.hpp:283
Iterator for the unknown ranges of a set variable.
Definition: set.hh:334
Set variables
Definition: set.hh:127
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
Custom brancher for Peacable queens.
virtual void print(const Space &, const Gecode::Choice &_c, unsigned int a, std::ostream &o) const
Print explanation.
virtual Gecode::Choice * choice(Space &home)
Return choice.
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.
virtual Actor * copy(Space &home)
Copy brancher during cloning.
static void post(QueenArmies &home)
Post brancher.
virtual Choice * choice(const Space &, Archive &e)
Return choice.
Example: Peaceable co-existing armies of queens
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.
@ BRANCH_NAIVE
Choose variables left to right.
@ BRANCH_SPECIFIC
Choose variable with problem specific strategy.
QueenArmies(QueenArmies &s)
Constructor for cloning.
virtual IntVar cost(void) const
Return solution cost.
IntVar q
The number of white queens placed.
SetVar W
Set of squares occupied by white queens.
const int n
virtual Space * copy(void)
Return copy during cloning.
SetVar U
Set of un-attacked squares.
IntSet * A
Position of a piece in a square board.
ExecStatus
Definition: core.hpp:472
@ ES_OK
Execution is okay.
Definition: core.hpp:476
@ ES_FAILED
Execution has resulted in failure.
Definition: core.hpp:474
void post(Home home, Term *t, int n, FloatRelType frt, FloatVal c)
Post propagator for linear constraint over floats.
Definition: post.cpp:238
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition: modevent.hpp:54
void assign(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatAssign vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Assign all x with variable selection vars and value selection vals.
Definition: branch.cpp:111
void linear(Home home, const FloatVarArgs &x, FloatRelType frt, FloatVal c)
Post propagator for .
Definition: linear.cpp:41
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
@ IRT_EQ
Equality ( )
Definition: int.hh:926
@ IRT_GQ
Greater or equal ( )
Definition: int.hh:930
BoolAssign BOOL_ASSIGN_MAX(void)
Select largest value.
Definition: assign.hpp:105
BoolValBranch BOOL_VAL_MAX(void)
Select largest value.
Definition: val.hpp:135
BoolVarBranch BOOL_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:364
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:41
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode::FloatVal c(-8, 8)
Gecode::IntArgs i({1, 2, 3, 4})
Multi _c(Gecode::IntArgs({1, 2, 3}))
Options opt
The options.
Definition: test.cpp:97
SetExpr singleton(const LinIntExpr &e)
Singleton expression.
Definition: set-expr.cpp:691
LinIntExpr cardinality(const SetExpr &e)
Cardinality of set expression.
Definition: set-expr.cpp:817