Generated on Mon Aug 25 11:35:33 2008 for Gecode by doxygen 1.5.6

ortho-latin.cc

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, 2004
00008  *
00009  *  Last modified:
00010  *     $Date: 2007-11-30 13:58:34 +0100 (Fri, 30 Nov 2007) $ by $Author: tack $
00011  *     $Revision: 5524 $
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 "examples/support.hh"
00039 
00045 class OrthoLatinSquare : public Example {
00046 protected:
00048   const int n;
00050   IntVarArray x1;
00052   IntVarArray x2;
00053 
00054 public:
00056   IntVar& y1(int i, int j) {
00057     return x1[i*n+j];
00058   }
00060   IntVar& y2(int i, int j) {
00061     return x2[i*n+j];
00062   }
00063 
00065   OrthoLatinSquare(const SizeOptions& opt)
00066     : n(opt.size()),
00067       x1(this,n*n,1,n), x2(this,n*n,1,n) {
00068     const int nn = n*n;
00069     IntVarArray z(this,nn,0,n*n-1);
00070 
00071     distinct(this, z, opt.icl());
00072     // Connect
00073     {
00074       IntArgs mod(n*n);
00075       IntArgs div(n*n);
00076       for (int i=0; i<n; i++)
00077         for (int j=0; j<n; j++) {
00078           mod[i*n+j] = j+1;
00079           div[i*n+j] = i+1;
00080         }
00081       for (int i = nn; i--; ) {
00082         element(this, div, z[i], x2[i]);
00083         element(this, mod, z[i], x1[i]);
00084       }
00085     }
00086 
00087     // Rows
00088     for (int i = n; i--; ) {
00089       IntVarArgs ry(n);
00090       for (int j = n; j--; )
00091         ry[j] = y1(i,j);
00092       distinct(this, ry, opt.icl());
00093       for (int j = n; j--; )
00094         ry[j] = y2(i,j);
00095       distinct(this, ry, opt.icl());
00096     }
00097     for (int j = n; j--; ) {
00098       IntVarArgs cy(n);
00099       for (int i = n; i--; )
00100         cy[i] = y1(i,j);
00101       distinct(this, cy, opt.icl());
00102       for (int i = n; i--; )
00103         cy[i] = y2(i,j);
00104       distinct(this, cy, opt.icl());
00105     }
00106 
00107     for (int i = 1; i<n; i++) {
00108       IntVarArgs ry1(n);
00109       IntVarArgs ry2(n);
00110       for (int j = n; j--; ) {
00111         ry1[j] = y1(i-1,j);
00112         ry2[j] = y2(i,j);
00113       }
00114       rel(this, ry1, IRT_GQ, ry2);
00115     }
00116 
00117     branch(this, z, INT_VAR_SIZE_MIN, INT_VAL_SPLIT_MIN);
00118   }
00119 
00121   OrthoLatinSquare(bool share, OrthoLatinSquare& s)
00122     : Example(share,s), n(s.n) {
00123       x1.update(this, share, s.x1);
00124       x2.update(this, share, s.x2);
00125   }
00126 
00128   virtual Space*
00129   copy(bool share) {
00130     return new OrthoLatinSquare(share,*this);
00131   }
00133   virtual void
00134   print(std::ostream& os) {
00135     for (int i = 0; i<n; i++) {
00136       os << "\t";
00137       for (int j = 0; j<n; j++) {
00138         os.width(2);
00139         os << y1(i,j) << "  ";
00140       }
00141       os << std::endl;
00142     }
00143     os << std::endl;
00144     for (int i = 0; i<n; i++) {
00145       os << "\t";
00146       for (int j = 0; j<n; j++) {
00147         os.width(2);
00148         os << y2(i,j) << "  ";
00149       }
00150       os << std::endl;
00151     }
00152     os << std::endl;
00153   }
00154 
00155 };
00156 
00161 int
00162 main(int argc, char* argv[]) {
00163   SizeOptions opt("OrthoLatinSquare");
00164   opt.size(7);
00165   opt.icl(ICL_DOM);
00166   opt.parse(argc,argv);
00167   Example::run<OrthoLatinSquare,DFS,SizeOptions>(opt);
00168   return 0;
00169 }
00170 
00171 // STATISTICS: example-any
00172