Generated on Thu Mar 22 10:39:45 2012 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  *  Last modified:
00010  *     $Date: 2010-07-15 22:33:36 +0200 (Thu, 15 Jul 2010) $ by $Author: tack $
00011  *     $Revision: 11206 $
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) {
00058          testfix = false;
00059        }
00061        virtual bool solution(const Assignment& x) const {
00062          double d0 = static_cast<double>(x[0]);
00063          double d1 = static_cast<double>(x[1]);
00064          double d2 = static_cast<double>(x[2]);
00065          return d0*d1 == d2;
00066        }
00068        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00069          using namespace Gecode;
00070          rel(home, expr(home, x[0] * x[1]), IRT_EQ, x[2], ICL_DOM);
00071        }
00072      };
00073 
00075      class Div : public Test {
00076      public:
00078        Div(const std::string& s, const Gecode::IntSet& d)
00079          : Test("MiniModel::Div::"+s,3,d) {
00080          testfix = false;
00081        }
00083        virtual bool solution(const Assignment& x) const {
00084          return (x[1] != 0) && (x[0] / x[1] == x[2]);
00085        }
00087        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00088          using namespace Gecode;
00089          rel(home, expr(home, x[0] / x[1]), IRT_EQ, x[2], ICL_DOM);
00090        }
00091      };
00092 
00094      class Mod : public Test {
00095      public:
00097        Mod(const std::string& s, const Gecode::IntSet& d)
00098          : Test("MiniModel::Mod::"+s,3,d) {
00099          testfix = false;
00100        }
00102        virtual bool solution(const Assignment& x) const {
00103          return (x[1] != 0) && (x[0] % x[1] == x[2]);
00104        }
00106        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00107          using namespace Gecode;
00108          rel(home, expr(home, x[0] % x[1]), IRT_EQ, x[2], ICL_DOM);
00109        }
00110      };
00111 
00113      class Plus : public Test {
00114      public:
00116        Plus(const std::string& s, const Gecode::IntSet& d)
00117          : Test("MiniModel::Plus::"+s,3,d) {
00118          testfix = false;
00119        }
00121        virtual bool solution(const Assignment& x) const {
00122          double d0 = static_cast<double>(x[0]);
00123          double d1 = static_cast<double>(x[1]);
00124          double d2 = static_cast<double>(x[2]);
00125          return ((d0+d1 >= Gecode::Int::Limits::min) &&
00126                  (d0+d1 <= Gecode::Int::Limits::max) &&
00127                  (d0+d1 == d2));
00128        }
00130        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00131          using namespace Gecode;
00132          rel(home, expr(home, x[0] + x[1]), IRT_EQ, x[2], ICL_DOM);
00133        }
00134      };
00135 
00137      class Minus : public Test {
00138      public:
00140        Minus(const std::string& s, const Gecode::IntSet& d)
00141          : Test("MiniModel::Minus::"+s,3,d) {
00142          testfix = false;
00143        }
00145        virtual bool solution(const Assignment& x) const {
00146          double d0 = static_cast<double>(x[0]);
00147          double d1 = static_cast<double>(x[1]);
00148          double d2 = static_cast<double>(x[2]);
00149          return ((d0-d1 >= Gecode::Int::Limits::min) &&
00150                  (d0-d1 <= Gecode::Int::Limits::max) &&
00151                  (d0-d1 == d2));
00152        }
00154        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00155          using namespace Gecode;
00156          rel(home, expr(home, x[0] - x[1]), IRT_EQ, x[2], ICL_DOM);
00157        }
00158      };
00159 
00161      class Sqr : public Test {
00162      public:
00164        Sqr(const std::string& s, const Gecode::IntSet& d)
00165          : Test("MiniModel::Sqr::"+s,2,d) {
00166          testfix = false;
00167        }
00169        virtual bool solution(const Assignment& x) const {
00170          double d0 = static_cast<double>(x[0]);
00171          double d1 = static_cast<double>(x[1]);
00172          return d0*d0 == d1;
00173        }
00175        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00176          using namespace Gecode;
00177          rel(home, expr(home, sqr(x[0])), IRT_EQ, x[1], ICL_DOM);
00178        }
00179      };
00180 
00182      class Sqrt : public Test {
00183      public:
00185        Sqrt(const std::string& s, const Gecode::IntSet& d)
00186          : Test("MiniModel::Sqrt::"+s,2,d) {
00187          testfix = false;
00188        }
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 >= 0) && (d0 >= d1*d1) && (d0 < (d1+1)*(d1+1));
00194        }
00196        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00197          using namespace Gecode;
00198          rel(home, expr(home, sqrt(x[0])), IRT_EQ, x[1], ICL_DOM);
00199        }
00200      };
00201 
00203      class Abs : public Test {
00204      public:
00206        Abs(const std::string& s, const Gecode::IntSet& d, Gecode::IntConLevel icl)
00207          : Test("MiniModel::Abs::"+str(icl)+"::"+s,
00208                    2,d,false,icl) {
00209          testfix = false;
00210        }
00212        virtual bool solution(const Assignment& x) const {
00213          double d0 = static_cast<double>(x[0]);
00214          double d1 = static_cast<double>(x[1]);
00215          return (d0<0.0 ? -d0 : d0) == d1;
00216        }
00218        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00219          using namespace Gecode;
00220          rel(home, expr(home, abs(x[0]), icl), IRT_EQ, x[1], ICL_DOM);
00221        }
00222      };
00223 
00225      class Min : public Test {
00226      public:
00228        Min(const std::string& s, const Gecode::IntSet& d)
00229          : Test("MiniModel::Min::Bin::"+s,3,d) {
00230          testfix = false;
00231        }
00233        virtual bool solution(const Assignment& x) const {
00234          return std::min(x[0],x[1]) == x[2];
00235        }
00237        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00238          using namespace Gecode;
00239          rel(home, expr(home, min(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
00240        }
00241      };
00242 
00244      class Max : public Test {
00245      public:
00247        Max(const std::string& s, const Gecode::IntSet& d)
00248          : Test("MiniModel::Max::Bin::"+s,3,d) {
00249          testfix = false;
00250        }
00252        virtual bool solution(const Assignment& x) const {
00253          return std::max(x[0],x[1]) == x[2];
00254        }
00256        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00257          using namespace Gecode;
00258          rel(home, expr(home, max(x[0], x[1])), IRT_EQ, x[2], ICL_DOM);
00259        }
00260      };
00261 
00263      class MinNary : public Test {
00264      public:
00266        MinNary(void) : Test("MiniModel::Min::Nary",4,-4,4) {
00267          testfix = false;
00268        }
00270        virtual bool solution(const Assignment& x) const {
00271          return std::min(std::min(x[0],x[1]), x[2]) == x[3];
00272        }
00274        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00275          using namespace Gecode;
00276          IntVarArgs m(3);
00277          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00278          rel(home, expr(home, min(m)), IRT_EQ, x[3], ICL_DOM);
00279        }
00280      };
00281 
00283      class MaxNary : public Test {
00284      public:
00286        MaxNary(void) : Test("MiniModel::Max::Nary",4,-4,4) {
00287          testfix = false;
00288        }
00290        virtual bool solution(const Assignment& x) const {
00291          return std::max(std::max(x[0],x[1]), x[2]) == x[3];
00292        }
00294        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00295          using namespace Gecode;
00296          IntVarArgs m(3);
00297          m[0]=x[0]; m[1]=x[1]; m[2]=x[2];
00298          rel(home, expr(home, max(m)), IRT_EQ, x[3], ICL_DOM);
00299        }
00300      };
00301 
00302      const int v1[7] = {
00303        Gecode::Int::Limits::min, Gecode::Int::Limits::min+1,
00304        -1,0,1,
00305        Gecode::Int::Limits::max-1, Gecode::Int::Limits::max
00306      };
00307      const int v2[9] = {
00308        static_cast<int>(-sqrt(static_cast<double>(-Gecode::Int::Limits::min))),
00309        -4,-2,-1,0,1,2,4,
00310        static_cast<int>(sqrt(static_cast<double>(Gecode::Int::Limits::max)))
00311      };
00312 
00313      Gecode::IntSet d1(v1,7);
00314      Gecode::IntSet d2(v2,9);
00315      Gecode::IntSet d3(-8,8);
00316 
00317      Mult mult_max("A",d1);
00318      Mult mult_med("B",d2);
00319      Mult mult_min("C",d3);
00320 
00321      Div div_max("A",d1);
00322      Div div_med("B",d2);
00323      Div div_min("C",d3);
00324 
00325      Mod mod_max("A",d1);
00326      Mod mod_med("B",d2);
00327      Mod mod_min("C",d3);
00328 
00329      Plus plus_max("A",d1);
00330      Plus plus_med("B",d2);
00331      Plus plus_min("C",d3);
00332 
00333      Minus minus_max("A",d1);
00334      Minus minus_med("B",d2);
00335      Minus minus_min("C",d3);
00336 
00337      Sqr sqr_max("A",d1);
00338      Sqr sqr_med("B",d2);
00339      Sqr sqr_min("C",d3);
00340 
00341      Sqrt sqrt_max("A",d1);
00342      Sqrt sqrt_med("B",d2);
00343      Sqrt sqrt_min("C",d3);
00344 
00345      Abs abs_bnd_max("A",d1,Gecode::ICL_BND);
00346      Abs abs_bnd_med("B",d2,Gecode::ICL_BND);
00347      Abs abs_bnd_min("C",d3,Gecode::ICL_BND);
00348      Abs abs_dom_max("A",d1,Gecode::ICL_DOM);
00349      Abs abs_dom_med("B",d2,Gecode::ICL_DOM);
00350      Abs abs_dom_min("C",d3,Gecode::ICL_DOM);
00351 
00352      Min min_max("A",d1);
00353      Min min_med("B",d2);
00354      Min min_min("C",d3);
00355 
00356      Max max_max("A",d1);
00357      Max max_med("B",d2);
00358      Max max_min("C",d3);
00359 
00360      MinNary min_nary;
00361      MaxNary max_nary;
00362 
00364    }
00365 
00366 }}
00367 
00368 // STATISTICS: test-minimodel