this repo has no description
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
45 Assignment::Assignment(int n0, const Gecode::FloatVal& d0)
46 : n(n0), d(d0) {}
47 inline int
48 Assignment::size(void) const {
49 return n;
50 }
51 inline
52 Assignment::~Assignment(void) {}
53
54 inline
55 CpltAssignment::CpltAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s)
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
64 CpltAssignment::has_more() const {
65 return dsv[0].min() <= d.max();
66 }
67 inline Gecode::FloatVal
68 CpltAssignment::operator[](int i) const {
69 assert((i>=0) && (i<n));
70 return dsv[i];
71 }
72 inline void
73 CpltAssignment::set(int i, const Gecode::FloatVal& val) {
74 assert((i>=0) && (i<n));
75 dsv[i] = val;
76 }
77 inline
78 CpltAssignment::~CpltAssignment(void) {
79 delete [] dsv;
80 }
81
82 inline
83 ExtAssignment::ExtAssignment(int n, const Gecode::FloatVal& d, Gecode::FloatNum s, const Test* pb,
84 Gecode::Support::RandomGenerator& rand)
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
94 ExtAssignment::has_more() const {
95 return dsv[0].min() <= d.max();
96 }
97 inline Gecode::FloatVal
98 ExtAssignment::operator[](int i) const {
99 assert((i>=0) && (i<n));
100 return dsv[i];
101 }
102 inline void
103 ExtAssignment::set(int i, const Gecode::FloatVal& val) {
104 assert((i>=0) && (i<n));
105 dsv[i] = val;
106 }
107 inline
108 ExtAssignment::~ExtAssignment(void) {
109 delete [] dsv;
110 }
111
112 forceinline Gecode::FloatNum
113 RandomAssignment::randval(Gecode::Support::RandomGenerator& rand) {
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)),
123 static_cast<FloatNum>(Gecode::Int::Limits::max)
124 ),
125 r.sub_down(d.max(),d.min())
126 )
127 );
128 }
129
130 inline
131 RandomAssignment::RandomAssignment(int n, const Gecode::FloatVal& d, int a0, Gecode::Support::RandomGenerator& rand)
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
138 RandomAssignment::has_more() const {
139 return a>0;
140 }
141 inline Gecode::FloatVal
142 RandomAssignment::operator[](int i) const {
143 assert((i>=0) && (i<n));
144 return vals[i];
145 }
146 inline void
147 RandomAssignment::set(int i, const Gecode::FloatVal& val) {
148 assert((i>=0) && (i<n));
149 vals[i] = val;
150 }
151 inline
152 RandomAssignment::~RandomAssignment(void) {
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,
174 Gecode::FloatNum st, AssignmentType at,
175 bool r)
176 : Base("Float::"+s), arity(a), dom(d), step(st), assigmentType(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,
184 Gecode::FloatNum max, Gecode::FloatNum st, AssignmentType at,
185 bool r)
186 : Base("Float::"+s), arity(a), dom(min,max), step(st),
187 assigmentType(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
195 Test::str(Gecode::FloatRelType frt) {
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 }
206 GECODE_NEVER;
207 return "NONE";
208 }
209
210 inline
211 std::string
212 Test::str(Gecode::FloatNum f) {
213 std::stringstream s;
214 s << f;
215 return s.str();
216 }
217
218 inline
219 std::string
220 Test::str(Gecode::FloatVal f) {
221 std::stringstream s;
222 s << "[" << f.min() << ":" << f.max() << "]";
223 return s.str();
224 }
225
226 inline
227 std::string
228 Test::str(const Gecode::FloatValArgs& x) {
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
236 Test::cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y) {
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
269 Test::eq(Gecode::FloatVal x, Gecode::FloatVal y) {
270 return cmp(x, Gecode::FRT_EQ, y);
271 }
272
273 inline bool
274 Test::flip(void) {
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
290 FloatRelTypes::FloatRelTypes(void)
291 : i(sizeof(frts)/sizeof(Gecode::FloatRelType)-1) {}
292 inline void
293 FloatRelTypes::reset(void) {
294 i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
295 }
296 inline bool
297 FloatRelTypes::operator()(void) const {
298 return i>=0;
299 }
300 inline void
301 FloatRelTypes::operator++(void) {
302 i--;
303 }
304 inline Gecode::FloatRelType
305 FloatRelTypes::frt(void) const {
306 return frts[i];
307 }
308
309}}
310
311// STATISTICS: test-float
312