Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.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 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
41using namespace Gecode;
42
43namespace {
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
100class BlackHole : public Script {
101protected:
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
114public:
116 enum {
119 };
121 enum {
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
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 }
238
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 }
251
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 }
283
284 virtual Space*
285 copy(void) {
286 return new BlackHole(*this);
287 }
288};
289
293int
294main(int argc, char* argv[]) {
295 SizeOptions opt("Black Hole patience");
297 opt.symmetry(BlackHole::SYMMETRY_NONE,"none",
298 "no symmetry breaking");
299 opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
300 "break conditional symmetries");
301 opt.propagation(BlackHole::PROPAGATION_TUPLE_SET);
302 opt.propagation(BlackHole::PROPAGATION_REIFIED,
303 "reified", "use reified propagation");
304 opt.propagation(BlackHole::PROPAGATION_DFA,
305 "dfa", "use DFA-based extensional propagation");
306 opt.propagation(BlackHole::PROPAGATION_TUPLE_SET,
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());
313 return 0;
314}
315
316// 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:721
Boolean integer variables.
Definition int.hh:515
Deterministic finite automaton (DFA)
Definition int.hh:2064
static void run(const Options &opt, Script *s=NULL)
Passing integer arguments.
Definition int.hh:634
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:662
Integer variable array.
Definition int.hh:772
Value iterator for integer variables.
Definition int.hh:493
Integer variables.
Definition int.hh:371
Regular expressions over integer values.
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1744
Class represeting a set of tuples.
Definition int.hh:2206
TupleSet & add(const IntArgs &t)
Add tuple t to tuple set.
void finalize(void)
Finalize tuple set.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:120
Driver::ScriptBase< Driver::IgnoreStepOption< Space > > Script
Base-class for scripts.
Definition driver.hh:801
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.
Definition branch.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
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:941
@ BOT_AND
Conjunction.
Definition int.hh:966
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:994
bool pos(const View &x)
Test whether x is postive.
Definition mult.hpp:41
Gecode toplevel namespace
Post propagator for SetVar SetOpType SetVar SetRelType r
Definition set.hh:773
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
IntVar expr(Home home, const LinIntExpr &e, const IntPropLevels &ipls=IntPropLevels::def)
Post linear expression and return its value.
Definition int-expr.cpp:915
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 .
Definition element.cpp:39
Gecode::IntArgs i({1, 2, 3, 4})