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 has_more(void) const = 0;
89 /// Move to next assignment
90 virtual void next(Gecode::Support::RandomGenerator& rand) = 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 has_more(void) const;
111 /// Move to next assignment
112 virtual void next(Gecode::Support::RandomGenerator& rand);
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 Gecode::Support::RandomGenerator& rand);
131 /// Test whether all assignments have been iterated
132 virtual bool has_more(void) const;
133 /// Move to next assignment
134 virtual void next(Gecode::Support::RandomGenerator& rand);
135 /// Return value for variable \a i
136 virtual Gecode::FloatVal operator[](int i) const;
137 /// Set assignment to value \a val for variable \a i
138 virtual void set(int i, const Gecode::FloatVal& val);
139 /// Destructor
140 virtual ~ExtAssignment(void);
141 };
142
143
144 /// Generate random selection of assignments
145 class RandomAssignment : public Assignment {
146 protected:
147 Gecode::FloatVal* vals; ///< The current values for the variables
148 int a; ///< How many assigments still to be generated
149 /// Generate new value according to domain
150 Gecode::FloatNum randval(Gecode::Support::RandomGenerator& rand);
151 public:
152 /// Initialize for \a a assignments for \a n variables and values \a d
153 RandomAssignment(int n, const Gecode::FloatVal& d, int a0, Gecode::Support::RandomGenerator& rand);
154 /// Test whether all assignments have been iterated
155 virtual bool has_more(void) const;
156 /// Move to next assignment
157 virtual void next(Gecode::Support::RandomGenerator& rand);
158 /// Return value for variable \a i
159 virtual Gecode::FloatVal operator[](int i) const;
160 /// Set assignment to value \a val for variable \a i
161 virtual void set(int i, const Gecode::FloatVal& val);
162 /// Destructor
163 virtual ~RandomAssignment(void);
164 };
165
166 /// Space for executing tests
167 class TestSpace : public Gecode::Space {
168 public:
169 /// Initial domain
170 Gecode::FloatVal d;
171 /// Step for going to next solution
172 Gecode::FloatNum step;
173 /// Variables to be tested
174 Gecode::FloatVarArray x;
175 /// Reification information
176 Gecode::Reify r;
177 /// The test currently run
178 Test* test;
179 /// Whether the test is for a reified propagator
180 bool reified;
181
182 /**
183 * \brief Create test space
184 *
185 * Creates \a n variables with domain \a d and step \a s for
186 * test \a t.
187 *
188 */
189 TestSpace(int n, Gecode::FloatVal& d, Gecode::FloatNum s, Test* t);
190 /**
191 * \brief Create test space
192 *
193 * Creates \a n variables with domain \a d and step \a s for
194 * test \a t annd reification mode \a rm.
195 *
196 */
197 TestSpace(int n, Gecode::FloatVal& d, Gecode::FloatNum s, Test* t,
198 Gecode::ReifyMode rm);
199 /// Constructor for cloning \a s
200 TestSpace(TestSpace& s);
201 /// Copy space during cloning
202 virtual Gecode::Space* copy(void);
203 /// Add constraints to skip solutions to the \a a assignment
204 virtual void dropUntil(const Assignment& a);
205 /// Test whether all variables are assigned
206 bool assigned(void) const;
207 /// Test whether all variables match assignment \a a
208 bool matchAssignment(const Assignment& a) const;
209 /// Post propagator
210 void post(void);
211 /// Compute a fixpoint and check for failure
212 bool failed(void);
213 /// Perform integer tell operation on \a x[i]
214 void rel(int i, Gecode::FloatRelType frt, Gecode::FloatVal n);
215 /// Perform Boolean tell on \a b
216 void rel(bool sol);
217 /// Assign all (or all but one, if \a skip is true) variables to values in \a a
218 /// If assignment of a variable is MT_MAYBE (if the two intervals are contiguous),
219 /// \a sol is set to MT_MAYBE
220 void assign(const Assignment& a, MaybeType& sol, bool skip, Gecode::Support::RandomGenerator& rand);
221 /// Assing a random variable to a random bound
222 void bound(Gecode::Support::RandomGenerator& rand);
223 /// Cut the bigger variable to an half sized interval. It returns
224 /// the new size of the cut interval. \a cutDirections gives the direction
225 /// to follow (upper part or lower part of the interval).
226 Gecode::FloatNum cut(int* cutDirections);
227 /// Prune some random values from variable \a i
228 void prune(int i, Gecode::Support::RandomGenerator& rand);
229 /// Prune some random values for some random variable
230 void prune(Gecode::Support::RandomGenerator& rand);
231 /// Prune values but not those in assignment \a a
232 bool prune(const Assignment& a, bool testfix, Gecode::Support::RandomGenerator& rand);
233 /// Disable propagators in space and compute fixpoint (make all idle)
234 void disable(void);
235 /// Enable propagators in space
236 void enable(void);
237 /// Return the number of propagators
238 unsigned int propagators(void);
239 };
240
241 /**
242 * \brief %Base class for tests with float constraints
243 *
244 */
245 class Test : public Base {
246 protected:
247 /// Number of variables
248 int arity;
249 /// Domain of variables
250 Gecode::FloatVal dom;
251 /// Step for going to next solution
252 Gecode::FloatNum step;
253 /// Gives the type of assignment to use
254 AssignmentType assigmentType;
255 /// Does the constraint also exist as reified constraint
256 bool reified;
257 /// Which reification modes are supported
258 int rms;
259 /// Whether to perform search test
260 bool testsearch;
261 /// Whether to perform fixpoint test
262 bool testfix;
263 /// Whether to test for subsumption
264 bool testsubsumed;
265 /// \name Test for reification modes
266 //@{
267 /// Test whether equivalence as reification mode is supported
268 bool eqv(void) const;
269 /// Test whether implication as reification mode is supported
270 bool imp(void) const;
271 /// Test whether reverse implication as reification mode is supported
272 bool pmi(void) const;
273 //@}
274 public:
275 /**
276 * \brief Constructor
277 *
278 * Constructs a test with name \a s and arity \a a and variable
279 * domain \a d and step \a st and assignment type \a at. Also
280 * tests for a reified constraint, if \a r is true.
281 */
282 Test(const std::string& s, int a, const Gecode::FloatVal& d,
283 Gecode::FloatNum st, AssignmentType at,
284 bool r);
285 /**
286 * \brief Constructor
287 *
288 * Constructs a test with name \a s and arity \a a and variable
289 * domain \a min ... \a max and step \a st and assignment type \a at. Also
290 * tests for a reified constraint, if \a r is true.
291 */
292 Test(const std::string& s, int a,
293 Gecode::FloatNum min, Gecode::FloatNum max,
294 Gecode::FloatNum st, AssignmentType at,
295 bool r);
296 /// Create assignment
297 virtual Assignment* assignment(void) const;
298 /// Complete the current assignment to get a feasible one (which satisfies all constraint).
299 /// If such an assignment is computed, it returns true, false otherwise
300 virtual bool extendAssignement(Assignment& a) const;
301 /// Check for solution
302 virtual MaybeType solution(const Assignment&) const = 0;
303 /// Test if \a ts is subsumed or not (i.e. if there is no more propagator unless
304 /// the assignment is an extended assigment.
305 bool subsumed(const TestSpace& ts) const;
306 /// Whether to ignore assignment for reification
307 virtual bool ignore(const Assignment& a) const;
308 /// Post constraint
309 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x) = 0;
310 /// Post reified constraint
311 virtual void post(Gecode::Space& home, Gecode::FloatVarArray& x,
312 Gecode::Reify r);
313 /// Perform test
314 virtual bool run(void);
315 /// \name Mapping scalar values to strings
316 //@{
317 /// Map float relation to string
318 static std::string str(Gecode::FloatRelType frt);
319 /// Map floatNum to string
320 static std::string str(Gecode::FloatNum f);
321 /// Map floatVal to string
322 static std::string str(Gecode::FloatVal f);
323 /// Map float array to string
324 static std::string str(const Gecode::FloatValArgs& f);
325 //@}
326 /// \name General support
327 //@{
328 /// Compare \a x and \a y with respect to \a r
329 static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r,
330 Gecode::FloatVal y);
331 /// Whether \a x and \a y are equal
332 static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y);
333 /// Flip a coin and return true or false randomly
334 bool flip(void);
335 //@}
336 };
337 //@}
338
339 /// Iterator for float relation types
340 class FloatRelTypes {
341 private:
342 /// Array of relation types
343 static const Gecode::FloatRelType frts[6];
344 /// Current position in relation type array
345 int i;
346 public:
347 /// Initialize iterator
348 FloatRelTypes(void);
349 /// Reset iterator
350 void reset(void);
351 /// Test whether iterator is done
352 bool operator()(void) const;
353 /// Increment to next relation type
354 void operator++(void);
355 /// Return current relation type
356 Gecode::FloatRelType frt(void) const;
357 };
358
359 }
360}
361
362/**
363 * \brief Print assignment \a
364 * \relates Assignment
365 */
366std::ostream& operator<<(std::ostream& os, const Test::Float::Assignment& a);
367
368#include "test/float.hpp"
369
370#endif
371
372// STATISTICS: test-float
373