Generated on for Gecode by doxygen 1.17.0
gcc.cpp
Go to the documentation of this file.
1/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2/*
3 * Main authors:
4 * Patrick Pekczynski <pekczynski@ps.uni-sb.de>
5 *
6 * Contributing authors:
7 * Christian Schulte <schulte@gecode.dev>
8 *
9 * Copyright:
10 * Patrick Pekczynski, 2005
11 * Christian Schulte, 2007
12 *
13 * This file is part of Gecode, the generic constraint
14 * development environment:
15 * http://www.gecode.dev
16 *
17 * Permission is hereby granted, free of charge, to any person obtaining
18 * a copy of this software and associated documentation files (the
19 * "Software"), to deal in the Software without restriction, including
20 * without limitation the rights to use, copy, modify, merge, publish,
21 * distribute, sublicense, and/or sell copies of the Software, and to
22 * permit persons to whom the Software is furnished to do so, subject to
23 * the following conditions:
24 *
25 * The above copyright notice and this permission notice shall be
26 * included in all copies or substantial portions of the Software.
27 *
28 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 *
36 */
37
38#include "test/int.hh"
39
40namespace Test { namespace Int {
41
43 namespace GCC {
44
50
51 class IntAllMinMax : public Test {
52 public:
55 : Test(TestTags::sweep(),"GCC::Int::All::MinMax::"+str(ipl),4,-1,3,false,ipl) {}
56
57 virtual bool solution(const Assignment& x) const {
58 int n[5];
59 for (int i=5; i--; )
60 n[i]=0;
61 for (int i=x.size(); i--; )
62 n[x[i]+1]++;
63 if (n[2] > 0)
64 return false;
65 for (int i=5; i--;)
66 if (n[i]>2)
67 return false;
68 return true;
69 }
70
71 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
72 using namespace Gecode;
73 IntArgs values(5);
74 IntSet fixed(0,2);
75 IntSetArgs cards(5);
76 for (int i=0; i<5; i++) {
77 values[i] = i-1; cards[i] = fixed;
78 }
79 cards[2] = IntSet(0,0);
80 count(home, x, cards, values, ipl);
81 }
82 };
83
85 class IntAllMinMaxDef : public Test {
86 public:
89 : Test(TestTags::sweep(),"GCC::Int::All::MinMaxDef::"+str(ipl),4,0,3,false,ipl) {}
90
91 virtual bool solution(const Assignment& x) const {
92 int n[4];
93 for (int i=4; i--; )
94 n[i]=0;
95 for (int i=x.size(); i--; )
96 n[x[i]]++;
97 if (n[2] > 0)
98 return false;
99 for (int i=4; i--;)
100 if (n[i]>2)
101 return false;
102 return true;
103 }
104
105 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
106 using namespace Gecode;
107 IntSet fixed(0,2);
108 IntSetArgs cards(4);
109 for (int i=0; i<4; i++) {
110 cards[i] = fixed;
111 }
112 cards[2] = IntSet(0,0);
113 count(home, x, cards, ipl);
114 }
115 };
116
118 class IntAllMax : public Test {
119 public:
123 "GCC::Int::All::Max::"+str(ipl), 4, 1,2, false, ipl) {}
124
125 virtual bool solution(const Assignment& x) const {
126 int n[2];
127 for (int i=2; i--; )
128 n[i] = 0;
129 for (int i=x.size(); i--; )
130 n[x[i] - 1]++;
131 if (n[0] != 2 || n[1] != 2)
132 return false;
133 return true;
134 }
135
136 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
137 Gecode::IntArgs values({1,2});
138 Gecode::count(home, x, Gecode::IntSet(2,2), values, ipl);
139 }
140 };
141
142
144 template<bool hole>
145 class IntSome : public Test {
146 public:
149 : Test(TestTags::sweep(),std::string("GCC::Int::Some::")+
150 (hole ? "::Hole" : "::Full")+str(ipl),4,1,4,false,ipl) {}
151
152 virtual bool solution(const Assignment& x) const {
153 int n[4];
154 for (int i=4; i--; )
155 n[i]=0;
156 for (int i=x.size(); i--; )
157 n[x[i]-1]++;
158 if ((n[0] < 2) || (n[1] < 2) || (n[2] > 0) || (n[3] > 0))
159 return false;
160 return true;
161 }
162
163 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
164 using namespace Gecode;
165 IntArgs values({1,2});
166 Gecode::IntSet fixed;
167 if (!hole) {
168 fixed = IntSet(0,2);
169 } else {
170 int ish[] = {0,2};
171 fixed = IntSet(ish,2);
172 }
173 Gecode::IntSetArgs cards(2);
174 cards[0]=fixed; cards[1]=fixed;
175 count(home, x, cards, values, ipl);
176 }
177 };
178
180 class VarAll : public Test {
181 protected:
183 static const int n = 4;
184 public:
187 : Test(TestTags::sweep(),"GCC::Var::All::"+str(ipl),7,0,2,false,ipl) {}
188
189 virtual bool solution(const Assignment& x) const {
190 // Number of cardinality variables
191 int m = x.size()-n;
192 for (int i=0; i<n; i++)
193 if ((x[i] < 0) || (x[i] > 2))
194 return false;
195 int* card = new int[m];
196 for (int i=0; i<m; i++) {
197 card[i] = 0;
198 if ((x[n+i] < 0) || (x[n+i] > 2)) {
199 delete [] card;
200 return false;
201 }
202 }
203 for (int i=0; i<n; i++)
204 card[x[i]]++;
205 for (int i=0; i<m; i++)
206 if (card[i] != x[n+i]) {
207 delete [] card;
208 return false;
209 }
210 delete [] card;
211 return true;
212 }
213
214 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
215 using namespace Gecode;
216 // Number of cardinality variables
217 int m = xy.size()-n;
218
219 IntVarArgs x(n), y(m);
220 for (int i=0; i<n; i++)
221 x[i]=xy[i];
222 for (int i=0; i<m; i++)
223 y[i]=xy[n+i];
224 count(home, x, y, ipl);
225 }
226 };
227
229 class VarSome : public Test {
230 protected:
232 int n;
234 static const int randomArity = 7;
235 public:
237 VarSome(std::string s, int n0, int min, int max,
239 : Test(TestTags::sweep(),"GCC::Var::Some::"+s+"::"+str(ipl),
240 n0+(max-min)+1,min,max,false,ipl)
241 , n(n0)
242 {
244 if (arity>randomArity)
245 testsearch = false;
246 }
247
248 virtual bool solution(const Assignment& x) const {
249 // Number of cardinality variables
250 int m = x.size()-n;
251 int* card = new int[m];
252 for (int i=0; i<m; i++) {
253 card[i] = 0;
254 if ((x[n+i] < 0) || (x[n+i] > n)) {
255 delete [] card;
256 return false;
257 }
258 }
259 for (int i=0; i<n; i++)
260 card[x[i]-dom.min()]++;
261 for (int i=0; i<m; i++)
262 if (card[i] != x[n+i]) {
263 delete [] card;
264 return false;
265 }
266 delete [] card;
267 return true;
268 }
269
270 virtual Assignment* assignment(void) const {
271 if (arity > randomArity)
272 return new RandomAssignment(arity, dom, 4000, _rand);
273 else
274 return new CpltAssignment(arity,dom);
275 }
276
277 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
278 using namespace Gecode;
279 // Number of cardinality variables
280 int m = xy.size()-n;
281 IntVarArgs x(n), y(m);
282 for (int i=0; i<n; i++)
283 x[i]=xy[i];
284 for (int i=0; i<m; i++)
285 y[i]=xy[n+i];
286 IntArgs values(m);
287 for (int i=m; i--;)
288 values[i] = i+dom.min();
289 count(home,x,y,values,ipl);
290 }
291 };
292
294 class Create {
295 public:
297 Create(void) {
298 for (IntPropLevels ipls; ipls(); ++ipls) {
299 (void) new IntAllMinMax(ipls.ipl());
300 (void) new IntAllMinMaxDef(ipls.ipl());
301 (void) new IntAllMax(ipls.ipl());
302 (void) new IntSome<false>(ipls.ipl());
303 (void) new IntSome<true>(ipls.ipl());
304 (void) new VarAll(ipls.ipl());
305 (void) new VarSome("Small",2,-1,3,ipls.ipl());
306 (void) new VarSome("Large",3,-1,4,ipls.ipl());
307 }
308 }
309 };
310
313
314 }
315}}
316
317// STATISTICS: test-int
Passing integer arguments.
Definition int.hh:653
Integer sets.
Definition int.hh:179
Passing integer variables.
Definition int.hh:681
Integer variable array.
Definition int.hh:792
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 class for assignments
Definition int.hh:73
int size(void) const
Return number of variables.
Definition int.hpp:46
Generate all assignments.
Definition int.hh:93
Help class to create and register tests.
Definition gcc.cpp:294
Create(void)
Perform creation and registration.
Definition gcc.cpp:297
Test for integer cardinality with max cardinality for all variables
Definition gcc.cpp:118
IntAllMax(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:121
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:125
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:136
Test for integer cardinality with min and max for all variables
Definition gcc.cpp:85
IntAllMinMaxDef(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:88
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:91
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:105
Test for integer cardinality with min and max for all variables
Definition gcc.cpp:51
IntAllMinMax(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:54
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:71
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:57
Test for integer cardinality for some variables
Definition gcc.cpp:145
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:152
IntSome(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:148
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:163
Test for variable cardinality for all cardinality values
Definition gcc.cpp:180
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:189
static const int n
Number of non-cardinality variables.
Definition gcc.cpp:183
VarAll(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:186
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition gcc.cpp:214
Test for variable cardinality for some cardinality values
Definition gcc.cpp:229
int n
Number of non-cardinality variables.
Definition gcc.cpp:232
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition gcc.cpp:277
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition gcc.cpp:270
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:248
VarSome(std::string s, int n0, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:237
static const int randomArity
Arity beyond which to use randomized tests.
Definition gcc.cpp:234
Iterator for simple integer propagation levels.
Definition int.hh:430
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_BND
Bounds propagation.
Definition int.hh:1278
Gecode toplevel namespace
ArgArray< IntSet > IntSetArgs
Passing set arguments.
Definition int.hh:644
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 min(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
void max(Home home, FloatVar x0, FloatVar x1, FloatVar x2)
Post propagator for .
Tests for counting constraints (global cardinality)
Definition gcc.cpp:43
Create c
Definition gcc.cpp:311
Testing finite domain integers.
Definition int.cpp:41
@ CTL_NONE
No consistency-test.
Definition int.hh:154
General test support.
Definition afc.cpp:41
@ standard
Standard test suite.
Definition test.hh:100
@ sweep
Really heavy sweep tests.
Definition test.hh:101