Generated on Sat Feb 7 2015 02:01:12 for Gecode by doxygen 1.8.9.1
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: 2013-07-08 14:22:40 +0200 (Mon, 08 Jul 2013) $ by $Author: schulte $
11  * $Revision: 13820 $
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 
128  branch(*this, z, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN());
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 
void size(unsigned int s)
Set default size.
Definition: options.hpp:467
Options for scripts with additional size parameter
Definition: driver.hh:567
IntVar & y2(int i, int j)
Access field at position i and j in second square.
Definition: ortho-latin.cpp:67
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntConLevel icl)
Post propagator for .
Definition: arithmetic.cpp:213
Example: Orthogonal latin squares
Definition: ortho-latin.cpp:48
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:435
IntVarArray x1
Fields of first square.
Definition: ortho-latin.cpp:53
int main(int argc, char *argv[])
Main function.
OrthoLatinSquare(bool share, OrthoLatinSquare &s)
Constructor for cloning s.
virtual void print(std::ostream &os) const
Print solution.
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:212
OrthoLatinSquare(const SizeOptions &opt)
Actual model.
Definition: ortho-latin.cpp:76
Integer variable array.
Definition: int.hh:741
IntVar & y1(int i, int j)
Access field at position i and j in first square.
Definition: ortho-latin.cpp:59
Computation spaces.
Definition: core.hpp:1362
Greater or equal ( )
Definition: int.hh:908
Parametric base-class for scripts.
Definition: driver.hh:622
const IntVar & y2(int i, int j) const
Access field at position i and j in second square.
Definition: ortho-latin.cpp:71
void update(Space &, bool share, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1072
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Options opt
The options.
Definition: test.cpp:101
const IntVar & y1(int i, int j) const
Access field at position i and j in first square.
Definition: ortho-latin.cpp:63
IntVarArray x2
Fields of second square.
Definition: ortho-latin.cpp:55
virtual Space * copy(bool share)
Copy during cloning.
unsigned int size(I &i)
Size of all ranges of range iterator i.
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntConLevel)
Post domain consistent propagator for .
Definition: element.cpp:43
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:135
const int n
Size of squares.
Definition: ortho-latin.cpp:51
Integer variables.
Definition: int.hh:350
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition: val.hpp:88
Gecode toplevel namespace
BrancherHandle branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf, FloatVarValPrint vvp)
Branch over x with variable selection vars and value selection vals.
Definition: branch.cpp:43
void icl(IntConLevel i)
Set default integer consistency level.
Definition: options.hpp:194
Domain propagation or consistency.
Definition: int.hh:940