Generated on Sat Feb 7 2015 02:01:11 for Gecode by doxygen 1.8.9.1
black-hole.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  * 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 
41 #include <vector>
42 #include <algorithm>
43 #include <sstream>
44 
45 using namespace Gecode;
46 
47 namespace {
48  using std::vector;
49 
51  vector<vector<int> > layout;
53  vector<int> layer, pile;
54 
61  void generate(int seed) {
62  // The layout consists of 17 piles of 3 cards each
63  layout = vector<vector<int> >(17, vector<int>(3));
64  // Deck without the Ace of Spades
65  vector<int> deck(51);
66  for (int i = 51; i--; ) deck[i] = i+1;
67  Support::RandomGenerator rnd(seed+1);
68  std::random_shuffle(deck.begin(), deck.end(), rnd);
69 
70  // Place cards from deck
71  int pos = 0;
72  for (int i = 17; i--; )
73  for (int j = 3; j--; )
74  layout[i][j] = deck[pos++];
75 
76  // Location-information for each card
77  layer = vector<int>(52);
78  pile = vector<int>(52);
79  for (int i = 17; i--; ) {
80  for (int j = 3; j--; ) {
81  layer[layout[i][j]] = j;
82  pile[ layout[i][j]] = i;
83  }
84  }
85  }
86 }
87 
104 class BlackHole : public Script {
105 protected:
107  y;
108 
110  std::string
111  card(int val) const {
112  const char* suit = "SCHD";
113  std::ostringstream o;
114  o << std::setw(2) << (1 + (val%13)) << suit[val/13];
115  return o.str();
116  }
117 
118 public:
120  enum {
122  SYMMETRY_CONDITIONAL
123  };
125  enum {
128  PROPAGATION_TUPLE_SET
129  };
132  : x(*this, 52, 0,51), y(*this, 52, 0,51)
133  {
134  // Black ace at bottom
135  rel(*this, x[0], IRT_EQ, 0);
136 
137  // x is order and y is placement
138  channel(*this, x, y, opt.icl());
139 
140  // The placement rules: the absolute value of the difference
141  // between two consecutive cards is 1 or 12.
142  if (opt.propagation() == PROPAGATION_REIFIED) {
143  // Build table for accessing the rank of a card
144  IntArgs modtable(52);
145  for (int i = 0; i < 52; ++i) {
146  modtable[i] = i%13;
147  }
148  for (int i = 0; i < 51; ++i) {
149  IntVar x1(*this, 0, 12), x2(*this, 0, 12);
150  element(*this, modtable, x[i], x1);
151  element(*this, modtable, x[i+1], x2);
152  const int dr[2] = {1, 12};
153  IntVar diff(*this, IntSet(dr, 2));
154  rel(*this, abs(x1-x2) == diff, ICL_DOM);
155  }
156  } else if (opt.propagation() == PROPAGATION_DFA) {
157  // Build table for allowed tuples
158  REG expression;
159  for (int r = 13; r--; ) {
160  for (int s1 = 4; s1--; ) {
161  for (int s2 = 4; s2--; ) {
162  for (int i = -1; i <= 1; i+=2) {
163  REG r1 = REG(r+13*s1);
164  REG r2 = REG((r+i+52+13*s2)%52);
165  REG r = r1 + r2;
166  expression |= r;
167  }
168  }
169  }
170  }
171  DFA table(expression);
172 
173  for (int i = 51; i--; )
174  extensional(*this, IntVarArgs() << x[i] << x[i+1], table);
175 
176  } else { // opt.propagation() == PROPAGATION_TUPLE_SET)
177  // Build table for allowed tuples
178  TupleSet tupleSet;
179  for (int r = 13; r--; )
180  for (int s1 = 4; s1--; )
181  for (int s2 = 4; s2--; )
182  for (int i = -1; i <= 1; i+=2) {
183  tupleSet.add(IntArgs(2, r+13*s1, (r+i+52+13*s2)%52));
184  }
185  tupleSet.finalize();
186 
187  for (int i = 51; i--; )
188  extensional(*this, IntVarArgs() << x[i] << x[i+1], tupleSet);
189  }
190 
191  // A card must be played before the one under it.
192  for (int i = 17; i--; )
193  for (int j = 2; j--; )
194  rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
195 
196  // Compute and break the conditional symmetries that are dependent
197  // on the current layout.
198  // Two cards with the same rank but different suits are symmetric
199  // with respect to their placement in the black hole if changing
200  // their order does not affect any other card.
201  if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
202  // For all ranks
203  for (int r = 13; r--; ) {
204  // For all pairs of suits
205  for (int s1 = 4; s1--; ) {
206  for (int s2 = s1; s2--; ) {
207  int c1 = 13*s1 + r,
208  c2 = 13*s2 + r;
209  // The ace of spades is already placed
210  if (c1 == 0 || c2 == 0) continue;
211  // Piles are handled by the rules of the game
212  if (pile[c1] == pile[c2]) continue;
213  // Fix the right order of the cards
214  int o1 = c1, o2 = c2;
215  if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
216  std::swap(o1, o2);
217  // cond is the condition for the symmetry
218  BoolVarArgs ba;
219  // Both cards played after the ones on top of them
220  for (int i = 0; i < layer[o1]; ++i)
221  ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
222  for (int i = 0; i < layer[o2]; ++i)
223  ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
224  // Both cards played before the ones under them
225  for (int i = layer[o1]+1; i < 3; ++i)
226  ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
227  for (int i = layer[o2]+1; i < 3; ++i)
228  ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
229  // Cond holds when all the above holds
230  BoolVar cond(*this, 0, 1);
231  rel(*this, BOT_AND, ba, cond);
232 
233  // If cond is fulfilled, then we can order the cards
234  // cond -> (y[o1] < y[o2])
235  rel(*this, !cond || (y[o1] < y[o2]));
236  }
237  }
238  }
239  }
240 
241  // Install custom brancher
242  branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
243  }
245  static int val(const Space&, IntVar x, int) {
246  int v = -1;
247  int w = 4;
248  for (IntVarValues vals(x); vals(); ++vals)
249  if (layer[vals.val()] < w) {
250  v = vals.val();
251  if ((w = layer[vals.val()]) == 0)
252  break;
253  }
254  assert(v >= 1 && v < 52);
255  return v;
256  }
258  virtual void
259  print(std::ostream& os) const {
260  os << "Layout:" << std::endl;
261  for (int i = 0; i < 17; i++) {
262  for (int j = 0; j < 3; j++)
263  os << card(layout[i][j]) << " ";
264  if ((i+1) % 3 == 0)
265  os << std::endl;
266  else
267  os << " \t";
268  }
269  os << std::endl << std::endl;
270 
271  os << "Solution:" << std::endl;
272  for (int i = 0; i < 52; ++i) {
273  if (x[i].assigned())
274  os << card(x[i].val()) << " ";
275  else
276  os << " ";
277  if ((i + 1) % 13 == 0)
278  os << std::endl;
279  }
280  os << std::endl;
281  os << std::endl;
282  }
283 
285  BlackHole(bool share, BlackHole& s) : Script(share,s) {
286  x.update(*this, share, s.x);
287  y.update(*this, share, s.y);
288  }
290  virtual Space*
291  copy(bool share) {
292  return new BlackHole(share,*this);
293  }
294 };
295 
299 int
300 main(int argc, char* argv[]) {
301  SizeOptions opt("Black Hole patience");
304  "no symmetry breaking");
305  opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
306  "break conditional symmetries");
309  "reified", "use reified propagation");
311  "dfa", "use DFA-based extensional propagation");
313  "tuple-set", "use TupleSet-based extensional propagation");
314  opt.icl(ICL_DOM);
315  opt.parse(argc,argv);
316  // Generates the new board
317  generate(opt.size());
318  Script::run<BlackHole,DFS,SizeOptions>(opt);
319  return 0;
320 }
321 
322 // STATISTICS: example-any
Value iterator for integer variables.
Definition: int.hh:469
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
Options for scripts with additional size parameter
Definition: driver.hh:567
Extensional propagation using automatons.
Definition: black-hole.cpp:127
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:111
std::string card(int val) const
Return a string representing the card of value val.
Definition: black-hole.cpp:111
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: arithmetic.cpp:218
void propagation(int v)
Set default propagation value.
Definition: options.hpp:181
Extensional propagation using tables.
Definition: black-hole.cpp:128
Regular expressions over integer values.
Definition: minimodel.hh:1401
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
static int val(const Space &, IntVar x, int)
Value selection function for branching.
Definition: black-hole.cpp:245
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Conjunction.
Definition: int.hh:917
Integer variable array.
Definition: int.hh:741
IntVarArray x
Card at position.
Definition: black-hole.cpp:106
const unsigned int card
Maximum cardinality of an integer set.
Definition: set.hh:103
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
Example: Black hole patience
Definition: black-hole.cpp:104
BlackHole(const SizeOptions &opt)
Actual model.
Definition: black-hole.cpp:131
Deterministic finite automaton (DFA)
Definition: int.hh:1881
Gecode::IntArgs i(4, 1, 2, 3, 4)
Equality ( )
Definition: int.hh:904
Options opt
The options.
Definition: test.cpp:101
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
BlackHole(bool share, BlackHole &s)
Constructor for cloning s.
Definition: black-hole.cpp:285
Template for linear congruential generators.
Definition: random.hpp:50
Integer sets.
Definition: int.hh:171
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
Passing Boolean variables.
Definition: int.hh:690
virtual void print(std::ostream &os) const
Print instance and solution.
Definition: black-hole.cpp:259
Boolean integer variables.
Definition: int.hh:491
const int v[7]
Definition: distinct.cpp:207
Class represeting a set of tuples.
Definition: int.hh:2022
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntConLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition: extensional.cpp:45
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Breaking conditional symmetries.
Definition: black-hole.cpp:122
IntVarArray y
Position of card.
Definition: black-hole.cpp:106
Integer variables.
Definition: int.hh:350
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
void symmetry(int v)
Set default symmetry value.
Definition: options.hpp:168
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
No symmetry breaking.
Definition: black-hole.cpp:121
Gecode toplevel namespace
IntValBranch INT_VAL(IntBranchVal v, IntBranchCommit c)
Select value as defined by the value function v and commit function c Uses a commit function as defau...
Definition: val.hpp:108
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 add(const IntArgs &tuple)
Add tuple to tuple set.
Definition: tuple-set.hpp:98
Reified propagation.
Definition: black-hole.cpp:126
int main(int argc, char *argv[])
Main-function.
Definition: black-hole.cpp:300
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
Domain propagation or consistency.
Definition: int.hh:940
virtual Space * copy(bool share)
Copy during cloning.
Definition: black-hole.cpp:291