Generated on Tue Mar 5 2013 22:37:15 for Gecode by doxygen 1.8.3.1
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, 2005
8  *
9  * Last modified:
10  * $Date: 2010-04-08 20:35:31 +1000 (Thu, 08 Apr 2010) $ by $Author: schulte $
11  * $Revision: 10684 $
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 <cmath>
41 #include <algorithm>
42 
43 namespace Test { namespace Int {
44 
46  namespace Arithmetic {
47 
53 
54  class MultXYZ : public Test {
55  public:
57  MultXYZ(const std::string& s, const Gecode::IntSet& d,
59  : Test("Arithmetic::Mult::XYZ::"+str(icl)+"::"+s,3,d,false,icl) {}
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  Gecode::mult(home, x[0], x[1], x[2], icl);
70  }
71  };
72 
74  class MultXXY : public Test {
75  public:
77  MultXXY(const std::string& s, const Gecode::IntSet& d,
79  : Test("Arithmetic::Mult::XXY::"+str(icl)+"::"+s,2,d,false,icl) {}
81  virtual bool solution(const Assignment& x) const {
82  double d0 = static_cast<double>(x[0]);
83  double d1 = static_cast<double>(x[0]);
84  double d2 = static_cast<double>(x[1]);
85  return d0*d1 == d2;
86  }
88  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
89  Gecode::mult(home, x[0], x[0], x[1], icl);
90  }
91  };
92 
94  class MultXYX : public Test {
95  public:
97  MultXYX(const std::string& s, const Gecode::IntSet& d,
99  : Test("Arithmetic::Mult::XYX::"+str(icl)+"::"+s,2,d,false,icl) {}
101  virtual bool solution(const Assignment& x) const {
102  double d0 = static_cast<double>(x[0]);
103  double d1 = static_cast<double>(x[1]);
104  double d2 = static_cast<double>(x[0]);
105  return d0*d1 == d2;
106  }
108  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
109  Gecode::mult(home, x[0], x[1], x[0], icl);
110  }
111  };
112 
114  class MultXYY : public Test {
115  public:
117  MultXYY(const std::string& s, const Gecode::IntSet& d,
119  : Test("Arithmetic::Mult::XYY::"+str(icl)+"::"+s,2,d,false,icl) {}
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[1]);
125  return d0*d1 == d2;
126  }
128  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
129  Gecode::mult(home, x[0], x[1], x[1], icl);
130  }
131  };
132 
134  class MultXXX : public Test {
135  public:
137  MultXXX(const std::string& s, const Gecode::IntSet& d,
139  : Test("Arithmetic::Mult::XXX::"+str(icl)+"::"+s,1,d,false,icl) {}
141  virtual bool solution(const Assignment& x) const {
142  double d0 = static_cast<double>(x[0]);
143  double d1 = static_cast<double>(x[0]);
144  double d2 = static_cast<double>(x[0]);
145  return d0*d1 == d2;
146  }
148  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
149  Gecode::mult(home, x[0], x[0], x[0], icl);
150  }
151  };
152 
154  class SqrXY : public Test {
155  public:
157  SqrXY(const std::string& s, const Gecode::IntSet& d,
159  : Test("Arithmetic::Sqr::XY::"+str(icl)+"::"+s,2,d,false,icl) {}
161  virtual bool solution(const Assignment& x) const {
162  double d0 = static_cast<double>(x[0]);
163  double d1 = static_cast<double>(x[1]);
164  return d0*d0 == d1;
165  }
167  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
168  Gecode::sqr(home, x[0], x[1], icl);
169  }
170  };
171 
173  class SqrXX : public Test {
174  public:
176  SqrXX(const std::string& s, const Gecode::IntSet& d,
178  : Test("Arithmetic::Sqr::XX::"+str(icl)+"::"+s,1,d,false,icl) {}
180  virtual bool solution(const Assignment& x) const {
181  double d0 = static_cast<double>(x[0]);
182  return d0*d0 == d0;
183  }
185  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
186  Gecode::sqr(home, x[0], x[0], icl);
187  }
188  };
189 
191  class SqrtXY : public Test {
192  public:
194  SqrtXY(const std::string& s, const Gecode::IntSet& d,
196  : Test("Arithmetic::Sqrt::XY::"+str(icl)+"::"+s,2,d,false,icl) {}
198  virtual bool solution(const Assignment& x) const {
199  double d0 = static_cast<double>(x[0]);
200  double d1 = static_cast<double>(x[1]);
201  return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
202  }
204  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
205  Gecode::sqrt(home, x[0], x[1], icl);
206  }
207  };
208 
210  class SqrtXX : public Test {
211  public:
213  SqrtXX(const std::string& s, const Gecode::IntSet& d,
215  : Test("Arithmetic::Sqrt::XX::"+str(icl)+"::"+s,1,d,false,icl) {}
217  virtual bool solution(const Assignment& x) const {
218  double d0 = static_cast<double>(x[0]);
219  return (d0 >= 0) && (d0 >= d0*d0) && (d0 < (d0+1)*(d0+1));
220  }
222  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
223  Gecode::sqrt(home, x[0], x[0], icl);
224  }
225  };
226 
228  class DivMod : public Test {
229  private:
230  static int abs(int a) { return a<0 ? -a:a; }
231  static int sgn(int a) { return a<0 ? -1:1; }
232  public:
234  DivMod(const std::string& s, const Gecode::IntSet& d)
235  : Test("Arithmetic::DivMod::"+s,4,d,false,icl) {}
237  virtual bool solution(const Assignment& x) const {
238  return x[0] == x[1]*x[2]+x[3] &&
239  abs(x[3]) < abs(x[1]) &&
240  (x[3] == 0 || sgn(x[3]) == sgn(x[0]));
241  }
243  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
244  Gecode::divmod(home, x[0], x[1], x[2], x[3], icl);
245  }
246  };
247 
249  class Div : public Test {
250  public:
252  Div(const std::string& s, const Gecode::IntSet& d)
253  : Test("Arithmetic::Div::"+s,3,d,false,icl) {}
255  virtual bool solution(const Assignment& x) const {
256  if (x[1] == 0)
257  return false;
258  int divsign = (x[0] / x[1] < 0) ? -1 : 1;
259  int divresult =
260  divsign *
261  static_cast<int>(floor(static_cast<double>(std::abs(x[0]))/
262  static_cast<double>(std::abs(x[1]))));
263  return x[2] == divresult;
264  }
266  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
267  Gecode::div(home, x[0], x[1], x[2], icl);
268  }
269  };
270 
272  class Mod : public Test {
273  public:
275  Mod(const std::string& s, const Gecode::IntSet& d)
276  : Test("Arithmetic::Mod::"+s,3,d,false,icl) {}
278  virtual bool solution(const Assignment& x) const {
279  if (x[1] == 0)
280  return false;
281  int divsign = (x[0] / x[1] < 0) ? -1 : 1;
282  int divresult =
283  divsign *
284  static_cast<int>(floor(static_cast<double>(std::abs(x[0]))/
285  static_cast<double>(std::abs(x[1]))));
286  return x[0] == x[1]*divresult+x[2];
287  }
289  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
290  Gecode::mod(home, x[0], x[1], x[2], icl);
291  }
292  };
293 
295  class AbsXY : public Test {
296  public:
298  AbsXY(const std::string& s, const Gecode::IntSet& d,
300  : Test("Arithmetic::Abs::XY::"+str(icl)+"::"+s,2,d,false,icl) {}
302  virtual bool solution(const Assignment& x) const {
303  double d0 = static_cast<double>(x[0]);
304  double d1 = static_cast<double>(x[1]);
305  return (d0<0 ? -d0 : d0) == d1;
306  }
308  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
309  Gecode::abs(home, x[0], x[1], icl);
310  }
311  };
312 
314  class AbsXX : public Test {
315  public:
317  AbsXX(const std::string& s, const Gecode::IntSet& d,
319  : Test("Arithmetic::Abs::XX::"+str(icl)+"::"+s,1,d,false,icl) {}
321  virtual bool solution(const Assignment& x) const {
322  double d0 = static_cast<double>(x[0]);
323  double d1 = static_cast<double>(x[0]);
324  return (d0<0 ? -d0 : d0) == d1;
325  }
327  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
328  Gecode::abs(home, x[0], x[0], icl);
329  }
330  };
331 
333  class MinXYZ : public Test {
334  public:
336  MinXYZ(const std::string& s, const Gecode::IntSet& d,
338  : Test("Arithmetic::Min::Bin::XYZ::"+str(icl)+"::"+s,3,d,false,icl) {}
340  virtual bool solution(const Assignment& x) const {
341  return std::min(x[0],x[1]) == x[2];
342  }
344  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
345  Gecode::min(home, x[0], x[1], x[2], icl);
346  }
347  };
348 
350  class MinXXY : public Test {
351  public:
353  MinXXY(const std::string& s, const Gecode::IntSet& d,
355  : Test("Arithmetic::Min::Bin::XYX::"+str(icl)+"::"+s,2,d) {}
357  virtual bool solution(const Assignment& x) const {
358  return std::min(x[0],x[0]) == x[1];
359  }
361  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
362  Gecode::min(home, x[0], x[0], x[1], icl);
363  }
364  };
365 
367  class MinXYX : public Test {
368  public:
370  MinXYX(const std::string& s, const Gecode::IntSet& d,
372  : Test("Arithmetic::Min::Bin::XYX::"+str(icl)+"::"+s,2,d) {}
374  virtual bool solution(const Assignment& x) const {
375  return std::min(x[0],x[1]) == x[0];
376  }
378  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
379  Gecode::min(home, x[0], x[1], x[0], icl);
380  }
381  };
382 
384  class MinXYY : public Test {
385  public:
387  MinXYY(const std::string& s, const Gecode::IntSet& d,
389  : Test("Arithmetic::Min::Bin::XYY::"+str(icl)+"::"+s,2,d) {}
391  virtual bool solution(const Assignment& x) const {
392  return std::min(x[0],x[1]) == x[1];
393  }
395  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
396  Gecode::min(home, x[0], x[1], x[1], icl);
397  }
398  };
399 
401  class MinXXX : public Test {
402  public:
404  MinXXX(const std::string& s, const Gecode::IntSet& d,
406  : Test("Arithmetic::Min::Bin::XXX::"+str(icl)+"::"+s,1,d) {}
408  virtual bool solution(const Assignment& x) const {
409  return std::min(x[0],x[0]) == x[0];
410  }
412  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
413  Gecode::min(home, x[0], x[0], x[0], icl);
414  }
415  };
416 
418  class MaxXYZ : public Test {
419  public:
421  MaxXYZ(const std::string& s, const Gecode::IntSet& d,
423  : Test("Arithmetic::Max::Bin::XYZ::"+str(icl)+"::"+s,3,d) {
425  }
427  virtual bool solution(const Assignment& x) const {
428  return std::max(x[0],x[1]) == x[2];
429  }
431  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
432  Gecode::max(home, x[0], x[1], x[2], icl);
433  }
434  };
435 
437  class MaxXXY : public Test {
438  public:
440  MaxXXY(const std::string& s, const Gecode::IntSet& d,
442  : Test("Arithmetic::Max::Bin::XXY::"+str(icl)+"::"+s,2,d) {}
444  virtual bool solution(const Assignment& x) const {
445  return std::max(x[0],x[0]) == x[1];
446  }
448  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
449  Gecode::max(home, x[0], x[0], x[1], icl);
450  }
451  };
452 
454  class MaxXYX : public Test {
455  public:
457  MaxXYX(const std::string& s, const Gecode::IntSet& d,
459  : Test("Arithmetic::Max::Bin::XYX::"+str(icl)+"::"+s,2,d) {}
461  virtual bool solution(const Assignment& x) const {
462  return std::max(x[0],x[1]) == x[0];
463  }
465  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
466  Gecode::max(home, x[0], x[1], x[0], icl);
467  }
468  };
469 
471  class MaxXYY : public Test {
472  public:
474  MaxXYY(const std::string& s, const Gecode::IntSet& d,
476  : Test("Arithmetic::Max::Bin::XYY::"+str(icl)+"::"+s,2,d) {}
478  virtual bool solution(const Assignment& x) const {
479  return std::max(x[0],x[1]) == x[1];
480  }
482  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
483  Gecode::max(home, x[0], x[1], x[1], icl);
484  }
485  };
486 
488  class MaxXXX : public Test {
489  public:
491  MaxXXX(const std::string& s, const Gecode::IntSet& d,
493  : Test("Arithmetic::Max::Bin::XXX::"+str(icl)+"::"+s,1,d) {}
495  virtual bool solution(const Assignment& x) const {
496  return std::max(x[0],x[0]) == x[0];
497  }
499  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
500  Gecode::max(home, x[0], x[0], x[0], icl);
501  }
502  };
503 
505  class MinNary : public Test {
506  public:
509  : Test("Arithmetic::Min::Nary::"+str(icl),4,-4,4,false,icl) {}
511  virtual bool solution(const Assignment& x) const {
512  return std::min(std::min(x[0],x[1]), x[2]) == x[3];
513  }
515  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
517  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
518  Gecode::min(home, m, x[3], icl);
519  }
520  };
521 
523  class MinNaryShared : public Test {
524  public:
527  : Test("Arithmetic::Min::Nary::Shared::"+str(icl),3,-4,4,false,icl) {}
529  virtual bool solution(const Assignment& x) const {
530  return std::min(std::min(x[0],x[1]), x[2]) == x[1];
531  }
533  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
535  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
536  Gecode::min(home, m, x[1], icl);
537  }
538  };
539 
541  class MaxNary : public Test {
542  public:
545  : Test("Arithmetic::Max::Nary::"+str(icl),4,-4,4,false,icl) {}
547  virtual bool solution(const Assignment& x) const {
548  return std::max(std::max(x[0],x[1]), x[2]) == x[3];
549  }
551  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
553  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
554  Gecode::max(home, m, x[3], icl);
555  }
556  };
557 
559  class MaxNaryShared : public Test {
560  public:
563  : Test("Arithmetic::Max::Nary::Shared::"+str(icl),3,-4,4,false,icl) {}
565  virtual bool solution(const Assignment& x) const {
566  return std::max(std::max(x[0],x[1]), x[2]) == x[1];
567  }
569  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
571  m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
572  Gecode::max(home, m, x[1], icl);
573  }
574  };
575 
576 
577 
578  const int va[7] = {
580  -1,0,1,
581  Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
582  };
583  const int vb[9] = {
584  static_cast<int>(-sqrt(static_cast<double>
586  -4,-2,-1,0,1,2,4,
587  static_cast<int>(sqrt(static_cast<double>
589  };
590 
591  Gecode::IntSet a(va,7);
592  Gecode::IntSet b(vb,9);
593  Gecode::IntSet c(-8,8);
594 
598 
602 
606 
610 
614 
618 
622 
626 
630 
634 
641 
648 
655 
662 
663  DivMod divmod_a_bnd("A",a);
664  DivMod divmod_b_bnd("B",b);
665  DivMod divmod_c_bnd("C",c);
666 
667  Div div_a_bnd("A",a);
668  Div div_b_bnd("B",b);
669  Div div_c_bnd("C",c);
670 
671  Mod mod_a_bnd("A",a);
672  Mod mod_b_bnd("B",b);
673  Mod mod_c_bnd("C",c);
674 
681 
688 
695 
702 
709 
716 
723 
730 
737 
744 
751 
758 
768 
769  }
770 }}
771 
772 // STATISTICS: test-int