Generated on Wed Mar 19 07:29:56 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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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) {}
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          :