brancher.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 Set { namespace LDSB {
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
LDSBSetBrancher<View,n,Val,a,Filter,Print>
::LDSBSetBrancher(Home home, ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
SymmetryImp<View>** syms, int nsyms,
BranchFilter<LDSBSetBrancher<View,n,Val,a,Filter,Print>::Var> bf,
VarValPrint<LDSBSetBrancher<View,n,Val,a,Filter,Print>::Var,Val> vvp)
: LDSBBrancher<View,n,Val,a,Filter,Print>
(home, x, vs, vsc, syms, nsyms, bf, vvp),
_prevPos(-1),
_copiedSyms(NULL),
_nCopiedSyms(0),
_stable(false) {
// Put all the value symmetries at the end of the list.
int seen = 0;
int dest = this->_nsyms - 1;
for (int i = 0 ; i < this->_nsyms - seen ; i++) {
if (dynamic_cast<ValueSymmetryImp<View>*>(this->_syms[i])) {
SymmetryImp<View>* t = this->_syms[i];
this->_syms[i] = this->_syms[dest];
this->_syms[dest] = t;
dest--;
seen++;
}
}
_nValueSymmetries = seen;
_nNonValueSymmetries = this->_nsyms - seen;
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
LDSBSetBrancher<View,n,Val,a,Filter,Print>::
LDSBSetBrancher(Space& home,
LDSBSetBrancher<View,n,Val,a,Filter,Print>& b)
: LDSBBrancher<View,n,Val,a,Filter,Print>(home,b),
_prevPos(b._prevPos),
_nNonValueSymmetries(b._nNonValueSymmetries),
_nValueSymmetries(b._nValueSymmetries),
_nCopiedSyms(b._nCopiedSyms),
_leftBranchValues(b._leftBranchValues),
_stable(b._stable) {
if (_nCopiedSyms > 0) {
_copiedSyms = home.alloc<ValueSymmetryImp<View>*>(_nCopiedSyms);
for (int i = 0 ; i < _nCopiedSyms ; i++)
_copiedSyms[i] = static_cast<ValueSymmetryImp<View>*>(
b._copiedSyms[i]->copy(home));
} else {
_copiedSyms = NULL;
}
}
template <class View>
ValueSymmetryImp<View>*
specialUpdate(Space& home, ValueSymmetryImp<View>* s, IntSet usedValues) {
// Calculate intersection and difference.
IntArgs intersection;
IntArgs difference;
int n = 0;
for (int i = s->values.next(s->values.offset()) ;
i <= s->values.max_bit() ; i = s->values.next(i+1)) {
n++;
if (usedValues.in(i))
intersection << i;
else
difference << i;
}
for (IntSetValues v(usedValues) ; v() ; ++v) {
s->update(Literal(0, v.val()));
}
if (intersection.size() < 2)
return NULL;
int *a = new int[intersection.size()];
for (int i = 0 ; i < intersection.size() ; i++) {
a[i] = intersection[i];
}
ValueSymmetryImp<View>* ns =
new (home) ValueSymmetryImp<View>(home, a, intersection.size());
delete [] a;
return ns;
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
void
LDSBSetBrancher<View,n,Val,a,Filter,Print>::
updatePart1(Space& home, int choicePos) {
if (_nValueSymmetries > 0) {
// If this is a different variable from the last commit, restore
// the old value symmetries and update the copy.
if (choicePos != _prevPos) {
if (_prevPos != -1) {
int i = 0;
for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
this->_syms[j] = _copiedSyms[i];
i++;
}
for (int i = 0 ; i < _nCopiedSyms ; i++) {
ValueSymmetryImp<View>* ns =
specialUpdate(home, _copiedSyms[i], _leftBranchValues);
if (ns) {
this->_syms = home.realloc<SymmetryImp<View>*>(this->_syms,
this->_nsyms,
this->_nsyms+1);
this->_syms[this->_nsyms] = ns;
this->_nsyms++;
this->_nValueSymmetries++;
}
}
}
// Reset for current variable, make copy of value symmetries
_leftBranchValues = IntSet::empty;
_prevPos = choicePos;
if (_nCopiedSyms > 0) home.free(_copiedSyms, _nCopiedSyms);
_nCopiedSyms = _nValueSymmetries;
_copiedSyms = home.alloc<ValueSymmetryImp<View>*>(_nCopiedSyms);
{
int i = 0;
for (int j = _nNonValueSymmetries ; j < this->_nsyms ; j++) {
ValueSymmetryImp<View>* vsi =
static_cast<ValueSymmetryImp<View>*>(this->_syms[j]);
_copiedSyms[i] =
static_cast<ValueSymmetryImp<View>*>(vsi->copy(home));
i++;
}
}
}
}
}
// Compute choice
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
const Choice*
LDSBSetBrancher<View,n,Val,a,Filter,Print>::choice(Space& home) {
// Making the PVC here is not so nice, I think.
const Choice* c = ViewValBrancher<View,n,Val,a,Filter,Print>::choice(home);
const PosValChoice<Val>* pvc = static_cast<const PosValChoice<Val>* >(c);
// Compute symmetries.
int choicePos = pvc->pos().pos;
delete c;
assert(!_stable);
updatePart1(home, choicePos);
return LDSBBrancher<View,n,Val,a,Filter,Print>::choice(home);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
ExecStatus
LDSBSetBrancher<View,n,Val,a,Filter,Print>
::commit(Space& home, const Choice& c, unsigned int b) {
const LDSBChoice<Val>& pvc
= static_cast<const LDSBChoice<Val>&>(c);
int choicePos = pvc.pos().pos;
int choiceVal = pvc.val();
if (!_stable)
updatePart1(home, choicePos);
if (b == 0) {
IntArgs ia;
for (IntSetValues v(_leftBranchValues) ; v() ; ++v) {
ia << v.val();
}
ia << choiceVal;
_leftBranchValues = IntSet(ia);
// Post the branching constraint.
ExecStatus fromBase = ViewValBrancher<View,n,Val,a,Filter,Print>
::commit(home, c, b);
GECODE_ES_CHECK(fromBase);
for (int i = 0 ; i < this->_nsyms ; i++)
this->_syms[i]->update(Literal(choicePos, choiceVal));
} else if (b == 1) {
// Post the branching constraint.
ExecStatus fromBase = ViewValBrancher<View,n,Val,a,Filter,Print>
::commit(home, c, b);
GECODE_ES_CHECK(fromBase);
// Post prunings.
int nliterals = pvc.nliterals();
const Literal* literals = pvc.literals();
for (int i = 0 ; i < nliterals ; i++) {
const Literal& l = literals[i];
ModEvent me = prune<View>(home, this->x[l._variable], l._value);
GECODE_ME_CHECK(me);
}
}
return ES_OK;
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
Actor*
LDSBSetBrancher<View,n,Val,a,Filter,Print>::copy(Space& home) {
return new (home) LDSBSetBrancher<View,n,Val,a,Filter,Print>
(home,*this);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline void
LDSBSetBrancher<View,n,Val,a,Filter,Print>::
post(Home home, ViewArray<View>& x,
ViewSel<View>* vs[n], ValSelCommitBase<View,Val>* vsc,
SymmetryImp<View>** syms, int nsyms,
BranchFilter<LDSBSetBrancher<View,n,Val,a,Filter,Print>::Var> bf,
VarValPrint<LDSBSetBrancher<View,n,Val,a,Filter,Print>::Var,Val> vvp) {
(void) new (home) LDSBSetBrancher<View,n,Val,a,Filter,Print>
(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
template<class View, int n, class Val, unsigned int a>
forceinline void
postldsbsetbrancher(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
SymmetryImp<View>** syms, int nsyms,
BranchFilter<typename View::VarType> bf,
VarValPrint<typename View::VarType,Val> vvp) {
if (bf) {
if (vvp) {
LDSBSetBrancher<View,n,Val,a,BrancherFilter<View>,BrancherPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
} else {
LDSBSetBrancher<View,n,Val,a,BrancherFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
} else {
if (vvp) {
LDSBSetBrancher<View,n,Val,a,BrancherNoFilter<View>,BrancherPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
} else {
LDSBSetBrancher<View,n,Val,a,BrancherNoFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
}
}
}}}
// STATISTICS: set-branch
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::BrancherNoPrint
Class without print function.
Definition print.hpp:73
Gecode::BrancherPrint
Class storing a print function.
Definition print.hpp:47
Gecode::Brancher::Space
friend class Space
Definition core.hpp:1444
Gecode::Brancher::Choice
friend class Choice
Definition core.hpp:1445
Gecode::Choice
Choice for performing commit
Definition core.hpp:1412
Gecode::Home
Home class for posting propagators
Definition core.hpp:856
Gecode::IntArgs
Passing integer arguments.
Definition int.hh:628
Gecode::IntSetValues
Value iterator for integer sets.
Definition int.hh:333
Gecode::IntSet
Integer sets.
Definition int.hh:174
Gecode::IntSet::in
bool in(int n) const
Return whether n is included in the set.
Definition int-set-1.hpp:177
Gecode::IntSet::empty
static const IntSet empty
Empty set.
Definition int.hh:283
Gecode::Int::LDSB::LDSBBrancher
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:332
Gecode::Int::LDSB::LDSBBrancher::_nsyms
int _nsyms
Number of symmetry implementations.
Definition ldsb.hh:338
Gecode::Int::LDSB::LDSBBrancher::_syms
SymmetryImp< View > ** _syms
Array of symmetry implementations.
Definition ldsb.hh:336
Gecode::Int::LDSB::LDSBBrancher::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition brancher.hpp:148
Gecode::Int::LDSB::LDSBChoice
Choice storing position and value, and symmetric literals to be excluded on the right branch.
Definition ldsb.hh:301
Gecode::Int::LDSB::LDSBChoice::nliterals
int nliterals(void) const
Return number of literals.
Definition brancher.hpp:76
Gecode::Int::LDSB::LDSBChoice::literals
const Literal * literals(void) const
Return literals.
Definition brancher.hpp:72
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::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::values
Support::BitSetOffset< Space > values
Symmetric values.
Definition ldsb.hh:207
Gecode::Int::LDSB::ValueSymmetryImp::update
void update(Literal)
Left-branch update.
Definition sym-imp.hpp:158
Gecode::PosChoice::pos
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
Gecode::PosValChoice
Choice storing position and value
Definition view-val.hpp:46
Gecode::PosValChoice::val
const Val & val(void) const
Definition view-val.hpp:165
Gecode::Pos::pos
const int pos
Position of view.
Definition view.hpp:47
Gecode::Set::LDSB::LDSBSetBrancher
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:58
Gecode::Set::LDSB::LDSBSetBrancher::_stable
bool _stable
Is the state of the brancher "stable"?
Definition ldsb.hh:82
Gecode::Set::LDSB::LDSBSetBrancher::_nCopiedSyms
int _nCopiedSyms
Number of copied symmetries.
Definition ldsb.hh:71
Gecode::Set::LDSB::LDSBSetBrancher::_prevPos
int _prevPos
Position of previous variable that was branched on.
Definition ldsb.hh:62
Gecode::Set::LDSB::LDSBSetBrancher::_copiedSyms
ValueSymmetryImp< View > ** _copiedSyms
Copy of value symmetries from the first node where the current variable was branched on.
Definition ldsb.hh:69
Gecode::Set::LDSB::LDSBSetBrancher::copy
virtual Actor * copy(Space &home)
Perform cloning.
Definition brancher.hpp:249
Gecode::Set::LDSB::LDSBSetBrancher::_leftBranchValues
IntSet _leftBranchValues
Set of values used on left branches for the current variable.
Definition ldsb.hh:73
Gecode::Set::LDSB::LDSBSetBrancher::_nNonValueSymmetries
int _nNonValueSymmetries
Number of non-value symmetries.
Definition ldsb.hh:64
Gecode::Set::LDSB::LDSBSetBrancher::LDSBSetBrancher
LDSBSetBrancher(Space &home, LDSBSetBrancher &b)
Constructor for cloning b.
Gecode::Set::LDSB::LDSBSetBrancher::updatePart1
void updatePart1(Space &home, int choicePos)
Part one of the update phase.
Definition brancher.hpp:134
Gecode::Set::LDSB::LDSBSetBrancher::_nValueSymmetries
int _nValueSymmetries
Number of value symmetries.
Definition ldsb.hh:66
Gecode::Set::LDSB::LDSBSetBrancher::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition brancher.hpp:184
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Space::realloc
T * realloc(T *b, long unsigned int n, long unsigned int m)
Reallocate block of n objects starting at b to m objects of type T from the space heap.
Definition core.hpp:2888
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::Val
Gecode::View
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewBrancher::x
ViewArray< View > x
Views to branch on.
Definition view.hpp:83
Gecode::ViewBrancher::vs
ViewSel< View > * vs[n]
View selection objects.
Definition view.hpp:87
Gecode::ViewSel
Abstract class for view selection.
Definition view-sel.hpp:44
Gecode::ViewValBrancher::choice
virtual const Choice * choice(Space &home)
Return choice.
Definition view-val.hpp:271
Gecode::ViewValBrancher::Var
View::VarType Var
The corresponding variable.
Definition view-val.hpp:97
Gecode::ViewValBrancher::ViewValBrancher
ViewValBrancher(Space &home, ViewValBrancher &b)
Constructor for cloning b.
Gecode::ViewValBrancher::vsc
ValSelCommitBase< View, Val > * vsc
Value selection and commit object.
Definition view-val.hpp:99
Gecode::Int::OffsetView::update
void update(const NoOffset &)
Integer-precision integer scale view.
Definition view.hpp:638
GECODE_ME_CHECK
#define GECODE_ME_CHECK(me)
Check whether modification event me is failed, and forward failure.
Definition macros.hpp:52
GECODE_ES_CHECK
#define GECODE_ES_CHECK(es)
Check whether execution status es is failed or subsumed, and forward failure or subsumption.
Definition macros.hpp:91
Gecode::Int::LDSB::prune
ModEvent prune(Space &home, View x, int v)
Exclude value \v from variable view \x.
Gecode::Set::LDSB
Symmetry breaking for set variables.
Definition ldsb.cpp:65
Gecode::Set::LDSB::postldsbsetbrancher
void postldsbsetbrancher(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **syms, int nsyms, BranchFilter< typename View::VarType > bf, VarValPrint< typename View::VarType, Val > vvp)
Definition brancher.hpp:269
Gecode::Set::LDSB::specialUpdate
ValueSymmetryImp< View > * specialUpdate(Space &home, ValueSymmetryImp< View > *s, IntSet usedValues)
Bulk update of a value symmetry s, using usedValues.
Definition brancher.hpp:100
Gecode::Set
Finite integer sets.
Definition var-imp.hpp:137
Gecode
Gecode toplevel namespace
Gecode::VarValPrint
std::function< void(const Space &home, const Brancher &b, unsigned int a, Var x, int i, const Val &m, std::ostream &o)> VarValPrint
Function type for printing variable and value selection.
Definition print.hpp:40
Gecode::values
void values(Home home, const IntVarArgs &x, IntSet y, IntPropLevel ipl=IPL_DEF)
Post constraint .
Definition aliases.hpp:143
Gecode::post
TFE post(PropagatorGroup g)
Only post functions (but not propagators) from g are considered.
Definition filter.cpp:138
Gecode::ExecStatus
ExecStatus
Definition core.hpp:472
Gecode::ES_OK
@ ES_OK
Execution is okay.
Definition core.hpp:476
Gecode::BranchFilter
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
Gecode::ModEvent
int ModEvent
Type for modification events.
Definition core.hpp:62
Test::Float::Arithmetic::b
Gecode::FloatVal b(9, 12)
Test::Float::Arithmetic::a
Gecode::FloatVal a(-8, 5)
Test::Int::Basic::i
Gecode::IntArgs i({1, 2, 3, 4})
forceinline
#define forceinline
Definition config.hpp:187