Generated on Tue Apr 18 10:21:49 2017 for Gecode by doxygen 1.6.3

count.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: 2015-09-11 16:29:45 +0200 (Fri, 11 Sep 2015) $ by $Author: schulte $
00011  *     $Revision: 14672 $
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 Count {
00044 
00050 
00051      class IntInt : public Test {
00052      protected:
00054        Gecode::IntRelType irt;
00055      public:
00057        IntInt(Gecode::IntRelType irt0)
00058          : Test("Count::Int::Int::"+str(irt0),4,-2,2), irt(irt0) {}
00060        virtual bool solution(const Assignment& x) const {
00061          int m = 0;
00062          for (int i=x.size(); i--; )
00063            if (x[i] == 0)
00064              m++;
00065          return cmp(m,irt,2);
00066        }
00068        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00069          Gecode::count(home, x, 0, irt, 2);
00070        }
00071      };
00072 
00074      class SetInt : public Test {
00075      protected:
00077        Gecode::IntRelType irt;
00078      public:
00080        SetInt(Gecode::IntRelType irt0)
00081          : Test("Count::Set::Int::"+str(irt0),4,-2,2), irt(irt0) {}
00083        virtual bool solution(const Assignment& x) const {
00084          int m = 0;
00085          for (int i=x.size(); i--; )
00086            if ((x[i] >= -1) && (x[i] <= 1))
00087              m++;
00088          return cmp(m,irt,2);
00089        }
00091        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00092          Gecode::IntSet s(-1,1);
00093          Gecode::count(home, x, s, irt, 2);
00094        }
00095      };
00096 
00098      class IntIntDup : public Test {
00099      protected:
00101        Gecode::IntRelType irt;
00102      public:
00104        IntIntDup(Gecode::IntRelType irt0)
00105          : Test("Count::Int::Int::Dup::"+str(irt0),4,-2,2), irt(irt0) {}
00107        virtual bool solution(const Assignment& x) const {
00108          int m = 0;
00109          for (int i=x.size(); i--; )
00110            if (x[i] == 0)
00111              m += 2;
00112          return cmp(m,irt,4);
00113        }
00115        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00116          Gecode::IntVarArgs y(8);
00117          for (int i=x.size(); i--; )
00118            y[i]=y[4+i]=x[i];
00119          Gecode::count(home, y, 0, irt, 4);
00120        }
00121      };
00122 
00124      class IntVar : public Test {
00125      protected:
00127        Gecode::IntRelType irt;
00128      public:
00130        IntVar(Gecode::IntRelType irt0)
00131          : Test("Count::Int::Var::"+str(irt0),5,-2,2), irt(irt0) {}
00133        virtual bool solution(const Assignment& x) const {
00134          int m = 0;
00135          for (int i=0; i<4; i++)
00136            if (x[i] == 0)
00137              m++;
00138          return cmp(m,irt,x[4]);
00139        }
00141        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00142          Gecode::IntVarArgs y(4);
00143          for (int i=0; i<4; i++)
00144            y[i]=x[i];
00145          Gecode::count(home, y, 0, irt, x[4]);
00146        }
00147      };
00148 
00150      class SetVar : public Test {
00151      protected:
00153        Gecode::IntRelType irt;
00154      public:
00156        SetVar(Gecode::IntRelType irt0)
00157          : Test("Count::Set::Var::"+str(irt0),5,-2,2), irt(irt0) {}
00159        virtual bool solution(const Assignment& x) const {
00160          int m = 0;
00161          for (int i=0; i<4; i++)
00162            if ((x[i] >= -1) && (x[i] <= 1))
00163              m++;
00164          return cmp(m,irt,x[4]);
00165        }
00167        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00168          Gecode::IntVarArgs y(4);
00169          for (int i=0; i<4; i++)
00170            y[i]=x[i];
00171          Gecode::IntSet s(-1,1);
00172          Gecode::count(home, y, s, irt, x[4]);
00173        }
00174      };
00175 
00176      Gecode::IntArgs ints(4, 1,0,3,2);
00177 
00179      class IntArrayInt : public Test {
00180      protected:
00182        Gecode::IntRelType irt;
00183      public:
00185        IntArrayInt(Gecode::IntRelType irt0)
00186          : Test("Count::IntArray::Int::"+str(irt0),5,-2,2), irt(irt0) {}
00188        virtual bool solution(const Assignment& x) const {
00189          int m = 0;
00190          for (int i=0; i<4; i++)
00191            if (x[i] == ints[i])
00192              m++;
00193          return cmp(m,irt,2);
00194        }
00196        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00197          Gecode::IntVarArgs y(4);
00198          for (int i=0; i<4; i++)
00199            y[i]=x[i];
00200          Gecode::count(home, y, ints, irt, 2);
00201        }
00202      };
00203 
00205      class IntArrayVar : public Test {
00206      protected:
00208        Gecode::IntRelType irt;
00209      public:
00211        IntArrayVar(Gecode::IntRelType irt0)
00212          : Test("Count::IntArray::Var::"+str(irt0),5,-2,2), irt(irt0) {}
00214        virtual bool solution(const Assignment& x) const {
00215          int m = 0;
00216          for (int i=0; i<4; i++)
00217            if (x[i] == ints[i])
00218              m++;
00219          return cmp(m,irt,x[4]);
00220        }
00222        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00223          Gecode::IntVarArgs y(4);
00224          for (int i=0; i<4; i++)
00225            y[i]=x[i];
00226          Gecode::count(home, y, ints, irt, x[4]);
00227        }
00228      };
00229 
00231      class IntVarShared : public Test {
00232      protected:
00234        Gecode::IntRelType irt;
00235      public:
00237        IntVarShared(Gecode::IntRelType irt0)
00238          : Test("Count::Int::Var::Shared::"+str(irt0),4,-2,2), irt(irt0) {}
00240        virtual bool solution(const Assignment& x) const {
00241          int m = 0;
00242          for (int i=0; i<4; i++)
00243            if (x[i] == 0)
00244              m++;
00245          return cmp(m,irt,x[2]);
00246        }
00248        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00249          Gecode::count(home, x, 0, irt, x[2]);
00250        }
00251      };
00252 
00254      class VarVar : public Test {
00255      protected:
00257        Gecode::IntRelType irt;
00258      public:
00260        VarVar(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
00261          : Test("Count::Var::Var::"+str(irt0)+"::"+str(ipl),5,-2,2,false,ipl),
00262            irt(irt0) {
00263          contest = CTL_NONE;
00264        }
00266        virtual bool solution(const Assignment& x) const {
00267          int m = 0;
00268          for (int i=0; i<3; i++)
00269            if (x[i] == x[3])
00270              m++;
00271          return cmp(m,irt,x[4]);
00272        }
00274        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00275          Gecode::IntVarArgs y(3);
00276          for (int i=0; i<3; i++)
00277            y[i]=x[i];
00278          Gecode::count(home, y, x[3], irt, x[4], ipl);
00279        }
00280      };
00281 
00283      class VarInt : public Test {
00284      protected:
00286        Gecode::IntRelType irt;
00287      public:
00289        VarInt(Gecode::IntRelType irt0, Gecode::IntPropLevel ipl)
00290          : Test("Count::Var::Int::"+str(irt0)+"::"+str(ipl),4,-2,2,false,ipl),
00291            irt(irt0) {
00292          contest = CTL_NONE;
00293        }
00295        virtual bool solution(const Assignment& x) const {
00296          int m = 0;
00297          for (int i=0; i<3; i++)
00298            if (x[i] == x[3])
00299              m++;
00300          return cmp(m,irt,2);
00301        }
00303        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00304          Gecode::IntVarArgs y(3);
00305          for (int i=0; i<3; i++)
00306            y[i]=x[i];
00307          Gecode::count(home, y, x[3], irt, 2, ipl);
00308        }
00309      };
00310 
00312      class VarVarSharedA : public Test {
00313      protected:
00315        Gecode::IntRelType irt;
00316      public:
00318        VarVarSharedA(Gecode::IntRelType irt0)
00319          : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
00321        virtual bool solution(const Assignment& x) const {
00322          int m = 0;
00323          for (int i=0; i<4; i++)
00324            if (x[i] == x[1])
00325              m++;
00326          return cmp(m,irt,x[4]);
00327        }
00329        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00330          Gecode::IntVarArgs y(4);
00331          for (int i=0; i<4; i++)
00332            y[i]=x[i];
00333          Gecode::count(home, y, x[1], irt, x[4]);
00334        }
00335      };
00336 
00338      class VarVarSharedB : public Test {
00339      protected:
00341        Gecode::IntRelType irt;
00342      public:
00344        VarVarSharedB(Gecode::IntRelType irt0)
00345          : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
00347        virtual bool solution(const Assignment& x) const {
00348          int m = 0;
00349          for (int i=0; i<4; i++)
00350            if (x[i] == x[4])
00351              m++;
00352          return cmp(m,irt,x[3]);
00353        }
00355        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00356          Gecode::IntVarArgs y(4);
00357          for (int i=0; i<4; i++)
00358            y[i]=x[i];
00359          Gecode::count(home, y, x[4], irt, x[3]);
00360        }
00361      };
00362 
00364      class VarVarSharedC : public Test {
00365      protected:
00367        Gecode::IntRelType irt;
00368      public:
00370        VarVarSharedC(Gecode::IntRelType irt0)
00371          : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
00373        virtual bool solution(const Assignment& x) const {
00374          int m = 0;
00375          for (int i=0; i<4; i++)
00376            if (x[i] == x[1])
00377              m++;
00378          return cmp(m,irt,x[3]);
00379        }
00381        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00382          Gecode::count(home, x, x[1], irt, x[3]);
00383        }
00384      };
00385 
00387      class Create {
00388      public:
00390        Create(void) {
00391          for (IntRelTypes irts; irts(); ++irts) {
00392            (void) new IntInt(irts.irt());
00393            (void) new SetInt(irts.irt());
00394            (void) new IntIntDup(irts.irt());
00395            (void) new IntVar(irts.irt());
00396            (void) new SetVar(irts.irt());
00397            (void) new IntArrayInt(irts.irt());
00398            (void) new IntArrayVar(irts.irt());
00399            (void) new IntVarShared(irts.irt());
00400            (void) new VarVar(irts.irt(), Gecode::IPL_BND);
00401            (void) new VarVar(irts.irt(), Gecode::IPL_DOM);
00402            (void) new VarInt(irts.irt(), Gecode::IPL_BND);
00403            (void) new VarInt(irts.irt(), Gecode::IPL_DOM);
00404            (void) new VarVarSharedA(irts.irt());
00405            (void) new VarVarSharedB(irts.irt());
00406            (void) new VarVarSharedC(irts.irt());
00407          }
00408        }
00409      };
00410 
00411      Create c;
00413 
00414    }
00415 }}
00416 
00417 // STATISTICS: test-int
00418