[opengm] 374/386: merge changes for functions and graphical model from the learning branch
Ghislain Vaillant
ghisvail-guest at moszumanska.debian.org
Wed Aug 31 08:38:36 UTC 2016
This is an automated email from the git hooks/post-receive script.
ghisvail-guest pushed a commit to branch debian/master
in repository opengm.
commit d3a7385f2aee09770e13a538ecb6bd1c9876f53e
Author: Joerg Kappes <kappes at math.uni-heidelberg.de>
Date: Wed Aug 3 22:27:26 2016 +0200
merge changes for functions and graphical model from the learning branch
---
.../opengm/datastructures/marray/marray_hdf5.hxx | 25 +++
include/opengm/functions/constant.hxx | 1 +
.../linear_constraint_function_base.hxx | 9 +-
.../opengm/functions/function_properties_base.hxx | 11 +-
include/opengm/functions/l_potts.hxx | 202 ---------------------
include/opengm/functions/potts.hxx | 4 +-
include/opengm/functions/readme.txt | 3 +
.../sum_constraint_function.hxx | 1 +
include/opengm/functions/view_convert_function.hxx | 36 +++-
include/opengm/graphicalmodel/graphicalmodel.hxx | 42 +++++
.../graphicalmodel/graphicalmodel_factor.hxx | 27 +++
.../graphicalmodel_function_wrapper.hxx | 79 ++++++++
include/opengm/inference/external/qpbo.hxx | 4 +-
include/opengm/utilities/metaprogramming.hxx | 105 ++++++++---
src/unittest/CMakeLists.txt | 5 +-
src/unittest/test_gm_learning_functions.cxx | 23 +--
16 files changed, 325 insertions(+), 252 deletions(-)
diff --git a/include/opengm/datastructures/marray/marray_hdf5.hxx b/include/opengm/datastructures/marray/marray_hdf5.hxx
index 6db10fb..8e292f1 100644
--- a/include/opengm/datastructures/marray/marray_hdf5.hxx
+++ b/include/opengm/datastructures/marray/marray_hdf5.hxx
@@ -74,6 +74,8 @@ template<class T>
void load(const hid_t&, const std::string&, Marray<T>&);
template<class T>
void loadShape(const hid_t&, const std::string&, Vector<T>&);
+template<class T>
+ void loadVec(const hid_t&, const std::string&, std::vector<T>&);
template<class T, class BaseIterator, class ShapeIterator>
void loadHyperslab(const hid_t&, const std::string&,
BaseIterator, BaseIterator, ShapeIterator, Marray<T>&);
@@ -511,6 +513,29 @@ void loadShape(
handleCheck.check();
}
+/// Load a vector of an HDF5 dataset.
+///
+/// \param groupHandle Handle of the parent HDF5 file or group.
+/// \param datasetName Name of the HDF5 dataset.
+/// \param out Shape.
+///
+/// \sa load()
+///
+template<class T>
+void loadVec(
+ const hid_t& groupHandle,
+ const std::string& datasetName,
+ std::vector<T>& out
+)
+{
+ marray::Marray<T> v;
+ load( groupHandle,datasetName,v);
+ out.resize(v.size());
+ for(size_t j=0; j<v.size(); ++j) {
+ out[j] = v(j);
+ }
+}
+
/// Load a hyperslab from an HDF5 dataset into an Marray.
///
/// \param groupHandle Handle of the parent HDF5 file or group.
diff --git a/include/opengm/functions/constant.hxx b/include/opengm/functions/constant.hxx
index d0348a5..00b8cf6 100644
--- a/include/opengm/functions/constant.hxx
+++ b/include/opengm/functions/constant.hxx
@@ -5,6 +5,7 @@
#include <cmath>
#include <algorithm>
#include <vector>
+#include <functional>
#include "opengm/opengm.hxx"
#include "opengm/functions/function_registration.hxx"
diff --git a/include/opengm/functions/constraint_functions/linear_constraint_function_base.hxx b/include/opengm/functions/constraint_functions/linear_constraint_function_base.hxx
index 4686307..7335e50 100644
--- a/include/opengm/functions/constraint_functions/linear_constraint_function_base.hxx
+++ b/include/opengm/functions/constraint_functions/linear_constraint_function_base.hxx
@@ -8,8 +8,15 @@ namespace opengm {
/*********************
* class definition *
*********************/
+
+struct LinearConstraintFunctionTraitsUndefined
+{};
+
template <typename LINEAR_CONSTRAINT_FUNCTION_TYPE>
-struct LinearConstraintFunctionTraits;
+struct LinearConstraintFunctionTraits
+{
+ typedef LinearConstraintFunctionTraitsUndefined ValueType;
+};
template<class LINEAR_CONSTRAINT_FUNCTION_TYPE>
class LinearConstraintFunctionBase : public FunctionBase<LINEAR_CONSTRAINT_FUNCTION_TYPE, typename LinearConstraintFunctionTraits<LINEAR_CONSTRAINT_FUNCTION_TYPE>::ValueType, typename LinearConstraintFunctionTraits<LINEAR_CONSTRAINT_FUNCTION_TYPE>::IndexType, typename LinearConstraintFunctionTraits<LINEAR_CONSTRAINT_FUNCTION_TYPE>::LabelType> {
diff --git a/include/opengm/functions/function_properties_base.hxx b/include/opengm/functions/function_properties_base.hxx
index 41f4130..68882d5 100644
--- a/include/opengm/functions/function_properties_base.hxx
+++ b/include/opengm/functions/function_properties_base.hxx
@@ -18,7 +18,7 @@
#include "opengm/operations/adder.hxx"
#include "opengm/operations/integrator.hxx"
#include "opengm/operations/multiplier.hxx"
-#include "opengm/graphicalmodel/parameters.hxx"
+#include "opengm/graphicalmodel/weights.hxx"
#define OPENGM_FLOAT_TOL 0.000001
@@ -55,6 +55,7 @@ public:
bool isAbsoluteDifference() const;
bool isTruncatedAbsoluteDifference() const;
bool isLinearConstraint() const;
+
/// find minimum and maximum of the function in a single sweep
/// \return class holding the minimum and the maximum
@@ -127,12 +128,16 @@ public:
FunctionShapeIteratorType functionShapeBegin() const;
FunctionShapeIteratorType functionShapeEnd() const;
- size_t numberOfParameters()const{
+ size_t numberOfWeights()const{
return 0;
}
- INDEX parameterIndex(const size_t paramNumber)const{
+ INDEX weightIndex(const size_t weightNumber)const{
throw RuntimeError("Function base has no parameters,this needs to be implemented in any function type");
}
+ void setWeights(const opengm::learning::Weights<VALUE>& /*weights*/) const {}
+ template<class ITERATOR>
+ VALUE weightGradient(size_t,ITERATOR) const {return 0;}
+
};
diff --git a/include/opengm/functions/l_potts.hxx b/include/opengm/functions/l_potts.hxx
deleted file mode 100644
index 8694859..0000000
--- a/include/opengm/functions/l_potts.hxx
+++ /dev/null
@@ -1,202 +0,0 @@
-#pragma once
-#ifndef OPENGM_L_POTTS_FUNCTION_HXX
-#define OPENGM_L_POTTS_FUNCTION_HXX
-
-#include <algorithm>
-#include <vector>
-#include <cmath>
-
-#include "opengm/opengm.hxx"
-#include "opengm/functions/function_registration.hxx"
-#include "opengm/functions/function_properties_base.hxx"
-
-namespace opengm {
-
-/// Potts function for two variables
-///
-/// \ingroup functions
-template<class T, class I = size_t, class L = size_t>
-class LPottsFunction
-: public FunctionBase<LPottsFunction<T, I, L>, T, I, L>
-{
-public:
- typedef T ValueType;
- typedef L LabelType;
- typedef I IndexType;
-
- LPottsFunction(
- const LabelType,
- const LabelType,
- const Parameters<ValueType,IndexType> & parameters,
- const IndexType valueNotEqual
- );
- LabelType shape(const size_t) const;
- size_t size() const;
- size_t dimension() const;
- template<class ITERATOR> ValueType operator()(ITERATOR) const;
- bool operator==(const LPottsFunction& ) const;
- // specializations
- bool isPotts() const;
- bool isGeneralizedPotts() const;
- ValueType min() const;
- ValueType max() const;
- ValueType sum() const;
- ValueType product() const;
- MinMaxFunctor<ValueType> minMax() const;
-
- // parameters
- size_t numberOfParameters()const{
- return 1;
- }
- IndexType parameterIndex(const size_t paramNumber)const{
- return piValueNotEqual_;
- }
-
-
-private:
- LabelType numberOfLabels1_;
- LabelType numberOfLabels2_;
-
- const Parameters<ValueType,IndexType> * params_;
-
- IndexType piValueNotEqual_;
-
-friend class FunctionSerialization<LPottsFunction<T, I, L> > ;
-};
-
-
-template<class T, class I, class L>
-struct FunctionRegistration<LPottsFunction<T, I, L> > {
- enum ID {
- Id = opengm::FUNCTION_TYPE_ID_OFFSET + 100 + 6
- };
-};
-
-
-
-
-
-template <class T, class I, class L>
-inline
-LPottsFunction<T, I, L>::LPottsFunction
-(
- const L numberOfLabels1,
- const L numberOfLabels2,
- const Parameters<ValueType,IndexType> & parameters,
- const IndexType valueNotEqual
-)
-: numberOfLabels1_(numberOfLabels1),
- numberOfLabels2_(numberOfLabels2),
- params_(¶meters),
- piValueNotEqual_(valueNotEqual)
-{}
-
-template <class T, class I, class L>
-template <class ITERATOR>
-inline T
-LPottsFunction<T, I, L>::operator()
-(
- ITERATOR begin
-) const {
- return (begin[0]==begin[1] ?
- static_cast<ValueType>(0.0) : params_->getParameter(piValueNotEqual_) );
-}
-
-
-
-template <class T, class I, class L>
-inline L
-LPottsFunction<T, I, L>::shape
-(
- const size_t i
-) const {
- OPENGM_ASSERT(i < 2);
- return (i==0 ? numberOfLabels1_ : numberOfLabels2_);
-}
-
-template <class T, class I, class L>
-inline size_t
-LPottsFunction<T, I, L>::dimension() const {
- return 2;
-}
-
-template <class T, class I, class L>
-inline size_t
-LPottsFunction<T, I, L>::size() const {
- return numberOfLabels1_*numberOfLabels2_;
-}
-
-
-template<class T, class I, class L>
-inline bool
-LPottsFunction<T, I, L>::operator==
-(
- const LPottsFunction & fb
- )const{
- return numberOfLabels1_ == fb.numberOfLabels1_ &&
- numberOfLabels2_ == fb.numberOfLabels2_ &&
- piValueNotEqual_ == fb.piValueNotEqual_;
-}
-
-
-template<class T, class I, class L>
-inline bool
-LPottsFunction<T, I, L>::isPotts() const
-{
- return true;
-}
-
-template<class T, class I, class L>
-inline bool
-LPottsFunction<T, I, L>::isGeneralizedPotts() const
-{
- return true;
-}
-
-template<class T, class I, class L>
-inline typename LPottsFunction<T, I, L>::ValueType
-LPottsFunction<T, I, L>::min() const
-{
- const T val = params_->getParameter(piValueNotEqual_);
- return 0.0<val ? 0.0 :val;
-}
-
-template<class T, class I, class L>
-inline typename LPottsFunction<T, I, L>::ValueType
-LPottsFunction<T, I, L>::max() const
-{
- const T val = params_->getParameter(piValueNotEqual_);
- return 0.0>val ? 0.0 :val;
-}
-
-template<class T, class I, class L>
-inline typename LPottsFunction<T, I, L>::ValueType
-LPottsFunction<T, I, L>::sum() const
-{
- const T val = params_->getParameter(piValueNotEqual_);
- const LabelType minLabels = std::min(numberOfLabels1_, numberOfLabels2_);
- return val * static_cast<T>(numberOfLabels1_ * numberOfLabels2_ - minLabels);
-}
-
-template<class T, class I, class L>
-inline typename LPottsFunction<T, I, L>::ValueType
-LPottsFunction<T, I, L>::product() const
-{
- return static_cast<ValueType>(0);
-}
-
-template<class T, class I, class L>
-inline MinMaxFunctor<typename LPottsFunction<T, I, L>::ValueType>
-LPottsFunction<T, I, L>::minMax() const
-{
- if(static_cast<ValueType>(0) < piValueNotEqual_) {
- return MinMaxFunctor<T>(static_cast<ValueType>(0), params_[piValueNotEqual_]);
- }
- else {
- return MinMaxFunctor<T>(params_[piValueNotEqual_], static_cast<ValueType>(0));
- }
-}
-
-} // namespace opengm
-
-#endif // #ifndef OPENGM_L_POTTS_FUNCTION_HXX
diff --git a/include/opengm/functions/potts.hxx b/include/opengm/functions/potts.hxx
index 092887f..cb061f9 100644
--- a/include/opengm/functions/potts.hxx
+++ b/include/opengm/functions/potts.hxx
@@ -33,7 +33,7 @@ public:
bool operator==(const PottsFunction& ) const;
ValueType valueEqual() const;
ValueType valueNotEqual() const;
- IndexType numberOfParameters() const;
+ IndexType numberOfWeights() const;
ValueType parameter(const size_t index) const;
ValueType& parameter(const size_t index);
@@ -211,7 +211,7 @@ PottsFunction<T, I, L>::operator==
template<class T, class I, class L>
inline typename PottsFunction<T, I, L>::IndexType
-PottsFunction<T, I, L>::numberOfParameters() const
+PottsFunction<T, I, L>::numberOfWeights() const
{
return 2;
}
diff --git a/include/opengm/functions/readme.txt b/include/opengm/functions/readme.txt
index c538627..6d9b9f5 100644
--- a/include/opengm/functions/readme.txt
+++ b/include/opengm/functions/readme.txt
@@ -59,6 +59,9 @@ opengm::StaticSingleSideFunction 16009
opengm::DynamicSingleSideFunction 16010
opengm::PottsG 16011
+opengm::LPotts 16165
+opengm::LUnary 16166
+opengm::SumOfExperts 16167
/////////////////////////////////////////////////////////////
4. Serialization and de-serialization
diff --git a/include/opengm/functions/soft_constraint_functions/sum_constraint_function.hxx b/include/opengm/functions/soft_constraint_functions/sum_constraint_function.hxx
index 52693b7..74c233f 100644
--- a/include/opengm/functions/soft_constraint_functions/sum_constraint_function.hxx
+++ b/include/opengm/functions/soft_constraint_functions/sum_constraint_function.hxx
@@ -2,6 +2,7 @@
#define OPENGM_SUM_CONSTRAINT_FUNCTION_HXX_
#include <cmath>
+#include <functional>
#include <opengm/opengm.hxx>
#include <opengm/functions/function_registration.hxx>
diff --git a/include/opengm/functions/view_convert_function.hxx b/include/opengm/functions/view_convert_function.hxx
index 26ced72..1cfad00 100644
--- a/include/opengm/functions/view_convert_function.hxx
+++ b/include/opengm/functions/view_convert_function.hxx
@@ -18,7 +18,10 @@ namespace detail_convert_function {
template<class T>
static ProbabilityType convert(const T x)
{ return static_cast<ProbabilityType>(x); }
- };
+ template<class T>
+ static ProbabilityType convert(const T x, const T invT)
+ { return static_cast<ProbabilityType>(x); }
+ };
template<class PROBABILITY>
struct ValueToProbability<Multiplier, Minimizer, PROBABILITY>
@@ -26,7 +29,10 @@ namespace detail_convert_function {
typedef PROBABILITY ProbabilityType;
template<class T>
static ProbabilityType convert(const T x)
- { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); }
+ { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); } // is this correct ?!?
+ template<class T>
+ static ProbabilityType convert(const T x, const T invT)
+ { return static_cast<ProbabilityType>(1) / static_cast<ProbabilityType>(x); } // is this correct ?!?
};
template<class PROBABILITY>
@@ -36,6 +42,9 @@ namespace detail_convert_function {
template<class T>
static ProbabilityType convert(const T x)
{ return static_cast<ProbabilityType>(std::exp(x)); }
+ template<class T>
+ static ProbabilityType convert(const T x, const T invT)
+ { return static_cast<ProbabilityType>(std::exp(invT * x)); }
};
template<class PROBABILITY>
@@ -44,7 +53,10 @@ namespace detail_convert_function {
typedef PROBABILITY ProbabilityType;
template<class T>
static ProbabilityType convert(const T x)
- { return static_cast<ProbabilityType>(std::exp(-x)); }
+ { return static_cast<ProbabilityType>(std::exp(-x)); }
+ template<class T>
+ static ProbabilityType convert(const T x, const T invT)
+ { return static_cast<ProbabilityType>(std::exp(-invT * x)); }
};
}
/// \endcond
@@ -67,6 +79,7 @@ public:
ViewConvertFunction();
ViewConvertFunction(const FactorType &);
+ ViewConvertFunction(const FactorType &, const ValueType);
template<class Iterator> ValueType operator()(Iterator begin) const;
IndexType shape(const IndexType) const;
IndexType dimension() const;
@@ -74,12 +87,13 @@ public:
private:
FactorType const* factor_;
+ ValueType inverseTemperature_;
};
template<class GM,class ACC,class VALUE_TYPE>
inline
ViewConvertFunction<GM,ACC,VALUE_TYPE>::ViewConvertFunction()
-: factor_(NULL)
+ : factor_(NULL),inverseTemperature_(1)
{}
template<class GM,class ACC,class VALUE_TYPE>
@@ -88,7 +102,17 @@ ViewConvertFunction<GM,ACC,VALUE_TYPE>::ViewConvertFunction
(
const typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::FactorType & factor
)
-: factor_(&factor)
+: factor_(&factor),inverseTemperature_(1)
+{}
+
+template<class GM,class ACC,class VALUE_TYPE>
+inline
+ViewConvertFunction<GM,ACC,VALUE_TYPE>::ViewConvertFunction
+(
+ const typename ViewConvertFunction<GM,ACC,VALUE_TYPE>::FactorType & factor,
+ const VALUE_TYPE invT
+)
+: factor_(&factor),inverseTemperature_(invT)
{}
template<class GM,class ACC,class VALUE_TYPE>
@@ -98,7 +122,7 @@ ViewConvertFunction<GM,ACC,VALUE_TYPE>::operator()
(
Iterator begin
) const {
- return detail_convert_function::ValueToProbability<OperatorType,ACC,ValueType>::convert(factor_->operator()(begin));
+ return detail_convert_function::ValueToProbability<OperatorType,ACC,ValueType>::convert(factor_->operator()(begin),inverseTemperature_);
}
template<class GM,class ACC,class VALUE_TYPE>
diff --git a/include/opengm/graphicalmodel/graphicalmodel.hxx b/include/opengm/graphicalmodel/graphicalmodel.hxx
index 1273e75..4bd5cd6 100755
--- a/include/opengm/graphicalmodel/graphicalmodel.hxx
+++ b/include/opengm/graphicalmodel/graphicalmodel.hxx
@@ -93,6 +93,12 @@ public:
GraphicalModel(const SpaceType& ,const size_t reserveFactorsPerVariable=0);
GraphicalModel& operator=(const GraphicalModel&);
+ template<class OTHER_TL>
+ GraphicalModel& operator=(
+ const GraphicalModel<T, OPERATOR, OTHER_TL, SPACE > & otherGM
+ );
+
+
const SpaceType& space() const;
IndexType numberOfVariables() const;
IndexType numberOfVariables(const IndexType) const;
@@ -791,6 +797,42 @@ GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::operator=
return *this;
}
+
+template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
+template<class OTHER_TL>
+inline GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>&
+GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::operator=
+(
+ const GraphicalModel<T, OPERATOR, OTHER_TL, SPACE>& gm
+) {
+
+ typedef GraphicalModel<T, OPERATOR, OTHER_TL, SPACE> OtherGm;
+ this->space_ = gm.space_;
+
+ //this->functionDataField_=gm.functionDataField_;
+
+
+
+ std::vector<int> srcFidToTarget(OtherGm::NrOfFunctionTypes,-1);
+ detail_graphical_model::CopyFunctions<0, OtherGm::NrOfFunctionTypes >::op(gm, *this,srcFidToTarget);
+
+ this->factors_.resize(gm.factors_.size());
+ this->variableFactorAdjaceny_=gm.variableFactorAdjaceny_;
+ this->factorsVis_ = gm.factorsVis_;
+ this->order_ = gm.order_;
+
+ for(size_t i = 0; i<this->factors_.size(); ++i) {
+ factors_[i].gm_=this;
+ factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
+
+ int newFidFunctionId = srcFidToTarget[gm.factors_[i].functionTypeId_];
+ OPENGM_CHECK_OP(newFidFunctionId,>,-1,"INTERNAL ERROR");
+ factors_[i].functionTypeId_= newFidFunctionId;
+ factors_[i].vis_=gm.factors_[i].vis_;
+ factors_[i].vis_.assignPtr(this->factorsVis_);
+ }
+ return *this;
+}
template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
template<size_t FUNCTION_INDEX>
diff --git a/include/opengm/graphicalmodel/graphicalmodel_factor.hxx b/include/opengm/graphicalmodel/graphicalmodel_factor.hxx
index 519cc15..b2fa38b 100755
--- a/include/opengm/graphicalmodel/graphicalmodel_factor.hxx
+++ b/include/opengm/graphicalmodel/graphicalmodel_factor.hxx
@@ -192,6 +192,33 @@ public:
ValueType min() const;
ValueType max() const;
IndexType dimension()const{return this->numberOfVariables();}
+
+
+
+ template<class LABEL_ITER>
+ struct GmToFactorLabelIter{
+ typedef SubsetAccessor<VariablesIteratorType, LABEL_ITER> Accessor;
+ typedef AccessorIterator<Accessor, true> Iter;
+ };
+
+ template<class LABEL_ITER>
+ typename GmToFactorLabelIter<LABEL_ITER>::Iter
+ gmToFactorLabelsBegin(LABEL_ITER gmLabelsBegin)const{
+ typedef typename GmToFactorLabelIter<LABEL_ITER>::Accessor Accessor;
+ Accessor accessor(variableIndicesBegin(),variableIndicesEnd(), gmLabelsBegin);
+ return Iter(accessor, 0);
+ }
+
+ template<class LABEL_ITER>
+ typename GmToFactorLabelIter<LABEL_ITER>::Iter
+ gmToFactorLabelsEnd(LABEL_ITER gmLabelsBegin)const{
+ typedef typename GmToFactorLabelIter<LABEL_ITER>::Accessor Accessor;
+ typedef typename GmToFactorLabelIter<LABEL_ITER>::Iter Iter;
+ Accessor accessor(variableIndicesBegin(),variableIndicesEnd(), gmLabelsBegin);
+ return Iter(accessor, this->numberOfVariables());
+ }
+
+
private:
void testInvariant() const;
//std::vector<IndexType> & variableIndexSequence();
diff --git a/include/opengm/graphicalmodel/graphicalmodel_function_wrapper.hxx b/include/opengm/graphicalmodel/graphicalmodel_function_wrapper.hxx
index 63683cb..2556808 100755
--- a/include/opengm/graphicalmodel/graphicalmodel_function_wrapper.hxx
+++ b/include/opengm/graphicalmodel/graphicalmodel_function_wrapper.hxx
@@ -39,6 +39,85 @@ template<class GRAPHICAL_MODEL> class Factor;
namespace detail_graphical_model {
+
+ template<bool IN_LIST>
+ struct MaybeCopyFunctionVector;
+
+ template<>
+ struct MaybeCopyFunctionVector<true>{
+
+ template<class FVEC, class GM_T, class SRC_FID_TO_TARGET>
+ void static op(
+ const FVEC & functionsS,
+ GM_T & gmT,
+ SRC_FID_TO_TARGET & srcFidToTarget,
+ size_t indexInSource
+ ){
+
+ typedef typename GM_T::FunctionTypeList TargetList;
+ typedef opengm::meta::GetIndexInTypeList<TargetList,typename FVEC::value_type> IndexGetter;
+
+ srcFidToTarget[indexInSource] = IndexGetter::value;
+ gmT. template functions<IndexGetter::value>() = functionsS;
+ }
+ };
+
+ template<>
+ struct MaybeCopyFunctionVector<false>{
+
+ template<class FVEC, class GM_T, class SRC_FID_TO_TARGET>
+ void static op(
+ const FVEC & functionsS,
+ GM_T & gmT,
+ SRC_FID_TO_TARGET & srcFidToTarget,
+ size_t indexInSource
+ ){
+ srcFidToTarget[indexInSource] = -1;
+ OPENGM_CHECK_OP(functionsS.size(),==,0,"incompatible functions must have zero size");
+ }
+ };
+
+
+ template<size_t I, size_t DX>
+ struct CopyFunctions{
+
+ template<class GM_S, class GM_T, class SRC_FID_TO_TARGET>
+ void static op(
+ const GM_S & gmS,
+ GM_T & gmT,
+ SRC_FID_TO_TARGET & srcFidToTarget
+ ){
+ //
+ typedef typename GM_S::FunctionTypeList SourceList;
+ typedef typename GM_T::FunctionTypeList TargetList;
+ typedef typename opengm::meta::TypeAtTypeList<SourceList, I>::type FType;
+
+ const std::vector<FType> & functions = gmS. template functions<I>();
+
+ typedef MaybeCopyFunctionVector<opengm::meta::HasTypeInTypeList<TargetList, FType>::value > CopyFVec;
+ CopyFVec::op(functions, gmT, srcFidToTarget, I);
+ // next function type
+ CopyFunctions<I+1, DX>::op(gmS,gmT,srcFidToTarget);
+ }
+ };
+ template<size_t DX>
+ struct CopyFunctions<DX,DX>{
+
+ template<class GM_S, class GM_T, class SRC_FID_TO_TARGET>
+ void static op(
+ const GM_S & gmS,
+ GM_T & gmT,
+ SRC_FID_TO_TARGET & srcFidToTarget
+ ){
+
+ }
+ };
+
+
+
+
+
+
#define OPENGM_BASIC_FUNCTION_WRAPPER_CODE_GENERATOR_MACRO( RETURN_TYPE , FUNCTION_NAME ) \
template<size_t NUMBER_OF_FUNCTIONS> \
template<class GM> \
diff --git a/include/opengm/inference/external/qpbo.hxx b/include/opengm/inference/external/qpbo.hxx
index 01656d4..7148bb9 100644
--- a/include/opengm/inference/external/qpbo.hxx
+++ b/include/opengm/inference/external/qpbo.hxx
@@ -72,8 +72,8 @@ namespace opengm {
Parameter() {
strongPersistency_ = true;
- useImproveing_ = false;
- useProbeing_ = false;
+ useImproveing_ = false;
+ useProbeing_ = false;
}
};
// construction
diff --git a/include/opengm/utilities/metaprogramming.hxx b/include/opengm/utilities/metaprogramming.hxx
index af8b56a..fe5f263 100644
--- a/include/opengm/utilities/metaprogramming.hxx
+++ b/include/opengm/utilities/metaprogramming.hxx
@@ -50,7 +50,9 @@ namespace opengm {
class LinearConstraintFunctionBase;
template <class LINEAR_CONSTRAINT_FUNCTION_TYPE>
class LinearConstraintFunctionTraits;
-
+
+ class LinearConstraintFunctionTraitsUndefined;
+
/// namespace for meta-programming
namespace meta {
/// rebind a templated class with one template argument
@@ -662,7 +664,16 @@ namespace opengm {
};
typedef HasTypeInTypeList< TTAIL,TypeToFind> type;
};
-
+
+ /// metaprogramming has type in typelist metafunction
+ template<class THEAD,class TTAIL>
+ struct HasTypeInTypeList<meta::TypeList<THEAD,TTAIL>,THEAD > : meta::TrueCase{
+ };
+ /// metaprogramming has type in typelist metafunction
+ template<class TypeToFindx>
+ struct HasTypeInTypeList<meta::ListEnd,TypeToFindx> : meta::FalseCase{
+ };
+
/// metaprogramming find type with a certain size in typelist metafunction
template<class TL,class TSL,size_t SIZE,class NOT_FOUND>
struct FindSizedType;
@@ -699,14 +710,70 @@ namespace opengm {
{
typedef OTHER_TL type;
};
- /// metaprogramming has type in typelist metafunction
- template<class THEAD,class TTAIL>
- struct HasTypeInTypeList<meta::TypeList<THEAD,TTAIL>,THEAD > : meta::TrueCase{
- };
- /// metaprogramming has type in typelist metafunction
- template<class TypeToFindx>
- struct HasTypeInTypeList<meta::ListEnd,TypeToFindx> : meta::FalseCase{
- };
+
+
+
+
+
+ template<class TL, class RES_TL>
+ struct RemoveDuplicates;
+
+
+
+ // entry poit
+ template<class TL>
+ struct RemoveDuplicates<TL, meta::ListEnd>{
+
+
+ // get the first type from tl
+ typedef typename TL::HeadType FirstEntry;
+ // rest of type list
+ typedef typename TL::TailType RestOfList;
+
+ typedef typename RemoveDuplicates<
+ RestOfList,
+ meta::TypeList<FirstEntry, meta::ListEnd>
+ >::type type;
+ };
+
+
+
+ template<class RES_TL>
+ struct RemoveDuplicates<meta::ListEnd, RES_TL>{
+ typedef RES_TL type;
+ };
+
+ template<class TL, class RES_TL>
+ struct RemoveDuplicates{
+
+ // get the first type from tl
+ typedef typename TL::HeadType FirstEntry;
+ // rest of type list
+ typedef typename TL::TailType RestOfList;
+
+
+ typedef typename meta::EvalIf<
+ meta::HasTypeInTypeList<RES_TL, FirstEntry>::value,
+ meta::Self<RES_TL>,
+ meta::BackInsert<RES_TL, FirstEntry>
+ >::type ResultTypeList;
+
+ typedef typename RemoveDuplicates<
+ RestOfList,
+ ResultTypeList
+ >::type type;
+ };
+
+
+
+ template<class TL,class OTHER_TL>
+ struct MergeTypeListsNoDuplicates{
+ typedef typename MergeTypeLists<TL, OTHER_TL>::type WithDuplicates;
+ typedef typename RemoveDuplicates<WithDuplicates, ListEnd>::type type;
+ };
+
+
+
/// metaprogramming inserts a type in typelist or move to end metafunction
///
/// back inserts a type in a typelist. If the type has been in the typelist
@@ -1042,20 +1109,10 @@ namespace opengm {
};
};
- // metaprogramming check if T is complete type
+ // metaprogramming check if T is a valid trait
template <class T>
- struct IsCompleteType {
- typedef char yes[1];
- typedef char no[2];
-
- template <class T1>
- static yes& test(int(*)[sizeof(T1)]);
- template <class T1>
- static no& test(...);
-
- enum Value{
- value = (sizeof(test<T>(0)) == sizeof(yes))
- };
+ struct IsValidTrait {
+ static const bool value = !Compare<typename T::ValueType, LinearConstraintFunctionTraitsUndefined>::value;
};
// constraint function typelist
// metaprogramming get linear constraint function typelist
@@ -1079,7 +1136,7 @@ namespace opengm {
struct IsLinearConstraintFunction<FUNCTION, true> {
typedef typename If<IsBaseOf<opengm::LinearConstraintFunctionBase<FUNCTION>, FUNCTION>::value, true_type, false_type>::type type;
};
- typedef IsCompleteType<opengm::LinearConstraintFunctionTraits<THEAD> > IsCompleteLinearConstraintFunction;
+ typedef IsValidTrait<opengm::LinearConstraintFunctionTraits<THEAD> > IsCompleteLinearConstraintFunction;
// add THEAD only if it is derived from LinearConstraintBase<THEAD>
typedef typename IsLinearConstraintFunction<THEAD, IsCompleteLinearConstraintFunction::value>::type type;
};
diff --git a/src/unittest/CMakeLists.txt b/src/unittest/CMakeLists.txt
index d99f00a..1bb2387 100644
--- a/src/unittest/CMakeLists.txt
+++ b/src/unittest/CMakeLists.txt
@@ -29,7 +29,10 @@ if(BUILD_TESTING)
add_test(test-fast-sequence ${CMAKE_CURRENT_BINARY_DIR}/test-fast-sequence)
add_executable(test-functions test_functions.cxx ${headers})
- add_test(test-functions ${CMAKE_CURRENT_BINARY_DIR}/test-functions)
+ add_test(test-functions ${CMAKE_CURRENT_BINARY_DIR}/test-functions)
+
+ add_executable(test-learnable-functions test_learnable_functions.cxx ${headers})
+ add_test(test-learnable-functions ${CMAKE_CURRENT_BINARY_DIR}/test-learnable-functions)
add_executable(test-factor test_factor.cxx ${headers})
add_test(test-factor ${CMAKE_CURRENT_BINARY_DIR}/test-factor)
diff --git a/src/unittest/test_gm_learning_functions.cxx b/src/unittest/test_gm_learning_functions.cxx
index 4c99ca6..b797175 100644
--- a/src/unittest/test_gm_learning_functions.cxx
+++ b/src/unittest/test_gm_learning_functions.cxx
@@ -1,7 +1,6 @@
#include <vector>
#include <opengm/functions/explicit_function.hxx>
-#include <opengm/functions/l_potts.hxx>
#include <opengm/functions/potts.hxx>
#include <opengm/functions/pottsn.hxx>
#include <opengm/unittests/test.hxx>
@@ -9,7 +8,7 @@
#include <opengm/operations/multiplier.hxx>
#include <opengm/inference/bruteforce.hxx>
#include <opengm/utilities/metaprogramming.hxx>
-
+#include <opengm/functions/learnable/lpotts.hxx>
struct TestFunctor{
@@ -41,8 +40,8 @@ struct GraphicalModelTest {
ValueType, //value type (should be float double or long double)
opengm::Multiplier, //operator (something like Adder or Multiplier)
typename opengm::meta::TypeListGenerator<
- opengm::ExplicitFunction<ValueType,I,L>,
- opengm::PottsNFunction<ValueType,I,L>
+ opengm::ExplicitFunction<ValueType,I,L>,
+ opengm::PottsNFunction<ValueType,I,L>
>::type, //implicit function functor
opengm::DiscreteSpace<I, L>
> GraphicalModelType;
@@ -55,14 +54,14 @@ struct GraphicalModelTest {
typedef typename opengm::meta::TypeListGenerator
<
opengm::ExplicitFunction<T,I,L>,
- opengm::LPottsFunction<T,I,L>
+ opengm::functions::learnable::LPotts<T,I,L>
>::type FunctionTypeList;
typedef opengm::GraphicalModel<T, opengm::Minimizer, FunctionTypeList, opengm::DiscreteSpace<I, L> > GmType;
typedef typename GmType::FunctionIdentifier Fid;
typedef opengm::ExplicitFunction<T,I,L> EF;
- typedef opengm::LPottsFunction<T,I,L> LPF;
+ typedef opengm::functions::learnable::LPotts<T,I,L> LPF;
// graphical model
@@ -70,10 +69,12 @@ struct GraphicalModelTest {
GmType gmA(opengm::DiscreteSpace<I, L > (nos, nos + 3));
// parameter
- const size_t numparam = 1;
- opengm::Parameters<T,I> param(numparam);
- param.setParameter(0,5.0);
- LPF lPotts(2,2,param,0);
+ const size_t numweights = 1;
+ opengm::learning::Weights<T> weights(numweights);
+ weights.setWeight(0,5.0);
+ std::vector<size_t> weightIds(1, 0);
+ std::vector<T> features(1, 1.0);
+ LPF lPotts(weights,2,weightIds, features);
I labels00[2]={0,0};
@@ -87,7 +88,7 @@ struct GraphicalModelTest {
OPENGM_ASSERT_OP(lPotts(labels10),<,5.01);
- param.setParameter(0,3.0);
+ weights.setWeight(0,3.0);
OPENGM_ASSERT_OP(lPotts(labels01),>,2.99);
OPENGM_ASSERT_OP(lPotts(labels01),<,3.01);
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/debian-science/packages/opengm.git
More information about the debian-science-commits
mailing list