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