Generated on Thu Apr 11 13:59:22 2019 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  *  This file is part of Gecode, the generic constraint
00010  *  development environment:
00011  *     http://www.gecode.org
00012  *
00013  *  Permission is hereby granted, free of charge, to any person obtaining
00014  *  a copy of this software and associated documentation files (the
00015  *  "Software"), to deal in the Software without restriction, including
00016  *  without limitation the rights to use, copy, modify, merge, publish,
00017  *  distribute, sublicense, and/or sell copies of the Software, and to
00018  *  permit persons to whom the Software is furnished to do so, subject to
00019  *  the following conditions:
00020  *
00021  *  The above copyright notice and this permission notice shall be
00022  *  included in all copies or substantial portions of the Software.
00023  *
00024  *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
00025  *  EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00026  *  MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00027  *  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
00028  *  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
00029  *  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
00030  *  WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00031  *
00032  */
00033 
00034 #include "test/int.hh"
00035 
00036 #include <gecode/minimodel.hh>
00037 
00038 namespace Test { namespace Int {
00039 
00041    namespace MiniModelCount {
00042 
00044      std::string expand(Gecode::IntRelType irt) {
00045        switch (irt) {
00046        case Gecode::IRT_EQ: return "Exactly";
00047        case Gecode::IRT_LQ: return "AtMost";
00048        case Gecode::IRT_GQ: return "AtLeast";
00049        default: GECODE_NEVER;
00050        }
00051        GECODE_NEVER;
00052        return "";
00053      }
00054 
00060 
00061      class IntInt : public Test {
00062      protected:
00064        Gecode::IntRelType irt;
00065      public:
00067        IntInt(Gecode::IntRelType irt0)
00068          : Test("MiniModel::"+expand(irt0)+"::Int::Int",4,-2,2), irt(irt0) {}
00070        virtual bool solution(const Assignment& x) const {
00071          int m = 0;
00072          for (int i=x.size(); i--; )
00073            if (x[i] == 0)
00074              m++;
00075          return cmp(m,irt,2);
00076        }
00078        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00079          switch (irt) {
00080          case Gecode::IRT_EQ:
00081            Gecode::exactly(home,x,0,2); break;
00082          case Gecode::IRT_LQ:
00083            Gecode::atmost(home,x,0,2); break;
00084          case Gecode::IRT_GQ:
00085            Gecode::atleast(home,x,0,2); break;
00086          default: GECODE_NEVER;
00087          }
00088        }
00089      };
00090 
00092      class IntVar : public Test {
00093      protected:
00095        Gecode::IntRelType irt;
00096      public:
00098        IntVar(Gecode::IntRelType irt0)
00099          : Test("MiniModel::"+expand(irt0)+"::Int::Var",5,-2,2), irt(irt0) {}
00101        virtual bool solution(const Assignment& x) const {
00102          int m = 0;
00103          for (int i=0; i<4; i++)
00104            if (x[i] == 0)
00105              m++;
00106          return cmp(m,irt,x[4]);
00107        }
00109        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00110          Gecode::IntVarArgs y(4);
00111          for (int i=0; i<4; i++)
00112            y[i]=x[i];
00113          switch (irt) {
00114          case Gecode::IRT_EQ:
00115            Gecode::exactly(home,y,0,x[4]); break;
00116          case Gecode::IRT_LQ:
00117            Gecode::atmost(home,y,0,x[4]); break;
00118          case Gecode::IRT_GQ:
00119            Gecode::atleast(home,y,0,x[4]); break;
00120          default: GECODE_NEVER;
00121          }
00122        }
00123      };
00124 
00126      class VarVar : public Test {
00127      protected:
00129        Gecode::IntRelType irt;
00130      public:
00132        VarVar(Gecode::IntRelType irt0)
00133          : Test("MiniModel::"+expand(irt0)+"::Var::Var",5,-2,2), irt(irt0) {}
00135        virtual bool solution(const Assignment& x) const {
00136          int m = 0;
00137          for (int i=0; i<3; i++)
00138            if (x[i] == x[3])
00139              m++;
00140          return cmp(m,irt,x[4]);
00141        }
00143        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00144          Gecode::IntVarArgs y(3);
00145          for (int i=0; i<3; i++)
00146            y[i]=x[i];
00147          switch (irt) {
00148          case Gecode::IRT_EQ:
00149            Gecode::exactly(home,y,x[3],x[4]); break;
00150          case Gecode::IRT_LQ:
00151            Gecode::atmost(home,y,x[3],x[4]); break;
00152          case Gecode::IRT_GQ:
00153            Gecode::atleast(home,y,x[3],x[4]); break;
00154          default: GECODE_NEVER;
00155          }
00156        }
00157      };
00158 
00160      class VarInt : public Test {
00161      protected:
00163        Gecode::IntRelType irt;
00164      public:
00166        VarInt(Gecode::IntRelType irt0)
00167          : Test("MiniModel::"+expand(irt0)+"::Var::Int",4,-2,2), irt(irt0) {}
00169        virtual bool solution(const Assignment& x) const {
00170          int m = 0;
00171          for (int i=0; i<3; i++)
00172            if (x[i] == x[3])
00173              m++;
00174          return cmp(m,irt,2);
00175        }
00177        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00178          Gecode::IntVarArgs y(3);
00179          for (int i=0; i<3; i++)
00180            y[i]=x[i];
00181          switch (irt) {
00182          case Gecode::IRT_EQ:
00183            Gecode::exactly(home,y,x[3],2); break;
00184          case Gecode::IRT_LQ:
00185            Gecode::atmost(home,y,x[3],2); break;
00186          case Gecode::IRT_GQ:
00187            Gecode::atleast(home,y,x[3],2); break;
00188          default: GECODE_NEVER;
00189          }
00190        }
00191      };
00192 
00193      Gecode::IntArgs ints({1,0,3,2});
00194 
00196      class IntArrayInt : public Test {
00197      protected:
00199        Gecode::IntRelType irt;
00200      public:
00202        IntArrayInt(Gecode::IntRelType irt0)
00203          : Test("MiniModel::"+expand(irt0)+"::IntArray::Int",5,-2,2),
00204            irt(irt0) {}
00206        virtual bool solution(const Assignment& x) const {
00207          int m = 0;
00208          for (int i=0; i<4; i++)
00209            if (x[i] == ints[i])
00210              m++;
00211          return cmp(m,irt,2);
00212        }
00214        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00215          Gecode::IntVarArgs y(4);
00216          for (int i=0; i<4; i++)
00217            y[i]=x[i];
00218          switch (irt) {
00219          case Gecode::IRT_EQ:
00220            Gecode::exactly(home,y,ints,2); break;
00221          case Gecode::IRT_LQ:
00222            Gecode::atmost(home,y,ints,2); break;
00223          case Gecode::IRT_GQ:
00224            Gecode::atleast(home,y,ints,2); break;
00225          default: GECODE_NEVER;
00226          }
00227        }
00228      };
00229 
00231      class IntArrayVar : public Test {
00232      protected:
00234        Gecode::IntRelType irt;
00235      public:
00237        IntArrayVar(Gecode::IntRelType irt0)
00238          : Test("MiniModel::"+expand(irt0)+"::IntArray::Var",5,-2,2),
00239            irt(irt0) {}
00241        virtual bool solution(const Assignment& x) const {
00242          int m = 0;
00243          for (int i=0; i<4; i++)
00244            if (x[i] == ints[i])
00245              m++;
00246          return cmp(m,irt,x[4]);
00247        }
00249        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00250          Gecode::IntVarArgs y(4);
00251          for (int i=0; i<4; i++)
00252            y[i]=x[i];
00253          switch (irt) {
00254          case Gecode::IRT_EQ:
00255            Gecode::exactly(home,y,ints,x[4]); break;
00256          case Gecode::IRT_LQ:
00257            Gecode::atmost(home,y,ints,x[4]); break;
00258          case Gecode::IRT_GQ:
00259            Gecode::atleast(home,y,ints,x[4]); break;
00260          default: GECODE_NEVER;
00261          }
00262        }
00263      };
00264 
00266      class Create {
00267      public:
00269        Create(void) {
00270          for (IntRelTypes irts; irts(); ++irts)
00271            if ((irts.irt() == Gecode::IRT_EQ) ||
00272                (irts.irt() == Gecode::IRT_LQ) ||
00273                (irts.irt() == Gecode::IRT_GQ)) {
00274              (void) new IntInt(irts.irt());
00275              (void) new IntVar(irts.irt());
00276              (void) new VarVar(irts.irt());
00277              (void) new VarInt(irts.irt());
00278              (void) new IntArrayInt(irts.irt());
00279              (void) new IntArrayVar(irts.irt());
00280            }
00281        }
00282      };
00283 
00284      Create c;
00286 
00287    }
00288 
00289 }}
00290 
00291 // STATISTICS: test-minimodel