Generated on Wed Mar 25 2020 20:15:55 for Gecode by doxygen 1.8.5
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: 2015-03-17 16:09:39 +0100 (Tue, 17 Mar 2015) $ by $Author: schulte $
11  * $Revision: 14447 $
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  : Script(opt),
78  n(opt.size()),
79  x1(*this,n*n,1,n), x2(*this,n*n,1,n) {
80  const int nn = n*n;
81  IntVarArgs z(*this,nn,0,n*n-1);
82 
83  distinct(*this, z, opt.icl());
84  // Connect
85  {
86  IntArgs mod(n*n);
87  IntArgs div(n*n);
88  for (int i=0; i<n; i++)
89  for (int j=0; j<n; j++) {
90  mod[i*n+j] = j+1;
91  div[i*n+j] = i+1;
92  }
93  for (int i = nn; i--; ) {
94  element(*this, div, z[i], x2[i]);
95  element(*this, mod, z[i], x1[i]);
96  }
97  }
98 
99  // Rows
100  for (int i = n; i--; ) {
101  IntVarArgs ry(n);
102  for (int j = n; j--; )
103  ry[j] = y1(i,j);
104  distinct(*this, ry, opt.icl());
105  for (int j = n; j--; )
106  ry[j] = y2(i,j);
107  distinct(*this, ry, opt.icl());
108  }
109  for (int j = n; j--; ) {
110  IntVarArgs cy(n);
111  for (int i = n; i--; )
112  cy[i] = y1(i,j);
113  distinct(*this, cy, opt.icl());
114  for (int i = n; i--; )
115  cy[i] = y2(i,j);
116  distinct(*this, cy, opt.icl());
117  }
118 
119  for (int i = 1; i<n; i++) {
120  IntVarArgs ry1(n);
121  IntVarArgs ry2(n);
122  for (int j = n; j--; ) {
123  ry1[j] = y1(i-1,j);
124  ry2[j] = y2(i,j);
125  }
126  rel(*this, ry1, IRT_GQ, ry2);
127  }
128 
129  branch(*this, z, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN());
130  }
131 
134  : Script(share,s), n(s.n) {
135  x1.update(*this, share, s.x1);
136  x2.update(*this, share, s.x2);
137  }
138 
140  virtual Space*
141  copy(bool share) {
142  return new OrthoLatinSquare(share,*this);
143  }
145  virtual void
146  print(std::ostream& os) const {
147  for (int i = 0; i<n; i++) {
148  os << "\t";
149  for (int j = 0; j<n; j++) {
150  os.width(2);
151  os << y1(i,j) << " ";
152  }
153  os << std::endl;
154  }
155  os << std::endl;
156  for (int i = 0; i<n; i++) {
157  os << "\t";
158  for (int j = 0; j<n; j++) {
159  os.width(2);
160  os << y2(i,j) << " ";
161  }
162  os << std::endl;
163  }
164  os << std::endl;
165  }
166 
167 };
168 
173 int
174 main(int argc, char* argv[]) {
175  SizeOptions opt("OrthoLatinSquare");
176  opt.size(7);
177  opt.icl(ICL_DOM);
178  opt.parse(argc,argv);
179  Script::run<OrthoLatinSquare,DFS,SizeOptions>(opt);
180  return 0;
181 }
182 
183 // STATISTICS: example-any
184 
void size(unsigned int s)
Set default size.
Definition: options.hpp:485
Options for scripts with additional size parameter
Definition: driver.hh:579
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:437
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:633
const IntVar & y2(int i, int j) const
Access field at position i and j in second square.
Definition: ortho-latin.cpp:71
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
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