Generated on Mon Aug 25 11:35:44 2008 for Gecode by doxygen 1.5.6

mm-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, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2008-07-11 09:36:08 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7294 $
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 "gecode/minimodel.hh"
00041 
00042 namespace Test { namespace Int {
00043 
00045   namespace MiniModelArithmetic {
00046    
00052 
00053      class Mult : public Test {
00054      public:
00056        Mult(const std::string& s, const Gecode::IntSet& d)
00057          : Test("MiniModel::Mult::"+s,3,d) {}
00059        virtual bool solution(const Assignment& x) const {
00060          double d0 = static_cast<double>(x[0]);
00061          double d1 = static_cast<double>(x[1]);
00062          double d2 = static_cast<double>(x[2]);
00063          return d0*d1 == d2;
00064        }
00066        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00067          using namespace Gecode;
00068          rel(home, mult(home, x[0], x[1]), IRT_EQ, x[2], ICL_DOM);
00069        }
00070      };
00071    
00073      class Plus : public Test {
00074      public:
00076        Plus(const std::string& s, const Gecode::IntSet& d)
00077          : Test("MiniModel::Plus::"+s,3,d) {}
00079        virtual bool solution(const Assignment& x) const {
00080          double d0 = static_cast<double>(x[0]);
00081          double d1 = static_cast<double>(x[1]);
00082          double d2 = static_cast<double>(x[2]);
00083          return ((d0+d1 >= Gecode::Int::Limits::min) &&
00084                  (d0+d1 <= Gecode::Int::Limits::max) && 
00085                  (d0+d1 == d2));
00086        }
00088        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00089          using namespace Gecode;
00090          rel(home, plus(home, x[0], x[1]), IRT_EQ, x[2], ICL_DOM);
00091        }
00092      };
00093    
00095      class Minus : public Test {
00096      public:
00098        Minus(const std::string& s, const Gecode::IntSet& d)
00099          : Test("MiniModel::Minus::"+s,3,d) {}
00101        virtual bool solution(const Assignment& x) const {
00102          double d0 = static_cast<double>(x[0]);
00103          double d1 = static_cast<double>(x[1]);
00104          double d2 = static_cast<double>(x[2]);
00105          return ((d0-d1 >= Gecode::Int::Limits::min) &&
00106                  (d0-d1 <= Gecode::Int::Limits::max) && 
00107                  (d0-d1 == d2));
00108        }
00110        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00111          using namespace Gecode;
00112          rel(home, minus(home, x[0], x[1]), IRT_EQ, x[2], ICL_DOM);
00113        }
00114      };
00115    
00117      class Sqr : public Test {
00118      public:
00120        Sqr(const std::string& s, const Gecode::IntSet& d)
00121          : Test("MiniModel::Sqr::"+s,2,d) {}
00123        virtual bool solution(const Assignment& x) const {
00124          double d0 = static_cast<double>(x[0]);
00125          double d1 = static_cast<double>(x[1]);
00126          return d0*d0 == d1;
00127        }
00129        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00130          using namespace Gecode;
00131          rel(home, sqr(home, x[0]), IRT_EQ, x[1], ICL_DOM);
00132        }
00133      };
00134    
00136      class Sqrt : public Test {
00137      public:
00139        Sqrt(const std::string& s, const Gecode::IntSet& d)
00140          : Test("MiniModel::Sqrt::"+s,2,d) {}
00142        virtual bool solution(const Assignment& x) const {
00143          double d0 = static_cast<double>(x[0]);
00144          double d1 = static_cast<double>(x[1]);
00145          return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
00146        }
00148        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00149          using namespace Gecode;
00150          rel(home, sqrt(home,x[0]), IRT_EQ, x[1], ICL_DOM);
00151        }
00152      };
00153    
00155      class Abs : public Test {
00156      public:
00158        Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntConLevel icl)
00159          : Test("MiniModel::Abs::"+str(icl)+"::"+s,
00160                    2,d,false,icl) {}
00162        virtual bool solution(const Assignment& x) const {
00163          double d0 = static_cast<double>(x[0]);
00164          double d1 = static_cast<double>(x[1]);
00165          return (d0<0.0 ? -d0 : d0) == d1;
00166        }
00168        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00169          using namespace Gecode;
00170          rel(home, abs(home, x[0], icl), IRT_EQ, x[1], ICL_DOM);
00171        }
00172      };
00173    
00175      class Min : public Test {
00176      public:
00178        Min(const std::string& s, const Gecode::IntSet& d)
00179          : Test("MiniModel::Min::Bin::"+s,3,d) {}
00181        virtual bool solution(const Assignment& x) const {
00182          return std::min(x[0],x[1]) == x[2];
00183        }
00185        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00186          using namespace Gecode;
00187          rel(home, min(home, x[0], x[1]), IRT_EQ, x[2], ICL_DOM);
00188        }
00189      };
00190    
00192      class Max : public Test {
00193      public:
00195        Max(const std::string& s, const Gecode::IntSet& d) 
00196          : Test("MiniModel::Max::Bin::"+s,3,d) {}
00198        virtual bool solution(const Assignment& x) const {
00199          return std::max(x[0],x[1]) == x[2];
00200        }
00202        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00203          using namespace Gecode;
00204          rel(home, max(home, x[0], x[1]), IRT_EQ, x[2], ICL_DOM);
00205        }
00206      };
00207    
00209      class MinNary : public Test {
00210      public:
00212        MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {}
00214        virtual bool solution(const Assignment& x) const {
00215          return std::min(std::min(x[0],x[1]), x[2]) == x[3];
00216        }
00218        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00219          using namespace Gecode;
00220          IntVarArgs m(3);
00221          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00222          rel(home, min(home, m), IRT_EQ, x[3], ICL_DOM);
00223        }
00224      };
00225    
00227      class MaxNary : public Test {
00228      public:
00230        MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {}
00232        virtual bool solution(const Assignment& x) const {
00233          return std::max(std::max(x[0],x[1]), x[2]) == x[3];
00234        }
00236        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00237          using namespace Gecode;
00238          IntVarArgs m(3);
00239          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00240          rel(home, max(home, m), IRT_EQ, x[3], ICL_DOM);
00241        }
00242      };
00243    
00244      const int v1[7] = {
00245        Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
00246        -1,0,1,
00247        Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
00248      };
00249      const int v2[9] = {
00250        static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
00251        -4,-2,-1,0,1,2,4,
00252        static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
00253      };
00254      
00255      Gecode::IntSet d1(v1,7);
00256      Gecode::IntSet d2(v2,9);
00257      Gecode::IntSet d3(-8,8);
00258    
00259      Mult mult_max("A",d1);
00260      Mult mult_med("B",d2);
00261      Mult mult_min("C",d3);
00262    
00263      Plus plus_max("A",d1);
00264      Plus plus_med("B",d2);
00265      Plus plus_min("C",d3);
00266    
00267      Minus minus_max("A",d1);
00268      Minus minus_med("B",d2);
00269      Minus minus_min("C",d3);
00270    
00271      Sqr sqr_max("A",d1);
00272      Sqr sqr_med("B",d2);
00273      Sqr sqr_min("C",d3);
00274    
00275      Sqrt sqrt_max("A",d1);
00276      Sqrt sqrt_med("B",d2);
00277      Sqrt sqrt_min("C",d3);
00278    
00279      Abs abs_bnd_max("A",d1,Gecode::ICL_BND);
00280      Abs abs_bnd_med("B",d2,Gecode::ICL_BND);
00281      Abs abs_bnd_min("C",d3,Gecode::ICL_BND);
00282      Abs abs_dom_max("A",d1,Gecode::ICL_DOM);
00283      Abs abs_dom_med("B",d2,Gecode::ICL_DOM);
00284      Abs abs_dom_min("C",d3,Gecode::ICL_DOM);
00285    
00286      Min min_max("A",d1);
00287      Min min_med("B",d2);
00288      Min min_min("C",d3);
00289    
00290      Max max_max("A",d1);
00291      Max max_med("B",d2);
00292      Max max_min("C",d3);
00293    
00294      MinNary min_nary;
00295      MaxNary max_nary;
00296    
00298    }
00299 
00300 }}
00301 
00302 // STATISTICS: test-minimodel