Generated on Sat Feb 7 2015 02:01:32 for Gecode by doxygen 1.8.9.1
mm-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, 2008
8  *
9  * Last modified:
10  * $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $
11  * $Revision: 10684 $
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 #include <gecode/minimodel.hh>
41 
42 namespace Test { namespace Int {
43 
45  namespace MiniModelCount {
46 
48  std::string expand(Gecode::IntRelType irt) {
49  switch (irt) {
50  case Gecode::IRT_EQ: return "Exactly";
51  case Gecode::IRT_LQ: return "AtMost";
52  case Gecode::IRT_GQ: return "AtLeast";
53  default: GECODE_NEVER;
54  }
56  return "";
57  }
58 
64  class IntInt : public Test {
66  protected:
69  public:
72  : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {}
74  virtual bool solution(const Assignment& x) const {
75  int m = 0;
76  for (int i=x.size(); i--; )
77  if (x[i] == 0)
78  m++;
79  return cmp(m,irt,2);
80  }
82  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
83  switch (irt) {
84  case Gecode::IRT_EQ:
85  Gecode::exactly(home,x,0,2); break;
86  case Gecode::IRT_LQ:
87  Gecode::atmost(home,x,0,2); break;
88  case Gecode::IRT_GQ:
89  Gecode::atleast(home,x,0,2); break;
90  default: GECODE_NEVER;
91  }
92  }
93  };
94 
96  class IntVar : public Test {
97  protected:
100  public:
103  : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {}
105  virtual bool solution(const Assignment& x) const {
106  int m = 0;
107  for (int i=0; i<4; i++)
108  if (x[i] == 0)
109  m++;
110  return cmp(m,irt,x[4]);
111  }
113  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
114  Gecode::IntVarArgs y(4);
115  for (int i=0; i<4; i++)
116  y[i]=x[i];
117  switch (irt) {
118  case Gecode::IRT_EQ:
119  Gecode::exactly(home,y,0,x[4]); break;
120  case Gecode::IRT_LQ:
121  Gecode::atmost(home,y,0,x[4]); break;
122  case Gecode::IRT_GQ:
123  Gecode::atleast(home,y,0,x[4]); break;
124  default: GECODE_NEVER;
125  }
126  }
127  };
128 
130  class VarVar : public Test {
131  protected:
134  public:
137  : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
139  virtual bool solution(const Assignment& x) const {
140  int m = 0;
141  for (int i=0; i<3; i++)
142  if (x[i] == x[3])
143  m++;
144  return cmp(m,irt,x[4]);
145  }
147  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
148  Gecode::IntVarArgs y(3);
149  for (int i=0; i<3; i++)
150  y[i]=x[i];
151  switch (irt) {
152  case Gecode::IRT_EQ:
153  Gecode::exactly(home,y,x[3],x[4]); break;
154  case Gecode::IRT_LQ:
155  Gecode::atmost(home,y,x[3],x[4]); break;
156  case Gecode::IRT_GQ:
157  Gecode::atleast(home,y,x[3],x[4]); break;
158  default: GECODE_NEVER;
159  }
160  }
161  };
162 
164  class VarInt : public Test {
165  protected:
168  public:
171  : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
173  virtual bool solution(const Assignment& x) const {
174  int m = 0;
175  for (int i=0; i<3; i++)
176  if (x[i] == x[3])
177  m++;
178  return cmp(m,irt,2);
179  }
181  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
182  Gecode::IntVarArgs y(3);
183  for (int i=0; i<3; i++)
184  y[i]=x[i];
185  switch (irt) {
186  case Gecode::IRT_EQ:
187  Gecode::exactly(home,y,x[3],2); break;
188  case Gecode::IRT_LQ:
189  Gecode::atmost(home,y,x[3],2); break;
190  case Gecode::IRT_GQ:
191  Gecode::atleast(home,y,x[3],2); break;
192  default: GECODE_NEVER;
193  }
194  }
195  };
196 
197  Gecode::IntArgs ints(4, 1,0,3,2);
198 
200  class IntArrayInt : public Test {
201  protected:
204  public:
207  : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2),
208  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,2);
216  }
218  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
219  Gecode::IntVarArgs y(4);
220  for (int i=0; i<4; i++)
221  y[i]=x[i];
222  switch (irt) {
223  case Gecode::IRT_EQ:
224  Gecode::exactly(home,y,ints,2); break;
225  case Gecode::IRT_LQ:
226  Gecode::atmost(home,y,ints,2); break;
227  case Gecode::IRT_GQ:
228  Gecode::atleast(home,y,ints,2); break;
229  default: GECODE_NEVER;
230  }
231  }
232  };
233 
235  class IntArrayVar : public Test {
236  protected:
239  public:
242  : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2),
243  irt(irt0) {}
245  virtual bool solution(const Assignment& x) const {
246  int m = 0;
247  for (int i=0; i<4; i++)
248  if (x[i] == ints[i])
249  m++;
250  return cmp(m,irt,x[4]);
251  }
253  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
254  Gecode::IntVarArgs y(4);
255  for (int i=0; i<4; i++)
256  y[i]=x[i];
257  switch (irt) {
258  case Gecode::IRT_EQ:
259  Gecode::exactly(home,y,ints,x[4]); break;
260  case Gecode::IRT_LQ:
261  Gecode::atmost(home,y,ints,x[4]); break;
262  case Gecode::IRT_GQ:
263  Gecode::atleast(home,y,ints,x[4]); break;
264  default: GECODE_NEVER;
265  }
266  }
267  };
268 
270  class Create {
271  public:
273  Create(void) {
274  for (IntRelTypes irts; irts(); ++irts)
275  if ((irts.irt() == Gecode::IRT_EQ) ||
276  (irts.irt() == Gecode::IRT_LQ) ||
277  (irts.irt() == Gecode::IRT_GQ)) {
278  (void) new IntInt(irts.irt());
279  (void) new IntVar(irts.irt());
280  (void) new VarVar(irts.irt());
281  (void) new VarInt(irts.irt());
282  (void) new IntArrayInt(irts.irt());
283  (void) new IntArrayVar(irts.irt());
284  }
285  }
286  };
287 
290 
291  }
292 
293 }}
294 
295 // STATISTICS: test-minimodel
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:181
Test number of several equal integers equal to integer
Definition: mm-count.cpp:200
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:203
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:238
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:284
Less or equal ( )
Definition: int.hh:906
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:210
void atmost(Home home, const IntVarArgs &x, int n, int m, IntConLevel icl=ICL_DEF)
Post constraint .
Definition: minimodel.hh:1675
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:218
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:102
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:253
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:167
Test number of equal integers equal to integer variable
Definition: mm-count.cpp:96
Integer variable array.
Definition: int.hh:741
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:245
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:105
Computation spaces.
Definition: core.hpp:1362
Greater or equal ( )
Definition: int.hh:908
void atleast(Home home, const IntVarArgs &x, int n, int m, IntConLevel icl=ICL_DEF)
Post constraint .
Definition: minimodel.hh:1736
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:82
std::string expand(Gecode::IntRelType irt)
Expand relation to abbreviation.
Definition: mm-count.cpp:48
Gecode::IntArgs ints(4, 1, 0, 3, 2)
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:74
Gecode::IntArgs i(4, 1, 2, 3, 4)
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:71
Equality ( )
Definition: int.hh:904
VarInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:170
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:133
IntRelType
Relation types for integers.
Definition: int.hh:903
Iterator for integer relation types.
Definition: int.hh:342
Create(void)
Perform creation and registration.
Definition: mm-count.cpp:273
Passing integer variables.
Definition: int.hh:636
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:113
Passing integer arguments.
Definition: int.hh:607
void exactly(Home home, const IntVarArgs &x, int n, int m, IntConLevel icl=ICL_DEF)
Post constraint .
Definition: minimodel.hh:1797
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:99
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:147
General test support.
Definition: afc.cpp:43
Test number of several equal integers equal to integer variable
Definition: mm-count.cpp:235
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Test number of equal variables equal to integer
Definition: mm-count.cpp:164
Help class to create and register tests.
Definition: mm-count.cpp:270
Base class for assignments
Definition: int.hh:63
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:206
VarVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:136
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:173
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:139
Test number of equal variables equal to integer variable
Definition: mm-count.cpp:130
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:60
int size(void) const
Return number of variables.
Definition: int.hpp:50
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:241
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:68
Test number of equal integers equal to integer
Definition: mm-count.cpp:65