Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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
194 Test::str(Gecode::FloatRelType frt) {
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
211 Test::str(Gecode::FloatNum f) {
212 std::stringstream s;
213 s << f;
214 return s.str();
215 }
216
217 inline
218 std::string
219 Test::str(Gecode::FloatVal f) {
220 std::stringstream s;
221 s << "[" << f.min() << ":" << f.max() << "]";
222 return s.str();
223 }
224
225 inline
226 std::string
227 Test::str(const Gecode::FloatValArgs& x) {
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
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
289 FloatRelTypes::FloatRelTypes(void)
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
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int n
Number of negative literals for node type.
struct Gecode::@603::NNF::@65::@67 a
For atomic nodes.
Node * x
Pointer to corresponding Boolean expression node.
NNF * r
Right subtree.
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Passing float arguments.
Definition float.hh:950
Float value type.
Definition float.hh:334
friend FloatVal max(const FloatVal &x, const FloatVal &y)
Definition val.hpp:386
friend FloatVal min(const FloatVal &x, const FloatVal &y)
Definition val.hpp:398
Floating point rounding policy.
Definition float.hh:154
Base class for all tests to be run
Definition test.hh:103
static Gecode::Support::RandomGenerator rand
Random number generator.
Definition test.hh:134
Base class for assignments
Definition float.hh:80
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::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
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
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
bool eqv(void) const
Test whether equivalence as reification mode is supported.
Definition float.hpp:160
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:1068
@ FRT_EQ
Equality ( )
Definition float.hh:1069
@ RM_IMP
Implication for reification.
Definition int.hh:862
@ RM_PMI
Inverse implication for reification.
Definition int.hh:869
@ RM_EQV
Equivalence for reification (default)
Definition int.hh:855
Floating point numbers.
const int max
Largest allowed integer value.
Definition int.hh:116
Gecode toplevel namespace
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:187
#define GECODE_NEVER
Assert that this command is never executed.
Definition macros.hpp:56