Generated on Sun Aug 9 2020 05:34:08 for Gecode by doxygen 1.8.18
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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include <gecode/driver.hh>
35 #include <gecode/int.hh>
36 
37 using namespace Gecode;
38 
44 class OrthoLatinSquare : public Script {
45 protected:
47  const int n;
52 
53 public:
55  IntVar& y1(int i, int j) {
56  return x1[i*n+j];
57  }
59  const IntVar& y1(int i, int j) const {
60  return x1[i*n+j];
61  }
63  IntVar& y2(int i, int j) {
64  return x2[i*n+j];
65  }
67  const IntVar& y2(int i, int j) const {
68  return x2[i*n+j];
69  }
70 
73  : Script(opt),
74  n(opt.size()),
75  x1(*this,n*n,1,n), x2(*this,n*n,1,n) {
76  const int nn = n*n;
77  IntVarArgs z(*this,nn,0,n*n-1);
78 
79  distinct(*this, z, opt.ipl());
80  // Connect
81  {
82  IntArgs mod(n*n);
83  IntArgs div(n*n);
84  for (int i=0; i<n; i++)
85  for (int j=0; j<n; j++) {
86  mod[i*n+j] = j+1;
87  div[i*n+j] = i+1;
88  }
89  for (int i = nn; i--; ) {
90  element(*this, div, z[i], x2[i]);
91  element(*this, mod, z[i], x1[i]);
92  }
93  }
94 
95  // Rows
96  for (int i = n; i--; ) {
97  IntVarArgs ry(n);
98  for (int j = n; j--; )
99  ry[j] = y1(i,j);
100  distinct(*this, ry, opt.ipl());
101  for (int j = n; j--; )
102  ry[j] = y2(i,j);
103  distinct(*this, ry, opt.ipl());
104  }
105  for (int j = n; j--; ) {
106  IntVarArgs cy(n);
107  for (int i = n; i--; )
108  cy[i] = y1(i,j);
109  distinct(*this, cy, opt.ipl());
110  for (int i = n; i--; )
111  cy[i] = y2(i,j);
112  distinct(*this, cy, opt.ipl());
113  }
114 
115  for (int i = 1; i<n; i++) {
116  IntVarArgs ry1(n);
117  IntVarArgs ry2(n);
118  for (int j = n; j--; ) {
119  ry1[j] = y1(i-1,j);
120  ry2[j] = y2(i,j);
121  }
122  rel(*this, ry1, IRT_GQ, ry2);
123  }
124 
126  }
127 
130  : Script(s), n(s.n) {
131  x1.update(*this, s.x1);
132  x2.update(*this, s.x2);
133  }
134 
136  virtual Space*
137  copy(void) {
138  return new OrthoLatinSquare(*this);
139  }
141  virtual void
142  print(std::ostream& os) const {
143  for (int i = 0; i<n; i++) {
144  os << "\t";
145  for (int j = 0; j<n; j++) {
146  os.width(2);
147  os << y1(i,j) << " ";
148  }
149  os << std::endl;
150  }
151  os << std::endl;
152  for (int i = 0; i<n; i++) {
153  os << "\t";
154  for (int j = 0; j<n; j++) {
155  os.width(2);
156  os << y2(i,j) << " ";
157  }
158  os << std::endl;
159  }
160  os << std::endl;
161  }
162 
163 };
164 
169 int
170 main(int argc, char* argv[]) {
171  SizeOptions opt("OrthoLatinSquare");
172  opt.size(7);
173  opt.ipl(IPL_DOM);
174  opt.parse(argc,argv);
175  Script::run<OrthoLatinSquare,DFS,SizeOptions>(opt);
176  return 0;
177 }
178 
179 // STATISTICS: example-any
180 
OrthoLatinSquare(OrthoLatinSquare &s)
Constructor for cloning s.
virtual Space * copy(void)
Copy during cloning.
@ IRT_GQ
Greater or equal ( )
Definition: int.hh:930
Passing integer variables.
Definition: int.hh:656
void element(Home home, IntSharedArray c, IntVar x0, IntVar x1, IntPropLevel)
Post domain consistent propagator for .
Definition: element.cpp:39
unsigned int size(I &i)
Size of all ranges of range iterator i.
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition: set.hh:767
void ipl(IntPropLevel i)
Set default integer propagation level.
Definition: options.hpp:216
Computation spaces.
Definition: core.hpp:1742
void 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:39
const int n
Size of squares.
Definition: ortho-latin.cpp:47
Integer variable array.
Definition: int.hh:763
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Definition: arithmetic.cpp:127
Gecode toplevel namespace
Options opt
The options.
Definition: test.cpp:97
Parametric base-class for scripts.
Definition: driver.hh:729
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
void parse(int &argc, char *argv[])
Parse options from arguments argv (number is argc)
Definition: options.cpp:548
virtual void print(std::ostream &os) const
Print solution.
Integer variables.
Definition: int.hh:371
Example: Orthogonal latin squares
Definition: ortho-latin.cpp:44
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl)
Select variable with smallest domain size.
Definition: var.hpp:206
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:43
int main(int argc, char *argv[])
Main function.
IntVarArray x1
Fields of first square.
Definition: ortho-latin.cpp:49
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl)
Post propagator for .
Definition: arithmetic.cpp:360
IntVar & y2(int i, int j)
Access field at position i and j in second square.
Definition: ortho-latin.cpp:63
IntVar & y1(int i, int j)
Access field at position i and j in first square.
Definition: ortho-latin.cpp:55
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition: val.hpp:75
OrthoLatinSquare(const SizeOptions &opt)
Actual model.
Definition: ortho-latin.cpp:72
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
IntVarArray x2
Fields of second square.
Definition: ortho-latin.cpp:51
const IntVar & y1(int i, int j) const
Access field at position i and j in first square.
Definition: ortho-latin.cpp:59
Passing integer arguments.
Definition: int.hh:628
Gecode::IntArgs i({1, 2, 3, 4})
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition: array.hpp:1013
const IntVar & y2(int i, int j) const
Access field at position i and j in second square.
Definition: ortho-latin.cpp:67
Options for scripts with additional size parameter
Definition: driver.hh:675