Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
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$ by $Author$
11  * $Revision$
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.ipl());
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, IPL_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 ts(2);
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  ts.add(r+13*s1, (r+i+52+13*s2)%52);
183  ts.finalize();
184 
185  for (int i = 51; i--; )
186  extensional(*this, IntVarArgs() << x[i] << x[i+1], ts);
187  }
188 
189  // A card must be played before the one under it.
190  for (int i = 17; i--; )
191  for (int j = 2; j--; )
192  rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
193 
194  // Compute and break the conditional symmetries that are dependent
195  // on the current layout.
196  // Two cards with the same rank but different suits are symmetric
197  // with respect to their placement in the black hole if changing
198  // their order does not affect any other card.
199  if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
200  // For all ranks
201  for (int r = 13; r--; ) {
202  // For all pairs of suits
203  for (int s1 = 4; s1--; ) {
204  for (int s2 = s1; s2--; ) {
205  int c1 = 13*s1 + r,
206  c2 = 13*s2 + r;
207  // The ace of spades is already placed
208  if (c1 == 0 || c2 == 0) continue;
209  // Piles are handled by the rules of the game
210  if (pile[c1] == pile[c2]) continue;
211  // Fix the right order of the cards
212  int o1 = c1, o2 = c2;
213  if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
214  std::swap(o1, o2);
215  // cond is the condition for the symmetry
216  BoolVarArgs ba;
217  // Both cards played after the ones on top of them
218  for (int i = 0; i < layer[o1]; ++i)
219  ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
220  for (int i = 0; i < layer[o2]; ++i)
221  ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
222  // Both cards played before the ones under them
223  for (int i = layer[o1]+1; i < 3; ++i)
224  ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
225  for (int i = layer[o2]+1; i < 3; ++i)
226  ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
227  // Cond holds when all the above holds
228  BoolVar cond(*this, 0, 1);
229  rel(*this, BOT_AND, ba, cond);
230 
231  // If cond is fulfilled, then we can order the cards
232  // cond -> (y[o1] < y[o2])
233  rel(*this, !cond || (y[o1] < y[o2]));
234  }
235  }
236  }
237  }
238 
239  // Install custom brancher
240  branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
241  }
243  static int val(const Space&, IntVar x, int) {
244  int v = -1;
245  int w = 4;
246  for (IntVarValues vals(x); vals(); ++vals)
247  if (layer[vals.val()] < w) {
248  v = vals.val();
249  if ((w = layer[vals.val()]) == 0)
250  break;
251  }
252  assert(v >= 1 && v < 52);
253  return v;
254  }
256  virtual void
257  print(std::ostream& os) const {
258  os << "Layout:" << std::endl;
259  for (int i = 0; i < 17; i++) {
260  for (int j = 0; j < 3; j++)
261  os << card(layout[i][j]) << " ";
262  if ((i+1) % 3 == 0)
263  os << std::endl;
264  else
265  os << " \t";
266  }
267  os << std::endl << std::endl;
268 
269  os << "Solution:" << std::endl;
270  for (int i = 0; i < 52; ++i) {
271  if (x[i].assigned())
272  os << card(x[i].val()) << " ";
273  else
274  os << " ";
275  if ((i + 1) % 13 == 0)
276  os << std::endl;
277  }
278  os << std::endl;
279  os << std::endl;
280  }
281 
284  x.update(*this, s.x);
285  y.update(*this, s.y);
286  }
288  virtual Space*
289  copy(void) {
290  return new BlackHole(*this);
291  }
292 };
293 
297 int
298 main(int argc, char* argv[]) {
299  SizeOptions opt("Black Hole patience");
302  "no symmetry breaking");
303  opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
304  "break conditional symmetries");
307  "reified", "use reified propagation");
309  "dfa", "use DFA-based extensional propagation");
311  "tuple-set", "use TupleSet-based extensional propagation");
312  opt.ipl(IPL_DOM);
313  opt.parse(argc,argv);
314  // Generates the new board
315  generate(opt.size());
316  Script::run<BlackHole,DFS,SizeOptions>(opt);
317  return 0;
318 }
319 
320 // STATISTICS: example-any
Value iterator for integer variables.
Definition: int.hh:470
void size(unsigned int s)
Set default size.
Definition: options.hpp:590
Options for scripts with additional size parameter
Definition: driver.hh:679
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:100
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:159
void 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 channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: channel.cpp:45
void propagation(int v)
Set default propagation value.
Definition: options.hpp:207
No symmetry breaking.
Definition: black-hole.cpp:121
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1060
Regular expressions over integer values.
Definition: minimodel.hh:1522
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:45
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:670
static int val(const Space &, IntVar x, int)
Value selection function for branching.
Definition: black-hole.cpp:243
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Conjunction.
Definition: int.hh:930
Integer variable array.
Definition: int.hh:742
IntVarArray x
Card at position.
Definition: black-hole.cpp:106
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:220
Extensional propagation using tables.
Definition: black-hole.cpp:128
const unsigned int card
Maximum cardinality of an integer set.
Definition: set.hh:105
Computation spaces.
Definition: core.hpp:1668
Parametric base-class for scripts.
Definition: driver.hh:733
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:2027
Gecode::IntArgs i(4, 1, 2, 3, 4)
Equality ( )
Definition: int.hh:905
Extensional propagation using automatons.
Definition: black-hole.cpp:127
Options opt
The options.
Definition: test.cpp:101
virtual Space * copy(void)
Copy during cloning.
Definition: black-hole.cpp:289
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntPropLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Definition: extensional.cpp:47
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:99
Template for linear congruential generators.
Definition: random.hpp:50
Integer sets.
Definition: int.hh:174
Breaking conditional symmetries.
Definition: black-hole.cpp:122
Passing integer variables.
Definition: int.hh:637
Passing integer arguments.
Definition: int.hh:608
Passing Boolean variables.
Definition: int.hh:691
Reified propagation.
Definition: black-hole.cpp:126
BoolVar expr(Home home, const BoolExpr &e, IntPropLevel ipl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
Boolean integer variables.
Definition: int.hh:492
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:769
const int v[7]
Definition: distinct.cpp:263
Class represeting a set of tuples.
Definition: int.hh:2144
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition: ipl.hpp:47
IntVarArray y
Position of card.
Definition: black-hole.cpp:106
Integer variables.
Definition: int.hh:351
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:194
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:958
Post propagator for SetVar x
Definition: set.hh:769
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition: tuple-set.hpp:146
virtual void print(std::ostream &os) const
Print instance and solution.
Definition: black-hole.cpp:257
Gecode toplevel namespace
int main(int argc, char *argv[])
Main-function.
Definition: black-hole.cpp:298
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntPropLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
BlackHole(BlackHole &s)
Constructor for cloning s.
Definition: black-hole.cpp:283
std::string card(int val) const
Return a string representing the card of value val.
Definition: black-hole.cpp:111
IntRelType swap(IntRelType irt)
Return swapped relation type of irt.
Definition: irt.hpp:41