OpenGM  2.3.x
Discrete Graphical Model Library
infandflip.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_INF_AND_FLIP_HXX
3 #define OPENGM_INF_AND_FLIP_HXX
4 
5 #include <vector>
6 #include <set>
7 #include <string>
8 #include <iostream>
9 #include <stdexcept>
10 #include <list>
11 
12 #include "opengm/opengm.hxx"
18 
19 namespace opengm {
20 
21 
22 
26 template<class GM, class ACC, class INF>
27 class InfAndFlip : public Inference<GM, ACC> {
28 public:
29  typedef ACC AccumulationType;
30  typedef GM GraphicalModelType;
35 
36 
37 
38  template<class _GM>
39  struct RebindGm{
40  typedef typename INF::template RebindGm<_GM>::type _I;
42  };
43 
44  template<class _GM,class _ACC>
46  typedef typename INF::template RebindGmAndAcc<_GM,_ACC>::type _I;
48  };
49 
50 
51 
52  struct Parameter
53  {
54  Parameter(const size_t maxSubgraphSize=2)
55  :
56  maxSubgraphSize_(maxSubgraphSize),
57  subPara_(),
58  warmStartableInf_(false){
59  }
60  template<class P>
61  Parameter(const P & p)
62  :
63  maxSubgraphSize_(p.maxSubgraphSize_),
64  subPara_(p.subPara_),
65  warmStartableInf_(p.warmStartableInf_){
66  }
67 
69  typename INF::Parameter subPara_;
71  };
72 
73  InfAndFlip(const GraphicalModelType&, typename InfAndFlip::Parameter param);
74  std::string name() const;
75  const GraphicalModelType& graphicalModel() const;
76  ValueType value() const;
77  ValueType bound() const;
78  void reset();
80  template<class VisitorType>
81  InferenceTermination infer(VisitorType&);
82  InferenceTermination arg(std::vector<LabelType>&, const size_t = 1)const;
83  void setStartingPoint(typename std::vector<LabelType>::const_iterator sp){
84  sp_.resize(gm_.numberOfVariables());
85  sp_.assign(sp,sp+gm_.numberOfVariables());
86  spValue_=gm_.evaluate(sp_.begin());
87  }
88 private:
89  const GraphicalModelType& gm_;
90  Parameter para_;
91  std::vector<LabelType> state_;
92  ValueType value_;
93  ValueType bound_;
94 
95  ValueType spValue_;
96  std::vector<LabelType> sp_;
97 };
98 
99 
100 
101 // implementation of InfAndFlip
102 
103 template<class GM, class ACC, class INF>
104 inline
106  const GraphicalModelType& gm,
108  )
109  : gm_(gm), para_(param)
110 {
111  if(gm_.numberOfVariables() == 0) {
112  throw RuntimeError("The graphical model has no variables.");
113  }
114  value_ = ACC::template neutral<ValueType>();
115  bound_ = ACC::template ineutral<ValueType>();
116 }
117 
118 template<class GM, class ACC, class INF>
119 inline void
121 {}
122 
123 
124 template<class GM, class ACC, class INF>
125 inline std::string
127 {
128  return "InfAndFlip";
129 }
130 
131 template<class GM, class ACC, class INF>
134 {
135  return gm_;
136 }
137 
138 
140 template<class GM, class ACC, class INF>
141 template<class VisitorType>
144  VisitorType& visitor
145 )
146 {
147  INF inf(gm_,para_.subPara_);
148  LazyFlipper<GM,ACC> lf(gm_);
149 
150  visitor.begin(*this);
151  if(para_.warmStartableInf_ && !(sp_.size()==0))
152  inf.setStartingPoint(sp_.begin());
153  inf.infer();
154  inf.arg(state_);
155  value_ = gm_.evaluate(state_);
156  //value_=inf.value();
157  bound_=inf.bound();
158  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
159  visitor.end(*this);
160  return NORMAL;
161  }
162 
163  if(para_.maxSubgraphSize_>0){
165  if(sp_.size()!=gm_.numberOfVariables())
166  lf.setStartingPoint(state_.begin());
167  else{
168  if(ACC::bop(value_,spValue_))
169  lf.setStartingPoint(state_.begin());
170  else
171  lf.setStartingPoint(sp_.begin());
172  }
173  std::cout << "start flipping ..."<<std::endl;
174  lf.infer();
175  lf.arg(state_);
176  value_ = gm_.evaluate(state_);
177  //value_=lf.value(); //<- numerical bug in LF
178  }
179  visitor.end(*this);
180 
181  return NORMAL;
182 }
183 
185 template<class GM, class ACC, class INF>
188 {
189  EmptyVisitorType visitor;
190  return this->infer(visitor);
191 }
192 
193 
194 template<class GM, class ACC, class INF>
197  std::vector<LabelType>& arg,
198  const size_t N
199 ) const
200 {
201  if(N==1) {
202  arg.resize(gm_.numberOfVariables());
203  for(size_t j=0; j<arg.size(); ++j) {
204  arg[j] = state_[j];
205  }
206  return NORMAL;
207  }
208  else {
209  return UNKNOWN;
210  }
211 }
212 
213 template<class GM, class ACC, class INF>
216 {
217  return value_;
218 }
219 template<class GM, class ACC, class INF>
222 {
223  return bound_;
224 }
225 } // namespace opengm
226 
227 #endif // #ifndef OPENGM_INFANDFLIP_HXX
Parameter(const size_t maxSubgraphSize=2)
Definition: infandflip.hxx:54
The OpenGM namespace.
Definition: config.hxx:43
INF::template RebindGm< _GM >::type _I
Definition: infandflip.hxx:40
const GraphicalModelType & graphicalModel() const
Definition: infandflip.hxx:133
void setMaxSubgraphSize(const size_t)
void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
INF::template RebindGmAndAcc< _GM, _ACC >::type _I
Definition: infandflip.hxx:46
visitors::TimingVisitor< InfAndFlip< GM, ACC, INF > > TimingVisitorType
Definition: infandflip.hxx:34
std::string name() const
Definition: infandflip.hxx:126
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
InfAndFlip< _GM, ACC, _I > type
Definition: infandflip.hxx:41
Inference and Flip .
Definition: infandflip.hxx:27
ValueType value() const
return the solution (value)
Definition: infandflip.hxx:215
visitors::EmptyVisitor< InfAndFlip< GM, ACC, INF > > EmptyVisitorType
Definition: infandflip.hxx:33
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Definition: infandflip.hxx:196
void setStartingPoint(typename std::vector< LabelType >::const_iterator sp)
set initial labeling
Definition: infandflip.hxx:83
InfAndFlip(const GraphicalModelType &, typename InfAndFlip::Parameter param)
Definition: infandflip.hxx:105
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
Inference algorithm interface.
Definition: inference.hxx:43
ValueType bound() const
return a bound on the solution
Definition: infandflip.hxx:221
InferenceTermination infer()
start the algorithm
Definition: infandflip.hxx:187
InfAndFlip< _GM, _ACC, _I > type
Definition: infandflip.hxx:47
A generalization of ICM B. Andres, J. H. Kappes, U. Koethe and Hamprecht F. A., The Lazy Flipper: MA...
OpenGM runtime error.
Definition: opengm.hxx:100
visitors::VerboseVisitor< InfAndFlip< GM, ACC, INF > > VerboseVisitorType
Definition: infandflip.hxx:32
InferenceTermination infer()
start the algorithm
InferenceTermination
Definition: inference.hxx:24