Generated on for Gecode by doxygen 1.17.0
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.dev>
5 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
6 * Vincent Barichard <Vincent.Barichard@univ-angers.fr>
7 *
8 * Copyright:
9 * Christian Schulte, 2005
10 * Mikael Zayenz Lagerkvist, 2006
11 * Vincent Barichard, 2012
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
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
38namespace 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
85 : Assignment(n,d),curPb(pb),
86 dsv(new Gecode::FloatVal[static_cast<unsigned int>(n)]),
87 step(s) {
88 using namespace Gecode;
89 for (int i=n-1; i--; )
90 dsv[i] = FloatVal(d.min(),nextafter(d.min(),d.max()));
91 (*this).next(rand);
92 }
93 inline bool
95 return dsv[0].min() <= d.max();
96 }
97 inline Gecode::FloatVal
99 assert((i>=0) && (i<n));
100 return dsv[i];
101 }
102 inline void
104 assert((i>=0) && (i<n));
105 dsv[i] = val;
106 }
107 inline
109 delete [] dsv;
110 }
111
112 forceinline Gecode::FloatNum
114 using namespace Gecode;
115 using namespace Gecode::Float;
116 Rounding r;
117 return
118 r.add_down(
119 d.min(),
120 r.mul_down(
121 r.div_down(
122 rand(static_cast<unsigned int>(Gecode::Int::Limits::max)),
124 ),
125 r.sub_down(d.max(),d.min())
126 )
127 );
128 }
129
130 inline
132 : Assignment(n,d), vals(new Gecode::FloatVal[n]), a(a0) {
133 for (int i=n; i--; )
134 vals[i] = randval(rand);
135 }
136
137 inline bool
139 return a>0;
140 }
141 inline Gecode::FloatVal
143 assert((i>=0) && (i<n));
144 return vals[i];
145 }
146 inline void
148 assert((i>=0) && (i<n));
149 vals[i] = val;
150 }
151 inline
153 delete [] vals;
154 }
155
156 /*
157 * Tests with float constraints
158 *
159 */
160 forceinline bool
161 Test::eqv(void) const {
162 return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
163 }
164 forceinline bool
165 Test::imp(void) const {
166 return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
167 }
168 forceinline bool
169 Test::pmi(void) const {
170 return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
171 }
172 inline
173 Test::Test(const std::string& s, int a, const Gecode::FloatVal& d,
175 bool r)
176 : Base("Float::"+s), arity(a), dom(d), step(st), assignmentType(at),
177 reified(r), rms((1 << Gecode::RM_EQV) |
178 (1 << Gecode::RM_IMP) |
179 (1 << Gecode::RM_PMI)),
180 testsearch(true), testfix(true), testsubsumed(true) {}
181
182 inline
183 Test::Test(const std::string& s, int a, Gecode::FloatNum min,
185 bool r)
186 : Base("Float::"+s), arity(a), dom(min,max), step(st),
187 assignmentType(at), reified(r),
188 rms((1 << Gecode::RM_EQV) |
189 (1 << Gecode::RM_IMP) |
190 (1 << Gecode::RM_PMI)),
191 testsearch(true), testfix(true), testsubsumed(true) {}
192
193 inline
194 std::string
196 using namespace Gecode;
197 switch (frt) {
198 case FRT_EQ: return "Eq";
199 case FRT_NQ: return "Nq";
200 case FRT_LQ: return "Lq";
201 case FRT_LE: return "Le";
202 case FRT_GQ: return "Gq";
203 case FRT_GR: return "Gr";
204 default: ;
205 }
207 return "NONE";
208 }
209
210 inline
211 std::string
213 std::stringstream s;
214 s << f;
215 return s.str();
216 }
217
218 inline
219 std::string
221 std::stringstream s;
222 s << "[" << f.min() << ":" << f.max() << "]";
223 return s.str();
224 }
225
226 inline
227 std::string
229 std::string s = "";
230 for (int i=0; i<x.size()-1; i++)
231 s += str(x[i]) + ",";
232 return "[" + s + str(x[x.size()-1]) + "]";
233 }
234
235 inline MaybeType
237 using namespace Gecode;
238 switch (r) {
239 case FRT_EQ:
240 if (x == y) return MT_TRUE;
241 if (x != y) return MT_FALSE;
242 break;
243 case FRT_NQ:
244 if (x != y) return MT_TRUE;
245 if (x == y) return MT_FALSE;
246 break;
247 case FRT_LQ:
248 if (x <= y) return MT_TRUE;
249 if (x > y) return MT_FALSE;
250 break;
251 case FRT_LE:
252 if (x < y) return MT_TRUE;
253 if (x >= y) return MT_FALSE;
254 break;
255 case FRT_GQ:
256 if (x >= y) return MT_TRUE;
257 if (x < y) return MT_FALSE;
258 break;
259 case FRT_GR:
260 if (x > y) return MT_TRUE;
261 if (x <= y) return MT_FALSE;
262 break;
263 default: ;
264 }
265 return MT_MAYBE;
266 }
267
268 inline MaybeType
272
273 inline bool
275 return _rand(2U) == 0U;
276 }
277
278 inline MaybeType
279 operator &(MaybeType a, MaybeType b) {
280 switch (a) {
281 case MT_TRUE: return b;
282 case MT_FALSE: return MT_FALSE;
283 default: ;
284 }
285 return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
286 }
287
288
289 inline
291 : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
292 inline void
294 i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
295 }
296 inline bool
298 return i>=0;
299 }
300 inline void
302 i--;
303 }
305 FloatRelTypes::frt(void) const {
306 return frts[i];
307 }
308
309}}
310
311// STATISTICS: test-float
312
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1597
Passing float arguments.
Definition float.hh:953
Float value type.
Definition float.hh:334
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:403
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:415
Floating point rounding policy.
Definition float.hh:154
FloatNum sub_down(FloatNum x, FloatNum y)
Return lower bound of x minus y (domain: ).
FloatNum div_down(FloatNum x, FloatNum y)
Return lower bound of x divided by y (domain: ).
FloatNum mul_down(FloatNum x, FloatNum y)
Return lower bound of x times y (domain: ).
FloatNum add_down(FloatNum x, FloatNum y)
Return lower bound of x plus y (domain: ).
Gecode::Support::RandomGenerator _rand
Random number generator.
Definition test.hh:164
Base(std::string s)
Create and register test with name s.
int n
Number of variables.
Definition float.hh:82
Assignment(int n0, const Gecode::FloatVal &d0)
Initialize assignments for n0 variables and values d0.
Definition float.hpp:45
Gecode::FloatVal d
Domain for each variable.
Definition float.hh:83
int size(void) const
Return number of variables.
Definition float.hpp:48
virtual ~Assignment(void)
Destructor.
Definition float.hpp:52
virtual ~CpltAssignment(void)
Destructor.
Definition float.hpp:78
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:68
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:105
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:104
virtual bool has_more(void) const
Test whether all assignments have been iterated.
Definition float.hpp:64
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:73
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
virtual bool has_more(void) const
Test whether all assignments have been iterated.
Definition float.hpp:94
virtual ~ExtAssignment(void)
Destructor.
Definition float.hpp:108
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:103
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:125
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:98
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:126
const Test * curPb
Current problem used to complete assignment.
Definition float.hh:124
ExtAssignment(int n, const Gecode::FloatVal &d, Gecode::FloatNum s, const Test *pb, Gecode::Support::RandomGenerator &rand)
Initialize assignments for n variables and values d with step s.
Definition float.hpp:83
void operator++(void)
Increment to next relation type.
Definition float.hpp:301
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition float.hpp:305
FloatRelTypes(void)
Initialize iterator.
Definition float.hpp:290
bool operator()(void) const
Test whether iterator is done.
Definition float.hpp:297
void reset(void)
Reset iterator.
Definition float.hpp:293
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:142
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:147
RandomAssignment(int n, const Gecode::FloatVal &d, int a0, Gecode::Support::RandomGenerator &rand)
Initialize for a assignments for n variables and values d.
Definition float.hpp:131
Gecode::FloatNum randval(Gecode::Support::RandomGenerator &rand)
Definition float.hpp:113
Gecode::FloatVal * vals
The current values for the variables.
Definition float.hh:147
int a
How many assignments still to be generated Generate new value according to domain.
Definition float.hh:148
virtual bool has_more(void) const
Test whether all assignments have been iterated.
Definition float.hpp:138
virtual ~RandomAssignment(void)
Destructor.
Definition float.hpp:152
int rms
Which reification modes are supported.
Definition float.hh:258
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:269
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition float.hpp:236
bool testsubsumed
Whether to test for subsumption.
Definition float.hh:264
bool testfix
Whether to perform fixpoint test.
Definition float.hh:262
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:161
int arity
Number of variables.
Definition float.hh:248
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:195
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:274
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:252
AssignmentType assignmentType
Gives the type of assignment to use.
Definition float.hh:254
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition float.hpp:169
Test(const std::string &s, int a, const Gecode::FloatVal &d, Gecode::FloatNum st, AssignmentType at, bool r)
Constructor.
Definition float.hpp:173
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:256
bool testsearch
Whether to perform search test.
Definition float.hh:260
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:250
bool imp(void) const
Test whether implication as reification mode is supported.
Definition float.hpp:165
LinearCongruentialGenerator< 2147483647, 48271, 44488, 3399 > RandomGenerator
Default values for linear congruential generator.
Definition random.hpp:183
double FloatNum
Floating point number base type.
Definition float.hh:106
FloatRelType
Relation types for floats.
Definition float.hh:1075
@ FRT_EQ
Equality ( ).
Definition float.hh:1076
@ FRT_GR
Greater ( ).
Definition float.hh:1081
@ FRT_NQ
Disequality ( ).
Definition float.hh:1077
@ FRT_LE
Less ( ).
Definition float.hh:1079
@ FRT_LQ
Less or equal ( ).
Definition float.hh:1078
@ FRT_GQ
Greater or equal ( ).
Definition float.hh:1080
@ RM_IMP
Implication for reification.
Definition int.hh:896
@ RM_PMI
Inverse implication for reification.
Definition int.hh:903
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:889
Floating point numbers.
const int max
Largest allowed integer value.
Definition int.hh:120
Gecode toplevel namespace
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Testing domain floats.
Definition float.hh:48
AssignmentType
Assignment possible types.
Definition float.hh:61
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
General test support.
Definition assign.hh:52
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56