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);
94  GraphicalModel& operator=(const GraphicalModel&);
95 
96  template<class OTHER_TL>
97  GraphicalModel& operator=(
99  );
100 
101 
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;
119 
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);
132 
133  template<class ITERATOR>
134  IndexType addFactorNonFinalized(const FunctionIdentifier&, ITERATOR, ITERATOR);
135 
136  void finalize();
137 
138  // reserve stuff
139  template <class FUNCTION_TYPE>
140  void reserveFunctions(const size_t numF){
141  typedef meta::SizeT<
142  meta::GetIndexInTypeList<
143  FunctionTypeList,
144  FUNCTION_TYPE
145  >::value
146  > TLIndex;
147  this-> template functions<TLIndex::value>().reserve(numF);
148  }
149 
150  void reserveFactors(const size_t numF){
151  factors_.reserve(numF);
152  }
153 
154  void reserveFactorsVarialbeIndices(const size_t size){
155  factorsVis_.reserve(size);
156  }
157 
158 
159  LabelType maxNumberOfLabels()const{
160  LabelType mx=0;
161  for(size_t i=0; i<numberOfVariables(); ++i)
162  mx = std::max(numberOfLabels(i), mx);
163  return mx;
164  }
165 
166  //template<class FUNCTOR>
167  //void callFunctor(
168  // const IndexType factorIndex,
169  // FUNCTOR & functor
170  //){
171  // const FactorType & factor = this->operator[](factorIndex);
172  // return opengm::detail_graphical_model::FunctionWrapper<NrOfFunctionTypes>::
173  // callFunctor(this, factor.functionIndex_,factor.functionTypeId_,
174  // factorIndex,functor);
175  //}
176 
177 
178 protected:
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();
183 
184 private:
185  SpaceType space_;
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_;
190  IndexType order_;
191 
192 
193 template<size_t>
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>
198  friend void opengm::hdf5::save(const GM&, const std::string&, const std::string&);
199 template<typename GM>
200  friend void opengm::hdf5::load(GM&, const std::string&, const std::string&);
201 
202 template<class , size_t , size_t , bool>
204 template<typename, typename>
205  friend struct GraphicalModelEqualityTest;
206 template<typename, typename, typename >
207  friend class IndependentFactor;
208 template<typename>
209  friend class Factor;
210 template<typename, typename, typename , typename >
211  friend class GraphicalModel;
212 template <size_t , size_t, bool >
213  friend struct opengm::functionwrapper::executor::FactorInvariant;
214 template<unsigned int I,unsigned int D,bool END>
215  friend class FunctionIteratation;
216 template<class GM>
217  friend class ExplicitStorage;
218 };
219 
220 
222 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
223 struct FunctionIdentification {
224  typedef FUNCTION_INDEX_TYPE FunctionIndexType;
225  typedef FunctionIndexType IndexType;
226  typedef FUNCTION_TYPE_INDEX_TYPE FunctionTypeIndexType;
227 
228  FunctionIdentification(const FunctionIndexType=FunctionIndexType(0), const FunctionTypeIndexType=FunctionTypeIndexType(0));
229  bool operator < (const FunctionIdentification& ) const;
230  bool operator > (const FunctionIdentification& ) const;
231  bool operator <= (const FunctionIdentification& ) const;
232  bool operator >= (const FunctionIdentification& ) const;
233  bool operator == (const FunctionIdentification& ) const;
234 
235  FunctionTypeIndexType getFunctionType()const{return functionType;};
236  FunctionIndexType getFunctionIndex()const{return functionIndex;};
237 
238  FunctionIndexType functionIndex;
239  FunctionTypeIndexType functionType;
240 };
242 
245 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
248 (
249  const IndexType variableIndex
250 ) const {
251  OPENGM_ASSERT(variableIndex < numberOfVariables());
252  return variableFactorAdjaceny_[variableIndex].size();
253 }
254 
257 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
260 (
261  const IndexType factorIndex
262 ) const
263 {
264  OPENGM_ASSERT(factorIndex < numberOfFactors());
265  return factors_[factorIndex].numberOfVariables();
266 }
267 
269 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
272 (
273  const size_t functionTypeIndex
274 ) const
275 {
276  typedef meta::SizeT<GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::NrOfFunctionTypes> NoFt;
277  return detail_graphical_model::FunctionWrapper<NoFt::value>::numberOfFunctions(this, functionTypeIndex);
278 }
279 
282 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
285 (
286  const IndexType factorIndex,
287  const IndexType variableNumber
288 ) const
289 {
290  OPENGM_ASSERT(factorIndex < numberOfFactors());
291  OPENGM_ASSERT(variableNumber < numberOfVariables(factorIndex));
292  return factors_[factorIndex].variableIndex(variableNumber);
293 }
294 
297 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
300 (
301  const IndexType variableIndex,
302  const IndexType factorNumber
303 ) const
304 {
305  OPENGM_ASSERT(variableIndex < numberOfVariables());
306  OPENGM_ASSERT(factorNumber < numberOfFactors(variableIndex));
307  return variableFactorAdjaceny_[variableIndex][factorNumber];
308 }
309 
310 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
312 : space_(),
313  functionDataField_(),
314  variableFactorAdjaceny_(),
315  factors_(0, FactorType(this)),
316  factorsVis_(),
317  order_(0)
318 {
319  //this->assignGm(this);
320 }
321 
322 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
324 (
326 )
327 : space_(gm.space_),
328  functionDataField_(gm.functionDataField_),
329  variableFactorAdjaceny_(gm.variableFactorAdjaceny_),
330  factors_(gm.numberOfFactors()),
331  factorsVis_(gm.factorsVis_),
332  order_(gm.factorOrder())
333 {
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_;
338  //factors_[i].order_=gm.factors_[i].order_;
339  //factors_[i].indexInVisVector_=gm.factors_[i].indexInVisVector_;
340  factors_[i].vis_=gm.factors_[i].vis_;
341  factors_[i].vis_.assignPtr(this->factorsVis_);
342  }
343  //this->assignGm(this);
344  //this->initializeFactorFunctionAdjacency();
345 }
346 
347 
348 
350 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
351 inline
353 (
354  const SpaceType& space,
355  const size_t reserveFactorsPerVariable
356 )
357 : space_(space),
358  functionDataField_(),
359  variableFactorAdjaceny_(space.numberOfVariables()),
360  factors_(0, FactorType(this)),
361  order_(0)
362 {
363  if(reserveFactorsPerVariable==0){
364  variableFactorAdjaceny_.resize(space.numberOfVariables());
365  }
366  else{
367  RandomAccessSet<IndexType> reservedSet;
368  reservedSet.reserve(reserveFactorsPerVariable);
369  variableFactorAdjaceny_.resize(space.numberOfVariables(),reservedSet);
370  }
371  //this->assignGm(this);
372 }
375 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
378 (
379  const LabelType nLabels
380 )
381 {
382  space_.addVariable(nLabels);
383  variableFactorAdjaceny_.push_back(RandomAccessSet<IndexType>());
384  return space_.numberOfVariables() - 1;
385 }
386 
388 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
389 inline void
391 (
392  const SPACE& space
393 )
394 {
396  //this->assignGm(this);
397 }
398 
399 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
402 {
403  return space_.numberOfVariables();
404 }
405 
407 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
410 (
411  const IndexType index
412 ) const
413 {
414  OPENGM_ASSERT(index < this->numberOfVariables());
415  return space_.numberOfLabels(index);
416 }
417 
419 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
422 (
423  const IndexType index
424 ) const
425 {
426  OPENGM_ASSERT(index < this->numberOfFactors());
427  return factors_[index];
428 }
429 
430 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
433 {
434  return this->factors_.size();
435 }
436 
438 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
439 inline const SPACE&
441 {
442  return this->space_;
443 }
444 
447 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
448 template<class ITERATOR>
451 (
452  ITERATOR labels
453 ) const
454 {
455  ValueType v;
456  //std::vector<LabelType> factor_state(numberOfVariables()+1);
457  std::vector<LabelType> factor_state(factorOrder()+1);
458  OperatorType::neutral(v);
459  for(size_t j = 0; j < factors_.size(); ++j) {
460  //size_t nvar = factors_[j].numberOfVariables();
461  //if(factors_[j].numberOfVariables() == 0) {
462  // nvar = 1;
463  //};
464  //factor_state.resize(nvar, static_cast<LabelType> (0));
465  factor_state[0]=0;
466  for(size_t i = 0; i < factors_[j].numberOfVariables(); ++i) {
467  // OPENGM_ASSERT_OP( static_cast<LabelType>(labels[factors_[j].variableIndex(i)])
468  // ,< ,static_cast<LabelType>(factors_[j].numberOfLabels(i)));
469  factor_state[i] = labels[factors_[j].variableIndex(i)];
470  }
471  OperatorType::op(factors_[j](factor_state.begin()), v);
472  }
473  return v;
474 }
475 
478 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
479 template<class ITERATOR>
480 inline bool
482 (
483  ITERATOR begin,
484  ITERATOR end
485 ) const
486 {
487  ITERATOR previousIt = begin;
488  while(begin != end) {
489  if(*begin >= this->numberOfVariables()) {
490  return false;
491  }
492  if(previousIt != begin && *previousIt >= *begin) {
493  return false;
494  }
495  previousIt = begin;
496  ++begin;
497  }
498  return true;
499 }
500 
502 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
503 inline size_t
505 {
506  for(size_t i = 0; i < numberOfFactors(); i++) {
507  OPENGM_ASSERT(factors_[i].numberOfVariables()<=order_);
508  }
509  return order_;
510 /*
511  size_t factorOrder = 0;
512  for(size_t i = 0; i < numberOfFactors(); i++) {
513  if(factors_[i].numberOfVariables() > factorOrder)
514  factorOrder = factors_[i].numberOfVariables();
515  }
516  return factorOrder;
517 */
518 }
519 
525 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
526 template<class FUNCTION_TYPE>
529 (
530  const FUNCTION_TYPE& function
531 )
532 {
533  // find index of FUNCTION_TYPE in Typelist
534  typedef meta::SizeT<
535  meta::GetIndexInTypeList<
537  FUNCTION_TYPE
538  >::value
539  > TLIndex;
540  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
541  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
542  FunctionIdentifier functionIdentifier;
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);
548  //this-> template addFunctionToAdjacency < TLIndex::value > ();
549  return functionIdentifier;
550 }
551 
552 
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 &>
557 (
558  const FUNCTION_TYPE& function
559 ){
560  // find index of FUNCTION_TYPE in Typelist
561  typedef meta::SizeT<
562  meta::GetIndexInTypeList<
564  FUNCTION_TYPE
565  >::value
566  > TLIndex;
567  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
568  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
569  FunctionIdentifier functionIdentifier;
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);
575  //this-> template addFunctionToAdjacency < TLIndex::value > ();
576  std::pair<FunctionIdentifier,FUNCTION_TYPE &> fidFunction(functionIdentifier,this-> template functions<TLIndex::value>().back());
577  return fidFunction;
578 }
579 
580 
584 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
585 template<class FUNCTION_TYPE>
588 (
589  const FUNCTION_TYPE& function
590 )
591 {
592  //const size_t dim=function.dimension();
593  // find index of FUNCTION_TYPE in Typelist
594  typedef meta::SizeT<
595  meta::GetIndexInTypeList<
597  FUNCTION_TYPE
598  >::value
599  > TLIndex;
600  typedef typename meta::SmallerNumber<TLIndex::value, GraphicalModelType::NrOfFunctionTypes>::type MetaBoolAssertType;
601  OPENGM_META_ASSERT(MetaBoolAssertType::value, WRONG_FUNCTION_TYPE_INDEX);
602  FunctionIdentifier functionIdentifier;
603  functionIdentifier.functionType = TLIndex::value;
604  // search if function is already in the gm
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;
610  }
611  }
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);
615  //this-> template addFunctionToAdjacency < TLIndex::value > ();
616  return functionIdentifier;
617 }
618 
619 
620 
634 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
635 template<class FUNCTION_TYPE>
636 FUNCTION_TYPE&
638 (
639  const FunctionIdentifier& fid
640 )
641 {
642  typedef meta::SizeT<
643  meta::GetIndexInTypeList<
645  FUNCTION_TYPE
646  >::value
647  > TLIndex;
648  return this-> template functions<TLIndex::value>()[fid.getFunctionIndex()];
649 }
650 
651 
652 
658 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
659 template<class ITERATOR>
662 (
663  const FunctionIdentifier& functionIdentifier,
664  ITERATOR begin,
665  ITERATOR end
666 )
667 {
668 
669  const IndexType indexInVisVector = factorsVis_.size();
670  IndexType factorOrder = 0;
671  while(begin!=end){
672  factorsVis_.push_back(*begin);
673  ++begin;
674  ++factorOrder;
675  }
676  order_ = std::max(order_,factorOrder);
677 
678  // create factor
679  //FactorType factor();
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) {
683  const FactorType factor =factors_.back();
684  if(i!=0){
685  OPENGM_CHECK_OP(factor.variableIndex(i-1),<,factor.variableIndex(i),
686  "variable indices of a factor must be sorted");
687  }
688  OPENGM_CHECK_OP(factor.variableIndex(i),<,this->numberOfVariables(),
689  "variable indices of a factor must smaller than gm.numberOfVariables()");
690  this->variableFactorAdjaceny_[factor.variableIndex(i)].insert(factorIndex);
691  //++begin;
692  }
693  //this->addFactorToAdjacency(functionIdentifier.functionIndex, factorIndex, functionIdentifier.functionType);
694  //this->factors_[factorIndex].testInvariant();
695  return factorIndex;
696 }
697 
698 
699 
700 
709 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
710 template<class ITERATOR>
713 (
714  const FunctionIdentifier& functionIdentifier,
715  ITERATOR begin,
716  ITERATOR end
717 )
718 {
719 
720 
721  const IndexType indexInVisVector = factorsVis_.size();
722  IndexType factorOrder = 0;
723  while(begin!=end){
724  factorsVis_.push_back(*begin);
725  ++begin;
726  ++factorOrder;
727  }
728  order_ = std::max(order_,factorOrder);
729 
730 
731  // create factor
732  //FactorType factor();
733  const IndexType factorIndex = this->factors_.size();
734  this->factors_.push_back(FactorType(this, functionIdentifier.functionIndex, functionIdentifier.functionType , factorOrder, indexInVisVector));
735 
736  for(size_t i=0;i<factors_.back().numberOfVariables();++i) {
737  const FactorType factor =factors_.back();
738  if(i!=0){
739  OPENGM_CHECK_OP(factor.variableIndex(i-1),<,factor.variableIndex(i),
740  "variable indices of a factor must be sorted");
741  }
742  OPENGM_CHECK_OP(factor.variableIndex(i),<,this->numberOfVariables(),
743  "variable indices of a factor must smaller than gm.numberOfVariables()");
744  //this->variableFactorAdjaceny_[factor.variableIndex(i)].insert(factorIndex);
745  //++begin;
746  }
747  //this->addFactorToAdjacency(functionIdentifier.functionIndex, factorIndex, functionIdentifier.functionType);
748  //this->factors_[factorIndex].testInvariant();
749  return factorIndex;
750 }
751 
752 
753 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
754 void
756 
757  std::vector<std::set<IndexType> > variableFactorAdjaceny(this->numberOfVariables());
758  for(IndexType fi=0; fi < this->numberOfFactors();++fi){
759 
760  const FactorType & factor = factors_[fi];
761  const IndexType numVar = factor.numberOfVariables();
762  for(IndexType v=0;v<numVar;++v){
763  const IndexType vi=factor.variableIndex(v);
764  variableFactorAdjaceny[vi].insert(fi);
765  }
766  }
767 
768  for(IndexType vi=0;vi<this->numberOfVariables();++vi){
769  this->variableFactorAdjaceny_[vi].assignFromSet(variableFactorAdjaceny[vi]);
770  }
771 }
772 
773 
774 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
777 (
779 ) {
780  if(this!=&gm) {
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_);
793  }
794  //this->assignGm(this);
795  //this->initializeFactorFunctionAdjacency();
796  }
797  return *this;
798 }
799 
800 
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=
805 (
807 ) {
808 
810  this->space_ = gm.space_;
811 
812  //this->functionDataField_=gm.functionDataField_;
813 
814 
815 
816  std::vector<int> srcFidToTarget(OtherGm::NrOfFunctionTypes,-1);
817  detail_graphical_model::CopyFunctions<0, OtherGm::NrOfFunctionTypes >::op(gm, *this,srcFidToTarget);
818 
819  this->factors_.resize(gm.factors_.size());
820  this->variableFactorAdjaceny_=gm.variableFactorAdjaceny_;
821  this->factorsVis_ = gm.factorsVis_;
822  this->order_ = gm.order_;
823 
824  for(size_t i = 0; i<this->factors_.size(); ++i) {
825  factors_[i].gm_=this;
826  factors_[i].functionIndex_=gm.factors_[i].functionIndex_;
827 
828  int newFidFunctionId = srcFidToTarget[gm.factors_[i].functionTypeId_];
829  OPENGM_CHECK_OP(newFidFunctionId,>,-1,"INTERNAL ERROR");
830  factors_[i].functionTypeId_= newFidFunctionId;
831  factors_[i].vis_=gm.factors_[i].vis_;
832  factors_[i].vis_.assignPtr(this->factorsVis_);
833  }
834  return *this;
835 }
836 
837 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
838 template<size_t FUNCTION_INDEX>
839 const std::vector<
840  typename meta::TypeAtTypeList<
842  >::type
843 >&
845 {
846  return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
847  (this->functionDataField_).functionData_.functions_;
848 }
849 
850 template<class T, class OPERATOR, class FUNCTION_TYPE_LIST, class SPACE>
851 template<size_t FUNCTION_INDEX>
852 std::vector<
853  typename meta::TypeAtTypeList<
854  typename GraphicalModel<T, OPERATOR, FUNCTION_TYPE_LIST, SPACE>::FunctionTypeList,
855  FUNCTION_INDEX
856  >::type
857 >&
859 {
860  return meta::FieldAccess::template byIndex<FUNCTION_INDEX>
861  (this->functionDataField_).functionData_.functions_;
862 }
863 
864 
865 
866 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
867 inline
869 (
870  const FUNCTION_INDEX_TYPE functionIndex,
871  const FUNCTION_TYPE_INDEX_TYPE functionType
872 )
873 : functionIndex(functionIndex),
874  functionType(functionType)
875 {}
876 
877 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
878 inline bool
880 (
882 ) const
883 {
884  if(functionType < rhs.functionType)
885  return true;
886  else
887  return functionIndex < rhs.functionIndex;
888 }
889 
890 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
891 inline bool
893 (
895 ) const
896 {
897  if(functionType >rhs.functionType)
898  return true;
899  else
900  return functionIndex > rhs.functionIndex;
901 }
902 
903 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
904 inline bool
906 (
908 ) const
909 {
910  if(functionType <= rhs.functionType)
911  return true;
912  else
913  return functionIndex <= rhs.functionIndex;
914 }
915 
916 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
917 inline bool
918 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator >=
919 (
921 ) const
922 {
923  if(functionType >=rhs.functionType)
924  return true;
925  else
926  return functionIndex >= rhs.functionIndex;
927 }
928 
929 template<class FUNCTION_INDEX_TYPE, class FUNCTION_TYPE_INDEX_TYPE>
930 inline bool
931 FunctionIdentification<FUNCTION_INDEX_TYPE, FUNCTION_TYPE_INDEX_TYPE>::operator ==
932 (
934 ) const
935 {
936  return (functionType == rhs.functionType) && (functionIndex == rhs.functionIndex);
937 }
938 
940 namespace detail_graphical_model {
941  template<class FUNCTION_TYPE>
942  struct FunctionData {
943  std::vector<FUNCTION_TYPE> functions_;
944  };
945 
946  // template<class T, class INDEX_TYPE>
947  //struct FunctionAdjacencyData {
948  // std::vector<RandomAccessSet<INDEX_TYPE> > functionFactorAdjacencies_;
949  //};
950 
951  template<class FUNCTION_TYPE>
952  struct FunctionDataUnit{
953  FunctionData<FUNCTION_TYPE> functionData_;
954  };
955 
956  //template<class FUNCTION_TYPE, class INDEX_TYPE>
957  //struct FunctionAdjacencyDataUnit{
958  // FunctionAdjacencyData<FUNCTION_TYPE, INDEX_TYPE> functionAdjacencyData_;
959  //};
960 } // namespace detail_graphical_model
962 
963 } //namespace opengm
964 
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
The OpenGM namespace.
Definition: config.hxx:43
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...
Definition: factorgraph.hxx:18
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)
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
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
Definition: opengm.hxx:87
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)
Definition: submodel2.hxx:24
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)