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 * Mikael Lagerkvist <lagerkvist@gecode.org> 6 * Vincent Barichard <Vincent.Barichard@univ-angers.fr> 7 * 8 * Copyright: 9 * Christian Schulte, 2005 10 * Mikael Lagerkvist, 2006 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#ifndef GECODE_TEST_FLOAT_HH 39#define GECODE_TEST_FLOAT_HH 40 41#include "test/test.hh" 42 43#include <gecode/float.hh> 44 45namespace Test { 46 47 /// Testing domain floats 48 namespace Float { 49 50 /// Type for comparisons and solutions 51 enum MaybeType { 52 MT_FALSE = 0, //< Does hold 53 MT_TRUE, //< Does not hold 54 MT_MAYBE //< Might or might not hold 55 }; 56 57 /// Three-valued conjunction of MaybeType 58 MaybeType operator &(MaybeType a, MaybeType b); 59 60 /// Assignment possible types 61 enum AssignmentType { 62 CPLT_ASSIGNMENT = 0, 63 RANDOM_ASSIGNMENT, 64 EXTEND_ASSIGNMENT 65 }; 66 67 class Test; 68 69 /** 70 * \defgroup TaskTestFloat Testing domain floats 71 * \ingroup TaskTest 72 */ 73 74 /** 75 * \defgroup TaskTestFloatFloat General test support 76 * \ingroup TaskTestFloat 77 */ 78 //@{ 79 /// %Base class for assignments 80 class Assignment { 81 protected: 82 int n; ///< Number of variables 83 Gecode::FloatVal d; ///< Domain for each variable 84 public: 85 /// Initialize assignments for \a n0 variables and values \a d0 86 Assignment(int n0, const Gecode::FloatVal& d0); 87 /// Test whether all assignments have been iterated 88 virtual bool operator()(void) const = 0; 89 /// Move to next assignment 90 virtual void operator++(void) = 0; 91 /// Return value for variable \a i 92 virtual Gecode::FloatVal operator[](int i) const = 0; 93 /// Set assignment to value \a val for variable \a i 94 virtual void set(int i, const Gecode::FloatVal& val) = 0; 95 /// Return number of variables 96 int size(void) const; 97 /// Destructor 98 virtual ~Assignment(void); 99 }; 100 101 /// Generate all assignments 102 class CpltAssignment : public Assignment { 103 protected: 104 Gecode::FloatVal* dsv; ///< Iterator for each variable 105 Gecode::FloatNum step; ///< Step for next assignment 106 public: 107 /// Initialize assignments for \a n variables and values \a d with step \a s 108 CpltAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s); 109 /// Test whether all assignments have been iterated 110 virtual bool operator()(void) const; 111 /// Move to next assignment 112 virtual void operator++(void); 113 /// Return value for variable \a i 114 virtual Gecode::FloatVal operator[](int i) const; 115 /// Set assignment to value \a val for variable \a i 116 virtual void set(int i, const Gecode::FloatVal& val); 117 /// Destructor 118 virtual ~CpltAssignment(void); 119 }; 120 121 /// Generate all assignments except the last variable and complete it to get a solution 122 class ExtAssignment : public Assignment { 123 protected: 124 const Test* curPb; ///< Current problem used to complete assignment 125 Gecode::FloatVal* dsv; ///< Iterator for each variable 126 Gecode::FloatNum step; ///< Step for next assignment 127 public: 128 /// Initialize assignments for \a n variables and values \a d with step \a s 129 ExtAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s, const Test * pb); 130 /// Test whether all assignments have been iterated 131 virtual bool operator()(void) const; 132 /// Move to next assignment 133 virtual void operator++(void); 134 /// Return value for variable \a i 135 virtual Gecode::FloatVal operator[](int i) const; 136 /// Set assignment to value \a val for variable \a i 137 virtual void set(int i, const Gecode::FloatVal& val); 138 /// Destructor 139 virtual ~ExtAssignment(void); 140 }; 141 142 143 /// Generate random selection of assignments 144 class RandomAssignment : public Assignment { 145 protected: 146 Gecode::FloatVal* vals; ///< The current values for the variables 147 int a; ///< How many assigments still to be generated 148 /// Generate new value according to domain 149 Gecode::FloatNum randval(void); 150 public: 151 /// Initialize for \a a assignments for \a n variables and values \a d 152 RandomAssignment(int n, const Gecode::FloatVal& d, int a); 153 /// Test whether all assignments have been iterated 154 virtual bool operator()(void) const; 155 /// Move to next assignment 156 virtual void operator++(void); 157 /// Return value for variable \a i 158 virtual Gecode::FloatVal operator[](int i) const; 159 /// Set assignment to value \a val for variable \a i 160 virtual void set(int i, const Gecode::FloatVal& val); 161 /// Destructor 162 virtual ~RandomAssignment(void); 163 }; 164 165 /// Space for executing tests 166 class TestSpace : public Gecode::Space { 167 public: 168 /// Initial domain 169 Gecode::FloatVal d; 170 /// Step for going to next solution 171 Gecode::FloatNum step; 172 /// Variables to be tested 173 Gecode::FloatVarArray x; 174 /// Reification information 175 Gecode::Reify r; 176 /// The test currently run 177 Test* test; 178 /// Whether the test is for a reified propagator 179 bool reified; 180 181 /** 182 * \brief Create test space 183 * 184 * Creates \a n variables with domain \a d and step \a s for 185 * test \a t. 186 * 187 */ 188 TestSpace(int n, Gecode::FloatVal& d, Gecode::FloatNum s, Test* t); 189 /** 190 * \brief Create test space 191 * 192 * Creates \a n variables with domain \a d and step \a s for 193 * test \a t annd reification mode \a rm. 194 * 195 */ 196 TestSpace(int n, Gecode::FloatVal& d, Gecode::FloatNum s, Test* t, 197 Gecode::ReifyMode rm); 198 /// Constructor for cloning \a s 199 TestSpace(TestSpace& s); 200 /// Copy space during cloning 201 virtual Gecode::Space* copy(void); 202 /// Add constraints to skip solutions to the \a a assignment 203 virtual void dropUntil(const Assignment& a); 204 /// Test whether all variables are assigned 205 bool assigned(void) const; 206 /// Test whether all variables match assignment \a a 207 bool matchAssignment(const Assignment& a) const; 208 /// Post propagator 209 void post(void); 210 /// Compute a fixpoint and check for failure 211 bool failed(void); 212 /// Perform integer tell operation on \a x[i] 213 void rel(int i, Gecode::FloatRelType frt, Gecode::FloatVal n); 214 /// Perform Boolean tell on \a b 215 void rel(bool sol); 216 /// Assign all (or all but one, if \a skip is true) variables to values in \a a 217 /// If assignment of a variable is MT_MAYBE (if the two intervals are contiguous), 218 /// \a sol is set to MT_MAYBE 219 void assign(const Assignment& a, MaybeType& sol, bool skip=false); 220 /// Assing a random variable to a random bound 221 void bound(void); 222 /// Cut the bigger variable to an half sized interval. It returns 223 /// the new size of the cut interval. \a cutDirections gives the direction 224 /// to follow (upper part or lower part of the interval). 225 Gecode::FloatNum cut(int* cutDirections); 226 /// Prune some random values from variable \a i 227 void prune(int i); 228 /// Prune some random values for some random variable 229 void prune(void); 230 /// Prune values but not those in assignment \a a 231 bool prune(const Assignment& a, bool testfix); 232 /// Disable propagators in space and compute fixpoint (make all idle) 233 void disable(void); 234 /// Enable propagators in space 235 void enable(void); 236 /// Return the number of propagators 237 unsigned int propagators(void); 238 }; 239 240 /** 241 * \brief %Base class for tests with float constraints 242 * 243 */ 244 class Test : public Base { 245 protected: 246 /// Number of variables 247 int arity; 248 /// Domain of variables 249 Gecode::FloatVal dom; 250 /// Step for going to next solution 251 Gecode::FloatNum step; 252 /// Gives the type of assignment to use 253 AssignmentType assigmentType; 254 /// Does the constraint also exist as reified constraint 255 bool reified; 256 /// Which reification modes are supported 257 int rms; 258 /// Whether to perform search test 259 bool testsearch; 260 /// Whether to perform fixpoint test 261 bool testfix; 262 /// Whether to test for subsumption 263 bool testsubsumed; 264 /// \name Test for reification modes 265 //@{ 266 /// Test whether equivalence as reification mode is supported 267 bool eqv(void) const; 268 /// Test whether implication as reification mode is supported 269 bool imp(void) const; 270 /// Test whether reverse implication as reification mode is supported 271 bool pmi(void) const; 272 //@} 273 public: 274 /** 275 * \brief Constructor 276 * 277 * Constructs a test with name \a s and arity \a a and variable 278 * domain \a d and step \a st and assignment type \a at. Also 279 * tests for a reified constraint, if \a r is true. 280 */ 281 Test(const std::string& s, int a, const Gecode::FloatVal& d, 282 Gecode::FloatNum st, AssignmentType at, 283 bool r); 284 /** 285 * \brief Constructor 286 * 287 * Constructs a test with name \a s and arity \a a and variable 288 * domain \a min ... \a max and step \a st and assignment type \a at. Also 289 * tests for a reified constraint, if \a r is true. 290 */ 291 Test(const std::string& s, int a, 292 Gecode::FloatNum min, Gecode::FloatNum max, 293 Gecode::FloatNum st, AssignmentType at, 294 bool r); 295 /// Create assignment 296 virtual Assignment* assignment(void) const; 297 /// Complete the current assignment to get a feasible one (which satisfies all constraint). 298 /// If such an assignment is computed, it returns true, false otherwise 299 virtual bool extendAssignement(Assignment& a) const; 300 /// Check for solution 301 virtual MaybeType solution(const Assignment&) const = 0; 302 /// Test if \a ts is subsumed or not (i.e. if there is no more propagator unless 303 /// the assignment is an extended assigment. 304 bool subsumed(const TestSpace& ts) const; 305 /// Whether to ignore assignment for reification 306 virtual bool ignore(const Assignment& a) const; 307 /// Post constraint 308 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) = 0; 309 /// Post reified constraint 310 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x, 311 Gecode::Reify r); 312 /// Perform test 313 virtual bool run(void); 314 /// \name Mapping scalar values to strings 315 //@{ 316 /// Map float relation to string 317 static std::string str(Gecode::FloatRelType frt); 318 /// Map floatNum to string 319 static std::string str(Gecode::FloatNum f); 320 /// Map floatVal to string 321 static std::string str(Gecode::FloatVal f); 322 /// Map float array to string 323 static std::string str(const Gecode::FloatValArgs& f); 324 //@} 325 /// \name General support 326 //@{ 327 /// Compare \a x and \a y with respect to \a r 328 static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, 329 Gecode::FloatVal y); 330 /// Whether \a x and \a y are equal 331 static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y); 332 /// Flip a coin and return true or false randomly 333 bool flip(void); 334 //@} 335 }; 336 //@} 337 338 /// Iterator for float relation types 339 class FloatRelTypes { 340 private: 341 /// Array of relation types 342 static const Gecode::FloatRelType frts[6]; 343 /// Current position in relation type array 344 int i; 345 public: 346 /// Initialize iterator 347 FloatRelTypes(void); 348 /// Reset iterator 349 void reset(void); 350 /// Test whether iterator is done 351 bool operator()(void) const; 352 /// Increment to next relation type 353 void operator++(void); 354 /// Return current relation type 355 Gecode::FloatRelType frt(void) const; 356 }; 357 358 } 359} 360 361/** 362 * \brief Print assignment \a 363 * \relates Assignment 364 */ 365std::ostream& operator<<(std::ostream& os, const Test::Float::Assignment& a); 366 367#include "test/float.hpp" 368 369#endif 370 371// STATISTICS: test-float 372