Generated on Wed Jan 1 2020 10:37:59 for Gecode by doxygen 1.8.16
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  * 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 <gecode/minimodel.hh>
35 #include "test/int.hh"
36 
37 namespace Test { namespace Int {
38 
40  namespace NValues {
41 
47  class IntInt : public Test {
49  protected:
53  int m;
54  public:
56  IntInt(int n, int m0, Gecode::IntRelType irt0)
57  : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
58  n,0,n),
59  irt(irt0), m(m0) {
60  testfix = false;
61  if (arity > 5)
62  testsearch = false;
63  }
65  virtual Assignment* assignment(void) const {
66  if (arity > 5)
67  return new RandomAssignment(arity,dom,500);
68  else
69  return new CpltAssignment(arity,dom);
70  }
72  virtual bool solution(const Assignment& x) const {
73  int n = x.size();
74  bool* v = new bool[n+1];
75  for (int i=n+1; i--; )
76  v[i] = false;
77  int k = 0;
78  for (int i=n; i--; )
79  if (!v[x[i]]) {
80  k++;
81  v[x[i]] = true;
82  }
83  delete [] v;
84  return cmp(k,irt,m);
85  }
87  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
88  Gecode::nvalues(home, x, irt, m);
89  }
90  };
91 
93  class IntVar : public Test {
94  protected:
97  public:
100  : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
101  irt(irt0) {
102  testfix = false;
103  }
105  virtual bool solution(const Assignment& x) const {
106  int n = x.size() - 1;
107  bool* v = new bool[n+1];
108  for (int i=n+1; i--; )
109  v[i] = false;
110  int k = 0;
111  for (int i=n; i--; )
112  if (!v[x[i]]) {
113  k++;
114  v[x[i]] = true;
115  }
116  delete [] v;
117  return cmp(k,irt,x[n]);
118  }
120  virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
121  int n = xy.size() - 1;
123  for (int i=n; i--; )
124  x[i] = xy[i];
125  Gecode::nvalues(home, x, irt, xy[n]);
126  }
127  };
128 
130  class BoolInt : public Test {
131  protected:
135  int m;
136  public:
138  BoolInt(int n, int m0, Gecode::IntRelType irt0)
139  : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
140  n,0,2),
141  irt(irt0), m(m0) {}
143  virtual bool solution(const Assignment& x) const {
144  int n = x.size();
145  for (int i=n; i--; )
146  if (x[i] > 1)
147  return false;
148  bool* v = new bool[n+1];
149  for (int i=n+1; i--; )
150  v[i] = false;
151  int k = 0;
152  for (int i=n; i--; )
153  if (!v[x[i]]) {
154  k++;
155  v[x[i]] = true;
156  }
157  delete [] v;
158  return cmp(k,irt,m);
159  }
161  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
162  using namespace Gecode;
163  BoolVarArgs y(x.size());
164  for (int i=x.size(); i--; )
165  y[i] = channel(home, x[i]);
166  nvalues(home, y, irt, m);
167  }
168  };
169 
171  class BoolVar : public Test {
172  protected:
175  public:
178  : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
179  irt(irt0) {}
181  virtual bool solution(const Assignment& x) const {
182  int n = x.size() - 1;
183  for (int i=n; i--; )
184  if (x[i] > 1)
185  return false;
186  bool* v = new bool[n+1];
187  for (int i=n+1; i--; )
188  v[i] = false;
189  int k = 0;
190  for (int i=n; i--; )
191  if (!v[x[i]]) {
192  k++;
193  v[x[i]] = true;
194  }
195  delete [] v;
196  return cmp(k,irt,x[n]);
197  }
199  virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
200  using namespace Gecode;
201  int n = xy.size() - 1;
202  BoolVarArgs x(n);
203  for (int i=n; i--; )
204  x[i] = channel(home, xy[i]);
205  nvalues(home, x, irt, xy[n]);
206  }
207  };
208 
210  class Create {
211  public:
213  Create(void) {
214  for (IntRelTypes irts; irts(); ++irts) {
215  for (int i=1; i<=7; i += 3) {
216  for (int m=0; m<=3; m++)
217  (void) new BoolInt(i, m, irts.irt());
218  (void) new BoolVar(i, irts.irt());
219  }
220  for (int i=1; i<=7; i += 2) {
221  for (int m=0; m<=i+1; m++)
222  (void) new IntInt(i, m, irts.irt());
223  if (i <= 5)
224  (void) new IntVar(i, irts.irt());
225  }
226  }
227  }
228  };
229 
232 
233  }
234 }}
235 
236 // STATISTICS: test-int
237 
Create(void)
Perform creation and registration.
Definition: nvalues.cpp:213
Test number of values of integer variables equal to integer variable
Definition: nvalues.cpp:93
IntRelType
Relation types for integers.
Definition: int.hh:925
Iterator for integer relation types.
Definition: int.hh:368
Post propagator for SetVar SetOpType SetVar y
Definition: set.hh:767
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:143
bool testsearch
Whether to perform search test.
Definition: int.hh:238
IntInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:56
Generate random selection of assignments.
Definition: int.hh:96
Passing integer variables.
Definition: int.hh:656
Computation spaces.
Definition: core.hpp:1742
Integer variable array.
Definition: int.hh:763
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition: int.hpp:285
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:133
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:926
void nvalues(Home home, const BoolVarArgs &x, IntRelType irt, IntVar y, IntPropLevel)
Post propagator for .
Definition: nvalues.cpp:189
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel)
Post propagator for .
Definition: nvalues.cpp:40
int m
Number of values.
Definition: nvalues.cpp:135
int m
Number of values.
Definition: nvalues.cpp:53
IntVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:99
Gecode toplevel namespace
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:240
Create c
Definition: nvalues.cpp:230
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:51
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Passing Boolean variables.
Definition: int.hh:712
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:174
Test number of values of integer variables equal to integer
Definition: nvalues.cpp:48
Gecode::IntRelType irt
Integer relation type to propagate.
Definition: nvalues.cpp:96
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: nvalues.cpp:87
BoolInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:138
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: nvalues.cpp:65
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:72
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:105
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: nvalues.cpp:161
int arity
Number of variables.
Definition: int.hh:226
Generate all assignments.
Definition: int.hh:79
Test number of values of Boolean variables equal to integer
Definition: nvalues.cpp:130
Base class for assignments
Definition: int.hh:59
const int v[7]
Definition: distinct.cpp:259
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: channel.cpp:41
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition: nvalues.cpp:120
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition: nvalues.cpp:181
General test support.
Definition: afc.cpp:39
Help class to create and register tests.
Definition: nvalues.cpp:210
BoolVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition: nvalues.cpp:177
Test number of values of Boolean variables equal to integer variable
Definition: nvalues.cpp:171
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Gecode::IntArgs i({1, 2, 3, 4})
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition: nvalues.cpp:199
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:228
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:209