Generated on for Gecode by doxygen 1.17.0
distinct.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Christian Schulte <schulte@gecode.dev>
5 * Mikael Zayenz Lagerkvist <lagerkvist@gecode.dev>
6 *
7 * Copyright:
8 * Christian Schulte, 2005
9 * Mikael Zayenz Lagerkvist, 2006
10 *
11 * This file is part of Gecode, the generic constraint
12 * development environment:
13 * http://www.gecode.dev
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
42 namespace Distinct {
43
49
50 template<bool useCount>
51 class Distinct : public Test {
52 public:
55 int n=6)
56 : Test((useCount || (ipl != Gecode::IPL_VAL))
58 std::string(useCount ? "Count::Distinct::" : "Distinct::")+
59 str(ipl)+"::Sparse::"+str(n),n,d0,false,ipl) {}
60
62 : Test((!useCount || (ipl == Gecode::IPL_BND))
64 std::string(useCount ? "Count::Distinct::" : "Distinct::")+
65 str(ipl)+"::Dense",6,min,max,false,ipl) {}
66
67 virtual bool solution(const Assignment& x) const {
68 for (int i=0; i<x.size(); i++)
69 for (int j=i+1; j<x.size(); j++)
70 if (x[i]==x[j])
71 return false;
72 return true;
73 }
74
75 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
76 if (!useCount) {
77 Gecode::distinct(home, x, ipl);
78 } else {
80 int i = 0;
82 for (Gecode::IntSetValues dr2(dom); dr2(); ++dr2)
83 ia[i++] = dr2.val();
84 Gecode::count(home, x, Gecode::IntSet(0,1), ia, ipl);
85 }
86 }
87 };
88
90 class Offset : public Test {
91 public:
94 : Test(TestTags::sweep(),
95 "Distinct::Offset::Sparse::"+str(ipl),6,d,false,ipl) {}
96
97 Offset(int min, int max, Gecode::IntPropLevel ipl)
98 : Test(ipl == Gecode::IPL_BND ? TestTags::standard() : TestTags::sweep(),
99 "Distinct::Offset::Dense::"+str(ipl),6,min,max,false,ipl) {}
100
101 virtual bool solution(const Assignment& x) const {
102 for (int i=0; i<x.size(); i++)
103 for (int j=i+1; j<x.size(); j++)
104 if (x[i]+i==x[j]+j)
105 return false;
106 return true;
107 }
108
109 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
110 Gecode::IntArgs c(x.size());
111 for (int i=0; i<x.size(); i++)
112 c[i]=i;
113 Gecode::distinct(home, c, x, ipl);
114 }
115 };
116
118 class Optional : public Test {
119 public:
122 : Test("Distinct::Optional::"+str(ipl)+"::"+str(d),
123 6,Gecode::IntSet(d),false,ipl) {}
124
125 virtual bool solution(const Assignment& x) const {
126 int n = x.size() / 2;
127 for (int i=0; i<n; i++)
128 if ((x[i] < 0) || (x[i] > 1))
129 return false;
130 for (int i=0; i<n; i++)
131 for (int j=i+1; j<n; j++)
132 if ((x[i] == 1) && (x[j] == 1) && (x[n+i] == x[n+j]))
133 return false;
134 return true;
135 }
136
137 virtual void post(Gecode::Space& home, Gecode::IntVarArray& bx) {
138 int n = bx.size() / 2;
141 for (int i=0; i<n; i++) {
142 b[i] = Gecode::channel(home, bx[i]);
143 x[i] = bx[n+i];
144 }
145 Gecode::distinct(home, b, x, ipl);
146 }
147 };
148
150 class Except : public Test {
151 public:
154 : Test("Distinct::Except::"+str(ipl)+"::"+str(d),
155 3,Gecode::IntSet(d),false,ipl) {
157 }
158
159 virtual bool solution(const Assignment& x) const {
160 for (int i=0; i<x.size(); i++)
161 for (int j=i+1; j<x.size(); j++)
162 if ((x[i] != 0) && (x[j] != 0) && (x[i] == x[j]))
163 return false;
164 return true;
165 }
166
167 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
168 Gecode::distinct(home, x, 0, ipl);
169 }
170 };
171
173 class Random : public Test {
174 public:
176 Random(int n, int min, int max, Gecode::IntPropLevel ipl)
177 : Test(TestTags::check(),
178 "Distinct::Random::"+str(ipl),n,min,max,false,ipl) {
179 testsearch = false;
180 }
181
182 virtual Assignment* assignment(void) const {
183 return new RandomAssignment(arity, dom, 100, _rand);
184 }
185
186 virtual bool solution(const Assignment& x) const {
187 for (int i=0; i<x.size(); i++)
188 for (int j=i+1; j<x.size(); j++)
189 if (x[i]==x[j])
190 return false;
191 return true;
192 }
193
194 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
195 Gecode::distinct(home, x, ipl);
196 }
197 };
198
200 class Pathological : public Base {
201 protected:
203 int n;
207 class TestSpace : public Gecode::Space {
208 public:
210 TestSpace(void) {}
213 : Gecode::Space(s) {}
214
215 virtual Gecode::Space* copy(void) {
216 return new TestSpace(*this);
217 }
218 };
219 public:
222 : Base("Int::Distinct::Pathological::"+
223 Test::str(n0)+"::"+Test::str(ipl0), TestTags::sweep()),
224 n(n0), ipl(ipl0) {}
225
226 virtual bool run(void) {
227 using namespace Gecode;
228 {
229 TestSpace* s = new TestSpace;
230 IntVarArgs x(n);
231 for (int i=0; i<n; i++)
232 x[i] = IntVar(*s,0,i);
233 distinct(*s,x,ipl);
234 if (s->status() == SS_FAILED) {
235 delete s; return false;
236 }
237 for (int i=0; i<n; i++)
238 if (!x[i].assigned() || (x[i].val() != i)) {
239 delete s; return false;
240 }
241 delete s;
242 }
243 {
244 TestSpace* s = new TestSpace;
245 IntVarArgs x(2*n);
246 for (int i=0; i<n; i++) {
247 int v[] = {0,i};
248 IntSet d(v,2);
249 x[i] = IntVar(*s,d);
250 }
251 for (int i=n; i<2*n; i++)
252 x[i] = IntVar(*s,n-1,i);
253 distinct(*s,x,ipl);
254 if (s->status() == SS_FAILED) {
255 delete s; return false;
256 }
257 for (int i=0; i<n; i++)
258 if (!x[i].assigned() || (x[i].val() != i)) {
259 delete s; return false;
260 }
261 delete s;
262 }
263 return true;
264 }
265 };
266
267 const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
276
283
287
294
301
303 0,1,
306 0,1,
308 Gecode::IntArgs v3({0,1,2,3});
311
327
337
341
345
350
351 }
352}}
353
354// STATISTICS: test-int
Passing Boolean variables.
Definition int.hh:739
Passing integer arguments.
Definition int.hh:653
Range iterator for integer sets.
Definition int.hh:311
Value iterator for integer sets.
Definition int.hh:352
Integer sets.
Definition int.hh:179
Passing integer variables.
Definition int.hh:681
Integer variable array.
Definition int.hh:792
Integer variables.
Definition int.hh:390
Computation spaces.
Definition core.hpp:1787
int size(void) const
Return size of array (number of elements).
Definition array.hpp:945
Gecode::Support::RandomGenerator _rand
Random number generator.
Definition test.hh:238
Base(std::string s)
Create and register a standard test with name s.
Definition test.cpp:125
static std::string str(bool b)
Map bool to string.
Definition test.hpp:121
Base class for assignments
Definition int.hh:73
int size(void) const
Return number of variables.
Definition int.hpp:46
Simple test for distinct constraint.
Definition distinct.cpp:51
Distinct(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:61
Distinct(const Gecode::IntSet &d0, Gecode::IntPropLevel ipl, int n=6)
Create and register test.
Definition distinct.cpp:54
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition distinct.cpp:75
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:67
Simple test for distinct except constant constraint.
Definition distinct.cpp:150
Except(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:153
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:159
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on bx.
Definition distinct.cpp:167
Simple test for distinct constraint with offsets.
Definition distinct.cpp:90
Offset(const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:93
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:101
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition distinct.cpp:109
Offset(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:97
Simple test for optional distinct constraint.
Definition distinct.cpp:118
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:125
virtual void post(Gecode::Space &home, Gecode::IntVarArray &bx)
Post constraint on bx.
Definition distinct.cpp:137
Optional(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:121
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition distinct.cpp:215
TestSpace(TestSpace &s)
Constructor for cloning s.
Definition distinct.cpp:212
Testing pathological cases
Definition distinct.cpp:200
int n
Number of variables.
Definition distinct.cpp:203
virtual bool run(void)
Perform test.
Definition distinct.cpp:226
Pathological(int n0, Gecode::IntPropLevel ipl0)
Create and register test.
Definition distinct.cpp:221
Gecode::IntPropLevel ipl
Consistency level.
Definition distinct.cpp:205
Randomized test for distinct constraint.
Definition distinct.cpp:173
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition distinct.cpp:182
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition distinct.cpp:194
Random(int n, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:176
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:186
Generate random selection of assignments.
Definition int.hh:110
bool testsearch
Whether to perform search test.
Definition int.hh:285
static std::string str(bool b)
Map bool to string.
Definition int.hpp:278
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:281
int arity
Number of variables.
Definition int.hh:273
Gecode::IntSet dom
Domain of variables.
Definition int.hh:275
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:283
Set of test tags.
Definition test.hh:105
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:1274
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:1279
@ IPL_VAL
Value propagation.
Definition int.hh:1277
@ IPL_BND
Bounds propagation.
Definition int.hh:1278
Space(void)
Default constructor.
Definition core.cpp:121
SpaceStatus status(StatusStatistics &stat)
Query space status.
Definition core.cpp:281
@ SS_FAILED
Space is failed
Definition core.hpp:1727
const int min
Smallest allowed integer value.
Definition int.hh:123
const int max
Largest allowed integer value.
Definition int.hh:121
unsigned int size(I &i)
Size of all ranges of range iterator i.
Gecode toplevel namespace
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition count.cpp:40
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl=IPL_DEF)
Post propagator for for all .
Definition distinct.cpp:46
Except eb2(v2, Gecode::IPL_BND)
Pathological p_16_b(16, Gecode::IPL_BND)
Pathological p_16_v(16, Gecode::IPL_VAL)
Pathological p_32_v(32, Gecode::IPL_VAL)
Distinct< false > val_s(d, Gecode::IPL_VAL)
Optional od2(v2, Gecode::IPL_DOM)
Random val_r(50,-500, 500, Gecode::IPL_VAL)
Optional ob2(v2, Gecode::IPL_BND)
Gecode::IntArgs v4({0, 1, 2})
Distinct< true > count_val_s(d, Gecode::IPL_VAL)
Except ev1(v1, Gecode::IPL_VAL)
Distinct< false > dom_l(dl, Gecode::IPL_DOM, 5)
Optional ov4(v4, Gecode::IPL_VAL)
Except eb1(v1, Gecode::IPL_BND)
Gecode::IntSet dl(vl, 6)
Except ev2(v2, Gecode::IPL_VAL)
Offset bnd_os(d, Gecode::IPL_BND)
Offset dom_od(-3, 3, Gecode::IPL_DOM)
Offset val_od(-3, 3, Gecode::IPL_VAL)
Except eb5(v5, Gecode::IPL_BND)
Distinct< true > count_dom_d(-3, 3, Gecode::IPL_DOM)
Distinct< true > count_dom_s(d, Gecode::IPL_DOM)
Gecode::IntArgs v3({0, 1, 2, 3})
Optional ob1(v1, Gecode::IPL_BND)
Except ev5(v5, Gecode::IPL_VAL)
Distinct< false > bnd_s(d, Gecode::IPL_BND)
Random dom_r(20,-50, 50, Gecode::IPL_DOM)
Optional ob5(v5, Gecode::IPL_BND)
Gecode::IntSet d(v, 7)
Distinct< false > val_l(dl, Gecode::IPL_VAL, 5)
Except ed1(v1, Gecode::IPL_DOM)
Distinct< false > bnd_l(dl, Gecode::IPL_BND, 5)
Gecode::IntArgs v5({0, 1})
Optional ob4(v4, Gecode::IPL_BND)
Pathological p_32_b(32, Gecode::IPL_BND)
Gecode::IntArgs v2({Gecode::Int::Limits::min, 0, 1, Gecode::Int::Limits::max-4})
Distinct< false > val_d(-3, 3, Gecode::IPL_VAL)
Optional ov3(v3, Gecode::IPL_VAL)
Offset dom_os(d, Gecode::IPL_DOM)
Pathological p_32_d(32, Gecode::IPL_DOM)
Distinct< true > count_val_d(-3, 3, Gecode::IPL_VAL)
Gecode::IntArgs v1({Gecode::Int::Limits::min+4, 0, 1, Gecode::Int::Limits::max})
Distinct< false > dom_d(-3, 3, Gecode::IPL_DOM)
Pathological p_16_d(16, Gecode::IPL_DOM)
Except ed5(v5, Gecode::IPL_DOM)
Optional od4(v4, Gecode::IPL_DOM)
Optional ov5(v5, Gecode::IPL_VAL)
Distinct< false > dom_s(d, Gecode::IPL_DOM)
Offset val_os(d, Gecode::IPL_VAL)
Optional ov1(v1, Gecode::IPL_VAL)
Distinct< true > count_bnd_s(d, Gecode::IPL_BND)
Optional od3(v3, Gecode::IPL_DOM)
Optional ob3(v3, Gecode::IPL_BND)
Except ed2(v2, Gecode::IPL_DOM)
Offset bnd_od(-3, 3, Gecode::IPL_BND)
Random bnd_r(50,-500, 500, Gecode::IPL_BND)
Distinct< true > count_bnd_d(-3, 3, Gecode::IPL_BND)
Optional od5(v5, Gecode::IPL_DOM)
Optional ov2(v2, Gecode::IPL_VAL)
Distinct< false > bnd_d(-3, 3, Gecode::IPL_BND)
Optional od1(v1, Gecode::IPL_DOM)
const int vl[6]
Definition distinct.cpp:269
Testing finite domain integers.
Definition int.cpp:41
@ CTL_NONE
No consistency-test.
Definition int.hh:154
General test support.
Definition afc.cpp:41
@ check
Basic integrity tests.
Definition test.hh:99
@ standard
Standard test suite.
Definition test.hh:100
@ sweep
Really heavy sweep tests.
Definition test.hh:101