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.
*
*/
#include <deque>
#include <set>
namespace Gecode { namespace Int { namespace LDSB {
forceinline
Literal::Literal(void)
: _variable(-1), _value(-1) {}
forceinline
Literal::Literal(int idx, int val)
: _variable(idx), _value(val) {}
forceinline
bool
Literal::operator <(const Literal &rhs) const {
int d = rhs._variable - _variable;
if (d > 0) return true;
else if (d == 0) return rhs._value > _value;
else return false;
}
template<class Val>
inline
LDSBChoice<Val>::LDSBChoice(const Brancher& b, unsigned int a, const Pos& p,
const Val& n, const Literal* literals,
int nliterals)
: PosValChoice<Val>(b,a,p,n), _literals(literals), _nliterals(nliterals)
{}
template<class Val>
LDSBChoice<Val>::~LDSBChoice(void) {
delete [] _literals;
}
template<class Val>
forceinline const Literal*
LDSBChoice<Val>::literals(void) const { return _literals; }
template<class Val>
forceinline int
LDSBChoice<Val>::nliterals(void) const { return _nliterals; }
template<class Val>
void
LDSBChoice<Val>::archive(Archive& e) const {
PosValChoice<Val>::archive(e);
e << _nliterals;
for (int i = 0 ; i < _nliterals ; i++) {
e << _literals[i]._variable;
e << _literals[i]._value;
}
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
LDSBBrancher<View,n,Val,a,Filter,Print>
::LDSBBrancher(Home home, ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
SymmetryImp<View>** syms, int nsyms,
BranchFilter<LDSBBrancher<View,n,Val,a,Filter,Print>::Var> bf,
VarValPrint<LDSBBrancher<View,n,Val,a,Filter,Print>::Var,Val> vvp)
: ViewValBrancher<View,n,Val,a,Filter,Print>(home, x, vs, vsc, bf, vvp),
_syms(syms),
_nsyms(nsyms),
_prevPos(-1)
{
home.notice(*this, AP_DISPOSE, true);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline void
LDSBBrancher<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<LDSBBrancher<View,n,Val,a,Filter,Print>::Var> bf,
VarValPrint<LDSBBrancher<View,n,Val,a,Filter,Print>::Var,Val> vvp) {
(void) new (home) LDSBBrancher<View,n,Val,a,Filter,Print>
(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline
LDSBBrancher<View,n,Val,a,Filter,Print>::
LDSBBrancher(Space& home,
LDSBBrancher<View,n,Val,a,Filter,Print>& b)
: ViewValBrancher<View,n,Val,a,Filter,Print>(home,b),
_nsyms(b._nsyms),
_prevPos(b._prevPos) {
_syms = home.alloc<SymmetryImp<View>*>(_nsyms);
for (int i = 0 ; i < _nsyms ; i++)
_syms[i] = b._syms[i]->copy(home);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
Actor*
LDSBBrancher<View,n,Val,a,Filter,Print>::copy(Space& home) {
return new (home) LDSBBrancher<View,n,Val,a,Filter,Print>
(home,*this);
}
// Compute choice
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
const Choice*
LDSBBrancher<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;
int choiceVal = pvc->val();
delete c;
_prevPos = choicePos;
// TODO: It should be possible to use simpler containers than the
// STL ones here.
std::deque<Literal> queue;
std::set<Literal> seen;
seen.insert(Literal(choicePos, choiceVal));
queue.push_back(Literal(choicePos, choiceVal));
do {
Literal l = queue[0];
queue.pop_front();
for (int i = 0 ; i < _nsyms ; i++) {
ArgArray<Literal> toExclude = _syms[i]->symmetric(l, this->x);
for (int j = 0 ; j < toExclude.size() ; ++j) {
if (seen.find(toExclude[j]) == seen.end())
queue.push_back(toExclude[j]);
seen.insert(toExclude[j]);
}
}
} while (queue.size() > 0);
// Convert "seen" vector into array.
int nliterals = static_cast<int>(seen.size());
Literal* literals = new Literal[nliterals];
std::set<Literal>::iterator it = seen.begin();
for (int i = 0 ; i < nliterals ; i++) {
literals[i] = *it;
++it;
}
return new LDSBChoice<Val>(*this,a,choicePos,choiceVal, literals, nliterals);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
const Choice*
LDSBBrancher<View,n,Val,a,Filter,Print>::choice(const Space& home,
Archive& e) {
(void) home;
int p; e >> p;
Val v; e >> v;
int nliterals; e >> nliterals;
Literal* literals = new Literal[nliterals];
for (int i = 0 ; i < nliterals ; i++) {
e >> literals[i]._variable;
e >> literals[i]._value;
}
return new LDSBChoice<Val>(*this,a,p,v, literals, nliterals);
}
template <>
inline
ModEvent
prune<Int::IntView>(Space& home, Int::IntView x, int v) {
return x.nq(home, v);
}
template <>
inline
ModEvent
prune<Int::BoolView>(Space& home, Int::BoolView x, int v) {
return x.nq(home, v);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
ExecStatus
LDSBBrancher<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 (b == 0) {
// 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>
size_t
LDSBBrancher<View,n,Val,a,Filter,Print>::dispose(Space& home) {
home.ignore(*this,AP_DISPOSE,true);
(void) ViewValBrancher<View,n,Val,a,Filter,Print>::dispose(home);
return sizeof(LDSBBrancher<View,n,Val,a,Filter,Print>);
}
template<class View, int n, class Val, unsigned int a>
forceinline void
postldsbbrancher(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) {
LDSBBrancher<View,n,Val,a,BrancherFilter<View>,BrancherPrint<View,Val>>
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
} else {
LDSBBrancher<View,n,Val,a,BrancherFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
} else {
if (vvp) {
LDSBBrancher<View,n,Val,a,BrancherNoFilter<View>,BrancherPrint<View,Val>>
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
} else {
LDSBBrancher<View,n,Val,a,BrancherNoFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,syms,nsyms,bf,vvp);
}
}
}
}}}
// STATISTICS: int-branch
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Archive
Archive representation
Definition archive.hpp:42
Gecode::ArgArrayBase::size
int size(void) const
Return size of array (number of elements).
Definition array.hpp:1607
Gecode::ArgArray
Argument array for non-primitive types.
Definition array.hpp:691
Gecode::BrancherNoPrint
Class without print function.
Definition print.hpp:73
Gecode::BrancherPrint
Class storing a print function.
Definition print.hpp:47
Gecode::Brancher
Base-class for branchers.
Definition core.hpp:1442
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::Home::notice
void notice(Actor &a, ActorProperty p, bool duplicate=false)
Notice actor property.
Definition core.hpp:3219
Gecode::Int::BoolView
Boolean view for Boolean variables.
Definition view.hpp:1380
Gecode::Int::BoolView::nq
ModEvent nq(Space &home, int n)
Restrict domain values to be different from n.
Definition bool.hpp:161
Gecode::Int::IntView
Integer view for integer variables.
Definition view.hpp:129
Gecode::Int::IntView::nq
ModEvent nq(Space &home, int n)
Restrict domain values to be different from n.
Definition int.hpp:157
Gecode::Int::LDSB::LDSBBrancher
Symmetry-breaking brancher with generic view and value selection.
Definition ldsb.hh:332
Gecode::Int::LDSB::LDSBBrancher::post
static void post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, SymmetryImp< View > **syms, int nsyms, BranchFilter< Var > bf, VarValPrint< Var, Val > vvp)
Brancher post function.
Definition brancher.hpp:112
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::LDSBBrancher::copy
virtual Actor * copy(Space &home)
Perform cloning.
Definition brancher.hpp:138
Gecode::Int::LDSB::LDSBBrancher::LDSBBrancher
LDSBBrancher(Space &home, LDSBBrancher &b)
Constructor for cloning b.
Gecode::Int::LDSB::LDSBBrancher::dispose
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition brancher.hpp:267
Gecode::Int::LDSB::LDSBBrancher::_prevPos
int _prevPos
Definition ldsb.hh:340
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::LDSBChoice::LDSBChoice
LDSBChoice(const Brancher &b, unsigned int a, const Pos &p, const Val &n, const Literal *literals, int nliterals)
Initialize choice for brancher b, position p, value n, and set of literals literals (of size nliteral...
Definition brancher.hpp:59
Gecode::Int::LDSB::LDSBChoice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition brancher.hpp:80
Gecode::Int::LDSB::LDSBChoice::~LDSBChoice
~LDSBChoice(void)
Destructor.
Definition brancher.hpp:66
Gecode::Int::LDSB::Literal
A Literal is a pair of variable index and value.
Definition ldsb.hh:46
Gecode::Int::LDSB::Literal::operator<
bool operator<(const Literal &rhs) const
Less than. The ordering is the lexicographical order on the (variable,value) pair.
Definition brancher.hpp:49
Gecode::Int::LDSB::Literal::Literal
Literal(void)
Constructor for an empty literal.
Definition brancher.hpp:40
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::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::PosValChoice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition view-val.hpp:171
Gecode::PosValChoice::PosValChoice
PosValChoice(const Brancher &b, unsigned int a, const Pos &p, const Val &n)
Initialize choice for brancher b, number of alternatives a, position p, and value n.
Definition view-val.hpp:159
Gecode::Pos
Position information.
Definition view.hpp:44
Gecode::Pos::pos
const int pos
Position of view.
Definition view.hpp:47
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::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
Generic brancher by view and value selection.
Definition view-val.hpp:91
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::ViewValBrancher::p
Print p
Print function.
Definition view-val.hpp:101
Gecode::ViewValBrancher::dispose
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view-val.hpp:331
Gecode::Int::OffsetView::update
void update(const NoOffset &)
Integer-precision integer scale view.
Definition view.hpp:638
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
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::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
Gecode::Int::LDSB
Symmetry breaking for integer variables.
Definition int.hh:5246
Gecode::Int::LDSB::prune
ModEvent prune(Space &home, View x, int v)
Exclude value \v from variable view \x.
Gecode::Int::LDSB::prune< Int::BoolView >
ModEvent prune< Int::BoolView >(Space &home, Int::BoolView x, int v)
Definition brancher.hpp:223
Gecode::Int::LDSB::postldsbbrancher
void postldsbbrancher(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)
Post LDSB brancher.
Definition brancher.hpp:275
Gecode::Int::LDSB::prune< Int::IntView >
ModEvent prune< Int::IntView >(Space &home, Int::IntView x, int v)
Definition brancher.hpp:216
Gecode::Int
Finite domain integers.
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::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
forceinline
#define forceinline
Definition config.hpp:187