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

ortho-latin.cc

Go to the documentation of this file.
00001 /*
00002  *  Main authors:
00003  *     Christian Schulte <schulte@gecode.org>
00004  *
00005  *  Copyright:
00006  *     Christian Schulte, 2004
00007  *
00008  *  Last modified:
00009  *     $Date: 2006-08-31 17:36:38 +0200 (Thu, 31 Aug 2006) $ by $Author: schulte $
00010  *     $Revision: 3579 $
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 
00029 class OrthoLatinSquare : public Example {
00030 protected:
00032   const int n;
00034   IntVarArray x1;
00036   IntVarArray x2;
00037 
00038 public:
00040   IntVar& y1(int i, int j) {
00041     return x1[i*n+j];
00042   }
00044   IntVar& y2(int i, int j) {
00045     return x2[i*n+j];
00046   }
00047 
00049   OrthoLatinSquare(const Options& opt)
00050     : n(opt.size),
00051       x1(this,n*n,1,n), x2(this,n*n,1,n) {
00052     const int nn = n*n;
00053     IntVarArray z(this,nn,0,n*n-1);
00054 
00055     distinct(this, z, opt.icl);
00056     // Connect
00057     {
00058       IntArgs mod(n*n);
00059       IntArgs div(n*n);
00060       for (int i=0; i<n; i++)
00061         for (int j=0; j<n; j++) {
00062           mod[i*n+j] = j+1;
00063           div[i*n+j] = i+1;
00064         }
00065       for (int i = nn; i--; ) {
00066         element(this, div, z[i], x2[i]);
00067         element(this, mod, z[i], x1[i]);
00068       }
00069     }
00070 
00071     // Rows
00072     for (int i = n; i--; ) {
00073       IntVarArgs ry(n);
00074       for (int j = n; j--; )
00075         ry[j] = y1(i,j);
00076       distinct(this, ry, opt.icl);
00077       for (int j = n; j--; )
00078         ry[j] = y2(i,j);
00079       distinct(this, ry, opt.icl);
00080     }
00081     for (int j = n; j--; ) {
00082       IntVarArgs cy(n);
00083       for (int i = n; i--; )
00084         cy[i] = y1(i,j);
00085       distinct(this, cy, opt.icl);
00086       for (int i = n; i--; )
00087         cy[i] = y2(i,j);
00088       distinct(this, cy, opt.icl);
00089     }
00090 
00091     for (int i = 1; i<n; i++) {
00092       IntVarArgs ry1(n);
00093       IntVarArgs ry2(n);
00094       for (int j = n; j--; ) {
00095         ry1[j] = y1(i-1,j);
00096         ry2[j] = y2(i,j);
00097       }
00098       rel(this, ry1, IRT_GQ, ry2);
00099     }
00100 
00101     branch(this, z, BVAR_SIZE_MIN, BVAL_SPLIT_MIN);
00102   }
00103 
00105   OrthoLatinSquare(bool share, OrthoLatinSquare& s)
00106     : Example(share,s), n(s.n) {
00107       x1.update(this, share, s.x1);
00108       x2.update(this, share, s.x2);
00109   }
00110 
00112   virtual Space*
00113   copy(bool share) {
00114     return new OrthoLatinSquare(share,*this);
00115   }
00117   virtual void
00118   print(void) {
00119     for (int i = 0; i<n; i++) {
00120       std::cout << "\t";
00121       for (int j = 0; j<n; j++) {
00122         std::cout.width(2);
00123         std::cout << y1(i,j) << "  ";
00124       }
00125       std::cout << std::endl;
00126     }
00127     std::cout << std::endl;
00128     for (int i = 0; i<n; i++) {
00129       std::cout << "\t";
00130       for (int j = 0; j<n; j++) {
00131         std::cout.width(2);
00132         std::cout << y2(i,j) << "  ";
00133       }
00134       std::cout << std::endl;
00135     }
00136     std::cout << std::endl;
00137   }
00138 
00139 };
00140 
00145 int
00146 main(int argc, char** argv) {
00147   Options opt("OrthoLatinSquare");
00148   opt.size = 7;
00149   opt.icl  = ICL_DOM;
00150   opt.parse(argc,argv);
00151   Example::run<OrthoLatinSquare,DFS>(opt);
00152   return 0;
00153 }
00154 
00155 // STATISTICS: example-any
00156