Generated on Thu Apr 11 13:58:59 2019 for Gecode by doxygen 1.6.3

rel.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  *  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 Rel {
00042 
00048 
00049      class IntVarXY : public Test {
00050      protected:
00052        Gecode::IntRelType irt;
00053      public:
00055        IntVarXY(Gecode::IntRelType irt0, int n, Gecode::IntPropLevel ipl)
00056          : Test("Rel::Int::Var::XY::"+str(irt0)+"::"+str(ipl)+"::"+str(n),
00057                 n+1,-3,3,n==1,ipl),
00058            irt(irt0) {}
00060        virtual bool solution(const Assignment& x) const {
00061          if (x.size() == 2) {
00062            return cmp(x[0],irt,x[1]);
00063          } else {
00064            return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]);
00065          }
00066        }
00068        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00069          using namespace Gecode;
00070          if (x.size() == 2) {
00071            rel(home, x[0], irt, x[1], ipl);
00072          } else {
00073            IntVarArgs y(2);
00074            y[0]=x[0]; y[1]=x[1];
00075            rel(home, y, irt, x[2], ipl);
00076          }
00077        }
00079        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00080                          Gecode::Reify r) {
00081          assert(x.size() == 2);
00082          Gecode::rel(home, x[0], irt, x[1], r, ipl);
00083        }
00084      };
00085 
00087      class IntVarXX : public Test {
00088      protected:
00090        Gecode::IntRelType irt;
00091      public:
00093        IntVarXX(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
00094          : Test("Rel::Int::Var::XX::"+str(irt0)+"::"+str(ipl),
00095                 1,-3,3,true,ipl),
00096            irt(irt0) {
00097          contest = ((irt != Gecode::IRT_LE) &&
00098                     (irt != Gecode::IRT_GR) &&
00099                     (irt != Gecode::IRT_NQ))
00100            ? CTL_DOMAIN : CTL_NONE;
00101        }
00103        virtual bool solution(const Assignment& x) const {
00104          return cmp(x[0],irt,x[0]);
00105        }
00107        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00108          Gecode::rel(home, x[0], irt, x[0], ipl);
00109        }
00111        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00112                          Gecode::Reify r) {
00113          Gecode::rel(home, x[0], irt, x[0], r, ipl);
00114        }
00115      };
00116 
00118      class BoolVarXY : public Test {
00119      protected:
00121        Gecode::IntRelType irt;
00122      public:
00124        BoolVarXY(Gecode::IntRelType irt0, int n)
00125          : Test("Rel::Bool::Var::XY::"+str(irt0)+"::"+str(n),n+1,0,1,
00126                 n==1),
00127            irt(irt0) {}
00129        virtual bool solution(const Assignment& x) const {
00130          if (x.size() == 2) {
00131            return cmp(x[0],irt,x[1]);
00132          } else {
00133            return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]);
00134          }
00135        }
00137        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00138          using namespace Gecode;
00139          if (x.size() == 2) {
00140            rel(home, channel(home,x[0]), irt, channel(home,x[1]));
00141          } else {
00142            BoolVarArgs y(2);
00143            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00144            rel(home, y, irt, channel(home,x[2]));
00145          }
00146        }
00148        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00149                          Gecode::Reify r) {
00150          assert(x.size() == 2);
00151          using namespace Gecode;
00152          rel(home,
00153              channel(home,x[0]), irt, channel(home,x[1]),
00154              r);
00155        }
00156      };
00157 
00159      class BoolVarXX : public Test {
00160      protected:
00162        Gecode::IntRelType irt;
00163      public:
00165        BoolVarXX(Gecode::IntRelType irt0)
00166          : Test("Rel::Bool::Var::XX::"+str(irt0),1,0,1),
00167            irt(irt0) {
00168          contest = ((irt != Gecode::IRT_LE) &&
00169                     (irt != Gecode::IRT_GR) &&
00170                     (irt != Gecode::IRT_NQ))
00171            ? CTL_DOMAIN : CTL_NONE;
00172        }
00174        virtual bool solution(const Assignment& x) const {
00175          return cmp(x[0],irt,x[0]);
00176        }
00178        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00179          Gecode::BoolVar b = Gecode::channel(home,x[0]);
00180          Gecode::rel(home, b, irt, b);
00181        }
00182      };
00183 
00185      class IntInt : public Test {
00186      protected:
00188        Gecode::IntRelType irt;
00190        int c;
00191      public:
00193        IntInt(Gecode::IntRelType irt0, int n, int c0)
00194          : Test("Rel::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),
00195                 n,-3,3,n==1),
00196            irt(irt0), c(c0) {}
00198        virtual bool solution(const Assignment& x) const {
00199          if (x.size() == 1)
00200            return cmp(x[0],irt,c);
00201          else
00202            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00203        }
00205        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00206          using namespace Gecode;
00207          if (x.size() == 1)
00208            rel(home, x[0], irt, c);
00209          else
00210            rel(home, x, irt, c);
00211        }
00213        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00214                          Gecode::Reify r) {
00215          assert(x.size() == 1);
00216          Gecode::rel(home, x[0], irt, c, r);
00217        }
00218      };
00219 
00221      class BoolInt : public Test {
00222      protected:
00224        Gecode::IntRelType irt;
00226        int c;
00227      public:
00229        BoolInt(Gecode::IntRelType irt0, int n, int c0)
00230          : Test("Rel::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),n,0,1,
00231                 n==1),
00232            irt(irt0), c(c0) {}
00234        virtual bool solution(const Assignment& x) const {
00235          if (x.size() == 1)
00236            return cmp(x[0],irt,c);
00237          else
00238            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00239        }
00241        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00242          using namespace Gecode;
00243          if (x.size() == 1) {
00244            rel(home, channel(home,x[0]), irt, c);
00245          } else {
00246            BoolVarArgs y(2);
00247            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00248            rel(home, y, irt, c);
00249          }
00250        }
00252        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00253                          Gecode::Reify r) {
00254          assert(x.size() == 1);
00255          using namespace Gecode;
00256          rel(home, channel(home,x[0]), irt, c, r);
00257        }
00258      };
00259 
00261      class IntSeq : public Test {
00262      protected:
00264        Gecode::IntRelType irt;
00265      public:
00267        IntSeq(int n, Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
00268          : Test("Rel::Int::Seq::"+str(n)+"::"+str(irt0)+"::"+str(ipl),
00269                 n,-3,3,false,ipl),
00270            irt(irt0) {}
00272        virtual bool solution(const Assignment& x) const {
00273          if (irt == Gecode::IRT_NQ) {
00274            if (x.size() < 2)
00275              return false;
00276            for (int i=0; i<x.size()-1; i++)
00277              if (x[i] != x[i+1])
00278                return true;
00279            return false;
00280          } else {
00281            for (int i=0; i<x.size()-1; i++)
00282              if (!cmp(x[i],irt,x[i+1]))
00283                return false;
00284            return true;
00285          }
00286        }
00288        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00289          Gecode::rel(home, x, irt, ipl);
00290        }
00291      };
00292 
00294      class IntSharedSeq : public Test {
00295      protected:
00297        Gecode::IntRelType irt;
00298      public:
00300        IntSharedSeq(int n, Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
00301          : Test("Rel::Int::Seq::Shared::"+str(n)+"::"+str(irt0)+"::"+str(ipl),
00302                 n,-3,3,false,ipl),
00303            irt(irt0) {}
00305        virtual bool solution(const Assignment& x) const {
00306          if (irt == Gecode::IRT_NQ) {
00307            if (x.size() < 2)
00308              return false;
00309            for (int i=0; i<x.size()-1; i++)
00310              if (x[i] != x[i+1])
00311                return true;
00312            return false;
00313          } else {
00314            int n = x.size();
00315            for (int i=0; i<2*n-1; i++)
00316              if (!cmp(x[i % n],irt,x[(i+1) % n]))
00317                return false;
00318            return true;
00319          }
00320        }
00322        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00323          using namespace Gecode;
00324          int n = x.size();
00325          IntVarArgs y(2*n);
00326          for (int i=n; i--; )
00327            y[i] = y[n+i] = x[i];
00328          rel(home, y, irt, ipl);
00329        }
00330      };
00331 
00333      class BoolSeq : public Test {
00334      protected:
00336        Gecode::IntRelType irt;
00337      public:
00339        BoolSeq(int n, Gecode::IntRelType irt0)
00340          : Test("Rel::Bool::Seq::"+str(n)+"::"+str(irt0),n,0,1),
00341            irt(irt0) {}
00343        virtual bool solution(const Assignment& x) const {
00344          if (irt == Gecode::IRT_NQ) {
00345            if (x.size() < 2)
00346              return false;
00347            for (int i=0; i<x.size()-1; i++)
00348              if (x[i] != x[i+1])
00349                return true;
00350            return false;
00351          } else {
00352            for (int i=0; i<x.size()-1; i++)
00353              if (!cmp(x[i],irt,x[i+1]))
00354                return false;
00355            return true;
00356          }
00357        }
00359        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00360          using namespace Gecode;
00361          BoolVarArgs b(x.size());
00362          for (int i=x.size(); i--; )
00363            b[i]=channel(home,x[i]);
00364          rel(home, b, irt);
00365        }
00366      };
00367 
00369      class BoolSharedSeq : public Test {
00370      protected:
00372        Gecode::IntRelType irt;
00373      public:
00375        BoolSharedSeq(int n, Gecode::IntRelType irt0)
00376          : Test("Rel::Bool::Seq::Shared::"+str(n)+"::"+str(irt0),n,0,1),
00377            irt(irt0) {}
00379        virtual bool solution(const Assignment& x) const {
00380          if (irt == Gecode::IRT_NQ) {
00381            if (x.size() < 2)
00382              return false;
00383            for (int i=0; i<x.size()-1; i++)
00384              if (x[i] != x[i+1])
00385                return true;
00386            return false;
00387          } else {
00388            int n = x.size();
00389            for (int i=0; i<2*n-1; i++)
00390              if (!cmp(x[i % n],irt,x[(i+1) % n]))
00391                return false;
00392            return true;
00393          }
00394        }
00396        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00397          using namespace Gecode;
00398          int n = x.size();
00399          BoolVarArgs b(2*n);
00400          for (int i=n; i--; )
00401            b[i]=b[n+i]=channel(home,x[i]);
00402          rel(home, b, irt);
00403        }
00404      };
00405 
00407      class IntArrayVar : public Test {
00408      protected:
00410        Gecode::IntRelType irt;
00411      public:
00413        IntArrayVar(Gecode::IntRelType irt0)
00414          : Test("Rel::Int::Array::Var::"+str(irt0),6,-2,2), irt(irt0) {}
00416        virtual bool solution(const Assignment& x) const {
00417          int n=x.size() >> 1;
00418          for (int i=0; i<n; i++)
00419            if (x[i] != x[n+i])
00420              return cmp(x[i],irt,x[n+i]);
00421          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00422                  (irt == Gecode::IRT_EQ));
00423          GECODE_NEVER;
00424          return false;
00425        }
00427        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00428          using namespace Gecode;
00429          int n=x.size() >> 1;
00430          IntVarArgs y(n); IntVarArgs z(n);
00431          for (int i=0; i<n; i++) {
00432            y[i]=x[i]; z[i]=x[n+i];
00433          }
00434          rel(home, y, irt, z);
00435        }
00436      };
00437 
00439      class IntArrayInt : public Test {
00440      protected:
00442        Gecode::IntRelType irt;
00443      public:
00445        IntArrayInt(Gecode::IntRelType irt0)
00446          : Test("Rel::Int::Array::Int::"+str(irt0),3,-2,2), irt(irt0) {}
00448        virtual bool solution(const Assignment& x) const {
00449          Gecode::IntArgs y({0,0,0});
00450          int n=x.size();
00451          for (int i=0; i<n; i++)
00452            if (x[i] != y[i])
00453              return cmp(x[i],irt,y[i]);
00454          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00455                  (irt == Gecode::IRT_EQ));
00456          GECODE_NEVER;
00457          return false;
00458        }
00460        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00461          using namespace Gecode;
00462          IntArgs y({0,0,0});
00463          rel(home, x, irt, y);
00464        }
00465      };
00466 
00468      class IntArrayDiff : public Test {
00469      protected:
00471        Gecode::IntRelType irt;
00473        static const int n = 4;
00475        int n_fst;
00476      public:
00478        IntArrayDiff(Gecode::IntRelType irt0, int m)
00479          : Test("Rel::Int::Array::"+str(irt0)+"::"+str(m)+"::"+str(n-m),
00480                 n,-2,2),
00481            irt(irt0), n_fst(m) {
00482          assert(n_fst <= n);
00483        }
00485        virtual bool solution(const Assignment& x) const {
00486          int n_snd = n - n_fst;
00487          for (int i=0; i<std::min(n_fst,n_snd); i++)
00488            if (x[i] != x[n_fst+i])
00489              return cmp(x[i],irt,x[n_fst+i]);
00490          return cmp(n_fst,irt,n_snd);
00491        }
00493        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00494          using namespace Gecode;
00495          int n_snd = n - n_fst;
00496          IntVarArgs y(n_fst); IntVarArgs z(n_snd);
00497          for (int i=0; i<n_fst; i++) {
00498            y[i]=x[i];
00499          }
00500          for (int i=0; i<n_snd; i++) {
00501            z[i]=x[n_fst + i];
00502          }
00503          rel(home, y, irt, z);
00504        }
00505      };
00506 
00508      class BoolArrayVar : public Test {
00509      protected:
00511        Gecode::IntRelType irt;
00512      public:
00514        BoolArrayVar(Gecode::IntRelType irt0)
00515          : Test("Rel::Bool::Array::Var::"+str(irt0),10,0,1), irt(irt0) {}
00517        virtual bool solution(const Assignment& x) const {
00518          int n=x.size() >> 1;
00519          for (int i=0; i<n; i++)
00520            if (x[i] != x[n+i])
00521              return cmp(x[i],irt,x[n+i]);
00522          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00523                  (irt == Gecode::IRT_EQ));
00524          GECODE_NEVER;
00525          return false;
00526        }
00528        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00529          using namespace Gecode;
00530          int n=x.size() >> 1;
00531          BoolVarArgs y(n); BoolVarArgs z(n);
00532          for (int i=0; i<n; i++) {
00533            y[i]=channel(home,x[i]); z[i]=channel(home,x[n+i]);
00534          }
00535          rel(home, y, irt, z);
00536        }
00537      };
00538 
00540      class BoolArrayInt : public Test {
00541      protected:
00543        Gecode::IntRelType irt;
00544      public:
00546        BoolArrayInt(Gecode::IntRelType irt0)
00547          : Test("Rel::Bool::Array::Int::"+str(irt0),5,0,1), irt(irt0) {}
00549        virtual bool solution(const Assignment& x) const {
00550          Gecode::IntArgs y({0,0,1,0,0});
00551          for (int i=0; i<5; i++)
00552            if (x[i] != y[i])
00553              return cmp(x[i],irt,y[i]);
00554          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00555                  (irt == Gecode::IRT_EQ));
00556          GECODE_NEVER;
00557          return false;
00558        }
00560        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00561          using namespace Gecode;
00562          Gecode::IntArgs z({0,0,1,0,0});
00563          int n=x.size();
00564          BoolVarArgs y(n);
00565          for (int i=0; i<n; i++)
00566            y[i]=channel(home,x[i]);
00567          rel(home, y, irt, z);
00568        }
00569      };
00570 
00572      class Create {
00573      public:
00575        Create(void) {
00576          using namespace Gecode;
00577          for (IntRelTypes irts; irts(); ++irts) {
00578            for (IntPropLevels ipls; ipls(); ++ipls) {
00579              (void) new IntVarXY(irts.irt(),1,ipls.ipl());
00580              (void) new IntVarXY(irts.irt(),2,ipls.ipl());
00581              (void) new IntVarXX(irts.irt(),ipls.ipl());
00582              (void) new IntSeq(1,irts.irt(),ipls.ipl());
00583              (void) new IntSeq(2,irts.irt(),ipls.ipl());
00584              (void) new IntSeq(3,irts.irt(),ipls.ipl());
00585              (void) new IntSeq(5,irts.irt(),ipls.ipl());
00586              (void) new IntSharedSeq(1,irts.irt(),ipls.ipl());
00587              (void) new IntSharedSeq(2,irts.irt(),ipls.ipl());
00588              (void) new IntSharedSeq(3,irts.irt(),ipls.ipl());
00589              (void) new IntSharedSeq(4,irts.irt(),ipls.ipl());
00590            }
00591            (void) new BoolVarXY(irts.irt(),1);
00592            (void) new BoolVarXY(irts.irt(),2);
00593            (void) new BoolVarXX(irts.irt());
00594            (void) new BoolSeq(1,irts.irt());
00595            (void) new BoolSeq(2,irts.irt());
00596            (void) new BoolSeq(3,irts.irt());
00597            (void) new BoolSeq(10,irts.irt());
00598            (void) new BoolSharedSeq(1,irts.irt());
00599            (void) new BoolSharedSeq(2,irts.irt());
00600            (void) new BoolSharedSeq(3,irts.irt());
00601            (void) new BoolSharedSeq(4,irts.irt());
00602            (void) new BoolSharedSeq(8,irts.irt());
00603            for (int c=-4; c<=4; c++) {
00604              (void) new IntInt(irts.irt(),1,c);
00605              (void) new IntInt(irts.irt(),2,c);
00606            }
00607            for (int c=0; c<=1; c++) {
00608              (void) new BoolInt(irts.irt(),1,c);
00609              (void) new BoolInt(irts.irt(),2,c);
00610            }
00611            (void) new IntArrayVar(irts.irt());
00612            (void) new IntArrayInt(irts.irt());
00613            for (int n_fst=0; n_fst<=4; n_fst++)
00614              (void) new IntArrayDiff(irts.irt(),n_fst);
00615            (void) new BoolArrayVar(irts.irt());
00616            (void) new BoolArrayInt(irts.irt());
00617          }
00618        }
00619      };
00620 
00621      Create c;
00623 
00624    }
00625 }}
00626 
00627 // STATISTICS: test-int