Generated on Tue Apr 18 10:21:28 2017 for Gecode by doxygen 1.6.3

black-hole.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Mikael Lagerkvist, 2006
00008  *
00009  *  Last modified:
00010  *     $Date: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
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     // The layout consists of 17 piles of 3 cards each
00063     layout = vector<vector<int> >(17, vector<int>(3));
00064     // Deck without the Ace of Spades
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     // Place cards from deck
00071     int pos = 0;
00072     for (int i = 17; i--; )
00073       for (int j = 3; j--; )
00074         layout[i][j] = deck[pos++];
00075 
00076     // Location-information for each card
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     // Black ace at bottom
00134     rel(*this, x[0], IRT_EQ, 0);
00135 
00136     // x is order and y is placement
00137     channel(*this, x, y, opt.ipl());
00138 
00139     // The placement rules: the absolute value of the difference
00140     // between two consecutive cards is 1 or 12.
00141     if (opt.propagation() == PROPAGATION_REIFIED) {
00142       // Build table for accessing the rank of a card
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       // Build table for allowed tuples
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 { // opt.propagation() == PROPAGATION_TUPLE_SET)
00176       // Build table for allowed tuples
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     // A card must be played before the one under it.
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     // Compute and break the conditional symmetries that are dependent
00196     // on the current layout.
00197     // Two cards with the same rank but different suits are symmetric
00198     // with respect to their placement in the black hole if changing
00199     // their order does not affect any other card.
00200     if (opt.symmetry() == SYMMETRY_CONDITIONAL) {
00201       // For all ranks
00202       for (int r = 13; r--; ) {
00203         // For all pairs of suits
00204         for (int s1 = 4; s1--; ) {
00205           for (int s2 = s1; s2--; ) {
00206             int c1 = 13*s1 + r,
00207               c2 = 13*s2 + r;
00208             // The ace of spades is already placed
00209             if (c1 == 0 || c2 == 0) continue;
00210             // Piles are handled by the rules of the game
00211             if (pile[c1] == pile[c2]) continue;
00212             // Fix the right order of the cards
00213             int o1 = c1, o2 = c2;
00214             if (pile[c1] > pile[c2] && layer[c2] >= layer[c1])
00215               std::swap(o1, o2);
00216             // cond is the condition for the symmetry
00217             BoolVarArgs ba;
00218             // Both cards played after the ones on top of them
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             // Both cards played before the ones under them
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             // Cond holds when all the above holds
00229             BoolVar cond(*this, 0, 1);
00230             rel(*this, BOT_AND, ba, cond);
00231 
00232             // If cond is fulfilled, then we can order the cards
00233             // cond -> (y[o1] < y[o2])
00234             rel(*this, !cond || (y[o1] < y[o2]));
00235           }
00236         }
00237       }
00238     }
00239 
00240     // Install custom brancher
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   // Generates the new board
00316   generate(opt.size());
00317   Script::run<BlackHole,DFS,SizeOptions>(opt);
00318   return 0;
00319 }
00320 
00321 // STATISTICS: example-any