Generated on Fri Jan 10 2020 11:38:25 for Gecode by doxygen 1.8.16
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  * 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 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 
37 #include <vector>
38 #include <algorithm>
39 #include <sstream>
40 
41 using namespace Gecode;
42 
43 namespace {
44  using std::vector;
45 
47  vector<vector<int> > layout;
49  vector<int> layer, pile;
50 
57  void generate(int seed) {
58  // The layout consists of 17 piles of 3 cards each
59  layout = vector<vector<int> >(17, vector<int>(3));
60  // Deck without the Ace of Spades
61  vector<int> deck(51);
62  for (int i = 51; i--; ) deck[i] = i+1;
63  Support::RandomGenerator rnd(seed+1);
64  std::random_shuffle(deck.begin(), deck.end(), rnd);
65 
66  // Place cards from deck
67  int pos = 0;
68  for (int i = 17; i--; )
69  for (int j = 3; j--; )
70  layout[i][j] = deck[pos++];
71 
72  // Location-information for each card
73  layer = vector<int>(52);
74  pile = vector<int>(52);
75  for (int i = 17; i--; ) {
76  for (int j = 3; j--; ) {
77  layer[layout[i][j]] = j;
78  pile[ layout[i][j]] = i;
79  }
80  }
81  }
82 }
83 
100 class BlackHole : public Script {
101 protected:
103  y;
104 
106  std::string
107  card(int val) const {
108  const char* suit = "SCHD";
109  std::ostringstream o;
110  o << std::setw(2) << (1 + (val%13)) << suit[val/13];
111  return o.str();
112  }
113 
114 public:
116  enum {
118  SYMMETRY_CONDITIONAL
119  };
121  enum {
124  PROPAGATION_TUPLE_SET
125  };
128  : Script(opt), x(*this, 52, 0,51), y(*this, 52, 0,51) {
129  // Black ace at bottom
130  rel(*this, x[0], IRT_EQ, 0);
131 
132  // x is order and y is placement
133  channel(*this, x, y, opt.ipl());
134 
135  // The placement rules: the absolute value of the difference
136  // between two consecutive cards is 1 or 12.
137  if (opt.propagation() == PROPAGATION_REIFIED) {
138  // Build table for accessing the rank of a card
139  IntArgs modtable(52);
140  for (int i = 0; i < 52; ++i) {
141  modtable[i] = i%13;
142  }
143  for (int i = 0; i < 51; ++i) {
144  IntVar x1(*this, 0, 12), x2(*this, 0, 12);
145  element(*this, modtable, x[i], x1);
146  element(*this, modtable, x[i+1], x2);
147  const int dr[2] = {1, 12};
148  IntVar diff(*this, IntSet(dr, 2));
149  rel(*this, abs(x1-x2) == diff, IPL_DOM);
150  }
151  } else if (opt.propagation() == PROPAGATION_DFA) {
152  // Build table for allowed tuples
153  REG expression;
154  for (int r = 13; r--; ) {
155  for (int s1 = 4; s1--; ) {
156  for (int s2 = 4; s2--; ) {
157  for (int i = -1; i <= 1; i+=2) {
158  REG r1 = REG(r+13*s1);
159  REG r2 = REG((r+i+52+13*s2)%52);
160  REG r = r1 + r2;
161  expression |= r;
162  }
163  }
164  }
165  }
166  DFA table(expression);
167 
168  for (int i = 51; i--; )
169  extensional(*this, IntVarArgs({x[i],x[i+1]}), table);
170 
171  } else { // opt.propagation() == PROPAGATION_TUPLE_SET)
172  // Build table for allowed tuples
173  TupleSet ts(2);
174  for (int r = 13; r--; )
175  for (int s1 = 4; s1--; )
176  for (int s2 = 4; s2--; )
177  for (int i = -1; i <= 1; i+=2)
178  ts.add({r+13*s1, (r+i+52+13*s2)%52});
179  ts.finalize();
180 
181  for (int i = 51; i--; )
182  extensional(*this, IntVarArgs({x[i],x[i+1]}), ts);
183  }
184 
185  // A card must be played before the one under it.
186  for (int i = 17; i--; )
187  for (int j = 2; j--; )
188  rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
189 
190  // Compute and break the conditional symmetries that are dependent
191  // on the current layout.
192  // Two cards with the same rank but different suits are symmetric
193  // with respect to their placement in the black hole if changing
194  // their order does not affect any other card.
195  if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
196  // For all ranks
197  for (int r = 13; r--; ) {
198  // For all pairs of suits
199  for (int s1 = 4; s1--; ) {
200  for (int s2 = s1; s2--; ) {
201  int c1 = 13*s1 + r,
202  c2 = 13*s2 + r;
203  // The ace of spades is already placed
204  if (c1 == 0 || c2 == 0) continue;
205  // Piles are handled by the rules of the game
206  if (pile[c1] == pile[c2]) continue;
207  // Fix the right order of the cards
208  int o1 = c1, o2 = c2;
209  if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
210  std::swap(o1, o2);
211  // cond is the condition for the symmetry
212  BoolVarArgs ba;
213  // Both cards played after the ones on top of them
214  for (int i = 0; i < layer[o1]; ++i)
215  ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
216  for (int i = 0; i < layer[o2]; ++i)
217  ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
218  // Both cards played before the ones under them
219  for (int i = layer[o1]+1; i < 3; ++i)
220  ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
221  for (int i = layer[o2]+1; i < 3; ++i)
222  ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
223  // Cond holds when all the above holds
224  BoolVar cond(*this, 0, 1);
225  rel(*this, BOT_AND, ba, cond);
226 
227  // If cond is fulfilled, then we can order the cards
228  // cond -> (y[o1] < y[o2])
229  rel(*this, !cond || (y[o1] < y[o2]));
230  }
231  }
232  }
233  }
234 
235  // Install custom brancher
236  branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
237  }
239  static int val(const Space&, IntVar x, int) {
240  int v = -1;
241  int w = 4;
242  for (IntVarValues vals(x); vals(); ++vals)
243  if (layer[vals.val()] < w) {
244  v = vals.val();
245  if ((w = layer[vals.val()]) == 0)
246  break;
247  }
248  assert(v >= 1 && v < 52);
249  return v;
250  }
252  virtual void
253  print(std::ostream& os) const {
254  os << "Layout:" << std::endl;
255  for (int i = 0; i < 17; i++) {
256  for (int j = 0; j < 3; j++)
257  os << card(layout[i][j]) << " ";
258  if ((i+1) % 3 == 0)
259  os << std::endl;
260  else
261  os << " \t";
262  }
263  os << std::endl << std::endl;
264 
265  os << "Solution:" << std::endl;
266  for (int i = 0; i < 52; ++i) {
267  if (x[i].assigned())
268  os << card(x[i].val()) << " ";
269  else
270  os << " ";
271  if ((i + 1) % 13 == 0)
272  os << std::endl;
273  }
274  os << std::endl;
275  os << std::endl;
276  }
277 
280  x.update(*this, s.x);
281  y.update(*this, s.y);
282  }
284  virtual Space*
285  copy(void) {
286  return new BlackHole(*this);
287  }
288 };
289 
293 int
294 main(int argc, char* argv[]) {
295  SizeOptions opt("Black Hole patience");
298  "no symmetry breaking");
300  "break conditional symmetries");
303  "reified", "use reified propagation");
305  "dfa", "use DFA-based extensional propagation");
307  "tuple-set", "use TupleSet-based extensional propagation");
308  opt.ipl(IPL_DOM);
309  opt.parse(argc,argv);
310  // Generates the new board
311  generate(opt.size());
312  Script::run<BlackHole,DFS,SizeOptions>(opt);
313  return 0;
314 }
315 
316 // STATISTICS: example-any
Post propagator for SetVar x
Definition: set.hh:767
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
Conjunction.
Definition: int.hh:951
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
Definition: tuple-set.hpp:142
void propagation(int v)
Set default propagation value.
Definition: options.hpp:203
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:96
Extensional propagation using automatons.
Definition: black-hole.cpp:123
Passing integer variables.
Definition: int.hh:656
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntPropLevel)
Post domain consistent propagator for .
Definition: element.cpp:39
Value iterator for integer variables.
Definition: int.hh:490
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
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:95
Computation spaces.
Definition: core.hpp:1742
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:39
Regular expressions over integer values.
Definition: minimodel.hh:1625
virtual Space * copy(void)
Copy during cloning.
Definition: black-hole.cpp:285
Integer variable array.
Definition: int.hh:763
Gecode toplevel namespace
Integer sets.
Definition: int.hh:174
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition: ipl.hpp:43
const unsigned int card
Maximum cardinality of an integer set.
Definition: set.hh:101
Options opt
The options.
Definition: test.cpp:97
void finalize(void)
Finalize tuple set.
Definition: tuple-set.hpp:155
BoolVar expr(Home home, const BoolExpr &e, const IntPropLevels &ipls)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:629
Passing Boolean variables.
Definition: int.hh:712
virtual void print(std::ostream &os) const
Print instance and solution.
Definition: black-hole.cpp:253
Parametric base-class for scripts.
Definition: driver.hh:729
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition: set.hh:767
Boolean integer variables.
Definition: int.hh:512
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
Example: Black hole patience
Definition: black-hole.cpp:100
IntRelType swap(IntRelType irt)
Return swapped relation type of irt.
Definition: irt.hpp:37
Template for linear congruential generators.
Definition: random.hpp:46
Reified propagation.
Definition: black-hole.cpp:122
Integer variables.
Definition: int.hh:371
Class represeting a set of tuples.
Definition: int.hh:2191
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:41
std::string card(int val) const
Return a string representing the card of value val.
Definition: black-hole.cpp:107
Deterministic finite automaton (DFA)
Definition: int.hh:2049
const int v[7]
Definition: distinct.cpp:259
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
IntVarArray y
Position of card.
Definition: black-hole.cpp:102
Breaking conditional symmetries.
Definition: black-hole.cpp:118
static int val(const Space &, IntVar x, int)
Value selection function for branching.
Definition: black-hole.cpp:239
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: channel.cpp:41
Extensional propagation using tables.
Definition: black-hole.cpp:124
BlackHole(BlackHole &s)
Constructor for cloning s.
Definition: black-hole.cpp:279
BlackHole(const SizeOptions &opt)
Actual model.
Definition: black-hole.cpp:127
void symmetry(int v)
Set default symmetry value.
Definition: options.hpp:190
IntVarArray x
Card at position.
Definition: black-hole.cpp:102
void update(Space &home, VarImpVar< VarImp > &y)
Update this variable to be a clone of variable y.
Definition: var.hpp:116
void extensional(Home home, const IntVarArgs &x, DFA dfa, IntPropLevel)
Post domain consistent propagator for extensional constraint described by a DFA.
Equality ( )
Definition: int.hh:926
int main(int argc, char *argv[])
Main-function.
Definition: black-hole.cpp:294
No symmetry breaking.
Definition: black-hole.cpp:117
Passing integer arguments.
Definition: int.hh:628
Gecode::IntArgs i({1, 2, 3, 4})
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:41
Options for scripts with additional size parameter
Definition: driver.hh:675