OpenGM  2.3.x
Discrete Graphical Model Library
graphicalmodel.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_GRAPHICALMODEL_HXX
3 #define OPENGM_GRAPHICALMODEL_HXX
4 
5 #include <exception>
6 #include <set>
7 #include <vector>
8 #include <queue>
9 #include <string>
10 
11 #include "opengm/opengm.hxx"
22 
23 namespace opengm {
24 
25 namespace hdf5 {
26  template<class GM>
27  void save(const GM&, const std::string&, const std::string&);
28  template<class GM_>
29  void load(GM_& gm, const std::string&, const std::string&);
30  template<class, size_t, size_t, bool>
32 }
33 
34 template<unsigned int I,unsigned int D,bool END>
36 
38 namespace detail_graphical_model {
39  template<class FUNCTION_TYPE>
40  struct FunctionData;
41 
42  template<class FUNCTION_TYPE>
43  struct FunctionDataUnit;
44 }
46 
47 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
49 
50 
57 template<
58  class T,
59  class OPERATOR,
60  class FUNCTION_TYPE_LIST = meta::TypeList<ExplicitFunction<T>, meta::ListEnd>,
62 >
64 : public FactorGraph<
65  GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>,
66  typename SPACE::IndexType
67  >
68 {
69 public:
71  typedef SPACE SpaceType;
72  typedef typename SpaceType::IndexType IndexType;
73  typedef typename SpaceType::LabelType LabelType;
74  typedef T ValueType;
75 
76  typedef typename meta::GenerateFunctionTypeList<
77  FUNCTION_TYPE_LIST,
78  ExplicitFunction<T,IndexType,LabelType>,false // refactor me
80 
82  NrOfFunctionTypes = meta::LengthOfTypeList<FunctionTypeList>::value
83  };
84 
88  typedef OPERATOR OperatorType;
89 
90 
93  GraphicalModel(const SpaceType& ,const size_t reserveFactorsPerVariable=0);
95 
96  const SpaceType& space() const;
97  IndexType numberOfVariables() const;
98  IndexType numberOfVariables(const IndexType) const;
99  IndexType numberOfLabels(const IndexType) const;
100  IndexType numberOfFunctions(const size_t) const;
101  IndexType numberOfFactors() const;
102  IndexType numberOfFactors(const IndexType) const;
103  IndexType variableOfFactor(const IndexType, const IndexType) const;
104  IndexType factorOfVariable(const IndexType, const IndexType) const;
105  const FactorType& operator[](const IndexType) const;
106  template<class ITERATOR>
107  ValueType evaluate(ITERATOR) const;
109  template<class ITERATOR>
110  bool isValidIndexSequence(ITERATOR, ITERATOR) const;
112  size_t factorOrder() const;
113 
114  void assign(const SpaceType& );
115  IndexType addVariable(const IndexType);
116  template<class FUNCTION_TYPE>
117  FunctionIdentifier addFunction(const FUNCTION_TYPE&);
118  template<class FUNCTION_TYPE>
119  std::pair<FunctionIdentifier,FUNCTION_TYPE &> addFunctionWithRefReturn(const FUNCTION_TYPE&);
120  template<class FUNCTION_TYPE>
121  FunctionIdentifier addSharedFunction(const FUNCTION_TYPE&);
122  template<class FUNCTION_TYPE>
123  FUNCTION_TYPE& getFunction(const FunctionIdentifier&);
124  template<class ITERATOR>
125  IndexType addFactor(const FunctionIdentifier&, ITERATOR, ITERATOR);
126 
127  template<class ITERATOR>
128  IndexType addFactorNonFinalized(const FunctionIdentifier&, ITERATOR, ITERATOR);
129 
130  void finalize();
131 
132  // reserve stuff
133  template <class FUNCTION_TYPE>
134  void reserveFunctions(const size_t numF){
135  typedef meta::SizeT<
136  meta::GetIndexInTypeList<
138  FUNCTION_TYPE
139  >::value
140  > TLIndex;
141  this-> template functions<TLIndex::value>().reserve(numF);
142  }
143 
144  void reserveFactors(const size_t numF){
145  factors_.reserve(numF);
146  }
147 
148  void reserveFactorsVarialbeIndices(const size_t size){
149  factorsVis_.reserve(size);
150  }
151 
152 
153  LabelType maxNumberOfLabels()const{
154  LabelType mx=0;
155  for(size_t i=0; i<numberOfVariables(); ++i)
156  mx = std::max(numberOfLabels(i), mx);
157  return mx;
158  }
159 
160  //template<class FUNCTOR>
161  //void callFunctor(
162  // const IndexType factorIndex,
163  // FUNCTOR & functor
164  //){
165  // const FactorType & factor = this->operator[](factorIndex);
166  // return opengm::detail_graphical_model::FunctionWrapper<NrOfFunctionTypes>::
167  // callFunctor(this, factor.functionIndex_,factor.functionTypeId_,
168  // factorIndex,functor);
169  //}
170 
171 
172 protected:
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();
177 
178 private:
179  SpaceType space_;
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_;
184  IndexType order_;
185 
186 
187 template<size_t>
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>
192  friend void opengm::hdf5::save(const GM&, const std::string&, const std::string&);
193 template<typename GM>
194  friend void opengm::hdf5::load(GM&, const std::string&, const std::string&);
195 
196 template<class , size_t , size_t , bool>
198 template<typename, typename>
200 template<typename, typename, typename >
201  friend class IndependentFactor;
202 template<typename>
203  friend class Factor;
204 template<typename, typename, typename , typename >
205  friend class GraphicalModel;
206 template <size_t , size_t, bool >
207  friend struct opengm::functionwrapper::executor::FactorInvariant;
208 template<unsigned int I,unsigned int D,bool END>
209  friend class FunctionIteratation;
210 template<class GM>
211  friend class ExplicitStorage;
212 };
213 
214 
216 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
217 struct FunctionIdentification {
218  typedef FUNCTION_INDEX_TYPE FunctionIndexType;
219  typedef FunctionIndexType IndexType;
220  typedef FUNCTION_TYPE_INDEX_TYPE FunctionTypeIndexType;
221 
222  FunctionIdentification(const FunctionIndexType=FunctionIndexType(0), const FunctionTypeIndexType=FunctionTypeIndexType(0));
223  bool operator < (const FunctionIdentification& ) const;
224  bool operator > (const FunctionIdentification& ) const;
225  bool operator <= (const FunctionIdentification& ) const;
226  bool operator >= (const FunctionIdentification& ) const;
227  bool operator == (const FunctionIdentification& ) const;
228 
229  FunctionTypeIndexType getFunctionType()const{return functionType;};
230  FunctionIndexType getFunctionIndex()const{return functionIndex;};
231 
232  FunctionIndexType functionIndex;
233  FunctionTypeIndexType functionType;
234 };
236 
239 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
240 inline typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::IndexType
242 (
243  const IndexType variableIndex
244 ) const {
245  OPENGM_ASSERT(variableIndex < numberOfVariables());
246  return variableFactorAdjaceny_[variableIndex].size();
247 }
248 
251 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
254 (
255  const IndexType factorIndex
256 ) const
257 {
258  OPENGM_ASSERT(factorIndex < numberOfFactors());
259  return factors_[factorIndex].numberOfVariables();
260 }
261 
263 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
266 (
267  const size_t functionTypeIndex
268 ) const
269 {
270  typedef meta::SizeT<GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::NrOfFunctionTypes> NoFt;
271  return detail_graphical_model::FunctionWrapper<NoFt::value>::numberOfFunctions(this, functionTypeIndex);
272 }
273 
276 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
279 (
280  const IndexType factorIndex,
281  const IndexType variableNumber
282 ) const
283 {
284  OPENGM_ASSERT(factorIndex < numberOfFactors());
285  OPENGM_ASSERT(variableNumber < numberOfVariables(factorIndex));
286  return factors_[factorIndex].variableIndex(variableNumber);
287 }
288 
291 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
294 (
295  const IndexType variableIndex,
296  const IndexType factorNumber
297 ) const
298 {
299  OPENGM_ASSERT(variableIndex < numberOfVariables());
300  OPENGM_ASSERT(factorNumber < numberOfFactors(variableIndex));
301  return variableFactorAdjaceny_[variableIndex][factorNumber];
302 }
303 
304 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
306 : space_(),
307  functionDataField_(),
308  variableFactorAdjaceny_(),
309  factors_(0, FactorType(this)),
310  factorsVis_(),
311  order_(0)
312 {
313  //this->assignGm(this);
314 }
315 
316 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
318 (
320 )
321 : space_(gm.space_),
322  functionDataField_(gm.functionDataField_),
323  variableFactorAdjaceny_(gm.variableFactorAdjaceny_),
324  factors_(gm.numberOfFactors()),
325  factorsVis_(gm.factorsVis_),
326  order_(gm.factorOrder())
327 {
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_;
332  //factors_[i].order_=gm.factors_[i].order_;
333  //factors_[i].indexInVisVector_=gm.factors_[i].indexInVisVector_;
334  factors_[i].vis_=gm.factors_[i].vis_;
335  factors_[i].vis_.assignPtr(this->factorsVis_);
336  }
337  //this->assignGm(this);
338  //this->initializeFactorFunctionAdjacency();
339 }
340 
341 
342 
344 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
345 inline
347 (
348  const SpaceType& space,
349  const size_t reserveFactorsPerVariable
350 )
351 : space_(space),
352  functionDataField_(),
353  variableFactorAdjaceny_(space.numberOfVariables()),
354  factors_(0, FactorType(this)),
355  order_(0)
356 {
357  if(reserveFactorsPerVariable==0){
358  variableFactorAdjaceny_.resize(space.numberOfVariables());
359  }
360  else{
361  RandomAccessSet<IndexType> reservedSet;
362  reservedSet.reserve(reserveFactorsPerVariable);
363  variableFactorAdjaceny_.resize(space.numberOfVariables(),reservedSet);
364  }
365  //this->assignGm(this);
366 }
369 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
372 (
373  const IndexType nLabels
374 )
375 {
376  space_.addVariable(nLabels);
377  variableFactorAdjaceny_.push_back(RandomAccessSet<size_t>());
378  return space_.numberOfVariables() - 1;
379 }
380 
382 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
383 inline void
385 (
386  const SPACE& space
387 )
388 {
390  //this->assignGm(this);
391 }
392 
393 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
396 {
397  return space_.numberOfVariables();
398 }
399 
401 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
404 (
405  const IndexType index
406 ) const
407 {
408  OPENGM_ASSERT(index < this->numberOfVariables());
409  return space_.numberOfLabels(index);
410 }
411 
413 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
416 (
417  const IndexType index
418 ) const
419 {
420  OPENGM_ASSERT(index < this->numberOfFactors());
421  return factors_[index];
422 }
423 
424 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
427 {
428  return this->factors_.size();
429 }
430 
432 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
433 inline const SPACE&
435 {
436  return this->space_;
437 }
438 
441 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
442 template<class ITERATOR>
445 (
446  ITERATOR labels
447 ) const
448 {
449  ValueType v;
450  //std::vector<LabelType> factor_state(numberOfVariables()+1);
451  std::vector<LabelType> factor_state(factorOrder()+1);
452  OperatorType::neutral(v);
453  for(size_t j = 0; j < factors_.size(); ++j) {
454  //size_t nvar = factors_[j].numberOfVariables();
455  //if(factors_[j].numberOfVariables() == 0) {
456  // nvar = 1;
457  //};
458  //factor_state.resize(nvar, static_cast<LabelType> (0));
459  factor_state[0]=0;
460  for(size_t i = 0; i < factors_[j].numberOfVariables(); ++i) {
461  // OPENGM_ASSERT_OP( static_cast<LabelType>(labels[factors_[j].variableIndex(i)])
462  // ,< ,static_cast<LabelType>(factors_[j].numberOfLabels(i)));
463  factor_state[i] = labels[factors_[j].variableIndex(i)];
464  }
465  OperatorType::op(factors_[j](factor_state.begin()), v);
466  }
467  return v;
468 }
469 
472 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
473 template<class ITERATOR>
474 inline bool
476 (
477  ITERATOR begin,
478  ITERATOR end
479 ) const
480 {
481  ITERATOR previousIt = begin;
482  while(begin != end) {
483  if(*begin >= this->numberOfVariables()) {
484  return false;
485  }
486  if(previousIt != begin && *previousIt >= *begin) {
487  return false;
488  }
489  previousIt = begin;
490  ++begin;
491  }
492  return true;
493 }
494 
496 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
497 inline size_t
499 {
500  for(size_t i = 0; i < numberOfFactors(); i++) {
501  OPENGM_ASSERT(factors_[i].numberOfVariables()<=order_);
502  }
503  return order_;
504 /*
505  size_t factorOrder = 0;
506  for(size_t i = 0; i < numberOfFactors(); i++) {
507  if(factors_[i].numberOfVariables() > factorOrder)
508  factorOrder = factors_[i].numberOfVariables();
509  }
510  return factorOrder;
511 */
512 }
513 
519 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
520 template<class FUNCTION_TYPE>
523 (
524  const FUNCTION_TYPE& function
525 )
526 {
527  // find index of FUNCTION_TYPE in Typelist
528  typedef meta::SizeT<
529  meta::GetIndexInTypeList<
531  FUNCTION_TYPE
532  >::value
533  > TLIndex;
534  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
535  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
536  FunctionIdentifier functionIdentifier;
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);
542  //this-> template addFunctionToAdjacency < TLIndex::value > ();
543  return functionIdentifier;
544 }
545 
546 
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 &>
551 (
552  const FUNCTION_TYPE& function
553 ){
554  // find index of FUNCTION_TYPE in Typelist
555  typedef meta::SizeT<
556  meta::GetIndexInTypeList<
558  FUNCTION_TYPE
559  >::value
560  > TLIndex;
561  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
562  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
563  FunctionIdentifier functionIdentifier;
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);
569  //this-> template addFunctionToAdjacency < TLIndex::value > ();
570  std::pair<FunctionIdentifier,FUNCTION_TYPE &> fidFunction(functionIdentifier,this-> template functions<TLIndex::value>().back());
571  return fidFunction;
572 }
573 
574 
578 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
579 template<class FUNCTION_TYPE>
582 (
583  const FUNCTION_TYPE& function
584 )
585 {
586  //const size_t dim=function.dimension();
587  // find index of FUNCTION_TYPE in Typelist
588  typedef meta::SizeT<
589  meta::GetIndexInTypeList<
591  FUNCTION_TYPE
592  >::value
593  > TLIndex;
594  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
595  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
596  FunctionIdentifier functionIdentifier;
597  functionIdentifier.functionType = TLIndex::value;
598  // search if function is already in the gm
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;
604  }
605  }
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);
609  //this-> template addFunctionToAdjacency < TLIndex::value > ();
610  return functionIdentifier;
611 }
612 
613 
614 
628 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
629 template<class FUNCTION_TYPE>
630 FUNCTION_TYPE&
632 (
633  const FunctionIdentifier& fid
634 )
635 {
636  typedef meta::SizeT<
637  meta::GetIndexInTypeList<
639  FUNCTION_TYPE
640  >::value
641  > TLIndex;
642  return this-> template functions<TLIndex::value>()[fid.getFunctionIndex()];
643 }
644 
645 
646 
652 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
653 template<class ITERATOR>
656 (
657  const FunctionIdentifier& functionIdentifier,
658  ITERATOR begin,
659  ITERATOR end
660 )
661 {
662 
663  const IndexType indexInVisVector = factorsVis_.size();
664  IndexType factorOrder = 0;
665  while(begin!=end){
666  factorsVis_.push_back(*begin);
667  ++begin;
668  ++factorOrder;
669  }
670  order_ = std::max(order_,factorOrder);
671 
672  // create factor
673  //FactorType factor();
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) {
677  const FactorType factor =factors_.back();
678  if(i!=0){
679  OPENGM_CHECK_OP(factor.variableIndex(i-1),<,factor.variableIndex(i),
680  "variable indices of a factor must be sorted");
681  }
682  OPENGM_CHECK_OP(factor.variableIndex(i),<,this->numberOfVariables(),
683  "variable indices of a factor must smaller than gm.numberOfVariables()");
684  this->variableFactorAdjaceny_[factor.variableIndex(i)].insert(factorIndex);
685  //++begin;
686  }
687  //this->addFactorToAdjacency(functionIdentifier.functionIndex, factorIndex, functionIdentifier.functionType);
688  //this->factors_[factorIndex].testInvariant();
689  return factorIndex;
690 }
691 
692 
693 
694 
703 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
704 template<class ITERATOR>
707 (
708  const FunctionIdentifier& functionIdentifier,
709  ITERATOR begin,
710  ITERATOR end
711 )
712 {
713 
714 
715  const IndexType indexInVisVector = factorsVis_.size();
716  IndexType factorOrder = 0;
717  while(begin!=end){
718  factorsVis_.push_back(*begin);
719  ++begin;
720  ++factorOrder;
721  }
722  order_ = std::max(order_,factorOrder);
723 
724 
725  // create factor
726  //FactorType factor();
727  const IndexType factorIndex = this->factors_.size();
728  this->factors_.push_back(FactorType(this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
729 
730  for(size_t i=0;i<factors_.back().numberOfVariables();++i) {
731  const FactorType factor =factors_.back();
732  if(i!=0){
733  OPENGM_CHECK_OP(factor.variableIndex(i-1),<,factor.variableIndex(i),
734  "variable indices of a factor must be sorted");
735  }
736  OPENGM_CHECK_OP(factor.variableIndex(i),<,this->numberOfVariables(),
737  "variable indices of a factor must smaller than gm.numberOfVariables()");
738  //this->variableFactorAdjaceny_[factor.variableIndex(i)].insert(factorIndex);
739  //++begin;
740  }
741  //this->addFactorToAdjacency(functionIdentifier.functionIndex, factorIndex, functionIdentifier.functionType);
742  //this->factors_[factorIndex].testInvariant();
743  return factorIndex;
744 }
745 
746 
747 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
748 void
750 
751  std::vector<std::set<IndexType> > variableFactorAdjaceny(this->numberOfVariables());
752  for(IndexType fi=0; fi < this->numberOfFactors();++fi){
753 
754  const FactorType & factor = factors_[fi];
755  const IndexType numVar = factor.numberOfVariables();
756  for(IndexType v=0;v<numVar;++v){
757  const IndexType vi=factor.variableIndex(v);
758  variableFactorAdjaceny[vi].insert(fi);
759  }
760  }
761 
762  for(IndexType vi=0;vi<this->numberOfVariables();++vi){
763  this->variableFactorAdjaceny_[vi].assignFromSet(variableFactorAdjaceny[vi]);
764  }
765 }
766 
767 
768 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
771 (
773 ) {
774  if(this!=&gm) {
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_);
787  }
788  //this->assignGm(this);
789  //this->initializeFactorFunctionAdjacency();
790  }
791  return *this;
792 }
793 
794 
795 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
796 template<size_t FUNCTION_INDEX>
797 const std::vector<
798  typename meta::TypeAtTypeList<
800  >::type
801 >&
803 {
804  return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
805  (this->functionDataField_).functionData_.functions_;
806 }
807 
808 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
809 template<size_t FUNCTION_INDEX>
810 std::vector<
811  typename meta::TypeAtTypeList<
812  typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionTypeList,
813  FUNCTION_INDEX
814  >::type
815 >&
817 {
818  return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
819  (this->functionDataField_).functionData_.functions_;
820 }
821 
822 
823 
824 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
825 inline
827 (
828  const FUNCTION_INDEX_TYPE functionIndex,
829  const FUNCTION_TYPE_INDEX_TYPE functionType
830 )
831 : functionIndex(functionIndex),
832  functionType(functionType)
833 {}
834 
835 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
836 inline bool
837 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator <
838 (
839  const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
840 ) const
841 {
842  if(functionType < rhs.functionType)
843  return true;
844  else
845  return functionIndex < rhs.functionIndex;
846 }
847 
848 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
849 inline bool
850 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >
851 (
852  const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
853 ) const
854 {
855  if(functionType >rhs.functionType)
856  return true;
857  else
858  return functionIndex > rhs.functionIndex;
859 }
860 
861 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
862 inline bool
863 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator <=
864 (
865  const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
866 ) const
867 {
868  if(functionType <= rhs.functionType)
869  return true;
870  else
871  return functionIndex <= rhs.functionIndex;
872 }
873 
874 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
875 inline bool
876 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >=
877 (
878  const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
879 ) const
880 {
881  if(functionType >=rhs.functionType)
882  return true;
883  else
884  return functionIndex >= rhs.functionIndex;
885 }
886 
887 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
888 inline bool
889 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator ==
890 (
891  const FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE> & rhs
892 ) const
893 {
894  return (functionType == rhs.functionType) && (functionIndex == rhs.functionIndex);
895 }
896 
898 namespace detail_graphical_model {
899  template<class FUNCTION_TYPE>
900  struct FunctionData {
901  std::vector<FUNCTION_TYPE> functions_;
902  };
903 
904  // template<class T, class INDEX_TYPE>
905  //struct FunctionAdjacencyData {
906  // std::vector<RandomAccessSet<INDEX_TYPE> > functionFactorAdjacencies_;
907  //};
908 
909  template<class FUNCTION_TYPE>
910  struct FunctionDataUnit{
911  FunctionData<FUNCTION_TYPE> functionData_;
912  };
913 
914  //template<class FUNCTION_TYPE, class INDEX_TYPE>
915  //struct FunctionAdjacencyDataUnit{
916  // FunctionAdjacencyData<FUNCTION_TYPE, INDEX_TYPE> functionAdjacencyData_;
917  //};
918 } // namespace detail_graphical_model
920 
921 } //namespace opengm
922 
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
The OpenGM namespace.
Definition: config.hxx:43
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...
Definition: factorgraph.hxx:18
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)
Definition: opengm.hxx:77
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
Definition: opengm.hxx:87
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)
Definition: submodel2.hxx:24
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)