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 (arithmetic)
41 namespace MiniModelArithmetic {
42
43 /**
44 * \defgroup TaskTestIntMiniModelArithmetic Minimal modelling constraints (arithmetic)
45 * \ingroup TaskTestInt
46 */
47 //@{
48 /// %Test for multiplication constraint
49 class Mult : public Test {
50 public:
51 /// Create and register test
52 Mult(const std::string& s, const Gecode::IntSet& d)
53 : Test("MiniModel::Mult::"+s,3,d) {
54 testfix = false;
55 }
56 /// %Test whether \a x is solution
57 virtual bool solution(const Assignment& x) const {
58 double d0 = static_cast<double>(x[0]);
59 double d1 = static_cast<double>(x[1]);
60 double d2 = static_cast<double>(x[2]);
61 return d0*d1 == d2;
62 }
63 /// Post constraint on \a x
64 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
65 using namespace Gecode;
66 rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], IPL_DOM);
67 }
68 };
69
70 /// %Test for division constraint
71 class Div : public Test {
72 public:
73 /// Create and register test
74 Div(const std::string& s, const Gecode::IntSet& d)
75 : Test("MiniModel::Div::"+s,3,d) {
76 testfix = false;
77 }
78 /// %Test whether \a x is solution
79 virtual bool solution(const Assignment& x) const {
80 return (x[1] != 0) && (x[0] / x[1] == x[2]);
81 }
82 /// Post constraint on \a x
83 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
84 using namespace Gecode;
85 rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], IPL_DOM);
86 }
87 };
88
89 /// %Test for division constraint
90 class Mod : public Test {
91 public:
92 /// Create and register test
93 Mod(const std::string& s, const Gecode::IntSet& d)
94 : Test("MiniModel::Mod::"+s,3,d) {
95 testfix = false;
96 }
97 /// %Test whether \a x is solution
98 virtual bool solution(const Assignment& x) const {
99 return (x[1] != 0) && (x[0] % x[1] == x[2]);
100 }
101 /// Post constraint on \a x
102 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
103 using namespace Gecode;
104 rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], IPL_DOM);
105 }
106 };
107
108 /// %Test for addition constraint
109 class Plus : public Test {
110 public:
111 /// Create and register test
112 Plus(const std::string& s, const Gecode::IntSet& d)
113 : Test("MiniModel::Plus::"+s,3,d) {
114 testfix = false;
115 }
116 /// %Test whether \a x is solution
117 virtual bool solution(const Assignment& x) const {
118 double d0 = static_cast<double>(x[0]);
119 double d1 = static_cast<double>(x[1]);
120 double d2 = static_cast<double>(x[2]);
121 return ((d0+d1 >= Gecode::Int::Limits::min) &&
122 (d0+d1 <= Gecode::Int::Limits::max) &&
123 (d0+d1 == d2));
124 }
125 /// Post constraint on \a x
126 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
127 using namespace Gecode;
128 rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], IPL_DOM);
129 }
130 };
131
132 /// %Test for subtraction constraint
133 class Minus : public Test {
134 public:
135 /// Create and register test
136 Minus(const std::string& s, const Gecode::IntSet& d)
137 : Test("MiniModel::Minus::"+s,3,d) {
138 testfix = false;
139 }
140 /// %Test whether \a x is solution
141 virtual bool solution(const Assignment& x) const {
142 double d0 = static_cast<double>(x[0]);
143 double d1 = static_cast<double>(x[1]);
144 double d2 = static_cast<double>(x[2]);
145 return ((d0-d1 >= Gecode::Int::Limits::min) &&
146 (d0-d1 <= Gecode::Int::Limits::max) &&
147 (d0-d1 == d2));
148 }
149 /// Post constraint on \a x
150 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
151 using namespace Gecode;
152 rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], IPL_DOM);
153 }
154 };
155
156 /// %Test for sqr constraint
157 class Sqr : public Test {
158 public:
159 /// Create and register test
160 Sqr(const std::string& s, const Gecode::IntSet& d)
161 : Test("MiniModel::Sqr::"+s,2,d) {
162 testfix = false;
163 }
164 /// %Test whether \a x is solution
165 virtual bool solution(const Assignment& x) const {
166 double d0 = static_cast<double>(x[0]);
167 double d1 = static_cast<double>(x[1]);
168 return d0*d0 == d1;
169 }
170 /// Post constraint on \a x
171 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
172 using namespace Gecode;
173 rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], IPL_DOM);
174 }
175 };
176
177 /// %Test for sqrt constraint
178 class Sqrt : public Test {
179 public:
180 /// Create and register test
181 Sqrt(const std::string& s, const Gecode::IntSet& d)
182 : Test("MiniModel::Sqrt::"+s,2,d) {
183 testfix = false;
184 }
185 /// %Test whether \a x is solution
186 virtual bool solution(const Assignment& x) const {
187 double d0 = static_cast<double>(x[0]);
188 double d1 = static_cast<double>(x[1]);
189 return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
190 }
191 /// Post constraint on \a x
192 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
193 using namespace Gecode;
194 rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], IPL_DOM);
195 }
196 };
197
198 /// %Test for absolute value constraint
199 class Abs : public Test {
200 public:
201 /// Create and register test
202 Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntPropLevel ipl)
203 : Test("MiniModel::Abs::"+str(ipl)+"::"+s,
204 2,d,false,ipl) {
205 testfix = false;
206 }
207 /// %Test whether \a x is solution
208 virtual bool solution(const Assignment& x) const {
209 double d0 = static_cast<double>(x[0]);
210 double d1 = static_cast<double>(x[1]);
211 return (d0<0.0 ? -d0 : d0) == d1;
212 }
213 /// Post constraint on \a x
214 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
215 using namespace Gecode;
216 rel(home, expr(home, abs(x[0]), ipl), IRT_EQ, x[1], IPL_DOM);
217 }
218 };
219
220 /// %Test for binary minimum constraint
221 class Min : public Test {
222 public:
223 /// Create and register test
224 Min(const std::string& s, const Gecode::IntSet& d)
225 : Test("MiniModel::Min::Bin::"+s,3,d) {
226 testfix = false;
227 }
228 /// %Test whether \a x is solution
229 virtual bool solution(const Assignment& x) const {
230 return std::min(x[0],x[1]) == x[2];
231 }
232 /// Post constraint on \a x
233 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
234 using namespace Gecode;
235 rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
236 }
237 };
238
239 /// %Test for binary maximum constraint
240 class Max : public Test {
241 public:
242 /// Create and register test
243 Max(const std::string& s, const Gecode::IntSet& d)
244 : Test("MiniModel::Max::Bin::"+s,3,d) {
245 testfix = false;
246 }
247 /// %Test whether \a x is solution
248 virtual bool solution(const Assignment& x) const {
249 return std::max(x[0],x[1]) == x[2];
250 }
251 /// Post constraint on \a x
252 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
253 using namespace Gecode;
254 rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
255 }
256 };
257
258 /// %Test for n-ary minimmum constraint
259 class MinNary : public Test {
260 public:
261 /// Create and register test
262 MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
263 testfix = false;
264 }
265 /// %Test whether \a x is solution
266 virtual bool solution(const Assignment& x) const {
267 return std::min(std::min(x[0],x[1]), x[2]) == x[3];
268 }
269 /// Post constraint on \a x
270 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
271 using namespace Gecode;
272 IntVarArgs m(3);
273 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
274 rel(home, expr(home, min(m)), IRT_EQ, x[3], IPL_DOM);
275 }
276 };
277
278 /// %Test for n-ary maximum constraint
279 class MaxNary : public Test {
280 public:
281 /// Create and register test
282 MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
283 testfix = false;
284 }
285 /// %Test whether \a x is solution
286 virtual bool solution(const Assignment& x) const {
287 return std::max(std::max(x[0],x[1]), x[2]) == x[3];
288 }
289 /// Post constraint on \a x
290 virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
291 using namespace Gecode;
292 IntVarArgs m(3);
293 m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
294 rel(home, expr(home, max(m)), IRT_EQ, x[3], IPL_DOM);
295 }
296 };
297
298 const int v1[7] = {
299 Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
300 -1,0,1,
301 Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
302 };
303 const int v2[9] = {
304 static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
305 -4,-2,-1,0,1,2,4,
306 static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
307 };
308
309 Gecode::IntSet d1(v1,7);
310 Gecode::IntSet d2(v2,9);
311 Gecode::IntSet d3(-8,8);
312
313 Mult mult_max("A",d1);
314 Mult mult_med("B",d2);
315 Mult mult_min("C",d3);
316
317 Div div_max("A",d1);
318 Div div_med("B",d2);
319 Div div_min("C",d3);
320
321 Mod mod_max("A",d1);
322 Mod mod_med("B",d2);
323 Mod mod_min("C",d3);
324
325 Plus plus_max("A",d1);
326 Plus plus_med("B",d2);
327 Plus plus_min("C",d3);
328
329 Minus minus_max("A",d1);
330 Minus minus_med("B",d2);
331 Minus minus_min("C",d3);
332
333 Sqr sqr_max("A",d1);
334 Sqr sqr_med("B",d2);
335 Sqr sqr_min("C",d3);
336
337 Sqrt sqrt_max("A",d1);
338 Sqrt sqrt_med("B",d2);
339 Sqrt sqrt_min("C",d3);
340
341 Abs abs_bnd_max("A",d1,Gecode::IPL_BND);
342 Abs abs_bnd_med("B",d2,Gecode::IPL_BND);
343 Abs abs_bnd_min("C",d3,Gecode::IPL_BND);
344 Abs abs_dom_max("A",d1,Gecode::IPL_DOM);
345 Abs abs_dom_med("B",d2,Gecode::IPL_DOM);
346 Abs abs_dom_min("C",d3,Gecode::IPL_DOM);
347
348 Min min_max("A",d1);
349 Min min_med("B",d2);
350 Min min_min("C",d3);
351
352 Max max_max("A",d1);
353 Max max_med("B",d2);
354 Max max_min("C",d3);
355
356 MinNary min_nary;
357 MaxNary max_nary;
358
359 //@}
360 }
361
362}}
363
364// STATISTICS: test-minimodel