Generated on Sat Aug 25 2012 03:32:42 for Gecode by doxygen 1.8.1.2
ortho-latin.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  *
6  * Copyright:
7  * Christian Schulte, 2004
8  *
9  * Last modified:
10  * $Date: 2010-10-07 20:52:01 +1100 (Thu, 07 Oct 2010) $ by $Author: schulte $
11  * $Revision: 11473 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include <gecode/driver.hh>
39 #include <gecode/int.hh>
40 
41 using namespace Gecode;
42 
48 class OrthoLatinSquare : public Script {
49 protected:
51  const int n;
56 
57 public:
59  IntVar& y1(int i, int j) {
60  return x1[i*n+j];
61  }
63  const IntVar& y1(int i, int j) const {
64  return x1[i*n+j];
65  }
67  IntVar& y2(int i, int j) {
68  return x2[i*n+j];
69  }
71  const IntVar& y2(int i, int j) const {
72  return x2[i*n+j];
73  }
74 
77  : n(opt.size()),
78  x1(*this,n*n,1,n), x2(*this,n*n,1,n) {
79  const int nn = n*n;
80  IntVarArgs z(*this,nn,0,n*n-1);
81 
82  distinct(*this, z, opt.icl());
83  // Connect
84  {
85  IntArgs mod(n*n);
86  IntArgs div(n*n);
87  for (int i=0; i<n; i++)
88  for (int j=0; j<n; j++) {
89  mod[i*n+j] = j+1;
90  div[i*n+j] = i+1;
91  }
92  for (int i = nn; i--; ) {
93  element(*this, div, z[i], x2[i]);
94  element(*this, mod, z[i], x1[i]);
95  }
96  }
97 
98  // Rows
99  for (int i = n; i--; ) {
100  IntVarArgs ry(n);
101  for (int j = n; j--; )
102  ry[j] = y1(i,j);
103  distinct(*this, ry, opt.icl());
104  for (int j = n; j--; )
105  ry[j] = y2(i,j);
106  distinct(*this, ry, opt.icl());
107  }
108  for (int j = n; j--; ) {
109  IntVarArgs cy(n);
110  for (int i = n; i--; )
111  cy[i] = y1(i,j);
112  distinct(*this, cy, opt.icl());
113  for (int i = n; i--; )
114  cy[i] = y2(i,j);
115  distinct(*this, cy, opt.icl());
116  }
117 
118  for (int i = 1; i<n; i++) {
119  IntVarArgs ry1(n);
120  IntVarArgs ry2(n);
121  for (int j = n; j--; ) {
122  ry1[j] = y1(i-1,j);
123  ry2[j] = y2(i,j);
124  }
125  rel(*this, ry1, IRT_GQ, ry2);
126  }
127 
129  }
130 
133  : Script(share,s), n(s.n) {
134  x1.update(*this, share, s.x1);
135  x2.update(*this, share, s.x2);
136  }
137 
139  virtual Space*
140  copy(bool share) {
141  return new OrthoLatinSquare(share,*this);
142  }
144  virtual void
145  print(std::ostream& os) const {
146  for (int i = 0; i<n; i++) {
147  os << "\t";
148  for (int j = 0; j<n; j++) {
149  os.width(2);
150  os << y1(i,j) << " ";
151  }
152  os << std::endl;
153  }
154  os << std::endl;
155  for (int i = 0; i<n; i++) {
156  os << "\t";
157  for (int j = 0; j<n; j++) {
158  os.width(2);
159  os << y2(i,j) << " ";
160  }
161  os << std::endl;
162  }
163  os << std::endl;
164  }
165 
166 };
167 
172 int
173 main(int argc, char* argv[]) {
174  SizeOptions opt("OrthoLatinSquare");
175  opt.size(7);
176  opt.icl(ICL_DOM);
177  opt.parse(argc,argv);
178  Script::run<OrthoLatinSquare,DFS,SizeOptions>(opt);
179  return 0;
180 }
181 
182 // STATISTICS: example-any
183