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

nvalues.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, 2011
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 <gecode/minimodel.hh>
00035 #include "test/int.hh"
00036 
00037 namespace Test { namespace Int {
00038 
00040    namespace NValues {
00041 
00047 
00048      class IntInt : public Test {
00049      protected:
00051        Gecode::IntRelType irt;
00053        int m;
00054      public:
00056        IntInt(int n, int m0, Gecode::IntRelType irt0)
00057          : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
00058                 n,0,n),
00059            irt(irt0), m(m0) {
00060          testfix = false;
00061          if (arity > 5)
00062            testsearch = false;
00063        }
00065        virtual Assignment* assignment(void) const {
00066          if (arity > 5)
00067            return new RandomAssignment(arity,dom,500);
00068          else
00069            return new CpltAssignment(arity,dom);
00070        }
00072        virtual bool solution(const Assignment& x) const {
00073          int n = x.size();
00074          bool* v = new bool[n+1];
00075          for (int i=n+1; i--; )
00076            v[i] = false;
00077          int k = 0;
00078          for (int i=n; i--; )
00079            if (!v[x[i]]) {
00080              k++;
00081              v[x[i]] = true;
00082            }
00083          delete [] v;
00084          return cmp(k,irt,m);
00085        }
00087        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00088          Gecode::nvalues(home, x, irt, m);
00089        }
00090      };
00091 
00093      class IntVar : public Test {
00094      protected:
00096        Gecode::IntRelType irt;
00097      public:
00099        IntVar(int n, Gecode::IntRelType irt0)
00100          : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n),
00101            irt(irt0) {
00102          testfix = false;
00103        }
00105        virtual bool solution(const Assignment& x) const {
00106          int n = x.size() - 1;
00107          bool* v = new bool[n+1];
00108          for (int i=n+1; i--; )
00109            v[i] = false;
00110          int k = 0;
00111          for (int i=n; i--; )
00112            if (!v[x[i]]) {
00113              k++;
00114              v[x[i]] = true;
00115            }
00116          delete [] v;
00117          return cmp(k,irt,x[n]);
00118        }
00120        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00121          int n = xy.size() - 1;
00122          Gecode::IntVarArgs x(n);
00123          for (int i=n; i--; )
00124            x[i] = xy[i];
00125          Gecode::nvalues(home, x, irt, xy[n]);
00126        }
00127      };
00128 
00130      class BoolInt : public Test {
00131      protected:
00133        Gecode::IntRelType irt;
00135        int m;
00136      public:
00138        BoolInt(int n, int m0, Gecode::IntRelType irt0)
00139          : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
00140                 n,0,2),
00141            irt(irt0), m(m0) {}
00143        virtual bool solution(const Assignment& x) const {
00144          int n = x.size();
00145          for (int i=n; i--; )
00146            if (x[i] > 1)
00147              return false;
00148          bool* v = new bool[n+1];
00149          for (int i=n+1; i--; )
00150            v[i] = false;
00151          int k = 0;
00152          for (int i=n; i--; )
00153            if (!v[x[i]]) {
00154              k++;
00155              v[x[i]] = true;
00156            }
00157          delete [] v;
00158          return cmp(k,irt,m);
00159        }
00161        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00162          using namespace Gecode;
00163          BoolVarArgs y(x.size());
00164          for (int i=x.size(); i--; )
00165            y[i] = channel(home, x[i]);
00166          nvalues(home, y, irt, m);
00167        }
00168      };
00169 
00171      class BoolVar : public Test {
00172      protected:
00174        Gecode::IntRelType irt;
00175      public:
00177        BoolVar(int n, Gecode::IntRelType irt0)
00178          : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2),
00179            irt(irt0) {}
00181        virtual bool solution(const Assignment& x) const {
00182          int n = x.size() - 1;
00183          for (int i=n; i--; )
00184            if (x[i] > 1)
00185              return false;
00186          bool* v = new bool[n+1];
00187          for (int i=n+1; i--; )
00188            v[i] = false;
00189          int k = 0;
00190          for (int i=n; i--; )
00191            if (!v[x[i]]) {
00192              k++;
00193              v[x[i]] = true;
00194            }
00195          delete [] v;
00196          return cmp(k,irt,x[n]);
00197        }
00199        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00200          using namespace Gecode;
00201          int n = xy.size() - 1;
00202          BoolVarArgs x(n);
00203          for (int i=n; i--; )
00204            x[i] = channel(home, xy[i]);
00205          nvalues(home, x, irt, xy[n]);
00206        }
00207      };
00208 
00210      class Create {
00211      public:
00213        Create(void) {
00214          for (IntRelTypes irts; irts(); ++irts) {
00215            for (int i=1; i<=7; i += 3) {
00216              for (int m=0; m<=3; m++)
00217                (void) new BoolInt(i, m, irts.irt());
00218              (void) new BoolVar(i, irts.irt());
00219            }
00220            for (int i=1; i<=7; i += 2) {
00221              for (int m=0; m<=i+1; m++)
00222                (void) new IntInt(i, m, irts.irt());
00223              if (i <= 5)
00224                (void) new IntVar(i, irts.irt());
00225            }
00226          }
00227        }
00228      };
00229 
00230      Create c;
00232 
00233    }
00234 }}
00235 
00236 // STATISTICS: test-int
00237