Generated on Tue Mar 24 2020 14:04:04 for Gecode by doxygen 1.8.17
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  * This file is part of Gecode, the generic constraint
10  * development environment:
11  * http://www.gecode.org
12  *
13  * Permission is hereby granted, free of charge, to any person obtaining
14  * a copy of this software and associated documentation files (the
15  * "Software"), to deal in the Software without restriction, including
16  * without limitation the rights to use, copy, modify, merge, publish,
17  * distribute, sublicense, and/or sell copies of the Software, and to
18  * permit persons to whom the Software is furnished to do so, subject to
19  * the following conditions:
20  *
21  * The above copyright notice and this permission notice shall be
22  * included in all copies or substantial portions of the Software.
23  *
24  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
25  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
26  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
27  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
28  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
29  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
30  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
31  *
32  */
33 
34 #include "test/int.hh"
35 
36 namespace Test { namespace Int {
37 
39  namespace Count {
40 
46  class IntInt : public Test {
48  protected:
51  public:
54  : Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
56  virtual bool solution(const Assignment& x) const {
57  int m = 0;
58  for (int i=x.size(); i--; )
59  if (x[i] == 0)
60  m++;
61  return cmp(m,irt,2);
62  }
64  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
65  Gecode::count(home, x, 0, irt, 2);
66  }
67  };
68 
70  class SetInt : public Test {
71  protected:
74  public:
77  : Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
79  virtual bool solution(const Assignment& x) const {
80  int m = 0;
81  for (int i=x.size(); i--; )
82  if ((x[i] >= -1) && (x[i] <= 1))
83  m++;
84  return cmp(m,irt,2);
85  }
87  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
88  Gecode::IntSet s(-1,1);
89  Gecode::count(home, x, s, irt, 2);
90  }
91  };
92 
94  class IntIntDup : public Test {
95  protected:
98  public:
101  : Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
103  virtual bool solution(const Assignment& x) const {
104  int m = 0;
105  for (int i=x.size(); i--; )
106  if (x[i] == 0)
107  m += 2;
108  return cmp(m,irt,4);
109  }
111  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
113  for (int i=x.size(); i--; )
114  y[i]=y[4+i]=x[i];
115  Gecode::count(home, y, 0, irt, 4);
116  }
117  };
118 
120  class IntVar : public Test {
121  protected:
124  public:
127  : Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
129  virtual bool solution(const Assignment& x) const {
130  int m = 0;
131  for (int i=0; i<4; i++)
132  if (x[i] == 0)
133  m++;
134  return cmp(m,irt,x[4]);
135  }
137  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
139  for (int i=0; i<4; i++)
140  y[i]=x[i];
141  Gecode::count(home, y, 0, irt, x[4]);
142  }
143  };
144 
146  class SetVar : public Test {
147  protected:
150  public:
153  : Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
155  virtual bool solution(const Assignment& x) const {
156  int m = 0;
157  for (int i=0; i<4; i++)
158  if ((x[i] >= -1) && (x[i] <= 1))
159  m++;
160  return cmp(m,irt,x[4]);
161  }
163  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
165  for (int i=0; i<4; i++)
166  y[i]=x[i];
167  Gecode::IntSet s(-1,1);
168  Gecode::count(home, y, s, irt, x[4]);
169  }
170  };
171 
172  Gecode::IntArgs ints({1,0,3,2});
173 
175  class IntArrayInt : public Test {
176  protected:
179  public:
182  : Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
184  virtual bool solution(const Assignment& x) const {
185  int m = 0;
186  for (int i=0; i<4; i++)
187  if (x[i] == ints[i])
188  m++;
189  return cmp(m,irt,2);
190  }
192  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
194  for (int i=0; i<4; i++)
195  y[i]=x[i];
196  Gecode::count(home, y, ints, irt, 2);
197  }
198  };
199 
201  class IntArrayVar : public Test {
202  protected:
205  public:
208  : Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
210  virtual bool solution(const Assignment& x) const {
211  int m = 0;
212  for (int i=0; i<4; i++)
213  if (x[i] == ints[i])
214  m++;
215  return cmp(m,irt,x[4]);
216  }
218  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
220  for (int i=0; i<4; i++)
221  y[i]=x[i];
222  Gecode::count(home, y, ints, irt, x[4]);
223  }
224  };
225 
227  class IntVarShared : public Test {
228  protected:
231  public:
234  : Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
236  virtual bool solution(const Assignment& x) const {
237  int m = 0;
238  for (int i=0; i<4; i++)
239  if (x[i] == 0)
240  m++;
241  return cmp(m,irt,x[2]);
242  }
244  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
245  Gecode::count(home, x, 0, irt, x[2]);
246  }
247  };
248 
250  class VarVar : public Test {
251  protected:
254  public:
257  : Test("Count::Var::Var::"+str(irt0)+"::"+str(ipl),5,-2,2,false,ipl),
258  irt(irt0) {
259  contest = CTL_NONE;
260  }
262  virtual bool solution(const Assignment& x) const {
263  int m = 0;
264  for (int i=0; i<3; i++)
265  if (x[i] == x[3])
266  m++;
267  return cmp(m,irt,x[4]);
268  }
270  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
272  for (int i=0; i<3; i++)
273  y[i]=x[i];
274  Gecode::count(home, y, x[3], irt, x[4], ipl);
275  }
276  };
277 
279  class VarInt : public Test {
280  protected:
283  public:
286  : Test("Count::Var::Int::"+str(irt0)+"::"+str(ipl),4,-2,2,false,ipl),
287  irt(irt0) {
288  contest = CTL_NONE;
289  }
291  virtual bool solution(const Assignment& x) const {
292  int m = 0;
293  for (int i=0; i<3; i++)
294  if (x[i] == x[3])
295  m++;
296  return cmp(m,irt,2);
297  }
299  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
301  for (int i=0; i<3; i++)
302  y[i]=x[i];
303  Gecode::count(home, y, x[3], irt, 2, ipl);
304  }
305  };
306 
308  class VarVarSharedA : public Test {
309  protected:
312  public:
315  : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
317  virtual bool solution(const Assignment& x) const {
318  int m = 0;
319  for (int i=0; i<4; i++)
320  if (x[i] == x[1])
321  m++;
322  return cmp(m,irt,x[4]);
323  }
325  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
327  for (int i=0; i<4; i++)
328  y[i]=x[i];
329  Gecode::count(home, y, x[1], irt, x[4]);
330  }
331  };
332 
334  class VarVarSharedB : public Test {
335  protected:
338  public:
341  : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
343  virtual bool solution(const Assignment& x) const {
344  int m = 0;
345  for (int i=0; i<4; i++)
346  if (x[i] == x[4])
347  m++;
348  return cmp(m,irt,x[3]);
349  }
351  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
353  for (int i=0; i<4; i++)
354  y[i]=x[i];
355  Gecode::count(home, y, x[4], irt, x[3]);
356  }
357  };
358 
360  class VarVarSharedC : public Test {
361  protected:
364  public:
367  : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
369  virtual bool solution(const Assignment& x) const {
370  int m = 0;
371  for (int i=0; i<4; i++)
372  if (x[i] == x[1])
373  m++;
374  return cmp(m,irt,x[3]);
375  }
377  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
378  Gecode::count(home, x, x[1], irt, x[3]);
379  }
380  };
381 
383  class Create {
384  public:
386  Create(void) {
387  for (IntRelTypes irts; irts(); ++irts) {
388  (void) new IntInt(irts.irt());
389  (void) new SetInt(irts.irt());
390  (void) new IntIntDup(irts.irt());
391  (void) new IntVar(irts.irt());
392  (void) new SetVar(irts.irt());
393  (void) new IntArrayInt(irts.irt());
394  (void) new IntArrayVar(irts.irt());
395  (void) new IntVarShared(irts.irt());
396  (void) new VarVar(irts.irt(), Gecode::IPL_BND);
397  (void) new VarVar(irts.irt(), Gecode::IPL_DOM);
398  (void) new VarInt(irts.irt(), Gecode::IPL_BND);
399  (void) new VarInt(irts.irt(), Gecode::IPL_DOM);
400  (void) new VarVarSharedA(irts.irt());
401  (void) new VarVarSharedB(irts.irt());
402  (void) new VarVarSharedC(irts.irt());
403  }
404  }
405  };
406 
409 
410  }
411 }}
412 
413 // STATISTICS: test-int
414 
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:56
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:163
IntRelType
Relation types for integers.
Definition: int.hh:925
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:291
Iterator for integer relation types.
Definition: int.hh:368
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
ConTestLevel contest
Whether to test for certain consistency.
Definition: int.hh:236
Test number of several equal integers equal to integer
Definition: count.cpp:175
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:79
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:351
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:129
Test number of equal integers equal to integer with duplicate variables
Definition: count.cpp:94
Gecode::IntPropLevel ipl
Propagation level.
Definition: int.hh:234
IntVarShared(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:233
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:334
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:97
Passing integer variables.
Definition: int.hh:656
VarVarSharedC(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:366
Test number of several equal integers equal to integer variable
Definition: count.cpp:201
@ CTL_NONE
No consistency-test.
Definition: int.hh:140
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:111
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:126
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:218
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:53
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:149
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Computation spaces.
Definition: core.hpp:1742
Integer variable array.
Definition: int.hh:763
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:299
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:285
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:262
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:253
Create(void)
Perform creation and registration.
Definition: count.cpp:386
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:184
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:308
VarVarSharedB(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:340
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:204
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:363
Integer sets.
Definition: int.hh:174
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:230
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:73
Test number of equal integers equal to integer
Definition: count.cpp:47
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Help class to create and register tests.
Definition: count.cpp:383
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:210
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:236
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:181
Test number of equal variables equal to integer variable with sharing
Definition: count.cpp:360
Test number of integers contained in an integer set equal to integer
Definition: count.cpp:70
@ IPL_DOM
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
VarInt(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
Create and register test.
Definition: count.cpp:285
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:369
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:311
Test number of integers contained in an integer set equal to integer variable
Definition: count.cpp:146
Test number of equal variables equal to integer
Definition: count.cpp:279
VarVarSharedA(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:314
Test number of equal variables equal to integer variable
Definition: count.cpp:250
Test number of equal integers equal to integer variable
Definition: count.cpp:120
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:377
@ IPL_BND
Bounds propagation.
Definition: int.hh:978
IntIntDup(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:100
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:282
Base class for assignments
Definition: int.hh:59
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:317
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:325
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:40
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:244
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:270
SetInt(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:76
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:87
Create c
Definition: count.cpp:407
VarVar(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
Create and register test.
Definition: count.cpp:256
General test support.
Definition: afc.cpp:39
Gecode::IntArgs ints({1, 0, 3, 2})
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:50
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:103
Passing integer arguments.
Definition: int.hh:628
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:64
Gecode::IntArgs i({1, 2, 3, 4})
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:192
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:337
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:178
SetVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:152
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:155
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition: count.cpp:207
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: count.cpp:343
Test number of equal integers equal to integer variable with sharing
Definition: count.cpp:227
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: count.cpp:123
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: count.cpp:137
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:209