this repo has no description
at develop 18 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 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 6 * 7 * Copyright: 8 * Christian Schulte, 2005 9 * Vincent Barichard, 2012 10 * 11 * This file is part of Gecode, the generic constraint 12 * development environment: 13 * http://www.gecode.org 14 * 15 * Permission is hereby granted, free of charge, to any person obtaining 16 * a copy of this software and associated documentation files (the 17 * "Software"), to deal in the Software without restriction, including 18 * without limitation the rights to use, copy, modify, merge, publish, 19 * distribute, sublicense, and/or sell copies of the Software, and to 20 * permit persons to whom the Software is furnished to do so, subject to 21 * the following conditions: 22 * 23 * The above copyright notice and this permission notice shall be 24 * included in all copies or substantial portions of the Software. 25 * 26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 33 * 34 */ 35 36#include "test/float.hh" 37#include <gecode/minimodel.hh> 38 39#ifdef GECODE_HAS_MPFR 40 41#include <cmath> 42#include <algorithm> 43 44namespace Test { namespace Float { 45 46 /// %Tests for trigonometric constraints 47 namespace Trigonometric { 48 49 /// %Test for sinus constraint 50 class SinXY : public Test { 51 public: 52 /// Create and register test 53 SinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 54 : Test("Trigonometric::Sin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 55 /// %Test whether \a x is solution 56 virtual MaybeType solution(const Assignment& x) const { 57 return eq(sin(x[0]), x[1]); 58 } 59 /// Post constraint on \a x 60 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 61 if (flip()) 62 Gecode::sin(home, x[0], x[1]); 63 else 64 Gecode::rel(home, sin(x[0]) == x[1]); 65 } 66 }; 67 68 /// %Test for sinus constraint where solution is ensured 69 class SinXYSol : public Test { 70 public: 71 /// Create and register test 72 SinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 73 : Test("Trigonometric::Sin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 74 /// %Test whether \a x is solution 75 virtual MaybeType solution(const Assignment& x) const { 76 return eq(sin(x[0]), x[1]); 77 } 78 /// Extend assignment \a x 79 virtual bool extendAssignement(Assignment& x) const { 80 Gecode::FloatVal d = sin(x[0]); 81 if (Gecode::Float::subset(d, dom)) { 82 x.set(1, d); 83 return true; 84 } else { 85 return false; 86 } 87 } 88 /// Post constraint on \a x 89 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 90 Gecode::sin(home, x[0], x[1]); 91 } 92 }; 93 94 /// %Test for sinus constraint with shared variables 95 class SinXX : public Test { 96 public: 97 /// Create and register test 98 SinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 99 : Test("Trigonometric::Sin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 100 /// %Test whether \a x is solution 101 virtual MaybeType solution(const Assignment& x) const { 102 return eq(sin(x[0]), x[0]); 103 } 104 /// Post constraint on \a x 105 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 106 Gecode::sin(home, x[0], x[0]); 107 } 108 }; 109 110 /// %Test for cosinus constraint 111 class CosXY : public Test { 112 public: 113 /// Create and register test 114 CosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 115 : Test("Trigonometric::Cos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 116 /// %Test whether \a x is solution 117 virtual MaybeType solution(const Assignment& x) const { 118 return eq(cos(x[0]), x[1]); 119 } 120 /// Post constraint on \a x 121 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 122 if (flip()) 123 Gecode::cos(home, x[0], x[1]); 124 else 125 Gecode::rel(home, cos(x[0]) == x[1]); 126 } 127 }; 128 129 /// %Test for cosinus constraint where solution is ensured 130 class CosXYSol : public Test { 131 public: 132 /// Create and register test 133 CosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 134 : Test("Trigonometric::Cos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 135 /// %Test whether \a x is solution 136 virtual MaybeType solution(const Assignment& x) const { 137 return eq(cos(x[0]), x[1]); 138 } 139 /// Extend assignment \a x 140 virtual bool extendAssignement(Assignment& x) const { 141 Gecode::FloatVal d = cos(x[0]); 142 if (Gecode::Float::subset(d, dom)) { 143 x.set(1, d); 144 return true; 145 } else { 146 return false; 147 } 148 } 149 /// Post constraint on \a x 150 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 151 Gecode::cos(home, x[0], x[1]); 152 } 153 }; 154 155 /// %Test for cosinus constraint with shared variables 156 class CosXX : public Test { 157 public: 158 /// Create and register test 159 CosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 160 : Test("Trigonometric::Cos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 161 /// %Test whether \a x is solution 162 virtual MaybeType solution(const Assignment& x) const { 163 return eq(cos(x[0]), x[0]); 164 } 165 /// Post constraint on \a x 166 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 167 Gecode::cos(home, x[0], x[0]); 168 } 169 }; 170 171 /// %Test for tangent constraint 172 class TanXY : public Test { 173 public: 174 /// Create and register test 175 TanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 176 : Test("Trigonometric::Tan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 177 /// %Test whether \a x is solution 178 virtual MaybeType solution(const Assignment& x) const { 179 return eq(tan(x[0]), x[1]); 180 } 181 /// Post constraint on \a x 182 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 183 if (flip()) 184 Gecode::tan(home, x[0], x[1]); 185 else 186 Gecode::rel(home, tan(x[0]) == x[1]); 187 } 188 }; 189 190 /// %Test for tangent constraint where solution is ensured 191 class TanXYSol : public Test { 192 public: 193 /// Create and register test 194 TanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 195 : Test("Trigonometric::Tan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 196 /// %Test whether \a x is solution 197 virtual MaybeType solution(const Assignment& x) const { 198 return eq(tan(x[0]), x[1]); 199 } 200 /// Extend assignment \a x 201 virtual bool extendAssignement(Assignment& x) const { 202 Gecode::FloatVal d = tan(x[0]); 203 if (Gecode::Float::subset(d, dom)) { 204 x.set(1, d); 205 return true; 206 } else { 207 return false; 208 } 209 } 210 /// Post constraint on \a x 211 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 212 Gecode::tan(home, x[0], x[1]); 213 } 214 }; 215 216 /// %Test for tangent constraint with shared variables 217 class TanXX : public Test { 218 public: 219 /// Create and register test 220 TanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 221 : Test("Trigonometric::Tan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 222 /// %Test whether \a x is solution 223 virtual MaybeType solution(const Assignment& x) const { 224 return eq(tan(x[0]), x[0]); 225 } 226 /// Post constraint on \a x 227 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 228 Gecode::tan(home, x[0], x[0]); 229 } 230 }; 231 232 /// %Test for asinus constraint 233 class ASinXY : public Test { 234 public: 235 /// Create and register test 236 ASinXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 237 : Test("Trigonometric::ASin::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 238 /// %Test whether \a x is solution 239 virtual MaybeType solution(const Assignment& x) const { 240 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 241 return MT_FALSE; 242 return eq(asin(x[0]), x[1]); 243 } 244 /// Post constraint on \a x 245 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 246 if (flip()) 247 Gecode::asin(home, x[0], x[1]); 248 else 249 Gecode::rel(home, asin(x[0]) == x[1]); 250 } 251 }; 252 253 /// %Test for asinus constraint where solution is ensured 254 class ASinXYSol : public Test { 255 public: 256 /// Create and register test 257 ASinXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 258 : Test("Trigonometric::ASin::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 259 /// %Test whether \a x is solution 260 virtual MaybeType solution(const Assignment& x) const { 261 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 262 return MT_FALSE; 263 return eq(asin(x[0]), x[1]); 264 } 265 /// Extend assignment \a x 266 virtual bool extendAssignement(Assignment& x) const { 267 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 268 return false; 269 Gecode::FloatVal d = asin(x[0]); 270 if (Gecode::Float::subset(d, dom)) { 271 x.set(1, d); 272 return true; 273 } else { 274 return false; 275 } 276 } 277 /// Post constraint on \a x 278 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 279 Gecode::asin(home, x[0], x[1]); 280 } 281 }; 282 283 /// %Test for asinus constraint with shared variables 284 class ASinXX : public Test { 285 public: 286 /// Create and register test 287 ASinXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 288 : Test("Trigonometric::ASin::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 289 /// %Test whether \a x is solution 290 virtual MaybeType solution(const Assignment& x) const { 291 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 292 return MT_FALSE; 293 return eq(asin(x[0]), x[0]); 294 } 295 /// Post constraint on \a x 296 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 297 Gecode::asin(home, x[0], x[0]); 298 } 299 }; 300 301 /// %Test for acosinus constraint 302 class ACosXY : public Test { 303 public: 304 /// Create and register test 305 ACosXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 306 : Test("Trigonometric::ACos::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 307 /// %Test whether \a x is solution 308 virtual MaybeType solution(const Assignment& x) const { 309 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 310 return MT_FALSE; 311 return eq(acos(x[0]), x[1]); 312 } 313 /// Post constraint on \a x 314 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 315 if (flip()) 316 Gecode::acos(home, x[0], x[1]); 317 else 318 Gecode::rel(home, acos(x[0]) == x[1]); 319 } 320 }; 321 322 /// %Test for acosinus constraint where solution is ensured 323 class ACosXYSol : public Test { 324 public: 325 /// Create and register test 326 ACosXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 327 : Test("Trigonometric::ACos::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 328 /// %Test whether \a x is solution 329 virtual MaybeType solution(const Assignment& x) const { 330 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 331 return MT_FALSE; 332 return eq(acos(x[0]), x[1]); 333 } 334 /// Extend assignment \a x 335 virtual bool extendAssignement(Assignment& x) const { 336 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 337 return false; 338 Gecode::FloatVal d = acos(x[0]); 339 if (Gecode::Float::subset(d, dom)) { 340 x.set(1, d); 341 return true; 342 } else { 343 return false; 344 } 345 } 346 /// Post constraint on \a x 347 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 348 Gecode::acos(home, x[0], x[1]); 349 } 350 }; 351 352 /// %Test for acosinus constraint with shared variables 353 class ACosXX : public Test { 354 public: 355 /// Create and register test 356 ACosXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 357 : Test("Trigonometric::ACos::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 358 /// %Test whether \a x is solution 359 virtual MaybeType solution(const Assignment& x) const { 360 if ((x[0].min() > 1.0) || (x[0].max() < -1.0)) 361 return MT_FALSE; 362 return eq(acos(x[0]), x[0]); 363 } 364 /// Post constraint on \a x 365 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 366 Gecode::acos(home, x[0], x[0]); 367 } 368 }; 369 370 /// %Test for atangent constraint 371 class ATanXY : public Test { 372 public: 373 /// Create and register test 374 ATanXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 375 : Test("Trigonometric::ATan::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 376 /// %Test whether \a x is solution 377 virtual MaybeType solution(const Assignment& x) const { 378 return eq(atan(x[0]), x[1]); 379 } 380 /// Post constraint on \a x 381 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 382 if (flip()) 383 Gecode::atan(home, x[0], x[1]); 384 else 385 Gecode::rel(home, atan(x[0]) == x[1]); 386 } 387 }; 388 389 /// %Test for atangent constraint where solution is ensured 390 class ATanXYSol : public Test { 391 public: 392 /// Create and register test 393 ATanXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 394 : Test("Trigonometric::ATan::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 395 /// %Test whether \a x is solution 396 virtual MaybeType solution(const Assignment& x) const { 397 return eq(atan(x[0]), x[1]); 398 } 399 /// Extend assignment \a x 400 virtual bool extendAssignement(Assignment& x) const { 401 Gecode::FloatVal d = atan(x[0]); 402 if (Gecode::Float::subset(d, dom)) { 403 x.set(1, d); 404 return true; 405 } else { 406 return false; 407 } 408 } 409 /// Post constraint on \a x 410 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 411 Gecode::atan(home, x[0], x[1]); 412 } 413 }; 414 415 /// %Test for atangent constraint with shared variables 416 class ATanXX : public Test { 417 public: 418 /// Create and register test 419 ATanXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 420 : Test("Trigonometric::ATan::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 421 /// %Test whether \a x is solution 422 virtual MaybeType solution(const Assignment& x) const { 423 return eq(atan(x[0]), x[0]); 424 } 425 /// Post constraint on \a x 426 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 427 Gecode::atan(home, x[0], x[0]); 428 } 429 }; 430 431 const Gecode::FloatNum step = 0.15; 432 const Gecode::FloatNum step2 = 2*step; 433 Gecode::FloatVal a(-8,5); 434 Gecode::FloatVal b(9,12); 435 Gecode::FloatVal c(-8,8); 436 437 SinXY sin_xy_a("A",a,step); 438 SinXY sin_xy_b("B",b,step); 439 SinXY sin_xy_c("C",c,step); 440 441 SinXYSol sin_xy_sol_a("A",a,step); 442 SinXYSol sin_xy_sol_b("B",b,step); 443 SinXYSol sin_xy_sol_c("C",c,step); 444 445 SinXX sin_xx_a("A",a,step); 446 SinXX sin_xx_b("B",b,step); 447 SinXX sin_xx_c("C",c,step); 448 449 CosXY cos_xy_a("A",a,step); 450 CosXY cos_xy_b("B",b,step); 451 CosXY cos_xy_c("C",c,step); 452 453 CosXYSol cos_xy_sol_a("A",a,step); 454 CosXYSol cos_xy_sol_b("B",b,step); 455 CosXYSol cos_xy_sol_c("C",c,step); 456 457 CosXX cos_xx_a("A",a,step); 458 CosXX cos_xx_b("B",b,step); 459 CosXX cos_xx_c("C",c,step); 460 461 TanXY tan_xy_a("A",a,step); 462 TanXY tan_xy_b("B",b,step); 463 TanXY tan_xy_c("C",c,step); 464 465 TanXYSol tan_xy_sol_a("A",a,step); 466 TanXYSol tan_xy_sol_b("B",b,step); 467 TanXYSol tan_xy_sol_c("C",c,step); 468 469 TanXX tan_xx_a("A",a,step); 470 TanXX tan_xx_b("B",b,step); 471 TanXX tan_xx_c("C",c,step); 472 473 ASinXY asin_xy_a("A",a,step); 474 ASinXY asin_xy_b("B",b,step); 475 ASinXY asin_xy_c("C",c,step); 476 477 ASinXYSol asin_xy_sol_a("A",a,step); 478 ASinXYSol asin_xy_sol_b("B",b,step); 479 ASinXYSol asin_xy_sol_c("C",c,step); 480 481 ASinXX asin_xx_a("A",a,step); 482 ASinXX asin_xx_b("B",b,step); 483 ASinXX asin_xx_c("C",c,step); 484 485 ACosXY acos_xy_a("A",a,step); 486 ACosXY acos_xy_b("B",b,step); 487 ACosXY acos_xy_c("C",c,step); 488 489 ACosXYSol acos_xy_sol_a("A",a,step); 490 ACosXYSol acos_xy_sol_b("B",b,step); 491 ACosXYSol acos_xy_sol_c("C",c,step); 492 493 ACosXX acos_xx_a("A",a,step); 494 ACosXX acos_xx_b("B",b,step); 495 ACosXX acos_xx_c("C",c,step); 496 497 ATanXY atan_xy_a("A",a,step); 498 ATanXY atan_xy_b("B",b,step); 499 ATanXY atan_xy_c("C",c,step); 500 501 ATanXYSol atan_xy_sol_a("A",a,step); 502 ATanXYSol atan_xy_sol_b("B",b,step); 503 ATanXYSol atan_xy_sol_c("C",c,step); 504 505 ATanXX atan_xx_a("A",a,step); 506 ATanXX atan_xx_b("B",b,step); 507 ATanXX atan_xx_c("C",c,step); 508 509 //@} 510 511 } 512}} 513 514#endif 515// STATISTICS: test-float