Generated on Thu Mar 22 10:39:40 2012 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  *  Last modified:
00010  *     $Date: 2011-08-17 22:25:49 +0200 (Wed, 17 Aug 2011) $ by $Author: schulte $
00011  *     $Revision: 12308 $
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 <gecode/minimodel.hh>
00039 #include "test/int.hh"
00040 
00041 namespace Test { namespace Int {
00042 
00044    namespace NValues {
00045 
00051 
00052      class IntInt : public Test {
00053      protected:
00055        Gecode::IntRelType irt;
00057        int m;
00058      public:
00060        IntInt(int n, int m0, Gecode::IntRelType irt0)
00061          : Test("NValues::Int::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
00062                 n,0,n), 
00063            irt(irt0), m(m0) {
00064          testfix = false;
00065          if (arity > 5)
00066            testsearch = false;
00067        }
00069        virtual Assignment* assignment(void) const {
00070          if (arity > 5)
00071            return new RandomAssignment(arity,dom,500);
00072          else
00073            return new CpltAssignment(arity,dom);
00074        }
00076        virtual bool solution(const Assignment& x) const {
00077          int n = x.size();
00078          bool* v = new bool[n+1];
00079          for (int i=n+1; i--; )
00080            v[i] = false;
00081          int k = 0;
00082          for (int i=n; i--; )
00083            if (!v[x[i]]) {
00084              k++;
00085              v[x[i]] = true;
00086            }
00087          delete [] v;
00088          return cmp(k,irt,m);
00089        }
00091        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00092          Gecode::nvalues(home, x, irt, m);
00093        }
00094      };
00095 
00097      class IntVar : public Test {
00098      protected:
00100        Gecode::IntRelType irt;
00101      public:
00103        IntVar(int n, Gecode::IntRelType irt0)
00104          : Test("NValues::Int::Var::"+str(irt0)+"::"+str(n),n+1,0,n), 
00105            irt(irt0) {
00106          testfix = false;
00107        }
00109        virtual bool solution(const Assignment& x) const {
00110          int n = x.size() - 1;
00111          bool* v = new bool[n+1];
00112          for (int i=n+1; i--; )
00113            v[i] = false;
00114          int k = 0;
00115          for (int i=n; i--; )
00116            if (!v[x[i]]) {
00117              k++;
00118              v[x[i]] = true;
00119            }
00120          delete [] v;
00121          return cmp(k,irt,x[n]);
00122        }
00124        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00125          int n = xy.size() - 1;
00126          Gecode::IntVarArgs x(n);
00127          for (int i=n; i--; )
00128            x[i] = xy[i];
00129          Gecode::nvalues(home, x, irt, xy[n]);
00130        }
00131      };
00132 
00134      class BoolInt : public Test {
00135      protected:
00137        Gecode::IntRelType irt;
00139        int m;
00140      public:
00142        BoolInt(int n, int m0, Gecode::IntRelType irt0)
00143          : Test("NValues::Bool::Int::"+str(irt0)+"::"+str(n)+"::"+str(m0),
00144                 n,0,2), 
00145            irt(irt0), m(m0) {}
00147        virtual bool solution(const Assignment& x) const {
00148          int n = x.size();
00149          for (int i=n; i--; )
00150            if (x[i] > 1)
00151              return false;
00152          bool* v = new bool[n+1];
00153          for (int i=n+1; i--; )
00154            v[i] = false;
00155          int k = 0;
00156          for (int i=n; i--; )
00157            if (!v[x[i]]) {
00158              k++;
00159              v[x[i]] = true;
00160            }
00161          delete [] v;
00162          return cmp(k,irt,m);
00163        }
00165        virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
00166          using namespace Gecode;
00167          BoolVarArgs y(x.size());
00168          for (int i=x.size(); i--; )
00169            y[i] = channel(home, x[i]);
00170          nvalues(home, y, irt, m);
00171        }
00172      };
00173 
00175      class BoolVar : public Test {
00176      protected:
00178        Gecode::IntRelType irt;
00179      public:
00181        BoolVar(int n, Gecode::IntRelType irt0)
00182          : Test("NValues::Bool::Var::"+str(irt0)+"::"+str(n),n+1,0,2), 
00183            irt(irt0) {}
00185        virtual bool solution(const Assignment& x) const {
00186          int n = x.size() - 1;
00187          for (int i=n; i--; )
00188            if (x[i] > 1)
00189              return false;
00190          bool* v = new bool[n+1];
00191          for (int i=n+1; i--; )
00192            v[i] = false;
00193          int k = 0;
00194          for (int i=n; i--; )
00195            if (!v[x[i]]) {
00196              k++;
00197              v[x[i]] = true;
00198            }
00199          delete [] v;
00200          return cmp(k,irt,x[n]);
00201        }
00203        virtual void post(Gecode::Space& home, Gecode::IntVarArray& xy) {
00204          using namespace Gecode;
00205          int n = xy.size() - 1;
00206          BoolVarArgs x(n);
00207          for (int i=n; i--; )
00208            x[i] = channel(home, xy[i]);
00209          nvalues(home, x, irt, xy[n]);
00210        }
00211      };
00212 
00214      class Create {
00215      public:
00217        Create(void) {
00218          for (IntRelTypes irts; irts(); ++irts) {
00219            for (int i=1; i<=7; i += 3) {
00220              for (int m=0; m<=3; m++)
00221                (void) new BoolInt(i, m, irts.irt());
00222              (void) new BoolVar(i, irts.irt());
00223            }
00224            for (int i=1; i<=7; i += 2) {
00225              for (int m=0; m<=i+1; m++)
00226                (void) new IntInt(i, m, irts.irt());
00227              if (i <= 5)
00228                (void) new IntVar(i, irts.irt());
00229            }
00230          }
00231        }
00232      };
00233 
00234      Create c;
00236 
00237    }
00238 }}
00239 
00240 // STATISTICS: test-int
00241