/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */ /* * Main author: * 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 TaskBranchMerit Generic merit for branchers based on view and value selection * * \ingroup TaskBranchViewVal */ //@{ /** * \brief Base-class for merit class */ template class MeritBase { public: /// View type typedef View_ View; /// Corresponding variable type typedef typename View::VarType Var; /// Type of merit typedef Val_ Val; /// Constructor for initialization MeritBase(Space& home, const VarBranch& vb); /// Constructor for cloning MeritBase(Space& home, MeritBase& mb); /// Whether dispose must always be called (that is, notice is needed) bool notice(void) const; /// Delete view merit class void dispose(Space& home); }; /** * \brief Merit class for user-defined merit function */ template class MeritFunction : public MeritBase { using typename MeritBase::Var; public: /// Corresponding merit function type typedef typename BranchTraits::Merit Function; protected: /// The user-defined merit function SharedData f; public: /// Constructor for initialization MeritFunction(Space& home, const VarBranch& vb); /// Constructor for cloning MeritFunction(Space& home, MeritFunction& mf); /// Return degree as merit for view \a x at position \a i double operator ()(const Space& home, View x, int i); /// Whether dispose must always be called (that is, notice is needed) bool notice(void) const; /// Delete view merit class void dispose(Space& home); }; /** * \brief Merit class for degree */ template class MeritDegree : public MeritBase { using typename MeritBase::Var; public: /// Constructor for initialization MeritDegree(Space& home, const VarBranch& vb); /// Constructor for cloning MeritDegree(Space& home, MeritDegree& md); /// Return degree as merit for view \a x at position \a i unsigned int operator ()(const Space& home, View x, int i); }; /** * \brief Merit class for AFC */ template class MeritAFC : public MeritBase { using typename MeritBase::Var; protected: /// AFC information AFC afc; public: /// Constructor for initialization MeritAFC(Space& home, const VarBranch& vb); /// Constructor for cloning MeritAFC(Space& home, MeritAFC& ma); /// Return AFC as merit for view \a x at position \a i double operator ()(const Space& home, View x, int i); /// Whether dispose must always be called (that is, notice is needed) bool notice(void) const; /// Dispose view selection void dispose(Space& home); }; /** * \brief Merit class for action */ template class MeritAction : public MeritBase { using typename MeritBase::Var; protected: /// Action information Action action; public: /// Constructor for initialization MeritAction(Space& home, const VarBranch& vb); /// Constructor for cloning MeritAction(Space& home, MeritAction& ma); /// Return action as merit for view \a x at position \a i double operator ()(const Space& home, View x, int i); /// Whether dispose must always be called (that is, notice is needed) bool notice(void) const; /// Dispose view selection void dispose(Space& home); }; //@} /** * \brief Merit class for CHB */ template class MeritCHB : public MeritBase { using typename MeritBase::Var; protected: /// CHB information CHB chb; public: /// Constructor for initialization MeritCHB(Space& home, const VarBranch& vb); /// Constructor for cloning MeritCHB(Space& home, MeritCHB& ma); /// Return action as merit for view \a x at position \a i double operator ()(const Space& home, View x, int i); /// Whether dispose must always be called (that is, notice is needed) bool notice(void) const; /// Dispose view selection void dispose(Space& home); }; //@} // Merit base class template forceinline MeritBase::MeritBase(Space&, const VarBranch&) {} template forceinline MeritBase::MeritBase(Space&, MeritBase&) {} template forceinline bool MeritBase::notice(void) const { return false; } template forceinline void MeritBase::dispose(Space&) {} // User-defined function merit template forceinline MeritFunction::MeritFunction (Space& home, const VarBranch::Var>& vb) : MeritBase(home,vb), f(vb.merit()) { if (!f()) throw InvalidFunction("MeritFunction::MeritFunction"); } template forceinline MeritFunction::MeritFunction(Space& home, MeritFunction& mf) : MeritBase(home,mf), f(mf.f) { } template forceinline double MeritFunction::operator ()(const Space& home, View x, int i) { typename View::VarType y(x.varimp()); GECODE_VALID_FUNCTION(f()); return f()(home,y,i); } template forceinline bool MeritFunction::notice(void) const { return true; } template forceinline void MeritFunction::dispose(Space&) { f.~SharedData(); } // Degree merit template forceinline MeritDegree::MeritDegree (Space& home, const VarBranch::Var>& vb) : MeritBase(home,vb) {} template forceinline MeritDegree::MeritDegree(Space& home, MeritDegree& md) : MeritBase(home,md) {} template forceinline unsigned int MeritDegree::operator ()(const Space&, View x, int) { return x.degree(); } // AFC merit template forceinline MeritAFC::MeritAFC (Space& home, const VarBranch::Var>& vb) : MeritBase(home,vb), afc(vb.afc()) {} template forceinline MeritAFC::MeritAFC(Space& home, MeritAFC& ma) : MeritBase(home,ma), afc(ma.afc) {} template forceinline double MeritAFC::operator ()(const Space&, View x, int) { return x.afc(); } template forceinline bool MeritAFC::notice(void) const { // Given that AFC is just a fake, this not really necessary return false; } template forceinline void MeritAFC::dispose(Space&) { // Given that AFC is just a fake, this not really necessary afc.~AFC(); } // Action merit template forceinline MeritAction::MeritAction (Space& home, const VarBranch::Var>& vb) : MeritBase(home,vb), action(vb.action()) {} template forceinline MeritAction::MeritAction(Space& home, MeritAction& ma) : MeritBase(home,ma), action(ma.action) {} template forceinline double MeritAction::operator ()(const Space&, View, int i) { return action[i]; } template forceinline bool MeritAction::notice(void) const { return true; } template forceinline void MeritAction::dispose(Space&) { action.~Action(); } // CHB merit template forceinline MeritCHB::MeritCHB (Space& home, const VarBranch::Var>& vb) : MeritBase(home,vb), chb(vb.chb()) {} template forceinline MeritCHB::MeritCHB(Space& home, MeritCHB& ma) : MeritBase(home,ma), chb(ma.chb) {} template forceinline double MeritCHB::operator ()(const Space&, View, int i) { return chb[i]; } template forceinline bool MeritCHB::notice(void) const { return true; } template forceinline void MeritCHB::dispose(Space&) { chb.~CHB(); } } // STATISTICS: kernel-branch