Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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.org>
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.org
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
51 class IntAllMinMax : public Test {
52 public:
55 : Test("GCC::Int::All::MinMax::"+str(ipl),4,-1,3,false,ipl) {}
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 }
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("GCC::Int::All::MinMaxDef::"+str(ipl),4,0,3,false,ipl) {}
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 }
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:
122 : Test("GCC::Int::All::Max::"+str(ipl), 4, 1,2, false, ipl) {}
124 virtual bool solution(const Assignment& x) const {
125 int n[2];
126 for (int i=2; i--; )
127 n[i] = 0;
128 for (int i=x.size(); i--; )
129 n[x[i] - 1]++;
130 if (n[0] != 2 || n[1] != 2)
131 return false;
132 return true;
133 }
135 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
136 Gecode::IntArgs values({1,2});
137 Gecode::count(home, x, Gecode::IntSet(2,2), values, ipl);
138 }
139 };
140
141
143 template<bool hole>
144 class IntSome : public Test {
145 public:
148 : Test(std::string("GCC::Int::Some::")+
149 (hole ? "::Hole" : "::Full")+str(ipl),4,1,4,false,ipl) {}
151 virtual bool solution(const Assignment& x) const {
152 int n[4];
153 for (int i=4; i--; )
154 n[i]=0;
155 for (int i=x.size(); i--; )
156 n[x[i]-1]++;
157 if ((n[0] < 2) || (n[1] < 2) || (n[2] > 0) || (n[3] > 0))
158 return false;
159 return true;
160 }
162 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
163 using namespace Gecode;
164 IntArgs values({1,2});
165 Gecode::IntSet fixed;
166 if (!hole) {
167 fixed = IntSet(0,2);
168 } else {
169 int ish[] = {0,2};
170 fixed = IntSet(ish,2);
171 }
172 Gecode::IntSetArgs cards(2);
173 cards[0]=fixed; cards[1]=fixed;
174 count(home, x, cards, values, ipl);
175 }
176 };
177
179 class VarAll : public Test {
180 protected:
182 static const int n = 4;
183 public:
186 : Test("GCC::Var::All::"+str(ipl),7,0,2,false,ipl) {}
188 virtual bool solution(const Assignment& x) const {
189 // Number of cardinality variables
190 int m = x.size()-n;
191 for (int i=0; i<n; i++)
192 if ((x[i] < 0) || (x[i] > 2))
193 return false;
194 int* card = new int[m];
195 for (int i=0; i<m; i++) {
196 card[i] = 0;
197 if ((x[n+i] < 0) || (x[n+i] > 2)) {
198 delete [] card;
199 return false;
200 }
201 }
202 for (int i=0; i<n; i++)
203 card[x[i]]++;
204 for (int i=0; i<m; i++)
205 if (card[i] != x[n+i]) {
206 delete [] card;
207 return false;
208 }
209 delete [] card;
210 return true;
211 }
213 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
214 using namespace Gecode;
215 // Number of cardinality variables
216 int m = xy.size()-n;
217
218 IntVarArgs x(n), y(m);
219 for (int i=0; i<n; i++)
220 x[i]=xy[i];
221 for (int i=0; i<m; i++)
222 y[i]=xy[n+i];
223 count(home, x, y, ipl);
224 }
225 };
226
228 class VarSome : public Test {
229 protected:
231 int n;
233 static const int randomArity = 7;
234 public:
236 VarSome(std::string s, int n0, int min, int max,
238 : Test("GCC::Var::Some::"+s+"::"+str(ipl),
239 n0+(max-min)+1,min,max,false,ipl)
240 , n(n0)
241 {
243 if (arity>randomArity)
244 testsearch = false;
245 }
247 virtual bool solution(const Assignment& x) const {
248 // Number of cardinality variables
249 int m = x.size()-n;
250 int* card = new int[m];
251 for (int i=0; i<m; i++) {
252 card[i] = 0;
253 if ((x[n+i] < 0) || (x[n+i] > n)) {
254 delete [] card;
255 return false;
256 }
257 }
258 for (int i=0; i<n; i++)
259 card[x[i]-dom.min()]++;
260 for (int i=0; i<m; i++)
261 if (card[i] != x[n+i]) {
262 delete [] card;
263 return false;
264 }
265 delete [] card;
266 return true;
267 }
269 virtual Assignment* assignment(void) const {
270 if (arity > randomArity)
271 return new RandomAssignment(arity,dom,4000);
272 else
273 return new CpltAssignment(arity,dom);
274 }
276 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
277 using namespace Gecode;
278 // Number of cardinality variables
279 int m = xy.size()-n;
280 IntVarArgs x(n), y(m);
281 for (int i=0; i<n; i++)
282 x[i]=xy[i];
283 for (int i=0; i<m; i++)
284 y[i]=xy[n+i];
285 IntArgs values(m);
286 for (int i=m; i--;)
287 values[i] = i+dom.min();
288 count(home,x,y,values,ipl);
289 }
290 };
291
293 class Create {
294 public:
296 Create(void) {
297 for (IntPropLevels ipls; ipls(); ++ipls) {
298 (void) new IntAllMinMax(ipls.ipl());
299 (void) new IntAllMinMaxDef(ipls.ipl());
300 (void) new IntAllMax(ipls.ipl());
301 (void) new IntSome<false>(ipls.ipl());
302 (void) new IntSome<true>(ipls.ipl());
303 (void) new VarAll(ipls.ipl());
304 (void) new VarSome("Small",2,-1,3,ipls.ipl());
305 (void) new VarSome("Large",3,-1,4,ipls.ipl());
306 }
307 }
308 };
309
312
313 }
314}}
315
316// STATISTICS: test-int
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 integer arguments.
Definition int.hh:628
Integer sets.
Definition int.hh:174
int min(int i) const
Return minimum of range at position i.
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Computation spaces.
Definition core.hpp:1742
int size(void) const
Return size of array (number of elements)
Definition array.hpp:926
Base class for assignments
Definition int.hh:59
Generate all assignments.
Definition int.hh:79
Help class to create and register tests.
Definition gcc.cpp:293
Create(void)
Perform creation and registration.
Definition gcc.cpp:296
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:124
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:135
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:144
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:151
IntSome(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:147
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition gcc.cpp:162
Test for variable cardinality for all cardinality values
Definition gcc.cpp:179
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:188
static const int n
Number of non-cardinality variables.
Definition gcc.cpp:182
VarAll(Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:185
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition gcc.cpp:213
Test for variable cardinality for some cardinality values
Definition gcc.cpp:228
int n
Number of non-cardinality variables.
Definition gcc.cpp:231
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition gcc.cpp:276
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition gcc.cpp:269
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition gcc.cpp:247
VarSome(std::string s, int n0, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition gcc.cpp:236
static const int randomArity
Arity beyond which to use randomized tests.
Definition gcc.cpp:233
Iterator for simple integer propagation levels.
Definition int.hh:332
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
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
Create c
Definition gcc.cpp:310
@ CTL_NONE
No consistency-test.
Definition int.hh:140
General test support.
Definition afc.cpp:39