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
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(TestTags tags, const std::string& s, int a,
175 AssignmentType at, bool r)
176 : Base("Float::"+s, tags), 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, const Gecode::FloatVal& d,
185 bool r)
186 : Test(TestTags::standard(),s,a,d,st,at,r) {}
187
188 inline
189 Test::Test(TestTags tags, const std::string& s, int a,
191 Gecode::FloatNum st, AssignmentType at, bool r)
192 : Base("Float::"+s, tags), arity(a), dom(min,max), step(st),
194 rms((1 << Gecode::RM_EQV) |
195 (1 << Gecode::RM_IMP) |
196 (1 << Gecode::RM_PMI)),
197 testsearch(true), testfix(true), testsubsumed(true) {}
198
199 inline
200 Test::Test(const std::string& s, int a, Gecode::FloatNum min,
202 bool r)
203 : Test(TestTags::standard(),s,a,min,max,st,at,r) {}
204
205 inline
206 std::string
208 using namespace Gecode;
209 switch (frt) {
210 case FRT_EQ: return "Eq";
211 case FRT_NQ: return "Nq";
212 case FRT_LQ: return "Lq";
213 case FRT_LE: return "Le";
214 case FRT_GQ: return "Gq";
215 case FRT_GR: return "Gr";
216 default: ;
217 }
219 return "NONE";
220 }
221
222 inline
223 std::string
225 std::stringstream s;
226 s << f;
227 return s.str();
228 }
229
230 inline
231 std::string
233 std::stringstream s;
234 s << "[" << f.min() << ":" << f.max() << "]";
235 return s.str();
236 }
237
238 inline
239 std::string
241 std::string s = "";
242 for (int i=0; i<x.size()-1; i++)
243 s += str(x[i]) + ",";
244 return "[" + s + str(x[x.size()-1]) + "]";
245 }
246
247 inline MaybeType
249 using namespace Gecode;
250 switch (r) {
251 case FRT_EQ:
252 if (x == y) return MT_TRUE;
253 if (x != y) return MT_FALSE;
254 break;
255 case FRT_NQ:
256 if (x != y) return MT_TRUE;
257 if (x == y) return MT_FALSE;
258 break;
259 case FRT_LQ:
260 if (x <= y) return MT_TRUE;
261 if (x > y) return MT_FALSE;
262 break;
263 case FRT_LE:
264 if (x < y) return MT_TRUE;
265 if (x >= y) return MT_FALSE;
266 break;
267 case FRT_GQ:
268 if (x >= y) return MT_TRUE;
269 if (x < y) return MT_FALSE;
270 break;
271 case FRT_GR:
272 if (x > y) return MT_TRUE;
273 if (x <= y) return MT_FALSE;
274 break;
275 default: ;
276 }
277 return MT_MAYBE;
278 }
279
280 inline MaybeType
284
285 inline bool
287 return _rand(2U) == 0U;
288 }
289
290 inline MaybeType
291 operator &(MaybeType a, MaybeType b) {
292 switch (a) {
293 case MT_TRUE: return b;
294 case MT_FALSE: return MT_FALSE;
295 default: ;
296 }
297 return (b == MT_FALSE) ? MT_FALSE : MT_MAYBE;
298 }
299
300
301 inline
303 : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
304 inline void
306 i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
307 }
308 inline bool
310 return i>=0;
311 }
312 inline void
314 i--;
315 }
317 FloatRelTypes::frt(void) const {
318 return frts[i];
319 }
320
321}}
322
323// STATISTICS: test-float
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1606
Passing float arguments.
Definition float.hh:948
Float value type.
Definition float.hh:329
Floating point rounding policy that restores the caller's mode.
Definition float.hh:269
Gecode::Support::RandomGenerator _rand
Random number generator.
Definition test.hh:238
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
TestTags tags(void) const
Return tags for test.
Definition test.hpp:95
int n
Number of variables.
Definition float.hh:96
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:97
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:119
Gecode::FloatVal * dsv
Iterator for each variable.
Definition float.hh:118
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:139
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:140
const Test * curPb
Current problem used to complete assignment.
Definition float.hh:138
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:313
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition float.hpp:317
FloatRelTypes(void)
Initialize iterator.
Definition float.hpp:302
bool operator()(void) const
Test whether iterator is done.
Definition float.hpp:309
void reset(void)
Reset iterator.
Definition float.hpp:305
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:161
int a
How many assignments still to be generated Generate new value according to domain.
Definition float.hh:162
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
Supported reification modes, encoded as a bit mask of 1 << Gecode::RM_*.
Definition float.hh:272
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:281
static MaybeType cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y)
Compare x and y with respect to r.
Definition float.hpp:248
bool testsubsumed
Whether to test for subsumption.
Definition float.hh:278
bool testfix
Whether to perform fixpoint test.
Definition float.hh:276
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:161
int arity
Number of variables.
Definition float.hh:262
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:207
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:286
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:266
AssignmentType assignmentType
Gives the type of assignment to use.
Definition float.hh:268
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:183
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:270
bool testsearch
Whether to perform search test.
Definition float.hh:274
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:264
bool imp(void) const
Test whether implication as reification mode is supported.
Definition float.hpp:165
static std::string str(bool b)
Map bool to string.
Definition int.hpp:278
Set of test tags.
Definition test.hh:105
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:1070
@ FRT_EQ
Equality ( ).
Definition float.hh:1071
@ FRT_GR
Greater ( ).
Definition float.hh:1076
@ FRT_NQ
Disequality ( ).
Definition float.hh:1072
@ FRT_LE
Less ( ).
Definition float.hh:1074
@ FRT_LQ
Less or equal ( ).
Definition float.hh:1073
@ FRT_GQ
Greater or equal ( ).
Definition float.hh:1075
@ RM_IMP
Implication for reification.
Definition int.hh:1162
@ RM_PMI
Inverse implication for reification.
Definition int.hh:1169
@ RM_EQV
Equivalence for reification (default).
Definition int.hh:1155
Floating point numbers.
const int max
Largest allowed integer value.
Definition int.hh:121
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.cpp:43
AssignmentType
Assignment possible types.
Definition float.hh:75
MaybeType
Type for comparisons and solutions.
Definition float.hh:65
General test support.
Definition afc.cpp:41
Region r
Definition region.cpp:65
@ standard
Standard test suite.
Definition test.hh:100
#define forceinline
Definition config.hpp:144
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56