this repo has no description
at develop 11 kB view raw
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Guido Tack <tack@gecode.org> 5 * 6 * Copyright: 7 * Guido Tack, 2005 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 "test/set.hh" 35 36using namespace Gecode; 37 38namespace Test { namespace Set { 39 40 /// %Tests for relation/operation constraints with constants 41 namespace RelOpConst { 42 43 /** 44 * \defgroup TaskTestSetRelOpConst Relation/operation constraints with constants 45 * \ingroup TaskTestSet 46 */ 47 //@{ 48 49 static IntSet ds_33(-3,3); 50 static IntSet ds_22(-2,2); 51 static IntSet ds_12(-1,2); 52 53 static IntSet iss[] = {IntSet(-1,1), IntSet(-4,-4), IntSet(0,2)}; 54 55 /// %Test for set relation constraint with constants 56 class RelSIS : public SetTest { 57 private: 58 IntSet is; 59 Gecode::SetOpType sot; 60 Gecode::SetRelType srt; 61 bool inverse; 62 63 template<class I, class J> 64 bool 65 sol(I& i, J& j) const { 66 switch (srt) { 67 case SRT_EQ: return Iter::Ranges::equal(i,j); 68 case SRT_NQ: return !Iter::Ranges::equal(i,j); 69 case SRT_SUB: return Iter::Ranges::subset(i,j); 70 case SRT_SUP: return Iter::Ranges::subset(j,i); 71 case SRT_DISJ: 72 { 73 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 74 return !inter(); 75 } 76 case SRT_CMPL: 77 { 78 Gecode::Set::RangesCompl<J> jc(j); 79 return Iter::Ranges::equal(i,jc); 80 } 81 default: GECODE_NEVER; 82 } 83 return false; 84 } 85 86 public: 87 /// Create and register test 88 RelSIS(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 89 int intSet, bool inverse0) 90 : SetTest("RelOp::ConstSIS::"+str(sot0)+"::"+str(srt0)+"::"+ 91 str(intSet)+(inverse0 ? "i" :""),2,ds_22,false) 92 , is(iss[intSet]), sot(sot0), srt(srt0), inverse(inverse0) {} 93 /// %Test whether \a x is solution 94 bool solution(const SetAssignment& x) const { 95 IntSetRanges isr(is); 96 CountableSetRanges xr0(x.lub, x[0]); 97 CountableSetRanges xr1(x.lub, x[1]); 98 switch (sot) { 99 case SOT_UNION: 100 { 101 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 102 u(isr, xr0); 103 return sol(u,xr1); 104 } 105 break; 106 case SOT_DUNION: 107 { 108 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 109 inter(isr, xr0); 110 if (inter()) 111 return false; 112 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 113 u(isr,xr0); 114 return sol(u,xr1); 115 } 116 break; 117 case SOT_INTER: 118 { 119 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 120 u(isr,xr0); 121 return sol(u,xr1); 122 } 123 break; 124 case SOT_MINUS: 125 { 126 if (!inverse) { 127 Iter::Ranges::Diff<IntSetRanges, CountableSetRanges> 128 u(isr,xr0); 129 return sol(u,xr1); 130 } else { 131 Iter::Ranges::Diff<CountableSetRanges, IntSetRanges> 132 u(xr0,isr); 133 return sol(u,xr1); 134 135 } 136 } 137 break; 138 default: GECODE_NEVER; 139 } 140 GECODE_NEVER; 141 return false; 142 } 143 /// Post constraint on \a x 144 void post(Space& home, SetVarArray& x, IntVarArray&) { 145 if (!inverse) 146 Gecode::rel(home, is, sot, x[0], srt, x[1]); 147 else 148 Gecode::rel(home, x[0], sot, is, srt, x[1]); 149 } 150 }; 151 152 /// %Test for set relation constraint with constants 153 class RelSSI : public SetTest { 154 private: 155 IntSet is; 156 Gecode::SetOpType sot; 157 Gecode::SetRelType srt; 158 159 template<class I, class J> 160 bool 161 sol(I& i, J& j) const { 162 switch (srt) { 163 case SRT_EQ: return Iter::Ranges::equal(i,j); 164 case SRT_NQ: return !Iter::Ranges::equal(i,j); 165 case SRT_SUB: return Iter::Ranges::subset(i,j); 166 case SRT_SUP: return Iter::Ranges::subset(j,i); 167 case SRT_DISJ: 168 { 169 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 170 return !inter(); 171 } 172 case SRT_CMPL: 173 { 174 Gecode::Set::RangesCompl<J> jc(j); 175 return Iter::Ranges::equal(i,jc); 176 } 177 default: GECODE_NEVER; 178 } 179 GECODE_NEVER; 180 return false; 181 } 182 183 public: 184 /// Create and register test 185 RelSSI(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 186 int intSet) 187 : SetTest("RelOp::ConstSSI::"+str(sot0)+"::"+str(srt0)+"::"+ 188 str(intSet),2,ds_22,false) 189 , is(iss[intSet]), sot(sot0), srt(srt0) {} 190 /// %Test whether \a x is solution 191 bool solution(const SetAssignment& x) const { 192 CountableSetRanges xr0(x.lub, x[0]); 193 CountableSetRanges xr1(x.lub, x[1]); 194 IntSetRanges isr(is); 195 switch (sot) { 196 case SOT_UNION: 197 { 198 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 199 u(xr0, xr1); 200 return sol(u,isr); 201 } 202 break; 203 case SOT_DUNION: 204 { 205 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 206 inter(xr0, xr1); 207 if (inter()) 208 return false; 209 Iter::Ranges::Union<CountableSetRanges, CountableSetRanges> 210 u(xr0, xr1); 211 return sol(u,isr); 212 } 213 break; 214 case SOT_INTER: 215 { 216 Iter::Ranges::Inter<CountableSetRanges, CountableSetRanges> 217 u(xr0,xr1); 218 return sol(u,isr); 219 } 220 break; 221 case SOT_MINUS: 222 { 223 Iter::Ranges::Diff<CountableSetRanges, CountableSetRanges> 224 u(xr0,xr1); 225 return sol(u,isr); 226 } 227 break; 228 default: GECODE_NEVER; 229 } 230 GECODE_NEVER; 231 return false; 232 } 233 /// Post constraint on \a x 234 void post(Space& home, SetVarArray& x, IntVarArray&) { 235 Gecode::rel(home, x[0], sot, x[1], srt, is); 236 } 237 }; 238 239 /// %Test for set relation constraint with constants 240 class RelISI : public SetTest { 241 private: 242 IntSet is0; 243 IntSet is1; 244 Gecode::SetOpType sot; 245 Gecode::SetRelType srt; 246 bool inverse; 247 248 template<class I, class J> 249 bool 250 sol(I& i, J& j) const { 251 switch (srt) { 252 case SRT_EQ: return Iter::Ranges::equal(i,j); 253 case SRT_NQ: return !Iter::Ranges::equal(i,j); 254 case SRT_SUB: return Iter::Ranges::subset(i,j); 255 case SRT_SUP: return Iter::Ranges::subset(j,i); 256 case SRT_DISJ: 257 { 258 Gecode::Iter::Ranges::Inter<I,J> inter(i,j); 259 return !inter(); 260 } 261 case SRT_CMPL: 262 { 263 Gecode::Set::RangesCompl<J> jc(j); 264 return Iter::Ranges::equal(i,jc); 265 } 266 default: GECODE_NEVER; 267 } 268 GECODE_NEVER; 269 return false; 270 } 271 272 public: 273 /// Create and register test 274 RelISI(Gecode::SetOpType sot0, Gecode::SetRelType srt0, 275 int intSet0, int intSet1, bool inverse0) 276 : SetTest("RelOp::ConstISI::"+str(sot0)+"::"+str(srt0)+"::"+ 277 str(intSet0)+"::"+str(intSet1)+ 278 (inverse0 ? "i" : ""),1,ds_33,false) 279 , is0(iss[intSet0]), is1(iss[intSet1]), sot(sot0), srt(srt0) 280 , inverse(inverse0) {} 281 /// %Test whether \a x is solution 282 bool solution(const SetAssignment& x) const { 283 CountableSetRanges xr0(x.lub, x[0]); 284 IntSetRanges isr0(is0); 285 IntSetRanges isr1(is1); 286 switch (sot) { 287 case SOT_UNION: 288 { 289 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 290 u(isr0, xr0); 291 return sol(u,isr1); 292 } 293 break; 294 case SOT_DUNION: 295 { 296 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 297 inter(isr0, xr0); 298 if (inter()) 299 return false; 300 Iter::Ranges::Union<IntSetRanges, CountableSetRanges> 301 u(isr0, xr0); 302 return sol(u,isr1); 303 } 304 break; 305 case SOT_INTER: 306 { 307 Iter::Ranges::Inter<IntSetRanges, CountableSetRanges> 308 u(isr0,xr0); 309 return sol(u,isr1); 310 } 311 break; 312 case SOT_MINUS: 313 { 314 if (!inverse) { 315 Iter::Ranges::Diff<IntSetRanges, CountableSetRanges> 316 u(isr0,xr0); 317 return sol(u,isr1); 318 } else { 319 Iter::Ranges::Diff<CountableSetRanges, IntSetRanges> 320 u(xr0,isr0); 321 return sol(u,isr1); 322 } 323 } 324 break; 325 default: GECODE_NEVER; 326 } 327 GECODE_NEVER; 328 return false; 329 } 330 /// Post constraint on \a x 331 void post(Space& home, SetVarArray& x, IntVarArray&) { 332 if (!inverse) 333 Gecode::rel(home, is0, sot, x[0], srt, is1); 334 else 335 Gecode::rel(home, x[0], sot, is0, srt, is1); 336 } 337 }; 338 339 /// Help class to create and register tests 340 class Create { 341 public: 342 /// Perform creation and registration 343 Create(void) { 344 using namespace Gecode; 345 for (SetRelTypes srts; srts(); ++srts) { 346 for (SetOpTypes sots; sots(); ++sots) { 347 for (int i=0; i<=2; i++) { 348 (void) new RelSIS(sots.sot(),srts.srt(),i,false); 349 (void) new RelSIS(sots.sot(),srts.srt(),i,true); 350 (void) new RelSSI(sots.sot(),srts.srt(),i); 351 (void) new RelISI(sots.sot(),srts.srt(),i,0,false); 352 (void) new RelISI(sots.sot(),srts.srt(),i,1,false); 353 (void) new RelISI(sots.sot(),srts.srt(),i,2,false); 354 (void) new RelISI(sots.sot(),srts.srt(),i,0,true); 355 (void) new RelISI(sots.sot(),srts.srt(),i,1,true); 356 (void) new RelISI(sots.sot(),srts.srt(),i,2,true); 357 } 358 } 359 } 360 } 361 }; 362 363 Create c; 364 365 //@} 366 367}}} 368 369// STATISTICS: test-set