Generated on Wed Dec 13 2017 05:45:47 for Gecode by doxygen 1.8.5
domino.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Guido Tack, 2006
9  * Mikael Lagerkvist, 2006
10  *
11  * Last modified:
12  * $Date: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
13  * $Revision: 14447 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 #include <gecode/driver.hh>
41 #include <gecode/int.hh>
42 #include <gecode/minimodel.hh>
43 
44 using namespace Gecode;
45 
46 namespace {
47 
52  extern const int *specs[];
57  extern const unsigned int n_examples;
58 
59 }
60 
72 class Domino : public Script {
73 private:
75  const int *spec;
77  int width;
79  int height;
80 
82  IntVarArray x;
83 
84 public:
86  enum {
88  PROP_EXTENSIONAL
89  };
90 
93  : Script(opt),
94  spec(specs[opt.size()]),
95  width(spec[0]), height(spec[1]),
96  x(*this, (width+1)*height, 0, 28) {
97  spec+=2; // skip board size information
98 
99  // Copy spec information to the board
100  IntArgs board((width+1)*height);
101  for (int i=0; i<width; i++)
102  for (int j=0; j<height; j++)
103  board[j*(width+1)+i] = spec[j*width+i];
104 
105  // Initialize the separator column in the board
106  for (int i=0; i<height; i++) {
107  board[i*(width+1)+8] = -1;
108  rel(*this, x[i*(width+1)+8]==28);
109  }
110 
111  // Variables representing the coordinates of the first
112  // and second half of a domino piece
113  IntVarArgs p1(*this, 28, 0, (width+1)*height-1);
114  IntVarArgs p2(*this, 28, 0, (width+1)*height-1);
115 
116 
117  if (opt.propagation() == PROP_ELEMENT) {
118  int dominoCount = 0;
119 
120  int possibleDiffsA[] = {1, width+1};
121  IntSet possibleDiffs(possibleDiffsA, 2);
122 
123  for (int i=0; i<=6; i++)
124  for (int j=i; j<=6; j++) {
125 
126  // The two coordinates must be adjacent.
127  // I.e., they may differ by 1 or by the width.
128  // The separator column makes sure that a field
129  // at the right border is not adjacent to the first field
130  // in the next row.
131  IntVar diff(*this, possibleDiffs);
132  abs(*this, expr(*this, p1[dominoCount]-p2[dominoCount]),
133  diff, ICL_DOM);
134 
135  // If the piece is symmetrical, order the locations
136  if (i == j)
137  rel(*this, p1[dominoCount], IRT_LE, p2[dominoCount]);
138 
139  // Link the current piece to the board
140  element(*this, board, p1[dominoCount], i);
141  element(*this, board, p2[dominoCount], j);
142 
143  // Link the current piece to the array where its
144  // number is stored.
145  element(*this, x, p1[dominoCount], dominoCount);
146  element(*this, x, p2[dominoCount], dominoCount);
147  dominoCount++;
148  }
149  } else {
150  int dominoCount = 0;
151 
152  for (int i=0; i<=6; i++)
153  for (int j=i; j<=6; j++) {
154  // Find valid placements for piece i-j
155  // Extensional is used as a table-constraint listing all valid
156  // tuples.
157  // Note that when i == j, only one of the orientations are used.
158  REG valids;
159  for (int pos = 0; pos < (width+1)*height; ++pos) {
160  if ((pos+1) % (width+1) != 0) { // not end-col
161  if (board[pos] == i && board[pos+1] == j)
162  valids |= REG(pos) + REG(pos+1);
163  if (board[pos] == j && board[pos+1] == i && i != j)
164  valids |= REG(pos+1) + REG(pos);
165  }
166  if (pos/(width+1) < height-1) { // not end-row
167  if (board[pos] == i && board[pos+width+1] == j)
168  valids |= REG(pos) + REG(pos+width+1);
169  if (board[pos] == j && board[pos+width+1] == i && i != j)
170  valids |= REG(pos+width+1) + REG(pos);
171  }
172  }
173  IntVarArgs piece(2);
174  piece[0] = p1[dominoCount];
175  piece[1] = p2[dominoCount];
176  extensional(*this, piece, valids);
177 
178 
179  // Link the current piece to the array where its
180  // number is stored.
181  element(*this, x, p1[dominoCount], dominoCount);
182  element(*this, x, p2[dominoCount], dominoCount);
183  dominoCount++;
184  }
185  }
186 
187  // Branch by piece
188  IntVarArgs ps(28*2);
189  for (int i=0; i<28; i++) {
190  ps[2*i] = p1[i];
191  ps[2*i+1] = p2[i];
192  }
193 
194  branch(*this, ps, INT_VAR_NONE(), INT_VAL_MIN());
195  }
196 
198  virtual void
199  print(std::ostream& os) const {
200  for (int h = 0; h < height; ++h) {
201  os << "\t";
202  for (int w = 0; w < width; ++w) {
203  int val = x[h*(width+1)+w].min();
204  char c = val < 10 ? '0'+val : 'A' + (val-10);
205  os << c;
206  }
207  os << std::endl;
208  }
209  os << std::endl;
210  }
212  Domino(bool share, Domino& s) :
213  Script(share,s), spec(s.spec), width(s.width), height(s.height) {
214  x.update(*this, share, s.x);
215  }
217  virtual Space*
218  copy(bool share) {
219  return new Domino(share,*this);
220  }
221 
222 };
223 
224 
228 int
229 main(int argc, char* argv[]) {
230  SizeOptions opt("Domino");
231  opt.size(0);
233  opt.propagation(Domino::PROP_ELEMENT, "element");
234  opt.propagation(Domino::PROP_EXTENSIONAL, "extensional");
235  opt.parse(argc,argv);
236  if (opt.size() >= n_examples) {
237  std::cerr << "Error: size must be between 0 and "
238  << n_examples-1 << std::endl;
239  return 1;
240  }
241  Script::run<Domino,DFS,SizeOptions>(opt);
242  return 0;
243 }
244 
245 
246 namespace {
247 
253 
255  const int domino0[] =
256  { // width*height of the board
257  8,7,
258  // the board itself
259  2,1,0,3,0,4,5,5,
260  6,2,0,6,3,1,4,0,
261  3,2,3,6,2,5,4,3,
262  5,4,5,1,1,2,1,2,
263  0,0,1,5,0,5,4,4,
264  4,6,2,1,3,6,6,1,
265  4,2,0,6,5,3,3,6
266  };
267 
269  const int domino1[] =
270  { // width*height of the board
271  8,7,
272  // the board itself
273  5,1,2,4,6,2,0,5,
274  6,6,4,3,5,0,1,5,
275  2,0,4,0,4,0,5,0,
276  6,1,3,6,3,5,4,3,
277  3,1,0,1,2,2,1,4,
278  3,6,6,2,4,0,5,4,
279  1,3,6,1,2,3,5,2
280  };
281 
283  const int domino2[] =
284  { // width*height of the board
285  8,7,
286  // the board itself
287  4,4,5,4,0,3,6,5,
288  1,6,0,1,5,3,4,1,
289  2,6,2,2,5,3,6,0,
290  1,3,0,6,4,4,2,3,
291  3,5,5,2,4,2,2,1,
292  2,1,3,3,5,6,6,1,
293  5,1,6,0,0,0,4,0
294  };
295 
297  const int domino3[] =
298  { // width*height of the board
299  8,7,
300  // the board itself
301  3,0,2,3,3,4,4,3,
302  6,5,3,4,2,0,2,1,
303  6,5,1,2,3,0,2,0,
304  4,5,4,1,6,6,2,5,
305  4,3,6,1,0,4,5,5,
306  1,3,2,5,6,0,0,1,
307  0,5,4,6,2,1,6,1
308  };
309 
311  const int domino4[] =
312  { // width*height of the board
313  8,7,
314  // the board itself
315  4,1,5,2,4,4,6,2,
316  2,5,6,1,4,6,0,2,
317  6,5,1,1,0,1,4,3,
318  6,2,1,1,3,2,0,6,
319  3,6,3,3,5,5,0,5,
320  3,0,1,0,0,5,4,3,
321  3,2,4,5,4,2,6,0
322  };
323 
325  const int domino5[] =
326  { // width*height of the board
327  8,7,
328  // the board itself
329  4,1,2,1,0,2,4,4,
330  5,5,6,6,0,4,6,3,
331  6,0,5,1,1,0,5,3,
332  3,4,2,2,0,3,1,2,
333  3,6,5,6,1,2,3,2,
334  2,5,0,6,6,3,3,5,
335  4,1,0,0,4,1,4,5
336  };
337 
339  const int *specs[] =
340  {domino0,domino1,domino2,domino3,domino4,domino5};
342  const unsigned n_examples = sizeof(specs)/sizeof(int*);
344 
345 }
346 
347 // STATISTICS: example-any
Use extensional constraints.
Definition: domino.cpp:88
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
Options for scripts with additional size parameter
Definition: driver.hh:579
virtual Space * copy(bool share)
Copy space during cloning.
Definition: domino.cpp:218
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition: var.hpp:108
Domino(const SizeOptions &opt)
Actual model.
Definition: domino.cpp:92
Use element constraints.
Definition: domino.cpp:87
void propagation(int v)
Set default propagation value.
Definition: options.hpp:181
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
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
int main(int argc, char *argv[])
Main-function.
Definition: domino.cpp:229
Integer variable array.
Definition: int.hh:741
Domino(bool share, Domino &s)
Constructor for cloning s.
Definition: domino.cpp:212
Computation spaces.
Definition: core.hpp:1362
Parametric base-class for scripts.
Definition: driver.hh:633
virtual void print(std::ostream &os) const
Print solution.
Definition: domino.cpp:199
Gecode::FloatVal c(-8, 8)
Gecode::IntArgs p2(4, 4, 3, 3, 5)
Gecode::IntArgs i(4, 1, 2, 3, 4)
Options opt
The options.
Definition: test.cpp:101
IntValBranch INT_VAL_MIN(void)
Select smallest value.
Definition: val.hpp:68
unsigned int size(I &i)
Size of all ranges of range iterator i.
Less ( )
Definition: int.hh:907
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
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:75
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
const unsigned int n_examples
Number of board specifications.
Definition: domino.cpp:57
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Integer variables.
Definition: int.hh:350
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Example: Solitaire domino
Definition: domino.cpp:72
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
Gecode::IntArgs p1(4, 2, 2, 2, 2)
Domain propagation or consistency.
Definition: int.hh:940