Generated on Tue Jan 28 2020 00:00:00 for Gecode by doxygen 1.8.17
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: 2016-04-19 17:19:45 +0200 (Tue, 19 Apr 2016) $ by $Author: schulte $
13  * $Revision: 14967 $
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 #include <gecode/minimodel.hh>
42 
43 namespace Test { namespace Int {
44 
46  namespace Distinct {
47 
53  template<bool useCount>
55  class Distinct : public Test {
56  public:
59  int n=6)
60  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
61  str(ipl)+"::Sparse::"+str(n),n,d0,false,ipl) {}
64  : Test(std::string(useCount ? "Count::Distinct::" : "Distinct::")+
65  str(ipl)+"::Dense",6,min,max,false,ipl) {}
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, ipl);
78  } else {
80  int i = 0;
82  for (Gecode::IntSetValues dr2(dom); dr2(); ++dr2)
83  ia[i++] = dr2.val();
84  Gecode::count(home, x, Gecode::IntSet(0,1), ia, ipl);
85  }
86  }
87  };
88 
90  class Offset : public Test {
91  public:
94  : Test("Distinct::Offset::Sparse::"+str(ipl),6,d,false,ipl) {}
97  : Test("Distinct::Offset::Dense::"+str(ipl),6,min,max,false,ipl) {}
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, ipl);
112  }
113  };
114 
116  class Optional : public Test {
117  public:
120  : Test("Distinct::Optional::"+str(ipl)+"::"+str(d),
121  6,Gecode::IntSet(d),false,ipl) {}
123  virtual bool solution(const Assignment& x) const {
124  int n = x.size() / 2;
125  for (int i=0; i<n; i++)
126  if ((x[i] < 0) || (x[i] > 1))
127  return false;
128  for (int i=0; i<n; i++)
129  for (int j=i+1; j<n; j++)
130  if ((x[i] == 1) && (x[j] == 1) && (x[n+i] == x[n+j]))
131  return false;
132  return true;
133  }
135  virtual void post(Gecode::Space& home, Gecode::IntVarArray& bx) {
136  int n = bx.size() / 2;
139  for (int i=0; i<n; i++) {
140  b[i] = Gecode::channel(home, bx[i]);
141  x[i] = bx[n+i];
142  }
143  Gecode::distinct(home, b, x, ipl);
144  }
145  };
146 
148  class Except : public Test {
149  public:
152  : Test("Distinct::Except::"+str(ipl)+"::"+str(d),
153  3,Gecode::IntSet(d),false,ipl) {
154  contest = CTL_NONE;
155  }
157  virtual bool solution(const Assignment& x) const {
158  for (int i=0; i<x.size(); i++)
159  for (int j=i+1; j<x.size(); j++)
160  if ((x[i] != 0) && (x[j] != 0) && (x[i] == x[j]))
161  return false;
162  return true;
163  }
165  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
166  Gecode::distinct(home, x, 0, ipl);
167  }
168  };
169 
171  class Random : public Test {
172  public:
175  : Test("Distinct::Random::"+str(ipl),n,min,max,false,ipl) {
176  testsearch = false;
177  }
179  virtual Assignment* assignment(void) const {
180  return new RandomAssignment(arity,dom,100);
181  }
183  virtual bool solution(const Assignment& x) const {
184  for (int i=0; i<x.size(); i++)
185  for (int j=i+1; j<x.size(); j++)
186  if (x[i]==x[j])
187  return false;
188  return true;
189  }
191  virtual void post(Gecode::Space& home, Gecode::IntVarArray& x) {
192  Gecode::distinct(home, x, ipl);
193  }
194  };
195 
197  class Pathological : public Base {
198  protected:
200  int n;
204  class TestSpace : public Gecode::Space {
205  public:
207  TestSpace(void) {}
209  TestSpace(bool share, TestSpace& s)
210  : Gecode::Space(share,s) {}
212  virtual Gecode::Space* copy(bool share) {
213  return new TestSpace(share,*this);
214  }
215  };
216  public:
219  : Base("Int::Distinct::Pathological::"+
220  Test::str(n0)+"::"+Test::str(ipl0)), n(n0), ipl(ipl0) {}
222  virtual bool run(void) {
223  using namespace Gecode;
224  {
225  TestSpace* s = new TestSpace;
226  IntVarArgs x(n);
227  for (int i=0; i<n; i++)
228  x[i] = IntVar(*s,0,i);
229  distinct(*s,x,ipl);
230  if (s->status() == SS_FAILED) {
231  delete s; return false;
232  }
233  for (int i=0; i<n; i++)
234  if (!x[i].assigned() || (x[i].val() != i)) {
235  delete s; return false;
236  }
237  delete s;
238  }
239  {
240  TestSpace* s = new TestSpace;
241  IntVarArgs x(2*n);
242  for (int i=0; i<n; i++) {
243  int v[] = {0,i};
244  IntSet d(v,2);
245  x[i] = IntVar(*s,d);
246  }
247  for (int i=n; i<2*n; i++)
248  x[i] = IntVar(*s,n-1,i);
249  distinct(*s,x,ipl);
250  if (s->status() == SS_FAILED) {
251  delete s; return false;
252  }
253  for (int i=0; i<n; i++)
254  if (!x[i].assigned() || (x[i].val() != i)) {
255  delete s; return false;
256  }
257  delete s;
258  }
259  return true;
260  }
261  };
262 
263  const int v[7] = {-1001,-1000,-10,0,10,1000,1001};
264  Gecode::IntSet d(v,7);
265  const int vl[6] = {Gecode::Int::Limits::min+0,
271  Gecode::IntSet dl(vl,6);
272 
273  Distinct<false> dom_d(-3,3,Gecode::IPL_DOM);
274  Distinct<false> bnd_d(-3,3,Gecode::IPL_BND);
275  Distinct<false> val_d(-3,3,Gecode::IPL_VAL);
276  Distinct<false> dom_s(d,Gecode::IPL_DOM);
277  Distinct<false> bnd_s(d,Gecode::IPL_BND);
278  Distinct<false> val_s(d,Gecode::IPL_VAL);
279 
280  Distinct<false> dom_l(dl,Gecode::IPL_DOM,5);
281  Distinct<false> bnd_l(dl,Gecode::IPL_BND,5);
282  Distinct<false> val_l(dl,Gecode::IPL_VAL,5);
283 
284  Distinct<true> count_dom_d(-3,3,Gecode::IPL_DOM);
285  Distinct<true> count_bnd_d(-3,3,Gecode::IPL_BND);
286  Distinct<true> count_val_d(-3,3,Gecode::IPL_VAL);
287  Distinct<true> count_dom_s(d,Gecode::IPL_DOM);
288  Distinct<true> count_bnd_s(d,Gecode::IPL_BND);
289  Distinct<true> count_val_s(d,Gecode::IPL_VAL);
290 
291  Offset dom_od(-3,3,Gecode::IPL_DOM);
292  Offset bnd_od(-3,3,Gecode::IPL_BND);
293  Offset val_od(-3,3,Gecode::IPL_VAL);
294  Offset dom_os(d,Gecode::IPL_DOM);
295  Offset bnd_os(d,Gecode::IPL_BND);
296  Offset val_os(d,Gecode::IPL_VAL);
297 
299  0,1,
302  0,1,
304  Gecode::IntArgs v3(4, 0,1,2,3);
305  Gecode::IntArgs v4(3, 0,1,2);
306  Gecode::IntArgs v5(2, 0,1);
307 
308  Optional od1(v1,Gecode::IPL_DOM);
309  Optional ob1(v1,Gecode::IPL_BND);
310  Optional ov1(v1,Gecode::IPL_VAL);
311  Optional od2(v2,Gecode::IPL_DOM);
312  Optional ob2(v2,Gecode::IPL_BND);
313  Optional ov2(v2,Gecode::IPL_VAL);
314  Optional od3(v3,Gecode::IPL_DOM);
315  Optional ob3(v3,Gecode::IPL_BND);
316  Optional ov3(v3,Gecode::IPL_VAL);
317  Optional od4(v4,Gecode::IPL_DOM);
318  Optional ob4(v4,Gecode::IPL_BND);
319  Optional ov4(v4,Gecode::IPL_VAL);
320  Optional od5(v5,Gecode::IPL_DOM);
321  Optional ob5(v5,Gecode::IPL_BND);
322  Optional ov5(v5,Gecode::IPL_VAL);
323 
324  Except ed1(v1,Gecode::IPL_DOM);
325  Except eb1(v1,Gecode::IPL_BND);
326  Except ev1(v1,Gecode::IPL_VAL);
327  Except ed2(v2,Gecode::IPL_DOM);
328  Except eb2(v2,Gecode::IPL_BND);
329  Except ev2(v2,Gecode::IPL_VAL);
330  Except ed5(v5,Gecode::IPL_DOM);
331  Except eb5(v5,Gecode::IPL_BND);
332  Except ev5(v5,Gecode::IPL_VAL);
333 
334  Random dom_r(20,-50,50,Gecode::IPL_DOM);
335  Random bnd_r(50,-500,500,Gecode::IPL_BND);
336  Random val_r(50,-500,500,Gecode::IPL_VAL);
337 
341 
346 
347  }
348 }}
349 
350 // STATISTICS: test-int
Optional ov5(v5, Gecode::IPL_VAL)
bool testsearch
Whether to perform search test.
Definition: int.hh:242
ConTestLevel contest
Whether to test for certain consistency.
Definition: int.hh:240
Simple test for optional distinct constraint.
Definition: distinct.cpp:116
Gecode::IntPropLevel ipl
Propagation level.
Definition: int.hh:238
Simple test for distinct constraint.
Definition: distinct.cpp:55
Generate random selection of assignments.
Definition: int.hh:100
Gecode::IntArgs v3(4, 0, 1, 2, 3)
Passing integer variables.
Definition: int.hh:639
virtual void post(Gecode::Space &home, Gecode::IntVarArray &bx)
Post constraint on bx.
Definition: distinct.cpp:135
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)
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:123
@ CTL_NONE
No consistency-test.
Definition: int.hh:144
Gecode::IntArgs i(4, 1, 2, 3, 4)
Gecode::IntArgs v1(4, Gecode::Int::Limits::min+4, 0, 1, Gecode::Int::Limits::max)
Except ed1(v1, Gecode::IPL_DOM)
virtual Assignment * assignment(void) const
Create and register initial assignment.
Definition: distinct.cpp:179
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:99
Offset(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:96
Distinct(const Gecode::IntSet &d0, Gecode::IntPropLevel ipl, int n=6)
Create and register test.
Definition: distinct.cpp:58
@ IPL_VAL
Value propagation.
Definition: int.hh:958
const FloatNum min
Smallest allowed float value.
Definition: float.hh:850
Random(int n, int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:174
IntPropLevel
Propagation levels for integer propagators.
Definition: int.hh:955
Computation spaces.
Definition: core.hpp:1748
Optional(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:119
Optional ob1(v1, Gecode::IPL_BND)
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on bx.
Definition: distinct.cpp:165
Integer variable array.
Definition: int.hh:744
Except eb5(v5, Gecode::IPL_BND)
Optional ob5(v5, Gecode::IPL_BND)
Range iterator for integer sets.
Definition: int.hh:274
int size(void) const
Return size of array (number of elements)
Definition: array.hpp:985
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:200
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:107
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:253
Distinct< true > count_bnd_d(-3, 3, Gecode::IPL_BND)
Optional ob4(v4, Gecode::IPL_BND)
Passing Boolean variables.
Definition: int.hh:693
Randomized test for distinct constraint.
Definition: distinct.cpp:171
struct Gecode::@579::NNF::@61::@62 b
For binary nodes (and, or, eqv)
Offset bnd_os(d, Gecode::IPL_BND)
@ IPL_DOM
Domain propagation Preferences: prefer speed or memory.
Definition: int.hh:960
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:183
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:353
Gecode::IntArgs v2(4, Gecode::Int::Limits::min, 0, 1, Gecode::Int::Limits::max-4)
TestSpace(void)
Constructor.
Definition: distinct.cpp:207
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:75
Gecode::IntArgs v5(2, 0, 1)
@ IPL_BND
Bounds propagation.
Definition: int.hh:959
Offset dom_os(d, Gecode::IPL_DOM)
int arity
Number of variables.
Definition: int.hh:230
Gecode::IntSet dl(vl, 6)
Test space
Definition: distinct.cpp:204
SpaceStatus status(StatusStatistics &stat=unused_status)
Query space status.
Definition: core.cpp:224
virtual bool run(void)
Perform test.
Definition: distinct.cpp:222
Base class for assignments
Definition: int.hh:63
Distinct< true > count_val_s(d, Gecode::IPL_VAL)
Simple test for distinct except constant constraint.
Definition: distinct.cpp:148
Offset bnd_od(-3, 3, Gecode::IPL_BND)
Space(void)
Default constructor.
Definition: core.cpp:114
Distinct< false > bnd_s(d, Gecode::IPL_BND)
const int v[7]
Definition: distinct.cpp:263
void count(Home home, const IntVarArgs &x, int n, IntRelType irt, int m, IntPropLevel)
Post propagator for .
Definition: count.cpp:44
Pathological p_32_b(32, Gecode::IPL_BND)
const int vl[6]
Definition: distinct.cpp:265
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:45
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:191
virtual bool solution(const Assignment &x) const
Check whether x is solution.
Definition: distinct.cpp:157
Except(const Gecode::IntArgs &d, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:151
Except ev1(v1, Gecode::IPL_VAL)
Gecode::IntArgs v4(3, 0, 1, 2)
Distinct< false > bnd_l(dl, Gecode::IPL_BND, 5)
Optional od3(v3, Gecode::IPL_DOM)
Except ed5(v5, Gecode::IPL_DOM)
General test support.
Definition: afc.cpp:43
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:67
void distinct(Home home, const IntVarArgs &x, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:50
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)
virtual Gecode::Space * copy(bool share)
Copy space during cloning.
Definition: distinct.cpp:212
Optional ov4(v4, Gecode::IPL_VAL)
Distinct(int min, int max, Gecode::IntPropLevel ipl)
Create and register test.
Definition: distinct.cpp:63
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Testing pathological cases
Definition: distinct.cpp:197
Gecode::IntPropLevel ipl
Consistency level.
Definition: distinct.cpp:202
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:93
Passing integer arguments.
Definition: int.hh:610
virtual void post(Gecode::Space &home, Gecode::IntVarArray &x)
Post constraint on x.
Definition: distinct.cpp:107
TestSpace(bool share, TestSpace &s)
Constructor for cloning s.
Definition: distinct.cpp:209
Value iterator for integer sets.
Definition: int.hh:315
Distinct< true > count_dom_s(d, Gecode::IPL_DOM)
@ SS_FAILED
Space is failed
Definition: core.hpp:1689
Pathological p_32_v(32, Gecode::IPL_VAL)
Optional ob3(v3, Gecode::IPL_BND)
Simple test for distinct constraint with offsets.
Definition: distinct.cpp:90
const FloatNum max
Largest allowed float value.
Definition: float.hh:848
void distinct(Home home, const IntVarArgs &x, int c, IntPropLevel ipl)
Post propagator for for all .
Definition: distinct.cpp:158
Pathological(int n0, Gecode::IntPropLevel ipl0)
Create and register test.
Definition: distinct.cpp:218
Except ed2(v2, Gecode::IPL_DOM)
Gecode::IntSet dom
Domain of variables.
Definition: int.hh:232
static std::string str(Gecode::IntPropLevel ipl)
Map integer propagation level to string.
Definition: int.hpp:212