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

gcc.cpp

Go to the documentation of this file.
00001 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
00002 /*
00003  *  Main authors:
00004  *     Patrick Pekczynski <pekczynski@ps.uni-sb.de>
00005  *
00006  *  Contributing authors:
00007  *     Christian Schulte <schulte@gecode.org>
00008  *
00009  *  Copyright:
00010  *     Patrick Pekczynski, 2005
00011  *     Christian Schulte, 2007
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 namespace Test { namespace Int {
00041 
00043    namespace GCC {
00044 
00050 
00051      class IntAllMinMax : public Test {
00052      public:
00054        IntAllMinMax(Gecode::IntPropLevel ipl)
00055          : Test("GCC::Int::All::MinMax::"+str(ipl),4,-1,3,false,ipl) {}
00057        virtual bool solution(const Assignment& x) const {
00058          int n[5];
00059          for (int i=5; i--; )
00060            n[i]=0;
00061          for (int i=x.size(); i--; )
00062            n[x[i]+1]++;
00063          if (n[2] > 0)
00064           return false;
00065          for (int i=5; i--;)
00066            if (n[i]>2)
00067              return false;
00068          return true;
00069        }
00071        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00072          using namespace Gecode;
00073          IntArgs values(5);
00074          IntSet fixed(0,2);
00075          IntSetArgs cards(5);
00076          for (int i=0; i<5; i++) {
00077            values[i] = i-1; cards[i] = fixed;
00078          }
00079          cards[2] = IntSet(0,0);
00080          count(home, x, cards, values, ipl);
00081        }
00082      };
00083 
00085      class IntAllMinMaxDef : public Test {
00086      public:
00088        IntAllMinMaxDef(Gecode::IntPropLevel ipl)
00089          : Test("GCC::Int::All::MinMaxDef::"+str(ipl),4,0,3,false,ipl) {}
00091        virtual bool solution(const Assignment& x) const {
00092          int n[4];
00093          for (int i=4; i--; )
00094            n[i]=0;
00095          for (int i=x.size(); i--; )
00096            n[x[i]]++;
00097          if (n[2] > 0)
00098            return false;
00099          for (int i=4; i--;)
00100            if (n[i]>2)
00101              return false;
00102          return true;
00103        }
00105        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00106          using namespace Gecode;
00107          IntSet fixed(0,2);
00108          IntSetArgs cards(4);
00109          for (int i=0; i<4; i++) {
00110            cards[i] = fixed;
00111          }
00112          cards[2] = IntSet(0,0);
00113          count(home, x, cards, ipl);
00114        }
00115      };
00116 
00118      class IntAllMax : public Test {
00119      public:
00121        IntAllMax(Gecode::IntPropLevel ipl)
00122          : Test("GCC::Int::All::Max::"+str(ipl), 4, 1,2, false, ipl) {}
00124        virtual bool solution(const Assignment& x) const {
00125          int n[2];
00126          for (int i=2; i--; )
00127            n[i] = 0;
00128          for (int i=x.size(); i--; )
00129            n[x[i] - 1]++;
00130          if (n[0] != 2 || n[1] != 2)
00131            return false;
00132          return true;
00133        }
00135        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00136          Gecode::IntArgs values({1,2});
00137          Gecode::count(home, x, Gecode::IntSet(2,2), values, ipl);
00138        }
00139      };
00140 
00141 
00143      template<bool hole>
00144      class IntSome : public Test {
00145      public:
00147        IntSome(Gecode::IntPropLevel ipl)
00148          : Test(std::string("GCC::Int::Some::")+
00149                 (hole ? "::Hole" : "::Full")+str(ipl),4,1,4,false,ipl) {}
00151        virtual bool solution(const Assignment& x) const {
00152          int n[4];
00153          for (int i=4; i--; )
00154            n[i]=0;
00155          for (int i=x.size(); i--; )
00156            n[x[i]-1]++;
00157          if ((n[0] < 2) || (n[1] < 2) || (n[2] > 0) || (n[3] > 0))
00158            return false;
00159          return true;
00160        }
00162        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00163          using namespace Gecode;
00164          IntArgs values({1,2});
00165          Gecode::IntSet fixed;
00166          if (!hole) {
00167            fixed = IntSet(0,2);
00168          } else {
00169            int ish[] = {0,2};
00170            fixed = IntSet(ish,2);
00171          }
00172          Gecode::IntSetArgs cards(2);
00173          cards[0]=fixed; cards[1]=fixed;
00174          count(home, x, cards, values, ipl);
00175        }
00176      };
00177 
00179      class VarAll : public Test {
00180      protected:
00182        static const int n = 4;
00183      public:
00185        VarAll(Gecode::IntPropLevel ipl)
00186          : Test("GCC::Var::All::"+str(ipl),7,0,2,false,ipl) {}
00188        virtual bool solution(const Assignment& x) const {
00189          // Number of cardinality variables
00190          int m = x.size()-n;
00191          for (int i=0; i<n; i++)
00192            if ((x[i] < 0) || (x[i] > 2))
00193              return false;
00194          int* card = new int[m];
00195          for (int i=0; i<m; i++) {
00196            card[i] = 0;
00197            if ((x[n+i] < 0) || (x[n+i] > 2)) {
00198              delete [] card;
00199              return false;
00200            }
00201          }
00202          for (int i=0; i<n; i++)
00203            card[x[i]]++;
00204          for (int i=0; i<m; i++)
00205            if (card[i] != x[n+i]) {
00206              delete [] card;
00207              return false;
00208            }
00209          delete [] card;
00210          return true;
00211        }
00213        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00214          using namespace Gecode;
00215          // Number of cardinality variables
00216          int m = xy.size()-n;
00217 
00218          IntVarArgs x(n), y(m);
00219          for (int i=0; i<n; i++)
00220            x[i]=xy[i];
00221          for (int i=0; i<m; i++)
00222            y[i]=xy[n+i];
00223          count(home, x, y, ipl);
00224        }
00225      };
00226 
00228      class VarSome : public Test {
00229      protected:
00231        int n;
00233        static const int randomArity = 7;
00234      public:
00236        VarSome(std::string s, int n0, int min, int max,
00237                Gecode::IntPropLevel ipl)
00238          : Test("GCC::Var::Some::"+s+"::"+str(ipl),
00239                 n0+(max-min)+1,min,max,false,ipl)
00240          , n(n0)
00241         {
00242           contest = CTL_NONE;
00243           if (arity>randomArity)
00244             testsearch = false;
00245         }
00247        virtual bool solution(const Assignment& x) const {
00248          // Number of cardinality variables
00249          int m = x.size()-n;
00250          int* card = new int[m];
00251          for (int i=0; i<m; i++) {
00252            card[i] = 0;
00253            if ((x[n+i] < 0) || (x[n+i] > n)) {
00254              delete [] card;
00255              return false;
00256            }
00257          }
00258          for (int i=0; i<n; i++)
00259            card[x[i]-dom.min()]++;
00260          for (int i=0; i<m; i++)
00261            if (card[i] != x[n+i]) {
00262              delete [] card;
00263              return false;
00264            }
00265          delete [] card;
00266          return true;
00267        }
00269        virtual Assignment* assignment(void) const {
00270          if (arity > randomArity)
00271            return new RandomAssignment(arity,dom,4000);
00272          else
00273            return new CpltAssignment(arity,dom);
00274        }
00276        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00277          using namespace Gecode;
00278          // Number of cardinality variables
00279          int m = xy.size()-n;
00280          IntVarArgs x(n), y(m);
00281          for (int i=0; i<n; i++)
00282            x[i]=xy[i];
00283          for (int i=0; i<m; i++)
00284            y[i]=xy[n+i];
00285          IntArgs values(m);
00286          for (int i=m; i--;)
00287            values[i] = i+dom.min();
00288          count(home,x,y,values,ipl);
00289        }
00290      };
00291 
00293      class Create {
00294      public:
00296        Create(void) {
00297          for (IntPropLevels ipls; ipls(); ++ipls) {
00298            (void) new IntAllMinMax(ipls.ipl());
00299            (void) new IntAllMinMaxDef(ipls.ipl());
00300            (void) new IntAllMax(ipls.ipl());
00301            (void) new IntSome<false>(ipls.ipl());
00302            (void) new IntSome<true>(ipls.ipl());
00303            (void) new VarAll(ipls.ipl());
00304            (void) new VarSome("Small",2,-1,3,ipls.ipl());
00305            (void) new VarSome("Large",3,-1,4,ipls.ipl());
00306          }
00307        }
00308      };
00309 
00310      Create c;
00312 
00313    }
00314 }}
00315 
00316 // STATISTICS: test-int