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