/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main authors: * Christian Schulte * * 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 { /** * \defgroup TaskBranchViewVal Generic brancher based on view and value selection * * Implements view-based brancher for an array of views and value. * \ingroup TaskActor */ //@{ /// %Choice storing position and value template class GECODE_VTABLE_EXPORT PosValChoice : public PosChoice { private: /// Value to assign to const Val _val; public: /// Initialize choice for brancher \a b, number of alternatives \a a, position \a p, and value \a n PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n); const Val& val(void) const; /// Archive into \a e virtual void archive(Archive& e) const; }; /// View-value no-good literal template class ViewValNGL : public NGL { protected: /// The stored view View x; /// The stored value Val n; public: /// Initialize for propagator \a p with view \a x and value \a n ViewValNGL(Space& home, View x, Val n); /// Constructor for cloning \a ngl ViewValNGL(Space& home, ViewValNGL& ngl); /// Create subscription for no-good literal virtual void subscribe(Space& home, Propagator& p); /// Cancel subscription for no-good literal virtual void cancel(Space& home, Propagator& p); /// Schedule propagator \a p virtual void reschedule(Space& home, Propagator& p); /// Dispose virtual size_t dispose(Space& home); }; /** * \brief Generic brancher by view and value selection * * Implements view-based branching for an array of views (of type * \a View) and value (of type \a Val). * */ template class ViewValBrancher : public ViewBrancher { protected: using ViewBrancher::vs; using ViewBrancher::x; using ViewBrancher::f; /// The corresponding variable typedef typename View::VarType Var; /// Value selection and commit object ValSelCommitBase* vsc; /// Print function Print p; /// Constructor for cloning \a b ViewValBrancher(Space& home, ViewValBrancher& b); /// Constructor for creation ViewValBrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, BranchFilter bf, VarValPrint vvp); public: /// Return choice virtual const Choice* choice(Space& home); /// Return choice virtual const Choice* choice(const Space& home, Archive& e); /// Perform commit for choice \a c and alternative \a b virtual ExecStatus commit(Space& home, const Choice& c, unsigned int b); /// Create no-good literal for choice \a c and alternative \a b virtual NGL* ngl(Space& home, const Choice& c, unsigned int b) const; /** * \brief Print branch for choice \a c and alternative \a b * * Prints an explanation of the alternative \a b of choice \a c * on the stream \a o. * */ virtual void print(const Space& home, const Choice& c, unsigned int b, std::ostream& o) const; /// Perform cloning virtual Actor* copy(Space& home); /// Delete brancher and return its size virtual size_t dispose(Space& home); /// Brancher post function static void post(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, BranchFilter bf, VarValPrint vvp); }; /// Post view value brancher template void postviewvalbrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, BranchFilter bf, VarValPrint vvp); //@} /* * %Choice with position and value * */ template forceinline PosValChoice::PosValChoice(const Brancher& b, unsigned int a, const Pos& p, const Val& n) : PosChoice(b,a,p), _val(n) {} template forceinline const Val& PosValChoice::val(void) const { return _val; } template forceinline void PosValChoice::archive(Archive& e) const { PosChoice::archive(e); e << _val; } /* * View-value no-good literal * */ template forceinline ViewValNGL::ViewValNGL(Space& home, View x0, Val n0) : NGL(home), x(x0), n(n0) {} template forceinline ViewValNGL::ViewValNGL(Space& home, ViewValNGL& ngl) : NGL(home,ngl), n(ngl.n) { x.update(home,ngl.x); } template void ViewValNGL::subscribe(Space& home, Propagator& p) { x.subscribe(home,p,pc); } template void ViewValNGL::cancel(Space& home, Propagator& p) { x.cancel(home,p,pc); } template void ViewValNGL::reschedule(Space& home, Propagator& p) { x.reschedule(home,p,pc); } template size_t ViewValNGL::dispose(Space& home) { (void) NGL::dispose(home); return sizeof(*this); } /* * Generic brancher based on variable/value selection * */ template forceinline ViewValBrancher:: ViewValBrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc0, BranchFilter bf, VarValPrint vvp) : ViewBrancher(home,x,vs,bf), vsc(vsc0), p(vvp) { if (vsc->notice() || f.notice() || p.notice()) home.notice(*this,AP_DISPOSE,true); } template forceinline void ViewValBrancher:: post(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, BranchFilter bf, VarValPrint vvp) { (void) new (home) ViewValBrancher (home,x,vs,vsc,bf,vvp); } template forceinline ViewValBrancher:: ViewValBrancher(Space& home, ViewValBrancher& b) : ViewBrancher(home,b), vsc(b.vsc->copy(home)), p(b.p) {} template Actor* ViewValBrancher::copy(Space& home) { return new (home) ViewValBrancher (home,*this); } template const Choice* ViewValBrancher::choice(Space& home) { Pos p = ViewBrancher::pos(home); View v = ViewBrancher::view(p); return new PosValChoice(*this,a,p,vsc->val(home,v,p.pos)); } template const Choice* ViewValBrancher::choice(const Space& home, Archive& e) { (void) home; int p; e >> p; Val v; e >> v; return new PosValChoice(*this,a,p,v); } template ExecStatus ViewValBrancher ::commit(Space& home, const Choice& c, unsigned int b) { const PosValChoice& pvc = static_cast&>(c); return me_failed(vsc->commit(home,b, ViewBrancher::view(pvc.pos()), pvc.pos().pos, pvc.val())) ? ES_FAILED : ES_OK; } template NGL* ViewValBrancher ::ngl(Space& home, const Choice& c, unsigned int b) const { const PosValChoice& pvc = static_cast&>(c); return vsc->ngl(home,b, ViewBrancher::view(pvc.pos()),pvc.val()); } template void ViewValBrancher ::print(const Space& home, const Choice& c, unsigned int b, std::ostream& o) const { const PosValChoice& pvc = static_cast&>(c); View xi = ViewBrancher::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 forceinline size_t ViewValBrancher::dispose(Space& home) { if (vsc->notice() || f.notice() || p.notice()) home.ignore(*this,AP_DISPOSE,true); vsc->dispose(home); (void) ViewBrancher::dispose(home); return sizeof(ViewValBrancher); } template forceinline void postviewvalbrancher(Home home, ViewArray& x, ViewSel* vs[n], ValSelCommitBase* vsc, BranchFilter bf, VarValPrint vvp) { if (bf) { if (vvp) { ViewValBrancher,BrancherPrint > ::post(home,x,vs,vsc,bf,vvp); } else { ViewValBrancher,BrancherNoPrint > ::post(home,x,vs,vsc,bf,vvp); } } else { if (vvp) ViewValBrancher,BrancherPrint > ::post(home,x,vs,vsc,bf,vvp); else ViewValBrancher,BrancherNoPrint > ::post(home,x,vs,vsc,bf,vvp); } } } // STATISTICS: kernel-branch