OpenGM  2.3.x
Discrete Graphical Model Library
hqpbo.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_HQPBO_HXX
3 #define OPENGM_HQPBO_HXX
4 
11 
13 #include "opengm/inference/fix-fusion/fusion-move.hpp"
14 
15 namespace opengm {
16 
21 template<class GM, class ACC>
22 class HQPBO : public Inference<GM, opengm::Minimizer>
23 {
24 public:
25  typedef GM GraphicalModelType;
26  typedef ACC AccumulationType;
31 
32 
33  template<class _GM>
34  struct RebindGm{
36  };
37 
38  template<class _GM,class _ACC>
41  };
42 
43 
44 
45  struct Parameter {
47 
48  }
49  template<class P>
50  Parameter(const P & p){
51 
52  }
53  };
54 
55  HQPBO(const GraphicalModelType&, Parameter = Parameter());
56  std::string name() const;
57  const GraphicalModelType& graphicalModel() const;
59  template<class VISITOR>
60  InferenceTermination infer(VISITOR &);
61  InferenceTermination arg(std::vector<LabelType>&, const size_t& = 1) const;
62  void setStartingPoint(typename std::vector<LabelType>::const_iterator begin );
63 private:
64  const GraphicalModelType& gm_;
65  ValueType constV_;
66  HigherOrderEnergy<ValueType, 10> hoe_;
67  std::vector<LabelType> conf_;
68  ValueType bound_;
69 };
70 
71 template<class GM, class ACC>
72 inline void
74 (
75  typename std::vector<typename HQPBO<GM,ACC>::LabelType>::const_iterator begin
76 ) {
77  for (size_t i=0; i<gm_.numberOfVariables(); ++i)
78  conf_[i] = *(begin+i);
79 }
80 
81 template<class GM,class ACC>
83 (
84  const GM & gm,
86 )
87  : gm_(gm), constV_(0.0), conf_(std::vector<LabelType>(gm.numberOfVariables(),0))
88 {
89  hoe_.AddVars(gm_.numberOfVariables());
90  for (IndexType f = 0; f < gm_.numberOfFactors(); ++f)
91  {
92  IndexType size = gm_[f].numberOfVariables();
93  const LabelType l0 = 0;
94  const LabelType l1 = 1;
95  if (size == 0)
96  {
97  constV_ += gm_[f](&l0);
98  continue;
99  }
100  else if (size == 1)
101  {
102  IndexType var = gm_[f].variableIndex(0);
103  const ValueType e0 = gm_[f](&l0);
104  const ValueType e1 = gm_[f](&l1);
105  hoe_.AddUnaryTerm(var, e1 - e0);
106  }
107  else
108  {
109  unsigned int numAssignments = 1 << size;
110  AutoDeleteArray<ValueType> coeffs_array(new ValueType[numAssignments]);
111  ValueType * coeffs = coeffs_array.get();
112  for (unsigned int subset = 1; subset < numAssignments; ++subset)
113  {
114  coeffs[subset] = 0;
115  }
116  // For each boolean assignment, get the clique energy at the
117  // corresponding labeling
118  AutoDeleteArray<LabelType> cliqueLabels_array(new LabelType[size]);
119  LabelType * cliqueLabels = cliqueLabels_array.get();
120  for (unsigned int assignment = 0; assignment < numAssignments; ++assignment)
121  {
122  for (unsigned int i = 0; i < size; ++i)
123  {
124  if (assignment & (1 << i))
125  {
126  cliqueLabels[i] = l1;
127  }
128  else
129  {
130  cliqueLabels[i] = l0;
131  }
132  }
133  ValueType energy = gm_[f](cliqueLabels);
134  for (unsigned int subset = 1; subset < numAssignments; ++subset)
135  {
136  if (assignment & ~subset)
137  {
138  continue;
139  }
140  else
141  {
142  int parity = 0;
143  for (unsigned int b = 0; b < size; ++b)
144  {
145  parity ^= (((assignment ^ subset) & (1 << b)) != 0);
146  }
147  coeffs[subset] += parity ? -energy : energy;
148  }
149  }
150  }
151  typename HigherOrderEnergy<ValueType, 10>::VarId vars[10];
152  for (unsigned int subset = 1; subset < numAssignments; ++subset)
153  {
154  int degree = 0;
155  for (unsigned int b = 0; b < size; ++b)
156  {
157  if (subset & (1 << b))
158  {
159  vars[degree++] = gm_[f].variableIndex(b);
160  }
161  }
162  std::sort(vars, vars + degree);
163  hoe_.AddTerm(coeffs[subset], degree, vars);
164  }
165  }
166  }
167 }
168 
169 template<class GM,class ACC>
170 inline std::string
172 {
173  return "HQPBO";
174 }
175 
176 template<class GM,class ACC>
177 inline const typename HQPBO<GM,ACC>::GraphicalModelType&
179 {
180  return gm_;
181 }
182 
183 template<class GM,class ACC>
187  return infer(v);
188 }
189 
190 template<class GM,class ACC>
191 template<class VISITOR>
193 HQPBO<GM,ACC>::infer(VISITOR & visitor)
194 {
195  visitor.begin(*this);
196  kolmogorov::qpbo::QPBO<ValueType> qr(gm_.numberOfVariables(), 0);
197  hoe_.ToQuadratic(qr);
198  qr.Solve();
199  IndexType numberOfChangedVariables = 0;
200  for (IndexType i = 0; i < gm_.numberOfVariables(); ++i)
201  {
202  int label = qr.GetLabel(i);
203  if (label == 0 )
204  {
205  conf_[i] = 0;
206  }
207  else if (label == 1)
208  {
209  conf_[i] = 1;
210  }
211  else
212  {
213  //conf_[i] = 0;
214  }
215  }
216  bound_ = constV_ + 0.5 * qr.ComputeTwiceLowerBound();
217  visitor.end(*this);
218  return NORMAL;
219 }
220 
221 template<class GM,class ACC>
224 (
225  std::vector<LabelType>& arg,
226  const size_t& n
227  ) const
228 {
229  if(n > 1) {
230  return UNKNOWN;
231  }
232  else {
233  arg.resize(gm_.numberOfVariables());
234  for (IndexType i = 0; i < gm_.numberOfVariables(); ++i)
235  arg[i] =conf_[i];
236  return NORMAL;
237  }
238 }
239 
240 
241 } // namespace opengm
242 
243 #endif // #ifndef OPENGM_HQPBO_HXX
The OpenGM namespace.
Definition: config.hxx:43
GM GraphicalModelType
Definition: hqpbo.hxx:25
HQPBO< _GM, _ACC > type
Definition: hqpbo.hxx:40
std::string name() const
Definition: hqpbo.hxx:171
visitors::VerboseVisitor< HQPBO< GM, ACC > > VerboseVisitorType
Definition: hqpbo.hxx:28
void setStartingPoint(typename std::vector< LabelType >::const_iterator begin)
Definition: hqpbo.hxx:74
const GraphicalModelType & graphicalModel() const
Definition: hqpbo.hxx:178
Parameter(const P &p)
Definition: hqpbo.hxx:50
ACC AccumulationType
Definition: hqpbo.hxx:26
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:49
visitors::TimingVisitor< HQPBO< GM, ACC > > TimingVisitorType
Definition: hqpbo.hxx:29
InferenceTermination arg(std::vector< LabelType > &, const size_t &=1) const
Definition: hqpbo.hxx:224
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
Inference algorithm interface.
Definition: inference.hxx:43
HQPBO Algorithm .
Definition: hqpbo.hxx:22
HQPBO(const GraphicalModelType &, Parameter=Parameter())
Definition: hqpbo.hxx:83
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:48
visitors::EmptyVisitor< HQPBO< GM, ACC > > EmptyVisitorType
Definition: hqpbo.hxx:30
HQPBO< _GM, ACC > type
Definition: hqpbo.hxx:35
OPENGM_GM_TYPE_TYPEDEFS
Definition: hqpbo.hxx:27
InferenceTermination
Definition: inference.hxx:24
InferenceTermination infer()
Definition: hqpbo.hxx:185