Generated on Thu Jan 16 2025 00:00:00 for Gecode by doxygen 1.14.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.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
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
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)),
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 }
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
271
272 inline bool
274 return Base::rand(2U) == 0U;
275 }
276
277 inline MaybeType
278 operator &(MaybeType a, MaybeType b) {
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 }
304 FloatRelTypes::frt(void) const {
305 return frts[i];
306 }
307
308}}
309
310// STATISTICS: test-float
311
Passing float arguments.
Definition float.hh:953
Float value type.
Definition float.hh:334
Floating point rounding policy.
Definition float.hh:154
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Base(const std::string &s)
Create and register test with name s.
Definition test.cpp:59
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 void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:73
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition float.hpp:64
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
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
virtual ~ExtAssignment(void)
Destructor.
Definition float.hpp:107
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:102
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:97
Gecode::FloatNum step
Step for next assignment.
Definition float.hh:126
const Test * curPb
Current problem used to complete assignment.
Definition float.hh:124
virtual bool operator()(void) const
Test whether all assignments have been iterated.
Definition float.hpp:93
void operator++(void)
Increment to next relation type.
Definition float.hpp:300
Gecode::FloatRelType frt(void) const
Return current relation type.
Definition float.hpp:304
FloatRelTypes(void)
Initialize iterator.
Definition float.hpp:289
bool operator()(void) const
Test whether iterator is done.
Definition float.hpp:296
void reset(void)
Reset iterator.
Definition float.hpp:292
RandomAssignment(int n, const Gecode::FloatVal &d, int a)
Initialize for a assignments for n variables and values d.
Definition float.hpp:130
virtual Gecode::FloatVal operator[](int i) const
Return value for variable i.
Definition float.hpp:141
virtual void set(int i, const Gecode::FloatVal &val)
Set assignment to value val for variable i.
Definition float.hpp:146
Gecode::FloatNum randval(void)
Definition float.hpp:112
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:137
int a
How many assigments still to be generated Generate new value according to domain.
Definition float.hh:147
virtual ~RandomAssignment(void)
Destructor.
Definition float.hpp:151
int rms
Which reification modes are supported.
Definition float.hh:257
static MaybeType eq(Gecode::FloatVal x, Gecode::FloatVal y)
Whether x and y are equal.
Definition float.hpp:268
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
bool testfix
Whether to perform fixpoint test.
Definition float.hh:261
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:160
int arity
Number of variables.
Definition float.hh:247
static std::string str(Gecode::FloatRelType frt)
Map float relation to string.
Definition float.hpp:194
bool flip(void)
Flip a coin and return true or false randomly.
Definition float.hpp:273
Gecode::FloatNum step
Step for going to next solution.
Definition float.hh:251
bool pmi(void) const
Test whether reverse implication as reification mode is supported.
Definition float.hpp:168
Test(const std::string &s, int a, const Gecode::FloatVal &d, Gecode::FloatNum st, AssignmentType at, bool r)
Constructor.
Definition float.hpp:172
bool reified
Does the constraint also exist as reified constraint.
Definition float.hh:255
bool testsearch
Whether to perform search test.
Definition float.hh:259
AssignmentType assigmentType
Gives the type of assignment to use.
Definition float.hh:253
Gecode::FloatVal dom
Domain of variables.
Definition float.hh:249
bool imp(void) const
Test whether implication as reification mode is supported.
Definition float.hpp:164
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
double FloatNum
Floating point number base type.
Definition float.hh:106
FloatRelType
Relation types for floats.
Definition float.hh:1074
@ FRT_EQ
Equality ( )
Definition float.hh:1075
@ FRT_GR
Greater ( )
Definition float.hh:1080
@ FRT_NQ
Disequality ( )
Definition float.hh:1076
@ FRT_LE
Less ( )
Definition float.hh:1078
@ FRT_LQ
Less or equal ( )
Definition float.hh:1077
@ FRT_GQ
Greater or equal ( )
Definition float.hh:1079
@ RM_IMP
Implication for reification.
Definition int.hh:877
@ RM_PMI
Inverse implication for reification.
Definition int.hh:884
@ RM_EQV
Equivalence for reification (default)
Definition int.hh:870
Floating point numbers.
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode toplevel namespace
void min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar SetOpType SetVar y
Definition set.hh:773
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Post propagator for SetVar x
Definition set.hh:773
Testing domain floats.
Definition float.cpp:43
AssignmentType
Assignment possible types.
Definition float.hh:61
MaybeType
Type for comparisons and solutions.
Definition float.hh:51
General test support.
Definition afc.cpp:39
Region r
Definition region.cpp:65
#define forceinline
Definition config.hpp:194
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56