Generated on Sat Feb 7 2015 02:01:21 for Gecode by doxygen 1.8.9.1
count.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  *
6  * Copyright:
7  * Christian Schulte, 2005
8  *
9  * Last modified:
10  * $Date: 2011-08-22 13:08:29 +0200 (Mon, 22 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12326 $
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 
40 namespace Test { namespace Int {
41 
43  namespace Count {
44 
50  class IntInt : public Test {
52  protected:
55  public:
58  : Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
60  virtual bool solution(const Assignment& x) const {
61  int m = 0;
62  for (int i=x.size(); i--; )
63  if (x[i] == 0)
64  m++;
65  return cmp(m,irt,2);
66  }
68  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
69  Gecode::count(home, x, 0, irt, 2);
70  }
71  };
72 
74  class SetInt : public Test {
75  protected:
78  public:
81  : Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
83  virtual bool solution(const Assignment& x) const {
84  int m = 0;
85  for (int i=x.size(); i--; )
86  if ((x[i] >= -1) && (x[i] <= 1))
87  m++;
88  return cmp(m,irt,2);
89  }
91  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
92  Gecode::IntSet s(-1,1);
93  Gecode::count(home, x, s, irt, 2);
94  }
95  };
96 
98  class IntIntDup : public Test {
99  protected:
102  public:
105  : Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
107  virtual bool solution(const Assignment& x) const {
108  int m = 0;
109  for (int i=x.size(); i--; )
110  if (x[i] == 0)
111  m += 2;
112  return cmp(m,irt,4);
113  }
115  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
116  Gecode::IntVarArgs y(8);
117  for (int i=x.size(); i--; )
118  y[i]=y[4+i]=x[i];
119  Gecode::count(home, y, 0, irt, 4);
120  }
121  };
122 
124  class IntVar : public Test {
125  protected:
128  public:
131  : Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
133  virtual bool solution(const Assignment& x) const {
134  int m = 0;
135  for (int i=0; i<4; i++)
136  if (x[i] == 0)
137  m++;
138  return cmp(m,irt,x[4]);
139  }
141  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
142  Gecode::IntVarArgs y(4);
143  for (int i=0; i<4; i++)
144  y[i]=x[i];
145  Gecode::count(home, y, 0, irt, x[4]);
146  }
147  };
148 
150  class SetVar : public Test {
151  protected:
154  public:
157  : Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
159  virtual bool solution(const Assignment& x) const {
160  int m = 0;
161  for (int i=0; i<4; i++)
162  if ((x[i] >= -1) && (x[i] <= 1))
163  m++;
164  return cmp(m,irt,x[4]);
165  }
167  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
168  Gecode::IntVarArgs y(4);
169  for (int i=0; i<4; i++)
170  y[i]=x[i];
171  Gecode::IntSet s(-1,1);
172  Gecode::count(home, y, s, irt, x[4]);
173  }
174  };
175 
176  Gecode::IntArgs ints(4, 1,0,3,2);
177 
179  class IntArrayInt : public Test {
180  protected:
183  public:
186  : Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
188  virtual bool solution(const Assignment& x) const {
189  int m = 0;
190  for (int i=0; i<4; i++)
191  if (x[i] == ints[i])
192  m++;
193  return cmp(m,irt,2);
194  }
196  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
197  Gecode::IntVarArgs y(4);
198  for (int i=0; i<4; i++)
199  y[i]=x[i];
200  Gecode::count(home, y, ints, irt, 2);
201  }
202  };
203 
205  class IntArrayVar : public Test {
206  protected:
209  public:
212  : Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
214  virtual bool solution(const Assignment& x) const {
215  int m = 0;
216  for (int i=0; i<4; i++)
217  if (x[i] == ints[i])
218  m++;
219  return cmp(m,irt,x[4]);
220  }
222  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
223  Gecode::IntVarArgs y(4);
224  for (int i=0; i<4; i++)
225  y[i]=x[i];
226  Gecode::count(home, y, ints, irt, x[4]);
227  }
228  };
229 
231  class IntVarShared : public Test {
232  protected:
235  public:
238  : Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
240  virtual bool solution(const Assignment& x) const {
241  int m = 0;
242  for (int i=0; i<4; i++)
243  if (x[i] == 0)
244  m++;
245  return cmp(m,irt,x[2]);
246  }
248  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
249  Gecode::count(home, x, 0, irt, x[2]);
250  }
251  };
252 
254  class VarVar : public Test {
255  protected:
258  public:
261  : Test("Count::Var::Var::"+str(irt0),5,-2,2), irt(irt0) {}
263  virtual bool solution(const Assignment& x) const {
264  int m = 0;
265  for (int i=0; i<3; i++)
266  if (x[i] == x[3])
267  m++;
268  return cmp(m,irt,x[4]);
269  }
271  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
272  Gecode::IntVarArgs y(3);
273  for (int i=0; i<3; i++)
274  y[i]=x[i];
275  Gecode::count(home, y, x[3], irt, x[4]);
276  }
277  };
278 
280  class VarInt : public Test {
281  protected:
284  public:
287  : Test("Count::Var::Int::"+str(irt0),4,-2,2), irt(irt0) {}
289  virtual bool solution(const Assignment& x) const {
290  int m = 0;
291  for (int i=0; i<3; i++)
292  if (x[i] == x[3])
293  m++;
294  return cmp(m,irt,2);
295  }
297  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
298  Gecode::IntVarArgs y(3);
299  for (int i=0; i<3; i++)
300  y[i]=x[i];
301  Gecode::count(home, y, x[3], irt, 2);
302  }
303  };
304 
306  class VarVarSharedA : public Test {
307  protected:
310  public:
313  : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
315  virtual bool solution(const Assignment& x) const {
316  int m = 0;
317  for (int i=0; i<4; i++)
318  if (x[i] == x[1])
319  m++;
320  return cmp(m,irt,x[4]);
321  }
323  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
324  Gecode::IntVarArgs y(4);
325  for (int i=0; i<4; i++)
326  y[i]=x[i];
327  Gecode::count(home, y, x[1], irt, x[4]);
328  }
329  };
330 
332  class VarVarSharedB : public Test {
333  protected:
336  public:
339  : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
341  virtual bool solution(const Assignment& x) const {
342  int m = 0;
343  for (int i=0; i<4; i++)
344  if (x[i] == x[4])
345  m++;
346  return cmp(m,irt,x[3]);
347  }
349  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
350  Gecode::IntVarArgs y(4);
351  for (int i=0; i<4; i++)
352  y[i]=x[i];
353  Gecode::count(home, y, x[4], irt, x[3]);
354  }
355  };
356 
358  class VarVarSharedC : public Test {
359  protected:
362  public:
365  : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
367  virtual bool solution(const Assignment& x) const {
368  int m = 0;
369  for (int i=0; i<4; i++)
370  if (x[i] == x[1])
371  m++;
372  return cmp(m,irt,x[3]);
373  }
375  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
376  Gecode::count(home, x, x[1], irt, x[3]);
377  }
378  };
379 
381  class Create {
382  public:
384  Create(void) {
385  for (IntRelTypes irts; irts(); ++irts) {
386  (void) new IntInt(irts.irt());
387  (void) new SetInt(irts.irt());
388  (void) new IntIntDup(irts.irt());
389  (void) new IntVar(irts.irt());
390  (void) new SetVar(irts.irt());
391  (void) new IntArrayInt(irts.irt());
392  (void) new IntArrayVar(irts.irt());
393  (void) new IntVarShared(irts.irt());
394  (void) new VarVar(irts.irt());
395  (void) new VarInt(irts.irt());
396  (void) new VarVarSharedA(irts.irt());
397  (void) new VarVarSharedB(irts.irt());
398  (void) new VarVarSharedC(irts.irt());
399  }
400  }
401  };
402 
405 
406  }
407 }}
408 
409 // STATISTICS: test-int
410 
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:323
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:358
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:211
VarVarSharedA(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:312
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:315
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:141
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:375
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:222
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:335
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:248
Test number of several equal integers equal to integer
Definition: count.cpp:179
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:289
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:263
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:153
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:284
VarInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:286
Test number of integers contained in an integer set equal to integer
Definition: count.cpp:74
Test number of equal integers equal to integer
Definition: count.cpp:51
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:332
VarVarSharedC(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:364
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:271
Integer variable array.
Definition: int.hh:741
Test number of several equal integers equal to integer variable
Definition: count.cpp:205
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:133
Test number of equal variables equal to integer
Definition: count.cpp:280
Computation spaces.
Definition: core.hpp:1362
Gecode::IntArgs ints(4, 1, 0, 3, 2)
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:107
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:361
static std::string str(Gecode::ExtensionalPropKind epk)
Map extensional propagation kind to string.
Definition: int.hpp:212
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:309
SetVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:156
Gecode::IntArgs i(4, 1, 2, 3, 4)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:297
Help class to create and register tests.
Definition: count.cpp:381
Create(void)
Perform creation and registration.
Definition: count.cpp:384
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:115
IntRelType
Relation types for integers.
Definition: int.hh:903
IntVarShared(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:237
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:234
IntIntDup(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:104
Iterator for integer relation types.
Definition: int.hh:342
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:159
Test number of equal integers equal to integer variable
Definition: count.cpp:124
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:77
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:257
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:83
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:208
Integer sets.
Definition: int.hh:171
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:185
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:367
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:60
Passing integer variables.
Definition: int.hh:636
Test number of integers contained in an integer set equal to integer variable
Definition: count.cpp:150
Passing integer arguments.
Definition: int.hh:607
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:68
General test support.
Definition: afc.cpp:43
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
VarVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:260
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:188
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:240
Test number of equal variables equal to integer variable
Definition: count.cpp:254
Base class for assignments
Definition: int.hh:63
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:127
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:54
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:214
Test number of equal integers equal to integer variable with sharing
Definition: count.cpp:231
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:167
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:341
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:196
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:182
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:283
VarVarSharedB(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:338
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:101
SetInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:80
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:91
int size(void) const
Return number of variables.
Definition: int.hpp:50
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:130
Test number of equal integers equal to integer with duplicate variables
Definition: count.cpp:98
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:57
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:349
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:306