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::has_more(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(Gecode::Support::RandomGenerator& rand) {
77 unsigned int skip = 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, Gecode::Support::RandomGenerator& rand)
89 : Assignment(n,d), vals(new int[static_cast<size_t>(n)]), a(a0) {
90 for (int i=n; i--; )
91 vals[i] = randval(rand);
92 }
93
94 inline bool
95 RandomAssignment::has_more(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, Gecode::Support::RandomGenerator& rand) {
110 unsigned int skip = 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, int n1, const Gecode::IntSet& d1, int a0,
122 Gecode::Support::RandomGenerator& rand)
123 : Assignment(n0+n1,d0),vals(new int[static_cast<size_t>(n0+n1)]),
124 a(a0),_n1(n1),_d1(d1) {
125 for (int i=n0; i--; )
126 vals[i] = randval(d, rand);
127 for (int i=n1; i--; )
128 vals[n0+i] = randval(_d1, rand);
129 }
130
131 inline bool
132 RandomMixAssignment::has_more(void) const {
133 return a>0;
134 }
135
136 inline int
137 RandomMixAssignment::operator[](int i) const {
138 assert((i>=0) && (i<n));
139 return vals[i];
140 }
141
142 inline
143 RandomMixAssignment::~RandomMixAssignment(void) {
144 delete [] vals;
145 }
146
147 /*
148 * Tests with integer constraints
149 *
150 */
151 forceinline bool
152 Test::eqv(void) const {
153 return reified && ((rms & (1 << Gecode::RM_EQV)) != 0);
154 }
155 forceinline bool
156 Test::imp(void) const {
157 return reified && ((rms & (1 << Gecode::RM_IMP)) != 0);
158 }
159 forceinline bool
160 Test::pmi(void) const {
161 return reified && ((rms & (1 << Gecode::RM_PMI)) != 0);
162 }
163 inline
164 Test::Test(const std::string& p, const std::string& s,
165 int a, const Gecode::IntSet& d, bool r,
166 Gecode::IntPropLevel i)
167 : Base(p+s), arity(a), dom(d),
168 reified(r), rms((1 << Gecode::RM_EQV) |
169 (1 << Gecode::RM_IMP) |
170 (1 << Gecode::RM_PMI)),
171 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
172 testsearch(true), testfix(true) {}
173
174 inline
175 Test::Test(const std::string& s,
176 int a, const Gecode::IntSet& d, bool r,
177 Gecode::IntPropLevel i)
178 : Base("Int::"+s), arity(a), dom(d),
179 reified(r), rms((1 << Gecode::RM_EQV) |
180 (1 << Gecode::RM_IMP) |
181 (1 << Gecode::RM_PMI)),
182 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
183 testsearch(true), testfix(true) {}
184
185 inline
186 Test::Test(const std::string& p, const std::string& s,
187 int a, int min, int max, bool r,
188 Gecode::IntPropLevel i)
189 : Base(p+s), arity(a), dom(min,max),
190 reified(r), rms((1 << Gecode::RM_EQV) |
191 (1 << Gecode::RM_IMP) |
192 (1 << Gecode::RM_PMI)),
193 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
194 testsearch(true), testfix(true) {}
195
196 inline
197 Test::Test(const std::string& s,
198 int a, int min, int max, bool r, Gecode::IntPropLevel i)
199 : Base("Int::"+s), arity(a), dom(min,max),
200 reified(r), rms((1 << Gecode::RM_EQV) |
201 (1 << Gecode::RM_IMP) |
202 (1 << Gecode::RM_PMI)),
203 ipl(i), contest(ipl == Gecode::IPL_DOM ? CTL_DOMAIN : CTL_NONE),
204 testsearch(true), testfix(true) {}
205
206 inline
207 std::string
208 Test::str(Gecode::IntPropLevel ipl) {
209 using namespace Gecode;
210 std::stringstream s;
211 switch (vbd(ipl)) {
212 case IPL_VAL: s << "Val"; break;
213 case IPL_BND: s << "Bnd"; break;
214 case IPL_DOM: s << "Dom"; break;
215 default: s << "Def"; break;
216 }
217 if (ipl & IPL_BASIC) s << "+B";
218 if (ipl & IPL_ADVANCED) s << "+A";
219 return s.str();
220 }
221
222 inline
223 std::string
224 Test::str(Gecode::IntRelType irt) {
225 using namespace Gecode;
226 switch (irt) {
227 case IRT_LQ: return "Lq";
228 case IRT_LE: return "Le";
229 case IRT_GQ: return "Gq";
230 case IRT_GR: return "Gr";
231 case IRT_EQ: return "Eq";
232 case IRT_NQ: return "Nq";
233 default: ;
234 }
235 GECODE_NEVER;
236 return "NONE";
237 }
238
239 inline std::string
240 Test::str(Gecode::BoolOpType bot) {
241 using namespace Gecode;
242 switch (bot) {
243 case BOT_AND: return "And";
244 case BOT_OR: return "Or";
245 case BOT_IMP: return "Imp";
246 case BOT_EQV: return "Eqv";
247 case BOT_XOR: return "Xor";
248 default: GECODE_NEVER;
249 }
250 GECODE_NEVER;
251 return "NONE";
252 }
253
254 inline std::string
255 Test::str(bool b) {
256 return Base::str(b);
257 }
258
259 inline std::string
260 Test::str(int i) {
261 return Base::str(i);
262 }
263
264 inline std::string
265 Test::str(const Gecode::IntArgs& x) {
266 return Base::str(x);
267 }
268
269
270
271 template<class T>
272 inline bool
273 Test::cmp(T x, Gecode::IntRelType r, T y) {
274 using namespace Gecode;
275 switch (r) {
276 case IRT_EQ: return x == y;
277 case IRT_NQ: return x != y;
278 case IRT_LQ: return x <= y;
279 case IRT_LE: return x < y;
280 case IRT_GR: return x > y;
281 case IRT_GQ: return x >= y;
282 default: ;
283 }
284 return false;
285 }
286
287
288
289 inline
290 IntPropLevels::IntPropLevels(void)
291 : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
292 inline bool
293 IntPropLevels::operator()(void) const {
294 return i>=0;
295 }
296 inline void
297 IntPropLevels::operator++(void) {
298 i--;
299 }
300 inline Gecode::IntPropLevel
301 IntPropLevels::ipl(void) const {
302 return ipls[i];
303 }
304
305
306 inline
307 IntPropBasicAdvanced::IntPropBasicAdvanced(void)
308 : i(sizeof(ipls)/sizeof(Gecode::IntPropLevel)-1) {}
309 inline bool
310 IntPropBasicAdvanced::operator()(void) const {
311 return i>=0;
312 }
313 inline void
314 IntPropBasicAdvanced::operator++(void) {
315 i--;
316 }
317 inline Gecode::IntPropLevel
318 IntPropBasicAdvanced::ipl(void) const {
319 return ipls[i];
320 }
321
322
323 inline
324 IntRelTypes::IntRelTypes(void)
325 : i(sizeof(irts)/sizeof(Gecode::IntRelType)-1) {}
326 inline void
327 IntRelTypes::reset(void) {
328 i = sizeof(irts)/sizeof(Gecode::IntRelType)-1;
329 }
330 inline bool
331 IntRelTypes::operator()(void) const {
332 return i>=0;
333 }
334 inline void
335 IntRelTypes::operator++(void) {
336 i--;
337 }
338 inline Gecode::IntRelType
339 IntRelTypes::irt(void) const {
340 return irts[i];
341 }
342
343 inline
344 BoolOpTypes::BoolOpTypes(void)
345 : i(sizeof(bots)/sizeof(Gecode::BoolOpType)-1) {}
346 inline bool
347 BoolOpTypes::operator()(void) const {
348 return i>=0;
349 }
350 inline void
351 BoolOpTypes::operator++(void) {
352 i--;
353 }
354 inline Gecode::BoolOpType
355 BoolOpTypes::bot(void) const {
356 return bots[i];
357 }
358
359}}
360
361// STATISTICS: test-int
362