OpenGM  2.3.x
Discrete Graphical Model Library
loop_corrected_bp.hxx
Go to the documentation of this file.
1 #ifndef OPENGM_LIBDAI_LOOP_CORRECTED_BP_HXX
2 #define OPENGM_LIBDAI_LOOP_CORRECTED_BP_HXX
4 
5 namespace opengm{
6 namespace external{
7 namespace libdai{
8 
9 class None{
10  struct Parameter{
11  Parameter() {
12  }
13  };
14 };
15 
16 template<class GM,class ACC,class CAVITY_INFERENCE>
17 class LoopCorrectedBp : public LibDaiInference<GM,ACC,LoopCorrectedBp<GM,ACC,CAVITY_INFERENCE> >, public opengm::Inference<GM,ACC>{
18  public:
19  typedef ACC AccumulationType;
20  typedef GM GraphicalModelType;
25 
26  typedef typename CAVITY_INFERENCE::Parameter CavityInferenceParameter;
27  enum UpdateRule{
30  };
31  enum Cavity{
36  };
37  std::string name() const{
38  return "libDAI-Loop-Corrected-Bp";
39  }
40 
41  //LC[cavity=FULL,reinit=1,updates=SEQFIX,maxiter=10000,cavainame=BP,cavaiopts=[updates=SEQMAX,tol=1e-9,maxiter=10000,logdomain=0],tol=1e-9]
42  struct Parameter{
43  Parameter
44  (
45  const Cavity cavity,
46  const size_t reinit,
47  const UpdateRule updateRule,
48  const size_t maxiter=10000,
49  const CavityInferenceParameter & cavityInferenceParam =CavityInferenceParameter(),
50  const double tolerance=1e-9,
51  const size_t verbose=0
52  ) :cavity_(cavity),
53  reinit_(reinit),
54  updateRule_(updateRule),
55  maxiter_(maxiter),
56  cavityInferenceParam_(cavityInferenceParam),
57  tolerance_(tolerance),
58  verbose_(verbose) {
59  }
60  std::string toString()const{
61  std::string ur,cav,cavAiName,cavAiOpts;
62  if(updateRule_==SEQFIX)ur = "SEQFIX";
63  else if(updateRule_==SEQRND)ur = "SEQRND";
64 
65  if(cavity_==FULL)cav = "FULL";
66  else if(cavity_==PAIR)cav = "PAIR";
67  else if(cavity_==PAIR2)cav = "PAIR2";
68  else if(cavity_==UNIFORM)cav = "UNIFORM";
69 
70  std::string cavAiAsString = cavityInferenceParam_.toString();
71  size_t counter=0;
72  if(opengm::meta::Compare<CAVITY_INFERENCE,opengm::external::libdai::None>::value) {
73  cavAiName="NONE";
74  cavAiOpts="[]";
75  }
76  while(CAVITY_INFERENCE[counter]!=']') {
77  cavAiName.push_back(cavAiAsString[counter]);
78  ++counter;
79  }
80  cavAiOpts.reserve(cavAiAsString.size()-cavAiName.size());
81  for(;counter<cavAiAsString.size();++counter) {
82  cavAiOpts.push_back(cavAiAsString[counter]);
83  }
84 
85  std::stringstream ss;
86  ss <<"LC["
87  <<"cavity="<<cav<<","
88  <<"reinit="<<reinit_<<","
89  <<"updates="<<ur<<","
90  <<"maxiter="<<maxiter_<<","
91  <<"cavainame="<<cavAiName<<","
92  <<"cavaiopts="<<cavAiOpts<<","
93  <<"verbose="<<verbose_<<"]";
94  return ss.str();
95  }
97  size_t reinit_;
99  size_t maxiter_;
100  CavityInferenceParameter cavityInferenceParam_;
101  double tolerance_;
102  size_t verbose_;
103  size_t logDomain_;
104  };
105  LoopCorrectedBp(const GM & gm,const Parameter param=Parameter())
106  :LibDaiInference<GM,ACC,LoopCorrectedBp<GM,ACC,CAVITY_INFERENCE> >(gm,param.toString()) {
107 
108  }
109 
110  virtual const GraphicalModelType& graphicalModel() const{
111  return this->graphicalModel_impl();
112  }
113 
114  virtual void reset(){
115  return this->reset_impl();
116  }
117 
119  return this->infer_impl();
120  }
121 
122  template<class VISITOR>
123  InferenceTermination infer(VISITOR& visitor ){
124  visitor.begin(*this);
125  InferenceTermination infTerm = this->infer_impl();
126  visitor.end(*this);
127  return infTerm;
128  }
129 
130  virtual InferenceTermination arg(std::vector<LabelType>& v, const size_t argnr=1)const{
131  return this->arg_impl(v,argnr);
132  }
133  virtual InferenceTermination marginal(const size_t v, IndependentFactorType& m) const{
134  return this->marginal_impl(v,m);
135  }
136  virtual InferenceTermination factorMarginal(const size_t f, IndependentFactorType& m) const{
137  return this->factorMarginal_impl(f,m);
138  }
139 
140 };
141 
142 } // end namespace libdai
143 } // end namespace external
144 } //end namespace opengm
145 
146 #endif // OPENGM_LIBDAI_LOOP_CORRECTED_BP_HXX
The OpenGM namespace.
Definition: config.hxx:43
opengm::visitors::EmptyVisitor< LoopCorrectedBp< GM, ACC, CAVITY_INFERENCE > > EmptyVisitorType
CAVITY_INFERENCE::Parameter CavityInferenceParameter
virtual InferenceTermination marginal(const size_t v, IndependentFactorType &m) const
output a solution for a marginal for a specific variable
InferenceTermination infer(VISITOR &visitor)
virtual InferenceTermination factorMarginal(const size_t f, IndependentFactorType &m) const
output a solution for a marginal for all variables connected to a factor
virtual const GraphicalModelType & graphicalModel() const
Inference algorithm interface.
Definition: inference.hxx:34
LoopCorrectedBp(const GM &gm, const Parameter param=Parameter())
opengm::visitors::VerboseVisitor< LoopCorrectedBp< GM, ACC, CAVITY_INFERENCE > > VerboseVisitorType
Parameter(const Cavity cavity, const size_t reinit, const UpdateRule updateRule, const size_t maxiter=10000, const CavityInferenceParameter &cavityInferenceParam=CavityInferenceParameter(), const double tolerance=1e-9, const size_t verbose=0)
virtual InferenceTermination arg(std::vector< LabelType > &v, const size_t argnr=1) const
output a solution
opengm::visitors::TimingVisitor< LoopCorrectedBp< GM, ACC, CAVITY_INFERENCE > > TimingVisitorType
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::IndependentFactorType IndependentFactorType
Definition: inference.hxx:44