sym-imp.cpp
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.
*
*/
#include <gecode/set/ldsb.hh>
namespace Gecode { namespace Set { namespace LDSB {
// XXX: implementation of equalValues could be improved?
bool
equalLUB(const Set::SetView& x, const Set::SetView& y) {
unsigned int n = x.lubSize();
if (n != y.lubSize()) return false;
for (unsigned int i = 0 ; i < n ; i++)
if (x.lubMinN(i) != y.lubMinN(i))
return false;
return true;
}
}}}
// Note carefully that this is in the Gecode::Int::LDSB namespace, not
// Gecode::Set::LDSB.
namespace Gecode { namespace Int { namespace LDSB {
template <>
ArgArray<Literal>
VariableSymmetryImp<Set::SetView>
::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
(void) x;
if (indices.valid(l._variable) && indices.get(l._variable)) {
int n = 0;
for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(indices) ; i() ; ++i)
n++;
ArgArray<Literal> lits(n);
int j = 0;
for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(indices) ; i() ; ++i) {
lits[j++] = Literal(i.val(), l._value);
}
return lits;
} else {
return ArgArray<Literal>(0);
}
}
template <>
ArgArray<Literal>
ValueSymmetryImp<Set::SetView>
::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
(void) x;
if (values.valid(l._value) && values.get(l._value)) {
int n = 0;
for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(values) ; i() ; ++i)
n++;
ArgArray<Literal> lits(n);
int j = 0;
for (Iter::Values::BitSetOffset<Support::BitSetOffset<Space> > i(values) ; i() ; ++i) {
lits[j++] = Literal(l._variable, i.val());
}
return lits;
} else {
return ArgArray<Literal>(0);
}
}
template <>
ArgArray<Literal>
VariableSequenceSymmetryImp<Set::SetView>
::symmetric(Literal l, const ViewArray<Set::SetView>& 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 Set::SetView& xv = x[firstSeq[i]];
const Set::SetView& yv = x[secondSeq[i]];
if ((!xv.assigned() && !yv.assigned())
|| (xv.assigned() && yv.assigned() && Set::LDSB::equalLUB(xv, yv))) {
continue;
} else {
active = false;
break;
}
}
if (active) {
s.push(Literal(secondSeq[seqPos], l._value));
}
}
}
return dynamicStackToArgArray(s);
}
template <>
ArgArray<Literal>
ValueSequenceSymmetryImp<Set::SetView>
::symmetric(Literal l, const ViewArray<Set::SetView>& x) const {
(void) x;
Region region;
Support::DynamicStack<Literal,Region> s(region);
std::pair<int,int> location = findVar(values, n_values, seq_size, l._value);
if (location.first == -1) return dynamicStackToArgArray(s);
unsigned int seqNum = location.first;
unsigned int seqPos = location.second;
for (unsigned int seq = 0 ; seq < n_seqs ; seq++) {
if (seq == seqNum) continue;
if (dead_sequences.get(seq)) continue;
s.push(Literal(l._variable, getVal(seq,seqPos)));
}
return dynamicStackToArgArray(s);
}
}}}
// STATISTICS: set-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::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::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:287
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< Gecode::ValSelCommitBase< typenameValSel::View, typenameValSel::Val >::View >::values
Support::BitSetOffset< Space > values
Definition ldsb.hh:207
Gecode::Int::LDSB::VariableSequenceSymmetryImp
Implementation of a variable sequence symmetry.
Definition ldsb.hh:224
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_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::indices
Support::BitSetOffset< Space > indices
Symmetric variable indices.
Definition ldsb.hh:186
Gecode::Iter::Values::BitSetOffset
Value iterator for values in an offset bitset.
Definition values-bitsetoffset.hpp:45
Gecode::Region
Handle to region.
Definition region.hpp:55
Gecode::Set::SetView
Set view for set variables
Definition view.hpp:56
Gecode::Set::SetView::lubSize
unsigned int lubSize(void) const
Return the number of elements in the least upper bound.
Definition set.hpp:66
Gecode::Set::SetView::lubMinN
int lubMinN(unsigned int n) const
Return n-th smallest element of the least upper bound.
Definition set.hpp:98
Gecode::Support::BitSetOffset
Bitsets with index offset.
Definition bitset-offset.hpp:51
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::VarImpView::assigned
bool assigned(void) const
Test whether view is assigned.
Definition view.hpp:516
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::Int::LDSB
Symmetry breaking for integer variables.
Definition int.hh:5246
Gecode::Int::LDSB::findVar
std::pair< int, int > findVar(int *indices, unsigned int n_values, unsigned int seq_size, int index)
Find the location of an integer in a collection of sequences.
Definition ldsb.cpp:42
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::Set::LDSB
Symmetry breaking for set variables.
Definition ldsb.cpp:65
Gecode::Set::LDSB::equalLUB
bool equalLUB(const Set::SetView &x, const Set::SetView &y)
Do two set variables have equal least-upper-bounds?
Definition sym-imp.cpp:41
Gecode::Set
Finite integer sets.
Definition var-imp.hpp:137
Gecode
Gecode toplevel namespace
ldsb.hh