Generated on Tue Feb 11 2025 17:33:26 for Gecode by doxygen 1.12.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.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
37namespace Test { namespace Int {
38
40 namespace NValues {
41
48 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
int n
Number of negative literals for node type.
Node * x
Pointer to corresponding Boolean expression node.
int size(void) const
Return size of array (number of elements)
Definition array.hpp:1607
Passing Boolean variables.
Definition int.hh:712
Passing integer variables.
Definition int.hh:656
Integer variable array.
Definition int.hh:763
Computation spaces.
Definition core.hpp:1742
int size(void) const
Return size of array (number of elements)
Definition array.hpp:926
Base class for assignments
Definition int.hh:59
Generate all assignments.
Definition int.hh:79
Iterator for integer relation types.
Definition int.hh:368
Test number of values of Boolean variables equal to integer
Definition nvalues.cpp:130
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:161
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:143
BoolInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:138
int m
Number of values.
Definition nvalues.cpp:135
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:133
Test number of values of Boolean variables equal to integer variable
Definition nvalues.cpp:171
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:174
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:181
BoolVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:177
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:199
Help class to create and register tests.
Definition nvalues.cpp:210
Create(void)
Perform creation and registration.
Definition nvalues.cpp:213
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:72
int m
Number of values.
Definition nvalues.cpp:53
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:51
IntInt(int n, int m0, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:56
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition nvalues.cpp:87
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition nvalues.cpp:65
Test number of values of integer variables equal to integer variable
Definition nvalues.cpp:93
Gecode::IntRelType irt
Integer relation type to propagate.
Definition nvalues.cpp:96
virtual bool solution(const Assignment &x) const
Test whether x is solution
Definition nvalues.cpp:105
IntVar(int n, Gecode::IntRelType irt0)
Create and register test.
Definition nvalues.cpp:99
virtual void post(Gecode::Space &home, Gecode::IntVarArray &xy)
Post constraint on xy.
Definition nvalues.cpp:120
Generate random selection of assignments.
Definition int.hh:96
bool testsearch
Whether to perform search test.
Definition int.hh:238
bool testfix
Whether to perform fixpoint test.
Definition int.hh:240
int arity
Number of variables.
Definition int.hh:226
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition int.hpp:209
static bool cmp(T x, Gecode::IntRelType r, T y)
Compare x and y with respect to r.
Definition int.hpp:285
Gecode::IntSet dom
Domain of variables.
Definition int.hh:228
IntRelType
Relation types for integers.
Definition int.hh:925
Gecode toplevel namespace
void nvalues(Home home, const IntVarArgs &x, IntRelType irt, int y, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition nvalues.cpp:40
General test support.
Definition afc.cpp:39