Generated on Sat Oct 20 2018 12:43:45 for Gecode by doxygen 1.8.13
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  * 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 namespace Test { namespace Float {
39 
40  /*
41  * Assignments
42  *
43  */
44  inline
46  : n(n0), d(d0) {}
47  inline int
48  Assignment::size(void) const {
49  return n;
50  }
51  inline
53 
54  inline
56  : Assignment(n,d),
57  dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
58  step(s) {
59  using namespace Gecode;
60  for (int i=n; i--; )
61  dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
62  }
63  inline bool
65  return dsv[0].min() <= d.max();
66  }
67  inline Gecode::FloatVal
69  assert((i>=0) && (i<n));
70  return dsv[i];
71  }
72  inline void
74  assert((i>=0) && (i<n));
75  dsv[i] = val;
76  }
77  inline
79  delete [] dsv;
80  }
81 
82  inline
84  : Assignment(n,d),curPb(pb),
85  dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
86  step(s) {
87  using namespace Gecode;
88  for (int i=n-1; i--; )
89  dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
90  ++(*this);
91  }
92  inline bool
94  return dsv[0].min() <= d.max();
95  }
96  inline Gecode::FloatVal
98  assert((i>=0) && (i<n));
99  return dsv[i];
100  }
101  inline void
103  assert((i>=0) && (i<n));
104  dsv[i] = val;
105  }
106  inline
108  delete [] dsv;
109  }
110 
113  using namespace Gecode;
114  using namespace Gecode::Float;
115  Rounding r;
116  return
117  r.add_down(
118  d.min(),
119  r.mul_down(
120  r.div_down(
121  Base::rand(static_cast<unsigned int>(Gecode::Int::Limits::max)),
122  static_cast<FloatNum>(Gecode::Int::Limits::max)
123  ),
124  r.sub_down(d.max(),d.min())
125  )
126  );
127  }
128 
129  inline
131  : Assignment(n,d), vals(new Gecode::FloatVal[n]), a(a0) {
132  for (int i=n; i--; )
133  vals[i] = randval();
134  }
135 
136  inline bool
138  return a>0;
139  }
140  inline Gecode::FloatVal
142  assert((i>=0) && (i<n));
143  return vals[i];
144  }
145  inline void
147  assert((i>=0) && (i<n));
148  vals[i] = val;
149  }
150  inline
152  delete [] vals;
153  }
154 
155  /*
156  * Tests with float constraints
157  *
158  */
159  forceinline bool
160  Test::eqv(void) const {
161  return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
162  }
163  forceinline bool
164  Test::imp(void) const {
165  return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
166  }
167  forceinline bool
168  Test::pmi(void) const {
169  return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
170  }
171  inline
172  Test::Test(const std::string& s, int a, const Gecode::FloatVal& d,
174  bool r)
175  : Base("Float::"+s), arity(a), dom(d), step(st), assigmentType(at),
176  reified(r), rms((1 << Gecode::RM_EQV) ||
177  (1 << Gecode::RM_IMP) ||
178  (1 << Gecode::RM_PMI)),
179  testsearch(true), testfix(true), testsubsumed(true) {}
180 
181  inline
182  Test::Test(const std::string& s, int a, Gecode::FloatNum min,
184  bool r)
185  : Base("Float::"+s), arity(a), dom(min,max), step(st),
186  assigmentType(at), reified(r),
187  rms((1 << Gecode::RM_EQV) ||
188  (1 << Gecode::RM_IMP) ||
189  (1 << Gecode::RM_PMI)),
190  testsearch(true), testfix(true), testsubsumed(true) {}
191 
192  inline
193  std::string
195  using namespace Gecode;
196  switch (frt) {
197  case FRT_EQ: return "Eq";
198  case FRT_NQ: return "Nq";
199  case FRT_LQ: return "Lq";
200  case FRT_LE: return "Le";
201  case FRT_GQ: return "Gq";
202  case FRT_GR: return "Gr";
203  default: ;
204  }
205  GECODE_NEVER;
206  return "NONE";
207  }
208 
209  inline
210  std::string
212  std::stringstream s;
213  s << f;
214  return s.str();
215  }
216 
217  inline
218  std::string
220  std::stringstream s;
221  s << "[" << f.min() << ":" << f.max() << "]";
222  return s.str();
223  }
224 
225  inline
226  std::string
228  std::string s = "";
229  for (int i=0; i<x.size()-1; i++)
230  s += str(x[i]) + ",";
231  return "[" + s + str(x[x.size()-1]) + "]";
232  }
233 
234  inline MaybeType
236  using namespace Gecode;
237  switch (r) {
238  case FRT_EQ:
239  if (x == y) return MT_TRUE;
240  if (x != y) return MT_FALSE;
241  break;
242  case FRT_NQ:
243  if (x != y) return MT_TRUE;
244  if (x == y) return MT_FALSE;
245  break;
246  case FRT_LQ:
247  if (x <= y) return MT_TRUE;
248  if (x > y) return MT_FALSE;
249  break;
250  case FRT_LE:
251  if (x < y) return MT_TRUE;
252  if (x >= y) return MT_FALSE;
253  break;
254  case FRT_GQ:
255  if (x >= y) return MT_TRUE;
256  if (x < y) return MT_FALSE;
257  break;
258  case FRT_GR:
259  if (x > y) return MT_TRUE;
260  if (x <= y) return MT_FALSE;
261  break;
262  default: ;
263  }
264  return MT_MAYBE;
265  }
266 
267  inline MaybeType
269  return cmp(x, Gecode::FRT_EQ, y);
270  }
271 
272  inline bool
273  Test::flip(void) {
274  return Base::rand(2U) == 0U;
275  }
276 
277  inline MaybeType
279  switch (a) {
280  case MT_TRUE: return b;
281  case MT_FALSE: return MT_FALSE;
282  default: ;
283  }
284  return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
285  }
286 
287 
288  inline
290  : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
291  inline void
293  i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
294  }
295  inline bool
297  return i>=0;
298  }
299  inline void
301  i--;
302  }
303  inline Gecode::FloatRelType
304  FloatRelTypes::frt(void) const {
305  return frts[i];
306  }
307 
308 }}
309 
310 // STATISTICS: test-float
311 
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition: float.hpp:268
const Gecode::FloatNum step
Definition: arithmetic.cpp:785
Passing float arguments.
Definition: float.hh:950
Gecode::FloatNum step
Step for going to next solution.
Definition: float.hh:251
virtual ~RandomAssignment(void)
Destructor.
Definition: float.hpp:151
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:208
FloatNum add_down(FloatNum x, FloatNum y)
Return lower bound of x plus y (domain: )
AssignmentType
Assignment possible types.
Definition: float.hh:61
FloatNum mul_down(FloatNum x, FloatNum y)
Return lower bound of x times y (domain: )
Inverse implication for reification.
Definition: int.hh:869
void reset(void)
Reset iterator.
Definition: float.hpp:292
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:1569
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition: float.hpp:304
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
Disequality ( )
Definition: float.hh:1070
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition: test.hh:134
Test(const std::string &s, int a, const Gecode::FloatVal &d, Gecode::FloatNum st, AssignmentType at, bool r)
Constructor.
Definition: float.hpp:172
Gecode::FloatNum step
Step for next assignment.
Definition: float.hh:105
Less or equal ( )
Definition: float.hh:1071
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:102
bool operator()(void) const
Test whether iterator is done.
Definition: float.hpp:296
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:83
bool testfix
Whether to perform fixpoint test.
Definition: float.hh:261
void dom(Home home, FloatVar x, FloatVal n)
Propagates .
Definition: dom.cpp:40
bool imp(void) const
Test whether implication as reification mode is supported.
Definition: float.hpp:164
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition: float.hpp:235
bool testsubsumed
Whether to test for subsumption.
Definition: float.hh:263
int n
Number of variables.
Definition: float.hh:82
Less ( )
Definition: float.hh:1072
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition: float.hpp:194
#define forceinline
Definition: config.hpp:185
const int max
Largest allowed integer value.
Definition: int.hh:116
FloatRelTypes(void)
Initialize iterator.
Definition: float.hpp:289
int a
How many assigments still to be generated Generate new value according to domain. ...
Definition: float.hh:147
Gecode::IntSet d(v, 7)
Greater or equal ( )
Definition: float.hh:1073
const FloatNum min
Smallest allowed float value.
Definition: float.hh:846
virtual ~Assignment(void)
Destructor.
Definition: float.hpp:52
CpltAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s)
Initialize assignments for n variables and values d with step s.
Definition: float.hpp:55
Gecode::FloatNum randval(void)
Definition: float.hpp:112
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
FloatRelType
Relation types for floats.
Definition: float.hh:1068
RandomAssignment(int n, const Gecode::FloatVal &d, int a)
Initialize for a assignments for n variables and values d.
Definition: float.hpp:130
struct Gecode::@593::NNF::@62::@63 b
For binary nodes (and, or, eqv)
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:64
Base class for all tests to be run
Definition: test.hh:103
MaybeType operator&(MaybeType a, MaybeType b)
Three-valued conjunction of MaybeType.
Definition: float.hpp:278
bool testsearch
Whether to perform search test.
Definition: float.hh:259
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:141
Floating point rounding policy.
Definition: float.hh:154
virtual ~ExtAssignment(void)
Destructor.
Definition: float.hpp:107
Equality ( )
Definition: float.hh:1069
Greater ( )
Definition: float.hh:1074
General test support.
Definition: afc.cpp:39
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition: float.hpp:168
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
Float value type.
Definition: float.hh:334
void operator++(void)
Increment to next relation type.
Definition: float.hpp:300
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
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:83
Region r
Definition: region.cpp:65
Gecode::FloatVal dom
Domain of variables.
Definition: float.hh:249
AssignmentType assigmentType
Gives the type of assignment to use.
Definition: float.hh:253
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:51
Gecode::FloatVal * dsv
Iterator for each variable.
Definition: float.hh:125
Gecode::FloatVal * vals
The current values for the variables.
Definition: float.hh:146
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:93
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:146
int size(void) const
Return number of variables.
Definition: float.hpp:48
Gecode toplevel namespace
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:97
Implication for reification.
Definition: int.hh:862
int arity
Number of variables.
Definition: float.hh:247
Gecode::FloatVal * dsv
Iterator for each variable.
Definition: float.hh:104
bool reified
Does the constraint also exist as reified constraint.
Definition: float.hh:255
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:386
Floating point numbers.
Definition: abs.hpp:38
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition: val.hpp:398
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition: float.hpp:137
double FloatNum
Floating point number base type.
Definition: float.hh:106
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition: float.hpp:160
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition: float.hpp:73
struct Gecode::@593::NNF::@62::@64 a
For atomic nodes.
virtual ~CpltAssignment(void)
Destructor.
Definition: float.hpp:78
int rms
Which reification modes are supported.
Definition: float.hh:257
Assignment(int n0, const Gecode::FloatVal &d0)
Initialize assignments for n0 variables and values d0.
Definition: float.hpp:45
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition: float.hpp:68
bool flip(void)
Flip a coin and return true or false randomly.
Definition: float.hpp:273
Base class for assignments
Definition: float.hh:80
Equivalence for reification (default)
Definition: int.hh:855