sym-imp.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christopher Mears <chris.mears@monash.edu>
*
* Copyright:
* Christopher Mears, 2012
*
* This file is part of Gecode, the generic constraint
* development environment:
* http://www.gecode.org
*
* Permission is hereby granted, free of charge, to any person obtaining
* a copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be
* included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
*/
namespace Gecode { namespace Int { namespace LDSB {
template <class T, class A>
ArgArray<T>
dynamicStackToArgArray(const Support::DynamicStack<T,A>& s) {
ArgArray<T> a(s.entries());
for (int i = 0 ; i < s.entries() ; ++i) {
a[i] = s[i];
}
return a;
}
template<class View>
SymmetryImp<View>::~SymmetryImp(void) {}
template<class View>
void*
SymmetryImp<View>::operator new(size_t s, Space& home) {
return home.ralloc(s);
}
template<class View>
void
SymmetryImp<View>::operator delete(void*,Space&) {}
template<class View>
void
SymmetryImp<View>::operator delete(void*) {}
template <class View>
VariableSymmetryImp<View>
::VariableSymmetryImp(Space& home, int* _indices, unsigned int n)
: indices(home, 0, 0) {
// Find minimum and maximum value in _indices: the minimum is the
// offset, and the maximum dictates how large the bitset needs to
// be.
int maximum = _indices[0];
int minimum = _indices[0];
for (unsigned int i = 1 ; i < n ; i++) {
if (_indices[i] > maximum) maximum = _indices[i];
if (_indices[i] < minimum) minimum = _indices[i];
}
indices.resize(home, maximum-minimum+1, minimum);
// Set the bits for the included indices.
for (unsigned int i = 0 ; i < n ; i++) {
indices.set(_indices[i]);
}
}
template <class View>
inline
VariableSymmetryImp<View>
::VariableSymmetryImp(Space& home, const VariableSymmetryImp& other) :
indices(home, other.indices) {}
template <class View>
size_t
VariableSymmetryImp<View>
::dispose(Space& home) {
indices.dispose(home);
return sizeof(*this);
}
template <class View>
void
VariableSymmetryImp<View>
::update(Literal l) {
if (indices.valid(l._variable)) {
indices.clear(l._variable);
}
}
template <class View>
SymmetryImp<View>*
VariableSymmetryImp<View>::copy(Space& home) const {
return new (home) VariableSymmetryImp<View>(home, *this);
}
// The minimum value in vs is the bitset's offset, and the maximum
// dictates how large the bitset needs to be.
template <class View>
ValueSymmetryImp<View>
::ValueSymmetryImp(Space& home, int* vs, unsigned int n)
: values(home, 0, 0) {
// Find minimum and maximum value in vs: the minimum is the
// offset, and the maximum dictates how large the bitset needs to
// be.
assert(n > 0);
int maximum = vs[0];
int minimum = vs[0];
for (unsigned int i = 1 ; i < n ; i++) {
if (vs[i] > maximum) maximum = vs[i];
if (vs[i] < minimum) minimum = vs[i];
}
values.resize(home, maximum-minimum+1, minimum);
// Set the bits for the included values.
for (unsigned int i = 0 ; i < n ; i++) {
values.set(vs[i]);
}
}
template <class View>
ValueSymmetryImp<View>
::ValueSymmetryImp(Space& home, const ValueSymmetryImp<View>& other)
: values(home, other.values) { }
template <class View>
size_t
ValueSymmetryImp<View>
::dispose(Space& home) {
values.dispose(home);
return sizeof(*this);
}
template <class View>
void
ValueSymmetryImp<View>
::update(Literal l) {
if (values.valid(l._value))
values.clear(l._value);
}
template <class View>
SymmetryImp<View>*
ValueSymmetryImp<View>::copy(Space& home) const {
return new (home) ValueSymmetryImp(home, *this);
}
template <class View>
int
VariableSequenceSymmetryImp<View>
::getVal(unsigned int sequence, unsigned int position) const {
return indices[sequence*seq_size + position];
}
template <class View>
VariableSequenceSymmetryImp<View>
::VariableSequenceSymmetryImp(Space& home, int* _indices, unsigned int n,
unsigned int seqsize)
: n_indices(n), seq_size(seqsize), n_seqs(n/seqsize) {
indices = home.alloc<unsigned int>(n_indices);
unsigned int max_index = _indices[0];
for (unsigned int i = 0 ; i < n_indices ; i++) {
indices[i] = _indices[i];
if (indices[i] > max_index)
max_index = indices[i];
}
lookup_size = max_index+1;
lookup = home.alloc<int>(lookup_size);
for (unsigned int i = 0 ; i < lookup_size ; i++)
lookup[i] = -1;
for (unsigned int i = 0 ; i < n_indices ; i++) {
if (lookup[indices[i]] == -1)
lookup[indices[i]] = i;
}
}
template <class View>
VariableSequenceSymmetryImp<View>
::VariableSequenceSymmetryImp(Space& home,
const VariableSequenceSymmetryImp& s)
: n_indices(s.n_indices), seq_size(s.seq_size), n_seqs(s.n_seqs),
lookup_size(s.lookup_size) {
indices = home.alloc<unsigned int>(n_indices);
memcpy(indices, s.indices, n_indices * sizeof(int));
lookup = home.alloc<int>(lookup_size);
memcpy(lookup, s.lookup, lookup_size * sizeof(int));
}
template <class View>
size_t
VariableSequenceSymmetryImp<View>
::dispose(Space& home) {
home.free<unsigned int>(indices, n_indices);
home.free<int>(lookup, lookup_size);
return sizeof(*this);
}
template <class View>
ArgArray<Literal>
VariableSequenceSymmetryImp<View>
::symmetric(Literal l, const ViewArray<View>& x) const {
Region region;
Support::DynamicStack<Literal,Region> s(region);
if (l._variable < (int)lookup_size) {
int posIt = lookup[l._variable];
if (posIt == -1) {
return dynamicStackToArgArray(s);
}
unsigned int seqNum = posIt / seq_size;
unsigned int seqPos = posIt % seq_size;
for (unsigned int seq = 0 ; seq < n_seqs ; seq++) {
if (seq == seqNum) {
continue;
}
if (x[getVal(seq, seqPos)].assigned()) {
continue;
}
bool active = true;
const unsigned int *firstSeq = &indices[seqNum*seq_size];
const unsigned int *secondSeq = &indices[seq*seq_size];
for (unsigned int i = 0 ; i < seq_size ; i++) {
const View& xv = x[firstSeq[i]];
const View& yv = x[secondSeq[i]];
if ((!xv.assigned() && !yv.assigned())
|| (xv.assigned() && yv.assigned() && xv.val() == yv.val())) {
continue;
} else {
active = false;
break;
}
}
if (active) {
s.push(Literal(secondSeq[seqPos], l._value));
}
}
}
return dynamicStackToArgArray(s);
}
template <class View>
void
VariableSequenceSymmetryImp<View>
::update(Literal l) {
// Do nothing.
(void) l;
}
template <class View>
SymmetryImp<View>*
VariableSequenceSymmetryImp<View>
::copy(Space& home) const {
return new (home) VariableSequenceSymmetryImp<View>(home, *this);
}
template <class View>
int
ValueSequenceSymmetryImp<View>
::getVal(unsigned int sequence, unsigned int position) const {
return values[sequence*seq_size + position];
}
template <class View>
ValueSequenceSymmetryImp<View>
::ValueSequenceSymmetryImp(Space& home, int* _values, unsigned int n,
unsigned int seqsize)
: n_values(n), seq_size(seqsize), n_seqs(n/seqsize),
dead_sequences(home, n_seqs) {
values = home.alloc<int>(n_values);
for (unsigned int i = 0 ; i < n_values ; i++)
values[i] = _values[i];
}
template <class View>
ValueSequenceSymmetryImp<View>
::ValueSequenceSymmetryImp(Space& home,
const ValueSequenceSymmetryImp<View>& vss)
: n_values(vss.n_values),
seq_size(vss.seq_size),
n_seqs(vss.n_seqs),
dead_sequences(home, vss.dead_sequences) {
values = home.alloc<int>(n_values);
for (unsigned int i = 0 ; i < n_values ; i++)
values[i] = vss.values[i];
}
template <class View>
size_t
ValueSequenceSymmetryImp<View>
::dispose(Space& home) {
home.free(values, n_values);
return sizeof(*this);
}
template <class View>
void
ValueSequenceSymmetryImp<View>
::update(Literal l) {
unsigned int seq = 0;
unsigned int pos = 0;
for (unsigned int i = 0 ; i < n_values ; i++) {
if (values[i] == l._value) {
dead_sequences.set(seq);
// TODO: This can be slightly optimised.
while (pos < seq_size) {
i++;
pos++;
}
}
pos++;
if (pos == seq_size) {
pos = 0;
seq++;
}
}
}
template <class View>
SymmetryImp<View>*
ValueSequenceSymmetryImp<View>
::copy(Space& home) const {
return new (home) ValueSequenceSymmetryImp<View>(home, *this);
}
}}}
// STATISTICS: int-branch
Gecode::ArgArray
Argument array for non-primitive types.
Definition array.hpp:691
Gecode::Int::LDSB::Literal
A Literal is a pair of variable index and value.
Definition ldsb.hh:46
Gecode::Int::LDSB::Literal::_variable
int _variable
Variable index. The ViewArray that the index is meant for is assumed to be known by context.
Definition ldsb.hh:55
Gecode::Int::LDSB::Literal::_value
int _value
The value of the literal. For int and bool variables, this is the value itself; for set variables,...
Definition ldsb.hh:59
Gecode::Int::LDSB::SymmetryImp
Implementation of a single symmetry.
Definition ldsb.hh:162
Gecode::Int::LDSB::SymmetryImp::~SymmetryImp
virtual ~SymmetryImp(void)
Unused destructor.
Definition sym-imp.hpp:48
Gecode::Int::LDSB::ValueSequenceSymmetryImp
Implementation of a value sequence symmetry.
Definition ldsb.hh:266
Gecode::Int::LDSB::ValueSequenceSymmetryImp::n_seqs
unsigned int n_seqs
Number of sequences in symmetry.
Definition ldsb.hh:275
Gecode::Int::LDSB::ValueSequenceSymmetryImp::values
int * values
Set of sequences.
Definition ldsb.hh:269
Gecode::Int::LDSB::ValueSequenceSymmetryImp::seq_size
unsigned int seq_size
Size of each sequence in symmetry.
Definition ldsb.hh:273
Gecode::Int::LDSB::ValueSequenceSymmetryImp::dead_sequences
Support::BitSet< Space > dead_sequences
Which sequences are dead.
Definition ldsb.hh:277
Gecode::Int::LDSB::ValueSequenceSymmetryImp::n_values
unsigned int n_values
Total number of values (n_seqs * seq_size).
Definition ldsb.hh:271
Gecode::Int::LDSB::ValueSymmetryImp
Implementation of a value symmetry.
Definition ldsb.hh:204
Gecode::Int::LDSB::ValueSymmetryImp::copy
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition sym-imp.hpp:165
Gecode::Int::LDSB::ValueSymmetryImp::ValueSymmetryImp
ValueSymmetryImp(Space &home, int *vs, unsigned int n)
Constructor for creation.
Definition sym-imp.hpp:122
Gecode::Int::LDSB::ValueSymmetryImp::values
Support::BitSetOffset< Space > values
Symmetric values.
Definition ldsb.hh:207
Gecode::Int::LDSB::VariableSequenceSymmetryImp
Implementation of a variable sequence symmetry.
Definition ldsb.hh:224
Gecode::Int::LDSB::VariableSequenceSymmetryImp::VariableSequenceSymmetryImp
VariableSequenceSymmetryImp(Space &home, int *_indices, unsigned int n, unsigned int seqsize)
Constructor for creation.
Definition sym-imp.hpp:180
Gecode::Int::LDSB::VariableSequenceSymmetryImp::indices
unsigned int * indices
Array of variable indices.
Definition ldsb.hh:227
Gecode::Int::LDSB::VariableSequenceSymmetryImp::getVal
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:174
Gecode::Int::LDSB::VariableSequenceSymmetryImp::seq_size
unsigned int seq_size
Size of each sequence in symmetry.
Definition ldsb.hh:231
Gecode::Int::LDSB::VariableSequenceSymmetryImp::lookup
int * lookup
Map from variable's index to its sequence and position.
Definition ldsb.hh:242
Gecode::Int::LDSB::VariableSequenceSymmetryImp::lookup_size
unsigned int lookup_size
Size of lookup.
Definition ldsb.hh:244
Gecode::Int::LDSB::VariableSequenceSymmetryImp::n_indices
unsigned int n_indices
Total number of indices (n_seqs * seq_size).
Definition ldsb.hh:229
Gecode::Int::LDSB::VariableSequenceSymmetryImp::n_seqs
unsigned int n_seqs
Number of sequences in symmetry.
Definition ldsb.hh:233
Gecode::Int::LDSB::VariableSymmetryImp
Implementation of a variable symmetry.
Definition ldsb.hh:183
Gecode::Int::LDSB::VariableSymmetryImp::VariableSymmetryImp
VariableSymmetryImp(Space &home, int *vs, unsigned int n)
Constructor for creation.
Definition sym-imp.hpp:66
Gecode::Int::LDSB::VariableSymmetryImp::copy
SymmetryImp< View > * copy(Space &home) const
Copy function.
Definition sym-imp.hpp:112
Gecode::Int::LDSB::VariableSymmetryImp::indices
Support::BitSetOffset< Space > indices
Symmetric variable indices.
Definition ldsb.hh:186
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::alloc
T * alloc(long unsigned int n)
Allocate block of n objects of type T from space heap.
Definition core.hpp:2837
Gecode::Space::free
void free(T *b, long unsigned int n)
Delete n objects allocated from space heap starting at b.
Definition core.hpp:2863
Gecode::Support::DynamicStack
Stack with arbitrary number of elements.
Definition dynamic-stack.hpp:42
Gecode::Support::DynamicStack::push
void push(const T &x)
Push element x on top of stack.
Definition dynamic-stack.hpp:140
Gecode::Support::DynamicStack::entries
int entries(void) const
Return number of entries currently on stack.
Definition dynamic-stack.hpp:154
Gecode::View
Gecode::ValSelCommitBase::val
virtual Val val(const Space &home, View x, int i)=0
Return value of view x at position i.
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::Int::LDSB
Symmetry breaking for integer variables.
Definition int.hh:5246
Gecode::Int::LDSB::dynamicStackToArgArray
ArgArray< T > dynamicStackToArgArray(const Support::DynamicStack< T, A > &s)
Convert a DynamicStack<T,A> into an ArgArray<T>.
Definition sym-imp.hpp:39
Gecode::Int
Finite domain integers.
Gecode
Gecode toplevel namespace
Gecode::sequence
void sequence(Home home, const IntVarArgs &x, const IntSet &s, int q, int l, int u, IntPropLevel ipl=IPL_DEF)
Post propagator for .
Definition sequence.cpp:47