Generated on Wed Jul 27 2011 15:08:45 for Gecode by doxygen 1.7.4
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  *  Last modified:
00010  *     $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $
00011  *     $Revision: 10684 $
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 Rel {
00046 
00052 
00053      class IntVarXY : public Test {
00054      protected:
00056        Gecode::IntRelType irt;
00057      public:
00059        IntVarXY(Gecode::IntRelType irt0, int n, Gecode::IntConLevel icl)
00060          : Test("Rel::Int::Var::XY::"+str(irt0)+"::"+str(icl)+"::"+str(n),
00061                 n+1,-3,3,n==1,icl),
00062            irt(irt0) {}
00064        virtual bool solution(const Assignment& x) const {
00065          if (x.size() == 2) {
00066            return cmp(x[0],irt,x[1]);
00067          } else {
00068            return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]);
00069          }
00070        }
00072        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00073          using namespace Gecode;
00074          if (x.size() == 2) {
00075            rel(home, x[0], irt, x[1], icl);
00076          } else {
00077            IntVarArgs y(2);
00078            y[0]=x[0]; y[1]=x[1];
00079            rel(home, y, irt, x[2], icl);
00080          }
00081        }
00083        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00084                          Gecode::BoolVar b) {
00085          assert(x.size() == 2);
00086          Gecode::rel(home, x[0], irt, x[1], b, icl);
00087        }
00088      };
00089 
00091      class IntVarXX : public Test {
00092      protected:
00094        Gecode::IntRelType irt;
00095      public:
00097        IntVarXX(Gecode::IntRelType irt0, Gecode::IntConLevel icl)
00098          : Test("Rel::Int::Var::XX::"+str(irt0)+"::"+str(icl),
00099                 1,-3,3,true,icl),
00100            irt(irt0) {
00101          contest = ((irt != Gecode::IRT_LE) &&
00102                     (irt != Gecode::IRT_GR) &&
00103                     (irt != Gecode::IRT_NQ))
00104            ? CTL_DOMAIN : CTL_NONE;
00105        }
00107        virtual bool solution(const Assignment& x) const {
00108          return cmp(x[0],irt,x[0]);
00109        }
00111        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00112          Gecode::rel(home, x[0], irt, x[0], icl);
00113        }
00115        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00116                          Gecode::BoolVar b) {
00117          Gecode::rel(home, x[0], irt, x[0], b, icl);
00118        }
00119      };
00120 
00122      class BoolVarXY : public Test {
00123      protected:
00125        Gecode::IntRelType irt;
00126      public:
00128        BoolVarXY(Gecode::IntRelType irt0, int n)
00129          : Test("Rel::Bool::Var::XY::"+str(irt0)+"::"+str(n),n+1,0,1,
00130                 n==1),
00131            irt(irt0) {}
00133        virtual bool solution(const Assignment& x) const {
00134          if (x.size() == 2) {
00135            return cmp(x[0],irt,x[1]);
00136          } else {
00137            return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]);
00138          }
00139        }
00141        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00142          using namespace Gecode;
00143          if (x.size() == 2) {
00144            rel(home, channel(home,x[0]), irt, channel(home,x[1]));
00145          } else {
00146            BoolVarArgs y(2);
00147            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00148            rel(home, y, irt, channel(home,x[2]));
00149          }
00150        }
00152        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00153                          Gecode::BoolVar b) {
00154          assert(x.size() == 2);
00155          using namespace Gecode;
00156          rel(home,
00157              channel(home,x[0]), irt, channel(home,x[1]),
00158              b);
00159        }
00160      };
00161 
00163      class BoolVarXX : public Test {
00164      protected:
00166        Gecode::IntRelType irt;
00167      public:
00169        BoolVarXX(Gecode::IntRelType irt0)
00170          : Test("Rel::Bool::Var::XX::"+str(irt0),1,0,1),
00171            irt(irt0) {
00172          contest = ((irt != Gecode::IRT_LE) &&
00173                     (irt != Gecode::IRT_GR) &&
00174                     (irt != Gecode::IRT_NQ))
00175            ? CTL_DOMAIN : CTL_NONE;
00176        }
00178        virtual bool solution(const Assignment& x) const {
00179          return cmp(x[0],irt,x[0]);
00180        }
00182        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00183          Gecode::BoolVar b = Gecode::channel(home,x[0]);
00184          Gecode::rel(home, b, irt, b);
00185        }
00186      };
00187 
00189      class IntInt : public Test {
00190      protected:
00192        Gecode::IntRelType irt;
00194        int c;
00195      public:
00197        IntInt(Gecode::IntRelType irt0, int n, int c0)
00198          : Test("Rel::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),
00199                 n,-3,3,n==1),
00200            irt(irt0), c(c0) {}
00202        virtual bool solution(const Assignment& x) const {
00203          if (x.size() == 1)
00204            return cmp(x[0],irt,c);
00205          else
00206            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00207        }
00209        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00210          using namespace Gecode;
00211          if (x.size() == 1)
00212            rel(home, x[0], irt, c);
00213          else
00214            rel(home, x, irt, c);
00215        }
00217        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00218                          Gecode::BoolVar b) {
00219          assert(x.size() == 1);
00220          Gecode::rel(home, x[0], irt, c, b);
00221        }
00222      };
00223 
00225      class BoolInt : public Test {
00226      protected:
00228        Gecode::IntRelType irt;
00230        int c;
00231      public:
00233        BoolInt(Gecode::IntRelType irt0, int n, int c0)
00234          : Test("Rel::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),n,0,1,
00235                 n==1),
00236            irt(irt0), c(c0) {}
00238        virtual bool solution(const Assignment& x) const {
00239          if (x.size() == 1)
00240            return cmp(x[0],irt,c);
00241          else
00242            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00243        }
00245        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00246          using namespace Gecode;
00247          if (x.size() == 1) {
00248            rel(home, channel(home,x[0]), irt, c);
00249          } else {
00250            BoolVarArgs y(2);
00251            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00252            rel(home, y, irt, c);
00253          }
00254        }
00256        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x,
00257                          Gecode::BoolVar b) {
00258          assert(x.size() == 1);
00259          using namespace Gecode;
00260          rel(home, channel(home,x[0]), irt, c, b);
00261        }
00262      };
00263 
00265      class IntPairwise : public Test {
00266      protected:
00268        Gecode::IntRelType irt;
00269      public:
00271        IntPairwise(Gecode::IntRelType irt0, Gecode::IntConLevel icl)
00272          : Test("Rel::Int::Pairwise::"+str(irt0)+"::"+str(icl),
00273                    4,-3,3,false,icl),
00274            irt(irt0) {}
00276        virtual bool solution(const Assignment& x) const {
00277          for (int i=0; i<x.size(); i++)
00278            for (int j=i+1; j<x.size(); j++)
00279              if (!cmp(x[i],irt,x[j]))
00280                return false;
00281          return true;
00282        }
00284        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00285          Gecode::rel(home, x, irt, icl);
00286        }
00287      };
00288 
00290      class BoolPairwise : public Test {
00291      protected:
00293        Gecode::IntRelType irt;
00294      public:
00296        BoolPairwise(Gecode::IntRelType irt0)
00297          : Test("Rel::Bool::Pairwise::"+str(irt0),8,0,1),
00298            irt(irt0) {}
00300        virtual bool solution(const Assignment& x) const {
00301          for (int i=0; i<x.size(); i++)
00302            for (int j=i+1; j<x.size(); j++)
00303              if (!cmp(x[i],irt,x[j]))
00304                return false;
00305          return true;
00306        }
00308        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00309          using namespace Gecode;
00310          BoolVarArgs b(x.size());
00311          for (int i=x.size(); i--; )
00312            b[i]=channel(home,x[i]);
00313          rel(home, b, irt);
00314        }
00315      };
00316 
00318      class IntArray : public Test {
00319      protected:
00321        Gecode::IntRelType irt;
00322      public:
00324        IntArray(Gecode::IntRelType irt0)
00325          : Test("Rel::Int::Array::"+str(irt0),6,-2,2), irt(irt0) {}
00327        virtual bool solution(const Assignment& x) const {
00328          int n=x.size() >> 1;
00329          for (int i=0; i<n; i++)
00330            if (x[i] != x[n+i])
00331              return cmp(x[i],irt,x[n+i]);
00332          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00333                  (irt == Gecode::IRT_EQ));
00334          GECODE_NEVER;
00335          return false;
00336        }
00338        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00339          using namespace Gecode;
00340          int n=x.size() >> 1;
00341          IntVarArgs y(n); IntVarArgs z(n);
00342          for (int i=0; i<n; i++) {
00343            y[i]=x[i]; z[i]=x[n+i];
00344          }
00345          rel(home, y, irt, z);
00346        }
00347      };
00348 
00350      class BoolArray : public Test {
00351      protected:
00353        Gecode::IntRelType irt;
00354      public:
00356        BoolArray(Gecode::IntRelType irt0)
00357          : Test("Rel::Bool::Array::"+str(irt0),10,0,1), irt(irt0) {}
00359        virtual bool solution(const Assignment& x) const {
00360          int n=x.size() >> 1;
00361          for (int i=0; i<n; i++)
00362            if (x[i] != x[n+i])
00363              return cmp(x[i],irt,x[n+i]);
00364          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) ||
00365                  (irt == Gecode::IRT_EQ));
00366          GECODE_NEVER;
00367          return false;
00368        }
00370        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00371          using namespace Gecode;
00372          int n=x.size() >> 1;
00373          BoolVarArgs y(n); BoolVarArgs z(n);
00374          for (int i=0; i<n; i++) {
00375            y[i]=channel(home,x[i]); z[i]=channel(home,x[n+i]);
00376          }
00377          rel(home, y, irt, z);
00378        }
00379      };
00380 
00382      class Create {
00383      public:
00385        Create(void) {
00386          using namespace Gecode;
00387          for (IntRelTypes irts; irts(); ++irts) {
00388            for (IntConLevels icls; icls(); ++icls) {
00389              (void) new IntVarXY(irts.irt(),1,icls.icl());
00390              (void) new IntVarXY(irts.irt(),2,icls.icl());
00391              (void) new IntVarXX(irts.irt(),icls.icl());
00392              (void) new IntPairwise(irts.irt(),icls.icl());
00393            }
00394            (void) new BoolVarXY(irts.irt(),1);
00395            (void) new BoolVarXY(irts.irt(),2);
00396            (void) new BoolVarXX(irts.irt());
00397            (void) new BoolPairwise(irts.irt());
00398            for (int c=-4; c<=4; c++) {
00399              (void) new IntInt(irts.irt(),1,c);
00400              (void) new IntInt(irts.irt(),2,c);
00401            }
00402            for (int c=0; c<=1; c++) {
00403              (void) new BoolInt(irts.irt(),1,c);
00404              (void) new BoolInt(irts.irt(),2,c);
00405            }
00406            (void) new IntArray(irts.irt());
00407            (void) new BoolArray(irts.irt());
00408          }
00409        }
00410      };
00411 
00412      Create c;
00414 
00415    }
00416 }}
00417 
00418 // STATISTICS: test-int