Generated on Sat Feb 7 2015 02:01:25 for Gecode by doxygen 1.8.9.1
nvalues.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, 2011
8  *
9  * Last modified:
10  * $Date: 2011-08-17 22:25:49 +0200 (Wed, 17 Aug 2011) $ by $Author: schulte $
11  * $Revision: 12308 $
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 <gecode/minimodel.hh>
39 #include "test/int.hh"
40 
41 namespace Test { namespace Int {
42 
44  namespace NValues {
45 
51  class IntInt : public Test {
53  protected:
57  int m;
58  public:
60  IntInt(int n, int m0, Gecode::IntRelType irt0)
61  : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
62  n,0,n),
63  irt(irt0), m(m0) {
64  testfix = false;
65  if (arity > 5)
66  testsearch = false;
67  }
69  virtual Assignment* assignment(void) const {
70  if (arity > 5)
71  return new RandomAssignment(arity,dom,500);
72  else
73  return new CpltAssignment(arity,dom);
74  }
76  virtual bool solution(const Assignment& x) const {
77  int n = x.size();
78  bool* v = new bool[n+1];
79  for (int i=n+1; i--; )
80  v[i] = false;
81  int k = 0;
82  for (int i=n; i--; )
83  if (!v[x[i]]) {
84  k++;
85  v[x[i]] = true;
86  }
87  delete [] v;
88  return cmp(k,irt,m);
89  }
91  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
92  Gecode::nvalues(home, x, irt, m);
93  }
94  };
95 
97  class IntVar : public Test {
98  protected:
101  public:
104  : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
105  irt(irt0) {
106  testfix = false;
107  }
109  virtual bool solution(const Assignment& x) const {
110  int n = x.size() - 1;
111  bool* v = new bool[n+1];
112  for (int i=n+1; i--; )
113  v[i] = false;
114  int k = 0;
115  for (int i=n; i--; )
116  if (!v[x[i]]) {
117  k++;
118  v[x[i]] = true;
119  }
120  delete [] v;
121  return cmp(k,irt,x[n]);
122  }
124  virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
125  int n = xy.size() - 1;
127  for (int i=n; i--; )
128  x[i] = xy[i];
129  Gecode::nvalues(home, x, irt, xy[n]);
130  }
131  };
132 
134  class BoolInt : public Test {
135  protected:
139  int m;
140  public:
142  BoolInt(int n, int m0, Gecode::IntRelType irt0)
143  : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
144  n,0,2),
145  irt(irt0), m(m0) {}
147  virtual bool solution(const Assignment& x) const {
148  int n = x.size();
149  for (int i=n; i--; )
150  if (x[i] > 1)
151  return false;
152  bool* v = new bool[n+1];
153  for (int i=n+1; i--; )
154  v[i] = false;
155  int k = 0;
156  for (int i=n; i--; )
157  if (!v[x[i]]) {
158  k++;
159  v[x[i]] = true;
160  }
161  delete [] v;
162  return cmp(k,irt,m);
163  }
165  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
166  using namespace Gecode;
167  BoolVarArgs y(x.size());
168  for (int i=x.size(); i--; )
169  y[i] = channel(home, x[i]);
170  nvalues(home, y, irt, m);
171  }
172  };
173 
175  class BoolVar : public Test {
176  protected:
179  public:
182  : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
183  irt(irt0) {}
185  virtual bool solution(const Assignment& x) const {
186  int n = x.size() - 1;
187  for (int i=n; i--; )
188  if (x[i] > 1)
189  return false;
190  bool* v = new bool[n+1];
191  for (int i=n+1; i--; )
192  v[i] = false;
193  int k = 0;
194  for (int i=n; i--; )
195  if (!v[x[i]]) {
196  k++;
197  v[x[i]] = true;
198  }
199  delete [] v;
200  return cmp(k,irt,x[n]);
201  }
203  virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
204  using namespace Gecode;
205  int n = xy.size() - 1;
206  BoolVarArgs x(n);
207  for (int i=n; i--; )
208  x[i] = channel(home, xy[i]);
209  nvalues(home, x, irt, xy[n]);
210  }
211  };
212 
214  class Create {
215  public:
217  Create(void) {
218  for (IntRelTypes irts; irts(); ++irts) {
219  for (int i=1; i<=7; i += 3) {
220  for (int m=0; m<=3; m++)
221  (void) new BoolInt(i, m, irts.irt());
222  (void) new BoolVar(i, irts.irt());
223  }
224  for (int i=1; i<=7; i += 2) {
225  for (int m=0; m<=i+1; m++)
226  (void) new IntInt(i, m, irts.irt());
227  if (i <= 5)
228  (void) new IntVar(i, irts.irt());
229  }
230  }
231  }
232  };
233 
236 
237  }
238 }}
239 
240 // STATISTICS: test-int
241 
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: arithmetic.cpp:218
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:109
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:185
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:284
BoolVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:181
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:147
IntVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:103
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:220
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: nvalues.cpp:69
Integer variable array.
Definition: int.hh:741
Computation spaces.
Definition: core.hpp:1362
Generate random selection of assignments.
Definition: int.hh:100
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition: nvalues.cpp:203
static std::string str(Gecode::ExtensionalPropKind epk)
Map extensional propagation kind to string.
Definition: int.hpp:212
Test number of values of integer variables equal to integer variable
Definition: nvalues.cpp:97
Gecode::IntArgs i(4, 1, 2, 3, 4)
Help class to create and register tests.
Definition: nvalues.cpp:214
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
IntRelType
Relation types for integers.
Definition: int.hh:903
Iterator for integer relation types.
Definition: int.hh:342
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:100
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:178
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: nvalues.cpp:165
Passing integer variables.
Definition: int.hh:636
Passing Boolean variables.
Definition: int.hh:690
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition: nvalues.cpp:124
Test number of values of Boolean variables equal to integer
Definition: nvalues.cpp:134
int m
Number of values.
Definition: nvalues.cpp:139
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:232
void nvalues(Home home, const BoolVarArgs &x, IntRelType irt, IntVar y, IntConLevel)
Post propagator for .
Definition: nvalues.cpp:193
const int v[7]
Definition: distinct.cpp:207
General test support.
Definition: afc.cpp:43
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
bool testsearch
Whether to perform search test.
Definition: int.hh:230
BoolInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:142
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:55
Base class for assignments
Definition: int.hh:63
IntInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:60
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:76
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: nvalues.cpp:91
Test number of values of Boolean variables equal to integer variable
Definition: nvalues.cpp:175
Gecode toplevel namespace
int arity
Number of variables.
Definition: int.hh:218
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:137
Test number of values of integer variables equal to integer
Definition: nvalues.cpp:52
Create(void)
Perform creation and registration.
Definition: nvalues.cpp:217
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntConLevel)
Post propagator for .
Definition: nvalues.cpp:44
int m
Number of values.
Definition: nvalues.cpp:57
int size(void) const
Return number of variables.
Definition: int.hpp:50
Generate all assignments.
Definition: int.hh:83