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

magic-square.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2001
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-04-11 15:58:37 +0200 (Tue, 11 Apr 2006) $ by $Author: tack $
00010  *     $Revision: 3188 $
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 
00033 class MagicSquare : public Example {
00034 private:
00036   const int n;
00038   IntVarArray x;
00039 
00040 public:
00042   MagicSquare(const Options& opt)
00043     : n(opt.size), x(this,n*n,1,n*n) {
00044     // Number of fields on square
00045     const int nn = n*n;
00046 
00047     // Sum of all a row, column, or diagonal
00048     const int s  = nn*(nn+1) / (2*n);
00049 
00050     // Matrix-wrapper for the square
00051     MiniModel::Matrix<IntVarArray> m(x, n, n);
00052 
00053     for (int i = n; i--; ) {
00054         linear(this, m.row(i), IRT_EQ, s, opt.icl);
00055         linear(this, m.col(i), IRT_EQ, s, opt.icl);
00056     }
00057     // Both diagonals must have sum s
00058     {
00059       IntVarArgs d1y(n);
00060       IntVarArgs d2y(n);
00061       for (int i = n; i--; ) {
00062         d1y[i] = m(i,i);
00063         d2y[i] = m(n-i-1,i);
00064       }
00065       linear(this, d1y, IRT_EQ, s, opt.icl);
00066       linear(this, d2y, IRT_EQ, s, opt.icl);
00067     }
00068 
00069     // All fields must be distinct
00070     distinct(this, x, opt.icl);
00071 
00072     // Break some (few) symmetries
00073     rel(this, m(0,0), IRT_GR, m(0,n-1));
00074     rel(this, m(0,0), IRT_GR, m(n-1,0));
00075 
00076     branch(this, x, BVAR_SIZE_MIN, BVAL_SPLIT_MIN);
00077   }
00078 
00080   MagicSquare(bool share, MagicSquare& s) : Example(share,s), n(s.n) {
00081     x.update(this, share, s.x);
00082   }
00083 
00085   virtual Space*
00086   copy(bool share) {
00087     return new MagicSquare(share,*this);
00088   }
00090   virtual void
00091   print(void) {
00092     // Matrix-wrapper for the square
00093     MiniModel::Matrix<IntVarArray> m(x, n, n);
00094     for (int i = 0; i<n; i++) {
00095       std::cout << "\t";
00096       for (int j = 0; j<n; j++) {
00097         std::cout.width(2);
00098         std::cout << m(i,j) << "  ";
00099       }
00100       std::cout << std::endl;
00101     }
00102   }
00103 
00104 };
00105 
00109 int
00110 main(int argc, char** argv) {
00111   Options opt("MagicSquare");
00112   opt.iterations = 1;
00113   opt.size       = 7;
00114   opt.parse(argc,argv);
00115   Example::run<MagicSquare,DFS>(opt);
00116   return 0;
00117 }
00118 
00119 // STATISTICS: example-any
00120