view-val.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main authors:
* Christian Schulte <schulte@gecode.org>
*
* Copyright:
* Christian Schulte, 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 {
template<class Val>
class GECODE_VTABLE_EXPORT PosValChoice : public PosChoice {
private:
const Val _val;
public:
PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n);
const Val& val(void) const;
virtual void archive(Archive& e) const;
};
template<class View, class Val, PropCond pc>
class ViewValNGL : public NGL {
protected:
View x;
Val n;
public:
ViewValNGL(Space& home, View x, Val n);
ViewValNGL(Space& home, ViewValNGL& ngl);
virtual void subscribe(Space& home, Propagator& p);
virtual void cancel(Space& home, Propagator& p);
virtual void reschedule(Space& home, Propagator& p);
virtual size_t dispose(Space& home);
};
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
class ViewValBrancher : public ViewBrancher<View,Filter,n> {
protected:
using ViewBrancher<View,Filter,n>::vs;
using ViewBrancher<View,Filter,n>::x;
using ViewBrancher<View,Filter,n>::f;
typedef typename View::VarType Var;
ValSelCommitBase<View,Val>* vsc;
Print p;
ViewValBrancher(Space& home, ViewValBrancher& b);
ViewValBrancher(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
BranchFilter<Var> bf,
VarValPrint<Var,Val> vvp);
public:
virtual const Choice* choice(Space& home);
virtual const Choice* choice(const Space& home, Archive& e);
virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b);
virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const;
virtual void print(const Space& home, const Choice& c, unsigned int b,
std::ostream& o) const;
virtual Actor* copy(Space& home);
virtual size_t dispose(Space& home);
static void post(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
BranchFilter<Var> bf,
VarValPrint<Var,Val> vvp);
};
template<class View, int n, class Val, unsigned int a>
void
postviewvalbrancher(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
BranchFilter<typename View::VarType> bf,
VarValPrint<typename View::VarType,Val> vvp);
/*
* %Choice with position and value
*
*/
template<class Val>
forceinline
PosValChoice<Val>::PosValChoice(const Brancher& b, unsigned int a,
const Pos& p, const Val& n)
: PosChoice(b,a,p), _val(n) {}
template<class Val>
forceinline const Val&
PosValChoice<Val>::val(void) const {
return _val;
}
template<class Val>
forceinline void
PosValChoice<Val>::archive(Archive& e) const {
PosChoice::archive(e);
e << _val;
}
/*
* View-value no-good literal
*
*/
template<class View, class Val, PropCond pc>
forceinline
ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, View x0, Val n0)
: NGL(home), x(x0), n(n0) {}
template<class View, class Val, PropCond pc>
forceinline
ViewValNGL<View,Val,pc>::ViewValNGL(Space& home, ViewValNGL& ngl)
: NGL(home,ngl), n(ngl.n) {
x.update(home,ngl.x);
}
template<class View, class Val, PropCond pc>
void
ViewValNGL<View,Val,pc>::subscribe(Space& home, Propagator& p) {
x.subscribe(home,p,pc);
}
template<class View, class Val, PropCond pc>
void
ViewValNGL<View,Val,pc>::cancel(Space& home, Propagator& p) {
x.cancel(home,p,pc);
}
template<class View, class Val, PropCond pc>
void
ViewValNGL<View,Val,pc>::reschedule(Space& home, Propagator& p) {
x.reschedule(home,p,pc);
}
template<class View, class Val, PropCond pc>
size_t
ViewValNGL<View,Val,pc>::dispose(Space& home) {
(void) NGL::dispose(home);
return sizeof(*this);
}
/*
* Generic brancher based on variable/value selection
*
*/
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline
ViewValBrancher<View,n,Val,a,Filter,Print>::
ViewValBrancher(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc0,
BranchFilter<Var> bf,
VarValPrint<Var,Val> vvp)
: ViewBrancher<View,Filter,n>(home,x,vs,bf), vsc(vsc0), p(vvp) {
if (vsc->notice() || f.notice() || p.notice())
home.notice(*this,AP_DISPOSE,true);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline void
ViewValBrancher<View,n,Val,a,Filter,Print>::
post(Home home, ViewArray<View>& x,
ViewSel<View>* vs[n], ValSelCommitBase<View,Val>* vsc,
BranchFilter<Var> bf,
VarValPrint<Var,Val> vvp) {
(void) new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
(home,x,vs,vsc,bf,vvp);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline
ViewValBrancher<View,n,Val,a,Filter,Print>::
ViewValBrancher(Space& home,
ViewValBrancher<View,n,Val,a,Filter,Print>& b)
: ViewBrancher<View,Filter,n>(home,b),
vsc(b.vsc->copy(home)), p(b.p) {}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
Actor*
ViewValBrancher<View,n,Val,a,Filter,Print>::copy(Space& home) {
return new (home) ViewValBrancher<View,n,Val,a,Filter,Print>
(home,*this);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
const Choice*
ViewValBrancher<View,n,Val,a,Filter,Print>::choice(Space& home) {
Pos p = ViewBrancher<View,Filter,n>::pos(home);
View v = ViewBrancher<View,Filter,n>::view(p);
return new PosValChoice<Val>(*this,a,p,vsc->val(home,v,p.pos));
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
const Choice*
ViewValBrancher<View,n,Val,a,Filter,Print>::choice(const Space& home,
Archive& e) {
(void) home;
int p; e >> p;
Val v; e >> v;
return new PosValChoice<Val>(*this,a,p,v);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
ExecStatus
ViewValBrancher<View,n,Val,a,Filter,Print>
::commit(Space& home, const Choice& c, unsigned int b) {
const PosValChoice<Val>& pvc
= static_cast<const PosValChoice<Val>&>(c);
return me_failed(vsc->commit(home,b,
ViewBrancher<View,Filter,n>::view(pvc.pos()),
pvc.pos().pos,
pvc.val()))
? ES_FAILED : ES_OK;
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
NGL*
ViewValBrancher<View,n,Val,a,Filter,Print>
::ngl(Space& home, const Choice& c, unsigned int b) const {
const PosValChoice<Val>& pvc
= static_cast<const PosValChoice<Val>&>(c);
return vsc->ngl(home,b,
ViewBrancher<View,Filter,n>::view(pvc.pos()),pvc.val());
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
void
ViewValBrancher<View,n,Val,a,Filter,Print>
::print(const Space& home, const Choice& c, unsigned int b,
std::ostream& o) const {
const PosValChoice<Val>& pvc
= static_cast<const PosValChoice<Val>&>(c);
View xi = ViewBrancher<View,Filter,n>::view(pvc.pos());
if (p)
p(home,*this,b,xi,pvc.pos().pos,pvc.val(),o);
else
vsc->print(home,b,xi,pvc.pos().pos,pvc.val(),o);
}
template<class View, int n, class Val, unsigned int a,
class Filter, class Print>
forceinline size_t
ViewValBrancher<View,n,Val,a,Filter,Print>::dispose(Space& home) {
if (vsc->notice() || f.notice() || p.notice())
home.ignore(*this,AP_DISPOSE,true);
vsc->dispose(home);
(void) ViewBrancher<View,Filter,n>::dispose(home);
return sizeof(ViewValBrancher<View,n,Val,a,Filter,Print>);
}
template<class View, int n, class Val, unsigned int a>
forceinline void
postviewvalbrancher(Home home,
ViewArray<View>& x,
ViewSel<View>* vs[n],
ValSelCommitBase<View,Val>* vsc,
BranchFilter<typename View::VarType> bf,
VarValPrint<typename View::VarType,Val> vvp) {
if (bf) {
if (vvp) {
ViewValBrancher<View,n,Val,a,
BrancherFilter<View>,BrancherPrint<View,Val> >
::post(home,x,vs,vsc,bf,vvp);
} else {
ViewValBrancher<View,n,Val,a,
BrancherFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,bf,vvp);
}
} else {
if (vvp)
ViewValBrancher<View,n,Val,a,
BrancherNoFilter<View>,BrancherPrint<View,Val> >
::post(home,x,vs,vsc,bf,vvp);
else
ViewValBrancher<View,n,Val,a,
BrancherNoFilter<View>,BrancherNoPrint<View,Val> >
::post(home,x,vs,vsc,bf,vvp);
}
}
}
// STATISTICS: kernel-branch
Gecode::Actor
Base-class for both propagators and branchers.
Definition core.hpp:628
Gecode::Archive
Archive representation
Definition archive.hpp:42
Gecode::BrancherFilter
Class storing a branch filter function.
Definition filter.hpp:45
Gecode::BrancherNoFilter
Class withot a branch filter function.
Definition filter.hpp:68
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::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::NGL
No-good literal recorded during search.
Definition core.hpp:1340
Gecode::NGL::NGL
NGL(void)
Constructor for creation.
Definition core.hpp:3812
Gecode::NGL::dispose
virtual size_t dispose(Space &home)
Dispose.
Definition core.hpp:3821
Gecode::PosChoice::archive
virtual void archive(Archive &e) const
Archive into e.
Definition view.hpp:130
Gecode::PosChoice::pos
const Pos & pos(void) const
Return position in array.
Definition view.hpp:126
Gecode::PosChoice::PosChoice
PosChoice(const PosChoice &c)
Initialize.
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::Propagator
Base-class for propagators.
Definition core.hpp:1064
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::Val
Gecode::View
Gecode::ViewArray
View arrays.
Definition array.hpp:253
Gecode::ViewBrancher
Generic brancher by view selection.
Definition view.hpp:78
Gecode::ViewBrancher::pos
Pos pos(Space &home)
Return position information.
Definition view.hpp:173
Gecode::ViewBrancher::f
Filter f
Filter function.
Definition view.hpp:89
Gecode::ViewBrancher::x
ViewArray< View > x
Views to branch on.
Definition view.hpp:83
Gecode::ViewBrancher::ViewBrancher
ViewBrancher(Space &home, ViewBrancher< View, Filter, n > &b)
Constructor for cloning b.
Definition view.hpp:152
Gecode::ViewBrancher::view
View view(const Pos &p) const
Return view according to position information p.
Definition view.hpp:213
Gecode::ViewBrancher::dispose
virtual size_t dispose(Space &home)
Delete brancher and return its size.
Definition view.hpp:219
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::print
virtual void print(const Space &home, const Choice &c, unsigned int b, std::ostream &o) const
Print branch for choice c and alternative b.
Definition view-val.hpp:317
Gecode::ViewValBrancher::copy
virtual Actor * copy(Space &home)
Perform cloning.
Definition view-val.hpp:263
Gecode::ViewValBrancher::ngl
virtual NGL * ngl(Space &home, const Choice &c, unsigned int b) const
Create no-good literal for choice c and alternative b.
Definition view-val.hpp:306
Gecode::ViewValBrancher::post
static void post(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter< Var > bf, VarValPrint< Var, Val > vvp)
Brancher post function.
Definition view-val.hpp:243
Gecode::ViewValBrancher::commit
virtual ExecStatus commit(Space &home, const Choice &c, unsigned int b)
Perform commit for choice c and alternative b.
Definition view-val.hpp:292
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::ViewValNGL::x
View x
The stored view.
Definition view-val.hpp:64
Gecode::ViewValNGL::reschedule
virtual void reschedule(Space &home, Propagator &p)
Schedule propagator p.
Definition view-val.hpp:207
Gecode::ViewValNGL::n
Val n
The stored value.
Definition view-val.hpp:66
Gecode::ViewValNGL::cancel
virtual void cancel(Space &home, Propagator &p)
Cancel subscription for no-good literal.
Definition view-val.hpp:201
Gecode::ViewValNGL::ViewValNGL
ViewValNGL(Space &home, View x, Val n)
Initialize for propagator p with view x and value n.
Definition view-val.hpp:183
Gecode::ViewValNGL::dispose
virtual size_t dispose(Space &home)
Dispose.
Definition view-val.hpp:213
Gecode::ViewValNGL::subscribe
virtual void subscribe(Space &home, Propagator &p)
Create subscription for no-good literal.
Definition view-val.hpp:195
Gecode::Space::ignore
void ignore(Actor &a, ActorProperty p, bool duplicate=false)
Ignore actor property.
Definition core.hpp:4074
Gecode::me_failed
bool me_failed(ModEvent me)
Check whether modification event me is failed.
Definition modevent.hpp:54
Gecode::AP_DISPOSE
@ AP_DISPOSE
Actor must always be disposed.
Definition core.hpp:562
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::postviewvalbrancher
void postviewvalbrancher(Home home, ViewArray< View > &x, ViewSel< View > *vs[n], ValSelCommitBase< View, Val > *vsc, BranchFilter< typename View::VarType > bf, VarValPrint< typename View::VarType, Val > vvp)
Post view value brancher.
Definition view-val.hpp:341
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::ES_FAILED
@ ES_FAILED
Execution has resulted in failure.
Definition core.hpp:474
Gecode::BranchFilter
std::function< bool(const Space &home, Var x, int i)> BranchFilter
Function type for branch filter functions.
Definition filter.hpp:40
forceinline
#define forceinline
Definition config.hpp:187
GECODE_VTABLE_EXPORT
#define GECODE_VTABLE_EXPORT
Definition support.hh:72