Generated on Sat Feb 7 2015 02:01:29 for Gecode by doxygen 1.8.9.1
set.hh
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Guido Tack <tack@gecode.org>
5  * Christian Schulte <schulte@gecode.org>
6  *
7  * Copyright:
8  * Guido Tack, 2005
9  * Christian Schulte, 2005
10  *
11  * Last modified:
12  * $Date: 2012-10-19 05:58:26 +0200 (Fri, 19 Oct 2012) $ by $Author: tack $
13  * $Revision: 13156 $
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 #ifndef __GECODE_TEST_SET_HH__
41 #define __GECODE_TEST_SET_HH__
42 
43 #include <gecode/set.hh>
44 #include "test/test.hh"
45 #include "test/int.hh"
46 
47 namespace Test {
48 
50  namespace Set {
51 
57  class FakeSpace : public Gecode::Space {
59  public:
61  FakeSpace(void) {}
63  virtual Gecode::Space* copy(bool share) {
64  (void) share;
65  return NULL;
66  }
67  };
68 
74 
77  private:
79  int cur;
80  int i;
81  public:
85  CountableSetValues(const Gecode::IntSet& d0, int cur0)
86  : dv(d0), cur(cur0), i(1) {
87  if (! (i & cur))
88  operator++();
89  }
91  void init(const Gecode::IntSet& d0, int cur0) {
92  dv = d0;
93  cur = cur0;
94  i = 1;
95  if (! (i & cur))
96  operator++();
97  }
99  bool operator()(void) const {
100  return i<=cur;
101  }
103  void operator++(void) {
104  do {
105  ++dv;
106  i = i<<1;
107  } while (! (i & cur) && i<cur);
108  }
110  int val(void) const { return dv.val(); }
111  };
112 
115  : public Gecode::Iter::Values::ToRanges<CountableSetValues> {
116  private:
119  public:
123  CountableSetRanges(const Gecode::IntSet& d, int cur) : v(d, cur) {
125  }
127  void init(const Gecode::IntSet& d, int cur) {
128  v.init(d, cur);
130  }
131  };
132 
134  class CountableSet {
135  private:
137  Gecode::IntSet d;
139  unsigned int cur;
141  unsigned int lubmax;
142  public:
144  CountableSet(const Gecode::IntSet& s);
146  CountableSet(void) {}
148  void init(const Gecode::IntSet& s);
150  bool operator()(void) const { return cur<lubmax; }
152  void operator++(void);
154  int val(void) const;
155  };
156 
159  private:
161  int n;
163  CountableSet* dsv;
167  bool done;
168  public:
172  int withInt;
174  SetAssignment(int n, const Gecode::IntSet& d, int i = 0);
176  bool operator()(void) const { return !done; }
178  void operator++(void);
180  int operator[](int i) const {
181  assert((i>=0) && (i<n));
182  return dsv[i].val();
183  }
185  int intval(void) const { return ir[0]; }
187  const Test::Int::Assignment& ints(void) const { return ir; }
189  int size(void) const { return n; }
191  ~SetAssignment(void) { delete [] dsv; }
192  };
193 
194 
195  class SetTest;
196 
198  class SetTestSpace : public Gecode::Space {
199  public:
207  int withInt;
211  bool reified;
214 
224  SetTestSpace(int n, Gecode::IntSet& d0, int i, SetTest* t,
225  bool log=true);
235  SetTestSpace(int n, Gecode::IntSet& d0, int i, SetTest* t,
236  Gecode::ReifyMode rm, bool log=true);
238  SetTestSpace(bool share, SetTestSpace& s);
240  virtual Gecode::Space* copy(bool share);
242  void post(void);
244  bool failed(void);
246  void rel(int i, Gecode::SetRelType srt, const Gecode::IntSet& is);
248  void cardinality(int i, int cmin, int cmax);
250  void rel(int i, Gecode::IntRelType irt, int n);
252  void rel(bool sol);
254  void assign(const SetAssignment& a);
256  bool assigned(void) const;
258  void removeFromLub(int v, int i, const SetAssignment& a);
260  void addToGlb(int v, int i, const SetAssignment& a);
262  bool fixprob(void);
264  bool prune(const SetAssignment& a);
265  };
266 
271  class SetTest : public Base {
272  private:
274  int arity;
276  Gecode::IntSet lub;
278  bool reified;
280  int withInt;
281 
283  void removeFromLub(int v, Gecode::SetVar& x, int i,
284  const Gecode::IntSet& a);
286  void addToGlb(int v, Gecode::SetVar& x, int i, const Gecode::IntSet& a);
287  SetAssignment* make_assignment(void);
288  public:
296  SetTest(const std::string& s,
297  int a, const Gecode::IntSet& d, bool r=false, int w=0)
298  : Base("Set::"+s), arity(a), lub(d), reified(r), withInt(w) {}
300  virtual bool solution(const SetAssignment&) const = 0;
302  virtual void post(Gecode::Space& home, Gecode::SetVarArray& x,
303  Gecode::IntVarArray& y) = 0;
308  virtual bool run(void);
309 
311 
312  static std::string str(Gecode::SetRelType srt);
315  static std::string str(Gecode::SetOpType srt);
317  static std::string str(int i);
319  static std::string str(const Gecode::IntArgs& i);
321  };
323 
325  class SetRelTypes {
326  private:
328  static const Gecode::SetRelType srts[6];
330  int i;
331  public:
333  SetRelTypes(void);
335  bool operator()(void) const;
337  void operator++(void);
339  Gecode::SetRelType srt(void) const;
340  };
341 
343  class SetOpTypes {
344  private:
346  static const Gecode::SetOpType sots[4];
348  int i;
349  public:
351  SetOpTypes(void);
353  bool operator()(void) const;
355  void operator++(void);
357  Gecode::SetOpType sot(void) const;
358  };
359 
360 }}
361 
366 std::ostream&
367 operator<<(std::ostream&, const Test::Set::SetAssignment& a);
368 
369 #include "test/set.hpp"
370 
371 #endif
372 
373 // STATISTICS: test-set
void removeFromLub(int v, int i, const SetAssignment &a)
Remove value v from the upper bound of x[i].
Definition: set.cpp:284
virtual void post(Gecode::Space &, Gecode::SetVarArray &, Gecode::IntVarArray &, Gecode::Reify)
Post reified propagator.
Definition: set.hh:305
void operator++(void)
Move to next subset.
Definition: set.cpp:55
void cardinality(int i, int cmin, int cmax)
Perform cardinality tell operation on x[i].
Definition: set.cpp:222
Iterator for Boolean operation types.
Definition: set.hh:343
NodeType t
Type of node.
Definition: bool-expr.cpp:234
CountableSetRanges(const Gecode::IntSet &d, int cur)
Initialize with set d0 and bit-pattern cur0.
Definition: set.hh:123
SetRelType
Common relation types for sets.
Definition: set.hh:644
int size(void) const
Return arity.
Definition: set.hh:189
Iterator for set relation types.
Definition: set.hh:325
void operator++(void)
Move to next value.
Definition: set.hh:103
Gecode::Reify r
Reification information.
Definition: set.hh:209
void log(Home home, FloatVar x0, FloatVar x1)
Post propagator for .
Definition: arithmetic.cpp:151
Gecode::SetVarArray x
Set variables to be tested.
Definition: set.hh:203
void assign(const SetAssignment &a)
Assign all variables to values in a.
Definition: set.cpp:257
int val(void) const
Return current subset.
Definition: set.cpp:68
bool operator()(void) const
Test whether all assignments have been iterated.
Definition: set.hh:176
static std::string str(Gecode::SetRelType srt)
Map set relation to string.
Definition: set.hpp:50
CountableSet(void)
Default constructor.
Definition: set.hh:146
Integer variable array.
Definition: int.hh:741
SetOpType
Common operations for sets.
Definition: set.hh:661
CountableSetRanges(void)
Default constructor.
Definition: set.hh:121
int operator[](int i) const
Return value for variable i.
Definition: set.hh:180
SetAssignment(int n, const Gecode::IntSet &d, int i=0)
Initialize with n set variables, initial bound d and i int variables.
Definition: set.cpp:72
Computation spaces.
Definition: core.hpp:1362
SetTest(const std::string &s, int a, const Gecode::IntSet &d, bool r=false, int w=0)
Constructor.
Definition: set.hh:296
void init(const Gecode::IntSet &d, int cur)
Initialize with set d0 and bit-pattern cur0.
Definition: set.hh:127
SetRelTypes(void)
Initialize iterator.
Definition: set.hpp:88
Gecode::IntSet d(v, 7)
bool reified
Whether the test is for a reified propagator.
Definition: set.hh:211
void rel(int i, Gecode::SetRelType srt, const Gecode::IntSet &is)
Perform set tell operation on x[i].
Definition: set.cpp:201
Gecode::SetOpType sot(void) const
Return current operation type.
Definition: set.hpp:115
Gecode::IntArgs i(4, 1, 2, 3, 4)
SetOpTypes(void)
Initialize iterator.
Definition: set.hpp:104
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
bool assigned(void) const
Test whether all variables are assigned.
Definition: set.cpp:273
bool operator()(void) const
Check if still subsets left.
Definition: set.hh:150
IntRelType
Relation types for integers.
Definition: int.hh:903
NNF * r
Right subtree.
Definition: bool-expr.cpp:246
const Test::Int::Assignment & ints(void) const
Return assignment for integer variables.
Definition: set.hh:187
void init(I &i)
Initialize with value iterator i.
bool operator()(void) const
Test whether iterator is done.
Definition: set.hpp:91
void operator++(void)
Increment to next operation type.
Definition: set.hpp:111
void post(void)
Post propagator.
Definition: set.cpp:174
bool failed(void)
Compute a fixpoint and check for failure.
Definition: set.cpp:187
Range iterator from value iterator.
Reification specification.
Definition: int.hh:854
Base class for all tests to be run
Definition: test.hh:107
virtual bool run(void)
Perform test.
Definition: set.cpp:496
Gecode::IntSet lub
The common superset for all domains.
Definition: set.hh:170
Integer sets.
Definition: int.hh:171
int val(void) const
Return current value.
Definition: set.hh:110
CountableSetValues(void)
Default constructor.
Definition: set.hh:83
Value iterator producing subsets of an IntSet.
Definition: set.hh:76
Passing integer arguments.
Definition: int.hh:607
virtual Gecode::Space * copy(bool share)
Faked copy function.
Definition: set.hh:63
Gecode::IntSet d
Initial domain.
Definition: set.hh:201
SetTestSpace(int n, Gecode::IntSet &d0, int i, SetTest *t, bool log=true)
Create test space without reification.
Definition: set.cpp:124
const int v[7]
Definition: distinct.cpp:207
bool prune(const SetAssignment &a)
Perform random pruning.
Definition: set.cpp:343
General test support.
Definition: afc.cpp:43
int intval(void) const
Return value for first integer variable.
Definition: set.hh:185
int withInt
How many integer variables to iterate.
Definition: set.hh:172
void addToGlb(int v, int i, const SetAssignment &a)
Remove value v from the lower bound of x[i].
Definition: set.cpp:297
FakeSpace(void)
Faked constructor.
Definition: set.hh:61
bool operator()(void) const
Test if finished.
Definition: set.hh:99
Node * x
Pointer to corresponding Boolean expression node.
Definition: bool-expr.cpp:253
Set variables
Definition: set.hh:129
virtual void post(Gecode::Space &home, Gecode::SetVarArray &x, Gecode::IntVarArray &y)=0
Post propagator.
Base class for tests with set constraints
Definition: set.hh:271
Generate all set assignments.
Definition: set.hh:158
void operator++(void)
Move to next assignment.
Definition: set.cpp:80
bool fixprob(void)
Perform fixpoint computation.
Definition: set.cpp:310
Base class for assignments
Definition: int.hh:63
bool operator()(void) const
Test whether iterator is done.
Definition: set.hpp:107
Gecode::IntVarArray y
Int variables to be tested.
Definition: set.hh:205
~SetAssignment(void)
Destructor.
Definition: set.hh:191
Value iterator for integer sets.
Definition: int.hh:312
Range iterator producing subsets of an IntSet.
Definition: set.hh:114
int withInt
How many integer variables are used by the test.
Definition: set.hh:207
CountableSetValues(const Gecode::IntSet &d0, int cur0)
Initialize with set d0 and bit-pattern cur0.
Definition: set.hh:85
int val(void) const
Return current value.
Gecode::SetRelType srt(void) const
Return current relation type.
Definition: set.hpp:99
Set variable array
Definition: set.hh:571
void init(const Gecode::IntSet &d0, int cur0)
Initialize with set d0 and bit-pattern cur0.
Definition: set.hh:91
std::basic_ostream< Char, Traits > & operator<<(std::basic_ostream< Char, Traits > &os, const FloatView &x)
Print float variable view.
Definition: print.hpp:62
virtual bool solution(const SetAssignment &) const =0
Check for solution.
SetTest * test
The test currently run.
Definition: set.hh:213
Space for executing set tests.
Definition: set.hh:198
Iterate all subsets of a given set.
Definition: set.hh:134
ReifyMode
Mode for reification.
Definition: int.hh:826
struct Gecode::@518::NNF::@57::@59 a
For atomic nodes.
Generate all assignments.
Definition: int.hh:83
virtual Gecode::Space * copy(bool share)
Copy space during cloning.
Definition: set.cpp:169
void operator++(void)
Increment to next relation type.
Definition: set.hpp:95
void init(const Gecode::IntSet &s)
Initialize with set s.
Definition: set.cpp:59