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::operator()(void) 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 : 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
93 ExtAssignment::operator()(void) const {
94 return dsv[0].min() <= d.max();
95 }
96 inline Gecode::FloatVal
97 ExtAssignment::operator[](int i) const {
98 assert((i>=0) && (i<n));
99 return dsv[i];
100 }
101 inline void
102 ExtAssignment::set(int i, const Gecode::FloatVal& val) {
103 assert((i>=0) && (i<n));
104 dsv[i] = val;
105 }
106 inline
107 ExtAssignment::~ExtAssignment(void) {
108 delete [] dsv;
109 }
110
111 forceinline Gecode::FloatNum
112 RandomAssignment::randval(void) {
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)),
122 static_cast<FloatNum>(Gecode::Int::Limits::max)
123 ),
124 r.sub_down(d.max(),d.min())
125 )
126 );
127 }
128
129 inline
130 RandomAssignment::RandomAssignment(int n, const Gecode::FloatVal& d, int a0)
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
137 RandomAssignment::operator()(void) const {
138 return a>0;
139 }
140 inline Gecode::FloatVal
141 RandomAssignment::operator[](int i) const {
142 assert((i>=0) && (i<n));
143 return vals[i];
144 }
145 inline void
146 RandomAssignment::set(int i, const Gecode::FloatVal& val) {
147 assert((i>=0) && (i<n));
148 vals[i] = val;
149 }
150 inline
151 RandomAssignment::~RandomAssignment(void) {
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,
173 Gecode::FloatNum st, AssignmentType at,
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,
183 Gecode::FloatNum max, Gecode::FloatNum st, AssignmentType at,
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 }
205 GECODE_NEVER;
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
235 Test::cmp(Gecode::FloatVal x, Gecode::FloatRelType r, Gecode::FloatVal y) {
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
268 Test::eq(Gecode::FloatVal x, Gecode::FloatVal y) {
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
292 FloatRelTypes::reset(void) {
293 i = sizeof(frts)/sizeof(Gecode::FloatRelType)-1;
294 }
295 inline bool
296 FloatRelTypes::operator()(void) const {
297 return i>=0;
298 }
299 inline void
300 FloatRelTypes::operator++(void) {
301 i--;
302 }
303 inline Gecode::FloatRelType
304 FloatRelTypes::frt(void) const {
305 return frts[i];
306 }
307
308}}
309
310// STATISTICS: test-float
311