Generated on Wed Nov 1 15:04:28 2006 for Gecode by doxygen 1.4.5

picture-puzzle.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Mikael Lagerkvist, 2005
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-04 16:06:52 +0200 (Fri, 04 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3517 $
00011  *
00012  *  This file is part of Gecode, the generic constraint
00013  *  development environment:
00014  *     http://www.gecode.org
00015  *
00016  *  See the file "LICENSE" for information on usage and
00017  *  redistribution of this file, and for a
00018  *     DISCLAIMER OF ALL WARRANTIES.
00019  *
00020  */
00021 
00022 #include "examples/support.hh"
00023 #include "gecode/minimodel.hh"
00024 
00025 extern const int *specs[];
00026 extern const unsigned int n_examples;
00027 
00044 class PicturePuzzle : public Example {
00045 private:
00046   const int *spec;
00047   int width, height;
00048   BoolVarArray b;
00049 
00051   BoolVar pos(int h, int w) {
00052     return b[h*width + w];
00053   }
00054 
00056   REG get_constraint(int& spos) {
00057     // Useful to check that a new specification was entered correctly.
00058     const bool print_spec = false;
00059     REG r = *REG(0);
00060     int size = spec[spos++];
00061     if (print_spec) std::cout << size << "(" << spos << "): ";
00062     for (int i = 0; i < size; ++i, ++spos) {
00063       if (i) r = r + +REG(0);
00064       if (print_spec) std::cout << spec[spos] << " ";
00065       r = r + REG(1)(spec[spos],spec[spos]);
00066     }
00067     if (print_spec) std::cout << std::endl;
00068     r = r + *REG(0);
00069 
00070     return r;
00071   }
00072 
00073 
00074 public:
00076   PicturePuzzle(const Options& o)
00077     : spec(specs[o.size]), width(spec[0]), height(spec[1]),
00078       b(this,width*height,0,1) {
00079     int spos = 2;
00080     MiniModel::Matrix<BoolVarArray> m(b, width, height);
00081 
00082     // Post constraints for columns
00083     for (int w = 0; w < width; ++w) {
00084       // Post constraint
00085       REG r = get_constraint(spos);
00086       DFA d = r;
00087       regular(this, m.col(w), d);
00088     }
00089 
00090     // Post constraints for rows
00091     for (int h = 0; h < height; ++h) {
00092       // Post constraint
00093       REG r = get_constraint(spos);
00094       DFA d = r;
00095       regular(this, m.row(h), d);
00096     }
00097 
00098     // Install branchings
00099     branch(this, b, BVAR_NONE, BVAL_MAX);
00100   }
00101 
00103   PicturePuzzle(bool share, PicturePuzzle& s) :
00104     Example(share,s), spec(s.spec), width(s.width), height(s.height) {
00105     b.update(this, share, s.b);
00106   }
00107 
00109   virtual Space*
00110   copy(bool share) {
00111     return new PicturePuzzle(share,*this);
00112   }
00113 
00115   virtual void
00116   print(void) {
00117     for (int h = 0; h < height; ++h) {
00118       std::cout << '\t';
00119       for (int w = 0; w < width; ++w) {
00120         if (pos(h,w).val() == 1) {
00121           std::cout << "#";
00122         } else {
00123           std::cout << " ";
00124         }
00125       }
00126       std::cout << std::endl;
00127     }
00128     std::cout << std::endl;
00129   }
00130 };
00131 
00132 
00136 int
00137 main(int argc, char** argv) {
00138   Options o("PicturePuzzle");
00139   o.size  = 8;
00140   o.parse(argc,argv);
00141   if (o.size >= n_examples) {
00142     std::cerr << "Error: size must be between 0 and "
00143               << n_examples-1 << std::endl;
00144     return 1;
00145   }
00146   Example::run<PicturePuzzle,DFS>(o);
00147   return 0;
00148 }
00149 
00150 
00162 
00163 static const int heart[] =
00164   { 9, 9,
00165     // Column constraints.
00166     1, 3,
00167     2, 2, 3,
00168     2, 2, 2,
00169     2, 2, 2,
00170     2, 2, 2,
00171     2, 2, 2,
00172     2, 2, 2,
00173     2, 2, 3,
00174     1, 3,
00175     // Row constraints
00176     2, 2, 2,
00177     2, 4, 4,
00178     3, 1, 3, 1,
00179     3, 2, 1, 2,
00180     2, 1, 1,
00181     2, 2, 2,
00182     2, 2, 2,
00183     1, 3,
00184     1, 1
00185   };
00186 
00188 static const int bear[] =
00189   { 13, 8,
00190     // Column constraints
00191     1, 2,
00192     2, 2, 1,
00193     2, 3, 2,
00194     1, 6,
00195     2, 1, 4,
00196     1, 3,
00197     1, 4,
00198     1, 4,
00199     1, 4,
00200     1, 5,
00201     1, 4,
00202     2, 1, 3,
00203     1, 2,
00204     // Row constraints
00205     1, 1,
00206     1, 2,
00207     2, 4, 4,
00208     1, 12,
00209     1, 8,
00210     1, 9,
00211     2, 3, 4,
00212     2, 2, 2
00213   };
00214 
00216 static const int crocodile[] =
00217   { 15, 9,
00218     // Column constraints
00219     1, 3,
00220     1, 4,
00221     2, 2, 2,
00222     2, 3, 1,
00223     2, 2, 3,
00224     2, 3, 2,
00225     2, 2, 3,
00226     2, 4, 2,
00227     2, 3, 2,
00228     1, 6,
00229     2, 1, 3,
00230     2, 1, 3,
00231     2, 1, 4,
00232     1, 5,
00233     1, 5,
00234     // Row constraints
00235     1, 3,
00236     3, 2, 3, 2,
00237     2, 10, 3,
00238     1, 15,
00239     5, 1, 1, 1, 1, 6,
00240     2, 1, 7,
00241     2, 1, 4,
00242     2, 1, 4,
00243     1, 4
00244   };
00245 
00247 static const int unknown[] =
00248   { 10, 10,
00249     // Column constraints
00250     1, 3,
00251     2, 2, 1,
00252     2, 2, 2,
00253     2, 2, 1,
00254     3, 1, 2, 1,
00255     2, 1, 1,
00256     3, 1, 4, 1,
00257     3, 1, 1, 2,
00258     2, 3, 1,
00259     1, 4,
00260     // Row constraints
00261     1, 3,
00262     2, 2, 1,
00263     2, 1, 1,
00264     2, 1, 4,
00265     4, 1, 1, 1, 1,
00266     4, 2, 1, 1, 1,
00267     3, 2, 1, 1,
00268     2, 1, 2,
00269     2, 2, 3,
00270     1, 3
00271   };
00272 
00274 static const int pinwheel[] =
00275   { 6, 6,
00276     // Column constraints
00277     2, 1, 2,
00278     1, 1,
00279     1, 2,
00280     1, 2,
00281     1, 1,
00282     2, 2, 1,
00283     // Row constraints
00284     2, 2, 1,
00285     1, 1,
00286     1, 2,
00287     1, 2,
00288     1, 1,
00289     2, 1, 2
00290   };
00291 
00293 static const int difficult[] =
00294   { 15, 15,
00295     // Column constraints
00296     1, 3,
00297     1, 2,
00298     1, 2,
00299     1, 1,
00300     1, 2,
00301     1, 3,
00302     1, 2,
00303     1, 4,
00304     1, 3,
00305     1, 4,
00306     2, 2, 1,
00307     2, 1, 1,
00308     2, 1, 1,
00309     2, 1, 1,
00310     1, 3,
00311     // Row constraints
00312     1, 3,
00313     2, 1, 1,
00314     2, 1, 1,
00315     2, 1, 1,
00316     2, 1, 2,
00317     1, 5,
00318     1, 1,
00319     1, 2,
00320     1, 1,
00321     1, 1,
00322     2, 1, 2,
00323     2, 1, 2,
00324     2, 2, 1,
00325     2, 2, 2,
00326     1, 3
00327   };
00328 
00330 static const int non_unique[] =
00331   { 11, 15,
00332     // Column constraints
00333     1, 5,
00334     3, 1, 2, 4,
00335     3, 2, 1, 3,
00336     4, 2, 2, 1, 1,
00337     4, 1, 1, 1, 1,
00338     2, 1, 5,
00339     5, 2, 1, 1, 3, 2,
00340     5, 2, 1, 1, 1, 1,
00341     3, 1, 4, 1,
00342     2, 1, 1,
00343     1, 1,
00344     // Row constraints
00345     2, 2, 2,
00346     2, 2, 2,
00347     1, 4,
00348     2, 1, 1,
00349     2, 1, 1,
00350     4, 1, 1, 1, 1,
00351     2, 1, 1,
00352     2, 1, 4,
00353     3, 1, 1, 1,
00354     3, 1, 1, 4,
00355     2, 1, 3,
00356     2, 1, 2,
00357     1, 5,
00358     2, 2, 2,
00359     2, 3, 3
00360   };
00361 
00367 static const int dragonfly[] =
00368   { 20, 20,
00369     // Column constraints.
00370     4, 1, 1, 1, 2,
00371     5, 3, 1, 2, 1, 1,
00372     5, 1, 4, 2, 1, 1,
00373     4, 1, 3, 2, 4,
00374     4, 1, 4, 6, 1,
00375     3, 1, 11, 1,
00376     4, 5, 1, 6, 2,
00377     1, 14,
00378     2, 7, 2,
00379     2, 7, 2,
00380     3, 6, 1, 1,
00381     2, 9, 2,
00382     4, 3, 1, 1, 1,
00383     3, 3, 1, 3,
00384     3, 2, 1, 3,
00385     3, 2, 1, 5,
00386     3, 3, 2, 2,
00387     3, 3, 3, 2,
00388     3, 2, 3, 2,
00389     2, 2, 6,
00390     // Row constraints
00391     2, 7, 1,
00392     3, 1, 1, 2,
00393     3, 2, 1, 2,
00394     3, 1, 2, 2,
00395     3, 4, 2, 3,
00396     3, 3, 1, 4,
00397     3, 3, 1, 3,
00398     3, 2, 1, 4,
00399     2, 2, 9,
00400     3, 2, 1, 5,
00401     2, 2, 7,
00402     1, 14,
00403     2, 8, 2,
00404     3, 6, 2, 2,
00405     4, 2, 8, 1, 3,
00406     4, 1, 5, 5, 2,
00407     5, 1, 3, 2, 4, 1,
00408     5, 3, 1, 2, 4, 1,
00409     5, 1, 1, 3, 1, 3,
00410     4, 2, 1, 1, 2
00411   };
00412 
00418 static const int p200[] =
00419   { 25, 25,
00420     // Column constraints
00421     4, 1,1,2,2,
00422     3, 5,5,7,
00423     4, 5,2,2,9,
00424     4, 3,2,3,9,
00425     5, 1,1,3,2,7,
00426     3, 3,1,5,
00427     5, 7,1,1,1,3,
00428     6, 1,2,1,1,2,1,
00429     3, 4,2,4,
00430     4, 1,2,2,2,
00431     3, 4,6,2,
00432     4, 1,2,2,1,
00433     4, 3,3,2,1,
00434     3, 4,1,15,
00435     6, 1,1,1,3,1,1,
00436     6, 2,1,1,2,2,3,
00437     4, 1,4,4,1,
00438     4, 1,4,3,2,
00439     4, 1,1,2,2,
00440     5, 7,2,3,1,1,
00441     5, 2,1,1,1,5,
00442     3, 1,2,5,
00443     4, 1,1,1,3,
00444     3, 4,2,1,
00445     1, 3,
00446     // Row constraints
00447     3, 2,2,3,
00448     5, 4,1,1,1,4,
00449     5, 4,1,2,1,1,
00450     7, 4,1,1,1,1,1,1,
00451     6, 2,1,1,2,3,5,
00452     6, 1,1,1,1,2,1,
00453     5, 3,1,5,1,2,
00454     6, 3,2,2,1,2,2,
00455     7, 2,1,4,1,1,1,1,
00456     6, 2,2,1,2,1,2,
00457     6, 1,1,1,3,2,3,
00458     5, 1,1,2,7,3,
00459     5, 1,2,2,1,5,
00460     5, 3,2,2,1,2,
00461     4, 3,2,1,2,
00462     3, 5,1,2,
00463     4, 2,2,1,2,
00464     4, 4,2,1,2,
00465     4, 6,2,3,2,
00466     4, 7,4,3,2,
00467     3, 7,4,4,
00468     3, 7,1,4,
00469     3, 6,1,4,
00470     3, 4,2,2,
00471     2, 2,1
00472   };
00473 
00475 const int *specs[] = {heart, bear, crocodile, unknown,
00476                       pinwheel, difficult, non_unique, dragonfly, p200};
00478 const unsigned n_examples = sizeof(specs)/sizeof(int*);
00480 
00481 // STATISTICS: example-any