Generated on Thu Mar 22 10:39:46 2012 for Gecode by doxygen 1.6.3

mm-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, 2008
00008  *
00009  *  Last modified:
00010  *     $Date: 2010-04-08 12:35:31 +0200 (Thu, 08 Apr 2010) $ by $Author: schulte $
00011  *     $Revision: 10684 $
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 MiniModelCount {
00046 
00048      std::string expand(Gecode::IntRelType irt) {
00049        switch (irt) {
00050        case Gecode::IRT_EQ: return "Exactly";
00051        case Gecode::IRT_LQ: return "AtMost";
00052        case Gecode::IRT_GQ: return "AtLeast";
00053        default: GECODE_NEVER;
00054        }
00055        GECODE_NEVER;
00056        return "";
00057      }
00058 
00064 
00065      class IntInt : public Test {
00066      protected:
00068        Gecode::IntRelType irt;
00069      public:
00071        IntInt(Gecode::IntRelType irt0)
00072          : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {}
00074        virtual bool solution(const Assignment& x) const {
00075          int m = 0;
00076          for (int i=x.size(); i--; )
00077            if (x[i] == 0)
00078              m++;
00079          return cmp(m,irt,2);
00080        }
00082        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00083          switch (irt) {
00084          case Gecode::IRT_EQ:
00085            Gecode::exactly(home,x,0,2); break;
00086          case Gecode::IRT_LQ:
00087            Gecode::atmost(home,x,0,2); break;
00088          case Gecode::IRT_GQ:
00089            Gecode::atleast(home,x,0,2); break;
00090          default: GECODE_NEVER;
00091          }
00092        }
00093      };
00094 
00096      class IntVar : public Test {
00097      protected:
00099        Gecode::IntRelType irt;
00100      public:
00102        IntVar(Gecode::IntRelType irt0)
00103          : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {}
00105        virtual bool solution(const Assignment& x) const {
00106          int m = 0;
00107          for (int i=0; i<4; i++)
00108            if (x[i] == 0)
00109              m++;
00110          return cmp(m,irt,x[4]);
00111        }
00113        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00114          Gecode::IntVarArgs y(4);
00115          for (int i=0; i<4; i++)
00116            y[i]=x[i];
00117          switch (irt) {
00118          case Gecode::IRT_EQ:
00119            Gecode::exactly(home,y,0,x[4]); break;
00120          case Gecode::IRT_LQ:
00121            Gecode::atmost(home,y,0,x[4]); break;
00122          case Gecode::IRT_GQ:
00123            Gecode::atleast(home,y,0,x[4]); break;
00124          default: GECODE_NEVER;
00125          }
00126        }
00127      };
00128 
00130      class VarVar : public Test {
00131      protected:
00133        Gecode::IntRelType irt;
00134      public:
00136        VarVar(Gecode::IntRelType irt0)
00137          : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
00139        virtual bool solution(const Assignment& x) const {
00140          int m = 0;
00141          for (int i=0; i<3; i++)
00142            if (x[i] == x[3])
00143              m++;
00144          return cmp(m,irt,x[4]);
00145        }
00147        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00148          Gecode::IntVarArgs y(3);
00149          for (int i=0; i<3; i++)
00150            y[i]=x[i];
00151          switch (irt) {
00152          case Gecode::IRT_EQ:
00153            Gecode::exactly(home,y,x[3],x[4]); break;
00154          case Gecode::IRT_LQ:
00155            Gecode::atmost(home,y,x[3],x[4]); break;
00156          case Gecode::IRT_GQ:
00157            Gecode::atleast(home,y,x[3],x[4]); break;
00158          default: GECODE_NEVER;
00159          }
00160        }
00161      };
00162 
00164      class VarInt : public Test {
00165      protected:
00167        Gecode::IntRelType irt;
00168      public:
00170        VarInt(Gecode::IntRelType irt0)
00171          : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
00173        virtual bool solution(const Assignment& x) const {
00174          int m = 0;
00175          for (int i=0; i<3; i++)
00176            if (x[i] == x[3])
00177              m++;
00178          return cmp(m,irt,2);
00179        }
00181        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00182          Gecode::IntVarArgs y(3);
00183          for (int i=0; i<3; i++)
00184            y[i]=x[i];
00185          switch (irt) {
00186          case Gecode::IRT_EQ:
00187            Gecode::exactly(home,y,x[3],2); break;
00188          case Gecode::IRT_LQ:
00189            Gecode::atmost(home,y,x[3],2); break;
00190          case Gecode::IRT_GQ:
00191            Gecode::atleast(home,y,x[3],2); break;
00192          default: GECODE_NEVER;
00193          }
00194        }
00195      };
00196 
00197      Gecode::IntArgs ints(4, 1,0,3,2);
00198 
00200      class IntArrayInt : public Test {
00201      protected:
00203        Gecode::IntRelType irt;
00204      public:
00206        IntArrayInt(Gecode::IntRelType irt0)
00207          : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2),
00208            irt(irt0) {}
00210        virtual bool solution(const Assignment& x) const {
00211          int m = 0;
00212          for (int i=0; i<4; i++)
00213            if (x[i] == ints[i])
00214              m++;
00215          return cmp(m,irt,2);
00216        }
00218        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00219          Gecode::IntVarArgs y(4);
00220          for (int i=0; i<4; i++)
00221            y[i]=x[i];
00222          switch (irt) {
00223          case Gecode::IRT_EQ:
00224            Gecode::exactly(home,y,ints,2); break;
00225          case Gecode::IRT_LQ:
00226            Gecode::atmost(home,y,ints,2); break;
00227          case Gecode::IRT_GQ:
00228            Gecode::atleast(home,y,ints,2); break;
00229          default: GECODE_NEVER;
00230          }
00231        }
00232      };
00233 
00235      class IntArrayVar : public Test {
00236      protected:
00238        Gecode::IntRelType irt;
00239      public:
00241        IntArrayVar(Gecode::IntRelType irt0)
00242          : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2),
00243            irt(irt0) {}
00245        virtual bool solution(const Assignment& x) const {
00246          int m = 0;
00247          for (int i=0; i<4; i++)
00248            if (x[i] == ints[i])
00249              m++;
00250          return cmp(m,irt,x[4]);
00251        }
00253        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00254          Gecode::IntVarArgs y(4);
00255          for (int i=0; i<4; i++)
00256            y[i]=x[i];
00257          switch (irt) {
00258          case Gecode::IRT_EQ:
00259            Gecode::exactly(home,y,ints,x[4]); break;
00260          case Gecode::IRT_LQ:
00261            Gecode::atmost(home,y,ints,x[4]); break;
00262          case Gecode::IRT_GQ:
00263            Gecode::atleast(home,y,ints,x[4]); break;
00264          default: GECODE_NEVER;
00265          }
00266        }
00267      };
00268 
00270      class Create {
00271      public:
00273        Create(void) {
00274          for (IntRelTypes irts; irts(); ++irts)
00275            if ((irts.irt() == Gecode::IRT_EQ) ||
00276                (irts.irt() == Gecode::IRT_LQ) ||
00277                (irts.irt() == Gecode::IRT_GQ)) {
00278              (void) new IntInt(irts.irt());
00279              (void) new IntVar(irts.irt());
00280              (void) new VarVar(irts.irt());
00281              (void) new VarInt(irts.irt());
00282              (void) new IntArrayInt(irts.irt());
00283              (void) new IntArrayVar(irts.irt());
00284            }
00285        }
00286      };
00287 
00288      Create c;
00290 
00291    }
00292 
00293 }}
00294 
00295 // STATISTICS: test-minimodel