this repo has no description
at develop 15 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 transcendental constraints 47 namespace Transcendental { 48 49 /// %Test for exponent constraint 50 class ExpXY : public Test { 51 public: 52 /// Create and register test 53 ExpXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 54 : Test("Transcendental::Exp::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(exp(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::exp(home, x[0], x[1]); 63 else 64 Gecode::rel(home, exp(x[0]) == x[1]); 65 } 66 }; 67 68 /// %Test for exponent constraint where solution is ensured 69 class ExpXYSol : public Test { 70 public: 71 /// Create and register test 72 ExpXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 73 : Test("Transcendental::Exp::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(exp(x[0]), x[1]); 77 } 78 /// Extend assignment \a x 79 virtual bool extendAssignement(Assignment& x) const { 80 Gecode::FloatVal d = exp(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::exp(home, x[0], x[1]); 91 } 92 }; 93 94 /// %Test for exponent constraint with shared variables 95 class ExpXX : public Test { 96 public: 97 /// Create and register test 98 ExpXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 99 : Test("Transcendental::Exp::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(exp(x[0]), x[0]); 103 } 104 /// Post constraint on \a x 105 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 106 Gecode::exp(home, x[0], x[0]); 107 } 108 }; 109 110 /// %Test for logarithm constraint 111 class LogXY : public Test { 112 public: 113 /// Create and register test 114 LogXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 115 : Test("Transcendental::Log::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false) {} 116 /// %Test whether \a x is solution 117 virtual MaybeType solution(const Assignment& x) const { 118 if (x[0].max() < 0.0) 119 return MT_FALSE; 120 return eq(log(x[0]), x[1]); 121 } 122 /// Post constraint on \a x 123 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 124 if (flip()) 125 Gecode::log(home, x[0], x[1]); 126 else 127 Gecode::rel(home, log(x[0]) == x[1]); 128 } 129 }; 130 131 /// %Test for logarithm constraint where solution is ensured 132 class LogXYSol : public Test { 133 public: 134 /// Create and register test 135 LogXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 136 : Test("Transcendental::Log::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false) {} 137 /// %Test whether \a x is solution 138 virtual MaybeType solution(const Assignment& x) const { 139 if (x[0].max() < 0.0) 140 return MT_FALSE; 141 return eq(log(x[0]), x[1]); 142 } 143 /// Extend assignment \a x 144 virtual bool extendAssignement(Assignment& x) const { 145 if (x[0].max() < 0.0) return false; 146 Gecode::FloatVal d = log(x[0]); 147 if (Gecode::Float::subset(d, dom)) { 148 x.set(1, d); 149 return true; 150 } else { 151 return false; 152 } 153 } 154 /// Post constraint on \a x 155 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 156 Gecode::log(home, x[0], x[1]); 157 } 158 }; 159 160 /// %Test for logarithm constraint with shared variables 161 class LogXX : public Test { 162 public: 163 /// Create and register test 164 LogXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum st) 165 : Test("Transcendental::Log::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false) {} 166 /// %Test whether \a x is solution 167 virtual MaybeType solution(const Assignment& x) const { 168 if (x[0].max() < 0.0) 169 return MT_FALSE; 170 return eq(log(x[0]), x[0]); 171 } 172 /// Post constraint on \a x 173 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 174 Gecode::log(home, x[0], x[0]); 175 } 176 }; 177 178 /// %Test for logarithm base n constraint 179 class LogNXY : public Test { 180 Gecode::FloatNum base; 181 public: 182 /// Create and register test 183 LogNXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 184 : Test("Transcendental::Log::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {} 185 /// %Test whether \a x is solution 186 virtual MaybeType solution(const Assignment& x) const { 187 if ((x[0].max() <= 0.0) || (base <= 0.0)) 188 return MT_FALSE; 189 return eq(log(x[0]) / log(base), x[1]); 190 } 191 /// Post constraint on \a x 192 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 193 Gecode::log(home, base, x[0], x[1]); 194 } 195 }; 196 197 /// %Test for logarithm base n constraint where solution is ensured 198 class LogNXYSol : public Test { 199 Gecode::FloatNum base; 200 public: 201 /// Create and register test 202 LogNXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 203 : Test("Transcendental::Log::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {} 204 /// %Test whether \a x is solution 205 virtual MaybeType solution(const Assignment& x) const { 206 if ((x[0].max() <= 0.0) || (base <= 0.0)) 207 return MT_FALSE; 208 return eq(log(x[0]) / log(base), x[1]); 209 } 210 /// Extend assignment \a x 211 virtual bool extendAssignement(Assignment& x) const { 212 if ((x[0].max() <= 0.0) || (base <= 0.0)) 213 return false; 214 Gecode::FloatVal d = log(x[0])/log(base); 215 if (Gecode::Float::subset(d, dom)) { 216 x.set(1, d); 217 return true; 218 } else { 219 return false; 220 } 221 } 222 /// Post constraint on \a x 223 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 224 Gecode::log(home, base, x[0], x[1]); 225 } 226 }; 227 228 /// %Test for logarithm base n constraint with shared variables 229 class LogNXX : public Test { 230 Gecode::FloatNum base; 231 public: 232 /// Create and register test 233 LogNXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 234 : Test("Transcendental::Log::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {} 235 /// %Test whether \a x is solution 236 virtual MaybeType solution(const Assignment& x) const { 237 if ((x[0].max() <= 0.0) || (base <= 0.0)) 238 return MT_FALSE; 239 return eq(log(x[0]) / log(base), x[0]); 240 } 241 /// Post constraint on \a x 242 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 243 Gecode::log(home, base, x[0], x[0]); 244 } 245 }; 246 247 /// %Test for pow exponent n constraint 248 class PowXY : public Test { 249 Gecode::FloatNum base; 250 public: 251 /// Create and register test 252 PowXY(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 253 : Test("Transcendental::Pow::N::"+str(_base)+"::XY::"+s,2,d,st,CPLT_ASSIGNMENT,false), base(_base) {} 254 /// %Test whether \a x is solution 255 virtual MaybeType solution(const Assignment& x) const { 256 if (base <= 0.0) 257 return MT_FALSE; 258 return eq(exp(x[0] * log(base)), x[1]); 259 } 260 /// Post constraint on \a x 261 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 262 Gecode::pow(home, base, x[0], x[1]); 263 } 264 }; 265 266 /// %Test for pow exponent n constraint where solution is ensured 267 class PowXYSol : public Test { 268 Gecode::FloatNum base; 269 public: 270 /// Create and register test 271 PowXYSol(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 272 : Test("Transcendental::Pow::N::"+str(_base)+"::XY::Sol::"+s,2,d,st,EXTEND_ASSIGNMENT,false), base(_base) {} 273 /// %Test whether \a x is solution 274 virtual MaybeType solution(const Assignment& x) const { 275 if (base <= 0.0) 276 return MT_FALSE; 277 return eq(exp(x[0] * log(base)), x[1]); 278 } 279 /// Extend assignment \a x 280 virtual bool extendAssignement(Assignment& x) const { 281 if (base <= 0.0) return false; 282 Gecode::FloatVal d = exp(x[0]*log(base)); 283 if (Gecode::Float::subset(d, dom)) { 284 x.set(1, d); 285 return true; 286 } else { 287 return false; 288 } 289 } 290 /// Post constraint on \a x 291 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 292 Gecode::pow(home, base, x[0], x[1]); 293 } 294 }; 295 296 /// %Test for pow exponent n constraint with shared variables 297 class PowXX : public Test { 298 Gecode::FloatNum base; 299 public: 300 /// Create and register test 301 PowXX(const std::string& s, const Gecode::FloatVal& d, Gecode::FloatNum _base, Gecode::FloatNum st) 302 : Test("Transcendental::Pow::N::"+str(_base)+"::XX::"+s,1,d,st,CPLT_ASSIGNMENT,false), base(_base) {} 303 /// %Test whether \a x is solution 304 virtual MaybeType solution(const Assignment& x) const { 305 if ((x[0].max() <= 0.0) || (base <= 0.0)) 306 return MT_FALSE; 307 return eq(exp(x[0] * log(base)), x[0]); 308 } 309 /// Post constraint on \a x 310 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) { 311 Gecode::pow(home, base, x[0], x[0]); 312 } 313 }; 314 315 const Gecode::FloatNum step = 0.15; 316 const Gecode::FloatNum step2 = 2*step; 317 Gecode::FloatVal a(-8,5); 318 Gecode::FloatVal b(9,12); 319 Gecode::FloatVal c(-8,8); 320 321 ExpXY exp_xy_a("A",a,step); 322 ExpXY exp_xy_b("B",b,step); 323 ExpXY exp_xy_c("C",c,step); 324 325 ExpXYSol exp_xy_sol_a("A",a,step); 326 ExpXYSol exp_xy_sol_b("B",b,step); 327 ExpXYSol exp_xy_sol_c("C",c,step); 328 329 ExpXX exp_xx_a("A",a,step); 330 ExpXX exp_xx_b("B",b,step); 331 ExpXX exp_xx_c("C",c,step); 332 333 LogXY log_xy_a("A",a,step); 334 LogXY log_xy_b("B",b,step); 335 LogXY log_xy_c("C",c,step); 336 337 LogXYSol log_xy_sol_a("A",a,step); 338 LogXYSol log_xy_sol_b("B",b,step); 339 LogXYSol log_xy_sol_c("C",c,step); 340 341 LogXX log_xx_a("A",a,step); 342 LogXX log_xx_b("B",b,step); 343 LogXX log_xx_c("C",c,step); 344 345 LogNXY logn_xy_a_1("A",a,-1.5,step); 346 LogNXY logn_xy_b_1("B",b,-1.5,step); 347 LogNXY logn_xy_c_1("C",c,-1.5,step); 348 349 LogNXYSol logn_xy_sol_a_1("A",a,-1.5,step); 350 LogNXYSol logn_xy_sol_b_1("B",b,-1.5,step); 351 LogNXYSol logn_xy_sol_c_1("C",c,-1.5,step); 352 353 LogNXX logn_xx_a_1("A",a,-1.5,step); 354 LogNXX logn_xx_b_1("B",b,-1.5,step); 355 LogNXX logn_xx_c_1("C",c,-1.5,step); 356 357 LogNXY logn_xy_a_2("A",a,1.5,step); 358 LogNXY logn_xy_b_2("B",b,1.5,step); 359 LogNXY logn_xy_c_2("C",c,1.5,step); 360 361 LogNXYSol logn_xy_sol_a_2("A",a,1.5,step); 362 LogNXYSol logn_xy_sol_b_2("B",b,1.5,step); 363 LogNXYSol logn_xy_sol_c_2("C",c,1.5,step); 364 365 LogNXX logn_xx_a_2("A",a,1.5,step); 366 LogNXX logn_xx_b_2("B",b,1.5,step); 367 LogNXX logn_xx_c_2("C",c,1.5,step); 368 369 LogNXY logn_xy_a_3("A",a,0,step); 370 LogNXY logn_xy_b_3("B",b,0,step); 371 LogNXY logn_xy_c_3("C",c,0,step); 372 373 LogNXYSol logn_xy_sol_a_3("A",a,0,step); 374 LogNXYSol logn_xy_sol_b_3("B",b,0,step); 375 LogNXYSol logn_xy_sol_c_3("C",c,0,step); 376 377 LogNXX logn_xx_a_3("A",a,0,step); 378 LogNXX logn_xx_b_3("B",b,0,step); 379 LogNXX logn_xx_c_3("C",c,0,step); 380 381 PowXY pow_xy_a_1("A",a,-1.5,step); 382 PowXY pow_xy_b_1("B",b,-1.5,step); 383 PowXY pow_xy_c_1("C",c,-1.5,step); 384 385 PowXYSol pow_xy_sol_a_1("A",a,-1.5,step); 386 PowXYSol pow_xy_sol_b_1("B",b,-1.5,step); 387 PowXYSol pow_xy_sol_c_1("C",c,-1.5,step); 388 389 PowXX pow_xx_a_1("A",a,-1.5,step); 390 PowXX pow_xx_b_1("B",b,-1.5,step); 391 PowXX pow_xx_c_1("C",c,-1.5,step); 392 393 PowXY pow_xy_a_2("A",a,1.5,step); 394 PowXY pow_xy_b_2("B",b,1.5,step); 395 PowXY pow_xy_c_2("C",c,1.5,step); 396 397 PowXYSol pow_xy_sol_a_2("A",a,1.5,step); 398 PowXYSol pow_xy_sol_b_2("B",b,1.5,step); 399 PowXYSol pow_xy_sol_c_2("C",c,1.5,step); 400 401 PowXX pow_xx_a_2("A",a,1.5,step); 402 PowXX pow_xx_b_2("B",b,1.5,step); 403 PowXX pow_xx_c_2("C",c,1.5,step); 404 405 PowXY pow_xy_a_3("A",a,0,step); 406 PowXY pow_xy_b_3("B",b,0,step); 407 PowXY pow_xy_c_3("C",c,0,step); 408 409 PowXYSol pow_xy_sol_a_3("A",a,0,step); 410 PowXYSol pow_xy_sol_b_3("B",b,0,step); 411 PowXYSol pow_xy_sol_c_3("C",c,0,step); 412 413 PowXX pow_xx_a_3("A",a,0,step); 414 PowXX pow_xx_b_3("B",b,0,step); 415 PowXX pow_xx_c_3("C",c,0,step); 416 417 //@} 418 419 } 420}} 421 422#endif 423// STATISTICS: test-float