Generated on for Gecode by doxygen 1.17.0
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.dev>
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.dev
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
37namespace Test { namespace Int {
38
40 namespace NValues {
41
47
48 class IntInt : public Test {
49 protected:
53 int m;
54 public:
56 IntInt(TestTags tags, int n, int m0, Gecode::IntRelType irt0)
57 : Test(tags,
58 "NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
59 n,0,n),
60 irt(irt0), m(m0) {
61 testfix = false;
62 if (arity > 5)
63 testsearch = false;
64 }
65
66 virtual Assignment* assignment(void) const {
67 if (arity > 5)
68 return new RandomAssignment(arity, dom, 500, _rand);
69 else
70 return new CpltAssignment(arity,dom);
71 }
72
73 virtual bool solution(const Assignment& x) const {
74 int n = x.size();
75 bool* v = new bool[n+1];
76 for (int i=n+1; i--; )
77 v[i] = false;
78 int k = 0;
79 for (int i=n; i--; )
80 if (!v[x[i]]) {
81 k++;
82 v[x[i]] = true;
83 }
84 delete [] v;
85 return cmp(k,irt,m);
86 }
87
88 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
89 Gecode::nvalues(home, x, irt, m);
90 }
91 };
92
94 class IntVar : public Test {
95 protected:
98 public:
101 : Test(tags,
102 "NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
103 irt(irt0) {
104 testfix = false;
105 }
106
107 virtual bool solution(const Assignment& x) const {
108 int n = x.size() - 1;
109 bool* v = new bool[n+1];
110 for (int i=n+1; i--; )
111 v[i] = false;
112 int k = 0;
113 for (int i=n; i--; )
114 if (!v[x[i]]) {
115 k++;
116 v[x[i]] = true;
117 }
118 delete [] v;
119 return cmp(k,irt,x[n]);
120 }
121
122 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
123 int n = xy.size() - 1;
125 for (int i=n; i--; )
126 x[i] = xy[i];
127 Gecode::nvalues(home, x, irt, xy[n]);
128 }
129 };
130
132 class BoolInt : public Test {
133 protected:
137 int m;
138 public:
140 BoolInt(int n, int m0, Gecode::IntRelType irt0)
141 : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
142 n,0,2),
143 irt(irt0), m(m0) {}
144
145 virtual bool solution(const Assignment& x) const {
146 int n = x.size();
147 for (int i=n; i--; )
148 if (x[i] > 1)
149 return false;
150 bool* v = new bool[n+1];
151 for (int i=n+1; i--; )
152 v[i] = false;
153 int k = 0;
154 for (int i=n; i--; )
155 if (!v[x[i]]) {
156 k++;
157 v[x[i]] = true;
158 }
159 delete [] v;
160 return cmp(k,irt,m);
161 }
162
163 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
164 using namespace Gecode;
165 BoolVarArgs y(x.size());
166 for (int i=x.size(); i--; )
167 y[i] = channel(home, x[i]);
168 nvalues(home, y, irt, m);
169 }
170 };
171
173 class BoolVar : public Test {
174 protected:
177 public:
180 : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
181 irt(irt0) {}
182
183 virtual bool solution(const Assignment& x) const {
184 int n = x.size() - 1;
185 for (int i=n; i--; )
186 if (x[i] > 1)
187 return false;
188 bool* v = new bool[n+1];
189 for (int i=n+1; i--; )
190 v[i] = false;
191 int k = 0;
192 for (int i=n; i--; )
193 if (!v[x[i]]) {
194 k++;
195 v[x[i]] = true;
196 }
197 delete [] v;
198 return cmp(k,irt,x[n]);
199 }
200
201 virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
202 using namespace Gecode;
203 int n = xy.size() - 1;
204 BoolVarArgs x(n);
205 for (int i=n; i--; )
206 x[i] = channel(home, xy[i]);
207 nvalues(home, x, irt, xy[n]);
208 }
209 };
210
212 class Create {
213 public:
215 Create(void) {
216 for (IntRelTypes irts; irts(); ++irts) {
217 for (int i=1; i<=7; i += 3) {
218 for (int m=0; m<=3; m++)
219 (void) new BoolInt(i, m, irts.irt());
220 (void) new BoolVar(i, irts.irt());
221 }
222 for (int i=1; i<=7; i += 2) {
223 for (int m=0; m<=i+1; m++) {
224 TestTags tags = ((i == 3) && (m == 2) &&
225 (irts.irt() == Gecode::IRT_EQ))
227 (void) new IntInt(tags,i,m,irts.irt());
228 }
229 if (i <= 5) {
230 TestTags tags = ((i == 3) &&
231 (irts.irt() == Gecode::IRT_EQ))
233 (void) new IntVar(tags,i,irts.irt());
234 }
235 }
236 }
237 }
238 };
239
242
243 }
244}}
245
246// STATISTICS: test-int
Passing Boolean variables.
Definition int.hh:739
Passing integer variables.
Definition int.hh:681
Integer variable array.
Definition int.hh:792
Computation spaces.
Definition core.hpp:1787
int size(void) const
Return size of array (number of elements).
Definition array.hpp:945
Gecode::Support::RandomGenerator _rand
Random number generator.
Definition test.hh:238
TestTags tags(void) const
Return tags for test.
Definition test.hpp:95
Base class for assignments
Definition int.hh:73
int size(void) const
Return number of variables.
Definition int.hpp:46
Generate all assignments.
Definition int.hh:93
Iterator for integer relation types.
Definition int.hh:466
Test number of values of Boolean variables equal to integer
Definition nvalues.cpp:132
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:163
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:145
BoolInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:140
int m
Number of values.
Definition nvalues.cpp:137
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:135
Test number of values of Boolean variables equal to integer variable
Definition nvalues.cpp:173
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:176
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:183
BoolVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:179
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:201
Help class to create and register tests.
Definition nvalues.cpp:212
Create(void)
Perform creation and registration.
Definition nvalues.cpp:215
Test number of values of integer variables equal to integer
Definition nvalues.cpp:48
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:73
int m
Number of values.
Definition nvalues.cpp:53
IntInt(TestTags tags, int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:56
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:51
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:88
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition nvalues.cpp:66
Test number of values of integer variables equal to integer variable
Definition nvalues.cpp:94
IntVar(TestTags tags, int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:100
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:97
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:107
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:122
Generate random selection of assignments.
Definition int.hh:110
bool testsearch
Whether to perform search test.
Definition int.hh:285
static std::string str(bool b)
Map bool to string.
Definition int.hpp:278
bool testfix
Whether to perform fixpoint test.
Definition int.hh:287
int arity
Number of variables.
Definition int.hh:273
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition int.hpp:296
Gecode::IntSet dom
Domain of variables.
Definition int.hh:275
Set of test tags.
Definition test.hh:105
static TestTags sweep(void)
Expensive parameter sweeps.
Definition test.hpp:59
static TestTags standard(void)
Standard suite without expensive sweeps.
Definition test.hpp:55
IntRelType
Relation types for integers.
Definition int.hh:1225
@ IRT_EQ
Equality ( ).
Definition int.hh:1226
Gecode toplevel namespace
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition channel.cpp:41
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition nvalues.cpp:40
Tests for number of values constraints
Definition nvalues.cpp:40
Testing finite domain integers.
Definition int.cpp:41
General test support.
Definition afc.cpp:41