Generated on Mon May 5 05:53:52 2008 for Gecode by doxygen 1.5.5

arithmetic.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Christian Schulte <schulte@gecode.org>
00005  *
00006  *  Copyright:
00007  *     Christian Schulte, 2005
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-02-25 00:16:01 +0100 (Mon, 25 Feb 2008) $ by $Author: schulte $
00011  *     $Revision: 6288 $
00012  *
00013  *  This file is part of Gecode, the generic constraint
00014  *  development environment:
00015  *     http://www.gecode.org
00016  *
00017  *  Permission is hereby granted, free of charge, to any person obtaining
00018  *  a copy of this software and associated documentation files (the
00019  *  "Software"), to deal in the Software without restriction, including
00020  *  without limitation the rights to use, copy, modify, merge, publish,
00021  *  distribute, sublicense, and/or sell copies of the Software, and to
00022  *  permit persons to whom the Software is furnished to do so, subject to
00023  *  the following conditions:
00024  *
00025  *  The above copyright notice and this permission notice shall be
00026  *  included in all copies or substantial portions of the Software.
00027  *
00028  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00029  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00030  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00031  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00032  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00033  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00034  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00035  *
00036  */
00037 
00038 #include "test/int.hh"
00039 
00040 #include <cmath>
00041 #include <algorithm>
00042 
00043 namespace Test { namespace Int {
00044 
00046    namespace Arithmetic {
00047    
00053 
00054      class MultXYZ : public Test {
00055      public:
00057        MultXYZ(const std::string& s, const Gecode::IntSet& d)
00058          : Test("Arithmetic::Mult::XYZ::"+s,3,d) {}
00060        virtual bool solution(const Assignment& x) const {
00061          double d0 = static_cast<double>(x[0]);
00062          double d1 = static_cast<double>(x[1]);
00063          double d2 = static_cast<double>(x[2]);
00064          return d0*d1 == d2;
00065        }
00067        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00068          Gecode::mult(home, x[0], x[1], x[2]);
00069        }
00070      };
00071    
00073      class MultXXY : public Test {
00074      public:
00076        MultXXY(const std::string& s, const Gecode::IntSet& d)
00077          : Test("Arithmetic::Mult::XXY::"+s,2,d) {}
00079        virtual bool solution(const Assignment& x) const {
00080          double d0 = static_cast<double>(x[0]);
00081          double d1 = static_cast<double>(x[0]);
00082          double d2 = static_cast<double>(x[1]);
00083          return d0*d1 == d2;
00084        }
00086        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00087          Gecode::mult(home, x[0], x[0], x[1]);
00088        }
00089      };
00090    
00092      class MultXYX : public Test {
00093      public:
00095        MultXYX(const std::string& s, const Gecode::IntSet& d)
00096          : Test("Arithmetic::Mult::XYX::"+s,2,d) {}
00098        virtual bool solution(const Assignment& x) const {
00099          double d0 = static_cast<double>(x[0]);
00100          double d1 = static_cast<double>(x[1]);
00101          double d2 = static_cast<double>(x[0]);
00102          return d0*d1 == d2;
00103        }
00105        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00106          Gecode::mult(home, x[0], x[1], x[0]);
00107        }
00108      };
00109    
00111      class MultXYY : public Test {
00112      public:
00114        MultXYY(const std::string& s, const Gecode::IntSet& d)
00115          : Test("Arithmetic::Mult::XYY::"+s,2,d) {}
00117        virtual bool solution(const Assignment& x) const {
00118          double d0 = static_cast<double>(x[0]);
00119          double d1 = static_cast<double>(x[1]);
00120          double d2 = static_cast<double>(x[1]);
00121          return d0*d1 == d2;
00122        }
00124        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00125          Gecode::mult(home, x[0], x[1], x[1]);
00126        }
00127      };
00128    
00130      class MultXXX : public Test {
00131      public:
00133        MultXXX(const std::string& s, const Gecode::IntSet& d)
00134          : Test("Arithmetic::Mult::XXX::"+s,1,d) {}
00136        virtual bool solution(const Assignment& x) const {
00137          double d0 = static_cast<double>(x[0]);
00138          double d1 = static_cast<double>(x[0]);
00139          double d2 = static_cast<double>(x[0]);
00140          return d0*d1 == d2;
00141        }
00143        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00144          Gecode::mult(home, x[0], x[0], x[0]);
00145        }
00146      };
00147    
00149      class SqrXY : public Test {
00150      public:
00152        SqrXY(const std::string& s, const Gecode::IntSet& d)
00153          : Test("Arithmetic::Sqr::XY::"+s,2,d) {}
00155        virtual bool solution(const Assignment& x) const {
00156          double d0 = static_cast<double>(x[0]);
00157          double d1 = static_cast<double>(x[1]);
00158          return d0*d0 == d1;
00159        }
00161        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00162          Gecode::sqr(home, x[0], x[1]);
00163        }
00164      };
00165    
00167      class SqrXX : public Test {
00168      public:
00170        SqrXX(const std::string& s, const Gecode::IntSet& d)
00171          : Test("Arithmetic::Sqr::XX::"+s,1,d) {}
00173        virtual bool solution(const Assignment& x) const {
00174          double d0 = static_cast<double>(x[0]);
00175          return d0*d0 == d0;
00176        }
00178        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00179          Gecode::sqr(home, x[0], x[0]);
00180        }
00181      };
00182    
00184      class SqrtXY : public Test {
00185      public:
00187        SqrtXY(const std::string& s, const Gecode::IntSet& d)
00188          : Test("Arithmetic::Sqrt::XY::"+s,2,d) {}
00190        virtual bool solution(const Assignment& x) const {
00191          double d0 = static_cast<double>(x[0]);
00192          double d1 = static_cast<double>(x[1]);
00193          return (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
00194        }
00196        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00197          Gecode::sqrt(home, x[0], x[1]);
00198        }
00199      };
00200    
00202      class SqrtXX : public Test {
00203      public:
00205        SqrtXX(const std::string& s, const Gecode::IntSet& d)
00206          : Test("Arithmetic::Sqrt::XX::"+s,1,d) {}
00208        virtual bool solution(const Assignment& x) const {
00209          double d0 = static_cast<double>(x[0]);
00210          return (d0 >= d0*d0) && (d0 < (d0+1)*(d0+1));
00211        }
00213        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00214          Gecode::sqrt(home, x[0], x[0]);
00215        }
00216      };
00217    
00219      class AbsXY : public Test {
00220      public:
00222        AbsXY(const std::string& s, const Gecode::IntSet& d, 
00223              Gecode::IntConLevel icl)
00224          : Test("Arithmetic::Abs::XY::"+str(icl)+"::"+s,2,d,false,icl) {}
00226        virtual bool solution(const Assignment& x) const {
00227          double d0 = static_cast<double>(x[0]);
00228          double d1 = static_cast<double>(x[1]);
00229          return (d0<0 ? -d0 : d0) == d1;
00230        }
00232        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00233          Gecode::abs(home, x[0], x[1], icl);
00234        }
00235      };
00236    
00238      class AbsXX : public Test {
00239      public:
00241        AbsXX(const std::string& s, const Gecode::IntSet& d, 
00242              Gecode::IntConLevel icl)
00243          : Test("Arithmetic::Abs::XX::"+str(icl)+"::"+s,1,d,false,icl) {}
00245        virtual bool solution(const Assignment& x) const {
00246          double d0 = static_cast<double>(x[0]);
00247          double d1 = static_cast<double>(x[0]);
00248          return (d0<0 ? -d0 : d0) == d1;
00249        }
00251        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00252          Gecode::abs(home, x[0], x[0], icl);
00253        }
00254      };
00255    
00257      class MinXYZ : public Test {
00258      public:
00260        MinXYZ(const std::string& s, const Gecode::IntSet& d)
00261          : Test("Arithmetic::Min::Bin::XYZ::"+s,3,d) {}
00263        virtual bool solution(const Assignment& x) const {
00264          return std::min(x[0],x[1]) == x[2];
00265        }
00267        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00268          Gecode::min(home, x[0], x[1], x[2]);
00269        }
00270      };
00271    
00273      class MinXXY : public Test {
00274      public:
00276        MinXXY(const std::string& s, const Gecode::IntSet& d)
00277          : Test("Arithmetic::Min::Bin::XYX::"+s,2,d) {}
00279        virtual bool solution(const Assignment& x) const {
00280          return std::min(x[0],x[0]) == x[1];
00281        }
00283        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00284          Gecode::min(home, x[0], x[0], x[1]);
00285        }
00286      };
00287    
00289      class MinXYX : public Test {
00290      public:
00292        MinXYX(const std::string& s, const Gecode::IntSet& d)
00293          : Test("Arithmetic::Min::Bin::XYX::"+s,2,d) {}
00295        virtual bool solution(const Assignment& x) const {
00296          return std::min(x[0],x[1]) == x[0];
00297        }
00299        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00300          Gecode::min(home, x[0], x[1], x[0]);
00301        }
00302      };
00303    
00305      class MinXYY : public Test {
00306      public:
00308        MinXYY(const std::string& s, const Gecode::IntSet& d)
00309          : Test("Arithmetic::Min::Bin::XYY::"+s,2,d) {}
00311        virtual bool solution(const Assignment& x) const {
00312          return std::min(x[0],x[1]) == x[1];
00313        }
00315        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00316          Gecode::min(home, x[0], x[1], x[1]);
00317        }
00318      };
00319    
00321      class MinXXX : public Test {
00322      public:
00324        MinXXX(const std::string& s, const Gecode::IntSet& d)
00325          : Test("Arithmetic::Min::Bin::XXX::"+s,1,d) {}
00327        virtual bool solution(const Assignment& x) const {
00328          return std::min(x[0],x[0]) == x[0];
00329        }
00331        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00332          Gecode::min(home, x[0], x[0], x[0]);
00333        }
00334      };
00335    
00337      class MaxXYZ : public Test {
00338      public:
00340        MaxXYZ(const std::string& s, const Gecode::IntSet& d)
00341          : Test("Arithmetic::Max::Bin::XYZ::"+s,3,d) {}
00343        virtual bool solution(const Assignment& x) const {
00344          return std::max(x[0],x[1]) == x[2];
00345        }
00347        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00348          Gecode::max(home, x[0], x[1], x[2]);
00349        }
00350      };
00351    
00353      class MaxXXY : public Test {
00354      public:
00356        MaxXXY(const std::string& s, const Gecode::IntSet& d)
00357          : Test("Arithmetic::Max::Bin::XXY::"+s,2,d) {}
00359        virtual bool solution(const Assignment& x) const {
00360          return std::max(x[0],x[0]) == x[1];
00361        }
00363        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00364          Gecode::max(home, x[0], x[0], x[1]);
00365        }
00366      };
00367    
00369      class MaxXYX : public Test {
00370      public:
00372        MaxXYX(const std::string& s, const Gecode::IntSet& d)
00373          : Test("Arithmetic::Max::Bin::XYX::"+s,2,d) {}
00375        virtual bool solution(const Assignment& x) const {
00376          return std::max(x[0],x[1]) == x[0];
00377        }
00379        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00380          Gecode::max(home, x[0], x[1], x[0]);
00381        }
00382      };
00383    
00385      class MaxXYY : public Test {
00386      public:
00388        MaxXYY(const std::string& s, const Gecode::IntSet& d)
00389          : Test("Arithmetic::Max::Bin::XYY::"+s,2,d) {}
00391        virtual bool solution(const Assignment& x) const {
00392          return std::max(x[0],x[1]) == x[1];
00393        }
00395        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00396          Gecode::max(home, x[0], x[1], x[1]);
00397        }
00398      };
00399    
00401      class MaxXXX : public Test {
00402      public:
00404        MaxXXX(const std::string& s, const Gecode::IntSet& d)
00405          : Test("Arithmetic::Max::Bin::XXX::"+s,1,d) {}
00407        virtual bool solution(const Assignment& x) const {
00408          return std::max(x[0],x[0]) == x[0];
00409        }
00411        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00412          Gecode::max(home, x[0], x[0], x[0]);
00413        }
00414      };
00415    
00417      class MinNary : public Test {
00418      public:
00420        MinNary(void) : Test("Arithmetic::Min::Nary",4,-4,4) {}
00422        virtual bool solution(const Assignment& x) const {
00423          return std::min(std::min(x[0],x[1]), x[2]) == x[3];
00424        }
00426        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00427          Gecode::IntVarArgs m(3);
00428          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00429          Gecode::min(home, m, x[3]);
00430        }
00431      };
00432    
00434      class MinNaryShared : public Test {
00435      public:
00437        MinNaryShared(void) : Test("Arithmetic::Min::Nary::Shared",3,-4,4) {}
00439        virtual bool solution(const Assignment& x) const {
00440          return std::min(std::min(x[0],x[1]), x[2]) == x[1];
00441        }
00443        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00444          Gecode::IntVarArgs m(3);
00445          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00446          Gecode::min(home, m, x[1]);
00447        }
00448      };
00449    
00451      class MaxNary : public Test {
00452      public:
00454        MaxNary(void) : Test("Arithmetic::Max::Nary",4,-4,4) {}
00456        virtual bool solution(const Assignment& x) const {
00457          return std::max(std::max(x[0],x[1]), x[2]) == x[3];
00458        }
00460        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00461          Gecode::IntVarArgs m(3);
00462          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00463          Gecode::max(home, m, x[3]);
00464        }
00465      };
00466    
00468      class MaxNaryShared : public Test {
00469      public:
00471        MaxNaryShared(void) : Test("Arithmetic::Max::Nary::Shared",3,-4,4) {}
00473        virtual bool solution(const Assignment& x) const {
00474          return std::max(std::max(x[0],x[1]), x[2]) == x[1];
00475        }
00477        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00478          Gecode::IntVarArgs m(3);
00479          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00480          Gecode::max(home, m, x[1]);
00481        }
00482      };
00483    
00484    
00485    
00486      const int va[7] = {
00487        Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
00488        -1,0,1,
00489        Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
00490      };
00491      const int vb[9] = {
00492        static_cast<int>(-sqrt(static_cast<double>
00493                               (-Gecode::Int::Limits::min))),
00494        -4,-2,-1,0,1,2,4,
00495        static_cast<int>(sqrt(static_cast<double>
00496                              (Gecode::Int::Limits::max)))
00497      };
00498      
00499      Gecode::IntSet a(va,7);
00500      Gecode::IntSet b(vb,9);
00501      Gecode::IntSet c(-8,8);
00502    
00503      MultXYZ mult_xyz_a("A",a);
00504      MultXYZ mult_xyz_b("B",b);
00505      MultXYZ mult_xyz_c("C",c);
00506    
00507      MultXXY mult_xxy_a("A",a);
00508      MultXXY mult_xxy_b("B",b);
00509      MultXXY mult_xxy_c("C",c);
00510    
00511      MultXYX mult_xyx_a("A",a);
00512      MultXYX mult_xyx_b("B",b);
00513      MultXYX mult_xyx_c("C",c);
00514    
00515      MultXYY mult_xyy_a("A",a);
00516      MultXYY mult_xyy_b("B",b);
00517      MultXYY mult_xyy_c("C",c);
00518    
00519      MultXXX mult_xxx_a("A",a);
00520      MultXXX mult_xxx_b("B",b);
00521      MultXXX mult_xxx_c("C",c);
00522    
00523      SqrXY sqr_xy_a("A",a);
00524      SqrXY sqr_xy_b("B",b);
00525      SqrXY sqr_xy_c("C",c);
00526    
00527      SqrXX sqr_xx_a("A",a);
00528      SqrXX sqr_xx_b("B",b);
00529      SqrXX sqr_xx_c("C",c);
00530    
00531      SqrtXY sqrt_xy_a("A",a);
00532      SqrtXY sqrt_xy_b("B",b);
00533      SqrtXY sqrt_xy_c("C",c);
00534    
00535      SqrtXX sqrt_xx_a("A",a);
00536      SqrtXX sqrt_xx_b("B",b);
00537      SqrtXX sqrt_xx_c("C",c);
00538    
00539      AbsXY abs_xy_bnd_a("A",a,Gecode::ICL_BND);
00540      AbsXY abs_xy_bnd_b("B",b,Gecode::ICL_BND);
00541      AbsXY abs_xy_bnd_c("C",c,Gecode::ICL_BND);
00542      AbsXY abs_xy_dom_a("A",a,Gecode::ICL_DOM);
00543      AbsXY abs_xy_dom_b("B",b,Gecode::ICL_DOM);
00544      AbsXY abs_xy_dom_c("C",c,Gecode::ICL_DOM);
00545 
00546      AbsXX abs_xx_bnd_a("A",a,Gecode::ICL_BND);
00547      AbsXX abs_xx_bnd_b("B",b,Gecode::ICL_BND);
00548      AbsXX abs_xx_bnd_c("C",c,Gecode::ICL_BND);
00549      AbsXX abs_xx_dom_a("A",a,Gecode::ICL_DOM);
00550      AbsXX abs_xx_dom_b("B",b,Gecode::ICL_DOM);
00551      AbsXX abs_xx_dom_c("C",c,Gecode::ICL_DOM);
00552    
00553      MinXYZ min_xyz_a("A",a);
00554      MinXYZ min_xyz_b("B",b);
00555      MinXYZ min_xyz_c("C",c);
00556    
00557      MinXXY min_xxy_a("A",a);
00558      MinXXY min_xxy_b("B",b);
00559      MinXXY min_xxy_c("C",c);
00560    
00561      MinXYX min_xyx_a("A",a);
00562      MinXYX min_xyx_b("B",b);
00563      MinXYX min_xyx_c("C",c);
00564    
00565      MinXYY min_xyy_a("A",a);
00566      MinXYY min_xyy_b("B",b);
00567      MinXYY min_xyy_c("C",c);
00568    
00569      MinXXX min_xxx_a("A",a);
00570      MinXXX min_xxx_b("B",b);
00571      MinXXX min_xxx_c("C",c);
00572    
00573      MaxXYZ max_xyz_a("A",a);
00574      MaxXYZ max_xyz_b("B",b);
00575      MaxXYZ max_xyz_c("C",c);
00576    
00577      MaxXXY max_xxy_a("A",a);
00578      MaxXXY max_xxy_b("B",b);
00579      MaxXXY max_xxy_c("C",c);
00580    
00581      MaxXYX max_xyx_a("A",a);
00582      MaxXYX max_xyx_b("B",b);
00583      MaxXYX max_xyx_c("C",c);
00584    
00585      MaxXYY max_xyy_a("A",a);
00586      MaxXYY max_xyy_b("B",b);
00587      MaxXYY max_xyy_c("C",c);
00588    
00589      MaxXXX max_xxx_a("A",a);
00590      MaxXXX max_xxx_b("B",b);
00591      MaxXXX max_xxx_c("C",c);
00592    
00593      MinNary min_nary;
00594      MinNaryShared min_s_nary;
00595      MaxNary max_nary;
00596      MaxNaryShared max_s_nary;
00598    
00599    }
00600 }}
00601 
00602 // STATISTICS: test-int