Generated on Thu Apr 5 2018 19:44:19 for Gecode by doxygen 1.8.13
sym-imp.hpp
Go to the documentation of this file.
1 /* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
2 /*
3  * Main authors:
4  * Christopher Mears <chris.mears@monash.edu>
5  *
6  * Copyright:
7  * Christopher Mears, 2012
8  *
9  * Last modified:
10  * $Date$ by $Author$
11  * $Revision$
12  *
13  * This file is part of Gecode, the generic constraint
14  * development environment:
15  * http://www.gecode.org
16  *
17  * Permission is hereby granted, free of charge, to any person obtaining
18  * a copy of this software and associated documentation files (the
19  * "Software"), to deal in the Software without restriction, including
20  * without limitation the rights to use, copy, modify, merge, publish,
21  * distribute, sublicense, and/or sell copies of the Software, and to
22  * permit persons to whom the Software is furnished to do so, subject to
23  * the following conditions:
24  *
25  * The above copyright notice and this permission notice shall be
26  * included in all copies or substantial portions of the Software.
27  *
28  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
29  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
30  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
31  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
32  * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
33  * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
34  * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35  *
36  */
37 
38 namespace Gecode { namespace Int { namespace LDSB {
39 
41  template <class T, class A>
42  ArgArray<T>
44  ArgArray<T> a(s.entries());
45  for (int i = 0 ; i < s.entries() ; ++i) {
46  a[i] = s[i];
47  }
48  return a;
49  }
50 
51  template<class View>
53 
54  template<class View>
55  void*
56  SymmetryImp<View>::operator new(size_t s, Space& home) {
57  return home.ralloc(s);
58  }
59 
60  template<class View>
61  void
63 
64  template<class View>
65  void
66  SymmetryImp<View>::operator delete(void*) {}
67 
68  template <class View>
70  ::VariableSymmetryImp(Space& home, int* _indices, unsigned int n)
71  : indices(home, 0, 0) {
72  // Find minimum and maximum value in _indices: the minimum is the
73  // offset, and the maximum dictates how large the bitset needs to
74  // be.
75  int maximum = _indices[0];
76  int minimum = _indices[0];
77  for (unsigned int i = 1 ; i < n ; i++) {
78  if (_indices[i] > maximum) maximum = _indices[i];
79  if (_indices[i] < minimum) minimum = _indices[i];
80  }
81  indices.resize(home, maximum-minimum+1, minimum);
82 
83  // Set the bits for the included indices.
84  for (unsigned int i = 0 ; i < n ; i++) {
85  indices.set(_indices[i]);
86  }
87  }
88 
89 
90 
91  template <class View>
92  inline
95  indices(home, other.indices) {}
96 
97  template <class View>
98  size_t
101  indices.dispose(home);
102  return sizeof(*this);
103  }
104 
105  template <class View>
106  void
109  if (indices.valid(l._variable)) {
110  indices.clear(l._variable);
111  }
112  }
113 
114  template <class View>
117  return new (home) VariableSymmetryImp<View>(home, *this);
118  }
119 
120 
121 
122  // The minimum value in vs is the bitset's offset, and the maximum
123  // dictates how large the bitset needs to be.
124  template <class View>
126  ::ValueSymmetryImp(Space& home, int* vs, unsigned int n)
127  : values(home, 0, 0) {
128  // Find minimum and maximum value in vs: the minimum is the
129  // offset, and the maximum dictates how large the bitset needs to
130  // be.
131  assert(n > 0);
132  int maximum = vs[0];
133  int minimum = vs[0];
134  for (unsigned int i = 1 ; i < n ; i++) {
135  if (vs[i] > maximum) maximum = vs[i];
136  if (vs[i] < minimum) minimum = vs[i];
137  }
138  values.resize(home, maximum-minimum+1, minimum);
139 
140  // Set the bits for the included values.
141  for (unsigned int i = 0 ; i < n ; i++) {
142  values.set(vs[i]);
143  }
144  }
145 
146  template <class View>
149  : values(home, other.values) { }
150 
151  template <class View>
152  size_t
155  values.dispose(home);
156  return sizeof(*this);
157  }
158 
159  template <class View>
160  void
163  if (values.valid(l._value))
164  values.clear(l._value);
165  }
166 
167  template <class View>
170  return new (home) ValueSymmetryImp(home, *this);
171  }
172 
173 
174 
175  template <class View>
176  int
178  ::getVal(unsigned int sequence, unsigned int position) const {
179  return indices[sequence*seq_size + position];
180  }
181 
182  template <class View>
184  ::VariableSequenceSymmetryImp(Space& home, int* _indices, unsigned int n,
185  unsigned int seqsize)
186  : n_indices(n), seq_size(seqsize), n_seqs(n/seqsize) {
187  indices = home.alloc<unsigned int>(n_indices);
188  unsigned int max_index = _indices[0];
189  for (unsigned int i = 0 ; i < n_indices ; i++) {
190  indices[i] = _indices[i];
191  if (indices[i] > max_index)
192  max_index = indices[i];
193  }
194 
195  lookup_size = max_index+1;
196  lookup = home.alloc<int>(lookup_size);
197  for (unsigned int i = 0 ; i < lookup_size ; i++)
198  lookup[i] = -1;
199  for (unsigned int i = 0 ; i < n_indices ; i++) {
200  if (lookup[indices[i]] == -1)
201  lookup[indices[i]] = i;
202  }
203  }
204 
205  template <class View>
209  : n_indices(s.n_indices), seq_size(s.seq_size), n_seqs(s.n_seqs),
210  lookup_size(s.lookup_size) {
211  indices = home.alloc<unsigned int>(n_indices);
212  memcpy(indices, s.indices, n_indices * sizeof(int));
213  lookup = home.alloc<int>(lookup_size);
214  memcpy(lookup, s.lookup, lookup_size * sizeof(int));
215  }
216 
217  template <class View>
218  size_t
221  home.free<unsigned int>(indices, n_indices);
222  home.free<int>(lookup, lookup_size);
223  return sizeof(*this);
224  }
225 
227  template <class View>
232  if (l._variable < (int)lookup_size) {
233  int posIt = lookup[l._variable];
234  if (posIt == -1) {
235  return dynamicStackToArgArray(s);
236  }
237  unsigned int seqNum = posIt / seq_size;
238  unsigned int seqPos = posIt % seq_size;
239  for (unsigned int seq = 0 ; seq < n_seqs ; seq++) {
240  if (seq == seqNum) {
241  continue;
242  }
243  if (x[getVal(seq, seqPos)].assigned()) {
244  continue;
245  }
246  bool active = true;
247  const unsigned int *firstSeq = &indices[seqNum*seq_size];
248  const unsigned int *secondSeq = &indices[seq*seq_size];
249  for (unsigned int i = 0 ; i < seq_size ; i++) {
250  const View& xv = x[firstSeq[i]];
251  const View& yv = x[secondSeq[i]];
252  if ((!xv.assigned() && !yv.assigned())
253  || (xv.assigned() && yv.assigned() && xv.val() == yv.val())) {
254  continue;
255  } else {
256  active = false;
257  break;
258  }
259  }
260 
261  if (active) {
262  s.push(Literal(secondSeq[seqPos], l._value));
263  }
264  }
265  }
266  return dynamicStackToArgArray(s);
267  }
268 
269 
270  template <class View>
271  void
274  // Do nothing.
275  (void) l;
276  }
277 
278  template <class View>
281  ::copy(Space& home) const {
282  return new (home) VariableSequenceSymmetryImp<View>(home, *this);
283  }
284 
285 
286 
287  template <class View>
288  int
290  ::getVal(unsigned int sequence, unsigned int position) const {
291  return values[sequence*seq_size + position];
292  }
293 
294  template <class View>
296  ::ValueSequenceSymmetryImp(Space& home, int* _values, unsigned int n,
297  unsigned int seqsize)
298  : n_values(n), seq_size(seqsize), n_seqs(n/seqsize),
299  dead_sequences(home, n_seqs) {
300  values = home.alloc<int>(n_values);
301  for (unsigned int i = 0 ; i < n_values ; i++)
302  values[i] = _values[i];
303  }
304 
305  template <class View>
309  : n_values(vss.n_values),
310  seq_size(vss.seq_size),
311  n_seqs(vss.n_seqs),
312  dead_sequences(home, vss.dead_sequences) {
313  values = home.alloc<int>(n_values);
314  for (unsigned int i = 0 ; i < n_values ; i++)
315  values[i] = vss.values[i];
316  }
317 
318  template <class View>
319  size_t
322  home.free(values, n_values);
323  return sizeof(*this);
324  }
325 
326  template <class View>
327  void
330  unsigned int seq = 0;
331  unsigned int pos = 0;
332  for (unsigned int i = 0 ; i < n_values ; i++) {
333  if (values[i] == l._value) {
334  dead_sequences.set(seq);
335  // TODO: This can be slightly optimised.
336  while (pos < seq_size) {
337  i++;
338  pos++;
339  }
340  }
341  pos++;
342  if (pos == seq_size) {
343  pos = 0;
344  seq++;
345  }
346  }
347  }
348 
349  template <class View>
352  ::copy(Space& home) const {
353  return new (home) ValueSequenceSymmetryImp<View>(home, *this);
354  }
355 
356 }}}
357 
358 // STATISTICS: int-branch
virtual size_t dispose(Space &home)
Disposal.
Definition: sym-imp.hpp:220
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition: sym-imp.hpp:281
NNF * l
Left subtree.
Definition: bool-expr.cpp:244
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntPropLevel)
Post propagator for .
Definition: sequence.cpp:51
A Literal is a pair of variable index and value.
Definition: ldsb.hh:50
ValueSymmetryImp(Space &home, int *vs, unsigned int n)
Constructor for creation.
Definition: sym-imp.hpp:126
virtual ~SymmetryImp(void)
Unused destructor.
Definition: sym-imp.hpp:52
VariableSymmetryImp(Space &home, int *vs, unsigned int n)
Constructor for creation.
Definition: sym-imp.hpp:70
bool pos(const View &x)
Test whether x is postive.
Definition: mult.hpp:45
Implementation of a variable sequence symmetry.
Definition: ldsb.hh:227
int * lookup
Map from variable&#39;s index to its sequence and position.
Definition: ldsb.hh:246
int _value
The value of the literal. For int and bool variables, this is the value itself; for set variables...
Definition: ldsb.hh:63
int _variable
Variable index. The ViewArray that the index is meant for is assumed to be known by context...
Definition: ldsb.hh:59
Computation spaces.
Definition: core.hpp:1668
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition: core.hpp:2757
Gecode::IntArgs i(4, 1, 2, 3, 4)
int n
Number of negative literals for node type.
Definition: bool-expr.cpp:238
Argument array for non-primitive types.
Definition: array.hpp:715
virtual size_t dispose(Space &home)
Disposal.
Definition: sym-imp.hpp:321
unsigned int * indices
Array of variable indices.
Definition: ldsb.hh:231
Implementation of a value symmetry.
Definition: ldsb.hh:207
int getVal(unsigned int sequence, unsigned int position) const
Get the value in the specified sequence at the specified position. (Both are zero-based.)
Definition: sym-imp.hpp:178
virtual size_t dispose(Space &home)
Disposal.
Definition: sym-imp.hpp:154
View arrays.
Definition: array.hpp:228
void free(T *b, long unsigned int n)
Delete n objects allocated from space heap starting at b.
Definition: core.hpp:2783
double position(const Space &home, IntVar x, int i)
Definition: ldsb.cpp:1269
Implementation of a single symmetry.
Definition: ldsb.hh:166
void update(Literal)
Left-branch update.
Definition: sym-imp.hpp:162
struct Gecode::@585::NNF::@62::@64 a
For atomic nodes.
Heap heap
The single global heap.
Definition: heap.cpp:48
bool assigned(View x, int v)
Whether x is assigned to value v.
Definition: single.hpp:47
Implementation of a variable symmetry.
Definition: ldsb.hh:187
Stack with arbitrary number of elements.
void update(Literal)
Left-branch update.
Definition: sym-imp.hpp:108
Implementation of a value sequence symmetry.
Definition: ldsb.hh:269
virtual ArgArray< Literal > symmetric(Literal, const ViewArray< View > &) const
Compute symmetric literals.
Definition: sym-imp.hpp:230
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition: minimodel.hh:1997
Post propagator for SetVar x
Definition: set.hh:769
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition: sym-imp.hpp:116
void update(Literal)
Search left-branch update.
Definition: sym-imp.hpp:273
Gecode toplevel namespace
ArgArray< T > dynamicStackToArgArray(const Support::DynamicStack< T, A > &s)
Convert a DynamicStack<T,A> into an ArgArray<T>
Definition: sym-imp.hpp:43
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition: sym-imp.hpp:169
virtual size_t dispose(Space &home)
Disposal.
Definition: sym-imp.hpp:100
int getVal(unsigned int sequence, unsigned int position) const
Get the value in the specified sequence at the specified position. (Both are zero-based.)
Definition: sym-imp.hpp:290
int entries(void) const
Return number of entries currently on stack.
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition: sym-imp.hpp:352
void update(Literal)
Left-branch update.
Definition: sym-imp.hpp:329
void push(const T &x)
Push element x on top of stack.
VariableSequenceSymmetryImp(Space &home, int *_indices, unsigned int n, unsigned int seqsize)
Constructor for creation.
Definition: sym-imp.hpp:184
int * values
Set of sequences.
Definition: ldsb.hh:273