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<
82 NrOfFunctionTypes = meta::LengthOfTypeList<FunctionTypeList>::value
93 GraphicalModel(
const SpaceType& ,
const size_t reserveFactorsPerVariable=0);
96 template<
class OTHER_TL>
102 const SpaceType& space()
const;
103 IndexType numberOfVariables()
const;
104 IndexType numberOfVariables(
const IndexType)
const;
105 IndexType numberOfLabels(
const IndexType)
const;
106 IndexType numberOfFunctions(
const size_t)
const;
107 IndexType numberOfFactors()
const;
108 IndexType numberOfFactors(
const IndexType)
const;
109 IndexType variableOfFactor(
const IndexType,
const IndexType)
const;
110 IndexType factorOfVariable(
const IndexType,
const IndexType)
const;
111 const FactorType& operator[](
const IndexType)
const;
112 template<
class ITERATOR>
113 ValueType evaluate(ITERATOR)
const;
115 template<
class ITERATOR>
116 bool isValidIndexSequence(ITERATOR, ITERATOR)
const;
118 size_t factorOrder()
const;
120 void assign(
const SpaceType& );
121 IndexType addVariable(
const LabelType);
122 template<
class FUNCTION_TYPE>
123 FunctionIdentifier addFunction(
const FUNCTION_TYPE&);
124 template<
class FUNCTION_TYPE>
125 std::pair<FunctionIdentifier,FUNCTION_TYPE &> addFunctionWithRefReturn(
const FUNCTION_TYPE&);
126 template<
class FUNCTION_TYPE>
127 FunctionIdentifier addSharedFunction(
const FUNCTION_TYPE&);
128 template<
class FUNCTION_TYPE>
129 FUNCTION_TYPE& getFunction(
const FunctionIdentifier&);
130 template<
class ITERATOR>
131 IndexType addFactor(
const FunctionIdentifier&, ITERATOR, ITERATOR);
133 template<
class ITERATOR>
134 IndexType addFactorNonFinalized(
const FunctionIdentifier&, ITERATOR, ITERATOR);
139 template <
class FUNCTION_TYPE>
142 meta::GetIndexInTypeList<
147 this->
template functions<TLIndex::value>().reserve(numF);
151 factors_.reserve(numF);
155 factorsVis_.reserve(size);
161 for(
size_t i=0; i<numberOfVariables(); ++i)
162 mx = std::max(numberOfLabels(i), mx);
179 template<
size_t FUNCTION_INDEX>
180 const std::vector<typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_INDEX>::type>& functions()
const;
181 template<
size_t FUNCTION_INDEX>
182 std::vector<typename meta::TypeAtTypeList<FunctionTypeList, FUNCTION_INDEX>::type>& functions();
186 meta::Field<FunctionTypeList, detail_graphical_model::FunctionDataUnit> functionDataField_;
187 std::vector<RandomAccessSet<IndexType> > variableFactorAdjaceny_;
188 std::vector<FactorType> factors_;
189 std::vector<IndexType> factorsVis_;
194 friend struct detail_graphical_model::FunctionWrapper;
195 template<
size_t,
size_t ,
bool>
196 friend struct detail_graphical_model::FunctionWrapperExecutor;
197 template<
typename GM>
199 template<
typename GM>
202 template<
class ,
size_t ,
size_t ,
bool>
204 template<
typename,
typename>
205 friend struct GraphicalModelEqualityTest;
206 template<
typename,
typename,
typename >
210 template<
typename,
typename,
typename ,
typename >
212 template <
size_t ,
size_t,
bool >
213 friend struct opengm::functionwrapper::executor::FactorInvariant;
214 template<
unsigned int I,
unsigned int D,
bool END>
222 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
224 typedef FUNCTION_INDEX_TYPE FunctionIndexType;
226 typedef FUNCTION_TYPE_INDEX_TYPE FunctionTypeIndexType;
228 FunctionIdentification(
const FunctionIndexType=FunctionIndexType(0),
const FunctionTypeIndexType=FunctionTypeIndexType(0));
235 FunctionTypeIndexType getFunctionType()
const{
return functionType;};
236 FunctionIndexType getFunctionIndex()
const{
return functionIndex;};
238 FunctionIndexType functionIndex;
239 FunctionTypeIndexType functionType;
245 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
249 const IndexType variableIndex
252 return variableFactorAdjaceny_[variableIndex].size();
257 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
261 const IndexType factorIndex
265 return factors_[factorIndex].numberOfVariables();
269 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
273 const size_t functionTypeIndex
276 typedef meta::SizeT<GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::NrOfFunctionTypes> NoFt;
277 return detail_graphical_model::FunctionWrapper<NoFt::value>::numberOfFunctions(
this, functionTypeIndex);
282 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
286 const IndexType factorIndex,
287 const IndexType variableNumber
291 OPENGM_ASSERT(variableNumber < numberOfVariables(factorIndex));
292 return factors_[factorIndex].variableIndex(variableNumber);
297 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
301 const IndexType variableIndex,
302 const IndexType factorNumber
306 OPENGM_ASSERT(factorNumber < numberOfFactors(variableIndex));
307 return variableFactorAdjaceny_[variableIndex][factorNumber];
310 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
313 functionDataField_(),
314 variableFactorAdjaceny_(),
322 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
328 functionDataField_(gm.functionDataField_),
329 variableFactorAdjaceny_(gm.variableFactorAdjaceny_),
331 factorsVis_(gm.factorsVis_),
334 for(
size_t i = 0; i<this->factors_.size(); ++i) {
335 factors_[i].gm_=
this;
336 factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
337 factors_[i].functionTypeId_=gm.factors_[i].functionTypeId_;
340 factors_[i].vis_=gm.factors_[i].vis_;
341 factors_[i].vis_.assignPtr(this->factorsVis_);
350 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
355 const size_t reserveFactorsPerVariable
358 functionDataField_(),
359 variableFactorAdjaceny_(space.numberOfVariables()),
363 if(reserveFactorsPerVariable==0){
364 variableFactorAdjaceny_.resize(space.numberOfVariables());
368 reservedSet.
reserve(reserveFactorsPerVariable);
369 variableFactorAdjaceny_.resize(space.numberOfVariables(),reservedSet);
375 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
384 return space_.numberOfVariables() - 1;
388 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
399 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
407 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
411 const IndexType index
415 return space_.numberOfLabels(index);
419 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
423 const IndexType index
427 return factors_[index];
430 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
434 return this->factors_.size();
438 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
447 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
448 template<
class ITERATOR>
457 std::vector<LabelType> factor_state(factorOrder()+1);
458 OperatorType::neutral(v);
459 for(
size_t j = 0; j < factors_.size(); ++j) {
466 for(
size_t i = 0; i < factors_[j].numberOfVariables(); ++i) {
469 factor_state[i] = labels[factors_[j].variableIndex(i)];
471 OperatorType::op(factors_[j](factor_state.begin()), v);
478 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
479 template<
class ITERATOR>
487 ITERATOR previousIt = begin;
488 while(begin != end) {
489 if(*begin >= this->numberOfVariables()) {
492 if(previousIt != begin && *previousIt >= *begin) {
502 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
506 for(
size_t i = 0; i < numberOfFactors(); i++) {
525 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
526 template<
class FUNCTION_TYPE>
530 const FUNCTION_TYPE&
function 535 meta::GetIndexInTypeList<
540 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
543 functionIdentifier.functionType = TLIndex::value;
544 const size_t functionIndex=this->
template functions<TLIndex::value>().size();
545 functionIdentifier.functionIndex = functionIndex;
546 this->
template functions<TLIndex::value>().push_back(
function);
547 OPENGM_ASSERT(functionIndex==this->
template functions<TLIndex::value>().size()-1);
549 return functionIdentifier;
553 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
554 template<
class FUNCTION_TYPE>
555 inline std::pair<typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionIdentifier,FUNCTION_TYPE &>
558 const FUNCTION_TYPE&
function 562 meta::GetIndexInTypeList<
567 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
570 functionIdentifier.functionType = TLIndex::value;
571 const size_t functionIndex=this->
template functions<TLIndex::value>().size();
572 functionIdentifier.functionIndex = functionIndex;
573 this->
template functions<TLIndex::value>().push_back(
function);
574 OPENGM_ASSERT(functionIndex==this->
template functions<TLIndex::value>().size()-1);
576 std::pair<FunctionIdentifier,FUNCTION_TYPE &> fidFunction(functionIdentifier,this->
template functions<TLIndex::value>().back());
584 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
585 template<
class FUNCTION_TYPE>
589 const FUNCTION_TYPE&
function 595 meta::GetIndexInTypeList<
600 typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
603 functionIdentifier.functionType = TLIndex::value;
605 for(
size_t i=0;i<this->
template functions<TLIndex::value>().size();++i) {
606 if(
function == this->
template functions<TLIndex::value>()[i]) {
607 functionIdentifier.functionIndex =
static_cast<IndexType
>(i);
608 OPENGM_ASSERT(
function==this->
template functions<TLIndex::value>()[functionIdentifier.functionIndex]);
609 return functionIdentifier;
612 functionIdentifier.functionIndex = this->
template functions<TLIndex::value>().size();
613 this->
template functions<TLIndex::value>().push_back(
function);
614 OPENGM_ASSERT(functionIdentifier.functionIndex==this->
template functions<TLIndex::value>().size()-1);
616 return functionIdentifier;
634 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
635 template<
class FUNCTION_TYPE>
643 meta::GetIndexInTypeList<
648 return this->
template functions<TLIndex::value>()[fid.getFunctionIndex()];
658 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
659 template<
class ITERATOR>
669 const IndexType indexInVisVector = factorsVis_.size();
670 IndexType factorOrder = 0;
672 factorsVis_.push_back(*begin);
676 order_ = std::max(order_,factorOrder);
680 const IndexType factorIndex = this->factors_.size();
681 this->factors_.push_back(
FactorType(
this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
682 for(
size_t i=0;i<factors_.back().numberOfVariables();++i) {
686 "variable indices of a factor must be sorted");
689 "variable indices of a factor must smaller than gm.numberOfVariables()");
690 this->variableFactorAdjaceny_[factor.
variableIndex(i)].insert(factorIndex);
709 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
710 template<
class ITERATOR>
721 const IndexType indexInVisVector = factorsVis_.size();
722 IndexType factorOrder = 0;
724 factorsVis_.push_back(*begin);
728 order_ = std::max(order_,factorOrder);
733 const IndexType factorIndex = this->factors_.size();
734 this->factors_.push_back(
FactorType(
this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
736 for(
size_t i=0;i<factors_.back().numberOfVariables();++i) {
740 "variable indices of a factor must be sorted");
743 "variable indices of a factor must smaller than gm.numberOfVariables()");
753 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
757 std::vector<std::set<IndexType> > variableFactorAdjaceny(this->numberOfVariables());
758 for(IndexType fi=0; fi < this->numberOfFactors();++fi){
762 for(IndexType v=0;v<numVar;++v){
764 variableFactorAdjaceny[vi].insert(fi);
768 for(IndexType vi=0;vi<this->numberOfVariables();++vi){
769 this->variableFactorAdjaceny_[vi].assignFromSet(variableFactorAdjaceny[vi]);
774 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
781 this->space_ = gm.space_;
782 this->functionDataField_=gm.functionDataField_;
783 this->factors_.resize(gm.factors_.size());
784 this->variableFactorAdjaceny_=gm.variableFactorAdjaceny_;
785 this->factorsVis_ = gm.factorsVis_;
786 this->order_ = gm.order_;
787 for(
size_t i = 0; i<this->factors_.size(); ++i) {
788 factors_[i].gm_=
this;
789 factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
790 factors_[i].functionTypeId_=gm.factors_[i].functionTypeId_;
791 factors_[i].vis_=gm.factors_[i].vis_;
792 factors_[i].vis_.assignPtr(this->factorsVis_);
801 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
802 template<
class OTHER_TL>
804 GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::operator=
810 this->space_ = gm.space_;
816 std::vector<int> srcFidToTarget(OtherGm::NrOfFunctionTypes,-1);
817 detail_graphical_model::CopyFunctions<0, OtherGm::NrOfFunctionTypes >::op(gm, *
this,srcFidToTarget);
819 this->factors_.resize(gm.factors_.size());
820 this->variableFactorAdjaceny_=gm.variableFactorAdjaceny_;
821 this->factorsVis_ = gm.factorsVis_;
822 this->order_ = gm.order_;
824 for(
size_t i = 0; i<this->factors_.size(); ++i) {
825 factors_[i].gm_=
this;
826 factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
828 int newFidFunctionId = srcFidToTarget[gm.factors_[i].functionTypeId_];
830 factors_[i].functionTypeId_= newFidFunctionId;
831 factors_[i].vis_=gm.factors_[i].vis_;
832 factors_[i].vis_.assignPtr(this->factorsVis_);
837 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
838 template<
size_t FUNCTION_INDEX>
840 typename meta::TypeAtTypeList<
846 return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
847 (this->functionDataField_).functionData_.functions_;
850 template<
class T,
class OPERATOR,
class FUNCTION_TYPE_LIST,
class SPACE>
851 template<
size_t FUNCTION_INDEX>
853 typename meta::TypeAtTypeList<
854 typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionTypeList,
860 return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
861 (this->functionDataField_).functionData_.functions_;
866 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
870 const FUNCTION_INDEX_TYPE functionIndex,
871 const FUNCTION_TYPE_INDEX_TYPE functionType
873 : functionIndex(functionIndex),
874 functionType(functionType)
877 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
884 if(functionType < rhs.functionType)
887 return functionIndex < rhs.functionIndex;
890 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
897 if(functionType >rhs.functionType)
900 return functionIndex > rhs.functionIndex;
903 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
910 if(functionType <= rhs.functionType)
913 return functionIndex <= rhs.functionIndex;
916 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
918 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >=
923 if(functionType >=rhs.functionType)
926 return functionIndex >= rhs.functionIndex;
929 template<
class FUNCTION_INDEX_TYPE,
class FUNCTION_TYPE_INDEX_TYPE>
931 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator ==
936 return (functionType == rhs.functionType) && (functionIndex == rhs.functionIndex);
940 namespace detail_graphical_model {
941 template<
class FUNCTION_TYPE>
942 struct FunctionData {
943 std::vector<FUNCTION_TYPE> functions_;
951 template<
class FUNCTION_TYPE>
952 struct FunctionDataUnit{
953 FunctionData<FUNCTION_TYPE> functionData_;
965 #endif // #ifndef OPENGM_GRAPHICALMODEL_HXX IndexType numberOfFunctions(const size_t) const
return the number of functions of a specific type
IndexType numberOfFactors() const
bool operator>=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
IndexType variableOfFactor(const IndexType, const IndexType) const
return the k-th variable of the j-th factor
LabelType maxNumberOfLabels() const
Discrete space in which variables can have differently many labels.
Factor (with corresponding function and variable indices), independent of a GraphicalModel.
FunctionIdentification< IndexType, UInt8Type > FunctionIdentifier
IndexType variableIndex(const IndexType) const
return the index of the j-th variable
size_t factorOrder() const
return the maximum of the orders of all factors
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...
void load(GM_ &gm, const std::string &, const std::string &)
IndexType numberOfVariables() const
void reserveFactors(const size_t numF)
bool operator==(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
#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
IndexType factorOfVariable(const IndexType, const IndexType) const
return the k-th factor connected to the j-th variable
#define OPENGM_META_ASSERT(assertion, msg)
opengm compile time assertion
GraphicalModel< T, OPERATOR, FUNCTION_TYPE_LIST, SPACE > GraphicalModelType
Abstraction (wrapper class) of factors, independent of the function used to implement the factor...
bool operator<=(const IndicatorVariable< INDEX1_TYPE, LABEL1_TYPE > &indicatorVar1, const IndicatorVariable< INDEX2_TYPE, LABEL2_TYPE > &indicatorVar2)
Function encoded as a dense multi-dimensional array, marray::Marray.
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 addVariable(const LabelType)
add a new variable to the graphical model and underlying label space
void reserve(const size_t size)
IndexType numberOfVariables() const
#define OPENGM_CHECK_OP(A, OP, B, TXT)
IndependentFactor< ValueType, IndexType, LabelType > IndependentFactorType
SpaceType::LabelType LabelType
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)