Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.0
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
37using namespace Gecode;
38
44class OrthoLatinSquare : public Script {
45protected:
47 const int n;
52
53public:
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
169int
170main(int argc, char* argv[]) {
171 SizeOptions opt("OrthoLatinSquare");
172 opt.size(7);
173 opt.ipl(IPL_DOM);
174 opt.parse(argc,argv);
176 return 0;
177}
178
179// STATISTICS: example-any
180
Parametric base-class for scripts.
Definition driver.hh:729
static void run(const Options &opt, Script *s=NULL)
Definition script.hpp:290
Passing integer arguments.
Definition int.hh:628
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Integer variables.
Definition int.hh:371
Options for scripts with additional size parameter
Definition driver.hh:675
Computation spaces.
Definition core.hpp:1742
void update(Space &home, VarArray< Var > &a)
Update array to be a clone of array a.
Definition array.hpp:1013
Example: Orthogonal latin squares
int main(int argc, char *argv[])
Main function.
IntVarArray x2
Fields of second square.
const IntVar & y1(int i, int j) const
Access field at position i and j in first square.
virtual Space * copy(void)
Copy during cloning.
const IntVar & y2(int i, int j) const
Access field at position i and j in second square.
IntVar & y1(int i, int j)
Access field at position i and j in first square.
virtual void print(std::ostream &os) const
Print solution.
const int n
Size of squares.
IntVar & y2(int i, int j)
Access field at position i and j in second square.
IntVarArray x1
Fields of first square.
OrthoLatinSquare(const SizeOptions &opt)
Actual model.
OrthoLatinSquare(OrthoLatinSquare &s)
Constructor for cloning s.
void parse(int argc, char *argv[])
Parse commandline arguments.
Definition test.cpp:120
void branch(Home home, const FloatVarArgs &x, FloatVarBranch vars, FloatValBranch vals, FloatBranchFilter bf=nullptr, FloatVarValPrint vvp=nullptr)
Branch over x with variable selection vars and value selection vals.
Definition branch.cpp:39
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVar x1)
Post propagator for .
Definition rel.cpp:68
@ IRT_GQ
Greater or equal ( )
Definition int.hh:930
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
Gecode toplevel namespace
IntValBranch INT_VAL_SPLIT_MIN(void)
Select values not greater than mean of smallest and largest value.
Definition val.hpp:75
void mod(Home home, IntVar x0, IntVar x1, IntVar x2, IntPropLevel ipl=IPL_DEF)
Post propagator for .
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
void div(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void element(Home home, IntSharedArray n, IntVar x0, IntVar x1, IntPropLevel ipl=IPL_DEF)
Post domain consistent propagator for .
Definition element.cpp:39
Post propagator for SetVar SetOpType SetVar SetRelType SetVar z
Definition set.hh:767
IntVarBranch INT_VAR_SIZE_MIN(BranchTbl tbl=nullptr)
Select variable with smallest domain size.
Definition var.hpp:206