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 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Mikael Lagerkvist, 2006
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.org
14 *
15 * Permission is hereby granted, free of charge, to any person obtaining
16 * a copy of this software and associated documentation files (the
17 * "Software"), to deal in the Software without restriction, including
18 * without limitation the rights to use, copy, modify, merge, publish,
19 * distribute, sublicense, and/or sell copies of the Software, and to
20 * permit persons to whom the Software is furnished to do so, subject to
21 * the following conditions:
22 *
23 * The above copyright notice and this permission notice shall be
24 * included in all copies or substantial portions of the Software.
25 *
26 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 *
34 */
35
36namespace Test { namespace Int {
37
38 /*
39 * Assignments
40 *
41 */
42 inline
43 Assignment::Assignment(int n0, const Gecode::IntSet& d0)
44 : n(n0), d(d0) {}
45 inline int
46 Assignment::size(void) const {
47 return n;
48 }
49 inline
50 Assignment::~Assignment(void) {}
51
52
53 inline
54 CpltAssignment::CpltAssignment(int n, const Gecode::IntSet& d)
55 : Assignment(n,d),
56 dsv(new Gecode::IntSetValues[static_cast<unsigned int>(n)]) {
57 for (int i=n; i--; )
58 dsv[i].init(d);
59 }
60 inline bool
61 CpltAssignment::operator()(void) const {
62 return dsv[0]();
63 }
64 inline int
65 CpltAssignment::operator[](int i) const {
66 assert((i>=0) && (i<n));
67 return dsv[i].val();
68 }
69 inline
70 CpltAssignment::~CpltAssignment(void) {
71 delete [] dsv;
72 }
73
74
75 forceinline int
76 RandomAssignment::randval(void) {
77 unsigned int skip = Base::rand(d.size());
78 for (Gecode::IntSetRanges it(d); true; ++it) {
79 if (it.width() > skip)
80 return it.min() + static_cast<int>(skip);
81 skip -= it.width();
82 }
83 GECODE_NEVER;
84 return 0;
85 }
86
87 inline
88 RandomAssignment::RandomAssignment(int n, const Gecode::IntSet& d, int a0)
89 : Assignment(n,d), vals(new int[static_cast<size_t>(n)]), a(a0) {
90 for (int i=n; i--; )
91 vals[i] = randval();
92 }
93
94 inline bool
95 RandomAssignment::operator()(void) const {
96 return a>0;
97 }
98 inline int
99 RandomAssignment::operator[](int i) const {
100 assert((i>=0) && (i<n));
101 return vals[i];
102 }
103 inline
104 RandomAssignment::~RandomAssignment(void) {
105 delete [] vals;
106 }
107
108 forceinline int
109 RandomMixAssignment::randval(const Gecode::IntSet& d) {
110 unsigned int skip = Base::rand(d.size());
111 for (Gecode::IntSetRanges it(d); true; ++it) {
112 if (it.width() > skip)
113 return it.min() + static_cast<int>(skip);
114 skip -= it.width();
115 }
116 GECODE_NEVER;
117 return 0;
118 }
119
120 inline
121 RandomMixAssignment::RandomMixAssignment(int n0, const Gecode::IntSet& d0,
122 int n1, const Gecode::IntSet& d1,
123 int a0)
124 : Assignment(n0+n1,d0),vals(new int[static_cast<size_t>(n0+n1)]),
125 a(a0),_n1(n1),_d1(d1) {
126 for (int i=n0; i--; )
127 vals[i] = randval(d);
128 for (int i=n1; i--; )
129 vals[n0+i] = randval(_d1);
130 }
131
132 inline bool
133 RandomMixAssignment::operator()(void) const {
134 return a>0;
135 }
136
137 inline int
138 RandomMixAssignment::operator[](int i) const {
139 assert((i>=0) && (i<n));
140 return vals[i];
141 }
142
143 inline
144 RandomMixAssignment::~RandomMixAssignment(void) {
145 delete [] vals;
146 }
147
148 /*
149 * Tests with integer constraints
150 *
151 */
152 forceinline bool
153 Test::eqv(void) const {
154 return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
155 }
156 forceinline bool
157 Test::imp(void) const {
158 return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
159 }
160 forceinline bool
161 Test::pmi(void) const {
162 return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
163 }
164 inline
165 Test::Test(const std::string& p, const std::string& s,
166 int a, const Gecode::IntSet& d, bool r,
167 Gecode::IntPropLevel i)
168 : Base(p+s), arity(a), dom(d),
169 reified(r), rms((1 << Gecode::RM_EQV) |
170 (1 << Gecode::RM_IMP) |
171 (1 << Gecode::RM_PMI)),
172 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
173 testsearch(true), testfix(true) {}
174
175 inline
176 Test::Test(const std::string& s,
177 int a, const Gecode::IntSet& d, bool r,
178 Gecode::IntPropLevel i)
179 : Base("Int::"+s), arity(a), dom(d),
180 reified(r), rms((1 << Gecode::RM_EQV) |
181 (1 << Gecode::RM_IMP) |
182 (1 << Gecode::RM_PMI)),
183 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
184 testsearch(true), testfix(true) {}
185
186 inline
187 Test::Test(const std::string& p, const std::string& s,
188 int a, int min, int max, bool r,
189 Gecode::IntPropLevel i)
190 : Base(p+s), arity(a), dom(min,max),
191 reified(r), rms((1 << Gecode::RM_EQV) |
192 (1 << Gecode::RM_IMP) |
193 (1 << Gecode::RM_PMI)),
194 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
195 testsearch(true), testfix(true) {}
196
197 inline
198 Test::Test(const std::string& s,
199 int a, int min, int max, bool r, Gecode::IntPropLevel i)
200 : Base("Int::"+s), arity(a), dom(min,max),
201 reified(r), rms((1 << Gecode::RM_EQV) |
202 (1 << Gecode::RM_IMP) |
203 (1 << Gecode::RM_PMI)),
204 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
205 testsearch(true), testfix(true) {}
206
207 inline
208 std::string
209 Test::str(Gecode::IntPropLevel ipl) {
210 using namespace Gecode;
211 std::stringstream s;
212 switch (vbd(ipl)) {
213 case IPL_VAL: s << "Val"; break;
214 case IPL_BND: s << "Bnd"; break;
215 case IPL_DOM: s << "Dom"; break;
216 default: s << "Def"; break;
217 }
218 if (ipl & IPL_BASIC) s << "+B";
219 if (ipl & IPL_ADVANCED) s << "+A";
220 return s.str();
221 }
222
223 inline
224 std::string
225 Test::str(Gecode::IntRelType irt) {
226 using namespace Gecode;
227 switch (irt) {
228 case IRT_LQ: return "Lq";
229 case IRT_LE: return "Le";
230 case IRT_GQ: return "Gq";
231 case IRT_GR: return "Gr";
232 case IRT_EQ: return "Eq";
233 case IRT_NQ: return "Nq";
234 default: ;
235 }
236 GECODE_NEVER;
237 return "NONE";
238 }
239
240 inline std::string
241 Test::str(Gecode::BoolOpType bot) {
242 using namespace Gecode;
243 switch (bot) {
244 case BOT_AND: return "And";
245 case BOT_OR: return "Or";
246 case BOT_IMP: return "Imp";
247 case BOT_EQV: return "Eqv";
248 case BOT_XOR: return "Xor";
249 default: GECODE_NEVER;
250 }
251 GECODE_NEVER;
252 return "NONE";
253 }
254
255 inline std::string
256 Test::str(bool b) {
257 return Base::str(b);
258 }
259
260 inline std::string
261 Test::str(int i) {
262 return Base::str(i);
263 }
264
265 inline std::string
266 Test::str(const Gecode::IntArgs& x) {
267 return Base::str(x);
268 }
269
270
271
272 template<class T>
273 inline bool
274 Test::cmp(T x, Gecode::IntRelType r, T y) {
275 using namespace Gecode;
276 switch (r) {
277 case IRT_EQ: return x == y;
278 case IRT_NQ: return x != y;
279 case IRT_LQ: return x <= y;
280 case IRT_LE: return x < y;
281 case IRT_GR: return x > y;
282 case IRT_GQ: return x >= y;
283 default: ;
284 }
285 return false;
286 }
287
288
289
290 inline
291 IntPropLevels::IntPropLevels(void)
292 : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
293 inline bool
294 IntPropLevels::operator()(void) const {
295 return i>=0;
296 }
297 inline void
298 IntPropLevels::operator++(void) {
299 i--;
300 }
301 inline Gecode::IntPropLevel
302 IntPropLevels::ipl(void) const {
303 return ipls[i];
304 }
305
306
307 inline
308 IntPropBasicAdvanced::IntPropBasicAdvanced(void)
309 : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
310 inline bool
311 IntPropBasicAdvanced::operator()(void) const {
312 return i>=0;
313 }
314 inline void
315 IntPropBasicAdvanced::operator++(void) {
316 i--;
317 }
318 inline Gecode::IntPropLevel
319 IntPropBasicAdvanced::ipl(void) const {
320 return ipls[i];
321 }
322
323
324 inline
325 IntRelTypes::IntRelTypes(void)
326 : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
327 inline void
328 IntRelTypes::reset(void) {
329 i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
330 }
331 inline bool
332 IntRelTypes::operator()(void) const {
333 return i>=0;
334 }
335 inline void
336 IntRelTypes::operator++(void) {
337 i--;
338 }
339 inline Gecode::IntRelType
340 IntRelTypes::irt(void) const {
341 return irts[i];
342 }
343
344 inline
345 BoolOpTypes::BoolOpTypes(void)
346 : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
347 inline bool
348 BoolOpTypes::operator()(void) const {
349 return i>=0;
350 }
351 inline void
352 BoolOpTypes::operator++(void) {
353 i--;
354 }
355 inline Gecode::BoolOpType
356 BoolOpTypes::bot(void) const {
357 return bots[i];
358 }
359
360}}
361
362// STATISTICS: test-int
363