this repo has no description
at develop 7.4 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, 2011 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/minimodel.hh> 35#include "test/int.hh" 36 37namespace Test { namespace Int { 38 39 /// %Tests for number of values constraints 40 namespace NValues { 41 42 /** 43 * \defgroup TaskTestIntNValues Number of values constraints 44 * \ingroup TaskTestInt 45 */ 46 //@{ 47 /// %Test number of values of integer variables equal to integer 48 class IntInt : public Test { 49 protected: 50 /// Integer relation type to propagate 51 Gecode::IntRelType irt; 52 /// Number of values 53 int m; 54 public: 55 /// Create and register test 56 IntInt(int n, int m0, Gecode::IntRelType irt0) 57 : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0), 58 n,0,n), 59 irt(irt0), m(m0) { 60 testfix = false; 61 if (arity > 5) 62 testsearch = false; 63 } 64 /// Create and register initial assignment 65 virtual Assignment* assignment(void) const { 66 if (arity > 5) 67 return new RandomAssignment(arity, dom, 500, _rand); 68 else 69 return new CpltAssignment(arity,dom); 70 } 71 /// %Test whether \a x is solution 72 virtual bool solution(const Assignment& x) const { 73 int n = x.size(); 74 bool* v = new bool[n+1]; 75 for (int i=n+1; i--; ) 76 v[i] = false; 77 int k = 0; 78 for (int i=n; i--; ) 79 if (!v[x[i]]) { 80 k++; 81 v[x[i]] = true; 82 } 83 delete [] v; 84 return cmp(k,irt,m); 85 } 86 /// Post constraint on \a x 87 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 88 Gecode::nvalues(home, x, irt, m); 89 } 90 }; 91 92 /// %Test number of values of integer variables equal to integer variable 93 class IntVar : public Test { 94 protected: 95 /// Integer relation type to propagate 96 Gecode::IntRelType irt; 97 public: 98 /// Create and register test 99 IntVar(int n, Gecode::IntRelType irt0) 100 : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n), 101 irt(irt0) { 102 testfix = false; 103 } 104 /// %Test whether \a x is solution 105 virtual bool solution(const Assignment& x) const { 106 int n = x.size() - 1; 107 bool* v = new bool[n+1]; 108 for (int i=n+1; i--; ) 109 v[i] = false; 110 int k = 0; 111 for (int i=n; i--; ) 112 if (!v[x[i]]) { 113 k++; 114 v[x[i]] = true; 115 } 116 delete [] v; 117 return cmp(k,irt,x[n]); 118 } 119 /// Post constraint on \a xy 120 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) { 121 int n = xy.size() - 1; 122 Gecode::IntVarArgs x(n); 123 for (int i=n; i--; ) 124 x[i] = xy[i]; 125 Gecode::nvalues(home, x, irt, xy[n]); 126 } 127 }; 128 129 /// %Test number of values of Boolean variables equal to integer 130 class BoolInt : public Test { 131 protected: 132 /// Integer relation type to propagate 133 Gecode::IntRelType irt; 134 /// Number of values 135 int m; 136 public: 137 /// Create and register test 138 BoolInt(int n, int m0, Gecode::IntRelType irt0) 139 : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0), 140 n,0,2), 141 irt(irt0), m(m0) {} 142 /// %Test whether \a x is solution 143 virtual bool solution(const Assignment& x) const { 144 int n = x.size(); 145 for (int i=n; i--; ) 146 if (x[i] > 1) 147 return false; 148 bool* v = new bool[n+1]; 149 for (int i=n+1; i--; ) 150 v[i] = false; 151 int k = 0; 152 for (int i=n; i--; ) 153 if (!v[x[i]]) { 154 k++; 155 v[x[i]] = true; 156 } 157 delete [] v; 158 return cmp(k,irt,m); 159 } 160 /// Post constraint on \a x 161 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) { 162 using namespace Gecode; 163 BoolVarArgs y(x.size()); 164 for (int i=x.size(); i--; ) 165 y[i] = channel(home, x[i]); 166 nvalues(home, y, irt, m); 167 } 168 }; 169 170 /// %Test number of values of Boolean variables equal to integer variable 171 class BoolVar : public Test { 172 protected: 173 /// Integer relation type to propagate 174 Gecode::IntRelType irt; 175 public: 176 /// Create and register test 177 BoolVar(int n, Gecode::IntRelType irt0) 178 : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2), 179 irt(irt0) {} 180 /// %Test whether \a x is solution 181 virtual bool solution(const Assignment& x) const { 182 int n = x.size() - 1; 183 for (int i=n; i--; ) 184 if (x[i] > 1) 185 return false; 186 bool* v = new bool[n+1]; 187 for (int i=n+1; i--; ) 188 v[i] = false; 189 int k = 0; 190 for (int i=n; i--; ) 191 if (!v[x[i]]) { 192 k++; 193 v[x[i]] = true; 194 } 195 delete [] v; 196 return cmp(k,irt,x[n]); 197 } 198 /// Post constraint on \a xy 199 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) { 200 using namespace Gecode; 201 int n = xy.size() - 1; 202 BoolVarArgs x(n); 203 for (int i=n; i--; ) 204 x[i] = channel(home, xy[i]); 205 nvalues(home, x, irt, xy[n]); 206 } 207 }; 208 209 /// Help class to create and register tests 210 class Create { 211 public: 212 /// Perform creation and registration 213 Create(void) { 214 for (IntRelTypes irts; irts(); ++irts) { 215 for (int i=1; i<=7; i += 3) { 216 for (int m=0; m<=3; m++) 217 (void) new BoolInt(i, m, irts.irt()); 218 (void) new BoolVar(i, irts.irt()); 219 } 220 for (int i=1; i<=7; i += 2) { 221 for (int m=0; m<=i+1; m++) 222 (void) new IntInt(i, m, irts.irt()); 223 if (i <= 5) 224 (void) new IntVar(i, irts.irt()); 225 } 226 } 227 } 228 }; 229 230 Create c; 231 //@} 232 233 } 234}} 235 236// STATISTICS: test-int 237