this repo has no description
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ 2/* 3 * Main authors: 4 * Christian Schulte <schulte@gecode.org> 5 * 6 * Contributing authors: 7 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 8 * 9 * Copyright: 10 * Christian Schulte, 2008 11 * Vincent Barichard, 2012 12 * 13 * This file is part of Gecode, the generic constraint 14 * development environment: 15 * http://www.gecode.org 16 * 17 * Permission is hereby granted, free of charge, to any person obtaining 18 * a copy of this software and associated documentation files (the 19 * "Software"), to deal in the Software without restriction, including 20 * without limitation the rights to use, copy, modify, merge, publish, 21 * distribute, sublicense, and/or sell copies of the Software, and to 22 * permit persons to whom the Software is furnished to do so, subject to 23 * the following conditions: 24 * 25 * The above copyright notice and this permission notice shall be 26 * included in all copies or substantial portions of the Software. 27 * 28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 35 * 36 */ 37 38#include "test/assign.hh" 39 40#include <gecode/search.hh> 41 42namespace Test { namespace Assign { 43 44 /// Space for executing integer tests 45 class IntTestSpace : public Gecode::Space { 46 public: 47 /// Variables to be tested 48 Gecode::IntVarArray x; 49 /// Initialize test space 50 IntTestSpace(int n, Gecode::IntSet& d) 51 : x(*this, n, d) {} 52 /// Constructor for cloning \a s 53 IntTestSpace(IntTestSpace& s) 54 : Gecode::Space(s) { 55 x.update(*this, s.x); 56 } 57 /// Copy space during cloning 58 virtual Gecode::Space* copy(void) { 59 return new IntTestSpace(*this); 60 } 61 }; 62 63 /// Space for executing Boolean tests 64 class BoolTestSpace : public Gecode::Space { 65 public: 66 /// Variables to be tested 67 Gecode::BoolVarArray x; 68 /// Initialize test space 69 BoolTestSpace(int n) 70 : x(*this, n, 0, 1) {} 71 /// Constructor for cloning \a s 72 BoolTestSpace(BoolTestSpace& s) 73 : Gecode::Space(s) { 74 x.update(*this, s.x); 75 } 76 /// Copy space during cloning 77 virtual Gecode::Space* copy(void) { 78 return new BoolTestSpace(*this); 79 } 80 }; 81 82#ifdef GECODE_HAS_SET_VARS 83 84 /// Space for executing Boolean tests 85 class SetTestSpace : public Gecode::Space { 86 public: 87 /// Variables to be tested 88 Gecode::SetVarArray x; 89 /// Initialize test space 90 SetTestSpace(int n, const Gecode::IntSet& d) 91 : x(*this, n, Gecode::IntSet::empty, d) {} 92 /// Constructor for cloning \a s 93 SetTestSpace(SetTestSpace& s) 94 : Gecode::Space(s) { 95 x.update(*this, s.x); 96 } 97 /// Copy space during cloning 98 virtual Gecode::Space* copy(void) { 99 return new SetTestSpace(*this); 100 } 101 }; 102 103#endif 104 105#ifdef GECODE_HAS_FLOAT_VARS 106 107 /// Space for executing Boolean tests 108 class FloatTestSpace : public Gecode::Space { 109 public: 110 /// Variables to be tested 111 Gecode::FloatVarArray x; 112 /// Initialize test space 113 FloatTestSpace(int n, const Gecode::FloatVal& d) 114 : x(*this, n, d.min(), d.max()) {} 115 /// Constructor for cloning \a s 116 FloatTestSpace(FloatTestSpace& s) 117 : Gecode::Space(s) { 118 x.update(*this, s.x); 119 } 120 /// Copy space during cloning 121 virtual Gecode::Space* copy(void) { 122 return new FloatTestSpace(*this); 123 } 124 }; 125 126#endif 127 128 /** \name Collection of possible arguments for integer assignments 129 * 130 * \relates IntTestSpace 131 */ 132 //@{ 133 /// Names for integer assignments 134 const char* int_assign_name[] = { 135 "INT_ASSIGN_MIN", 136 "INT_ASSIGN_MED", 137 "INT_ASSIGN_MAX", 138 "INT_ASSIGN_RND", 139 "INT_ASSIGN" 140 }; 141 /// Number of integer value selections 142 const int n_int_assign = 143 sizeof(int_assign_name)/sizeof(const char*); 144 /// Test function for branch value function 145 int int_val(const Gecode::Space&, Gecode::IntVar x, int) { 146 return x.min(); 147 } 148 //@} 149 150 /** \name Collection of possible arguments for Boolean assignments 151 * 152 * \relates BoolTestSpace 153 */ 154 //@{ 155 /// Names for integer assignments 156 const char* bool_assign_name[] = { 157 "BOOL_ASSIGN_MIN", 158 "BOOL_ASSIGN_MAX", 159 "BOOL_ASSIGN_RND", 160 "BOOL_ASSIGN" 161 }; 162 /// Number of integer value selections 163 const int n_bool_assign = 164 sizeof(bool_assign_name)/sizeof(const char*); 165 /// Test function for branch value function 166 int bool_val(const Gecode::Space&, Gecode::BoolVar x, int) { 167 return x.min(); 168 } 169 //@} 170 171 IntTest::IntTest(const std::string& s, int a, const Gecode::IntSet& d) 172 : Base("Int::Assign::"+s), arity(a), dom(d) { 173 } 174 175 bool 176 IntTest::run(void) { 177 using namespace Gecode; 178 IntTestSpace* root = new IntTestSpace(arity,dom); 179 post(*root, root->x); 180 (void) root->status(); 181 182 for (int val = 0; val<n_int_assign; val++) { 183 IntTestSpace* clone = static_cast<IntTestSpace*>(root->clone()); 184 Gecode::Search::Options o; 185 o.a_d = _rand(10); 186 o.c_d = _rand(10); 187 188 Rnd r(1); 189 IntAssign ia; 190 switch (val) { 191 case 0: ia = INT_ASSIGN_MIN(); break; 192 case 1: ia = INT_ASSIGN_MED(); break; 193 case 2: ia = INT_ASSIGN_MAX(); break; 194 case 3: ia = INT_ASSIGN_RND(r); break; 195 case 4: ia = INT_ASSIGN(&int_val); break; 196 default: GECODE_NEVER; 197 } 198 199 assign(*clone, clone->x, ia); 200 Gecode::DFS<IntTestSpace> e_s(clone, o); 201 delete clone; 202 203 // Find number of solutions 204 int solutions = 0; 205 while (Space* s = e_s.next()) { 206 delete s; solutions++; 207 } 208 if (solutions != 1) { 209 std::cout << "FAILURE" << std::endl 210 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 211 << "\t" << int_assign_name[val] << std::endl; 212 delete root; 213 return false; 214 } 215 } 216 delete root; 217 return true; 218 } 219 220 BoolTest::BoolTest(const std::string& s, int a) 221 : Base("Bool::Assign::"+s), arity(a) { 222 } 223 224 bool 225 BoolTest::run(void) { 226 using namespace Gecode; 227 BoolTestSpace* root = new BoolTestSpace(arity); 228 post(*root, root->x); 229 (void) root->status(); 230 231 for (int val = n_bool_assign; val--; ) { 232 BoolTestSpace* clone = static_cast<BoolTestSpace*>(root->clone()); 233 Gecode::Search::Options o; 234 o.a_d = _rand(10); 235 o.c_d = _rand(10); 236 Rnd r(1); 237 BoolAssign ia; 238 switch (val) { 239 case 0: ia = BOOL_ASSIGN_MIN(); break; 240 case 1: ia = BOOL_ASSIGN_MAX(); break; 241 case 2: ia = BOOL_ASSIGN_RND(r); break; 242 case 3: ia = BOOL_ASSIGN(&bool_val); break; 243 default: GECODE_NEVER; 244 } 245 246 assign(*clone, clone->x, ia); 247 Gecode::DFS<BoolTestSpace> e_s(clone, o); 248 delete clone; 249 250 // Find number of solutions 251 int solutions = 0; 252 while (Space* s = e_s.next()) { 253 delete s; solutions++; 254 } 255 if (solutions != 1) { 256 std::cout << "FAILURE" << std::endl 257 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 258 << "\t" << int_assign_name[val] << std::endl; 259 delete root; 260 return false; 261 } 262 } 263 delete root; 264 return true; 265 } 266 267#ifdef GECODE_HAS_SET_VARS 268 269 /** \name Collection of possible arguments for set assignments 270 * 271 * \relates SetTestSpace 272 */ 273 //@{ 274 /// Names for integer assignments 275 const char* set_assign_name[] = { 276 "SET_ASSIGN_MIN_INC", 277 "SET_ASSIGN_MIN_EXC", 278 "SET_ASSIGN_MED_INC", 279 "SET_ASSIGN_MED_EXC", 280 "SET_ASSIGN_MAX_INC", 281 "SET_ASSIGN_MAX_EXC", 282 "SET_ASSIGN_RND_INC", 283 "SET_ASSIGN_RND_EXC", 284 "SET_ASSIGN" 285 }; 286 /// Number of set value selections 287 const int n_set_assign = 288 sizeof(set_assign_name)/sizeof(const char*); 289 /// Test function for branch value function 290 int set_val(const Gecode::Space&, Gecode::SetVar x, int) { 291 Gecode::SetVarUnknownRanges r(x); 292 return r.min(); 293 } 294 //@} 295 296 SetTest::SetTest(const std::string& s, int a, const Gecode::IntSet& d) 297 : Base("Set::Assign::"+s), arity(a), dom(d) { 298 } 299 300 bool 301 SetTest::run(void) { 302 using namespace Gecode; 303 SetTestSpace* root = new SetTestSpace(arity,dom); 304 post(*root, root->x); 305 (void) root->status(); 306 307 for (int val = n_set_assign; val--; ) { 308 SetTestSpace* clone = static_cast<SetTestSpace*>(root->clone()); 309 Gecode::Search::Options o; 310 o.a_d = _rand(10); 311 o.c_d = _rand(10); 312 313 Rnd r(1); 314 315 SetAssign sa; 316 switch (val) { 317 case 0: sa = SET_ASSIGN_MIN_INC(); break; 318 case 1: sa = SET_ASSIGN_MIN_EXC(); break; 319 case 2: sa = SET_ASSIGN_MED_INC(); break; 320 case 3: sa = SET_ASSIGN_MED_EXC(); break; 321 case 4: sa = SET_ASSIGN_MAX_INC(); break; 322 case 5: sa = SET_ASSIGN_MAX_EXC(); break; 323 case 6: sa = SET_ASSIGN_RND_INC(r); break; 324 case 7: sa = SET_ASSIGN_RND_EXC(r); break; 325 case 8: sa = SET_ASSIGN(&set_val); break; 326 default: GECODE_NEVER; 327 } 328 329 assign(*clone, clone->x, sa); 330 Gecode::DFS<SetTestSpace> e_s(clone, o); 331 delete clone; 332 333 // Find number of solutions 334 int solutions = 0; 335 while (Space* s = e_s.next()) { 336 delete s; solutions++; 337 } 338 if (solutions != 1) { 339 std::cout << "FAILURE" << std::endl 340 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 341 << "\t" << set_assign_name[val] << std::endl; 342 delete root; 343 return false; 344 } 345 } 346 delete root; 347 return true; 348 } 349 350#endif 351 352#ifdef GECODE_HAS_FLOAT_VARS 353 354 /** \name Collection of possible arguments for float assignments 355 * 356 * \relates FloatTestSpace 357 */ 358 //@{ 359 /// Names for float assignments 360 const char* float_assign_name[] = { 361 "FLOAT_ASSIGN_MIN", 362 "FLOAT_ASSIGN_MAX", 363 "FLOAT_ASSIGN_RND", 364 "FLOAT_ASSIGN" 365 }; 366 /// Number of float value selections 367 const int n_float_assign = 368 sizeof(float_assign_name)/sizeof(const char*); 369 /// Test function for branch value function 370 Gecode::FloatNumBranch float_val(const Gecode::Space&, 371 Gecode::FloatVar x, int) { 372 Gecode::FloatNumBranch nl; nl.n=x.med(); nl.l=true; 373 return nl; 374 } 375 //@} 376 377 FloatTest::FloatTest(const std::string& s, int a, const Gecode::FloatVal& d) 378 : Base("Float::Assign::"+s), arity(a), dom(d) { 379 } 380 381 bool 382 FloatTest::run(void) { 383 using namespace Gecode; 384 FloatTestSpace* root = new FloatTestSpace(arity,dom); 385 post(*root, root->x); 386 (void) root->status(); 387 388 for (int val = n_float_assign; val--; ) { 389 FloatTestSpace* clone = static_cast<FloatTestSpace*>(root->clone()); 390 Gecode::Search::Options o; 391 o.a_d = _rand(10); 392 o.c_d = _rand(10); 393 394 Rnd r(1); 395 396 FloatAssign fa; 397 switch (val) { 398 case 0: fa = FLOAT_ASSIGN_MIN(); break; 399 case 1: fa = FLOAT_ASSIGN_MAX(); break; 400 case 2: fa = FLOAT_ASSIGN_RND(r); break; 401 case 3: fa = FLOAT_ASSIGN(&float_val); break; 402 default: GECODE_NEVER; 403 } 404 405 assign(*clone, clone->x, fa); 406 Gecode::DFS<FloatTestSpace> e_s(clone, o); 407 delete clone; 408 409 // Find number of solutions 410 int solutions = 0; 411 while (Space* s = e_s.next()) { 412 delete s; solutions++; 413 } 414 if (solutions != 1) { 415 std::cout << "FAILURE" << std::endl 416 << "\tc_d=" << o.c_d << ", a_d=" << o.a_d << std::endl 417 << "\t" << float_assign_name[val] << std::endl; 418 delete root; 419 return false; 420 } 421 } 422 delete root; 423 return true; 424 } 425 426#endif 427 428}} 429 430// STATISTICS: test-branch