Generated on Sat Feb 7 2015 02:01:17 for Gecode by doxygen 1.8.9.1
float.hpp
Go to the documentation of this file.
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  * Last modified:
14  * $Date: 2013-02-06 15:10:02 +0100 (Wed, 06 Feb 2013) $ by $Author: schulte $
15  * $Revision: 13270 $
16  *
17  * This file is part of Gecode, the generic constraint
18  * development environment:
19  * http://www.gecode.org
20  *
21  * Permission is hereby granted, free of charge, to any person obtaining
22  * a copy of this software and associated documentation files (the
23  * "Software"), to deal in the Software without restriction, including
24  * without limitation the rights to use, copy, modify, merge, publish,
25  * distribute, sublicense, and/or sell copies of the Software, and to
26  * permit persons to whom the Software is furnished to do so, subject to
27  * the following conditions:
28  *
29  * The above copyright notice and this permission notice shall be
30  * included in all copies or substantial portions of the Software.
31  *
32  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
33  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
34  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
35  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
36  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
37  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
38  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
39  *
40  */
41 
42 namespace Test { namespace Float {
43 
44  /*
45  * Assignments
46  *
47  */
48  inline
50  : n(n0), d(d0) {}
51  inline int
52  Assignment::size(void) const {
53  return n;
54  }
55  inline
57 
58  inline
60  : Assignment(n,d),
61  dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
62  step(s) {
63  using namespace Gecode;
64  for (int i=n; i--; )
65  dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
66  }
67  inline bool
69  return dsv[0].min() <= d.max();
70  }
71  inline Gecode::FloatVal
73  assert((i>=0) && (i<n));
74  return dsv[i];
75  }
76  inline void
78  assert((i>=0) && (i<n));
79  dsv[i] = val;
80  }
81  inline
83  delete [] dsv;
84  }
85 
86  inline
88  : Assignment(n,d),curPb(pb),
89  dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
90  step(s) {
91  using namespace Gecode;
92  for (int i=n-1; i--; )
93  dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
94  ++(*this);
95  }
96  inline bool
98  return dsv[0].min() <= d.max();
99  }
100  inline Gecode::FloatVal
102  assert((i>=0) && (i<n));
103  return dsv[i];
104  }
105  inline void
107  assert((i>=0) && (i<n));
108  dsv[i] = val;
109  }
110  inline
112  delete [] dsv;
113  }
114 
117  using namespace Gecode;
118  using namespace Gecode::Float;
119  Rounding r;
120  return
121  r.add_down(
122  d.min(),
123  r.mul_down(
124  r.div_down(
125  Base::rand(static_cast<unsigned int>(Gecode::Int::Limits::max)),
126  static_cast<FloatNum>(Gecode::Int::Limits::max)
127  ),
128  r.sub_down(d.max(),d.min())
129  )
130  );
131  }
132 
133  inline
135  : Assignment(n,d), vals(new Gecode::FloatVal[n]), a(a0) {
136  for (int i=n; i--; )
137  vals[i] = randval();
138  }
139 
140  inline bool
142  return a>0;
143  }
144  inline Gecode::FloatVal
146  assert((i>=0) && (i<n));
147  return vals[i];
148  }
149  inline void
151  assert((i>=0) && (i<n));
152  vals[i] = val;
153  }
154  inline
156  delete [] vals;
157  }
158 
159  /*
160  * Tests with float constraints
161  *
162  */
163  forceinline bool
164  Test::eqv(void) const {
165  return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
166  }
167  forceinline bool
168  Test::imp(void) const {
169  return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
170  }
171  forceinline bool
172  Test::pmi(void) const {
173  return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
174  }
175  inline
176  Test::Test(const std::string& s, int a, const Gecode::FloatVal& d,
178  bool r)
179  : Base("Float::"+s), arity(a), dom(d), step(st), assigmentType(at),
180  reified(r), rms((1 << Gecode::RM_EQV) ||
181  (1 << Gecode::RM_IMP) ||
182  (1 << Gecode::RM_PMI)),
183  testsearch(true), testfix(true), testsubsumed(true) {}
184 
185  inline
186  Test::Test(const std::string& s, int a, Gecode::FloatNum min,
188  bool r)
189  : Base("Float::"+s), arity(a), dom(min,max), step(st),
190  assigmentType(at), reified(r),
191  rms((1 << Gecode::RM_EQV) ||
192  (1 << Gecode::RM_IMP) ||
193  (1 << Gecode::RM_PMI)),
194  testsearch(true), testfix(true), testsubsumed(true) {}
195 
196  inline
197  std::string
199  using namespace Gecode;
200  switch (frt) {
201  case FRT_EQ: return "Eq";
202  case FRT_NQ: return "Nq";
203  case FRT_LQ: return "Lq";
204  case FRT_LE: return "Le";
205  case FRT_GQ: return "Gq";
206  case FRT_GR: return "Gr";
207  default: ;
208  }
209  GECODE_NEVER;
210  return "NONE";
211  }
212 
213  inline
214  std::string
216  std::stringstream s;
217  s << f;
218  return s.str();
219  }
220 
221  inline
222  std::string
224  std::stringstream s;
225  s << "[" << f.min() << ":" << f.max() << "]";
226  return s.str();
227  }
228 
229  inline
230  std::string
232  std::string s = "";
233  for (int i=0; i<x.size()-1; i++)
234  s += str(x[i]) + ",";
235  return "[" + s + str(x[x.size()-1]) + "]";
236  }
237 
238  inline MaybeType
240  using namespace Gecode;
241  switch (r) {
242  case FRT_EQ:
243  if (x == y) return MT_TRUE;
244  if (x != y) return MT_FALSE;
245  break;
246  case FRT_NQ:
247  if (x != y) return MT_TRUE;
248  if (x == y) return MT_FALSE;
249  break;
250  case FRT_LQ:
251  if (x <= y) return MT_TRUE;
252  if (x > y) return MT_FALSE;
253  break;
254  case FRT_LE:
255  if (x < y) return MT_TRUE;
256  if (x >= y) return MT_FALSE;
257  break;
258  case FRT_GQ:
259  if (x >= y) return MT_TRUE;
260  if (x < y) return MT_FALSE;
261  break;
262  case FRT_GR:
263  if (x > y) return MT_TRUE;
264  if (x <= y) return MT_FALSE;
265  break;
266  default: ;
267  }
268  return MT_MAYBE;
269  }
270 
271  inline MaybeType
273  return cmp(x, Gecode::FRT_EQ, y);
274  }
275 
276  inline bool
277  Test::flip(void) {
278  return Base::rand(2U) == 0U;
279  }
280 
281  inline MaybeType
283  switch (a) {
284  case MT_TRUE: return b;
285  case MT_FALSE: return MT_FALSE;
286  default: ;
287  }
288  return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
289  }
290 
291 
292  inline
294  : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
295  inline void
297  i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
298  }
299  inline bool
301  return i>=0;
302  }
303  inline void
305  i--;
306  }
307  inline Gecode::FloatRelType
308  FloatRelTypes::frt(void) const {
309  return frts[i];
310  }
311 
312 }}
313 
314 // STATISTICS: test-float
315 
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition: float.hpp:272
const Gecode::FloatNum step
Definition: arithmetic.cpp:789
Passing float arguments.
Definition: float.hh:937
virtual ~RandomAssignment(void)
Destructor.
Definition: float.hpp:155
FloatNum add_down(FloatNum x, FloatNum y)
Return lower bound of x plus y (domain: )
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:145
AssignmentType
Assignment possible types.
Definition: float.hh:65
FloatNum mul_down(FloatNum x, FloatNum y)
Return lower bound of x times y (domain: )
bool operator()(void) const
Test whether iterator is done.
Definition: float.hpp:300
Inverse implication for reification.
Definition: int.hh:847
void reset(void)
Reset iterator.
Definition: float.hpp:296
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:97
Disequality ( )
Definition: float.hh:1056
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1662
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:138
Test(const std::string &s, int a, const Gecode::FloatVal &d, Gecode::FloatNum st, AssignmentType at, bool r)
Constructor.
Definition: float.hpp:176
Gecode::FloatNum step
Step for next assignment.
Definition: float.hh:109
Less or equal ( )
Definition: float.hh:1057
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:106
ExtAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s, const Test *pb)
Initialize assignments for n variables and values d with step s.
Definition: float.hpp:87
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:44
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition: float.hpp:239
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:141
int n
Number of variables.
Definition: float.hh:86
Less ( )
Definition: float.hh:1058
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition: float.hpp:198
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition: float.hpp:172
const int max
Largest allowed integer value.
Definition: int.hh:113
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition: float.hpp:308
FloatRelTypes(void)
Initialize iterator.
Definition: float.hpp:293
int a
How many assigments still to be generated Generate new value according to domain. ...
Definition: float.hh:151
bool imp(void) const
Test whether implication as reification mode is supported.
Definition: float.hpp:168
Gecode::IntSet d(v, 7)
Greater or equal ( )
Definition: float.hh:1059
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual ~Assignment(void)
Destructor.
Definition: float.hpp:56
CpltAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s)
Initialize assignments for n variables and values d with step s.
Definition: float.hpp:59
Gecode::FloatNum randval(void)
Definition: float.hpp:116
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
FloatRelType
Relation types for floats.
Definition: float.hh:1054
RandomAssignment(int n, const Gecode::FloatVal &d, int a)
Initialize for a assignments for n variables and values d.
Definition: float.hpp:134
Base class for all tests to be run
Definition: test.hh:107
Floating point rounding policy.
Definition: float.hh:137
virtual ~ExtAssignment(void)
Destructor.
Definition: float.hpp:111
Equality ( )
Definition: float.hh:1055
Greater ( )
Definition: float.hh:1060
General test support.
Definition: afc.cpp:43
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:101
Float value type.
Definition: float.hh:321
void operator++(void)
Increment to next relation type.
Definition: float.hpp:304
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
FloatNum sub_down(FloatNum x, FloatNum y)
Return lower bound of x minus y (domain: )
Gecode::FloatVal d
Domain for each variable.
Definition: float.hh:87
FloatNum div_down(FloatNum x, FloatNum y)
Return lower bound of x divided by y (domain: )
MaybeType
Type for comparisons and solutions.
Definition: float.hh:55
#define forceinline
Definition: config.hpp:132
Gecode::FloatVal * dsv
Iterator for each variable.
Definition: float.hh:129
MaybeType operator&(MaybeType a, MaybeType b)
Three-valued conjunction of MaybeType.
Definition: float.hpp:282
int size(void) const
Return number of variables.
Definition: float.hpp:52
Gecode::FloatVal * vals
The current values for the variables.
Definition: float.hh:150
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:150
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:72
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:68
struct Gecode::@518::NNF::@57::@58 b
For binary nodes (and, or, eqv)
Gecode toplevel namespace
Implication for reification.
Definition: int.hh:840
Gecode::FloatVal * dsv
Iterator for each variable.
Definition: float.hh:108
bool reified
Does the constraint also exist as reified constraint.
Definition: float.hh:253
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:390
Floating point numbers.
Definition: div.hpp:40
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:402
double FloatNum
Floating point number base type.
Definition: float.hh:108
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:77
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
virtual ~CpltAssignment(void)
Destructor.
Definition: float.hpp:82
int rms
Which reification modes are supported.
Definition: float.hh:255
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition: float.hpp:164
Assignment(int n0, const Gecode::FloatVal &d0)
Initialize assignments for n0 variables and values d0.
Definition: float.hpp:49
bool flip(void)
Flip a coin and return true or false randomly.
Definition: float.hpp:277
Base class for assignments
Definition: float.hh:84
Equivalence for reification (default)
Definition: int.hh:833