this repo has no description
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 * 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 "test/int.hh"
35
36#include <gecode/minimodel.hh>
37
38namespace Test { namespace Int {
39
40 /// %Tests for minimal modelling constraints (counting)
41 namespace MiniModelCount {
42
43 /// Expand relation to abbreviation
44 std::string expand(Gecode::IntRelType irt) {
45 switch (irt) {
46 case Gecode::IRT_EQ: return "Exactly";
47 case Gecode::IRT_LQ: return "AtMost";
48 case Gecode::IRT_GQ: return "AtLeast";
49 default: GECODE_NEVER;
50 }
51 GECODE_NEVER;
52 return "";
53 }
54
55 /**
56 * \defgroup TaskTestIntMiniModelCount Minimal modelling constraints (counting)
57 * \ingroup TaskTestInt
58 */
59 //@{
60 /// %Test number of equal integers equal to integer
61 class IntInt : public Test {
62 protected:
63 /// Integer relation type to propagate
64 Gecode::IntRelType irt;
65 public:
66 /// Create and register test
67 IntInt(Gecode::IntRelType irt0)
68 : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {}
69 /// %Test whether \a x is solution
70 virtual bool solution(const Assignment& x) const {
71 int m = 0;
72 for (int i=x.size(); i--; )
73 if (x[i] == 0)
74 m++;
75 return cmp(m,irt,2);
76 }
77 /// Post constraint on \a x
78 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
79 switch (irt) {
80 case Gecode::IRT_EQ:
81 Gecode::exactly(home,x,0,2); break;
82 case Gecode::IRT_LQ:
83 Gecode::atmost(home,x,0,2); break;
84 case Gecode::IRT_GQ:
85 Gecode::atleast(home,x,0,2); break;
86 default: GECODE_NEVER;
87 }
88 }
89 };
90
91 /// %Test number of equal integers equal to integer variable
92 class IntVar : public Test {
93 protected:
94 /// Integer relation type to propagate
95 Gecode::IntRelType irt;
96 public:
97 /// Create and register test
98 IntVar(Gecode::IntRelType irt0)
99 : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {}
100 /// %Test whether \a x is solution
101 virtual bool solution(const Assignment& x) const {
102 int m = 0;
103 for (int i=0; i<4; i++)
104 if (x[i] == 0)
105 m++;
106 return cmp(m,irt,x[4]);
107 }
108 /// Post constraint on \a x
109 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
110 Gecode::IntVarArgs y(4);
111 for (int i=0; i<4; i++)
112 y[i]=x[i];
113 switch (irt) {
114 case Gecode::IRT_EQ:
115 Gecode::exactly(home,y,0,x[4]); break;
116 case Gecode::IRT_LQ:
117 Gecode::atmost(home,y,0,x[4]); break;
118 case Gecode::IRT_GQ:
119 Gecode::atleast(home,y,0,x[4]); break;
120 default: GECODE_NEVER;
121 }
122 }
123 };
124
125 /// %Test number of equal variables equal to integer variable
126 class VarVar : public Test {
127 protected:
128 /// Integer relation type to propagate
129 Gecode::IntRelType irt;
130 public:
131 /// Create and register test
132 VarVar(Gecode::IntRelType irt0)
133 : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
134 /// %Test whether \a x is solution
135 virtual bool solution(const Assignment& x) const {
136 int m = 0;
137 for (int i=0; i<3; i++)
138 if (x[i] == x[3])
139 m++;
140 return cmp(m,irt,x[4]);
141 }
142 /// Post constraint on \a x
143 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
144 Gecode::IntVarArgs y(3);
145 for (int i=0; i<3; i++)
146 y[i]=x[i];
147 switch (irt) {
148 case Gecode::IRT_EQ:
149 Gecode::exactly(home,y,x[3],x[4]); break;
150 case Gecode::IRT_LQ:
151 Gecode::atmost(home,y,x[3],x[4]); break;
152 case Gecode::IRT_GQ:
153 Gecode::atleast(home,y,x[3],x[4]); break;
154 default: GECODE_NEVER;
155 }
156 }
157 };
158
159 /// %Test number of equal variables equal to integer
160 class VarInt : public Test {
161 protected:
162 /// Integer relation type to propagate
163 Gecode::IntRelType irt;
164 public:
165 /// Create and register test
166 VarInt(Gecode::IntRelType irt0)
167 : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
168 /// %Test whether \a x is solution
169 virtual bool solution(const Assignment& x) const {
170 int m = 0;
171 for (int i=0; i<3; i++)
172 if (x[i] == x[3])
173 m++;
174 return cmp(m,irt,2);
175 }
176 /// Post constraint on \a x
177 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
178 Gecode::IntVarArgs y(3);
179 for (int i=0; i<3; i++)
180 y[i]=x[i];
181 switch (irt) {
182 case Gecode::IRT_EQ:
183 Gecode::exactly(home,y,x[3],2); break;
184 case Gecode::IRT_LQ:
185 Gecode::atmost(home,y,x[3],2); break;
186 case Gecode::IRT_GQ:
187 Gecode::atleast(home,y,x[3],2); break;
188 default: GECODE_NEVER;
189 }
190 }
191 };
192
193 Gecode::IntArgs ints({1,0,3,2});
194
195 /// %Test number of several equal integers equal to integer
196 class IntArrayInt : public Test {
197 protected:
198 /// Integer relation type to propagate
199 Gecode::IntRelType irt;
200 public:
201 /// Create and register test
202 IntArrayInt(Gecode::IntRelType irt0)
203 : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2),
204 irt(irt0) {}
205 /// %Test whether \a x is solution
206 virtual bool solution(const Assignment& x) const {
207 int m = 0;
208 for (int i=0; i<4; i++)
209 if (x[i] == ints[i])
210 m++;
211 return cmp(m,irt,2);
212 }
213 /// Post constraint on \a x
214 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
215 Gecode::IntVarArgs y(4);
216 for (int i=0; i<4; i++)
217 y[i]=x[i];
218 switch (irt) {
219 case Gecode::IRT_EQ:
220 Gecode::exactly(home,y,ints,2); break;
221 case Gecode::IRT_LQ:
222 Gecode::atmost(home,y,ints,2); break;
223 case Gecode::IRT_GQ:
224 Gecode::atleast(home,y,ints,2); break;
225 default: GECODE_NEVER;
226 }
227 }
228 };
229
230 /// %Test number of several equal integers equal to integer variable
231 class IntArrayVar : public Test {
232 protected:
233 /// Integer relation type to propagate
234 Gecode::IntRelType irt;
235 public:
236 /// Create and register test
237 IntArrayVar(Gecode::IntRelType irt0)
238 : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2),
239 irt(irt0) {}
240 /// %Test whether \a x is solution
241 virtual bool solution(const Assignment& x) const {
242 int m = 0;
243 for (int i=0; i<4; i++)
244 if (x[i] == ints[i])
245 m++;
246 return cmp(m,irt,x[4]);
247 }
248 /// Post constraint on \a x
249 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
250 Gecode::IntVarArgs y(4);
251 for (int i=0; i<4; i++)
252 y[i]=x[i];
253 switch (irt) {
254 case Gecode::IRT_EQ:
255 Gecode::exactly(home,y,ints,x[4]); break;
256 case Gecode::IRT_LQ:
257 Gecode::atmost(home,y,ints,x[4]); break;
258 case Gecode::IRT_GQ:
259 Gecode::atleast(home,y,ints,x[4]); break;
260 default: GECODE_NEVER;
261 }
262 }
263 };
264
265 /// Help class to create and register tests
266 class Create {
267 public:
268 /// Perform creation and registration
269 Create(void) {
270 for (IntRelTypes irts; irts(); ++irts)
271 if ((irts.irt() == Gecode::IRT_EQ) ||
272 (irts.irt() == Gecode::IRT_LQ) ||
273 (irts.irt() == Gecode::IRT_GQ)) {
274 (void) new IntInt(irts.irt());
275 (void) new IntVar(irts.irt());
276 (void) new VarVar(irts.irt());
277 (void) new VarInt(irts.irt());
278 (void) new IntArrayInt(irts.irt());
279 (void) new IntArrayVar(irts.irt());
280 }
281 }
282 };
283
284 Create c;
285 //@}
286
287 }
288
289}}
290
291// STATISTICS: test-minimodel