Generated on Fri Oct 19 11:25:20 2018 for Gecode by doxygen 1.6.3

mm-arithmetic.cpp

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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include "test/int.hh"
00035 
00036 #include <gecode/minimodel.hh>
00037 
00038 namespace Test { namespace Int {
00039 
00041   namespace MiniModelArithmetic {
00042 
00048 
00049      class Mult : public Test {
00050      public:
00052        Mult(const std::string& s, const Gecode::IntSet& d)
00053          : Test("MiniModel::Mult::"+s,3,d) {
00054          testfix = false;
00055        }
00057        virtual bool solution(const Assignment& x) const {
00058          double d0 = static_cast<double>(x[0]);
00059          double d1 = static_cast<double>(x[1]);
00060          double d2 = static_cast<double>(x[2]);
00061          return d0*d1 == d2;
00062        }
00064        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00065          using namespace Gecode;
00066          rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], IPL_DOM);
00067        }
00068      };
00069 
00071      class Div : public Test {
00072      public:
00074        Div(const std::string& s, const Gecode::IntSet& d)
00075          : Test("MiniModel::Div::"+s,3,d) {
00076          testfix = false;
00077        }
00079        virtual bool solution(const Assignment& x) const {
00080          return (x[1] != 0) && (x[0] / x[1] == x[2]);
00081        }
00083        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00084          using namespace Gecode;
00085          rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], IPL_DOM);
00086        }
00087      };
00088 
00090      class Mod : public Test {
00091      public:
00093        Mod(const std::string& s, const Gecode::IntSet& d)
00094          : Test("MiniModel::Mod::"+s,3,d) {
00095          testfix = false;
00096        }
00098        virtual bool solution(const Assignment& x) const {
00099          return (x[1] != 0) && (x[0] % x[1] == x[2]);
00100        }
00102        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00103          using namespace Gecode;
00104          rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], IPL_DOM);
00105        }
00106      };
00107 
00109      class Plus : public Test {
00110      public:
00112        Plus(const std::string& s, const Gecode::IntSet& d)
00113          : Test("MiniModel::Plus::"+s,3,d) {
00114          testfix = false;
00115        }
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[2]);
00121          return ((d0+d1 >= Gecode::Int::Limits::min) &&
00122                  (d0+d1 <= Gecode::Int::Limits::max) &&
00123                  (d0+d1 == d2));
00124        }
00126        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00127          using namespace Gecode;
00128          rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], IPL_DOM);
00129        }
00130      };
00131 
00133      class Minus : public Test {
00134      public:
00136        Minus(const std::string& s, const Gecode::IntSet& d)
00137          : Test("MiniModel::Minus::"+s,3,d) {
00138          testfix = false;
00139        }
00141        virtual bool solution(const Assignment& x) const {
00142          double d0 = static_cast<double>(x[0]);
00143          double d1 = static_cast<double>(x[1]);
00144          double d2 = static_cast<double>(x[2]);
00145          return ((d0-d1 >= Gecode::Int::Limits::min) &&
00146                  (d0-d1 <= Gecode::Int::Limits::max) &&
00147                  (d0-d1 == d2));
00148        }
00150        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00151          using namespace Gecode;
00152          rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], IPL_DOM);
00153        }
00154      };
00155 
00157      class Sqr : public Test {
00158      public:
00160        Sqr(const std::string& s, const Gecode::IntSet& d)
00161          : Test("MiniModel::Sqr::"+s,2,d) {
00162          testfix = false;
00163        }
00165        virtual bool solution(const Assignment& x) const {
00166          double d0 = static_cast<double>(x[0]);
00167          double d1 = static_cast<double>(x[1]);
00168          return d0*d0 == d1;
00169        }
00171        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00172          using namespace Gecode;
00173          rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], IPL_DOM);
00174        }
00175      };
00176 
00178      class Sqrt : public Test {
00179      public:
00181        Sqrt(const std::string& s, const Gecode::IntSet& d)
00182          : Test("MiniModel::Sqrt::"+s,2,d) {
00183          testfix = false;
00184        }
00186        virtual bool solution(const Assignment& x) const {
00187          double d0 = static_cast<double>(x[0]);
00188          double d1 = static_cast<double>(x[1]);
00189          return (d0 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
00190        }
00192        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00193          using namespace Gecode;
00194          rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], IPL_DOM);
00195        }
00196      };
00197 
00199      class Abs : public Test {
00200      public:
00202        Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntPropLevel ipl)
00203          : Test("MiniModel::Abs::"+str(ipl)+"::"+s,
00204                    2,d,false,ipl) {
00205          testfix = false;
00206        }
00208        virtual bool solution(const Assignment& x) const {
00209          double d0 = static_cast<double>(x[0]);
00210          double d1 = static_cast<double>(x[1]);
00211          return (d0<0.0 ? -d0 : d0) == d1;
00212        }
00214        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00215          using namespace Gecode;
00216          rel(home, expr(home, abs(x[0]), ipl), IRT_EQ, x[1], IPL_DOM);
00217        }
00218      };
00219 
00221      class Min : public Test {
00222      public:
00224        Min(const std::string& s, const Gecode::IntSet& d)
00225          : Test("MiniModel::Min::Bin::"+s,3,d) {
00226          testfix = false;
00227        }
00229        virtual bool solution(const Assignment& x) const {
00230          return std::min(x[0],x[1]) == x[2];
00231        }
00233        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00234          using namespace Gecode;
00235          rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
00236        }
00237      };
00238 
00240      class Max : public Test {
00241      public:
00243        Max(const std::string& s, const Gecode::IntSet& d)
00244          : Test("MiniModel::Max::Bin::"+s,3,d) {
00245          testfix = false;
00246        }
00248        virtual bool solution(const Assignment& x) const {
00249          return std::max(x[0],x[1]) == x[2];
00250        }
00252        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00253          using namespace Gecode;
00254          rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], IPL_DOM);
00255        }
00256      };
00257 
00259      class MinNary : public Test {
00260      public:
00262        MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
00263          testfix = false;
00264        }
00266        virtual bool solution(const Assignment& x) const {
00267          return std::min(std::min(x[0],x[1]), x[2]) == x[3];
00268        }
00270        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00271          using namespace Gecode;
00272          IntVarArgs m(3);
00273          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00274          rel(home, expr(home, min(m)), IRT_EQ, x[3], IPL_DOM);
00275        }
00276      };
00277 
00279      class MaxNary : public Test {
00280      public:
00282        MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
00283          testfix = false;
00284        }
00286        virtual bool solution(const Assignment& x) const {
00287          return std::max(std::max(x[0],x[1]), x[2]) == x[3];
00288        }
00290        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00291          using namespace Gecode;
00292          IntVarArgs m(3);
00293          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00294          rel(home, expr(home, max(m)), IRT_EQ, x[3], IPL_DOM);
00295        }
00296      };
00297 
00298      const int v1[7] = {
00299        Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
00300        -1,0,1,
00301        Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
00302      };
00303      const int v2[9] = {
00304        static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
00305        -4,-2,-1,0,1,2,4,
00306        static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
00307      };
00308 
00309      Gecode::IntSet d1(v1,7);
00310      Gecode::IntSet d2(v2,9);
00311      Gecode::IntSet d3(-8,8);
00312 
00313      Mult mult_max("A",d1);
00314      Mult mult_med("B",d2);
00315      Mult mult_min("C",d3);
00316 
00317      Div div_max("A",d1);
00318      Div div_med("B",d2);
00319      Div div_min("C",d3);
00320 
00321      Mod mod_max("A",d1);
00322      Mod mod_med("B",d2);
00323      Mod mod_min("C",d3);
00324 
00325      Plus plus_max("A",d1);
00326      Plus plus_med("B",d2);
00327      Plus plus_min("C",d3);
00328 
00329      Minus minus_max("A",d1);
00330      Minus minus_med("B",d2);
00331      Minus minus_min("C",d3);
00332 
00333      Sqr sqr_max("A",d1);
00334      Sqr sqr_med("B",d2);
00335      Sqr sqr_min("C",d3);
00336 
00337      Sqrt sqrt_max("A",d1);
00338      Sqrt sqrt_med("B",d2);
00339      Sqrt sqrt_min("C",d3);
00340 
00341      Abs abs_bnd_max("A",d1,Gecode::IPL_BND);
00342      Abs abs_bnd_med("B",d2,Gecode::IPL_BND);
00343      Abs abs_bnd_min("C",d3,Gecode::IPL_BND);
00344      Abs abs_dom_max("A",d1,Gecode::IPL_DOM);
00345      Abs abs_dom_med("B",d2,Gecode::IPL_DOM);
00346      Abs abs_dom_min("C",d3,Gecode::IPL_DOM);
00347 
00348      Min min_max("A",d1);
00349      Min min_med("B",d2);
00350      Min min_min("C",d3);
00351 
00352      Max max_max("A",d1);
00353      Max max_med("B",d2);
00354      Max max_min("C",d3);
00355 
00356      MinNary min_nary;
00357      MaxNary max_nary;
00358 
00360    }
00361 
00362 }}
00363 
00364 // STATISTICS: test-minimodel