Generated on Thu Mar 13 2014 04:39:36 for Gecode by doxygen 1.8.1.2
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 20:35:31 +1000 (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 
65  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