OpenGM  2.3.x
Discrete Graphical Model Library
greedygremlin.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_GREEDYGREMLIN_HXX
3 #define OPENGM_GREEDYGREMLIN_HXX
4 
5 #include <cmath>
6 #include <vector>
7 #include <list>
8 #include <set>
9 #include <iostream>
10 #include <algorithm>
11 #include <iostream>
12 #include <functional>
13 
14 #include "opengm/opengm.hxx"
18 namespace opengm {
19 
20 
21 
23 
33  template<class GM,class ACC>
34  class GreedyGremlin : public Inference<GM,ACC>
35  {
36  public:
38  typedef GM GraphicalModelType;
40  typedef ACC AccumulationType;
46 
47  template<class _GM>
48  struct RebindGm{
50  };
51 
52  template<class _GM,class _ACC>
55  };
56 
57 
58  struct Parameter {
60 
61  }
62  template<class P>
63  Parameter(const P & p){
64 
65  }
66  };
67  GreedyGremlin(const GM& gm, Parameter para = Parameter());
68  virtual std::string name() const {return "GreedyGremlin";}
69  const GraphicalModelType& graphicalModel() const;
70  virtual InferenceTermination infer();
71  virtual void reset();
72  template<class VisitorType> InferenceTermination infer(VisitorType& vistitor);
73  virtual InferenceTermination marginal(const size_t,IndependentFactorType& out)const {return UNKNOWN;}
74  virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType& out)const {return UNKNOWN;}
75  virtual InferenceTermination arg(std::vector<LabelType>& v, const size_t = 1)const;
76  virtual InferenceTermination args(std::vector< std::vector<LabelType> >& v)const;
77 
78  private:
79  const GM& gm_;
80  Parameter parameter_;
81  std::vector<LabelType> conf_;
82  };
83 
84 
85 //*******************
86 //** Impelentation **
87 //*******************
88 
92  template<class GM, class ACC >
94  (
95  const GM& gm,
96  Parameter para
97  ):gm_(gm), parameter_(para)
98  {
99  conf_.resize(gm.numberOfVariables(),0);
100  }
101 
108  template<class GM, class ACC >
109  void
111  {
113  }
114 
115  template <class GM, class ACC>
118  {
120  return infer(v);
121  }
122 
125  template<class GM, class ACC>
126  template<class VisitorType>
128  {
129  std::vector<bool> nodeColor(gm_.numberOfVariables(),false);
130  std::vector<IndexType> waitingList(gm_.numberOfVariables());
131  waitingList[0] = 0;
132  nodeColor[0] = true;
133  IndexType waitingListFirst = 0;
134  IndexType waitingListLast = 0;
135  visitor.begin(*this);
136  const ValueType neutral = GM::OperatorType::template neutral<ValueType>();
137  while(waitingListFirst<waitingList.size()){
138  OPENGM_ASSERT(waitingListFirst<=waitingListLast);
139  IndexType var = waitingList[waitingListFirst++];
140  std::vector<ValueType> vals(gm_.numberOfLabels(var),neutral);
141  //for all neigboured factors
142  for(typename GM::ConstFactorIterator fit=gm_.factorsOfVariableBegin(var); fit!=gm_.factorsOfVariableEnd(var); ++fit){
143  bool useIt = true;
144  for(typename GM::ConstVariableIterator vit=gm_.variablesOfFactorBegin(*fit); vit!=gm_.variablesOfFactorEnd(*fit); ++vit){
145  if(nodeColor[*vit]==false){
146  useIt = false;
147  break;
148  }
149  }
150  if(useIt){
151  std::vector<LabelType> l(gm_[*fit].numberOfVariables());
152  size_t p;
153  for(size_t i=0; i<l.size();++i){
154  if(gm_[*fit].variableIndex(i)==var)
155  p=i;
156  else
157  l[i] = conf_[gm_[*fit].variableIndex(i)];
158  }
159  for(l[p]=0; l[p]<gm_.numberOfLabels(var);++l[p]){
160  const ValueType v = gm_[*fit](l.begin());
161  GM::OperatorType::op(v,vals[l[p]]);
162  }
163  }
164  }
165 
166  //find best and fix
167  for(size_t i=0; i<vals.size();++i){
168  if(ACC::bop(vals[i],vals[conf_[var]]))
169  conf_[var]=i;
170  }
171 
172  //add white neighbours to waitingslist
173  for(typename GM::ConstFactorIterator fit=gm_.factorsOfVariableBegin(var); fit!=gm_.factorsOfVariableEnd(var); ++fit){
174  for(typename GM::ConstVariableIterator vit=gm_.variablesOfFactorBegin(*fit); vit!=gm_.variablesOfFactorEnd(*fit); ++vit){
175  if(nodeColor[*vit]==false){
176  nodeColor[*vit]=true;
177  waitingList[++waitingListLast] = *vit;
178  }
179  }
180  }
181  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
182  break;
183  }
184  }
185 
186  visitor.end(*this);
187  return NORMAL;
188  }
189 
190  template<class GM, class ACC>
192  ::arg(std::vector<LabelType>& conf, const size_t n)const
193  {
194  if(n==1) {
195  conf=conf_;
196  return NORMAL;
197  }else{
198  conf.resize(0);
199  return UNKNOWN;
200  }
201  }
202 
207  template<class GM, class ACC>
209  ::args(std::vector<std::vector<typename GreedyGremlin<GM,ACC>::LabelType> >& conf)const
210  {
211  return UNKNOWN;
212  }
213 
214 
215  template<class GM, class ACC>
216  inline const typename GreedyGremlin<GM, ACC>::GraphicalModelType&
218  {
219  return gm_;
220  }
221 
222 
223 } // namespace opengm
224 
225 #endif // #ifndef OPENGM_GREEDYGREMLIN_HXX
226 
The OpenGM namespace.
Definition: config.hxx:43
virtual InferenceTermination infer()
GreedyGremlin(const GM &gm, Parameter para=Parameter())
constructor
virtual std::string name() const
GM GraphicalModelType
graphical model type
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
virtual InferenceTermination args(std::vector< std::vector< LabelType > > &v) const
args
visitors::TimingVisitor< GreedyGremlin< GM, ACC > > TimingVisitorType
GreedyGremlin< _GM, ACC > type
visitors::VerboseVisitor< GreedyGremlin< GM, ACC > > VerboseVisitorType
visitor
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:49
virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType &out) const
output a solution for a marginal for all variables connected to a factor
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
const GraphicalModelType & graphicalModel() const
Inference algorithm interface.
Definition: inference.hxx:43
GreedyGremlin< _GM, _ACC > type
ACC AccumulationType
accumulation type
virtual void reset()
reset
virtual InferenceTermination arg(std::vector< LabelType > &v, const size_t=1) const
output a solution
virtual InferenceTermination marginal(const size_t, IndependentFactorType &out) const
output a solution for a marginal for a specific variable
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:48
GREEDY GREMLIN.
visitors::EmptyVisitor< GreedyGremlin< GM, ACC > > EmptyVisitorType
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::IndependentFactorType IndependentFactorType
Definition: inference.hxx:53