Generated on for Gecode by doxygen 1.17.0
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 Zayenz Lagerkvist <lagerkvist@gecode.dev>
5 *
6 * Copyright:
7 * Mikael Zayenz Lagerkvist, 2006
8 *
9 * This file is part of Gecode, the generic constraint
10 * development environment:
11 * http://www.gecode.dev
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
41using namespace Gecode;
42
43namespace {
44
45 using std::vector;
46
48 vector<vector<int> > layout;
50 vector<int> layer, pile;
51
58 void generate(int seed) {
59 // The layout consists of 17 piles of 3 cards each
60 layout = vector<vector<int> >(17, vector<int>(3));
61 // Deck without the Ace of Spades
62 vector<int> deck(51);
63 for (int i = 51; i--; ) deck[i] = i+1;
64 Support::RandomGenerator rnd(seed+1);
65 std::shuffle(deck.begin(), deck.end(), rnd);
66
67 // Place cards from deck
68 int pos = 0;
69 for (int i = 17; i--; )
70 for (int j = 3; j--; )
71 layout[i][j] = deck[pos++];
72
73 // Location-information for each card
74 layer = vector<int>(52);
75 pile = vector<int>(52);
76 for (int i = 17; i--; ) {
77 for (int j = 3; j--; ) {
78 layer[layout[i][j]] = j;
79 pile[ layout[i][j]] = i;
80 }
81 }
82 }
83}
84
101class BlackHole : public Script {
102protected:
105
107 std::string
108 card(int val) const {
109 const char* suit = "SCHD";
110 std::ostringstream o;
111 o << std::setw(2) << (1 + (val%13)) << suit[val/13];
112 return o.str();
113 }
114
115public:
117 enum {
120 };
122 enum {
126 };
129 : Script(opt), x(*this, 52, 0,51), y(*this, 52, 0,51) {
130 // Black ace at bottom
131 rel(*this, x[0], IRT_EQ, 0);
132
133 // x is order and y is placement
134 channel(*this, x, y, opt.ipl());
135
136 // The placement rules: the absolute value of the difference
137 // between two consecutive cards is 1 or 12.
138 if (opt.propagation() == PROPAGATION_REIFIED) {
139 // Build table for accessing the rank of a card
140 IntArgs modtable(52);
141 for (int i = 0; i < 52; ++i) {
142 modtable[i] = i%13;
143 }
144 for (int i = 0; i < 51; ++i) {
145 IntVar x1(*this, 0, 12), x2(*this, 0, 12);
146 element(*this, modtable, x[i], x1);
147 element(*this, modtable, x[i+1], x2);
148 const int dr[2] = {1, 12};
149 IntVar diff(*this, IntSet(dr, 2));
150 rel(*this, abs(x1-x2) == diff, IPL_DOM);
151 }
152 } else if (opt.propagation() == PROPAGATION_DFA) {
153 // Build table for allowed tuples
154 REG expression;
155 for (int r = 13; r--; ) {
156 for (int s1 = 4; s1--; ) {
157 for (int s2 = 4; s2--; ) {
158 for (int i = -1; i <= 1; i+=2) {
159 REG r1 = REG(r+13*s1);
160 REG r2 = REG((r+i+52+13*s2)%52);
161 REG r = r1 + r2;
162 expression |= r;
163 }
164 }
165 }
166 }
167 DFA table(expression);
168
169 for (int i = 51; i--; )
170 extensional(*this, IntVarArgs({x[i],x[i+1]}), table);
171
172 } else { // opt.propagation() == PROPAGATION_TUPLE_SET)
173 // Build table for allowed tuples
174 TupleSet ts(2);
175 for (int r = 13; r--; )
176 for (int s1 = 4; s1--; )
177 for (int s2 = 4; s2--; )
178 for (int i = -1; i <= 1; i+=2)
179 ts.add({r+13*s1, (r+i+52+13*s2)%52});
180 ts.finalize(EPK_AUTO);
181
182 for (int i = 51; i--; )
183 extensional(*this, IntVarArgs({x[i],x[i+1]}), ts);
184 }
185
186 // A card must be played before the one under it.
187 for (int i = 17; i--; )
188 for (int j = 2; j--; )
189 rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
190
191 // Compute and break the conditional symmetries that are dependent
192 // on the current layout.
193 // Two cards with the same rank but different suits are symmetric
194 // with respect to their placement in the black hole if changing
195 // their order does not affect any other card.
196 if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
197 // For all ranks
198 for (int r = 13; r--; ) {
199 // For all pairs of suits
200 for (int s1 = 4; s1--; ) {
201 for (int s2 = s1; s2--; ) {
202 int c1 = 13*s1 + r,
203 c2 = 13*s2 + r;
204 // The ace of spades is already placed
205 if (c1 == 0 || c2 == 0) continue;
206 // Piles are handled by the rules of the game
207 if (pile[c1] == pile[c2]) continue;
208 // Fix the right order of the cards
209 int o1 = c1, o2 = c2;
210 if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
211 std::swap(o1, o2);
212 // cond is the condition for the symmetry
214 // Both cards played after the ones on top of them
215 for (int i = 0; i < layer[o1]; ++i)
216 ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
217 for (int i = 0; i < layer[o2]; ++i)
218 ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
219 // Both cards played before the ones under them
220 for (int i = layer[o1]+1; i < 3; ++i)
221 ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
222 for (int i = layer[o2]+1; i < 3; ++i)
223 ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
224 // Cond holds when all the above holds
225 BoolVar cond(*this, 0, 1);
226 rel(*this, BOT_AND, ba, cond);
227
228 // If cond is fulfilled, then we can order the cards
229 // cond -> (y[o1] < y[o2])
230 rel(*this, !cond || (y[o1] < y[o2]));
231 }
232 }
233 }
234 }
235
236 // Install custom brancher
237 branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
238 }
239
240 static int val(const Space&, IntVar x, int) {
241 int v = -1;
242 int w = 4;
243 for (IntVarValues vals(x); vals(); ++vals)
244 if (layer[vals.val()] < w) {
245 v = vals.val();
246 if ((w = layer[vals.val()]) == 0)
247 break;
248 }
249 assert(v >= 1 && v < 52);
250 return v;
251 }
252
253 virtual void
254 print(std::ostream& os) const {
255 os << "Layout:" << std::endl;
256 for (int i = 0; i < 17; i++) {
257 for (int j = 0; j < 3; j++)
258 os << card(layout[i][j]) << " ";
259 if ((i+1) % 3 == 0)
260 os << std::endl;
261 else
262 os << " \t";
263 }
264 os << std::endl << std::endl;
265
266 os << "Solution:" << std::endl;
267 for (int i = 0; i < 52; ++i) {
268 if (x[i].assigned())
269 os << card(x[i].val()) << " ";
270 else
271 os << " ";
272 if ((i + 1) % 13 == 0)
273 os << std::endl;
274 }
275 os << std::endl;
276 os << std::endl;
277 }
278
281 x.update(*this, s.x);
282 y.update(*this, s.y);
283 }
284
285 virtual Space*
286 copy(void) {
287 return new BlackHole(*this);
288 }
289};
290
294int
295main(int argc, char* argv[]) {
296 SizeOptions opt("Black Hole patience");
298 opt.symmetry(BlackHole::SYMMETRY_NONE,"none",
299 "no symmetry breaking");
300 opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
301 "break conditional symmetries");
302 opt.propagation(BlackHole::PROPAGATION_TUPLE_SET);
303 opt.propagation(BlackHole::PROPAGATION_REIFIED,
304 "reified", "use reified propagation");
305 opt.propagation(BlackHole::PROPAGATION_DFA,
306 "dfa", "use DFA-based extensional propagation");
307 opt.propagation(BlackHole::PROPAGATION_TUPLE_SET,
308 "tuple-set", "use TupleSet-based extensional propagation");
309 opt.ipl(IPL_DOM);
310 opt.parse(argc,argv);
311 // Generates the new board
312 generate(opt.size());
314 return 0;
315}
316
317// STATISTICS: example-any
int main(int argc, char *argv[])
Main-function.
BlackHole(BlackHole &s)
Constructor for cloning s.
@ SYMMETRY_CONDITIONAL
Breaking conditional symmetries.
@ SYMMETRY_NONE
No symmetry breaking.
IntVarArray x
Card at position.
std::string card(int val) const
Return a string representing the card of value val.
BlackHole(const SizeOptions &opt)
Actual model.
virtual Space * copy(void)
Copy during cloning.
static int val(const Space &, IntVar x, int)
Value selection function for branching.
IntVarArray y
Position of card.
@ PROPAGATION_REIFIED
Reified propagation.
@ PROPAGATION_DFA
Extensional propagation using automatons.
@ PROPAGATION_TUPLE_SET
Extensional propagation using tables.
virtual void print(std::ostream &os) const
Print instance and solution.
Passing Boolean variables.
Definition int.hh:738
Boolean integer variables.
Definition int.hh:533
Deterministic finite automaton (DFA).
Definition int.hh:2203
static void run(const Options &opt, Script *s=nullptr)
Passing integer arguments.
Definition int.hh:652
Integer sets.
Definition int.hh:178
Passing integer variables.
Definition int.hh:680
Integer variable array.
Definition int.hh:791
Value iterator for integer variables.
Definition int.hh:511
Integer variables.
Definition int.hh:389
Regular expressions over integer values.
Options for scripts with additional size parameter
Definition driver.hh:723
Computation spaces.
Definition core.hpp:1775
Class representing a set of tuples.
Definition int.hh:2382
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
void finalize(void)
Finalize tuple set with dense support data.
void parse(int argc, char *argv[])
Parse commandline arguments.
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:183
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:849
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
void extensional(Home home, const IntVarArgs &x, DFA d, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for extensional constraint described by a DFA.
@ IRT_EQ
Equality ( ).
Definition int.hh:960
@ BOT_AND
Conjunction.
Definition int.hh:985
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1013
bool pos(const View &x)
Test whether x is positive.
Definition mult.hpp:41
Gecode toplevel namespace
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
IntValBranch INT_VAL(IntBranchVal v, IntBranchCommit c=nullptr)
Select value as defined by the value function v and commit function c Uses a commit function as defau...
Definition val.hpp:95
IntPropLevel ba(IntPropLevel ipl)
Extract basic or advanced from propagation level.
Definition ipl.hpp:43
IntVarBranch INT_VAR_NONE(void)
Select first unassigned variable.
Definition var.hpp:96
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .