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  struct Parameter {
48 
49  };
50  GreedyGremlin(const GM& gm, Parameter para = Parameter());
51  virtual std::string name() const {return "GreedyGremlin";}
52  const GraphicalModelType& graphicalModel() const;
53  virtual InferenceTermination infer();
54  virtual void reset();
55  template<class VisitorType> InferenceTermination infer(VisitorType& vistitor);
56  virtual InferenceTermination marginal(const size_t,IndependentFactorType& out)const {return UNKNOWN;}
57  virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType& out)const {return UNKNOWN;}
58  virtual InferenceTermination arg(std::vector<LabelType>& v, const size_t = 1)const;
59  virtual InferenceTermination args(std::vector< std::vector<LabelType> >& v)const;
60 
61  private:
62  const GM& gm_;
63  Parameter parameter_;
64  std::vector<LabelType> conf_;
65  };
66 
67 
68 //*******************
69 //** Impelentation **
70 //*******************
71 
75  template<class GM, class ACC >
77  (
78  const GM& gm,
79  Parameter para
80  ):gm_(gm), parameter_(para)
81  {
82  conf_.resize(gm.numberOfVariables(),0);
83  }
84 
91  template<class GM, class ACC >
92  void
94  {
96  }
97 
98  template <class GM, class ACC>
101  {
102  EmptyVisitorType v;
103  return infer(v);
104  }
105 
108  template<class GM, class ACC>
109  template<class VisitorType>
111  {
112  std::vector<bool> nodeColor(gm_.numberOfVariables(),false);
113  std::vector<IndexType> waitingList(gm_.numberOfVariables());
114  waitingList[0] = 0;
115  nodeColor[0] = true;
116  IndexType waitingListFirst = 0;
117  IndexType waitingListLast = 0;
118  visitor.begin(*this);
119  const ValueType neutral = GM::OperatorType::template neutral<ValueType>();
120  while(waitingListFirst<waitingList.size()){
121  OPENGM_ASSERT(waitingListFirst<=waitingListLast);
122  IndexType var = waitingList[waitingListFirst++];
123  std::vector<ValueType> vals(gm_.numberOfLabels(var),neutral);
124  //for all neigboured factors
125  for(typename GM::ConstFactorIterator fit=gm_.factorsOfVariableBegin(var); fit!=gm_.factorsOfVariableEnd(var); ++fit){
126  bool useIt = true;
127  for(typename GM::ConstVariableIterator vit=gm_.variablesOfFactorBegin(*fit); vit!=gm_.variablesOfFactorEnd(*fit); ++vit){
128  if(nodeColor[*vit]==false){
129  useIt = false;
130  break;
131  }
132  }
133  if(useIt){
134  std::vector<LabelType> l(gm_[*fit].numberOfVariables());
135  size_t p;
136  for(size_t i=0; i<l.size();++i){
137  if(gm_[*fit].variableIndex(i)==var)
138  p=i;
139  else
140  l[i] = conf_[gm_[*fit].variableIndex(i)];
141  }
142  for(l[p]=0; l[p]<gm_.numberOfLabels(var);++l[p]){
143  const ValueType v = gm_[*fit](l.begin());
144  GM::OperatorType::op(v,vals[l[p]]);
145  }
146  }
147  }
148 
149  //find best and fix
150  for(size_t i=0; i<vals.size();++i){
151  if(ACC::bop(vals[i],vals[conf_[var]]))
152  conf_[var]=i;
153  }
154 
155  //add white neighbours to waitingslist
156  for(typename GM::ConstFactorIterator fit=gm_.factorsOfVariableBegin(var); fit!=gm_.factorsOfVariableEnd(var); ++fit){
157  for(typename GM::ConstVariableIterator vit=gm_.variablesOfFactorBegin(*fit); vit!=gm_.variablesOfFactorEnd(*fit); ++vit){
158  if(nodeColor[*vit]==false){
159  nodeColor[*vit]=true;
160  waitingList[++waitingListLast] = *vit;
161  }
162  }
163  }
164  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
165  break;
166  }
167  }
168 
169  visitor.end(*this);
170  return NORMAL;
171  }
172 
173  template<class GM, class ACC>
175  ::arg(std::vector<LabelType>& conf, const size_t n)const
176  {
177  if(n==1) {
178  conf=conf_;
179  return NORMAL;
180  }else{
181  conf.resize(0);
182  return UNKNOWN;
183  }
184  }
185 
190  template<class GM, class ACC>
192  ::args(std::vector<std::vector<typename GreedyGremlin<GM,ACC>::LabelType> >& conf)const
193  {
194  return UNKNOWN;
195  }
196 
197 
198  template<class GM, class ACC>
199  inline const typename GreedyGremlin<GM, ACC>::GraphicalModelType&
201  {
202  return gm_;
203  }
204 
205 
206 } // namespace opengm
207 
208 #endif // #ifndef OPENGM_GREEDYGREMLIN_HXX
209 
The OpenGM namespace.
Definition: config.hxx:43
virtual InferenceTermination infer()
GreedyGremlin(const GM &gm, Parameter para=Parameter())
constructor
GM GraphicalModelType
graphical model type
virtual InferenceTermination marginal(const size_t, IndependentFactorType &out) const
output a solution for a marginal for a specific variable
#define OPENGM_ASSERT(expression)
Definition: opengm.hxx:77
visitors::TimingVisitor< GreedyGremlin< GM, ACC > > TimingVisitorType
visitors::VerboseVisitor< GreedyGremlin< GM, ACC > > VerboseVisitorType
visitor
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:40
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:41
virtual InferenceTermination factorMarginal(const size_t, IndependentFactorType &out) const
output a solution for a marginal for all variables connected to a factor
Inference algorithm interface.
Definition: inference.hxx:34
virtual InferenceTermination args(std::vector< std::vector< LabelType > > &v) const
args
ACC AccumulationType
accumulation type
virtual void reset()
reset
virtual InferenceTermination arg(std::vector< LabelType > &v, const size_t=1) const
output a solution
const GraphicalModelType & graphicalModel() const
virtual std::string name() const
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:39
GREEDY GREMLIN.
visitors::EmptyVisitor< GreedyGremlin< GM, ACC > > EmptyVisitorType
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::IndependentFactorType IndependentFactorType
Definition: inference.hxx:44