Generated on Tue May 31 2016 10:19:10 for Gecode by doxygen 1.8.11
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: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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  : Script(opt), x(*this, 52, 0,51), y(*this, 52, 0,51) {
133  // Black ace at bottom
134  rel(*this, x[0], IRT_EQ, 0);
135 
136  // x is order and y is placement
137  channel(*this, x, y, opt.icl());
138 
139  // The placement rules: the absolute value of the difference
140  // between two consecutive cards is 1 or 12.
141  if (opt.propagation() == PROPAGATION_REIFIED) {
142  // Build table for accessing the rank of a card
143  IntArgs modtable(52);
144  for (int i = 0; i < 52; ++i) {
145  modtable[i] = i%13;
146  }
147  for (int i = 0; i < 51; ++i) {
148  IntVar x1(*this, 0, 12), x2(*this, 0, 12);
149  element(*this, modtable, x[i], x1);
150  element(*this, modtable, x[i+1], x2);
151  const int dr[2] = {1, 12};
152  IntVar diff(*this, IntSet(dr, 2));
153  rel(*this, abs(x1-x2) == diff, ICL_DOM);
154  }
155  } else if (opt.propagation() == PROPAGATION_DFA) {
156  // Build table for allowed tuples
157  REG expression;
158  for (int r = 13; r--; ) {
159  for (int s1 = 4; s1--; ) {
160  for (int s2 = 4; s2--; ) {
161  for (int i = -1; i <= 1; i+=2) {
162  REG r1 = REG(r+13*s1);
163  REG r2 = REG((r+i+52+13*s2)%52);
164  REG r = r1 + r2;
165  expression |= r;
166  }
167  }
168  }
169  }
170  DFA table(expression);
171 
172  for (int i = 51; i--; )
173  extensional(*this, IntVarArgs() << x[i] << x[i+1], table);
174 
175  } else { // opt.propagation() == PROPAGATION_TUPLE_SET)
176  // Build table for allowed tuples
177  TupleSet tupleSet;
178  for (int r = 13; r--; )
179  for (int s1 = 4; s1--; )
180  for (int s2 = 4; s2--; )
181  for (int i = -1; i <= 1; i+=2) {
182  tupleSet.add(IntArgs(2, r+13*s1, (r+i+52+13*s2)%52));
183  }
184  tupleSet.finalize();
185 
186  for (int i = 51; i--; )
187  extensional(*this, IntVarArgs() << x[i] << x[i+1], tupleSet);
188  }
189 
190  // A card must be played before the one under it.
191  for (int i = 17; i--; )
192  for (int j = 2; j--; )
193  rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
194 
195  // Compute and break the conditional symmetries that are dependent
196  // on the current layout.
197  // Two cards with the same rank but different suits are symmetric
198  // with respect to their placement in the black hole if changing
199  // their order does not affect any other card.
200  if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
201  // For all ranks
202  for (int r = 13; r--; ) {
203  // For all pairs of suits
204  for (int s1 = 4; s1--; ) {
205  for (int s2 = s1; s2--; ) {
206  int c1 = 13*s1 + r,
207  c2 = 13*s2 + r;
208  // The ace of spades is already placed
209  if (c1 == 0 || c2 == 0) continue;
210  // Piles are handled by the rules of the game
211  if (pile[c1] == pile[c2]) continue;
212  // Fix the right order of the cards
213  int o1 = c1, o2 = c2;
214  if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
215  std::swap(o1, o2);
216  // cond is the condition for the symmetry
217  BoolVarArgs ba;
218  // Both cards played after the ones on top of them
219  for (int i = 0; i < layer[o1]; ++i)
220  ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
221  for (int i = 0; i < layer[o2]; ++i)
222  ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
223  // Both cards played before the ones under them
224  for (int i = layer[o1]+1; i < 3; ++i)
225  ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
226  for (int i = layer[o2]+1; i < 3; ++i)
227  ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
228  // Cond holds when all the above holds
229  BoolVar cond(*this, 0, 1);
230  rel(*this, BOT_AND, ba, cond);
231 
232  // If cond is fulfilled, then we can order the cards
233  // cond -> (y[o1] < y[o2])
234  rel(*this, !cond || (y[o1] < y[o2]));
235  }
236  }
237  }
238  }
239 
240  // Install custom brancher
241  branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
242  }
244  static int val(const Space&, IntVar x, int) {
245  int v = -1;
246  int w = 4;
247  for (IntVarValues vals(x); vals(); ++vals)
248  if (layer[vals.val()] < w) {
249  v = vals.val();
250  if ((w = layer[vals.val()]) == 0)
251  break;
252  }
253  assert(v >= 1 && v < 52);
254  return v;
255  }
257  virtual void
258  print(std::ostream& os) const {
259  os << "Layout:" << std::endl;
260  for (int i = 0; i < 17; i++) {
261  for (int j = 0; j < 3; j++)
262  os << card(layout[i][j]) << " ";
263  if ((i+1) % 3 == 0)
264  os << std::endl;
265  else
266  os << " \t";
267  }
268  os << std::endl << std::endl;
269 
270  os << "Solution:" << std::endl;
271  for (int i = 0; i < 52; ++i) {
272  if (x[i].assigned())
273  os << card(x[i].val()) << " ";
274  else
275  os << " ";
276  if ((i + 1) % 13 == 0)
277  os << std::endl;
278  }
279  os << std::endl;
280  os << std::endl;
281  }
282 
284  BlackHole(bool share, BlackHole& s) : Script(share,s) {
285  x.update(*this, share, s.x);
286  y.update(*this, share, s.y);
287  }
289  virtual Space*
290  copy(bool share) {
291  return new BlackHole(share,*this);
292  }
293 };
294 
298 int
299 main(int argc, char* argv[]) {
300  SizeOptions opt("Black Hole patience");
303  "no symmetry breaking");
304  opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
305  "break conditional symmetries");
308  "reified", "use reified propagation");
310  "dfa", "use DFA-based extensional propagation");
312  "tuple-set", "use TupleSet-based extensional propagation");
313  opt.icl(ICL_DOM);
314  opt.parse(argc,argv);
315  // Generates the new board
316  generate(opt.size());
317  Script::run<BlackHole,DFS,SizeOptions>(opt);
318  return 0;
319 }
320 
321 // STATISTICS: example-any
Value iterator for integer variables.
Definition: int.hh:469
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
Options for scripts with additional size parameter
Definition: driver.hh:579
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
No symmetry breaking.
Definition: black-hole.cpp:121
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:437
static int val(const Space &, IntVar x, int)
Value selection function for branching.
Definition: black-hole.cpp:244
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
Extensional propagation using tables.
Definition: black-hole.cpp:128
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:633
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
Extensional propagation using automatons.
Definition: black-hole.cpp:127
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:284
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
Breaking conditional symmetries.
Definition: black-hole.cpp:122
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:258
Reified propagation.
Definition: black-hole.cpp:126
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
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
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
int main(int argc, char *argv[])
Main-function.
Definition: black-hole.cpp:299
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:290