Generated on Sat Feb 7 2015 02:01:21 for Gecode by doxygen 1.8.9.1
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: 2013-11-15 14:53:16 +0100 (Fri, 15 Nov 2013) $ by $Author: schulte $
13  * $Revision: 14077 $
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  template<bool useCount>
54  class Distinct : public Test {
55  public:
58  int n=6)
59  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
60  str(icl)+"::Sparse::"+str(n),n,d0,false,icl) {}
63  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
64  str(icl)+"::Dense",6,min,max,false,icl) {}
66  virtual bool solution(const Assignment& x) const {
67  for (int i=0; i<x.size(); i++)
68  for (int j=i+1; j<x.size(); j++)
69  if (x[i]==x[j])
70  return false;
71  return true;
72  }
74  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
75  if (!useCount) {
76  Gecode::distinct(home, x, icl);
77  } else {
79  int i = 0;
81  for (Gecode::IntSetValues dr2(dom); dr2(); ++dr2)
82  ia[i++] = dr2.val();
83  Gecode::count(home, x, Gecode::IntSet(0,1), ia, icl);
84  }
85  }
86  };
87 
89  class Offset : public Test {
90  public:
93  : Test("Distinct::Offset::Sparse::"+str(icl),6,d,false,icl) {}
96  : Test("Distinct::Offset::Dense::"+str(icl),6,min,max,false,icl) {}
98  virtual bool solution(const Assignment& x) const {
99  for (int i=0; i<x.size(); i++)
100  for (int j=i+1; j<x.size(); j++)
101  if (x[i]+i==x[j]+j)
102  return false;
103  return true;
104  }
106  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
107  Gecode::IntArgs c(x.size());
108  for (int i=0; i<x.size(); i++)
109  c[i]=i;
110  Gecode::distinct(home, c, x, icl);
111  }
112  };
113 
115  class Random : public Test {
116  public:
119  : Test("Distinct::Random::"+str(icl),n,min,max,false,icl) {
120  testsearch = false;
121  }
123  virtual Assignment* assignment(void) const {
124  return new RandomAssignment(arity,dom,100);
125  }
127  virtual bool solution(const Assignment& x) const {
128  for (int i=0; i<x.size(); i++)
129  for (int j=i+1; j<x.size(); j++)
130  if (x[i]==x[j])
131  return false;
132  return true;
133  }
135  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
136  Gecode::distinct(home, x, icl);
137  }
138  };
139 
141  class Pathological : public Base {
142  protected:
144  int n;
148  class TestSpace : public Gecode::Space {
149  public:
151  TestSpace(void) {}
153  TestSpace(bool share, TestSpace& s)
154  : Gecode::Space(share,s) {}
156  virtual Gecode::Space* copy(bool share) {
157  return new TestSpace(share,*this);
158  }
159  };
160  public:
163  : Base("Int::Distinct::Pathological::"+
164  Test::str(n0)+"::"+Test::str(icl0)), n(n0), icl(icl0) {}
166  virtual bool run(void) {
167  using namespace Gecode;
168  {
169  TestSpace* s = new TestSpace;
170  IntVarArgs x(n);
171  for (int i=0; i<n; i++)
172  x[i] = IntVar(*s,0,i);
173  distinct(*s,x,icl);
174  if (s->status() == SS_FAILED) {
175  delete s; return false;
176  }
177  for (int i=0; i<n; i++)
178  if (!x[i].assigned() || (x[i].val() != i)) {
179  delete s; return false;
180  }
181  delete s;
182  }
183  {
184  TestSpace* s = new TestSpace;
185  IntVarArgs x(2*n);
186  for (int i=0; i<n; i++) {
187  int v[] = {0,i};
188  IntSet d(v,2);
189  x[i] = IntVar(*s,d);
190  }
191  for (int i=n; i<2*n; i++)
192  x[i] = IntVar(*s,n-1,i);
193  distinct(*s,x,icl);
194  if (s->status() == SS_FAILED) {
195  delete s; return false;
196  }
197  for (int i=0; i<n; i++)
198  if (!x[i].assigned() || (x[i].val() != i)) {
199  delete s; return false;
200  }
201  delete s;
202  }
203  return true;
204  }
205  };
206 
207  const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
208  Gecode::IntSet d(v,7);
209  const int vl[6] = {Gecode::Int::Limits::min+0,
211  Gecode::Int::Limits::min+2,
213  Gecode::Int::Limits::max-1,
214  Gecode::Int::Limits::max-0};
215  Gecode::IntSet dl(vl,6);
216 
223 
227 
234 
235  Offset dom_od(-3,3,Gecode::ICL_DOM);
236  Offset bnd_od(-3,3,Gecode::ICL_BND);
237  Offset val_od(-3,3,Gecode::ICL_VAL);
238  Offset dom_os(d,Gecode::ICL_DOM);
239  Offset bnd_os(d,Gecode::ICL_BND);
240  Offset val_os(d,Gecode::ICL_VAL);
241 
242  Random dom_r(20,-50,50,Gecode::ICL_DOM);
243  Random bnd_r(50,-500,500,Gecode::ICL_BND);
244  Random val_r(50,-500,500,Gecode::ICL_VAL);
245 
249 
254 
255  }
256 }}
257 
258 // STATISTICS: test-int
Distinct< false > dom_l(dl, Gecode::ICL_DOM, 5)
Offset bnd_os(d, Gecode::ICL_BND)
Pathological p_16_b(16, Gecode::ICL_BND)
Propagator for negated equality
Definition: rel.hh:261
Offset dom_od(-3, 3, Gecode::ICL_DOM)
IntConLevel
Consistency levels for integer propagators.
Definition: int.hh:937
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:135
Distinct< true > count_bnd_d(-3, 3, Gecode::ICL_BND)
Offset val_od(-3, 3, Gecode::ICL_VAL)
Offset bnd_od(-3, 3, Gecode::ICL_BND)
Range iterator for integer sets.
Definition: int.hh:271
const FloatNum max
Largest allowed float value.
Definition: float.hh:831
Distinct< true > count_dom_s(d, Gecode::ICL_DOM)
Distinct< true > count_bnd_s(d, Gecode::ICL_BND)
Value propagation or consistency (naive)
Definition: int.hh:938
Gecode::IntSet dl(vl, 6)
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:220
Offset dom_os(d, Gecode::ICL_DOM)
Integer variable array.
Definition: int.hh:741
Distinct< false > bnd_d(-3, 3, Gecode::ICL_BND)
Random dom_r(20,-50, 50, Gecode::ICL_DOM)
Pathological p_16_d(16, Gecode::ICL_DOM)
TestSpace(bool share, TestSpace &s)
Constructor for cloning s.
Definition: distinct.cpp:153
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: distinct.cpp:123
Pathological p_32_v(32, Gecode::ICL_VAL)
const int max
Largest allowed integer value.
Definition: int.hh:113
Computation spaces.
Definition: core.hpp:1362
Generate random selection of assignments.
Definition: int.hh:100
const int min
Smallest allowed integer value.
Definition: int.hh:115
Gecode::IntSet d(v, 7)
static std::string str(Gecode::ExtensionalPropKind epk)
Map extensional propagation kind to string.
Definition: int.hpp:212
Pathological p_32_d(32, Gecode::ICL_DOM)
Random(int n, int min, int max, Gecode::IntConLevel icl)
Create and register test.
Definition: distinct.cpp:118
Pathological p_32_b(32, Gecode::ICL_BND)
Gecode::FloatVal c(-8, 8)
const FloatNum min
Smallest allowed float value.
Definition: float.hh:833
Distinct< false > bnd_s(d, Gecode::ICL_BND)
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Randomized test for distinct constraint.
Definition: distinct.cpp:115
virtual Gecode::Space * copy(bool share)
Copy space during cloning.
Definition: distinct.cpp:156
Random val_r(50,-500, 500, Gecode::ICL_VAL)
Gecode::IntConLevel icl
Consistency level.
Definition: int.hh:226
Distinct< true > count_val_d(-3, 3, Gecode::ICL_VAL)
Simple test for distinct constraint.
Definition: distinct.cpp:54
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:74
Pathological p_16_v(16, Gecode::ICL_VAL)
Distinct< false > val_s(d, Gecode::ICL_VAL)
Offset(const Gecode::IntSet &d, Gecode::IntConLevel icl)
Create and register test.
Definition: distinct.cpp:92
unsigned int size(I &i)
Size of all ranges of range iterator i.
Base class for all tests to be run
Definition: test.hh:107
Random bnd_r(50,-500, 500, Gecode::ICL_BND)
Distinct< false > bnd_l(dl, Gecode::ICL_BND, 5)
Distinct< false > dom_d(-3, 3, Gecode::ICL_DOM)
Integer sets.
Definition: int.hh:171
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:66
Offset(int min, int max, Gecode::IntConLevel icl)
Create and register test.
Definition: distinct.cpp:95
Passing integer variables.
Definition: int.hh:636
Passing integer arguments.
Definition: int.hh:607
Pathological(int n0, Gecode::IntConLevel icl0)
Create and register test.
Definition: distinct.cpp:162
const int v[7]
Definition: distinct.cpp:207
General test support.
Definition: afc.cpp:43
Distinct(int min, int max, Gecode::IntConLevel icl)
Create and register test.
Definition: distinct.cpp:62
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:106
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Space(void)
Default constructor.
Definition: core.cpp:114
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntConLevel)
Post propagator for .
Definition: count.cpp:44
bool testsearch
Whether to perform search test.
Definition: int.hh:230
Base class for assignments
Definition: int.hh:63
Integer variables.
Definition: int.hh:350
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
Value iterator for integer sets.
Definition: int.hh:312
const int vl[6]
Definition: distinct.cpp:209
void distinct(Home home, const IntArgs &c, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:65
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
Distinct< true > count_dom_d(-3, 3, Gecode::ICL_DOM)
void distinct(Home home, const IntVarArgs &x, IntConLevel icl)
Post propagator for for all .
Definition: distinct.cpp:47
Gecode::IntConLevel icl
Consistency level.
Definition: distinct.cpp:146
Bounds propagation or consistency.
Definition: int.hh:939
Distinct(const Gecode::IntSet &d0, Gecode::IntConLevel icl, int n=6)
Create and register test.
Definition: distinct.cpp:57
Testing pathological cases
Definition: distinct.cpp:141
Gecode toplevel namespace
Offset val_os(d, Gecode::ICL_VAL)
int arity
Number of variables.
Definition: int.hh:218
virtual bool run(void)
Perform test.
Definition: distinct.cpp:166
Space is failed
Definition: core.hpp:1301
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:98
Simple test for distinct constraint with offsets.
Definition: distinct.cpp:89
Distinct< false > val_d(-3, 3, Gecode::ICL_VAL)
Distinct< true > count_val_s(d, Gecode::ICL_VAL)
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
Distinct< false > dom_s(d, Gecode::ICL_DOM)
int size(void) const
Return number of variables.
Definition: int.hpp:50
Domain propagation or consistency.
Definition: int.hh:940
int n
Number of variables.
Definition: distinct.cpp:144
Distinct< false > val_l(dl, Gecode::ICL_VAL, 5)
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:127