Generated on Mon Aug 25 11:35:33 2008 for Gecode by doxygen 1.5.6

rel.cc

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: 2008-07-11 09:18:29 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00011  *     $Revision: 7280 $
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          testdomcon = ((irt != Gecode::IRT_LE) && 
00102                        (irt != Gecode::IRT_GR) &&
00103                        (irt != Gecode::IRT_NQ));
00104        }
00106        virtual bool solution(const Assignment& x) const {
00107          return cmp(x[0],irt,x[0]);
00108        }
00110        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00111          Gecode::rel(home, x[0], irt, x[0], icl);
00112        }
00114        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x, 
00115                          Gecode::BoolVar b) {
00116          Gecode::rel(home, x[0], irt, x[0], b, icl);
00117        }
00118      };
00119    
00121      class BoolVarXY : public Test {
00122      protected:
00124        Gecode::IntRelType irt;
00125      public:
00127        BoolVarXY(Gecode::IntRelType irt0, int n) 
00128          : Test("Rel::Bool::Var::XY::"+str(irt0)+"::"+str(n),n+1,0,1,
00129                 n==1), 
00130            irt(irt0) {}
00132        virtual bool solution(const Assignment& x) const {
00133          if (x.size() == 2) {
00134            return cmp(x[0],irt,x[1]);
00135          } else {
00136            return cmp(x[0],irt,x[2]) && cmp(x[1],irt,x[2]);
00137          }
00138        }
00140        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00141          using namespace Gecode;
00142          if (x.size() == 2) {
00143            rel(home, channel(home,x[0]), irt, channel(home,x[1]));
00144          } else {
00145            BoolVarArgs y(2);
00146            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00147            rel(home, y, irt, channel(home,x[2]));
00148          }
00149        }
00151        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x, 
00152                          Gecode::BoolVar b) {
00153          assert(x.size() == 2);
00154          using namespace Gecode;
00155          rel(home, 
00156              channel(home,x[0]), irt, channel(home,x[1]), 
00157              b);
00158        }
00159      };
00160    
00162      class BoolVarXX : public Test {
00163      protected:
00165        Gecode::IntRelType irt;
00166      public:
00168        BoolVarXX(Gecode::IntRelType irt0) 
00169          : Test("Rel::Bool::Var::XX::"+str(irt0),1,0,1), 
00170            irt(irt0) {
00171          testdomcon = ((irt != Gecode::IRT_LE) && 
00172                        (irt != Gecode::IRT_GR) &&
00173                        (irt != Gecode::IRT_NQ));
00174        }
00176        virtual bool solution(const Assignment& x) const {
00177          return cmp(x[0],irt,x[0]);
00178        }
00180        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00181          Gecode::BoolVar b = Gecode::channel(home,x[0]);
00182          Gecode::rel(home, b, irt, b);
00183        }
00184      };
00185    
00187      class IntInt : public Test {
00188      protected:
00190        Gecode::IntRelType irt;
00192        int c;
00193      public:
00195        IntInt(Gecode::IntRelType irt0, int n, int c0) 
00196          : Test("Rel::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),
00197                 n,-3,3,n==1), 
00198            irt(irt0), c(c0) {}
00200        virtual bool solution(const Assignment& x) const {
00201          if (x.size() == 1)
00202            return cmp(x[0],irt,c);
00203          else
00204            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00205        }
00207        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00208          using namespace Gecode;
00209          if (x.size() == 1)
00210            rel(home, x[0], irt, c);
00211          else
00212            rel(home, x, irt, c);
00213        }
00215        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x, 
00216                          Gecode::BoolVar b) {
00217          assert(x.size() == 1);
00218          Gecode::rel(home, x[0], irt, c, b);
00219        }
00220      };
00221    
00223      class BoolInt : public Test {
00224      protected:
00226        Gecode::IntRelType irt;
00228        int c;
00229      public:
00231        BoolInt(Gecode::IntRelType irt0, int n, int c0) 
00232          : Test("Rel::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(c0),n,0,1,
00233                 n==1), 
00234            irt(irt0), c(c0) {}
00236        virtual bool solution(const Assignment& x) const {
00237          if (x.size() == 1)
00238            return cmp(x[0],irt,c);
00239          else
00240            return cmp(x[0],irt,c) && cmp(x[1],irt,c);
00241        }
00243        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00244          using namespace Gecode;
00245          if (x.size() == 1) {
00246            rel(home, channel(home,x[0]), irt, c);
00247          } else {
00248            BoolVarArgs y(2);
00249            y[0]=channel(home,x[0]); y[1]=channel(home,x[1]);
00250            rel(home, y, irt, c);
00251          }
00252        }
00254        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x, 
00255                          Gecode::BoolVar b) {
00256          assert(x.size() == 1);
00257          using namespace Gecode;
00258          rel(home, channel(home,x[0]), irt, c, b);
00259        }
00260      };
00261    
00263      class IntPairwise : public Test {
00264      protected:
00266        Gecode::IntRelType irt;
00267      public:
00269        IntPairwise(Gecode::IntRelType irt0, Gecode::IntConLevel icl)
00270          : Test("Rel::Int::Pairwise::"+str(irt0)+"::"+str(icl),
00271                    4,-3,3,false,icl), 
00272            irt(irt0) {}
00274        virtual bool solution(const Assignment& x) const {
00275          for (int i=0; i<x.size(); i++)
00276            for (int j=i+1; j<x.size(); j++)
00277              if (!cmp(x[i],irt,x[j]))
00278                return false;
00279          return true;
00280        }
00282        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00283          Gecode::rel(home, x, irt, icl);
00284        }
00285      };
00286    
00288      class BoolPairwise : public Test {
00289      protected:
00291        Gecode::IntRelType irt;
00292      public:
00294        BoolPairwise(Gecode::IntRelType irt0)
00295          : Test("Rel::Bool::Pairwise::"+str(irt0),8,0,1), 
00296            irt(irt0) {}
00298        virtual bool solution(const Assignment& x) const {
00299          for (int i=0; i<x.size(); i++)
00300            for (int j=i+1; j<x.size(); j++)
00301              if (!cmp(x[i],irt,x[j]))
00302                return false;
00303          return true;
00304        }
00306        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00307          using namespace Gecode;
00308          BoolVarArgs b(x.size());
00309          for (int i=x.size(); i--; )
00310            b[i]=channel(home,x[i]);
00311          rel(home, b, irt);
00312        }
00313      };
00314    
00316      class IntArray : public Test {
00317      protected:
00319        Gecode::IntRelType irt;
00320      public:
00322        IntArray(Gecode::IntRelType irt0)
00323          : Test("Rel::Int::Array::"+str(irt0),6,-2,2), irt(irt0) {}
00325        virtual bool solution(const Assignment& x) const {
00326          int n=x.size() >> 1;
00327          for (int i=0; i<n; i++)
00328            if (x[i] != x[n+i])
00329              return cmp(x[i],irt,x[n+i]);
00330          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) || 
00331                  (irt == Gecode::IRT_EQ));
00332          GECODE_NEVER;
00333          return false;
00334        }
00336        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00337          using namespace Gecode;
00338          int n=x.size() >> 1;
00339          IntVarArgs y(n); IntVarArgs z(n);
00340          for (int i=0; i<n; i++) {
00341            y[i]=x[i]; z[i]=x[n+i];
00342          }
00343          rel(home, y, irt, z);
00344        }
00345      };
00346    
00348      class BoolArray : public Test {
00349      protected:
00351        Gecode::IntRelType irt;
00352      public:
00354        BoolArray(Gecode::IntRelType irt0)
00355          : Test("Rel::Bool::Array::"+str(irt0),10,0,1), irt(irt0) {}
00357        virtual bool solution(const Assignment& x) const {
00358          int n=x.size() >> 1;
00359          for (int i=0; i<n; i++)
00360            if (x[i] != x[n+i])
00361              return cmp(x[i],irt,x[n+i]);
00362          return ((irt == Gecode::IRT_LQ) || (irt == Gecode::IRT_GQ) || 
00363                  (irt == Gecode::IRT_EQ));
00364          GECODE_NEVER;
00365          return false;
00366        }
00368        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00369          using namespace Gecode;
00370          int n=x.size() >> 1;
00371          BoolVarArgs y(n); BoolVarArgs z(n);
00372          for (int i=0; i<n; i++) {
00373            y[i]=channel(home,x[i]); z[i]=channel(home,x[n+i]);
00374          }
00375          rel(home, y, irt, z);
00376        }
00377      };
00378    
00380      class Create {
00381      public:
00383        Create(void) {
00384          using namespace Gecode;
00385          for (IntRelTypes irts; irts(); ++irts) {
00386            for (IntConLevels icls; icls(); ++icls) {
00387              (void) new IntVarXY(irts.irt(),1,icls.icl());
00388              (void) new IntVarXY(irts.irt(),2,icls.icl());
00389              (void) new IntVarXX(irts.irt(),icls.icl());
00390              (void) new IntPairwise(irts.irt(),icls.icl());
00391            }
00392            (void) new BoolVarXY(irts.irt(),1);
00393            (void) new BoolVarXY(irts.irt(),2);
00394            (void) new BoolVarXX(irts.irt());
00395            (void) new BoolPairwise(irts.irt());
00396            for (int c=-4; c<=4; c++) {
00397              (void) new IntInt(irts.irt(),1,c);
00398              (void) new IntInt(irts.irt(),2,c);
00399            }
00400            for (int c=0; c<=1; c++) {
00401              (void) new BoolInt(irts.irt(),1,c);
00402              (void) new BoolInt(irts.irt(),2,c);
00403            }
00404            (void) new IntArray(irts.irt());
00405            (void) new BoolArray(irts.irt());
00406          }      
00407        }
00408      };
00409    
00410      Create c;
00412    
00413    }
00414 }}
00415 
00416 // STATISTICS: test-int