OpenGM  2.3.x
Discrete Graphical Model Library
dmc.hxx
Go to the documentation of this file.
1 #pragma once
2 #ifndef OPENGM_DMC_HXX
3 #define OPENGM_DMC_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 
18 
19 namespace opengm {
20 
21 
22 template<class GM, class INF>
23 class DMC : public Inference<GM, typename INF::AccumulationType>
24 {
25 public:
26 
27  typedef typename INF::AccumulationType ACC;
28  typedef ACC AccumulationType;
29  typedef GM GraphicalModelType;
31  typedef typename INF::Parameter InfParam;
35 
36  class Parameter {
37  public:
38 
40  const ValueType threshold = ValueType(-0.000000001),
41  const InfParam infParam = InfParam()
42  )
43  : threshold_(threshold),
44  infParam_(infParam){
45 
46  }
47 
49  InfParam infParam_;
50  };
51 
52  DMC(const GraphicalModelType&, const Parameter&);
53  std::string name() const;
54  const GraphicalModelType& graphicalModel() const;
56  void reset();
57  template<class VisitorType>
58  InferenceTermination infer(VisitorType&);
59  void setStartingPoint(typename std::vector<LabelType>::const_iterator);
60  virtual InferenceTermination arg(std::vector<LabelType>&, const size_t = 1) const ;
61  virtual ValueType value()const{
62  assert(false); // FIXME: the return of this function was missing, just added something arbitrary
63  return ValueType();
64  }
65 
66 private:
67  const GraphicalModelType& gm_;
68  Parameter param_;
69 
70  ValueType value_;
71  std::vector<LabelType> arg_;
72 };
73 
74 template<class GM, class INF>
75 inline
77 (
78  const GraphicalModelType& gm,
79  const Parameter& parameter
80 )
81 : gm_(gm),
82  param_(parameter),
83  value_(),
84  arg_(gm.numberOfVariables(), 0) {
85 
86 }
87 
88 
89 
90 template<class GM, class INF>
91 inline void
93 {
94 
95 }
96 
97 template<class GM, class INF>
98 inline void
100 (
101  typename std::vector<typename DMC<GM,INF>::LabelType>::const_iterator begin
102 ) {
103 }
104 
105 template<class GM, class INF>
106 inline std::string
108 {
109  return "DMC";
110 }
111 
112 template<class GM, class INF>
113 inline const typename DMC<GM, INF>::GraphicalModelType&
115 {
116  return gm_;
117 }
118 
119 template<class GM, class INF>
122 {
123  EmptyVisitorType v;
124  return infer(v);
125 }
126 
127 
128 template<class GM, class INF>
129 template<class VisitorType>
131 (
132  VisitorType& visitor
133 )
134 {
135 
136  visitor.begin(*this);
137 
138 
139  LabelType lAA[2]={0, 0};
140  LabelType lAB[2]={0, 1};
141 
142  // decomposition
143  Partition<LabelType> ufd(gm_.numberOfVariables());
144  for(size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
145  if(gm_[fi].numberOfVariables()==2){
146 
147  const ValueType val00 = gm_[fi](lAA);
148  const ValueType val01 = gm_[fi](lAB);
149  const ValueType weight = val01 - val00;
150 
151  if(weight>param_.threshold_){
152  const size_t vi0 = gm_[fi].variableIndex(0);
153  const size_t vi1 = gm_[fi].variableIndex(1);
154  ufd.merge(vi0, vi1);
155  }
156  }
157  else{
158  throw RuntimeError("wrong factor order for multicut");
159  }
160  }
161 
162  if(ufd.numberOfSets() == 1){
163  //std::cout<<" all in one cc\n";
164  // FALL BACK HERE!!!
165  typedef typename INF:: template rebind<GM,ACC>::type OrgInf;
166  typename OrgInf::Parameter orgInfParam(param_.infParam_);
167  OrgInf orgInf(gm_, orgInfParam);
168  orgInf.infer();
169  orgInf.arg(arg_);
170  value_ = gm_.evaluate(arg_);
171  }
172  else {
173  //std::cout<<" NOT all in one cc\n";
174  std::map<LabelType, LabelType> repr;
175  ufd.representativeLabeling(repr);
176  //std::cout<<"gm_.numVar "<<gm_.numberOfVariables()<<"\n";
177  //std::cout<<"reprs size"<<repr.size()<<"\n";
178  //std::cout<<"ufd.numberOfSets() "<<ufd.numberOfSets()<<"\n";
179  std::vector< std::vector< LabelType> > subVar(ufd.numberOfSets());
180  // set up the sub var
181  for(size_t vi=0; vi<gm_.numberOfVariables(); ++vi){
182  subVar[repr[ufd.find(vi)]].push_back(vi);
183  }
184 
185  const size_t nSubProb = subVar.size();
186 
187  std::vector<unsigned char> usedFactors_(gm_.numberOfFactors(),0);
188 
189  // mark all factors where weight is smaller
190  // as param_.threshold_ as used
191  for(size_t fi=0; fi< gm_.numberOfFactors(); ++fi){
192  if(
193  ufd.find(gm_[fi].variableIndex(0))
194  !=
195  ufd.find(gm_[fi].variableIndex(1))
196  )
197  {
198  usedFactors_[fi] = 1;
199  }
200  }
201 
202  std::vector<IndexType> globalToLocal(gm_.numberOfVariables(), gm_.numberOfVariables()+1);
203 
204  IndexType offset = 0;
205  for(size_t subProb = 0; subProb<nSubProb; ++subProb){
206 
207  //std::cout<<"subProb "<<subProb<<"\n";
208  const IndexType nSubVar = subVar[subProb].size();
209  //std::cout<<"nSubVar "<<nSubVar<<"\n";
210 
214  Space space(nSubVar, nSubVar);
215  Model subGm(space);
216 
217  for(IndexType lvi=0; lvi<nSubVar; ++lvi){
218  const IndexType gvi = subVar[subProb][lvi];
219  globalToLocal[gvi] = lvi;
220  }
221 
222 
223  if(nSubVar==1){
224  const IndexType gvi = subVar[subProb][0];
225  arg_[gvi] = offset;
226  }
227  else if(nSubVar==2){
228  const IndexType gvi0 = subVar[subProb][0];
229  const IndexType gvi1 = subVar[subProb][1];
230  arg_[gvi0] = offset;
231  arg_[gvi1] = offset;
232  }
233  else{
234 
235  for(IndexType lvi=0; lvi<nSubVar; ++lvi){
236  const IndexType gvi = subVar[subProb][lvi];
237  OPENGM_CHECK_OP(lvi, == , globalToLocal[gvi], ' ');
238  // number of factors
239  const size_t nf = gm_.numberOfFactors(gvi);
240 
241  for(size_t f=0; f<nf; ++f){
242  const IndexType nfi = gm_.factorOfVariable(gvi, f);
243  if(usedFactors_[nfi] != 1){
244  usedFactors_[nfi] = 1;
245 
246  // add factor to graphical model
247  const ValueType val00 = gm_[nfi](lAA);
248  const ValueType val01 = gm_[nfi](lAB);
249  const ValueType weight = val01 - val00;
250  const IndexType vi0 = gm_[nfi].variableIndex(0);
251  const IndexType vi1 = gm_[nfi].variableIndex(1);
252 
253  if( ufd.find(vi0) != ufd.find(vi1)){
254  OPENGM_CHECK_OP(ufd.find(vi0),!=,ufd.find(vi1), "internal error");
255  }
256 
257  const IndexType lvis[] = {
258  std::min(globalToLocal[vi0],globalToLocal[vi1]),
259  std::max(globalToLocal[vi0],globalToLocal[vi1])
260  };
261  const Pf pf(nSubVar, nSubVar, 0.0, weight);
262  subGm.addFactor(subGm.addFunction(pf), lvis, lvis+2);
263  }
264  }
265  }
266 
267  // infer the submodel
268  typedef typename INF:: template rebind<Model,ACC>::type SubInf;
269  typename SubInf::Parameter subInfParam(param_.infParam_);
270  SubInf subInf(subGm, subInfParam);
271  subInf.infer();
272 
273  std::vector<LabelType> subArg(subGm.numberOfVariables());
274  subInf.arg(subArg);
275 
276  for(IndexType lvi=0; lvi<nSubVar; ++lvi){
277  const IndexType gvi = subVar[subProb][lvi];
278  arg_[gvi] = subArg[lvi] + offset;
279  }
280  }
281 
282  offset += nSubVar;
283  }
284  value_ = gm_.evaluate(arg_);
285  visitor.end(*this);
286 
287  }
288  return NORMAL;
289 }
290 
291 template<class GM, class INF>
294 (
295  std::vector<LabelType>& x,
296  const size_t N
297 ) const
298 {
299  if(N==1) {
300  x.resize(gm_.numberOfVariables());
301  for(size_t j=0; j<x.size(); ++j) {
302  x[j] =arg_[j];
303  }
304  return NORMAL;
305  }
306  else {
307  return UNKNOWN;
308  }
309 }
310 
311 } // namespace opengm
312 
313 #endif // #ifndef OPENGM_DMC_HXX
virtual ValueType value() const
return the solution (value)
Definition: dmc.hxx:61
INF::AccumulationType ACC
Definition: dmc.hxx:27
opengm::visitors::VerboseVisitor< DMC< GM, INF > > VerboseVisitorType
Definition: dmc.hxx:32
The OpenGM namespace.
Definition: config.hxx:43
InfParam infParam_
Definition: dmc.hxx:49
Discrete space in which all variables have the same number of labels.
const GraphicalModelType & graphicalModel() const
Definition: dmc.hxx:114
std::string name() const
Definition: dmc.hxx:107
OPENGM_GM_TYPE_TYPEDEFS
Definition: dmc.hxx:30
Parameter(const ValueType threshold=ValueType(-0.000000001), const InfParam infParam=InfParam())
Definition: dmc.hxx:39
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:49
opengm::visitors::EmptyVisitor< DMC< GM, INF > > EmptyVisitorType
Definition: dmc.hxx:33
virtual InferenceTermination arg(std::vector< LabelType > &, const size_t=1) const
Definition: dmc.hxx:294
InferenceTermination infer()
Definition: dmc.hxx:121
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:50
opengm::visitors::TimingVisitor< DMC< GM, INF > > TimingVisitorType
Definition: dmc.hxx:34
Inference algorithm interface.
Definition: inference.hxx:43
ValueType threshold_
Definition: dmc.hxx:48
#define OPENGM_CHECK_OP(A, OP, B, TXT)
Definition: submodel2.hxx:24
Potts function for two variables.
Definition: potts.hxx:19
INF::Parameter InfParam
Definition: dmc.hxx:31
void setStartingPoint(typename std::vector< LabelType >::const_iterator)
Definition: dmc.hxx:100
Disjoint set data structure with path compression.
Definition: partition.hxx:13
GM GraphicalModelType
Definition: dmc.hxx:29
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:48
DMC(const GraphicalModelType &, const Parameter &)
Definition: dmc.hxx:77
OpenGM runtime error.
Definition: opengm.hxx:100
void reset()
Definition: dmc.hxx:92
ACC AccumulationType
Definition: dmc.hxx:28
InferenceTermination
Definition: inference.hxx:24