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
36#include "test/int.hh"
37#include <gecode/minimodel.hh>
38
39namespace Test { namespace Int {
40
41 /// %Tests for distinct constraints
42 namespace Distinct {
43
44 /**
45 * \defgroup TaskTestIntDistinct Distinct constraints
46 * \ingroup TaskTestInt
47 */
48 //@{
49 /// Simple test for distinct constraint
50 template<bool useCount>
51 class Distinct : public Test {
52 public:
53 /// Create and register test
54 Distinct(const Gecode::IntSet& d0, Gecode::IntPropLevel ipl,
55 int n=6)
56 : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
57 str(ipl)+"::Sparse::"+str(n),n,d0,false,ipl) {}
58 /// Create and register test
59 Distinct(int min, int max, Gecode::IntPropLevel ipl)
60 : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
61 str(ipl)+"::Dense",6,min,max,false,ipl) {}
62 /// Check whether \a x is solution
63 virtual bool solution(const Assignment& x) const {
64 for (int i=0; i<x.size(); i++)
65 for (int j=i+1; j<x.size(); j++)
66 if (x[i]==x[j])
67 return false;
68 return true;
69 }
70 /// Post constraint on \a x
71 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
72 if (!useCount) {
73 Gecode::distinct(home, x, ipl);
74 } else {
75 Gecode::IntSetRanges dr(dom);
76 int i = 0;
77 Gecode::IntArgs ia(Gecode::Iter::Ranges::size(dr));
78 for (Gecode::IntSetValues dr2(dom); dr2(); ++dr2)
79 ia[i++] = dr2.val();
80 Gecode::count(home, x, Gecode::IntSet(0,1), ia, ipl);
81 }
82 }
83 };
84
85 /// Simple test for distinct constraint with offsets
86 class Offset : public Test {
87 public:
88 /// Create and register test
89 Offset(const Gecode::IntSet& d, Gecode::IntPropLevel ipl)
90 : Test("Distinct::Offset::Sparse::"+str(ipl),6,d,false,ipl) {}
91 /// Create and register test
92 Offset(int min, int max, Gecode::IntPropLevel ipl)
93 : Test("Distinct::Offset::Dense::"+str(ipl),6,min,max,false,ipl) {}
94 /// Check whether \a x is solution
95 virtual bool solution(const Assignment& x) const {
96 for (int i=0; i<x.size(); i++)
97 for (int j=i+1; j<x.size(); j++)
98 if (x[i]+i==x[j]+j)
99 return false;
100 return true;
101 }
102 /// Post constraint on \a x
103 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
104 Gecode::IntArgs c(x.size());
105 for (int i=0; i<x.size(); i++)
106 c[i]=i;
107 Gecode::distinct(home, c, x, ipl);
108 }
109 };
110
111 /// Simple test for optional distinct constraint
112 class Optional : public Test {
113 public:
114 /// Create and register test
115 Optional(const Gecode::IntArgs& d, Gecode::IntPropLevel ipl)
116 : Test("Distinct::Optional::"+str(ipl)+"::"+str(d),
117 6,Gecode::IntSet(d),false,ipl) {}
118 /// Check whether \a x is solution
119 virtual bool solution(const Assignment& x) const {
120 int n = x.size() / 2;
121 for (int i=0; i<n; i++)
122 if ((x[i] < 0) || (x[i] > 1))
123 return false;
124 for (int i=0; i<n; i++)
125 for (int j=i+1; j<n; j++)
126 if ((x[i] == 1) && (x[j] == 1) && (x[n+i] == x[n+j]))
127 return false;
128 return true;
129 }
130 /// Post constraint on \a bx
131 virtual void post(Gecode::Space& home, Gecode::IntVarArray& bx) {
132 int n = bx.size() / 2;
133 Gecode::BoolVarArgs b(n);
134 Gecode::IntVarArgs x(n);
135 for (int i=0; i<n; i++) {
136 b[i] = Gecode::channel(home, bx[i]);
137 x[i] = bx[n+i];
138 }
139 Gecode::distinct(home, b, x, ipl);
140 }
141 };
142
143 /// Simple test for distinct except constant constraint
144 class Except : public Test {
145 public:
146 /// Create and register test
147 Except(const Gecode::IntArgs& d, Gecode::IntPropLevel ipl)
148 : Test("Distinct::Except::"+str(ipl)+"::"+str(d),
149 3,Gecode::IntSet(d),false,ipl) {
150 contest = CTL_NONE;
151 }
152 /// Check whether \a x is solution
153 virtual bool solution(const Assignment& x) const {
154 for (int i=0; i<x.size(); i++)
155 for (int j=i+1; j<x.size(); j++)
156 if ((x[i] != 0) && (x[j] != 0) && (x[i] == x[j]))
157 return false;
158 return true;
159 }
160 /// Post constraint on \a bx
161 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
162 Gecode::distinct(home, x, 0, ipl);
163 }
164 };
165
166 /// Randomized test for distinct constraint
167 class Random : public Test {
168 public:
169 /// Create and register test
170 Random(int n, int min, int max, Gecode::IntPropLevel ipl)
171 : Test("Distinct::Random::"+str(ipl),n,min,max,false,ipl) {
172 testsearch = false;
173 }
174 /// Create and register initial assignment
175 virtual Assignment* assignment(void) const {
176 return new RandomAssignment(arity, dom, 100, _rand);
177 }
178 /// Check whether \a x is solution
179 virtual bool solution(const Assignment& x) const {
180 for (int i=0; i<x.size(); i++)
181 for (int j=i+1; j<x.size(); j++)
182 if (x[i]==x[j])
183 return false;
184 return true;
185 }
186 /// Post constraint on \a x
187 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
188 Gecode::distinct(home, x, ipl);
189 }
190 };
191
192 /// %Testing pathological cases
193 class Pathological : public Base {
194 protected:
195 /// Number of variables
196 int n;
197 /// Consistency level
198 Gecode::IntPropLevel ipl;
199 /// %Test space
200 class TestSpace : public Gecode::Space {
201 public:
202 /// Constructor
203 TestSpace(void) {}
204 /// Constructor for cloning \a s
205 TestSpace(TestSpace& s)
206 : Gecode::Space(s) {}
207 /// Copy space during cloning
208 virtual Gecode::Space* copy(void) {
209 return new TestSpace(*this);
210 }
211 };
212 public:
213 /// Create and register test
214 Pathological(int n0, Gecode::IntPropLevel ipl0)
215 : Base("Int::Distinct::Pathological::"+
216 Test::str(n0)+"::"+Test::str(ipl0)), n(n0), ipl(ipl0) {}
217 /// Perform test
218 virtual bool run(void) {
219 using namespace Gecode;
220 {
221 TestSpace* s = new TestSpace;
222 IntVarArgs x(n);
223 for (int i=0; i<n; i++)
224 x[i] = IntVar(*s,0,i);
225 distinct(*s,x,ipl);
226 if (s->status() == SS_FAILED) {
227 delete s; return false;
228 }
229 for (int i=0; i<n; i++)
230 if (!x[i].assigned() || (x[i].val() != i)) {
231 delete s; return false;
232 }
233 delete s;
234 }
235 {
236 TestSpace* s = new TestSpace;
237 IntVarArgs x(2*n);
238 for (int i=0; i<n; i++) {
239 int v[] = {0,i};
240 IntSet d(v,2);
241 x[i] = IntVar(*s,d);
242 }
243 for (int i=n; i<2*n; i++)
244 x[i] = IntVar(*s,n-1,i);
245 distinct(*s,x,ipl);
246 if (s->status() == SS_FAILED) {
247 delete s; return false;
248 }
249 for (int i=0; i<n; i++)
250 if (!x[i].assigned() || (x[i].val() != i)) {
251 delete s; return false;
252 }
253 delete s;
254 }
255 return true;
256 }
257 };
258
259 const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
260 Gecode::IntSet d(v,7);
261 const int vl[6] = {Gecode::Int::Limits::min+0,
262 Gecode::Int::Limits::min+1,
263 Gecode::Int::Limits::min+2,
264 Gecode::Int::Limits::max-2,
265 Gecode::Int::Limits::max-1,
266 Gecode::Int::Limits::max-0};
267 Gecode::IntSet dl(vl,6);
268
269 Distinct<false> dom_d(-3,3,Gecode::IPL_DOM);
270 Distinct<false> bnd_d(-3,3,Gecode::IPL_BND);
271 Distinct<false> val_d(-3,3,Gecode::IPL_VAL);
272 Distinct<false> dom_s(d,Gecode::IPL_DOM);
273 Distinct<false> bnd_s(d,Gecode::IPL_BND);
274 Distinct<false> val_s(d,Gecode::IPL_VAL);
275
276 Distinct<false> dom_l(dl,Gecode::IPL_DOM,5);
277 Distinct<false> bnd_l(dl,Gecode::IPL_BND,5);
278 Distinct<false> val_l(dl,Gecode::IPL_VAL,5);
279
280 Distinct<true> count_dom_d(-3,3,Gecode::IPL_DOM);
281 Distinct<true> count_bnd_d(-3,3,Gecode::IPL_BND);
282 Distinct<true> count_val_d(-3,3,Gecode::IPL_VAL);
283 Distinct<true> count_dom_s(d,Gecode::IPL_DOM);
284 Distinct<true> count_bnd_s(d,Gecode::IPL_BND);
285 Distinct<true> count_val_s(d,Gecode::IPL_VAL);
286
287 Offset dom_od(-3,3,Gecode::IPL_DOM);
288 Offset bnd_od(-3,3,Gecode::IPL_BND);
289 Offset val_od(-3,3,Gecode::IPL_VAL);
290 Offset dom_os(d,Gecode::IPL_DOM);
291 Offset bnd_os(d,Gecode::IPL_BND);
292 Offset val_os(d,Gecode::IPL_VAL);
293
294 Gecode::IntArgs v1({Gecode::Int::Limits::min+4,
295 0,1,
296 Gecode::Int::Limits::max});
297 Gecode::IntArgs v2({Gecode::Int::Limits::min,
298 0,1,
299 Gecode::Int::Limits::max-4});
300 Gecode::IntArgs v3({0,1,2,3});
301 Gecode::IntArgs v4({0,1,2});
302 Gecode::IntArgs v5({0,1});
303
304 Optional od1(v1,Gecode::IPL_DOM);
305 Optional ob1(v1,Gecode::IPL_BND);
306 Optional ov1(v1,Gecode::IPL_VAL);
307 Optional od2(v2,Gecode::IPL_DOM);
308 Optional ob2(v2,Gecode::IPL_BND);
309 Optional ov2(v2,Gecode::IPL_VAL);
310 Optional od3(v3,Gecode::IPL_DOM);
311 Optional ob3(v3,Gecode::IPL_BND);
312 Optional ov3(v3,Gecode::IPL_VAL);
313 Optional od4(v4,Gecode::IPL_DOM);
314 Optional ob4(v4,Gecode::IPL_BND);
315 Optional ov4(v4,Gecode::IPL_VAL);
316 Optional od5(v5,Gecode::IPL_DOM);
317 Optional ob5(v5,Gecode::IPL_BND);
318 Optional ov5(v5,Gecode::IPL_VAL);
319
320 Except ed1(v1,Gecode::IPL_DOM);
321 Except eb1(v1,Gecode::IPL_BND);
322 Except ev1(v1,Gecode::IPL_VAL);
323 Except ed2(v2,Gecode::IPL_DOM);
324 Except eb2(v2,Gecode::IPL_BND);
325 Except ev2(v2,Gecode::IPL_VAL);
326 Except ed5(v5,Gecode::IPL_DOM);
327 Except eb5(v5,Gecode::IPL_BND);
328 Except ev5(v5,Gecode::IPL_VAL);
329
330 Random dom_r(20,-50,50,Gecode::IPL_DOM);
331 Random bnd_r(50,-500,500,Gecode::IPL_BND);
332 Random val_r(50,-500,500,Gecode::IPL_VAL);
333
334 Pathological p_16_v(16,Gecode::IPL_VAL);
335 Pathological p_16_b(16,Gecode::IPL_BND);
336 Pathological p_16_d(16,Gecode::IPL_DOM);
337
338 Pathological p_32_v(32,Gecode::IPL_VAL);
339 Pathological p_32_b(32,Gecode::IPL_BND);
340 Pathological p_32_d(32,Gecode::IPL_DOM);
341 //@}
342
343 }
344}}
345
346// STATISTICS: test-int