OpenGM  2.3.x
Discrete Graphical Model Library
trws_adsal.hxx
Go to the documentation of this file.
1 #ifndef TRWS_ADSAL_HXX_
2 #define TRWS_ADSAL_HXX_
7 
8 namespace opengm{
9 
10 template<class GM>
12 {
13  typedef typename GM::ValueType ValueType;
21 
22  ADSal_Parameter(size_t numOfExternalIterations=0,
23  ValueType precision=1.0,
24  bool absolutePrecision=true,
25  size_t numOfInternalIterations=3,
27  ValueType smoothingGapRatio=4,
28  ValueType startSmoothingValue=0.0,
29  ValueType primalBoundPrecision=std::numeric_limits<ValueType>::epsilon(),
31  size_t presolveMaxIterNumber=100,
32  bool canonicalNormalization=true,
35  bool lazyDerivativeComputation=false,
36  ValueType smoothingDecayMultiplier=-1.0,
37  //bool worstCaseSmoothing=false,
39  bool fastComputations=true,
40  bool verbose=false
41  )
42  :parent(numOfExternalIterations,
43  precision,
44  absolutePrecision,
45  numOfInternalIterations,
49  primalBoundPrecision,
51  presolveMaxIterNumber,
56  //worstCaseSmoothing,
59  verbose
60  ),
62  {};
63 
65 
68 
69 #ifdef TRWS_DEBUG_OUTPUT
70  void print(std::ostream& fout)const
71  {
72  parent::print(fout);
73  fout <<"lazyDerivativeComputation="<< lazyDerivativeComputation()<< std::endl;
74  }
75 #endif
76 };
77 
95 
96 template<class GM, class ACC>
97 class ADSal : public trws_base::SmoothingBasedInference<GM, ACC> //public Inference<GM, ACC>,SmoothingBasedInference<GM, ACC>
98 {
99 public:
101  typedef ACC AccumulationType;
102  typedef GM GraphicalModelType;
104 
105  typedef typename parent::Storage Storage;
109 
111 
115 
116  ADSal(const GraphicalModelType& gm,const Parameter& param
117 #ifdef TRWS_DEBUG_OUTPUT
118  ,std::ostream& fout=std::cout
119 #endif
120  )
121  :
122  parent(gm,param
123 #ifdef TRWS_DEBUG_OUTPUT
124  ,(param.verbose_ ? fout : *OUT::nullstream::Instance())
125 #endif
126  ),
127  _parameters(param)
128  {
129 #ifdef TRWS_DEBUG_OUTPUT
130  parent::_fout << "Parameters of the "<< name() <<" algorithm:"<<std::endl;
131  param.print(parent::_fout);
132 #endif
133 
134  if (param.numOfExternalIterations_==0) throw std::runtime_error("ADSal: a strictly positive number of iterations must be provided!");
135  };
136 
137  std::string name() const{ return "ADSal"; }
138  InferenceTermination infer(){EmptyVisitorType visitor; return infer(visitor);};
139  template<class VISITOR> InferenceTermination infer(VISITOR & visitor);
140 
141  /*
142  * for testing only!
143  */
145 private:
146  Parameter _parameters;
147 };
148 
149 template<class GM,class ACC>
150 template<class VISITOR>
152 {
154  size_t counter=0;
155 
156  visitor.addLog("oracleCalls");
157  visitor.addLog("primalLPbound");
158  visitor.begin();
159 
160  if (parent::_sumprodsolver.GetSmoothing()<=0.0)
161  {
162  if (parent::_Presolve(visitor, &counter)==CONVERGENCE)
163  {
164  parent::_SelectOptimalBoundsAndLabeling();
165  visitor();
166  visitor.log("oracleCalls",(double)counter);
167  visitor.log("primalLPbound",(double)parent::_bestPrimalLPbound);
168 
169  visitor.end();
170  return NORMAL;
171  }
172 #ifdef TRWS_DEBUG_OUTPUT
173  parent::_fout <<"Switching to the smooth solver============================================"<<std::endl;
174 #endif
175  counter+=parent::_EstimateStartingSmoothing(visitor);
176  }
177 
178  bool forwardMoveNeeded=true;
179  for (size_t i=0;i<_parameters.numOfExternalIterations_;++i)
180  {
181 #ifdef TRWS_DEBUG_OUTPUT
182  parent::_fout <<"Main iteration Nr."<<i<<"============================================"<<std::endl;
183 #endif
184 
185  InferenceTermination returncode;
186  counter+=_parameters.numberOfInternalIterations();
187  if (forwardMoveNeeded)
188  {
189  ++counter;returncode=parent::_sumprodsolver.infer();
190  forwardMoveNeeded=false;
191  }
192  else
193  returncode=parent::_sumprodsolver.core_infer();
194 
195  if (returncode==CONVERGENCE)
196  {
197  parent::_SelectOptimalBoundsAndLabeling();
198  visitor();
199  visitor.log("oracleCalls",(double)counter);
200  visitor.log("primalLPbound",(double)parent::_bestPrimalLPbound);
201  visitor.end();
202  return NORMAL;
203  }
204 
205 
206  ++counter;parent::_maxsumsolver.ForwardMove();//initializes a move, makes a forward move and computes the dual bound, is used also in derivative computation in the next line
207 #ifdef TRWS_DEBUG_OUTPUT
208  parent::_fout << "_maxsumsolver.bound()=" <<parent::_maxsumsolver.bound()<<std::endl;
209 #endif
210 
211  ValueType derivative;
212  if (parent::_isLPBoundComputed() || !_parameters.lazyDerivativeComputation())
213  {
214  ++counter; parent::_sumprodsolver.GetMarginalsAndDerivativeMove();
215  derivative=parent::_EstimateRhoDerivative();
216 #ifdef TRWS_DEBUG_OUTPUT
217  parent::_fout << "derivative="<<derivative<<std::endl;
218 #endif
219  forwardMoveNeeded=true;
220  }
221  else
222  derivative=parent::_FastEstimateRhoDerivative();
223 
224  if ( parent::_CheckStoppingCondition(&returncode))
225  {
226  visitor();
227  visitor.log("oracleCalls",(double)counter);
228  visitor.log("primalLPbound",(double)parent::_bestPrimalLPbound);
229  visitor.end();
230  return NORMAL;
231  }
232 
233 
234  size_t flag=visitor();
235  visitor.log("oracleCalls",(double)counter);
236  visitor.log("primalLPbound",(double)parent::_bestPrimalLPbound);
238  break;
239  }
240 
241  if (parent::_UpdateSmoothing(parent::_bestPrimalBound,parent::_maxsumsolver.bound(),parent::_sumprodsolver.bound(),derivative,i+1))
242  forwardMoveNeeded=true;
243  else if (parent::_sumprodsolver.ConvergenceFlag())
244  {
245 #ifdef TRWS_DEBUG_OUTPUT
246  parent::_fout << "Numerical Precision attained (smoothing can not be decreased further). Stopping." <<std::endl;
247 #endif
248  break;
249  }
250 
251  }
252 
253  parent::_SelectOptimalBoundsAndLabeling();
254  visitor();
255  visitor.log("oracleCalls",(double)counter);
256  visitor.log("primalLPbound",(double)parent::_bestPrimalLPbound);
257  visitor.end();
258 
259  return NORMAL;
260 }
261 
262 template<class GM,class ACC>
264 {
265  if (parent::_sumprodsolver.GetSmoothing()<=0.0)
266  {
267  parent::_EstimateStartingSmoothing();
268  }
269 
270  for (size_t i=0;i<_parameters.numOfExternalIterations_;++i)
271  {
272 #ifdef TRWS_DEBUG_OUTPUT
273  parent::_fout <<"Main iteration Nr."<<i<<"============================================"<<std::endl;
274 #endif
275  for (size_t innerIt=0;innerIt<_parameters.maxNumberOfIterations_;++innerIt)
276  {
277  parent::_sumprodsolver.ForwardMove();
278  parent::_sumprodsolver.BackwardMove();
279 #ifdef TRWS_DEBUG_OUTPUT
280  parent::_fout <<"subIter="<< innerIt<<", smoothDualBound=" << parent::_sumprodsolver.bound() <<std::endl;
281 #endif
282  }
283 
284  parent::_sumprodsolver.ForwardMove();
285  parent::_sumprodsolver.GetMarginalsAndDerivativeMove();
286  parent::_maxsumsolver.ForwardMove();//initializes a move, makes a forward move and computes the dual bound, is used also in derivative computation in the next line
287  ValueType derivative=parent::_EstimateRhoDerivative();
288 #ifdef TRWS_DEBUG_OUTPUT
289  parent::_fout << "derivative="<<derivative<<std::endl;
290 #endif
291  InferenceTermination returncode;
292  if ( parent::_CheckStoppingCondition(&returncode)) return returncode;
293 
294  parent::_UpdateSmoothing(parent::_bestPrimalBound,parent::_maxsumsolver.bound(),parent::_sumprodsolver.bound(),derivative,i+1);
295  }
296  return opengm::NORMAL;
297 }
298 
299 }
300 #endif
bool & lazyDerivativeComputation()
Definition: trws_adsal.hxx:66
The OpenGM namespace.
Definition: config.hxx:43
std::string name() const
Definition: trws_adsal.hxx:137
parent::SumProdSolver SumProdSolver
Definition: trws_adsal.hxx:106
InferenceTermination oldinfer()
Definition: trws_adsal.hxx:263
void log(const std::string &logName, double value)
parent::MaxSumSolver MaxSumSolver
Definition: trws_adsal.hxx:107
parent::SumProdSolverParametersType SumProdSolverParametersType
Definition: trws_adsal.hxx:17
InferenceTermination infer()
Definition: trws_adsal.hxx:138
visitors::TimingVisitor< ADSal< GM, ACC > > TimingVisitorType
Definition: trws_adsal.hxx:113
visitors::EmptyVisitor< ADSal< GM, ACC > > EmptyVisitorType
Definition: trws_adsal.hxx:114
SmoothingParametersType::SmoothingStrategyType SmoothingStrategyType
ADSal_Parameter(size_t numOfExternalIterations=0, ValueType precision=1.0, bool absolutePrecision=true, size_t numOfInternalIterations=3, typename Storage::StructureType decompositionType=Storage::GENERALSTRUCTURE, ValueType smoothingGapRatio=4, ValueType startSmoothingValue=0.0, ValueType primalBoundPrecision=std::numeric_limits< ValueType >::epsilon(), size_t maxPrimalBoundIterationNumber=100, size_t presolveMaxIterNumber=100, bool canonicalNormalization=true, ValueType presolveMinRelativeDualImprovement=0.01, bool lazyLPPrimalBoundComputation=true, bool lazyDerivativeComputation=false, ValueType smoothingDecayMultiplier=-1.0, SmoothingStrategyType smoothingStrategy=SmoothingParametersType::ADAPTIVE_DIMINISHING, bool fastComputations=true, bool verbose=false)
Definition: trws_adsal.hxx:22
SumProdTRWS_Parameters< ValueType > SumProdSolverParametersType
ADSal(const GraphicalModelType &gm, const Parameter &param)
Definition: trws_adsal.hxx:116
trws_base::DecompositionStorage< GM > Storage
Definition: trws_adsal.hxx:14
trws_base::DecompositionStorage< GM > Storage
const bool & lazyDerivativeComputation() const
Definition: trws_adsal.hxx:67
parent::Storage Storage
Definition: trws_adsal.hxx:105
trws_base::SmoothingBasedInference_Parameter< GM > parent
Definition: trws_adsal.hxx:15
parent::SmoothingStrategyType SmoothingStrategyType
Definition: trws_adsal.hxx:20
parent::MaxSumSolverParametersType MaxSumSolverParametersType
Definition: trws_adsal.hxx:18
[class adsal] ADSal - adaptive diminishing smoothing algorithm Based on the paper: B...
Definition: trws_adsal.hxx:97
trws_base::SmoothingBasedInference< GM, ACC > parent
Definition: trws_adsal.hxx:100
parent::PrimalBoundEstimator PrimalBoundEstimator
Definition: trws_adsal.hxx:108
trws_base::SumProdTRWS< GM, ACC > SumProdSolver
visitors::VerboseVisitor< ADSal< GM, ACC > > VerboseVisitorType
Definition: trws_adsal.hxx:112
parent::PrimalLPEstimatorParametersType PrimalLPEstimatorParametersType
Definition: trws_adsal.hxx:19
PrimalLPBound_Parameter< ValueType > PrimalLPEstimatorParametersType
ADSal_Parameter< GM > Parameter
Definition: trws_adsal.hxx:110
void addLog(const std::string &logName)
parent::SmoothingParametersType SmoothingParametersType
Definition: trws_adsal.hxx:16
trws_base::MaxSumTRWS< GM, ACC > MaxSumSolver
GM::ValueType ValueType
Definition: trws_adsal.hxx:13
InferenceTermination
Definition: inference.hxx:24