Generated on Mon Jul 6 18:09:08 2009 for Gecode by doxygen 1.5.9

element.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, 2005
00008  *
00009  *  Last modified:
00010  *     $Date: 2009-03-25 11:38:03 +0100 (Wed, 25 Mar 2009) $ by $Author: schulte $
00011  *     $Revision: 8560 $
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 #include <climits>
00042 
00043 namespace Test { namespace Int {
00044 
00046    namespace Element {
00047 
00053 
00054      class IntIntVar : public Test {
00055      protected:
00057        Gecode::IntArgs c;
00058      public:
00060        IntIntVar(const std::string& s, const Gecode::IntArgs& c0,
00061                  int min, int max)
00062          : Test("Element::Int::Int::Var::"+s,2,min,max), 
00063            c(c0) {}
00065        virtual bool solution(const Assignment& x) const {
00066          return (x[0]>= 0) && (x[0]<c.size()) && c[x[0]]==x[1];
00067        }
00069        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00070          Gecode::element(home, c, x[0], x[1]);
00071        }
00072      };
00073 
00075      class IntIntInt : public Test {
00076      protected:
00078        Gecode::IntArgs c;
00080        int r;
00081      public:
00083        IntIntInt(const std::string& s, const Gecode::IntArgs& c0, int r0)
00084          : Test("Element::Int::Int::Int::"+s+"::"+str(r0),1,-4,8),
00085            c(c0), r(r0) {}
00087        virtual bool solution(const Assignment& x) const {
00088          return (x[0]>= 0) && (x[0]<c.size()) && c[x[0]]==r;
00089        }
00091        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00092          Gecode::element(home, c, x[0], r);
00093        }
00094      };
00095 
00097      class IntIntShared : public Test {
00098      protected:
00100        Gecode::IntArgs c;
00101      public:
00103        IntIntShared(const std::string& s, const Gecode::IntArgs& c0,
00104                     int minDomain=-4)
00105          : Test("Element::Int::Int::Shared::"+s,1,minDomain,8), c(c0) {}
00107        virtual bool solution(const Assignment& x) const {
00108          return (x[0]>= 0) && (x[0]<c.size()) && c[x[0]]==x[0];
00109        }
00111        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00112          Gecode::element(home, c, x[0], x[0]);
00113        }
00114      };
00115 
00117      class IntBoolVar : public Test {
00118      protected:
00120        Gecode::IntArgs c;
00121      public:
00123        IntBoolVar(const std::string& s, const Gecode::IntArgs& c0)
00124          : Test("Element::Int::Bool::Var::"+s,2,-4,8), c(c0) {}
00126        virtual bool solution(const Assignment& x) const {
00127          return (x[0]>= 0) && (x[0]<c.size()) && c[x[0]]==x[1];
00128        }
00130        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00131          Gecode::element(home, c, x[0], Gecode::channel(home,x[1]));
00132        }
00133      };
00134 
00136      class IntBoolInt : public Test {
00137      protected:
00139        Gecode::IntArgs c;
00141        int r;
00142      public:
00144        IntBoolInt(const std::string& s, const Gecode::IntArgs& c0, int r0)
00145          : Test("Element::Int::Bool::Int::"+s+"::"+str(r0),1,-4,8),
00146            c(c0), r(r0) {}
00148        virtual bool solution(const Assignment& x) const {
00149          return (x[0]>= 0) && (x[0]<c.size()) && c[x[0]]==r;
00150        }
00152        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00153          Gecode::element(home, c, x[0], r);
00154        }
00155      };
00156 
00158      class VarIntVar : public Test {
00159      public:
00161        VarIntVar(Gecode::IntConLevel icl)
00162          : Test("Element::Var::Int::Var::"+str(icl),6,-1,3,false,icl) {}
00164        virtual bool solution(const Assignment& x) const {
00165          return (x[0]>= 0) && (x[0]<x.size()-2) && x[2+x[0]]==x[1];
00166        }
00168        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00169          Gecode::IntVarArgs c(x.size()-2);
00170          for (int i=0; i<x.size()-2; i++)
00171            c[i]=x[2+i];
00172          Gecode::element(home, c, x[0], x[1], icl);
00173        }
00174      };
00175 
00177      class VarIntInt : public Test {
00178      protected:
00180        int r;
00181      public:
00183        VarIntInt(Gecode::IntConLevel icl, int r0)
00184          : Test("Element::Var::Int::Int::"+str(icl)+"::"+str(r0),
00185                 5,-1,3,false,icl), r(r0) {
00186          contest = CTL_NONE;
00187        }
00189        virtual bool solution(const Assignment& x) const {
00190          return (x[0]>= 0) && (x[0]<x.size()-1) && x[1+x[0]]==r;
00191        }
00193        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00194          Gecode::IntVarArgs c(x.size()-1);
00195          for (int i=0; i<x.size()-1; i++)
00196            c[i]=x[1+i];
00197          Gecode::element(home, c, x[0], r, icl);
00198        }
00199      };
00200 
00202      class VarIntShared : public Test {
00203      public:
00205        VarIntShared(Gecode::IntConLevel icl)
00206          : Test("Element::Var::Int::Shared::"+str(icl),5,-1,3,false,icl) {
00207          contest = CTL_NONE;
00208        }
00210        virtual bool solution(const Assignment& x) const {
00211          return (x[0]>= 0) && (x[0]<x.size()-1) && x[1+x[0]]==x[0];
00212        }
00214        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00215          Gecode::IntVarArgs c(x.size()-1);
00216          for (int i=0; i<x.size()-1; i++)
00217            c[i]=x[1+i];
00218          Gecode::element(home, c, x[0], x[0], icl);
00219        }
00220      };
00221 
00223      class VarBoolVar : public Test {
00224      public:
00226        VarBoolVar(void) : Test("Element::Var::Bool::Var",6,-1,3,false) {}
00228        virtual bool solution(const Assignment& x) const {
00229          for (int i=0; i<x.size()-2; i++)
00230            if ((x[2+i] < 0) || (x[2+i]>1))
00231              return false;
00232          return ((x[0]>= 0) && (x[0]<x.size()-2) && x[2+x[0]]==x[1]
00233                  && (x[1]>=0) && (x[1]<=1));
00234        }
00236        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00237          using namespace Gecode;
00238          BoolVarArgs c(x.size()-2);
00239          for (int i=0; i<x.size()-2; i++)
00240            c[i]=channel(home,x[2+i]);
00241          element(home, c, x[0], channel(home,x[1]));
00242        }
00243      };
00244 
00246      class VarBoolInt : public Test {
00247      protected:
00249        int r;
00250      public:
00252        VarBoolInt(int r0)
00253          : Test("Element::Var::Bool::Int::"+str(r0),5,-1,3,false), r(r0) {}
00255        virtual bool solution(const Assignment& x) const {
00256          for (int i=0; i<x.size()-1; i++)
00257            if ((x[1+i] < 0) || (x[1+i]>1))
00258              return false;
00259          return ((x[0]>= 0) && (x[0]<x.size()-1) && x[1+x[0]]==r);
00260        }
00262        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00263          using namespace Gecode;
00264          BoolVarArgs c(x.size()-1);
00265          for (int i=0; i<x.size()-1; i++)
00266            c[i]=channel(home,x[1+i]);
00267          element(home, c, x[0], r);
00268        }
00269      };
00270 
00272      class Create {
00273      public:
00275        void optimized(int idx, int val) {
00276          Gecode::IntArgs c(idx);
00277          for (int i=0; i<idx; i++)
00278            c[i]=std::max(val-i,0);
00279          (void) new IntIntVar(Test::str(idx)+"::"+Test::str(val)+"::val",c,
00280                               val-8,val-1);
00281          if (idx != val)
00282            (void) new IntIntVar(Test::str(idx)+"::"+Test::str(val)+"::idx",c,
00283                                 idx-8,idx-1);
00284        }
00286        Create(void) {
00287          using namespace Gecode;
00288          IntArgs ic1(5, -1,1,-3,3,-4);
00289          IntArgs ic2(8, -1,1,-1,1,-1,1,0,0);
00290          IntArgs ic3(1, -1);
00291          IntArgs ic4(7, 0,-1,2,-2,4,-3,6);
00292          IntArgs ic5(6, 0,0,1,2,3,4);
00293 
00294          IntArgs bc1(5, 0,1,1,0,1);
00295          IntArgs bc2(8, 1,1,0,1,0,1,0,0);
00296          IntArgs bc3(1, 1);
00297 
00298          (void) new IntIntVar("A",ic1,-8,8);
00299          (void) new IntIntVar("B",ic2,-8,8);
00300          (void) new IntIntVar("C",ic3,-8,8);
00301          (void) new IntIntVar("D",ic4,-8,8);
00302 
00303          // Test optimizations
00304          {
00305            int ov[] = {
00306              SCHAR_MAX-1,SCHAR_MAX,
00307              SHRT_MAX-1,SHRT_MAX,
00308              0
00309            };
00310            for (int i=0; ov[i] != 0; i++)
00311              for (int j=0; ov[j] != 0; j++)
00312                optimized(ov[i],ov[j]);
00313          }
00314 
00315          for (int i=-4; i<=4; i++) {
00316            (void) new IntIntInt("A",ic1,i);
00317            (void) new IntIntInt("B",ic2,i);
00318            (void) new IntIntInt("C",ic3,i);
00319            (void) new IntIntInt("D",ic4,i);
00320          }
00321 
00322          (void) new IntIntShared("A",ic1);
00323          (void) new IntIntShared("B",ic2);
00324          (void) new IntIntShared("C",ic3);
00325          (void) new IntIntShared("D",ic4);
00326          (void) new IntIntShared("E",ic5,1);
00327 
00328          (void) new IntBoolVar("A",bc1);
00329          (void) new IntBoolVar("B",bc2);
00330          (void) new IntBoolVar("C",bc3);
00331 
00332          for (int i=0; i<=1; i++) {
00333            (void) new IntBoolInt("A",bc1,i);
00334            (void) new IntBoolInt("B",bc2,i);
00335            (void) new IntBoolInt("C",bc3,i);
00336          }
00337 
00338          (void) new VarIntVar(ICL_BND);
00339          (void) new VarIntVar(ICL_DOM);
00340 
00341          for (int i=-4; i<=4; i++) {
00342            (void) new VarIntInt(ICL_BND,i);
00343            (void) new VarIntInt(ICL_DOM,i);
00344          }
00345 
00346          (void) new VarIntShared(ICL_BND);
00347          (void) new VarIntShared(ICL_DOM);
00348 
00349          (void) new VarBoolVar();
00350          (void) new VarBoolInt(0);
00351          (void) new VarBoolInt(1);
00352        }
00353      };
00354 
00355      Create c;
00357 
00358    }
00359 }}
00360 
00361 // STATISTICS: test-int