Generated on Tue Jan 26 2021 00:00:00 for Gecode by doxygen 1.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  * 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 #include <gecode/minimodel.hh>
37 
38 namespace Test { namespace Int {
39 
41  namespace MiniModelCount {
42 
44  std::string expand(Gecode::IntRelType irt) {
45  switch (irt) {
46  case Gecode::IRT_EQ: return "Exactly";
47  case Gecode::IRT_LQ: return "AtMost";
48  case Gecode::IRT_GQ: return "AtLeast";
49  default: GECODE_NEVER;
50  }
52  return "";
53  }
54 
61  class IntInt : public Test {
62  protected:
65  public:
68  : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {}
70  virtual bool solution(const Assignment& x) const {
71  int m = 0;
72  for (int i=x.size(); i--; )
73  if (x[i] == 0)
74  m++;
75  return cmp(m,irt,2);
76  }
78  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
79  switch (irt) {
80  case Gecode::IRT_EQ:
81  Gecode::exactly(home,x,0,2); break;
82  case Gecode::IRT_LQ:
83  Gecode::atmost(home,x,0,2); break;
84  case Gecode::IRT_GQ:
85  Gecode::atleast(home,x,0,2); break;
86  default: GECODE_NEVER;
87  }
88  }
89  };
90 
92  class IntVar : public Test {
93  protected:
96  public:
99  : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {}
101  virtual bool solution(const Assignment& x) const {
102  int m = 0;
103  for (int i=0; i<4; i++)
104  if (x[i] == 0)
105  m++;
106  return cmp(m,irt,x[4]);
107  }
109  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
110  Gecode::IntVarArgs y(4);
111  for (int i=0; i<4; i++)
112  y[i]=x[i];
113  switch (irt) {
114  case Gecode::IRT_EQ:
115  Gecode::exactly(home,y,0,x[4]); break;
116  case Gecode::IRT_LQ:
117  Gecode::atmost(home,y,0,x[4]); break;
118  case Gecode::IRT_GQ:
119  Gecode::atleast(home,y,0,x[4]); break;
120  default: GECODE_NEVER;
121  }
122  }
123  };
124 
126  class VarVar : public Test {
127  protected:
130  public:
133  : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
135  virtual bool solution(const Assignment& x) const {
136  int m = 0;
137  for (int i=0; i<3; i++)
138  if (x[i] == x[3])
139  m++;
140  return cmp(m,irt,x[4]);
141  }
143  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
144  Gecode::IntVarArgs y(3);
145  for (int i=0; i<3; i++)
146  y[i]=x[i];
147  switch (irt) {
148  case Gecode::IRT_EQ:
149  Gecode::exactly(home,y,x[3],x[4]); break;
150  case Gecode::IRT_LQ:
151  Gecode::atmost(home,y,x[3],x[4]); break;
152  case Gecode::IRT_GQ:
153  Gecode::atleast(home,y,x[3],x[4]); break;
154  default: GECODE_NEVER;
155  }
156  }
157  };
158 
160  class VarInt : public Test {
161  protected:
164  public:
167  : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
169  virtual bool solution(const Assignment& x) const {
170  int m = 0;
171  for (int i=0; i<3; i++)
172  if (x[i] == x[3])
173  m++;
174  return cmp(m,irt,2);
175  }
177  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
178  Gecode::IntVarArgs y(3);
179  for (int i=0; i<3; i++)
180  y[i]=x[i];
181  switch (irt) {
182  case Gecode::IRT_EQ:
183  Gecode::exactly(home,y,x[3],2); break;
184  case Gecode::IRT_LQ:
185  Gecode::atmost(home,y,x[3],2); break;
186  case Gecode::IRT_GQ:
187  Gecode::atleast(home,y,x[3],2); break;
188  default: GECODE_NEVER;
189  }
190  }
191  };
192 
193  Gecode::IntArgs ints({1,0,3,2});
194 
196  class IntArrayInt : public Test {
197  protected:
200  public:
203  : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2),
204  irt(irt0) {}
206  virtual bool solution(const Assignment& x) const {
207  int m = 0;
208  for (int i=0; i<4; i++)
209  if (x[i] == ints[i])
210  m++;
211  return cmp(m,irt,2);
212  }
214  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
215  Gecode::IntVarArgs y(4);
216  for (int i=0; i<4; i++)
217  y[i]=x[i];
218  switch (irt) {
219  case Gecode::IRT_EQ:
220  Gecode::exactly(home,y,ints,2); break;
221  case Gecode::IRT_LQ:
222  Gecode::atmost(home,y,ints,2); break;
223  case Gecode::IRT_GQ:
224  Gecode::atleast(home,y,ints,2); break;
225  default: GECODE_NEVER;
226  }
227  }
228  };
229 
231  class IntArrayVar : public Test {
232  protected:
235  public:
238  : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2),
239  irt(irt0) {}
241  virtual bool solution(const Assignment& x) const {
242  int m = 0;
243  for (int i=0; i<4; i++)
244  if (x[i] == ints[i])
245  m++;
246  return cmp(m,irt,x[4]);
247  }
249  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
250  Gecode::IntVarArgs y(4);
251  for (int i=0; i<4; i++)
252  y[i]=x[i];
253  switch (irt) {
254  case Gecode::IRT_EQ:
255  Gecode::exactly(home,y,ints,x[4]); break;
256  case Gecode::IRT_LQ:
257  Gecode::atmost(home,y,ints,x[4]); break;
258  case Gecode::IRT_GQ:
259  Gecode::atleast(home,y,ints,x[4]); break;
260  default: GECODE_NEVER;
261  }
262  }
263  };
264 
266  class Create {
267  public:
269  Create(void) {
270  for (IntRelTypes irts; irts(); ++irts)
271  if ((irts.irt() == Gecode::IRT_EQ) ||
272  (irts.irt() == Gecode::IRT_LQ) ||
273  (irts.irt() == Gecode::IRT_GQ)) {
274  (void) new IntInt(irts.irt());
275  (void) new IntVar(irts.irt());
276  (void) new VarVar(irts.irt());
277  (void) new VarInt(irts.irt());
278  (void) new IntArrayInt(irts.irt());
279  (void) new IntArrayVar(irts.irt());
280  }
281  }
282  };
283 
286 
287  }
288 
289 }}
290 
291 // STATISTICS: test-minimodel
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Passing integer arguments.
Definition: int.hh:628
Passing integer variables.
Definition: int.hh:656
Integer variable array.
Definition: int.hh:763
Computation spaces.
Definition: core.hpp:1742
Base class for assignments
Definition: int.hh:59
Iterator for integer relation types.
Definition: int.hh:368
Help class to create and register tests.
Definition: mm-count.cpp:266
Create(void)
Perform creation and registration.
Definition: mm-count.cpp:269
Test number of several equal integers equal to integer
Definition: mm-count.cpp:196
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:214
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:206
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:199
IntArrayInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:202
Test number of several equal integers equal to integer variable
Definition: mm-count.cpp:231
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:234
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:241
IntArrayVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:237
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:249
Test number of equal integers equal to integer
Definition: mm-count.cpp:61
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:78
IntInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:67
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:70
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:64
Test number of equal integers equal to integer variable
Definition: mm-count.cpp:92
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:95
IntVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:98
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:109
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:101
Test number of equal variables equal to integer
Definition: mm-count.cpp:160
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:177
VarInt(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:166
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:169
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:163
Test number of equal variables equal to integer variable
Definition: mm-count.cpp:126
VarVar(Gecode::IntRelType irt0)
Create and register test.
Definition: mm-count.cpp:132
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: mm-count.cpp:135
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: mm-count.cpp:129
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: mm-count.cpp:143
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:285
IntRelType
Relation types for integers.
Definition: int.hh:925
@ IRT_EQ
Equality ( )
Definition: int.hh:926
@ IRT_GQ
Greater or equal ( )
Definition: int.hh:930
@ IRT_LQ
Less or equal ( )
Definition: int.hh:928
Gecode::IntArgs i({1, 2, 3, 4})
std::string expand(Gecode::IntRelType irt)
Expand relation to abbreviation.
Definition: mm-count.cpp:44
Gecode::IntArgs ints({1, 0, 3, 2})
General test support.
Definition: afc.cpp:39
#define GECODE_NEVER
Assert that this command is never executed.
Definition: macros.hpp:56