Generated on Fri Aug 24 2012 04:52:09 for Gecode by doxygen 1.8.1.2
distinct.cpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christian Schulte <schulte@gecode.org>
5  * Mikael Lagerkvist <lagerkvist@gecode.org>
6  *
7  * Copyright:
8  * Christian Schulte, 2005
9  * Mikael Lagerkvist, 2006
10  *
11  * Last modified:
12  * $Date: 2011-09-08 00:15:16 +1000 (Thu, 08 Sep 2011) $ by $Author: schulte $
13  * $Revision: 12394 $
14  *
15  * This file is part of Gecode, the generic constraint
16  * development environment:
17  * http://www.gecode.org
18  *
19  * Permission is hereby granted, free of charge, to any person obtaining
20  * a copy of this software and associated documentation files (the
21  * "Software"), to deal in the Software without restriction, including
22  * without limitation the rights to use, copy, modify, merge, publish,
23  * distribute, sublicense, and/or sell copies of the Software, and to
24  * permit persons to whom the Software is furnished to do so, subject to
25  * the following conditions:
26  *
27  * The above copyright notice and this permission notice shall be
28  * included in all copies or substantial portions of the Software.
29  *
30  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
31  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
32  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
33  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
34  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
35  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
36  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
37  *
38  */
39 
40 #include "test/int.hh"
41 
42 namespace Test { namespace Int {
43 
45  namespace Distinct {
46 
52 
53  template<bool useCount>
54  class Distinct : public Test {
55  private:
57  public:
60  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
61  str(icl)+"::Sparse",6,d0,false,icl), d(d0) {}
64  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
65  str(icl)+"::Dense",6,min,max,false,icl), d(min,max) {}
67  virtual bool solution(const Assignment& x) const {
68  for (int i=0; i<x.size(); i++)
69  for (int j=i+1; j<x.size(); j++)
70  if (x[i]==x[j])
71  return false;
72  return true;
73  }
75  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
76  if (!useCount) {
77  Gecode::distinct(home, x, icl);
78  } else {
80  int i = 0;
82  for (Gecode::IntSetValues dr2(d); dr2(); ++dr2)
83  ia[i++] = dr2.val();
84  Gecode::count(home, x, Gecode::IntSet(0,1), ia, icl);
85  }
86  }
87  };
88 
90  class Offset : public Test {
91  public:
94  : Test("Distinct::Offset::Sparse::"+str(icl),6,d,false,icl) {}
97  : Test("Distinct::Offset::Dense::"+str(icl),6,min,max,false,icl) {}
99  virtual bool solution(const Assignment& x) const {
100  for (int i=0; i<x.size(); i++)
101  for (int j=i+1; j<x.size(); j++)
102  if (x[i]+i==x[j]+j)
103  return false;
104  return true;
105  }
107  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
108  Gecode::IntArgs c(x.size());
109  for (int i=0; i<x.size(); i++)
110  c[i]=i;
111  Gecode::distinct(home, c, x, icl);
112  }
113  };
114 
116  class Random : public Test {
117  public:
120  : Test("Distinct::Random::"+str(icl),n,min,max,false,icl) {
121  testsearch = false;
122  }
124  virtual Assignment* assignment(void) const {
125  return new RandomAssignment(arity,dom,100);
126  }
128  virtual bool solution(const Assignment& x) const {
129  for (int i=0; i<x.size(); i++)
130  for (int j=i+1; j<x.size(); j++)
131  if (x[i]==x[j])
132  return false;
133  return true;
134  }
136  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
137  Gecode::distinct(home, x, icl);
138  }
139  };
140 
142  class Pathological : public Base {
143  protected:
145  int n;
149  class TestSpace : public Gecode::Space {
150  public:
152  TestSpace(void) {}
154  TestSpace(bool share, TestSpace& s)
155  : Gecode::Space(share,s) {}
157  virtual Gecode::Space* copy(bool share) {
158  return new TestSpace(share,*this);
159  }
160  };
161  public:
164  : Base("Int::Distinct::Pathological::"+
165  Test::str(n0)+"::"+Test::str(icl0)), n(n0), icl(icl0) {}
167  virtual bool run(void) {
168  using namespace Gecode;
169  {
170  TestSpace* s = new TestSpace;
171  IntVarArgs x(n);
172  for (int i=0; i<n; i++)
173  x[i] = IntVar(*s,0,i);
174  distinct(*s,x,icl);
175  if (s->status() == SS_FAILED) {
176  delete s; return false;
177  }
178  for (int i=0; i<n; i++)
179  if (!x[i].assigned() || (x[i].val() != i)) {
180  delete s; return false;
181  }
182  delete s;
183  }
184  {
185  TestSpace* s = new TestSpace;
186  IntVarArgs x(2*n);
187  for (int i=0; i<n; i++) {
188  int v[] = {0,i};
189  IntSet d(v,2);
190  x[i] = IntVar(*s,d);
191  }
192  for (int i=n; i<2*n; i++)
193  x[i] = IntVar(*s,n-1,i);
194  distinct(*s,x,icl);
195  if (s->status() == SS_FAILED) {
196  delete s; return false;
197  }
198  for (int i=0; i<n; i++)
199  if (!x[i].assigned() || (x[i].val() != i)) {
200  delete s; return false;
201  }
202  delete s;
203  }
204  return true;
205  }
206  };
207 
208  const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
209  Gecode::IntSet d(v,7);
210 
211  Distinct<false> dom_d(-3,3,Gecode::ICL_DOM);
212  Distinct<false> bnd_d(-3,3,Gecode::ICL_BND);
213  Distinct<false> val_d(-3,3,Gecode::ICL_VAL);
214  Distinct<false> dom_s(d,Gecode::ICL_DOM);
215  Distinct<false> bnd_s(d,Gecode::ICL_BND);
216  Distinct<false> val_s(d,Gecode::ICL_VAL);
217 
218  Distinct<true> count_dom_d(-3,3,Gecode::ICL_DOM);
219  Distinct<true> count_bnd_d(-3,3,Gecode::ICL_BND);
220  Distinct<true> count_val_d(-3,3,Gecode::ICL_VAL);
221  Distinct<true> count_dom_s(d,Gecode::ICL_DOM);
222  Distinct<true> count_bnd_s(d,Gecode::ICL_BND);
223  Distinct<true> count_val_s(d,Gecode::ICL_VAL);
224 
225  Offset dom_od(-3,3,Gecode::ICL_DOM);
226  Offset bnd_od(-3,3,Gecode::ICL_BND);
227  Offset val_od(-3,3,Gecode::ICL_VAL);
228  Offset dom_os(d,Gecode::ICL_DOM);
229  Offset bnd_os(d,Gecode::ICL_BND);
230  Offset val_os(d,Gecode::ICL_VAL);
231 
232  Random dom_r(20,-50,50,Gecode::ICL_DOM);
233  Random bnd_r(50,-500,500,Gecode::ICL_BND);
234  Random val_r(50,-500,500,Gecode::ICL_VAL);
235 
239 
244 
245  }
246 }}
247 
248 // STATISTICS: test-int