this repo has no description
at develop 4.6 kB view raw
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 39/** 40 * \brief %Example: Orthogonal latin squares 41 * 42 * \ingroup Example 43 */ 44class OrthoLatinSquare : public Script { 45protected: 46 /// Size of squares 47 const int n; 48 /// Fields of first square 49 IntVarArray x1; 50 /// Fields of second square 51 IntVarArray x2; 52 53public: 54 /// Access field at position \a i and \a j in first square 55 IntVar& y1(int i, int j) { 56 return x1[i*n+j]; 57 } 58 /// Access field at position \a i and \a j in first square 59 const IntVar& y1(int i, int j) const { 60 return x1[i*n+j]; 61 } 62 /// Access field at position \a i and \a j in second square 63 IntVar& y2(int i, int j) { 64 return x2[i*n+j]; 65 } 66 /// Access field at position \a i and \a j in second square 67 const IntVar& y2(int i, int j) const { 68 return x2[i*n+j]; 69 } 70 71 /// Actual model 72 OrthoLatinSquare(const SizeOptions& opt) 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 125 branch(*this, z, INT_VAR_SIZE_MIN(), INT_VAL_SPLIT_MIN()); 126 } 127 128 /// Constructor for cloning \a s 129 OrthoLatinSquare(OrthoLatinSquare& s) 130 : Script(s), n(s.n) { 131 x1.update(*this, s.x1); 132 x2.update(*this, s.x2); 133 } 134 135 /// Copy during cloning 136 virtual Space* 137 copy(void) { 138 return new OrthoLatinSquare(*this); 139 } 140 /// Print solution 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 165/** 166 * \brief Main function 167 * \relates OrthoLatinSquare 168 */ 169int 170main(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