Generated on Fri Mar 20 15:56:06 2015 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: 2011-08-22 13:08:29 +0200 (Mon, 22 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12326 $
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)
00261          : Test("Count::Var::Var::"+str(irt0),5,-2,2), irt(irt0) {}
00263        virtual bool solution(const Assignment& x) const {
00264          int m = 0;
00265          for (int i=0; i<3; i++)
00266            if (x[i] == x[3])
00267              m++;
00268          return cmp(m,irt,x[4]);
00269        }
00271        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00272          Gecode::IntVarArgs y(3);
00273          for (int i=0; i<3; i++)
00274            y[i]=x[i];
00275          Gecode::count(home, y, x[3], irt, x[4]);
00276        }
00277      };
00278 
00280      class VarInt : public Test {
00281      protected:
00283        Gecode::IntRelType irt;
00284      public:
00286        VarInt(Gecode::IntRelType irt0)
00287          : Test("Count::Var::Int::"+str(irt0),4,-2,2), irt(irt0) {}
00289        virtual bool solution(const Assignment& x) const {
00290          int m = 0;
00291          for (int i=0; i<3; i++)
00292            if (x[i] == x[3])
00293              m++;
00294          return cmp(m,irt,2);
00295        }
00297        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00298          Gecode::IntVarArgs y(3);
00299          for (int i=0; i<3; i++)
00300            y[i]=x[i];
00301          Gecode::count(home, y, x[3], irt, 2);
00302        }
00303      };
00304 
00306      class VarVarSharedA : public Test {
00307      protected:
00309        Gecode::IntRelType irt;
00310      public:
00312        VarVarSharedA(Gecode::IntRelType irt0)
00313          : Test("Count::Var::Var::Shared::A::"+str(irt0),5,-2,2), irt(irt0) {}
00315        virtual bool solution(const Assignment& x) const {
00316          int m = 0;
00317          for (int i=0; i<4; i++)
00318            if (x[i] == x[1])
00319              m++;
00320          return cmp(m,irt,x[4]);
00321        }
00323        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00324          Gecode::IntVarArgs y(4);
00325          for (int i=0; i<4; i++)
00326            y[i]=x[i];
00327          Gecode::count(home, y, x[1], irt, x[4]);
00328        }
00329      };
00330 
00332      class VarVarSharedB : public Test {
00333      protected:
00335        Gecode::IntRelType irt;
00336      public:
00338        VarVarSharedB(Gecode::IntRelType irt0)
00339          : Test("Count::Var::Var::Shared::B::"+str(irt0),5,-2,2), irt(irt0) {}
00341        virtual bool solution(const Assignment& x) const {
00342          int m = 0;
00343          for (int i=0; i<4; i++)
00344            if (x[i] == x[4])
00345              m++;
00346          return cmp(m,irt,x[3]);
00347        }
00349        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00350          Gecode::IntVarArgs y(4);
00351          for (int i=0; i<4; i++)
00352            y[i]=x[i];
00353          Gecode::count(home, y, x[4], irt, x[3]);
00354        }
00355      };
00356 
00358      class VarVarSharedC : public Test {
00359      protected:
00361        Gecode::IntRelType irt;
00362      public:
00364        VarVarSharedC(Gecode::IntRelType irt0)
00365          : Test("Count::Var::Var::Shared::C::"+str(irt0),4,-2,2), irt(irt0) {}
00367        virtual bool solution(const Assignment& x) const {
00368          int m = 0;
00369          for (int i=0; i<4; i++)
00370            if (x[i] == x[1])
00371              m++;
00372          return cmp(m,irt,x[3]);
00373        }
00375        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00376          Gecode::count(home, x, x[1], irt, x[3]);
00377        }
00378      };
00379 
00381      class Create {
00382      public:
00384        Create(void) {
00385          for (IntRelTypes irts; irts(); ++irts) {
00386            (void) new IntInt(irts.irt());
00387            (void) new SetInt(irts.irt());
00388            (void) new IntIntDup(irts.irt());
00389            (void) new IntVar(irts.irt());
00390            (void) new SetVar(irts.irt());
00391            (void) new IntArrayInt(irts.irt());
00392            (void) new IntArrayVar(irts.irt());
00393            (void) new IntVarShared(irts.irt());
00394            (void) new VarVar(irts.irt());
00395            (void) new VarInt(irts.irt());
00396            (void) new VarVarSharedA(irts.irt());
00397            (void) new VarVarSharedB(irts.irt());
00398            (void) new VarVarSharedC(irts.irt());
00399          }
00400        }
00401      };
00402 
00403      Create c;
00405 
00406    }
00407 }}
00408 
00409 // STATISTICS: test-int
00410