2 #ifndef OPENGM_GRAPHICALMODEL_HXX
3 #define OPENGM_GRAPHICALMODEL_HXX
27 void save(
const GM&,
const std::string&,
const std::string&);
29 void load(GM_& gm,
const std::string&,
const std::string&);
30 template<
class,
size_t,
size_t,
bool>
34 template<
unsigned int I,
unsigned int D,
bool END>
38 namespace detail_graphical_model {
39 template<
class FUNCTION_TYPE>
42 template<
class FUNCTION_TYPE>
43 struct FunctionDataUnit;
47 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
60 class FUNCTION_TYPE_LIST = meta::TypeList<ExplicitFunction<T>, meta::ListEnd>,
65 GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>,
66 typename SPACE::IndexType
76 typedef typename meta::GenerateFunctionTypeList<
93 GraphicalModel(
const SpaceType& ,
const size_t reserveFactorsPerVariable=0);
96 const SpaceType&
space()
const;
105 const FactorType&
operator[](
const IndexType)
const;
106 template<
class ITERATOR>
109 template<
class ITERATOR>
110 bool isValidIndexSequence(ITERATOR, ITERATOR)
const;
114 void assign(
const SpaceType& );
116 template<
class FUNCTION_TYPE>
117 FunctionIdentifier
addFunction(
const FUNCTION_TYPE&);
118 template<
class FUNCTION_TYPE>
120 template<
class FUNCTION_TYPE>
122 template<
class FUNCTION_TYPE>
123 FUNCTION_TYPE&
getFunction(
const FunctionIdentifier&);
124 template<
class ITERATOR>
125 IndexType
addFactor(
const FunctionIdentifier&, ITERATOR, ITERATOR);
127 template<
class ITERATOR>
133 template <
class FUNCTION_TYPE>
136 meta::GetIndexInTypeList<
141 this->
template functions<TLIndex::value>().reserve(numF);
145 factors_.reserve(numF);
149 factorsVis_.reserve(size);
173 template<
size_t FUNCTION_INDEX>
174 const std::vector<typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_INDEX>::type>&
functions()
const;
175 template<
size_t FUNCTION_INDEX>
176 std::vector<typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_INDEX>::type>&
functions();
180 meta::Field<FunctionTypeList, detail_graphical_model::FunctionDataUnit> functionDataField_;
181 std::vector<RandomAccessSet<IndexType> > variableFactorAdjaceny_;
182 std::vector<FactorType> factors_;
183 std::vector<IndexType> factorsVis_;
188 friend struct detail_graphical_model::FunctionWrapper;
189 template<
size_t,
size_t ,
bool>
190 friend struct detail_graphical_model::FunctionWrapperExecutor;
191 template<
typename GM>
193 template<
typename GM>
196 template<
class ,
size_t ,
size_t ,
bool>
198 template<
typename,
typename>
200 template<
typename,
typename,
typename >
204 template<
typename,
typename,
typename ,
typename >
206 template <
size_t ,
size_t,
bool >
207 friend struct opengm::functionwrapper::executor::FactorInvariant;
208 template<
unsigned int I,
unsigned int D,
bool END>
216 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
218 typedef FUNCTION_INDEX_TYPE FunctionIndexType;
219 typedef FunctionIndexType IndexType;
220 typedef FUNCTION_TYPE_INDEX_TYPE FunctionTypeIndexType;
222 FunctionIdentification(
const FunctionIndexType=FunctionIndexType(0),
const FunctionTypeIndexType=FunctionTypeIndexType(0));
229 FunctionTypeIndexType getFunctionType()
const{
return functionType;};
230 FunctionIndexType getFunctionIndex()
const{
return functionIndex;};
232 FunctionIndexType functionIndex;
233 FunctionTypeIndexType functionType;
239 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
240 inline typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::IndexType
243 const IndexType variableIndex
246 return variableFactorAdjaceny_[variableIndex].size();
251 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
255 const IndexType factorIndex
259 return factors_[factorIndex].numberOfVariables();
263 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
267 const size_t functionTypeIndex
270 typedef meta::SizeT<GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::NrOfFunctionTypes> NoFt;
271 return detail_graphical_model::FunctionWrapper<NoFt::value>::numberOfFunctions(
this, functionTypeIndex);
276 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
280 const IndexType factorIndex,
281 const IndexType variableNumber
285 OPENGM_ASSERT(variableNumber < numberOfVariables(factorIndex));
286 return factors_[factorIndex].variableIndex(variableNumber);
291 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
295 const IndexType variableIndex,
296 const IndexType factorNumber
300 OPENGM_ASSERT(factorNumber < numberOfFactors(variableIndex));
301 return variableFactorAdjaceny_[variableIndex][factorNumber];
304 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
307 functionDataField_(),
308 variableFactorAdjaceny_(),
316 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
322 functionDataField_(gm.functionDataField_),
323 variableFactorAdjaceny_(gm.variableFactorAdjaceny_),
325 factorsVis_(gm.factorsVis_),
328 for(
size_t i = 0; i<this->factors_.size(); ++i) {
329 factors_[i].gm_=
this;
330 factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
331 factors_[i].functionTypeId_=gm.factors_[i].functionTypeId_;
334 factors_[i].vis_=gm.factors_[i].vis_;
335 factors_[i].vis_.assignPtr(this->factorsVis_);
344 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
349 const size_t reserveFactorsPerVariable
352 functionDataField_(),
353 variableFactorAdjaceny_(space.numberOfVariables()),
357 if(reserveFactorsPerVariable==0){
358 variableFactorAdjaceny_.resize(space.numberOfVariables());
362 reservedSet.
reserve(reserveFactorsPerVariable);
363 variableFactorAdjaceny_.resize(space.numberOfVariables(),reservedSet);
369 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
373 const IndexType nLabels
376 space_.addVariable(nLabels);
378 return space_.numberOfVariables() - 1;
382 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
393 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
397 return space_.numberOfVariables();
401 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
405 const IndexType index
409 return space_.numberOfLabels(index);
413 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
417 const IndexType index
421 return factors_[index];
424 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
428 return this->factors_.size();
432 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
441 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
442 template<
class ITERATOR>
451 std::vector<LabelType> factor_state(factorOrder()+1);
452 OperatorType::neutral(v);
453 for(
size_t j = 0; j < factors_.size(); ++j) {
460 for(
size_t i = 0; i < factors_[j].numberOfVariables(); ++i) {
463 factor_state[i] = labels[factors_[j].variableIndex(i)];
465 OperatorType::op(factors_[j](factor_state.begin()), v);
472 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
473 template<
class ITERATOR>
481 ITERATOR previousIt = begin;
482 while(begin != end) {
483 if(*begin >= this->numberOfVariables()) {
486 if(previousIt != begin && *previousIt >= *begin) {
496 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
500 for(
size_t i = 0; i < numberOfFactors(); i++) {
519 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
520 template<
class FUNCTION_TYPE>
524 const FUNCTION_TYPE&
function
529 meta::GetIndexInTypeList<
534 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
537 functionIdentifier.functionType = TLIndex::value;
538 const size_t functionIndex=this->
template functions<TLIndex::value>().size();
539 functionIdentifier.functionIndex = functionIndex;
540 this->
template functions<TLIndex::value>().push_back(
function);
541 OPENGM_ASSERT(functionIndex==this->
template functions<TLIndex::value>().size()-1);
543 return functionIdentifier;
547 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
548 template<
class FUNCTION_TYPE>
549 inline std::pair<typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionIdentifier,FUNCTION_TYPE &>
552 const FUNCTION_TYPE&
function
556 meta::GetIndexInTypeList<
561 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
564 functionIdentifier.functionType = TLIndex::value;
565 const size_t functionIndex=this->
template functions<TLIndex::value>().size();
566 functionIdentifier.functionIndex = functionIndex;
567 this->
template functions<TLIndex::value>().push_back(
function);
568 OPENGM_ASSERT(functionIndex==this->
template functions<TLIndex::value>().size()-1);
570 std::pair<FunctionIdentifier,FUNCTION_TYPE &> fidFunction(functionIdentifier,this->
template functions<TLIndex::value>().back());
578 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
579 template<
class FUNCTION_TYPE>
583 const FUNCTION_TYPE&
function
589 meta::GetIndexInTypeList<
594 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
597 functionIdentifier.functionType = TLIndex::value;
599 for(
size_t i=0;i<this->
template functions<TLIndex::value>().size();++i) {
600 if(
function == this->
template functions<TLIndex::value>()[i]) {
601 functionIdentifier.functionIndex =
static_cast<IndexType
>(i);
602 OPENGM_ASSERT(
function==this->
template functions<TLIndex::value>()[functionIdentifier.functionIndex]);
603 return functionIdentifier;
606 functionIdentifier.functionIndex = this->
template functions<TLIndex::value>().size();
607 this->
template functions<TLIndex::value>().push_back(
function);
608 OPENGM_ASSERT(functionIdentifier.functionIndex==this->
template functions<TLIndex::value>().size()-1);
610 return functionIdentifier;
628 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
629 template<
class FUNCTION_TYPE>
637 meta::GetIndexInTypeList<
642 return this->
template functions<TLIndex::value>()[fid.getFunctionIndex()];
652 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
653 template<
class ITERATOR>
663 const IndexType indexInVisVector = factorsVis_.size();
664 IndexType factorOrder = 0;
666 factorsVis_.push_back(*begin);
670 order_ = std::max(order_,factorOrder);
674 const IndexType factorIndex = this->factors_.size();
675 this->factors_.push_back(
FactorType(
this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
676 for(
size_t i=0;i<factors_.back().numberOfVariables();++i) {
680 "variable indices of a factor must be sorted");
683 "variable indices of a factor must smaller than gm.numberOfVariables()");
684 this->variableFactorAdjaceny_[factor.
variableIndex(i)].insert(factorIndex);
703 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
704 template<
class ITERATOR>
715 const IndexType indexInVisVector = factorsVis_.size();
716 IndexType factorOrder = 0;
718 factorsVis_.push_back(*begin);
722 order_ = std::max(order_,factorOrder);
727 const IndexType factorIndex = this->factors_.size();
728 this->factors_.push_back(
FactorType(
this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
730 for(
size_t i=0;i<factors_.back().numberOfVariables();++i) {
734 "variable indices of a factor must be sorted");
737 "variable indices of a factor must smaller than gm.numberOfVariables()");
747 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
751 std::vector<std::set<IndexType> > variableFactorAdjaceny(this->numberOfVariables());
752 for(IndexType fi=0; fi < this->numberOfFactors();++fi){
756 for(IndexType v=0;v<numVar;++v){
758 variableFactorAdjaceny[vi].insert(fi);
762 for(IndexType vi=0;vi<this->numberOfVariables();++vi){
763 this->variableFactorAdjaceny_[vi].assignFromSet(variableFactorAdjaceny[vi]);
768 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
775 this->space_ = gm.space_;
776 this->functionDataField_=gm.functionDataField_;
777 this->factors_.resize(gm.factors_.size());
778 this->variableFactorAdjaceny_=gm.variableFactorAdjaceny_;
779 this->factorsVis_ = gm.factorsVis_;
780 this->order_ = gm.order_;
781 for(
size_t i = 0; i<this->factors_.size(); ++i) {
782 factors_[i].gm_=
this;
783 factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
784 factors_[i].functionTypeId_=gm.factors_[i].functionTypeId_;
785 factors_[i].vis_=gm.factors_[i].vis_;
786 factors_[i].vis_.assignPtr(this->factorsVis_);
795 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
796 template<
size_t FUNCTION_INDEX>
798 typename meta::TypeAtTypeList<
804 return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
805 (this->functionDataField_).functionData_.functions_;
808 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
809 template<
size_t FUNCTION_INDEX>
811 typename meta::TypeAtTypeList<
812 typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionTypeList,
818 return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
819 (this->functionDataField_).functionData_.functions_;
824 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
828 const FUNCTION_INDEX_TYPE functionIndex,
829 const FUNCTION_TYPE_INDEX_TYPE functionType
831 : functionIndex(functionIndex),
832 functionType(functionType)
835 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
837 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator <
839 const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
842 if(functionType < rhs.functionType)
845 return functionIndex < rhs.functionIndex;
848 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
850 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >
852 const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
855 if(functionType >rhs.functionType)
858 return functionIndex > rhs.functionIndex;
861 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
863 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator <=
865 const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
868 if(functionType <= rhs.functionType)
871 return functionIndex <= rhs.functionIndex;
874 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
876 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >=
878 const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
881 if(functionType >=rhs.functionType)
884 return functionIndex >= rhs.functionIndex;
887 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
889 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator ==
891 const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
894 return (functionType == rhs.functionType) && (functionIndex == rhs.functionIndex);
898 namespace detail_graphical_model {
899 template<
class FUNCTION_TYPE>
900 struct FunctionData {
901 std::vector<FUNCTION_TYPE> functions_;
909 template<
class FUNCTION_TYPE>
910 struct FunctionDataUnit{
911 FunctionData<FUNCTION_TYPE> functionData_;
923 #endif // #ifndef OPENGM_GRAPHICALMODEL_HXX
bool operator>=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
FUNCTION_TYPE & getFunction(const FunctionIdentifier &)
access functions
IndexType numberOfFunctions(const size_t) const
return the number of functions of a specific type
IndexType addFactor(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
Discrete space in which variables can have differently many labels.
Factor (with corresponding function and variable indices), independent of a GraphicalModel.
const FactorType & operator[](const IndexType) const
access a factor of the graphical model
FunctionIdentification< IndexType, UInt8Type > FunctionIdentifier
FunctionIdentifier addFunction(const FUNCTION_TYPE &)
add a function to the graphical model
const std::vector< typename meta::TypeAtTypeList< FunctionTypeList, FUNCTION_INDEX >::type > & functions() const
ExplicitStorage (continous storage) of a graphical model function data.
Interface that makes an object of type S (the template parameter) look like a (non-editable) factor g...
ValueType evaluate(ITERATOR) const
evaluate the modeled function for a given labeling
void load(GM_ &gm, const std::string &, const std::string &)
void reserveFactors(const size_t numF)
IndexType factorOfVariable(const IndexType, const IndexType) const
return the k-th factor connected to the j-th variable
GraphicalModel & operator=(const GraphicalModel &)
bool operator==(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
IndexType addFactorNonFinalized(const FunctionIdentifier &, ITERATOR, ITERATOR)
add a factor to the graphical model
LabelType maxNumberOfLabels() const
#define OPENGM_ASSERT(expression)
meta::GenerateFunctionTypeList< FUNCTION_TYPE_LIST, ExplicitFunction< T, IndexType, LabelType >, false >::type FunctionTypeList
Factor< GraphicalModel< T, OPERATOR, FUNCTION_TYPE_LIST, SPACE > > FactorType
#define OPENGM_META_ASSERT(assertion, msg)
opengm compile time assertion
GraphicalModel< T, OPERATOR, FUNCTION_TYPE_LIST, SPACE > GraphicalModelType
FunctionIdentifier addSharedFunction(const FUNCTION_TYPE &)
add a function to the graphical model avoiding duplicates (requires search)
Abstraction (wrapper class) of factors, independent of the function used to implement the factor...
IndexType numberOfVariables() const
bool operator<=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
size_t factorOrder() const
return the maximum of the orders of all factors
Function encoded as a dense multi-dimensional array, marray::Marray.
const SpaceType & space() const
return the label space underlying the graphical model
void reserveFactorsVarialbeIndices(const size_t size)
bool operator>(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
void reserveFunctions(const size_t numF)
SpaceType::IndexType IndexType
IndexType numberOfLabels(const IndexType) const
return the number of labels of an indicated variable
IndexType addVariable(const IndexType)
add a new variable to the graphical model and underlying label space
void reserve(const size_t size)
IndexType variableIndex(const IndexType) const
return the index of the j-th variable
void assign(const SpaceType &)
clear the graphical model and construct a new one based on a label space
IndexType variableOfFactor(const IndexType, const IndexType) const
return the k-th variable of the j-th factor
#define OPENGM_CHECK_OP(A, OP, B, TXT)
IndependentFactor< ValueType, IndexType, LabelType > IndependentFactorType
friend struct GraphicalModelEqualityTest
IndexType numberOfVariables() const
std::pair< FunctionIdentifier, FUNCTION_TYPE & > addFunctionWithRefReturn(const FUNCTION_TYPE &)
SpaceType::LabelType LabelType
IndexType numberOfFactors() const
void save(const GM &, const std::string &, const std::string &)
save a graphical model to an HDF5 file
bool operator<(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)