Generated on Sat Feb 7 2015 02:01:32 for Gecode by doxygen 1.8.9.1
mm-arithmetic.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, 2008
8  *
9  * Last modified:
10  * $Date: 2010-07-15 22:33:36 +0200 (Thu, 15 Jul 2010) $ by $Author: tack $
11  * $Revision: 11206 $
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 #include "test/int.hh"
39 
40 #include <gecode/minimodel.hh>
41 
42 namespace Test { namespace Int {
43 
45  namespace MiniModelArithmetic {
46 
52  class Mult : public Test {
54  public:
56  Mult(const std::string& s, const Gecode::IntSet& d)
57  : Test("MiniModel::Mult::"+s,3,d) {
58  testfix = false;
59  }
61  virtual bool solution(const Assignment& x) const {
62  double d0 = static_cast<double>(x[0]);
63  double d1 = static_cast<double>(x[1]);
64  double d2 = static_cast<double>(x[2]);
65  return d0*d1 == d2;
66  }
68  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
69  using namespace Gecode;
70  rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], ICL_DOM);
71  }
72  };
73 
75  class Div : public Test {
76  public:
78  Div(const std::string& s, const Gecode::IntSet& d)
79  : Test("MiniModel::Div::"+s,3,d) {
80  testfix = false;
81  }
83  virtual bool solution(const Assignment& x) const {
84  return (x[1] != 0) && (x[0] / x[1] == x[2]);
85  }
87  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
88  using namespace Gecode;
89  rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], ICL_DOM);
90  }
91  };
92 
94  class Mod : public Test {
95  public:
97  Mod(const std::string& s, const Gecode::IntSet& d)
98  : Test("MiniModel::Mod::"+s,3,d) {
99  testfix = false;
100  }
102  virtual bool solution(const Assignment& x) const {
103  return (x[1] != 0) && (x[0] % x[1] == x[2]);
104  }
106  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
107  using namespace Gecode;
108  rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], ICL_DOM);
109  }
110  };
111 
113  class Plus : public Test {
114  public:
116  Plus(const std::string& s, const Gecode::IntSet& d)
117  : Test("MiniModel::Plus::"+s,3,d) {
118  testfix = false;
119  }
121  virtual bool solution(const Assignment& x) const {
122  double d0 = static_cast<double>(x[0]);
123  double d1 = static_cast<double>(x[1]);
124  double d2 = static_cast<double>(x[2]);
125  return ((d0+d1 >= Gecode::Int::Limits::min) &&
126  (d0+d1 <= Gecode::Int::Limits::max) &&
127  (d0+d1 == d2));
128  }
130  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
131  using namespace Gecode;
132  rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], ICL_DOM);
133  }
134  };
135 
137  class Minus : public Test {
138  public:
140  Minus(const std::string& s, const Gecode::IntSet& d)
141  : Test("MiniModel::Minus::"+s,3,d) {
142  testfix = false;
143  }
145  virtual bool solution(const Assignment& x) const {
146  double d0 = static_cast<double>(x[0]);
147  double d1 = static_cast<double>(x[1]);
148  double d2 = static_cast<double>(x[2]);
149  return ((d0-d1 >= Gecode::Int::Limits::min) &&
150  (d0-d1 <= Gecode::Int::Limits::max) &&
151  (d0-d1 == d2));
152  }
154  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
155  using namespace Gecode;
156  rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], ICL_DOM);
157  }
158  };
159 
161  class Sqr : public Test {
162  public:
164  Sqr(const std::string& s, const Gecode::IntSet& d)
165  : Test("MiniModel::Sqr::"+s,2,d) {
166  testfix = false;
167  }
169  virtual bool solution(const Assignment& x) const {
170  double d0 = static_cast<double>(x[0]);
171  double d1 = static_cast<double>(x[1]);
172  return d0*d0 == d1;
173  }
175  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
176  using namespace Gecode;
177  rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], ICL_DOM);
178  }
179  };
180 
182  class Sqrt : public Test {
183  public:
185  Sqrt(const std::string& s, const Gecode::IntSet& d)
186  : Test("MiniModel::Sqrt::"+s,2,d) {
187  testfix = false;
188  }
190  virtual bool solution(const Assignment& x) const {
191  double d0 = static_cast<double>(x[0]);
192  double d1 = static_cast<double>(x[1]);
193  return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
194  }
196  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
197  using namespace Gecode;
198  rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], ICL_DOM);
199  }
200  };
201 
203  class Abs : public Test {
204  public:
206  Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntConLevel icl)
207  : Test("MiniModel::Abs::"+str(icl)+"::"+s,
208  2,d,false,icl) {
209  testfix = false;
210  }
212  virtual bool solution(const Assignment& x) const {
213  double d0 = static_cast<double>(x[0]);
214  double d1 = static_cast<double>(x[1]);
215  return (d0<0.0 ? -d0 : d0) == d1;
216  }
218  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
219  using namespace Gecode;
220  rel(home, expr(home, abs(x[0]), icl), IRT_EQ, x[1], ICL_DOM);
221  }
222  };
223 
225  class Min : public Test {
226  public:
228  Min(const std::string& s, const Gecode::IntSet& d)
229  : Test("MiniModel::Min::Bin::"+s,3,d) {
230  testfix = false;
231  }
233  virtual bool solution(const Assignment& x) const {
234  return std::min(x[0],x[1]) == x[2];
235  }
237  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
238  using namespace Gecode;
239  rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
240  }
241  };
242 
244  class Max : public Test {
245  public:
247  Max(const std::string& s, const Gecode::IntSet& d)
248  : Test("MiniModel::Max::Bin::"+s,3,d) {
249  testfix = false;
250  }
252  virtual bool solution(const Assignment& x) const {
253  return std::max(x[0],x[1]) == x[2];
254  }
256  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
257  using namespace Gecode;
258  rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
259  }
260  };
261 
263  class MinNary : public Test {
264  public:
266  MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
267  testfix = false;
268  }
270  virtual bool solution(const Assignment& x) const {
271  return std::min(std::min(x[0],x[1]), x[2]) == x[3];
272  }
274  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
275  using namespace Gecode;
276  IntVarArgs m(3);
277  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
278  rel(home, expr(home, min(m)), IRT_EQ, x[3], ICL_DOM);
279  }
280  };
281 
283  class MaxNary : public Test {
284  public:
286  MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
287  testfix = false;
288  }
290  virtual bool solution(const Assignment& x) const {
291  return std::max(std::max(x[0],x[1]), x[2]) == x[3];
292  }
294  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
295  using namespace Gecode;
296  IntVarArgs m(3);
297  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
298  rel(home, expr(home, max(m)), IRT_EQ, x[3], ICL_DOM);
299  }
300  };
301 
302  const int v1[7] = {
304  -1,0,1,
305  Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
306  };
307  const int v2[9] = {
308  static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
309  -4,-2,-1,0,1,2,4,
310  static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
311  };
312 
313  Gecode::IntSet d1(v1,7);
314  Gecode::IntSet d2(v2,9);
315  Gecode::IntSet d3(-8,8);
316 
317  Mult mult_max("A",d1);
318  Mult mult_med("B",d2);
319  Mult mult_min("C",d3);
320 
321  Div div_max("A",d1);
322  Div div_med("B",d2);
323  Div div_min("C",d3);
324 
325  Mod mod_max("A",d1);
326  Mod mod_med("B",d2);
327  Mod mod_min("C",d3);
328 
329  Plus plus_max("A",d1);
330  Plus plus_med("B",d2);
331  Plus plus_min("C",d3);
332 
333  Minus minus_max("A",d1);
334  Minus minus_med("B",d2);
335  Minus minus_min("C",d3);
336 
337  Sqr sqr_max("A",d1);
338  Sqr sqr_med("B",d2);
339  Sqr sqr_min("C",d3);
340 
341  Sqrt sqrt_max("A",d1);
342  Sqrt sqrt_med("B",d2);
343  Sqrt sqrt_min("C",d3);
344 
351 
352  Min min_max("A",d1);
353  Min min_med("B",d2);
354  Min min_min("C",d3);
355 
356  Max max_max("A",d1);
357  Max max_med("B",d2);
358  Max max_min("C",d3);
359 
362 
364  }
365 
366 }}
367 
368 // STATISTICS: test-minimodel
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Test for subtraction constraint
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
Abs abs_bnd_med("B", d2, Gecode::ICL_BND)
Plus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
Abs(const std::string &s, const Gecode::IntSet &d, Gecode::IntConLevel icl)
Create and register test.
Abs abs_dom_med("B", d2, Gecode::ICL_DOM)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
void abs(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:49
Integer variable array.
Definition: int.hh:741
Test for addition constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Mod(const std::string &s, const Gecode::IntSet &d)
Create and register test.
const int max
Largest allowed integer value.
Definition: int.hh:113
Computation spaces.
Definition: core.hpp:1362
const int min
Smallest allowed integer value.
Definition: int.hh:115
Gecode::IntSet d1(v1, 7)
Gecode::IntSet d(v, 7)
static std::string str(Gecode::ExtensionalPropKind epk)
Map extensional propagation kind to string.
Definition: int.hpp:212
virtual bool solution(const Assignment &x) const
Test whether x is solution
Abs abs_bnd_min("C", d3, Gecode::ICL_BND)
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Test for multiplication constraint
Test for binary minimum constraint
Equality ( )
Definition: int.hh:904
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Gecode::IntSet d2(v2, 9)
Test for n-ary minimmum constraint
Gecode::IntConLevel icl
Consistency level.
Definition: int.hh:226
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual bool solution(const Assignment &x) const
Test whether x is solution
Test for division constraint
void sqr(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:103
virtual bool solution(const Assignment &x) const
Test whether x is solution
void sqrt(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:110
Test for division constraint
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Gecode::IntSet d3(-8, 8)
virtual bool solution(const Assignment &x) const
Test whether x is solution
Integer sets.
Definition: int.hh:171
Max(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Passing integer variables.
Definition: int.hh:636
bool testfix
Whether to perform fixpoint test.
Definition: int.hh:232
General test support.
Definition: afc.cpp:43
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
BoolVar expr(Home home, const BoolExpr &e, IntConLevel icl)
Post Boolean expression and return its value.
Definition: bool-expr.cpp:632
virtual bool solution(const Assignment &x) const
Test whether x is solution
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Abs abs_dom_max("A", d1, Gecode::ICL_DOM)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
virtual bool solution(const Assignment &x) const
Test whether x is solution
Base class for assignments
Definition: int.hh:63
void rel(Home home, FloatVar x0, FloatRelType frt, FloatVal n)
Propagates .
Definition: rel.cpp:47
Minus(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual bool solution(const Assignment &x) const
Test whether x is solution
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
MinNary(void)
Create and register test.
Mult(const std::string &s, const Gecode::IntSet &d)
Create and register test.
MaxNary(void)
Create and register test.
Bounds propagation or consistency.
Definition: int.hh:939
Gecode toplevel namespace
Abs abs_bnd_max("A", d1, Gecode::ICL_BND)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Test for absolute value constraint
virtual bool solution(const Assignment &x) const
Test whether x is solution
Abs abs_dom_min("C", d3, Gecode::ICL_DOM)
Sqrt(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Test for binary maximum constraint
Div(const std::string &s, const Gecode::IntSet &d)
Create and register test.
Test for n-ary maximum constraint
Domain propagation or consistency.
Definition: int.hh:940
Sqr(const std::string &s, const Gecode::IntSet &d)
Create and register test.
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Min(const std::string &s, const Gecode::IntSet &d)
Create and register test.