Generated on Thu Mar 22 10:39:31 2012 for Gecode by doxygen 1.6.3

dominating-queens.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2011
00008  *
00009  *  Last modified:
00010  *     $Date: 2011-08-17 22:25:49 +0200 (Wed, 17 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12308 $
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 #include <gecode/minimodel.hh>
00041 
00042 using namespace Gecode;
00043 
00057 class DominatingQueens : public MinimizeScript {
00058 protected:
00060   const int n;
00062   IntVarArray b;
00064   IntVar q;
00066   int xy(int x, int y) const {
00067     return y*n + x;
00068   }
00070   int x(int xy) const {
00071     return xy % n;
00072   }
00074   int y(int xy) const {
00075     return xy / n;
00076   }
00078   IntSet attacked(int xy) {
00079     IntArgs a;
00080     for (int i=0; i<n; i++)
00081       for (int j=0; j<n; j++)
00082         if ((i == x(xy)) || // Same row
00083             (j == y(xy)) || // Same column
00084             (abs(i-x(xy)) == abs(j-y(xy)))) // Same diagonal
00085           a << DominatingQueens::xy(i,j);
00086     return IntSet(a);
00087   }
00088 public:
00090   DominatingQueens(const SizeOptions& opt)
00091     : n(opt.size()), b(*this,n*n,0,n*n-1), q(*this,1,n) {
00092 
00093     // Constrain field to the fields that can attack a field
00094     for (int i=0; i<n*n; i++)
00095       dom(*this, b[i], attacked(i));
00096 
00097     // At most q queens can be placed
00098     nvalues(*this, b, IRT_LQ, q);
00099 
00100     /*
00101      * According to: P. R. J. Östergard and W. D. Weakley, Values
00102      * of Domination Numbers of the Queen's Graph, Electronic Journal
00103      * of Combinatorics, 8:1-19, 2001, for n <= 120, the minimal number
00104      * of queens is either ceil(n/2) or ceil(n/2 + 1).
00105      */
00106     if (n <= 120)
00107       dom(*this, q, (n+1)/2, (n+1)/2 + 1);
00108 
00109     branch(*this, b, INT_VAR_SIZE_MIN, INT_VAL_MIN);
00110     // Try the easier solution first
00111     branch(*this, q, INT_VAL_MAX);
00112   }
00113 
00115   virtual IntVar cost(void) const {
00116     return q;
00117   }
00118 
00120   DominatingQueens(bool share, DominatingQueens& s) 
00121     : MinimizeScript(share,s), n(s.n) {
00122     b.update(*this, share, s.b);
00123     q.update(*this, share, s.q);
00124   }
00125 
00127   virtual Space*
00128   copy(bool share) {
00129     return new DominatingQueens(share,*this);
00130   }
00131 
00133   virtual void
00134   print(std::ostream& os) const {
00135     os << "\tNumber of Queens: " << q << std::endl;
00136     os << "\tBoard: " << b << std::endl;
00137     if (b.assigned()) {
00138       // Print a nice board
00139       bool* placed = new bool[n*n];
00140       for (int i=0; i<n*n; i++)
00141         placed[i] = false;
00142       for (int i=0; i<n*n; i++)
00143         placed[b[i].val()] = true;
00144       for (int j=0; j<n; j++) {
00145         std::cout << "\t\t";
00146         for (int i=0; i<n; i++)
00147           std::cout << (placed[xy(i,j)] ? 'Q' : '.') << ' ';
00148         std::cout << std::endl;
00149       }
00150       delete [] placed;
00151     }
00152     os << std::endl;
00153   }
00154 };
00155 
00159 int
00160 main(int argc, char* argv[]) {
00161   SizeOptions opt("DominatingQueens");
00162   opt.size(7);
00163   opt.solutions(0);
00164   opt.parse(argc,argv);
00165   MinimizeScript::run<DominatingQueens,BAB,SizeOptions>(opt);
00166   return 0;
00167 }
00168 
00169 // STATISTICS: example-any
00170