00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038 #include <gecode/driver.hh>
00039 #include <gecode/int.hh>
00040
00041 #include <vector>
00042 #include <algorithm>
00043 #include <sstream>
00044
00045 using namespace Gecode;
00046
00047 namespace {
00048 using std::vector;
00049
00051 vector<vector<int> > layout;
00053 vector<int> layer, pile;
00054
00061 void generate(int seed) {
00062
00063 layout = vector<vector<int> >(17, vector<int>(3));
00064
00065 vector<int> deck(51);
00066 for (int i = 51; i--; ) deck[i] = i+1;
00067 Support::RandomGenerator rnd(seed+1);
00068 std::random_shuffle(deck.begin(), deck.end(), rnd);
00069
00070
00071 int pos = 0;
00072 for (int i = 17; i--; )
00073 for (int j = 3; j--; )
00074 layout[i][j] = deck[pos++];
00075
00076
00077 layer = vector<int>(52);
00078 pile = vector<int>(52);
00079 for (int i = 17; i--; ) {
00080 for (int j = 3; j--; ) {
00081 layer[layout[i][j]] = j;
00082 pile[ layout[i][j]] = i;
00083 }
00084 }
00085 }
00086 }
00087
00104 class BlackHole : public Script {
00105 protected:
00106 IntVarArray x,
00107 y;
00108
00110 std::string
00111 card(int val) const {
00112 const char* suit = "SCHD";
00113 std::ostringstream o;
00114 o << std::setw(2) << (1 + (val%13)) << suit[val/13];
00115 return o.str();
00116 }
00117
00118 public:
00120 enum {
00121 SYMMETRY_NONE,
00122 SYMMETRY_CONDITIONAL
00123 };
00125 enum {
00126 PROPAGATION_REIFIED,
00127 PROPAGATION_DFA,
00128 PROPAGATION_TUPLE_SET
00129 };
00131 BlackHole(const SizeOptions& opt)
00132 : Script(opt), x(*this, 52, 0,51), y(*this, 52, 0,51) {
00133
00134 rel(*this, x[0], IRT_EQ, 0);
00135
00136
00137 channel(*this, x, y, opt.ipl());
00138
00139
00140
00141 if (opt.propagation() == PROPAGATION_REIFIED) {
00142
00143 IntArgs modtable(52);
00144 for (int i = 0; i < 52; ++i) {
00145 modtable[i] = i%13;
00146 }
00147 for (int i = 0; i < 51; ++i) {
00148 IntVar x1(*this, 0, 12), x2(*this, 0, 12);
00149 element(*this, modtable, x[i], x1);
00150 element(*this, modtable, x[i+1], x2);
00151 const int dr[2] = {1, 12};
00152 IntVar diff(*this, IntSet(dr, 2));
00153 rel(*this, abs(x1-x2) == diff, IPL_DOM);
00154 }
00155 } else if (opt.propagation() == PROPAGATION_DFA) {
00156
00157 REG expression;
00158 for (int r = 13; r--; ) {
00159 for (int s1 = 4; s1--; ) {
00160 for (int s2 = 4; s2--; ) {
00161 for (int i = -1; i <= 1; i+=2) {
00162 REG r1 = REG(r+13*s1);
00163 REG r2 = REG((r+i+52+13*s2)%52);
00164 REG r = r1 + r2;
00165 expression |= r;
00166 }
00167 }
00168 }
00169 }
00170 DFA table(expression);
00171
00172 for (int i = 51; i--; )
00173 extensional(*this, IntVarArgs() << x[i] << x[i+1], table);
00174
00175 } else {
00176
00177 TupleSet tupleSet;
00178 for (int r = 13; r--; )
00179 for (int s1 = 4; s1--; )
00180 for (int s2 = 4; s2--; )
00181 for (int i = -1; i <= 1; i+=2) {
00182 tupleSet.add(IntArgs(2, r+13*s1, (r+i+52+13*s2)%52));
00183 }
00184 tupleSet.finalize();
00185
00186 for (int i = 51; i--; )
00187 extensional(*this, IntVarArgs() << x[i] << x[i+1], tupleSet);
00188 }
00189
00190
00191 for (int i = 17; i--; )
00192 for (int j = 2; j--; )
00193 rel(*this, y[layout[i][j]] < y[layout[i][j+1]]);
00194
00195
00196
00197
00198
00199
00200 if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
00201
00202 for (int r = 13; r--; ) {
00203
00204 for (int s1 = 4; s1--; ) {
00205 for (int s2 = s1; s2--; ) {
00206 int c1 = 13*s1 + r,
00207 c2 = 13*s2 + r;
00208
00209 if (c1 == 0 || c2 == 0) continue;
00210
00211 if (pile[c1] == pile[c2]) continue;
00212
00213 int o1 = c1, o2 = c2;
00214 if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
00215 std::swap(o1, o2);
00216
00217 BoolVarArgs ba;
00218
00219 for (int i = 0; i < layer[o1]; ++i)
00220 ba << expr(*this, (y[layout[pile[o1]][i]] < y[o2]));
00221 for (int i = 0; i < layer[o2]; ++i)
00222 ba << expr(*this, (y[layout[pile[o2]][i]] < y[o1]));
00223
00224 for (int i = layer[o1]+1; i < 3; ++i)
00225 ba << expr(*this, (y[o2] < y[layout[pile[o1]][i]]));
00226 for (int i = layer[o2]+1; i < 3; ++i)
00227 ba << expr(*this, (y[o1] < y[layout[pile[o2]][i]]));
00228
00229 BoolVar cond(*this, 0, 1);
00230 rel(*this, BOT_AND, ba, cond);
00231
00232
00233
00234 rel(*this, !cond || (y[o1] < y[o2]));
00235 }
00236 }
00237 }
00238 }
00239
00240
00241 branch(*this, x, INT_VAR_NONE(), INT_VAL(&val));
00242 }
00244 static int val(const Space&, IntVar x, int) {
00245 int v = -1;
00246 int w = 4;
00247 for (IntVarValues vals(x); vals(); ++vals)
00248 if (layer[vals.val()] < w) {
00249 v = vals.val();
00250 if ((w = layer[vals.val()]) == 0)
00251 break;
00252 }
00253 assert(v >= 1 && v < 52);
00254 return v;
00255 }
00257 virtual void
00258 print(std::ostream& os) const {
00259 os << "Layout:" << std::endl;
00260 for (int i = 0; i < 17; i++) {
00261 for (int j = 0; j < 3; j++)
00262 os << card(layout[i][j]) << " ";
00263 if ((i+1) % 3 == 0)
00264 os << std::endl;
00265 else
00266 os << " \t";
00267 }
00268 os << std::endl << std::endl;
00269
00270 os << "Solution:" << std::endl;
00271 for (int i = 0; i < 52; ++i) {
00272 if (x[i].assigned())
00273 os << card(x[i].val()) << " ";
00274 else
00275 os << " ";
00276 if ((i + 1) % 13 == 0)
00277 os << std::endl;
00278 }
00279 os << std::endl;
00280 os << std::endl;
00281 }
00282
00284 BlackHole(bool share, BlackHole& s) : Script(share,s) {
00285 x.update(*this, share, s.x);
00286 y.update(*this, share, s.y);
00287 }
00289 virtual Space*
00290 copy(bool share) {
00291 return new BlackHole(share,*this);
00292 }
00293 };
00294
00298 int
00299 main(int argc, char* argv[]) {
00300 SizeOptions opt("Black Hole patience");
00301 opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL);
00302 opt.symmetry(BlackHole::SYMMETRY_NONE,"none",
00303 "no symmetry breaking");
00304 opt.symmetry(BlackHole::SYMMETRY_CONDITIONAL,"conditional",
00305 "break conditional symmetries");
00306 opt.propagation(BlackHole::PROPAGATION_DFA);
00307 opt.propagation(BlackHole::PROPAGATION_REIFIED,
00308 "reified", "use reified propagation");
00309 opt.propagation(BlackHole::PROPAGATION_DFA,
00310 "dfa", "use DFA-based extensional propagation");
00311 opt.propagation(BlackHole::PROPAGATION_TUPLE_SET,
00312 "tuple-set", "use TupleSet-based extensional propagation");
00313 opt.ipl(IPL_DOM);
00314 opt.parse(argc,argv);
00315
00316 generate(opt.size());
00317 Script::run<BlackHole,DFS,SizeOptions>(opt);
00318 return 0;
00319 }
00320
00321