Generated on Fri Jan 10 2020 11:38:25 for Gecode by doxygen 1.8.16
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  * This file is part of Gecode, the generic constraint
12  * development environment:
13  * http://www.gecode.org
14  *
15  * Permission is hereby granted, free of charge, to any person obtaining
16  * a copy of this software and associated documentation files (the
17  * "Software"), to deal in the Software without restriction, including
18  * without limitation the rights to use, copy, modify, merge, publish,
19  * distribute, sublicense, and/or sell copies of the Software, and to
20  * permit persons to whom the Software is furnished to do so, subject to
21  * the following conditions:
22  *
23  * The above copyright notice and this permission notice shall be
24  * included in all copies or substantial portions of the Software.
25  *
26  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33  *
34  */
35 
36 #include "test/int.hh"
37 #include <gecode/minimodel.hh>
38 
39 namespace Test { namespace Int {
40 
42  namespace Distinct {
43 
49  template<bool useCount>
51  class Distinct : public Test {
52  public:
55  int n=6)
56  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
57  str(ipl)+"::Sparse::"+str(n),n,d0,false,ipl) {}
60  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
61  str(ipl)+"::Dense",6,min,max,false,ipl) {}
63  virtual bool solution(const Assignment& x) const {
64  for (int i=0; i<x.size(); i++)
65  for (int j=i+1; j<x.size(); j++)
66  if (x[i]==x[j])
67  return false;
68  return true;
69  }
71  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
72  if (!useCount) {
73  Gecode::distinct(home, x, ipl);
74  } else {
76  int i = 0;
78  for (Gecode::IntSetValues dr2(dom); dr2(); ++dr2)
79  ia[i++] = dr2.val();
80  Gecode::count(home, x, Gecode::IntSet(0,1), ia, ipl);
81  }
82  }
83  };
84 
86  class Offset : public Test {
87  public:
90  : Test("Distinct::Offset::Sparse::"+str(ipl),6,d,false,ipl) {}
93  : Test("Distinct::Offset::Dense::"+str(ipl),6,min,max,false,ipl) {}
95  virtual bool solution(const Assignment& x) const {
96  for (int i=0; i<x.size(); i++)
97  for (int j=i+1; j<x.size(); j++)
98  if (x[i]+i==x[j]+j)
99  return false;
100  return true;
101  }
103  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
104  Gecode::IntArgs c(x.size());
105  for (int i=0; i<x.size(); i++)
106  c[i]=i;
107  Gecode::distinct(home, c, x, ipl);
108  }
109  };
110 
112  class Optional : public Test {
113  public:
116  : Test("Distinct::Optional::"+str(ipl)+"::"+str(d),
117  6,Gecode::IntSet(d),false,ipl) {}
119  virtual bool solution(const Assignment& x) const {
120  int n = x.size() / 2;
121  for (int i=0; i<n; i++)
122  if ((x[i] < 0) || (x[i] > 1))
123  return false;
124  for (int i=0; i<n; i++)
125  for (int j=i+1; j<n; j++)
126  if ((x[i] == 1) && (x[j] == 1) && (x[n+i] == x[n+j]))
127  return false;
128  return true;
129  }
131  virtual void post(Gecode::Space& home, Gecode::IntVarArray& bx) {
132  int n = bx.size() / 2;
135  for (int i=0; i<n; i++) {
136  b[i] = Gecode::channel(home, bx[i]);
137  x[i] = bx[n+i];
138  }
139  Gecode::distinct(home, b, x, ipl);
140  }
141  };
142 
144  class Except : public Test {
145  public:
148  : Test("Distinct::Except::"+str(ipl)+"::"+str(d),
149  3,Gecode::IntSet(d),false,ipl) {
150  contest = CTL_NONE;
151  }
153  virtual bool solution(const Assignment& x) const {
154  for (int i=0; i<x.size(); i++)
155  for (int j=i+1; j<x.size(); j++)
156  if ((x[i] != 0) && (x[j] != 0) && (x[i] == x[j]))
157  return false;
158  return true;
159  }
161  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
162  Gecode::distinct(home, x, 0, ipl);
163  }
164  };
165 
167  class Random : public Test {
168  public:
171  : Test("Distinct::Random::"+str(ipl),n,min,max,false,ipl) {
172  testsearch = false;
173  }
175  virtual Assignment* assignment(void) const {
176  return new RandomAssignment(arity,dom,100);
177  }
179  virtual bool solution(const Assignment& x) const {
180  for (int i=0; i<x.size(); i++)
181  for (int j=i+1; j<x.size(); j++)
182  if (x[i]==x[j])
183  return false;
184  return true;
185  }
187  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
188  Gecode::distinct(home, x, ipl);
189  }
190  };
191 
193  class Pathological : public Base {
194  protected:
196  int n;
200  class TestSpace : public Gecode::Space {
201  public:
203  TestSpace(void) {}
206  : Gecode::Space(s) {}
208  virtual Gecode::Space* copy(void) {
209  return new TestSpace(*this);
210  }
211  };
212  public:
215  : Base("Int::Distinct::Pathological::"+
216  Test::str(n0)+"::"+Test::str(ipl0)), n(n0), ipl(ipl0) {}
218  virtual bool run(void) {
219  using namespace Gecode;
220  {
221  TestSpace* s = new TestSpace;
222  IntVarArgs x(n);
223  for (int i=0; i<n; i++)
224  x[i] = IntVar(*s,0,i);
225  distinct(*s,x,ipl);
226  if (s->status() == SS_FAILED) {
227  delete s; return false;
228  }
229  for (int i=0; i<n; i++)
230  if (!x[i].assigned() || (x[i].val() != i)) {
231  delete s; return false;
232  }
233  delete s;
234  }
235  {
236  TestSpace* s = new TestSpace;
237  IntVarArgs x(2*n);
238  for (int i=0; i<n; i++) {
239  int v[] = {0,i};
240  IntSet d(v,2);
241  x[i] = IntVar(*s,d);
242  }
243  for (int i=n; i<2*n; i++)
244  x[i] = IntVar(*s,n-1,i);
245  distinct(*s,x,ipl);
246  if (s->status() == SS_FAILED) {
247  delete s; return false;
248  }
249  for (int i=0; i<n; i++)
250  if (!x[i].assigned() || (x[i].val() != i)) {
251  delete s; return false;
252  }
253  delete s;
254  }
255  return true;
256  }
257  };
258 
259  const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
260  Gecode::IntSet d(v,7);
261  const int vl[6] = {Gecode::Int::Limits::min+0,
267  Gecode::IntSet dl(vl,6);
268 
269  Distinct<false> dom_d(-3,3,Gecode::IPL_DOM);
270  Distinct<false> bnd_d(-3,3,Gecode::IPL_BND);
271  Distinct<false> val_d(-3,3,Gecode::IPL_VAL);
272  Distinct<false> dom_s(d,Gecode::IPL_DOM);
273  Distinct<false> bnd_s(d,Gecode::IPL_BND);
274  Distinct<false> val_s(d,Gecode::IPL_VAL);
275 
276  Distinct<false> dom_l(dl,Gecode::IPL_DOM,5);
277  Distinct<false> bnd_l(dl,Gecode::IPL_BND,5);
278  Distinct<false> val_l(dl,Gecode::IPL_VAL,5);
279 
280  Distinct<true> count_dom_d(-3,3,Gecode::IPL_DOM);
281  Distinct<true> count_bnd_d(-3,3,Gecode::IPL_BND);
282  Distinct<true> count_val_d(-3,3,Gecode::IPL_VAL);
283  Distinct<true> count_dom_s(d,Gecode::IPL_DOM);
284  Distinct<true> count_bnd_s(d,Gecode::IPL_BND);
285  Distinct<true> count_val_s(d,Gecode::IPL_VAL);
286 
287  Offset dom_od(-3,3,Gecode::IPL_DOM);
288  Offset bnd_od(-3,3,Gecode::IPL_BND);
289  Offset val_od(-3,3,Gecode::IPL_VAL);
290  Offset dom_os(d,Gecode::IPL_DOM);
291  Offset bnd_os(d,Gecode::IPL_BND);
292  Offset val_os(d,Gecode::IPL_VAL);
293 
295  0,1,
298  0,1,
300  Gecode::IntArgs v3({0,1,2,3});
301  Gecode::IntArgs v4({0,1,2});
302  Gecode::IntArgs v5({0,1});
303 
304  Optional od1(v1,Gecode::IPL_DOM);
305  Optional ob1(v1,Gecode::IPL_BND);
306  Optional ov1(v1,Gecode::IPL_VAL);
307  Optional od2(v2,Gecode::IPL_DOM);
308  Optional ob2(v2,Gecode::IPL_BND);
309  Optional ov2(v2,Gecode::IPL_VAL);
310  Optional od3(v3,Gecode::IPL_DOM);
311  Optional ob3(v3,Gecode::IPL_BND);
312  Optional ov3(v3,Gecode::IPL_VAL);
313  Optional od4(v4,Gecode::IPL_DOM);
314  Optional ob4(v4,Gecode::IPL_BND);
315  Optional ov4(v4,Gecode::IPL_VAL);
316  Optional od5(v5,Gecode::IPL_DOM);
317  Optional ob5(v5,Gecode::IPL_BND);
318  Optional ov5(v5,Gecode::IPL_VAL);
319 
320  Except ed1(v1,Gecode::IPL_DOM);
321  Except eb1(v1,Gecode::IPL_BND);
322  Except ev1(v1,Gecode::IPL_VAL);
323  Except ed2(v2,Gecode::IPL_DOM);
324  Except eb2(v2,Gecode::IPL_BND);
325  Except ev2(v2,Gecode::IPL_VAL);
326  Except ed5(v5,Gecode::IPL_DOM);
327  Except eb5(v5,Gecode::IPL_BND);
328  Except ev5(v5,Gecode::IPL_VAL);
329 
330  Random dom_r(20,-50,50,Gecode::IPL_DOM);
331  Random bnd_r(50,-500,500,Gecode::IPL_BND);
332  Random val_r(50,-500,500,Gecode::IPL_VAL);
333 
337 
342 
343  }
344 }}
345 
346 // STATISTICS: test-int
Optional ov5(v5, Gecode::IPL_VAL)
struct Gecode::@602::NNF::@65::@66 b
For binary nodes (and, or, eqv)
bool testsearch
Whether to perform search test.
Definition: int.hh:238
ConTestLevel contest
Whether to test for certain consistency.
Definition: int.hh:236
Gecode::IntArgs v5({0, 1})
Simple test for optional distinct constraint.
Definition: distinct.cpp:112
Gecode::IntPropLevel ipl
Propagation level.
Definition: int.hh:234
Simple test for distinct constraint.
Definition: distinct.cpp:51
Generate random selection of assignments.
Definition: int.hh:96
Passing integer variables.
Definition: int.hh:656
virtual void post(Gecode::Space &home, Gecode::IntVarArray &bx)
Post constraint on bx.
Definition: distinct.cpp:131
unsigned int size(I &i)
Size of all ranges of range iterator i.
Optional od2(v2, Gecode::IPL_DOM)
Distinct< false > val_s(d, Gecode::IPL_VAL)
Distinct< false > dom_s(d, Gecode::IPL_DOM)
Gecode::IntArgs v2({Gecode::Int::Limits::min, 0, 1, Gecode::Int::Limits::max-4})
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:43
Gecode::IntArgs v1({Gecode::Int::Limits::min+4, 0, 1, Gecode::Int::Limits::max})
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:119
No consistency-test.
Definition: int.hh:140
Except ed1(v1, Gecode::IPL_DOM)
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: distinct.cpp:175
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:95
Offset(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:92
Distinct(const Gecode::IntSet &d0, Gecode::IntPropLevel ipl, int n=6)
Create and register test.
Definition: distinct.cpp:54
Value propagation.
Definition: int.hh:977
const FloatNum min
Smallest allowed float value.
Definition: float.hh:846
Random(int n, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:170
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:974
Computation spaces.
Definition: core.hpp:1742
Optional(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:115
Optional ob1(v1, Gecode::IPL_BND)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on bx.
Definition: distinct.cpp:161
Integer variable array.
Definition: int.hh:763
Except eb5(v5, Gecode::IPL_BND)
Optional ob5(v5, Gecode::IPL_BND)
Range iterator for integer sets.
Definition: int.hh:292
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:926
Except eb2(v2, Gecode::IPL_BND)
Offset val_od(-3, 3, Gecode::IPL_VAL)
Distinct< false > val_l(dl, Gecode::IPL_VAL, 5)
Random val_r(50,-500, 500, Gecode::IPL_VAL)
Optional ob2(v2, Gecode::IPL_BND)
Optional ov3(v3, Gecode::IPL_VAL)
int n
Number of variables.
Definition: distinct.cpp:196
Random dom_r(20,-50, 50, Gecode::IPL_DOM)
Gecode toplevel namespace
Except eb1(v1, Gecode::IPL_BND)
Base class for all tests to be run
Definition: test.hh:103
Integer sets.
Definition: int.hh:174
Distinct< true > count_dom_d(-3, 3, Gecode::IPL_DOM)
Offset dom_od(-3, 3, Gecode::IPL_DOM)
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:249
Distinct< true > count_bnd_d(-3, 3, Gecode::IPL_BND)
Optional ob4(v4, Gecode::IPL_BND)
Passing Boolean variables.
Definition: int.hh:712
Randomized test for distinct constraint.
Definition: distinct.cpp:167
TestSpace(TestSpace &s)
Constructor for cloning s.
Definition: distinct.cpp:205
Offset bnd_os(d, Gecode::IPL_BND)
Domain propagation Options: basic versus advanced propagation.
Definition: int.hh:979
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:179
Offset val_os(d, Gecode::IPL_VAL)
Optional od4(v4, Gecode::IPL_DOM)
Pathological p_16_b(16, Gecode::IPL_BND)
Except ev2(v2, Gecode::IPL_VAL)
Optional ov2(v2, Gecode::IPL_VAL)
Distinct< true > count_bnd_s(d, Gecode::IPL_BND)
const int max
Largest allowed integer value.
Definition: int.hh:116
Integer variables.
Definition: int.hh:371
TestSpace(void)
Constructor.
Definition: distinct.cpp:203
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:71
Bounds propagation.
Definition: int.hh:978
Offset dom_os(d, Gecode::IPL_DOM)
int arity
Number of variables.
Definition: int.hh:226
Gecode::IntSet dl(vl, 6)
Test space
Definition: distinct.cpp:200
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:252
virtual bool run(void)
Perform test.
Definition: distinct.cpp:218
Base class for assignments
Definition: int.hh:59
Distinct< true > count_val_s(d, Gecode::IPL_VAL)
Simple test for distinct except constant constraint.
Definition: distinct.cpp:144
Offset bnd_od(-3, 3, Gecode::IPL_BND)
Space(void)
Default constructor.
Definition: core.cpp:115
Distinct< false > bnd_s(d, Gecode::IPL_BND)
const int v[7]
Definition: distinct.cpp:259
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:40
Gecode::IntArgs v3({0, 1, 2, 3})
Pathological p_32_b(32, Gecode::IPL_BND)
const int vl[6]
Definition: distinct.cpp:261
Optional ov1(v1, Gecode::IPL_VAL)
Pathological p_32_d(32, Gecode::IPL_DOM)
Optional od5(v5, Gecode::IPL_DOM)
Gecode::IntSet d(v, 7)
void channel(Home home, FloatVar x0, IntVar x1)
Post propagator for channeling a float and an integer variable .
Definition: channel.cpp:41
Definition: flatzinc.cpp:52
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:187
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:153
Except(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:147
Except ev1(v1, Gecode::IPL_VAL)
Distinct< false > bnd_l(dl, Gecode::IPL_BND, 5)
Gecode::IntArgs v4({0, 1, 2})
Optional od3(v3, Gecode::IPL_DOM)
Except ed5(v5, Gecode::IPL_DOM)
General test support.
Definition: afc.cpp:39
const int min
Smallest allowed integer value.
Definition: int.hh:118
Optional od1(v1, Gecode::IPL_DOM)
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:63
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:46
Distinct< false > val_d(-3, 3, Gecode::IPL_VAL)
Pathological p_16_d(16, Gecode::IPL_DOM)
Pathological p_16_v(16, Gecode::IPL_VAL)
Except ev5(v5, Gecode::IPL_VAL)
Distinct< false > bnd_d(-3, 3, Gecode::IPL_BND)
Random bnd_r(50,-500, 500, Gecode::IPL_BND)
Gecode::FloatVal c(-8, 8)
Distinct< false > dom_l(dl, Gecode::IPL_DOM, 5)
Distinct< false > dom_d(-3, 3, Gecode::IPL_DOM)
Optional ov4(v4, Gecode::IPL_VAL)
Distinct(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:59
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:234
Testing pathological cases
Definition: distinct.cpp:193
Gecode::IntPropLevel ipl
Consistency level.
Definition: distinct.cpp:198
Distinct< true > count_val_d(-3, 3, Gecode::IPL_VAL)
Offset(const Gecode::IntSet &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:89
Passing integer arguments.
Definition: int.hh:628
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:103
Value iterator for integer sets.
Definition: int.hh:333
Gecode::IntArgs i({1, 2, 3, 4})
Distinct< true > count_dom_s(d, Gecode::IPL_DOM)
Space is failed
Definition: core.hpp:1682
virtual Gecode::Space * copy(void)
Copy space during cloning.
Definition: distinct.cpp:208
Pathological p_32_v(32, Gecode::IPL_VAL)
Optional ob3(v3, Gecode::IPL_BND)
Simple test for distinct constraint with offsets.
Definition: distinct.cpp:86
const FloatNum max
Largest allowed float value.
Definition: float.hh:844
void distinct(Home home, const IntVarArgs &x, int c, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:154
Pathological(int n0, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: distinct.cpp:214
Except ed2(v2, Gecode::IPL_DOM)
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:228
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:209