merit.hpp
Go to the documentation of this file.
/* -*- mode: C++; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
* Main author:
* 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 _View, class _Val>
class MeritBase {
public:
typedef _View View;
typedef typename View::VarType Var;
typedef _Val Val;
MeritBase(Space& home, const VarBranch<Var>& vb);
MeritBase(Space& home, MeritBase& mb);
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
class MeritFunction : public MeritBase<View,double> {
using typename MeritBase<View,double>::Var;
public:
typedef typename BranchTraits<Var>::Merit Function;
protected:
SharedData<Function> f;
public:
MeritFunction(Space& home, const VarBranch<Var>& vb);
MeritFunction(Space& home, MeritFunction& mf);
double operator ()(const Space& home, View x, int i);
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
class MeritDegree : public MeritBase<View,unsigned int> {
using typename MeritBase<View,unsigned int>::Var;
public:
MeritDegree(Space& home, const VarBranch<Var>& vb);
MeritDegree(Space& home, MeritDegree& md);
unsigned int operator ()(const Space& home, View x, int i);
};
template<class View>
class MeritAFC : public MeritBase<View,double> {
using typename MeritBase<View,double>::Var;
protected:
AFC afc;
public:
MeritAFC(Space& home, const VarBranch<Var>& vb);
MeritAFC(Space& home, MeritAFC& ma);
double operator ()(const Space& home, View x, int i);
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
class MeritAction : public MeritBase<View,double> {
using typename MeritBase<View,double>::Var;
protected:
Action action;
public:
MeritAction(Space& home, const VarBranch<Var>& vb);
MeritAction(Space& home, MeritAction& ma);
double operator ()(const Space& home, View x, int i);
bool notice(void) const;
void dispose(Space& home);
};
template<class View>
class MeritCHB : public MeritBase<View,double> {
using typename MeritBase<View,double>::Var;
protected:
CHB chb;
public:
MeritCHB(Space& home, const VarBranch<Var>& vb);
MeritCHB(Space& home, MeritCHB& ma);
double operator ()(const Space& home, View x, int i);
bool notice(void) const;
void dispose(Space& home);
};
// Merit base class
template<class View, class Val>
forceinline
MeritBase<View,Val>::MeritBase(Space&, const VarBranch<Var>&) {}
template<class View, class Val>
forceinline
MeritBase<View,Val>::MeritBase(Space&, MeritBase&) {}
template<class View, class Val>
forceinline bool
MeritBase<View,Val>::notice(void) const {
return false;
}
template<class View, class Val>
forceinline void
MeritBase<View,Val>::dispose(Space&) {}
// User-defined function merit
template<class View>
forceinline
MeritFunction<View>::MeritFunction
(Space& home, const VarBranch<MeritFunction<View>::Var>& vb)
: MeritBase<View,double>(home,vb), f(vb.merit()) {
if (!f())
throw InvalidFunction("MeritFunction::MeritFunction");
}
template<class View>
forceinline
MeritFunction<View>::MeritFunction(Space& home, MeritFunction& mf)
: MeritBase<View,double>(home,mf), f(mf.f) {
}
template<class View>
forceinline double
MeritFunction<View>::operator ()(const Space& home, View x, int i) {
typename View::VarType y(x.varimp());
GECODE_VALID_FUNCTION(f());
return f()(home,y,i);
}
template<class View>
forceinline bool
MeritFunction<View>::notice(void) const {
return true;
}
template<class View>
forceinline void
MeritFunction<View>::dispose(Space&) {
f.~SharedData<Function>();
}
// Degree merit
template<class View>
forceinline
MeritDegree<View>::MeritDegree
(Space& home, const VarBranch<MeritDegree<View>::Var>& vb)
: MeritBase<View,unsigned int>(home,vb) {}
template<class View>
forceinline
MeritDegree<View>::MeritDegree(Space& home, MeritDegree& md)
: MeritBase<View,unsigned int>(home,md) {}
template<class View>
forceinline unsigned int
MeritDegree<View>::operator ()(const Space&, View x, int) {
return x.degree();
}
// AFC merit
template<class View>
forceinline
MeritAFC<View>::MeritAFC
(Space& home, const VarBranch<MeritAFC<View>::Var>& vb)
: MeritBase<View,double>(home,vb), afc(vb.afc()) {}
template<class View>
forceinline
MeritAFC<View>::MeritAFC(Space& home, MeritAFC& ma)
: MeritBase<View,double>(home,ma), afc(ma.afc) {}
template<class View>
forceinline double
MeritAFC<View>::operator ()(const Space&, View x, int) {
return x.afc();
}
template<class View>
forceinline bool
MeritAFC<View>::notice(void) const {
// Given that AFC is just a fake, this not really necessary
return false;
}
template<class View>
forceinline void
MeritAFC<View>::dispose(Space&) {
// Given that AFC is just a fake, this not really necessary
afc.~AFC();
}
// Action merit
template<class View>
forceinline
MeritAction<View>::MeritAction
(Space& home, const VarBranch<MeritAction<View>::Var>& vb)
: MeritBase<View,double>(home,vb), action(vb.action()) {}
template<class View>
forceinline
MeritAction<View>::MeritAction(Space& home, MeritAction& ma)
: MeritBase<View,double>(home,ma), action(ma.action) {}
template<class View>
forceinline double
MeritAction<View>::operator ()(const Space&, View, int i) {
return action[i];
}
template<class View>
forceinline bool
MeritAction<View>::notice(void) const {
return true;
}
template<class View>
forceinline void
MeritAction<View>::dispose(Space&) {
action.~Action();
}
// CHB merit
template<class View>
forceinline
MeritCHB<View>::MeritCHB
(Space& home, const VarBranch<MeritCHB<View>::Var>& vb)
: MeritBase<View,double>(home,vb), chb(vb.chb()) {}
template<class View>
forceinline
MeritCHB<View>::MeritCHB(Space& home, MeritCHB& ma)
: MeritBase<View,double>(home,ma), chb(ma.chb) {}
template<class View>
forceinline double
MeritCHB<View>::operator ()(const Space&, View, int i) {
return chb[i];
}
template<class View>
forceinline bool
MeritCHB<View>::notice(void) const {
return true;
}
template<class View>
forceinline void
MeritCHB<View>::dispose(Space&) {
chb.~CHB();
}
}
// STATISTICS: kernel-branch
Gecode::AFC
Class for AFC (accumulated failure count) management.
Definition afc.hpp:40
Gecode::Action
Class for action management.
Definition action.hpp:42
Gecode::BranchTraits
Traits for branching.
Definition traits.hpp:55
Gecode::CHB
Class for CHB management.
Definition chb.hpp:46
Gecode::InvalidFunction
Exception: invalid function
Definition exception.hpp:114
Gecode::MeritAFC::dispose
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:261
Gecode::MeritAFC::MeritAFC
MeritAFC(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Gecode::MeritAFC::afc
AFC afc
AFC information.
Definition merit.hpp:112
Gecode::MeritAFC::operator()
double operator()(const Space &home, View x, int i)
Return AFC as merit for view x at position i.
Definition merit.hpp:250
Gecode::MeritAFC::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition merit.hpp:255
Gecode::MeritAction::MeritAction
MeritAction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Gecode::MeritAction::dispose
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:289
Gecode::MeritAction::operator()
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition merit.hpp:279
Gecode::MeritAction::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition merit.hpp:284
Gecode::MeritAction::action
Action action
Action information.
Definition merit.hpp:134
Gecode::MeritBase
Base-class for merit class.
Definition merit.hpp:46
Gecode::MeritBase::View
_View View
View type.
Definition merit.hpp:49
Gecode::MeritBase::Var
View::VarType Var
Corresponding variable type.
Definition merit.hpp:51
Gecode::MeritBase::dispose
void dispose(Space &home)
Delete view merit class.
Definition merit.hpp:187
Gecode::MeritBase::Val
_Val Val
Type of merit.
Definition merit.hpp:53
Gecode::MeritBase::MeritBase
MeritBase(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Definition merit.hpp:176
Gecode::MeritBase::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition merit.hpp:182
Gecode::MeritCHB::MeritCHB
MeritCHB(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Gecode::MeritCHB::chb
CHB chb
CHB information.
Definition merit.hpp:157
Gecode::MeritCHB::dispose
void dispose(Space &home)
Dispose view selection.
Definition merit.hpp:315
Gecode::MeritCHB::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition merit.hpp:310
Gecode::MeritCHB::operator()
double operator()(const Space &home, View x, int i)
Return action as merit for view x at position i.
Definition merit.hpp:305
Gecode::MeritDegree::MeritDegree
MeritDegree(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Gecode::MeritDegree::operator()
unsigned int operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition merit.hpp:234
Gecode::MeritFunction::dispose
void dispose(Space &home)
Delete view merit class.
Definition merit.hpp:217
Gecode::MeritFunction::operator()
double operator()(const Space &home, View x, int i)
Return degree as merit for view x at position i.
Definition merit.hpp:205
Gecode::MeritFunction::Function
BranchTraits< Var >::Merit Function
Corresponding merit function type.
Definition merit.hpp:72
Gecode::MeritFunction::f
SharedData< Function > f
The user-defined merit function.
Definition merit.hpp:75
Gecode::MeritFunction::notice
bool notice(void) const
Whether dispose must always be called (that is, notice is needed).
Definition merit.hpp:212
Gecode::MeritFunction::MeritFunction
MeritFunction(Space &home, const VarBranch< Var > &vb)
Constructor for initialization.
Gecode::SharedData
Class for sharing data between spaces.
Definition shared-data.hpp:38
Gecode::Space
Computation spaces.
Definition core.hpp:1742
Gecode::View
Gecode::VarBranch
Variable branching information.
Definition var.hpp:55
Gecode
Gecode toplevel namespace
forceinline
#define forceinline
Definition config.hpp:187
GECODE_VALID_FUNCTION
#define GECODE_VALID_FUNCTION(f)
Assert that a function is valid.
Definition macros.hpp:94