Generated on Tue Apr 18 10:21:29 2017 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: 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 #include <gecode/minimodel.hh>
00041 
00042 using namespace Gecode;
00043 
00057 class DominatingQueens : public IntMinimizeScript {
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     : IntMinimizeScript(opt),
00092       n(opt.size()), b(*this,n*n,0,n*n-1), q(*this,1,n) {
00093 
00094     // Constrain field to the fields that can attack a field
00095     for (int i=0; i<n*n; i++)
00096       dom(*this, b[i], attacked(i));
00097 
00098     // At most q queens can be placed
00099     nvalues(*this, b, IRT_LQ, q);
00100 
00101     /*
00102      * According to: P. R. J. Östergard and W. D. Weakley, Values
00103      * of Domination Numbers of the Queen's Graph, Electronic Journal
00104      * of Combinatorics, 8:1-19, 2001, for n <= 120, the minimal number
00105      * of queens is either ceil(n/2) or ceil(n/2 + 1).
00106      */
00107     if (n <= 120)
00108       dom(*this, q, (n+1)/2, (n+1)/2 + 1);
00109 
00110     branch(*this, b, INT_VAR_SIZE_MIN(), INT_VAL_MIN());
00111     // Try the easier solution first
00112     branch(*this, q, INT_VAL_MAX());
00113   }
00114 
00116   virtual IntVar cost(void) const {
00117     return q;
00118   }
00119 
00121   DominatingQueens(bool share, DominatingQueens& s)
00122     : IntMinimizeScript(share,s), n(s.n) {
00123     b.update(*this, share, s.b);
00124     q.update(*this, share, s.q);
00125   }
00126 
00128   virtual Space*
00129   copy(bool share) {
00130     return new DominatingQueens(share,*this);
00131   }
00132 
00134   virtual void
00135   print(std::ostream& os) const {
00136     os << "\tNumber of Queens: " << q << std::endl;
00137     os << "\tBoard: " << b << std::endl;
00138     if (b.assigned()) {
00139       // Print a nice board
00140       bool* placed = new bool[n*n];
00141       for (int i=0; i<n*n; i++)
00142         placed[i] = false;
00143       for (int i=0; i<n*n; i++)
00144         placed[b[i].val()] = true;
00145       for (int j=0; j<n; j++) {
00146         std::cout << "\t\t";
00147         for (int i=0; i<n; i++)
00148           std::cout << (placed[xy(i,j)] ? 'Q' : '.') << ' ';
00149         std::cout << std::endl;
00150       }
00151       delete [] placed;
00152     }
00153     os << std::endl;
00154   }
00155 };
00156 
00160 int
00161 main(int argc, char* argv[]) {
00162   SizeOptions opt("DominatingQueens");
00163   opt.size(7);
00164   opt.solutions(0);
00165   opt.parse(argc,argv);
00166   IntMinimizeScript::run<DominatingQueens,BAB,SizeOptions>(opt);
00167   return 0;
00168 }
00169 
00170 // STATISTICS: example-any
00171