Generated on Tue Apr 18 10:21:53 2017 for Gecode by doxygen 1.6.3

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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
00011  *     $Revision: 14967 $
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::IntPropLevel ipl)
00162          : Test("Element::Var::Int::Var::"+str(ipl),6,-1,3,false,ipl) {}
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], ipl);
00173        }
00174      };
00175 
00177      class VarIntInt : public Test {
00178      protected:
00180        int r;
00181      public:
00183        VarIntInt(Gecode::IntPropLevel ipl, int r0)
00184          : Test("Element::Var::Int::Int::"+str(ipl)+"::"+str(r0),
00185                 5,-1,3,false,ipl), 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, ipl);
00198        }
00199      };
00200 
00202      class VarIntShared : public Test {
00203      public:
00205        VarIntShared(Gecode::IntPropLevel ipl)
00206          : Test("Element::Var::Int::Shared::"+str(ipl),5,-1,3,false,ipl) {
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], ipl);
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          if (r == 1) {
00268            switch (Base::rand(3)) {
00269            case 0:
00270              element(home, c, x[0], 1);
00271              break;
00272            case 1:
00273              {
00274                BoolVar one(home,1,1);
00275                rel(home, element(c,x[0]) == one);
00276              }
00277              break;
00278            case 2:
00279              rel(home, element(c,x[0]));
00280              break;
00281            default: GECODE_NEVER;
00282            }
00283          } else {
00284            element(home, c, x[0], r);
00285          }
00286        }
00287      };
00288 
00289 
00291      class MatrixIntIntVarXY : public Test {
00292      protected:
00294        Gecode::IntArgs tm;
00295      public:
00297        MatrixIntIntVarXY(void)
00298          : Test("Element::Matrix::Int::IntVar::XY",3,0,5,false),
00299            tm(6, 0,1,2,3,4,5) {}
00301        virtual bool solution(const Assignment& x) const {
00302          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00303          using namespace Gecode;
00304          if ((x[0] > 2) || (x[1] > 1))
00305            return false;
00306          Matrix<IntArgs> m(tm,3,2);
00307          return m(x[0],x[1]) == x[2];
00308        }
00310        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00311          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00312          using namespace Gecode;
00313          Matrix<IntArgs> m(tm,3,2);
00314          element(home, m, x[0], x[1], x[2]);
00315        }
00316      };
00317 
00319      class MatrixIntIntVarXX : public Test {
00320      protected:
00322        Gecode::IntArgs tm;
00323      public:
00325        MatrixIntIntVarXX(void)
00326          : Test("Element::Matrix::Int::IntVar::XX",2,0,3,false),
00327            tm(4, 0,1,2,3) {}
00329        virtual bool solution(const Assignment& x) const {
00330          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00331          using namespace Gecode;
00332          if (x[0] > 1)
00333            return false;
00334          Matrix<IntArgs> m(tm,2,2);
00335          return m(x[0],x[0]) == x[1];
00336        }
00338        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00339          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00340          using namespace Gecode;
00341          Matrix<IntArgs> m(tm,2,2);
00342          element(home, m, x[0], x[0], x[1]);
00343        }
00344      };
00345 
00347      class MatrixIntBoolVarXY : public Test {
00348      protected:
00350        Gecode::IntArgs tm;
00351      public:
00353        MatrixIntBoolVarXY(void)
00354          : Test("Element::Matrix::Int::BoolVar::XY",3,0,3,false),
00355            tm(4, 0,1,1,0) {}
00357        virtual bool solution(const Assignment& x) const {
00358          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00359          using namespace Gecode;
00360          if ((x[0] > 1) || (x[1] > 1))
00361            return false;
00362          Matrix<IntArgs> m(tm,2,2);
00363          return m(x[0],x[1]) == x[2];
00364        }
00366        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00367          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00368          using namespace Gecode;
00369          Matrix<IntArgs> m(tm,2,2);
00370          element(home, m, x[0], x[1], channel(home,x[2]));
00371        }
00372      };
00373 
00375      class MatrixIntBoolVarXX : public Test {
00376      protected:
00378        Gecode::IntArgs tm;
00379      public:
00381        MatrixIntBoolVarXX(void)
00382          : Test("Element::Matrix::Int::BoolVar::XX",2,0,3,false),
00383            tm(4, 0,1,1,0) {}
00385        virtual bool solution(const Assignment& x) const {
00386          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00387          using namespace Gecode;
00388          if (x[0] > 1)
00389            return false;
00390          Matrix<IntArgs> m(tm,2,2);
00391          return m(x[0],x[0]) == x[1];
00392        }
00394        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00395          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00396          using namespace Gecode;
00397          Matrix<IntArgs> m(tm,2,2);
00398          element(home, m, x[0], x[0], channel(home,x[1]));
00399        }
00400      };
00401 
00403      class MatrixIntVarIntVarXY : public Test {
00404      public:
00406        MatrixIntVarIntVarXY(void)
00407          : Test("Element::Matrix::IntVar::IntVar::XY",3+4,0,3,false) {}
00409        virtual bool solution(const Assignment& x) const {
00410          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00411          // remaining: matrix
00412          using namespace Gecode;
00413          if ((x[0] > 1) || (x[1] > 1))
00414            return false;
00415          IntArgs tm(4);
00416          tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6];
00417          Matrix<IntArgs> m(tm,2,2);
00418          return m(x[0],x[1]) == x[2];
00419        }
00421        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00422          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00423          using namespace Gecode;
00424          IntVarArgs tm(4);
00425          tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6];
00426          Matrix<IntVarArgs> m(tm,2,2);
00427          element(home, m, x[0], x[1], x[2]);
00428        }
00429      };
00430 
00432      class MatrixIntVarIntVarXX : public Test {
00433      public:
00435        MatrixIntVarIntVarXX(void)
00436          : Test("Element::Matrix::IntVar::IntVar::XX",2+4,0,3,false) {}
00438        virtual bool solution(const Assignment& x) const {
00439          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00440          // remaining: matrix
00441          using namespace Gecode;
00442          if (x[0] > 1)
00443            return false;
00444          IntArgs tm(4);
00445          tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5];
00446          Matrix<IntArgs> m(tm,2,2);
00447          return m(x[0],x[0]) == x[1];
00448        }
00450        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00451          // x-coordinate: x[0], y-coordinate: x[1], result: x[1]
00452          using namespace Gecode;
00453          IntVarArgs tm(4);
00454          tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5];
00455          Matrix<IntVarArgs> m(tm,2,2);
00456          element(home, m, x[0], x[0], x[1]);
00457        }
00458      };
00459 
00461      class MatrixBoolVarBoolVarXY : public Test {
00462      public:
00464        MatrixBoolVarBoolVarXY(void)
00465          : Test("Element::Matrix::BoolVar::BoolVar::XY",3+4,0,1,false) {}
00467        virtual bool solution(const Assignment& x) const {
00468          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00469          // remaining: matrix
00470          using namespace Gecode;
00471          IntArgs tm(4);
00472          tm[0]=x[3]; tm[1]=x[4]; tm[2]=x[5]; tm[3]=x[6];
00473          Matrix<IntArgs> m(tm,2,2);
00474          return m(x[0],x[1]) == x[2];
00475        }
00477        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00478          // x-coordinate: x[0], y-coordinate: x[1], result: x[2]
00479          using namespace Gecode;
00480          BoolVarArgs tm(4);
00481          tm[0]=channel(home,x[3]); tm[1]=channel(home,x[4]);
00482          tm[2]=channel(home,x[5]); tm[3]=channel(home,x[6]);
00483          Matrix<BoolVarArgs> m(tm,2,2);
00484          element(home, m, x[0], x[1], channel(home,x[2]));
00485        }
00486      };
00487 
00489      class MatrixBoolVarBoolVarXX : public Test {
00490      public:
00492        MatrixBoolVarBoolVarXX(void)
00493          : Test("Element::Matrix::BoolVar::BoolVar::XX",2+4,0,1,false) {}
00495        virtual bool solution(const Assignment& x) const {
00496          // x-coordinate: x[0], y-coordinate: x[0], result: x[1]
00497          // remaining: matrix
00498          using namespace Gecode;
00499          IntArgs tm(4);
00500          tm[0]=x[2]; tm[1]=x[3]; tm[2]=x[4]; tm[3]=x[5];
00501          Matrix<IntArgs> m(tm,2,2);
00502          return m(x[0],x[0]) == x[1];
00503        }
00505        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00506          // x-coordinate: x[0], y-coordinate: x[1], result: x[1]
00507          using namespace Gecode;
00508          BoolVarArgs tm(4);
00509          tm[0]=channel(home,x[2]); tm[1]=channel(home,x[3]);
00510          tm[2]=channel(home,x[4]); tm[3]=channel(home,x[5]);
00511          Matrix<BoolVarArgs> m(tm,2,2);
00512          element(home, m, x[0], x[0], channel(home,x[1]));
00513        }
00514      };
00515 
00516 
00517 
00518 
00520      class Create {
00521      public:
00523        void optimized(int idx, int val) {
00524          Gecode::IntArgs c(idx);
00525          for (int i=0; i<idx; i++)
00526            c[i]=std::max(val-i,0);
00527          (void) new IntIntVar(Test::str(idx)+"::"+Test::str(val)+"::val",c,
00528                               val-8,val-1);
00529          if (idx != val)
00530            (void) new IntIntVar(Test::str(idx)+"::"+Test::str(val)+"::idx",c,
00531                                 idx-8,idx-1);
00532        }
00534        Create(void) {
00535          using namespace Gecode;
00536          IntArgs ic1(5, -1,1,-3,3,-4);
00537          IntArgs ic2(8, -1,1,-1,1,-1,1,0,0);
00538          IntArgs ic3(1, -1);
00539          IntArgs ic4(7, 0,-1,2,-2,4,-3,6);
00540          IntArgs ic5(6, 0,0,1,2,3,4);
00541 
00542          IntArgs bc1(5, 0,1,1,0,1);
00543          IntArgs bc2(8, 1,1,0,1,0,1,0,0);
00544          IntArgs bc3(1, 1);
00545 
00546          (void) new IntIntVar("A",ic1,-8,8);
00547          (void) new IntIntVar("B",ic2,-8,8);
00548          (void) new IntIntVar("C",ic3,-8,8);
00549          (void) new IntIntVar("D",ic4,-8,8);
00550 
00551          // Test optimizations
00552          {
00553            int ov[] = {
00554              SCHAR_MAX-1,SCHAR_MAX,
00555              SHRT_MAX-1,SHRT_MAX,
00556              0
00557            };
00558            for (int i=0; ov[i] != 0; i++)
00559              for (int j=0; ov[j] != 0; j++)
00560                optimized(ov[i],ov[j]);
00561          }
00562 
00563          for (int i=-4; i<=4; i++) {
00564            (void) new IntIntInt("A",ic1,i);
00565            (void) new IntIntInt("B",ic2,i);
00566            (void) new IntIntInt("C",ic3,i);
00567            (void) new IntIntInt("D",ic4,i);
00568          }
00569 
00570          (void) new IntIntShared("A",ic1);
00571          (void) new IntIntShared("B",ic2);
00572          (void) new IntIntShared("C",ic3);
00573          (void) new IntIntShared("D",ic4);
00574          (void) new IntIntShared("E",ic5,1);
00575 
00576          (void) new IntBoolVar("A",bc1);
00577          (void) new IntBoolVar("B",bc2);
00578          (void) new IntBoolVar("C",bc3);
00579 
00580          for (int i=0; i<=1; i++) {
00581            (void) new IntBoolInt("A",bc1,i);
00582            (void) new IntBoolInt("B",bc2,i);
00583            (void) new IntBoolInt("C",bc3,i);
00584          }
00585 
00586          (void) new VarIntVar(IPL_BND);
00587          (void) new VarIntVar(IPL_DOM);
00588 
00589          for (int i=-4; i<=4; i++) {
00590            (void) new VarIntInt(IPL_BND,i);
00591            (void) new VarIntInt(IPL_DOM,i);
00592          }
00593 
00594          (void) new VarIntShared(IPL_BND);
00595          (void) new VarIntShared(IPL_DOM);
00596 
00597          (void) new VarBoolVar();
00598          (void) new VarBoolInt(0);
00599          (void) new VarBoolInt(1);
00600 
00601          // Matrix tests
00602          (void) new MatrixIntIntVarXY();
00603          (void) new MatrixIntIntVarXX();
00604          (void) new MatrixIntBoolVarXY();
00605          (void) new MatrixIntBoolVarXX();
00606 
00607          (void) new MatrixIntVarIntVarXY();
00608          (void) new MatrixIntVarIntVarXX();
00609          (void) new MatrixBoolVarBoolVarXY();
00610          (void) new MatrixBoolVarBoolVarXX();
00611        }
00612      };
00613 
00614      Create c;
00616 
00617    }
00618 }}
00619 
00620 // STATISTICS: test-int