Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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.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
42 namespace Distinct {
43
50 template<bool useCount>
51 class Distinct : public Test {
52 public:
55 int n=6)
56 : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
57 str(ipl)+"::Sparse::"+str(n),n,d0,false,ipl) {}
60 : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
61 str(ipl)+"::Dense",6,min,max,false,ipl) {}
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 }
71 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
72 if (!useCount) {
73 Gecode::distinct(home, x, ipl);
74 } else {
76 int i = 0;
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
86 class Offset : public Test {
87 public:
90 : Test("Distinct::Offset::Sparse::"+str(ipl),6,d,false,ipl) {}
92 Offset(int min, int max, Gecode::IntPropLevel ipl)
93 : Test("Distinct::Offset::Dense::"+str(ipl),6,min,max,false,ipl) {}
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 }
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
112 class Optional : public Test {
113 public:
116 : Test("Distinct::Optional::"+str(ipl)+"::"+str(d),
117 6,Gecode::IntSet(d),false,ipl) {}
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 }
131 virtual void post(Gecode::Space& home, Gecode::IntVarArray& bx) {
132 int n = bx.size() / 2;
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
144 class Except : public Test {
145 public:
148 : Test("Distinct::Except::"+str(ipl)+"::"+str(d),
149 3,Gecode::IntSet(d),false,ipl) {
151 }
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 }
161 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
162 Gecode::distinct(home, x, 0, ipl);
163 }
164 };
165
167 class Random : public Test {
168 public:
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 }
175 virtual Assignment* assignment(void) const {
176 return new RandomAssignment(arity,dom,100);
177 }
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 }
187 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
188 Gecode::distinct(home, x, ipl);
189 }
190 };
191
193 class Pathological : public Base {
194 protected:
196 int n;
200 class TestSpace : public Gecode::Space {
201 public:
203 TestSpace(void) {}
206 : Gecode::Space(s) {}
208 virtual Gecode::Space* copy(void) {
209 return new TestSpace(*this);
210 }
211 };
212 public:
215 : Base("Int::Distinct::Pathological::"+
216 Test::str(n0)+"::"+Test::str(ipl0)), n(n0), ipl(ipl0) {}
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};
268
275
279
286
293
295 0,1,
298 0,1,
300 Gecode::IntArgs v3({0,1,2,3});
303
319
329
333
337
342
343 }
344}}
345
346// STATISTICS: test-int
struct Gecode::@603::NNF::@65::@66 b
For binary nodes (and, or, eqv)
int n
Number of negative literals for node type.
Node * x
Pointer to corresponding Boolean expression node.
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Passing Boolean variables.
Definition int.hh:712
Passing integer arguments.
Definition int.hh:628
Range iterator for integer sets.
Definition int.hh:292
Value iterator for integer sets.
Definition int.hh:333
Integer sets.
Definition int.hh:174
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Integer variables.
Definition int.hh:371
Computation spaces.
Definition core.hpp:1742
int size(void) const
Return size of array (number of elements)
Definition array.hpp:926
Base class for all tests to be run
Definition test.hh:103
Base class for assignments
Definition int.hh:59
Simple test for distinct constraint.
Definition distinct.cpp:51
Distinct(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:59
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:71
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:63
Simple test for distinct except constant constraint.
Definition distinct.cpp:144
Except(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:147
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:153
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on bx.
Definition distinct.cpp:161
Simple test for distinct constraint with offsets.
Definition distinct.cpp:86
Offset(const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:89
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:95
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition distinct.cpp:103
Offset(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:92
Simple test for optional distinct constraint.
Definition distinct.cpp:112
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:119
virtual void post(Gecode::Space &home, Gecode::IntVarArray &bx)
Post constraint on bx.
Definition distinct.cpp:131
Optional(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:115
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition distinct.cpp:208
TestSpace(TestSpace &s)
Constructor for cloning s.
Definition distinct.cpp:205
Testing pathological cases
Definition distinct.cpp:193
int n
Number of variables.
Definition distinct.cpp:196
virtual bool run(void)
Perform test.
Definition distinct.cpp:218
Pathological(int n0, Gecode::IntPropLevel ipl0)
Create and register test.
Definition distinct.cpp:214
Gecode::IntPropLevel ipl
Consistency level.
Definition distinct.cpp:198
Randomized test for distinct constraint.
Definition distinct.cpp:167
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition distinct.cpp:175
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition distinct.cpp:187
Random(int n, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition distinct.cpp:170
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition distinct.cpp:179
Generate random selection of assignments.
Definition int.hh:96
bool testsearch
Whether to perform search test.
Definition int.hh:238
Gecode::IntPropLevel ipl
Propagation level.
Definition int.hh:234
int arity
Number of variables.
Definition int.hh:226
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
ConTestLevel contest
Whether to test for certain consistency.
Definition int.hh:236
IntPropLevel
Propagation levels for integer propagators.
Definition int.hh:974
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition int.hh:979
@ IPL_VAL
Value propagation.
Definition int.hh:977
@ IPL_BND
Bounds propagation.
Definition int.hh:978
Space(void)
Default constructor.
Definition core.cpp:115
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition core.cpp:252
const int min
Smallest allowed integer value.
Definition int.hh:118
const int max
Largest allowed integer value.
Definition int.hh:116
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:261
@ CTL_NONE
No consistency-test.
Definition int.hh:140
General test support.
Definition afc.cpp:39