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  struct Parameter
37  {
38  Parameter(const size_t maxSubgraphSize=2)
39  :
40  maxSubgraphSize_(maxSubgraphSize),
41  subPara_(),
42  warmStartableInf_(false){
43  }
44 
46  typename INF::Parameter subPara_;
48  };
49 
50  InfAndFlip(const GraphicalModelType&, typename InfAndFlip::Parameter param);
51  std::string name() const;
52  const GraphicalModelType& graphicalModel() const;
53  ValueType value() const;
54  ValueType bound() const;
55  void reset();
57  template<class VisitorType>
58  InferenceTermination infer(VisitorType&);
59  InferenceTermination arg(std::vector<LabelType>&, const size_t = 1)const;
60  void setStartingPoint(typename std::vector<LabelType>::const_iterator sp){
61  sp_.resize(gm_.numberOfVariables());
62  sp_.assign(sp,sp+gm_.numberOfVariables());
63  spValue_=gm_.evaluate(sp_.begin());
64  }
65 private:
66  const GraphicalModelType& gm_;
67  Parameter para_;
68  std::vector<LabelType> state_;
69  ValueType value_;
70  ValueType bound_;
71 
72  ValueType spValue_;
73  std::vector<LabelType> sp_;
74 };
75 
76 
77 
78 // implementation of InfAndFlip
79 
80 template<class GM, class ACC, class INF>
81 inline
83  const GraphicalModelType& gm,
85  )
86  : gm_(gm), para_(param)
87 {
88  if(gm_.numberOfVariables() == 0) {
89  throw RuntimeError("The graphical model has no variables.");
90  }
91  value_ = ACC::template neutral<ValueType>();
92  bound_ = ACC::template ineutral<ValueType>();
93 }
94 
95 template<class GM, class ACC, class INF>
96 inline void
98 {}
99 
100 
101 template<class GM, class ACC, class INF>
102 inline std::string
104 {
105  return "InfAndFlip";
106 }
107 
108 template<class GM, class ACC, class INF>
111 {
112  return gm_;
113 }
114 
115 
117 template<class GM, class ACC, class INF>
118 template<class VisitorType>
121  VisitorType& visitor
122 )
123 {
124  INF inf(gm_,para_.subPara_);
125  LazyFlipper<GM,ACC> lf(gm_);
126 
127  visitor.begin(*this);
128  if(para_.warmStartableInf_ && !(sp_.size()==0))
129  inf.setStartingPoint(sp_.begin());
130  inf.infer();
131  inf.arg(state_);
132  value_ = gm_.evaluate(state_);
133  //value_=inf.value();
134  bound_=inf.bound();
135  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
136  visitor.end(*this);
137  return NORMAL;
138  }
139 
140  if(para_.maxSubgraphSize_>0){
141  lf.setMaxSubgraphSize(para_.maxSubgraphSize_);
142  if(sp_.size()!=gm_.numberOfVariables())
143  lf.setStartingPoint(state_.begin());
144  else{
145  if(ACC::bop(value_,spValue_))
146  lf.setStartingPoint(state_.begin());
147  else
148  lf.setStartingPoint(sp_.begin());
149  }
150  std::cout << "start flipping ..."<<std::endl;
151  lf.infer();
152  lf.arg(state_);
153  value_ = gm_.evaluate(state_);
154  //value_=lf.value(); //<- numerical bug in LF
155  }
156  visitor.end(*this);
157 
158  return NORMAL;
159 }
160 
162 template<class GM, class ACC, class INF>
165 {
166  EmptyVisitorType visitor;
167  return this->infer(visitor);
168 }
169 
170 
171 template<class GM, class ACC, class INF>
174  std::vector<LabelType>& arg,
175  const size_t N
176 ) const
177 {
178  if(N==1) {
179  arg.resize(gm_.numberOfVariables());
180  for(size_t j=0; j<arg.size(); ++j) {
181  arg[j] = state_[j];
182  }
183  return NORMAL;
184  }
185  else {
186  return UNKNOWN;
187  }
188 }
189 
190 template<class GM, class ACC, class INF>
193 {
194  return value_;
195 }
196 template<class GM, class ACC, class INF>
199 {
200  return bound_;
201 }
202 } // namespace opengm
203 
204 #endif // #ifndef OPENGM_INFANDFLIP_HXX
Parameter(const size_t maxSubgraphSize=2)
Definition: infandflip.hxx:38
The OpenGM namespace.
Definition: config.hxx:43
void setMaxSubgraphSize(const size_t)
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Definition: infandflip.hxx:173
void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
ValueType value() const
return the solution (value)
Definition: infandflip.hxx:192
ValueType bound() const
return a bound on the solution
Definition: infandflip.hxx:198
visitors::TimingVisitor< InfAndFlip< GM, ACC, INF > > TimingVisitorType
Definition: infandflip.hxx:34
Inference and Flip .
Definition: infandflip.hxx:27
visitors::EmptyVisitor< InfAndFlip< GM, ACC, INF > > EmptyVisitorType
Definition: infandflip.hxx:33
void setStartingPoint(typename std::vector< LabelType >::const_iterator sp)
set initial labeling
Definition: infandflip.hxx:60
InfAndFlip(const GraphicalModelType &, typename InfAndFlip::Parameter param)
Definition: infandflip.hxx:82
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:41
Inference algorithm interface.
Definition: inference.hxx:34
InferenceTermination infer()
start the algorithm
Definition: infandflip.hxx:164
InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
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
const GraphicalModelType & graphicalModel() const
Definition: infandflip.hxx:110
InferenceTermination
Definition: inference.hxx:24
std::string name() const
Definition: infandflip.hxx:103