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

extensional.cc

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Mikael Lagerkvist <lagerkvist@gecode.org>
00005  *     Christian Schulte <schulte@gecode.org>
00006  *
00007  *  Copyright:
00008  *     Mikael Lagerkvist, 2007
00009  *     Christian Schulte, 2005
00010  *
00011  *  Last modified:
00012  *     $Date: 2008-07-11 09:20:23 +0200 (Fri, 11 Jul 2008) $ by $Author: tack $
00013  *     $Revision: 7282 $
00014  *
00015  *  This file is part of Gecode, the generic constraint
00016  *  development environment:
00017  *     http://www.gecode.org
00018  *
00019  *  Permission is hereby granted, free of charge, to any person obtaining
00020  *  a copy of this software and associated documentation files (the
00021  *  "Software"), to deal in the Software without restriction, including
00022  *  without limitation the rights to use, copy, modify, merge, publish,
00023  *  distribute, sublicense, and/or sell copies of the Software, and to
00024  *  permit persons to whom the Software is furnished to do so, subject to
00025  *  the following conditions:
00026  *
00027  *  The above copyright notice and this permission notice shall be
00028  *  included in all copies or substantial portions of the Software.
00029  *
00030  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00031  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00032  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00033  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00034  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00035  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00036  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00037  *
00038  */
00039 
00040 #include "test/int.hh"
00041 
00042 #include "gecode/minimodel.hh"
00043 
00044 namespace Test { namespace Int {
00045 
00047    namespace Extensional {
00048    
00054 
00055      class RegSimpleA : public Test {
00056      public:
00058        RegSimpleA(void) : Test("Extensional::Reg::Simple::A",4,2,2) {}
00060        virtual bool solution(const Assignment& x) const {
00061          return (((x[0] == 0) || (x[0] == 2)) &&
00062                  ((x[1] == -1) || (x[1] == 1)) &&
00063                  ((x[2] == 0) || (x[2] == 1)) &&
00064                  ((x[3] == 0) || (x[3] == 1)));
00065        }
00067        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00068          using namespace Gecode;
00069          extensional(home, x,
00070                      (REG(0) | REG(2)) +
00071                      (REG(-1) | REG(1)) +
00072                      (REG(7) | REG(0) | REG(1)) +
00073                      (REG(0) | REG(1)));
00074        }
00075      };
00076      
00078      class RegSimpleB : public Test {
00079      public:
00081        RegSimpleB(void) : Test("Extensional::Reg::Simple::B",4,2,2) {}
00083        virtual bool solution(const Assignment& x) const {
00084          return (x[0]<x[1]) && (x[1]<x[2]) && (x[2]<x[3]);
00085        }
00087        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00088          using namespace Gecode;
00089          extensional(home, x,
00090                      (REG(-2) + REG(-1) + REG(0) + REG(1)) |
00091                      (REG(-2) + REG(-1) + REG(0) + REG(2)) |
00092                      (REG(-2) + REG(-1) + REG(1) + REG(2)) |
00093                      (REG(-2) + REG(0) + REG(1) + REG(2)) |
00094                      (REG(-1) + REG(0) + REG(1) + REG(2)));
00095          }
00096      };
00097 
00099      class RegSimpleC : public Test {
00100      public:
00102        RegSimpleC(void) : Test("Extensional::Reg::Simple::C",6,0,1) {}
00104        virtual bool solution(const Assignment& x) const {
00105          int pos = 0;
00106          int s = x.size();
00107 
00108          while (pos < s && x[pos] == 0) ++pos;
00109          if (pos + 4 > s) return false;
00110 
00111          for (int i = 0; i < 2; ++i, ++pos)
00112            if (x[pos] != 1) return false;
00113          if (pos + 2 > s) return false;
00114 
00115          for (int i = 0; i < 1; ++i, ++pos)
00116            if (x[pos] != 0) return false;
00117          while (pos < s && x[pos] == 0) ++pos;
00118          if (pos + 1 > s) return false;
00119 
00120          for (int i = 0; i < 1; ++i, ++pos)
00121            if (x[pos] != 1) return false;
00122          while (pos < s) if (x[pos++] != 0) return false;
00123          return true;
00124 
00125        }
00127        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00128          using namespace Gecode;
00129          extensional(home, x,
00130                      *REG(0) + REG(1)(2,2) + +REG(0) + REG(1)(1,1) + *REG(0));
00131        }
00132      };
00133      
00135      class RegDistinct : public Test {
00136      public:
00138        RegDistinct(void) : Test("Extensional::Reg::Distinct",4,-1,4) {}
00140        virtual bool solution(const Assignment& x) const {
00141          for (int i=0; i<x.size(); i++) {
00142            if ((x[i] < 0) || (x[i] > 3))
00143              return false;
00144            for (int j=i+1; j<x.size(); j++)
00145              if (x[i]==x[j])
00146                return false;
00147          }
00148          return true;
00149        }
00151        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00152          using namespace Gecode;
00153          extensional(home, x,
00154                      (REG(0)+REG(1)+REG(2)+REG(3)) |
00155                      (REG(0)+REG(1)+REG(3)+REG(2)) |
00156                      (REG(0)+REG(2)+REG(1)+REG(3)) |
00157                      (REG(0)+REG(2)+REG(3)+REG(1)) |
00158                      (REG(0)+REG(3)+REG(1)+REG(2)) |
00159                      (REG(0)+REG(3)+REG(2)+REG(1)) |
00160                      (REG(1)+REG(0)+REG(2)+REG(3)) |
00161                      (REG(1)+REG(0)+REG(3)+REG(2)) |
00162                      (REG(1)+REG(2)+REG(0)+REG(3)) |
00163                      (REG(1)+REG(2)+REG(3)+REG(0)) |
00164                      (REG(1)+REG(3)+REG(0)+REG(2)) |
00165                      (REG(1)+REG(3)+REG(2)+REG(0)) |
00166                      (REG(2)+REG(0)+REG(1)+REG(3)) |
00167                      (REG(2)+REG(0)+REG(3)+REG(1)) |
00168                      (REG(2)+REG(1)+REG(0)+REG(3)) |
00169                      (REG(2)+REG(1)+REG(3)+REG(0)) |
00170                      (REG(2)+REG(3)+REG(0)+REG(1)) |
00171                      (REG(2)+REG(3)+REG(1)+REG(0)) |
00172                      (REG(3)+REG(0)+REG(1)+REG(2)) |
00173                      (REG(3)+REG(0)+REG(2)+REG(1)) |
00174                      (REG(3)+REG(1)+REG(0)+REG(2)) |
00175                      (REG(3)+REG(1)+REG(2)+REG(0)) |
00176                      (REG(3)+REG(2)+REG(0)+REG(1)) |
00177                      (REG(3)+REG(2)+REG(1)+REG(0)));
00178        }
00179      };
00180      
00182      class RegSharedA : public Test {
00183      public:
00185        RegSharedA(void) : Test("Extensional::Reg::Shared::A",4,2,2) {}
00187        virtual bool solution(const Assignment& x) const {
00188          return (((x[0] == 0) || (x[0] == 2)) &&
00189                  ((x[1] == -1) || (x[1] == 1)) &&
00190                  ((x[2] == 0) || (x[2] == 1)) &&
00191                  ((x[3] == 0) || (x[3] == 1)));
00192        }
00194        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00195          using namespace Gecode;
00196          IntVarArgs y(8);
00197          for (int i=0; i<4; i++)
00198            y[i]=y[i+4]=x[i];
00199          unshare(home,y);
00200          extensional(home, y,
00201                      ((REG(0) | REG(2)) +
00202                       (REG(-1) | REG(1)) +
00203                       (REG(7) | REG(0) | REG(1)) +
00204                       (REG(0) | REG(1)))(2,2));
00205        }
00206      };
00207    
00209      class RegSharedB : public Test {
00210      public:
00212        RegSharedB(void) : Test("Extensional::Reg::Shared::B",4,2,2) {}
00214        virtual bool solution(const Assignment& x) const {
00215          return (((x[0] == 0) || (x[0] == 2)) &&
00216                  ((x[1] == -1) || (x[1] == 1)) &&
00217                  ((x[2] == 0) || (x[2] == 1)) &&
00218                  ((x[3] == 0) || (x[3] == 1)));
00219        }
00221        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00222          using namespace Gecode;
00223          IntVarArgs y(12);
00224          for (int i=0; i<4; i++)
00225            y[i]=y[i+4]=y[i+8]=x[i];
00226          unshare(home,y);
00227          extensional(home, y,
00228                      ((REG(0) | REG(2)) +
00229                       (REG(-1) | REG(1)) +
00230                       (REG(7) | REG(0) | REG(1)) +
00231                       (REG(0) | REG(1)))(3,3));
00232        }
00233      };
00234    
00236      class RegSharedC : public Test {
00237      public:
00239        RegSharedC(void) : Test("Extensional::Reg::Shared::C",4,0,1) {}
00241        virtual bool solution(const Assignment& x) const {
00242          return (x[1]==1) && (x[2]==0) && (x[3]==1);
00243        }
00245        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00246          using namespace Gecode;
00247          Gecode::BoolVarArgs y(8);
00248          for (int i=0; i<4; i++)
00249            y[i]=y[i+4]=channel(home,x[i]);
00250          unshare(home,y);
00251          extensional(home,y,
00252                      ((REG(0) | REG(1)) + REG(1) + REG(0) + REG(1))(2,2));
00253        }
00254      };
00255    
00257      class RegSharedD : public Test {
00258      public:
00260        RegSharedD(void) : Test("Extensional::Reg::Shared::D",4,0,1) {}
00262        virtual bool solution(const Assignment& x) const {
00263          return (x[1]==1) && (x[2]==0) && (x[3]==1);
00264        }
00266        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00267          using namespace Gecode;
00268          Gecode::BoolVarArgs y(12);
00269          for (int i=0; i<4; i++)
00270            y[i]=y[i+4]=y[i+8]=channel(home,x[i]);
00271          unshare(home, y);
00272          extensional(home, y,
00273                      ((REG(0) | REG(1)) + REG(1) + REG(0) + REG(1))(3,3));
00274        }
00275      };
00276    
00278      class RegEmptyDFA : public Test {
00279      public:
00281        RegEmptyDFA(void) : Test("Extensional::Reg::Empty::DFA",1,0,0) {
00282          testsearch = false;
00283        }
00285        virtual bool solution(const Assignment& x) const {
00286          (void)x;
00287          return false;
00288        }
00290        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00291          Gecode::DFA d;
00292          Gecode::extensional(home, x, d);
00293        }
00294      };
00295    
00297      class RegEmptyREG : public Test {
00298      public:
00300        RegEmptyREG(void) : Test("Extensional::Reg::Empty::REG",1,0,0) {
00301          testsearch = false;
00302        }
00304        virtual bool solution(const Assignment& x) const {
00305          (void)x;
00306          return false;
00307        }
00309        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00310          Gecode::REG r;
00311          Gecode::extensional(home, x, r);
00312        }
00313      };
00314    
00316      class TupleSetA : public Test {
00317      public:
00319        TupleSetA(Gecode::PropKind pk)
00320          : Test("Extensional::TupleSet::A::"+str(pk),
00321                 4,1,5,false,Gecode::ICL_DOM,pk) {}
00323        virtual bool solution(const Assignment& x) const {
00324          return ((x[0] == 1 && x[1] == 3 && x[2] == 2 && x[3] == 3) ||
00325                  (x[0] == 2 && x[1] == 1 && x[2] == 2 && x[3] == 4) ||
00326                  (x[0] == 2 && x[1] == 2 && x[2] == 1 && x[3] == 4) ||
00327                  (x[0] == 3 && x[1] == 3 && x[2] == 3 && x[3] == 2) ||
00328                  (x[0] == 4 && x[1] == 3 && x[2] == 4 && x[3] == 1)
00329                  );
00330        }
00332        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00333          using namespace Gecode;
00334          TupleSet t;
00335          IntArgs t1(4,  2, 1, 2, 4);
00336          IntArgs t2(4,  2, 2, 1, 4);
00337          IntArgs t3(4,  4, 3, 4, 1);
00338          IntArgs t4(4,  1, 3, 2, 3);
00339          IntArgs t5(4,  3, 3, 3, 2);
00340          t.add(t1);
00341          t.add(t2);
00342          t.add(t3);
00343          t.add(t4);
00344          t.add(t5);
00345          t.finalize();
00346 
00347          extensional(home, x, t, ICL_DEF, pk);
00348        }
00349      };
00350 
00352      class TupleSetB : public Test {
00353        mutable Gecode::TupleSet t;
00354      public:
00356        TupleSetB(Gecode::PropKind pk)
00357          : Test("Extensional::TupleSet::B::"+str(pk),
00358                 4,1,5,false,Gecode::ICL_DOM,pk) {
00359          using namespace Gecode;
00360          IntArgs t1 (4,  2, 1, 2, 4);
00361          IntArgs t2 (4,  2, 2, 1, 4);
00362          IntArgs t3 (4,  4, 3, 4, 1);
00363          IntArgs t4 (4,  1, 3, 2, 3);
00364          IntArgs t5 (4,  3, 3, 3, 2);
00365          IntArgs t6 (4,  5, 1, 4, 4);
00366          IntArgs t7 (4,  2, 5, 1, 5);
00367          IntArgs t8 (4,  4, 3, 5, 1);
00368          IntArgs t9 (4,  1, 5, 2, 5);
00369          IntArgs t10(4,  5, 3, 3, 2);
00370          t.add(t1);
00371          t.add(t2);
00372          t.add(t3);
00373          t.add(t4);
00374          t.add(t5);
00375          t.add(t6);
00376          t.add(t7);
00377          t.add(t8);
00378          t.add(t9);
00379          t.add(t10);
00380        }
00382        virtual bool solution(const Assignment& x) const {
00383          using namespace Gecode;
00384          for (int i = 0; i < t.tuples(); ++i) {
00385            TupleSet::Tuple l = t[i];
00386            bool same = true;
00387            for (int j = 0; j < t.arity() && same; ++j)
00388              if (l[j] != x[j]) same = false;
00389            if (same) return true;
00390          }
00391          return false;
00392        }
00394        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00395          using namespace Gecode;
00396          extensional(home, x, t, ICL_DEF, pk);
00397        }
00398      };
00399 
00400 
00401 
00403      class TupleSetBool : public Test {
00404        mutable Gecode::TupleSet t;
00405      public:
00407        TupleSetBool(Gecode::PropKind pk, double prob)
00408          : Test("Extensional::TupleSet::Bool::"+str(pk),
00409                 5,0,1,false,Gecode::ICL_DOM,pk) {
00410          using namespace Gecode;
00411          
00412          CpltAssignment ass(5, IntSet(0, 1));
00413          while (ass()) {
00414            if (Base::rand(100) <= prob*100) {
00415              IntArgs tuple(5);
00416              for (int i = 5; i--; ) tuple[i] = ass[i];
00417              t.add(tuple);
00418            }   
00419            ++ass;
00420          }
00421          t.finalize();
00422        }
00424        virtual bool solution(const Assignment& x) const {
00425          using namespace Gecode;
00426          for (int i = 0; i < t.tuples(); ++i) {
00427            TupleSet::Tuple l = t[i];
00428            bool same = true;
00429            for (int j = 0; j < t.arity() && same; ++j)
00430              if (l[j] != x[j]) same = false;
00431            if (same) return true;
00432          }
00433          return false;
00434        }
00436        virtual void post(Gecode::Space* home, Gecode::IntVarArray& x) {
00437          using namespace Gecode;
00438          BoolVarArgs y(x.size());
00439          for (int i = x.size(); i--; ) y[i] = channel(home, x[i]);
00440          extensional(home, y, t, ICL_DEF, pk);
00441        }
00442      };
00443 
00444 
00445      RegSimpleA ra;
00446      RegSimpleB rb;
00447      RegSimpleC rc;
00448    
00449      RegDistinct rd;
00450    
00451      RegSharedA rsa;
00452      RegSharedB rsb;
00453      RegSharedC rsc;
00454      RegSharedD rsd;
00455 
00456      RegEmptyDFA redfa;
00457      RegEmptyREG rereg;
00458 
00459 
00460      TupleSetA tsam(Gecode::PK_MEMORY);
00461      TupleSetA tsas(Gecode::PK_SPEED);
00462 
00463      TupleSetB tsbm(Gecode::PK_MEMORY);
00464      TupleSetB tsbs(Gecode::PK_SPEED);
00465 
00466      TupleSetBool tsboolm(Gecode::PK_MEMORY, 0.3);
00467      TupleSetBool tsbools(Gecode::PK_SPEED, 0.3);
00469 
00470    }
00471 }}
00472 
00473 
00474 // STATISTICS: test-int
00475