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 * 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 <iostream> 37 38namespace Gecode { namespace Float { 39 40 /** 41 * \defgroup TaskActorFloatView Float views 42 * 43 * Float propagators and branchers compute with float views. 44 * Float views provide views on float variable implementations. 45 * \ingroup TaskActorFloat 46 */ 47 48 /** 49 * \brief Float view for float variables 50 * \ingroup TaskActorFloatView 51 */ 52 class FloatView : public VarImpView<FloatVar> { 53 protected: 54 using VarImpView<FloatVar>::x; 55 public: 56 /// \name Constructors and initialization 57 //@{ 58 /// Default constructor 59 FloatView(void); 60 /// Initialize from float variable \a y 61 FloatView(const FloatVar& y); 62 /// Initialize from float variable \a y 63 FloatView(FloatVarImp* y); 64 //@} 65 66 /// \name Value access 67 //@{ 68 /// Return domain 69 FloatVal domain(void) const; 70 /// Return minimum of domain 71 FloatNum min(void) const; 72 /// Return maximum of domain 73 FloatNum max(void) const; 74 /// Return median of domain (closest representation) 75 FloatNum med(void) const; 76 /** 77 * \brief Return assigned value 78 * 79 * Throws an exception of type Float::ValOfUnassignedVar if variable 80 * is not yet assigned. 81 * 82 */ 83 FloatVal val(void) const; 84 85 /// Return size of domain (distance between maximum and minimum) 86 FloatNum size(void) const; 87 //@} 88 89 /// \name Domain tests 90 //@{ 91 /// Test whether 0 is contained in domain 92 bool zero_in(void) const; 93 /// Test whether \a n is contained in domain 94 bool in(FloatNum n) const; 95 /// Test whether \a n is contained in domain 96 bool in(const FloatVal& n) const; 97 //@} 98 99 /// \name Domain update by value 100 //@{ 101 /// Restrict domain values to be less or equal than \a n 102 ModEvent lq(Space& home, int n); 103 /// Restrict domain values to be less or equal than \a n 104 ModEvent lq(Space& home, FloatNum n); 105 /// Restrict domain values to be less or equal than \a n 106 ModEvent lq(Space& home, FloatVal n); 107 108 /// Restrict domain values to be greater or equal than \a n 109 ModEvent gq(Space& home, int n); 110 /// Restrict domain values to be greater or equal than \a n 111 ModEvent gq(Space& home, FloatNum n); 112 /// Restrict domain values to be greater or equal than \a n 113 ModEvent gq(Space& home, FloatVal n); 114 115 /// Restrict domain values to be equal to \a n 116 ModEvent eq(Space& home, int n); 117 /// Restrict domain values to be equal to \a n 118 ModEvent eq(Space& home, FloatNum n); 119 /// Restrict domain values to be equal to \a n 120 ModEvent eq(Space& home, const FloatVal& n); 121 122 //@} 123 124 /// \name Delta information for advisors 125 //@{ 126 /// Return minimum value just pruned 127 FloatNum min(const Delta& d) const; 128 /// Return maximum value just pruned 129 FloatNum max(const Delta& d) const; 130 //@} 131 132 /// \name View-dependent propagator support 133 //@{ 134 /// Translate modification event \a me to modification event delta for view 135 static ModEventDelta med(ModEvent me); 136 //@} 137 }; 138 139 /** 140 * \brief Print float variable view 141 * \relates Gecode::Float::FloatView 142 */ 143 template<class Char, class Traits> 144 std::basic_ostream<Char,Traits>& 145 operator <<(std::basic_ostream<Char,Traits>& os, const FloatView& x); 146 147 /** 148 * \brief Minus float view 149 * 150 * A minus float view \f$m\f$ for an float view \f$x\f$ provides 151 * operations such that \f$m\f$ behaves as \f$-x\f$. 152 * \ingroup TaskActorFloatView 153 */ 154 class MinusView : public DerivedView<FloatView> { 155 protected: 156 using DerivedView<FloatView>::x; 157 public: 158 /// \name Constructors and initialization 159 //@{ 160 /// Default constructor 161 MinusView(void); 162 /// Initialize with float view \a y 163 explicit MinusView(const FloatView& y); 164 //@} 165 166 /// \name Value access 167 //@{ 168 /// Return domain 169 FloatVal domain(void) const; 170 /// Return minimum of domain 171 FloatNum min(void) const; 172 /// Return maximum of domain 173 FloatNum max(void) const; 174 /// Return median of domain (closest representation) 175 FloatNum med(void) const; 176 /** 177 * \brief Return assigned value 178 * 179 * Throws an exception of type Float::ValOfUnassignedVar if variable 180 * is not yet assigned. 181 * 182 */ 183 FloatVal val(void) const; 184 185 /// Return size of domain (distance between maximum and minimum) 186 FloatNum size(void) const; 187 //@} 188 189 /// \name Domain tests 190 //@{ 191 /// Test whether 0 is contained in domain 192 bool zero_in(void) const; 193 /// Test whether \a n is contained in domain 194 bool in(FloatNum n) const; 195 /// Test whether \a n is contained in domain 196 bool in(const FloatVal& n) const; 197 //@} 198 199 /// \name Domain update by value 200 //@{ 201 /// Restrict domain values to be less or equal than \a n 202 ModEvent lq(Space& home, int n); 203 /// Restrict domain values to be less or equal than \a n 204 ModEvent lq(Space& home, FloatNum n); 205 /// Restrict domain values to be less or equal than \a n 206 ModEvent lq(Space& home, FloatVal n); 207 208 /// Restrict domain values to be greater or equal than \a n 209 ModEvent gq(Space& home, int n); 210 /// Restrict domain values to be greater or equal than \a n 211 ModEvent gq(Space& home, FloatNum n); 212 /// Restrict domain values to be greater or equal than \a n 213 ModEvent gq(Space& home, FloatVal n); 214 215 /// Restrict domain values to be equal to \a n 216 ModEvent eq(Space& home, int n); 217 /// Restrict domain values to be equal to \a n 218 ModEvent eq(Space& home, FloatNum n); 219 /// Restrict domain values to be equal to \a n 220 ModEvent eq(Space& home, const FloatVal& n); 221 222 //@} 223 224 /// \name Delta information for advisors 225 //@{ 226 /// Return minimum value just pruned 227 FloatNum min(const Delta& d) const; 228 /// Return maximum value just pruned 229 FloatNum max(const Delta& d) const; 230 //@} 231 232 /// \name View-dependent propagator support 233 //@{ 234 /// Translate modification event \a me to modification event delta for view 235 static ModEventDelta med(ModEvent me); 236 //@} 237 }; 238 239 /** 240 * \brief Print float minus view 241 * \relates Gecode::Float::MinusView 242 */ 243 template<class Char, class Traits> 244 std::basic_ostream<Char,Traits>& 245 operator <<(std::basic_ostream<Char,Traits>& os, const MinusView& x); 246 247 /** \name View comparison 248 * \relates Gecode::Float::MinusView 249 */ 250 //@{ 251 /// Test whether views \a x and \a y are the same 252 bool operator ==(const MinusView& x, const MinusView& y); 253 /// Test whether views \a x and \a y are not the same 254 bool operator !=(const MinusView& x, const MinusView& y); 255 //@} 256 257 258 /** 259 * \brief OffsetView float view 260 * 261 * An offset float view \f$o\f$ for an float view \f$x\f$ and 262 * an float \f$c\f$ provides operations such that \f$o\f$ 263 * behaves as \f$x+c\f$. 264 * \ingroup TaskActorFloatView 265 */ 266 class OffsetView : public DerivedView<FloatView> { 267 protected: 268 /// Offset 269 FloatNum c; 270 using DerivedView<FloatView>::x; 271 public: 272 /// \name Constructors and initialization 273 //@{ 274 /// Default constructor 275 OffsetView(void); 276 /// Initialize with float view \a y and an offset c 277 explicit OffsetView(const FloatView& y, FloatNum c); 278 //@} 279 280 /// \name Value access 281 //@{ 282 /// Return offset 283 FloatNum offset(void) const; 284 /// Change offset to be \a n 285 void offset(FloatNum n); 286 /// Return domain 287 FloatVal domain(void) const; 288 /// Return minimum of domain 289 FloatNum min(void) const; 290 /// Return maximum of domain 291 FloatNum max(void) const; 292 /// Return median of domain (closest representation) 293 FloatNum med(void) const; 294 /** 295 * \brief Return assigned value 296 * 297 * Throws an exception of type Float::ValOfUnassignedVar if variable 298 * is not yet assigned. 299 * 300 */ 301 FloatVal val(void) const; 302 303 /// Return size of domain (distance between maximum and minimum) 304 FloatNum size(void) const; 305 //@} 306 307 /// \name Domain tests 308 //@{ 309 /// Test whether 0 is contained in domain 310 bool zero_in(void) const; 311 /// Test whether \a n is contained in domain 312 bool in(FloatNum n) const; 313 /// Test whether \a n is contained in domain 314 bool in(const FloatVal& n) const; 315 //@} 316 317 /// \name Domain update by value 318 //@{ 319 /// Restrict domain values to be less or equal than \a n 320 ModEvent lq(Space& home, int n); 321 /// Restrict domain values to be less or equal than \a n 322 ModEvent lq(Space& home, FloatNum n); 323 /// Restrict domain values to be less or equal than \a n 324 ModEvent lq(Space& home, FloatVal n); 325 326 /// Restrict domain values to be greater or equal than \a n 327 ModEvent gq(Space& home, int n); 328 /// Restrict domain values to be greater or equal than \a n 329 ModEvent gq(Space& home, FloatNum n); 330 /// Restrict domain values to be greater or equal than \a n 331 ModEvent gq(Space& home, FloatVal n); 332 333 /// Restrict domain values to be equal to \a n 334 ModEvent eq(Space& home, int n); 335 /// Restrict domain values to be equal to \a n 336 ModEvent eq(Space& home, FloatNum n); 337 /// Restrict domain values to be equal to \a n 338 ModEvent eq(Space& home, const FloatVal& n); 339 340 //@} 341 342 /// \name Delta information for advisors 343 //@{ 344 /// Return minimum value just pruned 345 FloatNum min(const Delta& d) const; 346 /// Return maximum value just pruned 347 FloatNum max(const Delta& d) const; 348 //@} 349 350 /// \name View-dependent propagator support 351 //@{ 352 /// Translate modification event \a me to modification event delta for view 353 static ModEventDelta med(ModEvent me); 354 //@} 355 356 /// \name Cloning 357 //@{ 358 void update(Space& home, OffsetView& y); 359 //@} 360 361 /// \name Ordering 362 //@{ 363 /// Whether this view comes before view \a y (arbitray order) 364 bool operator <(const OffsetView& y) const; 365 //@} 366 }; 367 368 /** 369 * \brief Print float offset view 370 * \relates Gecode::Float::OffsetView 371 */ 372 template<class Char, class Traits> 373 std::basic_ostream<Char,Traits>& 374 operator <<(std::basic_ostream<Char,Traits>& os, const OffsetView& x); 375 376 /** \name View comparison 377 * \relates Gecode::Float::OffsetView 378 */ 379 //@{ 380 /// Test whether views \a x and \a y are the same 381 bool operator ==(const OffsetView& x, const OffsetView& y); 382 /// Test whether views \a x and \a y are not the same 383 bool operator !=(const OffsetView& x, const OffsetView& y); 384 //@} 385 386 /** 387 * \brief Scale float view 388 * 389 * A scale float view \f$s\f$ for a float view \f$x\f$ and 390 * a non-negative float \f$a\f$ provides operations such that \f$s\f$ 391 * behaves as \f$a\cdot x\f$. 392 * 393 * \ingroup TaskActorFloatView 394 */ 395 class ScaleView : public DerivedView<FloatView> { 396 protected: 397 using DerivedView<FloatView>::x; 398 /// Scale factor 399 FloatVal a; 400 401 public: 402 /// \name Constructors and initialization 403 //@{ 404 /// Default constructor 405 ScaleView(void); 406 /// Initialize as \f$b\cdot y\f$ 407 ScaleView(FloatVal b, const FloatView& y); 408 //@} 409 410 /// \name Value access 411 //@{ 412 /// Return domain 413 FloatVal domain(void) const; 414 /// Return scale factor of scale view 415 FloatVal scale(void) const; 416 /// Return minimum of domain 417 FloatNum min(void) const; 418 /// Return maximum of domain 419 FloatNum max(void) const; 420 /// Return median of domain (closest representation) 421 FloatNum med(void) const; 422 423 /** 424 * \brief Return assigned value 425 * 426 * Throws an exception of type Float::ValOfUnassignedVar if variable 427 * is not yet assigned. 428 * 429 */ 430 FloatVal val(void) const; 431 432 /// Return size of domain (distance between maximum and minimum) 433 FloatNum size(void) const; 434 //@} 435 436 /// \name Domain tests 437 //@{ 438 /// Test whether 0 is contained in domain 439 bool zero_in(void) const; 440 /// Test whether \a n is contained in domain 441 bool in(FloatNum n) const; 442 /// Test whether \a n is contained in domain 443 bool in(const FloatVal& n) const; 444 //@} 445 446 /// \name Domain update by value 447 //@{ 448 /// Restrict domain values to be less or equal than \a n 449 ModEvent lq(Space& home, int n); 450 /// Restrict domain values to be less or equal than \a n 451 ModEvent lq(Space& home, FloatNum n); 452 /// Restrict domain values to be less or equal than \a n 453 ModEvent lq(Space& home, FloatVal n); 454 455 /// Restrict domain values to be greater or equal than \a n 456 ModEvent gq(Space& home, int n); 457 /// Restrict domain values to be greater or equal than \a n 458 ModEvent gq(Space& home, FloatNum n); 459 /// Restrict domain values to be greater or equal than \a n 460 ModEvent gq(Space& home, FloatVal n); 461 462 /// Restrict domain values to be equal to \a n 463 ModEvent eq(Space& home, int n); 464 /// Restrict domain values to be equal to \a n 465 ModEvent eq(Space& home, FloatNum n); 466 /// Restrict domain values to be equal to \a n 467 ModEvent eq(Space& home, const FloatVal& n); 468 469 //@} 470 471 /// \name Delta information for advisors 472 //@{ 473 /// Return minimum value just pruned 474 FloatNum min(const Delta& d) const; 475 /// Return maximum value just pruned 476 FloatNum max(const Delta& d) const; 477 //@} 478 479 /// \name View-dependent propagator support 480 //@{ 481 /// Translate modification event \a me to modification event delta for view 482 static ModEventDelta med(ModEvent me); 483 //@} 484 485 /// \name Cloning 486 //@{ 487 void update(Space& home, ScaleView& y); 488 //@} 489 490 /// \name Ordering 491 //@{ 492 /// Whether this view comes before view \a y (arbitray order) 493 bool operator <(const ScaleView& y) const; 494 //@} 495 }; 496 497 /** 498 * \brief Print scale view 499 * \relates Gecode::Float::ScaleView 500 */ 501 template<class Char, class Traits> 502 std::basic_ostream<Char,Traits>& 503 operator <<(std::basic_ostream<Char,Traits>& os, const ScaleView& x); 504 505 /** \name View comparison 506 * \relates Gecode::Float::ScaleView 507 */ 508 //@{ 509 /// Test whether views \a x and \a y are the same 510 bool operator ==(const ScaleView& x, const ScaleView& y); 511 /// Test whether views \a x and \a y are not the same 512 bool operator !=(const ScaleView& x, const ScaleView& y); 513 //@} 514 515}} 516 517#include <gecode/float/var/float.hpp> 518#include <gecode/float/view/float.hpp> 519#include <gecode/float/view/minus.hpp> 520#include <gecode/float/view/offset.hpp> 521#include <gecode/float/view/scale.hpp> 522#include <gecode/float/view/print.hpp> 523#include <gecode/float/var/print.hpp> 524 525namespace Gecode { namespace Float { 526 /** 527 * \defgroup TaskActorFloatTest Testing relations between float views 528 * \ingroup TaskActorFloat 529 */ 530 531 //@{ 532 /// Result of testing relation 533 enum RelTest { 534 RT_FALSE = 0, ///< Relation does not hold 535 RT_MAYBE = 1, ///< Relation may hold or not 536 RT_TRUE = 2 ///< Relation does hold 537 }; 538 539 /// Test whether views \a x and \a y are equal 540 template<class View> RelTest rtest_eq(View x, View y); 541 /// Test whether view \a x and Float \a n are equal 542 template<class View> RelTest rtest_eq(View x, FloatVal n); 543 544 /// Test whether view \a x is less or equal than view \a y 545 template<class View> RelTest rtest_lq(View x, View y); 546 /// Test whether view \a x is less or equal than float \a n 547 template<class View> RelTest rtest_lq(View x, FloatVal n); 548 549 /// Test whether view \a x is less than view \a y 550 template<class View> RelTest rtest_le(View x, View y); 551 /// Test whether view \a x is less or equal than float \a n 552 template<class View> RelTest rtest_le(View x, FloatVal n); 553 554 //@} 555 556}} 557 558#include <gecode/float/view/rel-test.hpp> 559 560// STATISTICS: float-var