OpenGM  2.3.x
Discrete Graphical Model Library
bp.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LIBDAI_BP_HXX
2 #define OPENGM_LIBDAI_BP_HXX
3 
5 
7 
8 namespace opengm{
9 namespace external{
10 namespace libdai{
11 
12 template<class GM,class ACC>
13 class Bp : public LibDaiInference<GM,ACC, Bp<GM,ACC> > , public opengm::Inference<GM,ACC>{
14  public:
15  typedef ACC AccumulationType;
16  typedef GM GraphicalModelType;
18  typedef opengm::visitors::VerboseVisitor< Bp<GM,ACC> > VerboseVisitorType;
19  typedef opengm::visitors::TimingVisitor< Bp<GM,ACC> > TimingVisitorType;
20  typedef opengm::visitors::EmptyVisitor< Bp<GM,ACC> > EmptyVisitorType;
21 
23  enum UpdateRule{
24  PARALL,
25  SEQFIX,
26  SEQRND,
27  SEQMAX
28  };
29  std::string name() const {
30  return "libDAI-Bp";
31  }
32  struct Parameter{
33  Parameter
34  (
35  const size_t maxIterations=100,
36  const double damping=0.0,
37  const double tolerance=0.000001,
38  UpdateRule updateRule= PARALL,
39  const size_t verbose=0
40  ) :maxIterations_(maxIterations),
41  damping_(damping),
42  tolerance_(tolerance),
43  updateRule_(updateRule),
44  verbose_(verbose),
45  logDomain_(0) {
46 
47  }
48  std::string toString()const{
49  std::string ur;
50  std::stringstream ss;
51  if(updateRule_==PARALL)ur="PARALL";
52  else if(updateRule_==SEQFIX)ur = "SEQFIX";
53  else if(updateRule_==SEQMAX)ur = "SEQMAX";
54  else if(updateRule_==SEQRND)ur = "SEQRND";
55  ss <<"BP["
56  <<"updates="<<ur<<","
57  <<"damping="<<damping_<<","
58  <<"maxiter="<<maxIterations_<<","
59  <<"tol="<<tolerance_<<","
60  <<"logdomain="<<logDomain_<<","
61  <<"inference="<< std::string(::opengm::meta::Compare<ACC,::opengm::Integrator>::value==true ? std::string("SUMPROD") : std::string("MAXPROD") ) <<","
62  <<"verbose="<<verbose_<<"]";
63  return ss.str();
64  }
65 
66  size_t maxIterations_;
67  double damping_;
68  double tolerance_;
69  UpdateRule updateRule_;
70  size_t verbose_;
71  size_t logDomain_;
72  };
73  Bp(const GM & gm,const Parameter param=Parameter())
74  :LibDaiInference<GM,ACC, Bp<GM,ACC> >(gm,param.toString()) {
75 
76  }
77 
78  virtual const GraphicalModelType& graphicalModel() const{
79  return this->graphicalModel_impl();
80  }
81 
82  virtual void reset(){
83  return this->reset_impl();
84  }
85 
86  virtual InferenceTermination infer(){
87  return this->infer_impl();
88  }
89 
90  template<class VISITOR>
91  InferenceTermination infer(VISITOR& visitor ){
92  visitor.begin(*this);
93  InferenceTermination infTerm = this->infer_impl();
94  visitor.end(*this);
95  return infTerm;
96  }
97 
98  virtual InferenceTermination arg(std::vector<LabelType>& v, const size_t argnr=1)const{
99  return this->arg_impl(v,argnr);
100  }
101  virtual InferenceTermination marginal(const size_t v, IndependentFactorType& m) const{
102  return this->marginal_impl(v,m);
103  }
104  virtual InferenceTermination factorMarginal(const size_t f, IndependentFactorType& m) const{
105  return this->factorMarginal_impl(f,m);
106  }
107 
108 };
109 
110 } // end namespace libdai
111 } // end namespace external
112 } //end namespace opengm
113 
115 
116 #endif // OPENGM_LIBDAI_BP_HXX
The OpenGM namespace.
Definition: config.hxx:43
Inference algorithm interface.
Definition: inference.hxx:34
#define OPENGM_GM_TYPE_TYPEDEFS
Definition: inference.hxx:13
InferenceTermination
Definition: inference.hxx:24