OpenGM  2.3.x
Discrete Graphical Model Library
icm.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_ICM_HXX
3 #define OPENGM_ICM_HXX
4 
5 #include <vector>
6 #include <string>
7 #include <iostream>
8 
9 #include "opengm/opengm.hxx"
10 //#include "opengm/inference/visitors/visitor.hxx"
14 
15 
17 
18 namespace opengm {
19 
23 template<class GM, class ACC>
24 class ICM : public Inference<GM, ACC>
25 {
26 public:
27  enum MoveType {
29  FACTOR = 1
30  };
31  typedef ACC AccumulationType;
32  typedef GM GraphicalModelType;
38 
39  template<class _GM>
40  struct RebindGm{
42  };
43 
44  template<class _GM,class _ACC>
47  };
48 
49 
50  class Parameter {
51  public:
52 
53  Parameter(const std::vector<LabelType>& startPoint)
54  : moveType_(SINGLE_VARIABLE),
55  startPoint_(startPoint)
56  {}
57  Parameter(MoveType moveType, const std::vector<LabelType>& startPoint)
58  : moveType_(moveType),
59  startPoint_(startPoint)
60  {}
61 
63  : moveType_(moveType),
64  startPoint_()
65  {}
66 
67  template<class OP>
68  Parameter(const OP & otherParameter)
69  {
70  moveType_ = otherParameter.moveType_== 0? SINGLE_VARIABLE : FACTOR;
71  startPoint_(otherParameter.startPoint_);
72  }
73 
75  std::vector<LabelType> startPoint_;
76  };
77 
78  ICM(const GraphicalModelType&);
79  ICM(const GraphicalModelType&, const Parameter&);
80  std::string name() const;
81  const GraphicalModelType& graphicalModel() const;
83  void reset();
84  template<class VisitorType>
85  InferenceTermination infer(VisitorType&);
86  void setStartingPoint(typename std::vector<LabelType>::const_iterator);
87  virtual InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const ;
88  virtual ValueType value()const{return movemaker_.value();}
89  size_t currentMoveType() const;
90 
91 private:
92  const GraphicalModelType& gm_;
93  MovemakerType movemaker_;
94  Parameter param_;
95  MoveType currentMoveType_;
96 
97 };
98 
99 template<class GM, class ACC>
100 inline size_t
102  return currentMoveType_==SINGLE_VARIABLE?0:1;
103 }
104 
105 template<class GM, class ACC>
106 inline
108 (
109  const GraphicalModelType& gm
110 )
111 : gm_(gm),
112  movemaker_(gm),
113  param_(Parameter()),
114  currentMoveType_(SINGLE_VARIABLE) {
115 }
116 
117 template<class GM, class ACC>
119 (
120  const GraphicalModelType& gm,
121  const Parameter& parameter
122 )
123 : gm_(gm),
124  movemaker_(gm),
125  param_(parameter),
126  currentMoveType_(SINGLE_VARIABLE)
127 {
128  if(parameter.startPoint_.size() == gm.numberOfVariables()) {
129  movemaker_.initialize(parameter.startPoint_.begin() );
130  }
131  else if(parameter.startPoint_.size() != 0) {
132  throw RuntimeError("unsuitable starting point");
133  }
134 }
135 
136 template<class GM, class ACC>
137 inline void
139 {
140  if(param_.startPoint_.size() == gm_.numberOfVariables()) {
141  movemaker_.initialize(param_.startPoint_.begin() );
142  }
143  else if(param_.startPoint_.size() != 0) {
144  throw RuntimeError("unsuitable starting point");
145  }
146  else{
147  movemaker_.reset();
148  }
149 }
150 
151 template<class GM, class ACC>
152 inline void
154 (
155  typename std::vector<typename ICM<GM,ACC>::LabelType>::const_iterator begin
156 ) {
157  movemaker_.initialize(begin);
158 }
159 
160 template<class GM, class ACC>
161 inline std::string
163 {
164  return "ICM";
165 }
166 
167 template<class GM, class ACC>
168 inline const typename ICM<GM, ACC>::GraphicalModelType&
170 {
171  return gm_;
172 }
173 
174 template<class GM, class ACC>
177 {
179  return infer(v);
180 }
181 
182 
183 template<class GM, class ACC>
184 template<class VisitorType>
186 (
187  VisitorType& visitor
188 )
189 {
190  bool exitInf=false;
191  visitor.begin(*this);
192  if(param_.moveType_==SINGLE_VARIABLE ||param_.moveType_==FACTOR) {
193  bool updates = true;
194  std::vector<bool> isLocalOptimal(gm_.numberOfVariables());
195  std::vector<opengm::RandomAccessSet<IndexType> >variableAdjacencyList;
196  gm_.variableAdjacencyList(variableAdjacencyList);
197  size_t v=0,s=0,n=0;
198  while(updates && exitInf==false) {
199  updates = false;
200  for(v=0; v<gm_.numberOfVariables() && exitInf==false; ++v) {
201  if(isLocalOptimal[v]==false) {
202  for(s=0; s<gm_.numberOfLabels(v); ++s) {
203  if(s != movemaker_.state(v)) {
204  if(AccumulationType::bop(movemaker_.valueAfterMove(&v, &v+1, &s), movemaker_.value())) {
205  movemaker_.move(&v, &v+1, &s);
206  for(n=0;n<variableAdjacencyList[v].size();++n) {
207  isLocalOptimal[variableAdjacencyList[v][n]]=false;
208  }
209  updates = true;
210  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){ exitInf=true;
211  break;
212  }
213  }
214  }
215  }
216  isLocalOptimal[v]=true;
217  }
218  }
219  }
220  }
221  if(param_.moveType_==FACTOR) {
222  currentMoveType_=FACTOR;
223  //visitor(*this, movemaker_.value(),movemaker_.value());
224  bool updates = true;
225  std::vector<bool> isLocalOptimal(gm_.numberOfFactors(),false);
226  //std::vector<opengm::RandomAccessSet<size_t> >variableAdjacencyList;
227  opengm::BufferVector<LabelType> stateBuffer;
228  stateBuffer.reserve(10);
229  //gm_.factorAdjacencyList(variableAdjacencyList);
230  size_t f=0,ff=0,v=0;
231  while(updates && exitInf==false) {
232  updates = false;
233  for(f=0; f<gm_.numberOfFactors() && exitInf==false; ++f) {
234  if(isLocalOptimal[f]==false && gm_[f].numberOfVariables()>1) {
235  stateBuffer.clear();
236  stateBuffer.resize(gm_[f].numberOfVariables());
237  for(v=0;v<gm_[f].numberOfVariables();++v) {
238  stateBuffer[v]=movemaker_.state(gm_[f].variableIndex(v));
239  }
240  ValueType oldValue=movemaker_.value();
241  ValueType newValue=movemaker_. template moveOptimally<ACC>(gm_[f].variableIndicesBegin(),gm_[f].variableIndicesEnd());
242  if(ACC::bop(newValue,oldValue)) {
243  updates = true ;
244  if( visitor(*this) != visitors::VisitorReturnFlag::ContinueInf ){
245  exitInf=true;
246  break;
247  }
248  for(v=0;v<gm_[f].numberOfVariables();++v) {
249  const size_t varIndex=gm_[f].variableIndex(v);
250  if(stateBuffer[v]!=movemaker_.state(varIndex)) {
251  for(ff=0;ff<gm_.numberOfFactors(varIndex);++ff) {
252  isLocalOptimal[gm_.factorOfVariable(varIndex,ff)]=false;
253  }
254  }
255  }
256  }
257  isLocalOptimal[f]=true;
258  }
259  }
260  }
261  }
262  visitor.end(*this);
263  return NORMAL;
264 }
265 
266 template<class GM, class ACC>
269 (
270  std::vector<LabelType>& x,
271  const size_t N
272 ) const
273 {
274  if(N==1) {
275  x.resize(gm_.numberOfVariables());
276  for(size_t j=0; j<x.size(); ++j) {
277  x[j] = movemaker_.state(j);
278  }
279  return NORMAL;
280  }
281  else {
282  return UNKNOWN;
283  }
284 }
285 
286 } // namespace opengm
287 
288 #endif // #ifndef OPENGM_ICM_HXX
virtual ValueType value() const
return the solution (value)
Definition: icm.hxx:88
The OpenGM namespace.
Definition: config.hxx:43
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
output a solution
Definition: icm.hxx:269
OPENGM_GM_TYPE_TYPEDEFS
Definition: icm.hxx:33
Movemaker< GraphicalModelType > MovemakerType
Definition: icm.hxx:34
std::vector< LabelType > startPoint_
Definition: icm.hxx:75
ICM< _GM, ACC > type
Definition: icm.hxx:41
void setStartingPoint(typename std::vector< LabelType >::const_iterator)
set initial labeling
Definition: icm.hxx:154
Parameter(MoveType moveType=SINGLE_VARIABLE)
Definition: icm.hxx:62
opengm::visitors::TimingVisitor< ICM< GM, ACC > > TimingVisitorType
Definition: icm.hxx:37
std::string name() const
Definition: icm.hxx:162
Parameter(const std::vector< LabelType > &startPoint)
Definition: icm.hxx:53
size_t currentMoveType() const
Definition: icm.hxx:101
MoveType moveType_
Definition: icm.hxx:74
ACC AccumulationType
Definition: icm.hxx:31
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
GM GraphicalModelType
Definition: icm.hxx:32
Inference algorithm interface.
Definition: inference.hxx:43
Iterated Conditional Modes Algorithm J. E. Besag, "On the Statistical Analysis of Dirty Pictures"...
Definition: icm.hxx:24
const GraphicalModelType & graphicalModel() const
Definition: icm.hxx:169
opengm::visitors::VerboseVisitor< ICM< GM, ACC > > VerboseVisitorType
Definition: icm.hxx:35
ICM< _GM, _ACC > type
Definition: icm.hxx:46
opengm::visitors::EmptyVisitor< ICM< GM, ACC > > EmptyVisitorType
Definition: icm.hxx:36
ICM(const GraphicalModelType &)
Definition: icm.hxx:108
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:48
InferenceTermination infer()
Definition: icm.hxx:176
void reset()
Definition: icm.hxx:138
Parameter(MoveType moveType, const std::vector< LabelType > &startPoint)
Definition: icm.hxx:57
OpenGM runtime error.
Definition: opengm.hxx:100
Parameter(const OP &otherParameter)
Definition: icm.hxx:68
InferenceTermination
Definition: inference.hxx:24