00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
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
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
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
00172