OpenGM  2.3.x
Discrete Graphical Model Library
smoothing_strategy.hxx
Go to the documentation of this file.
1 /*
2  * smoothing_strategy.hxx
3  *
4  * Created on: Jan 7, 2014
5  * Author: bsavchyn
6  */
7 
8 #ifndef SMOOTHING_STRATEGY_HXX_
9 #define SMOOTHING_STRATEGY_HXX_
12 #include <algorithm>
13 
14 namespace opengm{
15 namespace trws_base{
16 
17 template<class VALUETYPE>
19 {
20  typedef VALUETYPE ValueType;
22 
23 
24  static SmoothingStrategyType getSmoothingStrategyType(const std::string& name)
25  {
26  if (name.compare("WC_DIMINISHING")==0) return WC_DIMINISHING;
27  else if (name.compare("ADAPTIVE_PRECISIONORIENTED")==0) return ADAPTIVE_PRECISIONORIENTED;
28  else if (name.compare("WC_PRECISIONORIENTED")==0) return WC_PRECISIONORIENTED;
29  else if (name.compare("FIXED")==0) return FIXED;
30  else return ADAPTIVE_DIMINISHING;
31  }
32 
33  static std::string getString(SmoothingStrategyType strategytype)
34  {
35  switch (strategytype)
36  {
37  case ADAPTIVE_DIMINISHING: return std::string("ADAPTIVE_DIMINISHING");
38  case WC_DIMINISHING : return std::string("WC_DIMINISHING");
39  case ADAPTIVE_PRECISIONORIENTED : return std::string("ADAPTIVE_PRECISIONORIENTED");
40  case WC_PRECISIONORIENTED : return std::string("WC_PRECISIONORIENTED");
41  case FIXED: return std::string("FIXED");
42  default: return std::string("UNKNOWN");
43  }
44  }
45 
46  SmoothingParameters(ValueType smoothingGapRatio=4,
47  ValueType smoothingValue=0.0,
48  ValueType smoothingDecayMultiplier=-1.0,
49  ValueType precision=0,
51  smoothingGapRatio_(smoothingGapRatio),
52  smoothingValue_(smoothingValue),
53  smoothingDecayMultiplier_(smoothingDecayMultiplier),
54  precision_(precision),
55  smoothingStrategy_(smoothingStrategy){};
56 
57  ValueType smoothingGapRatio_;
58  ValueType smoothingValue_;
60  ValueType precision_;
62 };
63 
64 
65 
66 
67 
68 //template<class VALUETYPE,class GM>
69 template<class GM>
71 {
72  //typedef VALUETYPE ValueType;
73  typedef typename GM::ValueType ValueType;
79 
81  bool lazyLPPrimalBoundComputation,
82  const SmoothingParametersType& smoothingParameters,
83  const SumProdSolverParametersType& sumProdSolverParameters,
84  const MaxSumSolverParametersType& maxSumSolverParameters,
85  const PrimalLPEstimatorParametersType& primalLPEstimatorParameters):
86  decompositionType_(decompositionType),
87  lazyLPPrimalBoundComputation_(lazyLPPrimalBoundComputation),
88  smoothingParameters_(smoothingParameters),
89  sumProdSolverParameters_(sumProdSolverParameters),
90  maxSumSolverParameters_(maxSumSolverParameters),
91  primalLPEstimatorParameters_(primalLPEstimatorParameters)
92  {};
94 
95  typename Storage::StructureType decompositionType_;
97  const SmoothingParametersType& getSmoothingParameters()const{return smoothingParameters_;}
98  const SumProdSolverParametersType& getSumProdSolverParameters()const{return sumProdSolverParameters_;}
99  const MaxSumSolverParametersType& getMaxSumSolverParameters()const{return maxSumSolverParameters_;}
100  const PrimalLPEstimatorParametersType& getPrimalLPEstimatorParameters()const{return primalLPEstimatorParameters_;}
101 protected:
102  SmoothingParametersType smoothingParameters_;
103  SumProdSolverParametersType sumProdSolverParameters_;
104  MaxSumSolverParametersType maxSumSolverParameters_;
105  PrimalLPEstimatorParametersType primalLPEstimatorParameters_;
106 };
107 
108 //template<class ValueType,class GM>
109 template<class GM>
110 //struct SmoothingBasedInference_Parameter : public trws_base::SmoothingBasedInference_Parameter_Base<typename GM::ValueType,GM>
112 {
113  typedef typename GM::ValueType ValueType;
121 
122  SmoothingBasedInference_Parameter(size_t numOfExternalIterations=0,
123  ValueType precision=1.0,
124  bool absolutePrecision=true,
125  size_t numOfInternalIterations=3,
127  ValueType smoothingGapRatio=4,
128  ValueType startSmoothingValue=0.0,
129  ValueType primalBoundPrecision=std::numeric_limits<ValueType>::epsilon(),
131  size_t presolveMaxIterNumber=100,
132  bool canonicalNormalization=true,
133  ValueType presolveMinRelativeDualImprovement=0.01,
135  ValueType smoothingDecayMultiplier=-1.0,
137 // bool worstCaseSmoothing=false,
138  bool fastComputations=true,
139  bool verbose=false
140  )
141  :parent(decompositionType,
144  //SumProdSolverParametersType(numOfInternalIterations,startSmoothingValue,precision,absolutePrecision,std::numeric_limits<ValueType>::epsilon(),fastComputations,canonicalNormalization),
145  SumProdSolverParametersType(numOfInternalIterations,startSmoothingValue,precision,absolutePrecision,0.0,fastComputations,canonicalNormalization),
146  MaxSumSolverParametersType(presolveMaxIterNumber,precision,absolutePrecision,presolveMinRelativeDualImprovement,fastComputations,canonicalNormalization),
147  PrimalLPEstimatorParametersType(primalBoundPrecision,maxPrimalBoundIterationNumber)
148  ),
149  numOfExternalIterations_(numOfExternalIterations),
151  {};
152 
154  bool verbose_;
155 
156  /*
157  * Main algorithm parameters
158  */
160  const size_t& maxNumberOfIterations()const {return numOfExternalIterations_;}
161 
162  size_t& numberOfInternalIterations(){return parent::sumProdSolverParameters_.maxNumberOfIterations_;}
163  const size_t& numberOfInternalIterations()const{return parent::sumProdSolverParameters_.maxNumberOfIterations_;}
164 
165 // ValueType& precision(){return parent::sumProdSolverParameters_.precision_;}
166  const ValueType& precision()const{return parent::sumProdSolverParameters_.precision_;}
168 
169  bool& isAbsolutePrecision(){return parent::sumProdSolverParameters_.absolutePrecision_;}
170  const bool& isAbsolutePrecision()const {return parent::sumProdSolverParameters_.absolutePrecision_;}
171 
174 
177 
178 // bool& lazyDerivativeComputation(){return lazyDerivativeComputation_;}
179 // const bool& lazyDerivativeComputation()const {return lazyDerivativeComputation_;}
180 
183 
184 // bool& worstCaseSmoothing(){return parent::smoothingParameters_.worstCaseSmoothing_;}
185 // const bool& worstCaseSmoothing()const{return parent::smoothingParameters_.worstCaseSmoothing_;}
187  const SmoothingStrategyType& smoothingStrategy()const{return parent::smoothingParameters_.smoothingStrategy_;}
188 
191 
192  const ValueType& startSmoothingValue()const{return parent::sumProdSolverParameters_.smoothingValue_;}
193  void setStartSmoothingValue(ValueType val){ parent::smoothingParameters_.smoothingValue_=parent::sumProdSolverParameters_.smoothingValue_=val;}//TODO: duplicated!
194 
195  const bool& fastComputations()const{return parent::sumProdSolverParameters_.fastComputations_;}
196  void setFastComputations(bool fast){parent::sumProdSolverParameters_.fastComputations_=parent::maxSumSolverParameters_.fastComputations_=fast;}
197 
198  const bool& canonicalNormalization()const{return parent::sumProdSolverParameters_.canonicalNormalization_;}
199  void setCanonicalNormalization(bool canonical){parent::sumProdSolverParameters_.canonicalNormalization_=parent::maxSumSolverParameters_.canonicalNormalization_=canonical;}
200 
201  /*
202  * Presolve parameters
203  */
204  size_t& maxNumberOfPresolveIterations(){return parent::maxSumSolverParameters_.maxNumberOfIterations_;}
205  const size_t& maxNumberOfPresolveIterations()const{return parent::maxSumSolverParameters_.maxNumberOfIterations_;}
206 
207  ValueType& presolveMinRelativeDualImprovement() {return parent::maxSumSolverParameters_.minRelativeDualImprovement_;}
208  const ValueType& presolveMinRelativeDualImprovement()const {return parent::maxSumSolverParameters_.minRelativeDualImprovement_;}
209 
210  /*
211  * Fractional primal bound estimator parameters
212  */
214  const size_t& maxPrimalBoundIterationNumber()const{return parent::primalLPEstimatorParameters_.maxIterationNumber_;}
215 
217  const ValueType& primalBoundRelativePrecision()const{return parent::primalLPEstimatorParameters_.relativePrecision_;}
218 
219  bool& verbose(){return verbose_;}
220  const bool& verbose()const{return verbose_;}
221 
222 #ifdef TRWS_DEBUG_OUTPUT
223  void print(std::ostream& fout)const
224  {
225  fout << "maxNumberOfIterations="<< maxNumberOfIterations()<<std::endl;
226  fout << "numberOfInternalIterations="<< numberOfInternalIterations()<<std::endl;
227  fout << "precision=" <<precision()<<std::endl;
228  fout <<"isAbsolutePrecision=" << isAbsolutePrecision()<< std::endl;
229  fout <<"smoothingGapRatio=" << smoothingGapRatio()<< std::endl;
230  fout <<"lazyLPPrimalBoundComputation="<<lazyLPPrimalBoundComputation()<< std::endl;
231  fout <<"smoothingDecayMultiplier=" << smoothingDecayMultiplier()<< std::endl;
232 
233  fout << "smoothing strategy="<<SmoothingParametersType::getString(smoothingStrategy())<<std::endl;
234  fout << "decompositionType=" << Storage::getString(decompositionType()) << std::endl;
235 
236  fout <<"startSmoothingValue=" << startSmoothingValue()<<std::endl;
237  fout <<"fastComputations="<<fastComputations()<<std::endl;
238  fout <<"canonicalNormalization="<<canonicalNormalization()<<std::endl;
239 
240  /*
241  * Presolve parameters
242  */
243  fout <<"maxNumberOfPresolveIterations="<<maxNumberOfPresolveIterations()<<std::endl;
244  fout <<"presolveMinRelativeDualImprovement=" <<presolveMinRelativeDualImprovement()<<std::endl;
245 
246  /*
247  * Fractional primal bound estimator parameters
248  */
249  fout <<"maxPrimalBoundIterationNumber="<<maxPrimalBoundIterationNumber()<<std::endl;
250  fout <<"primalBoundRelativePrecision=" <<primalBoundRelativePrecision()<<std::endl;
251  fout <<"verbose="<<verbose()<<std::endl;
252  }
253 #endif
254 };
255 
256 
257 
258 //====================================================================================
259 template<class GM, class ACC>
261 
262 template<class GM,class ACC>
264 {
265 public:
266  typedef ACC AccumulationType;
267  typedef GM GraphicalModelType;
270 
271  SmoothingStrategy(ValueType smoothingMultiplier, const Parameter& param=Parameter(), std::ostream& fout=std::cout):
272  _fout(fout),
273  _initializationStage(true),
274  _smoothingMultiplier(smoothingMultiplier),
275  _parameters(param),
276  _oracleCallsCounter(0){};
277  virtual ~SmoothingStrategy(){};
278  ValueType InitSmoothing(SmoothingBasedInference<GM,ACC>& smoothInference,
279  ValueType primalBound,
280  ValueType dualBound){
281  //ValueType initialSmoothing=getStartingSmoothing();
282  return _InitAdaptiveSmoothing(smoothInference,primalBound,dualBound,getStartingSmoothing(primalBound,dualBound));
283  };
284  ValueType UpdateSmoothing(ValueType smoothingValue,
285  ValueType primalBound,
286  ValueType dualBound,
287  ValueType smoothDualBound,
288  ValueType smoothingDerivative,
289  size_t iterationCounter)
290  {
291  if (SmoothingMustBeDecreased(smoothingValue,primalBound,dualBound,smoothDualBound,smoothingDerivative,iterationCounter))
292  {
293  smoothingValue= std::max((ValueType)(smoothingValue/2.0),SmoothingEstimateValue(primalBound,smoothDualBound,smoothingDerivative));
294 
295 #ifdef TRWS_DEBUG_OUTPUT
296  _fout << "Smoothing decreased to = "<<smoothingValue<<std::endl;
297 #endif
298  }
299  return smoothingValue;
300  };
301 
302  bool SmoothingMustBeDecreased(ValueType smoothingValue,
303  ValueType primalBound,
304  ValueType dualBound,
305  ValueType smoothDualBound,
306  ValueType smoothingDerivative,
307  size_t iterationCounter)
308  {
309  ValueType rhs=fabs(primalBound-smoothDualBound);
310 
311  return (smoothingValue > SmoothingEstimateValue(primalBound,smoothDualBound,smoothingDerivative));
312  }
313 
314 
315  //---------------------------------------------------
316 
317  template<class DualDecompositionStorage >
318  static ValueType ComputeSmoothingMultiplier(const DualDecompositionStorage& storage)
319  {
320  //compute max number of labels
321  typename DualDecompositionStorage::LabelType numOfLabels=0;
322  for (size_t i=0;i<storage.numberOfSharedVariables();++i)
323  numOfLabels=std::max(numOfLabels,storage.numberOfLabels(i));
324 
325  //multiplier = \sum_{i=1}^n\log|\SX_i| <= \sum_{i=1}^n numOfLabels
326  ValueType multiplier=0;
327  ValueType logLabels=log((ValueType)numOfLabels);
328  for (size_t i=0;i<storage.numberOfModels();++i)
329  multiplier+=storage.size(i)*logLabels;
330 
331  return multiplier;
332  }
333 
335 
336  virtual ValueType SmoothingEstimateValue(
337  ValueType primalBound,
338  ValueType smoothDualBound,
339  ValueType smoothingDerivative)const=0;
340 
341  ValueType getStartingSmoothing(ValueType primalBound,ValueType dualBound)const
342  {
343  if (_parameters.smoothingValue_ > 0 )
345  else
346  return SmoothingEstimateValue(primalBound,dualBound,_smoothingMultiplier);
347  }
348 
350 
351 
352 protected:
353  ValueType _InitAdaptiveSmoothing(SmoothingBasedInference<GM,ACC>& smoothInference,
354  ValueType primalBound,
355  ValueType dualBound,
356  ValueType initialSmoothing);
357 
358  std::ostream& _fout;
361  Parameter _parameters;
363 };
364 
365 template<class GM, class ACC>
368  ValueType primalBound,
369  ValueType dualBound,
370  ValueType smoothing)//initial smoothing
371 {
372 #ifdef TRWS_DEBUG_OUTPUT
373  _fout <<"_maxsumsolver.value()="<<primalBound<<", _maxsumsolver.bound()="<<dualBound<<std::endl;
374  _fout << "Initial smoothing="<<smoothing<<std::endl;
375 #endif
376 
377  ValueType derivativeValue;
378  ValueType smoothDualBound=smoothInference.UpdateSmoothDualEstimates(smoothing,&derivativeValue);
379 
380  _oracleCallsCounter=1;
381  do{
382  ++_oracleCallsCounter;
383  smoothing*=2;
384 #ifdef TRWS_DEBUG_OUTPUT
385  _fout << "test smoothing = "<<smoothing<<std::endl;
386 #endif
387  smoothDualBound=smoothInference.UpdateSmoothDualEstimates(smoothing,&derivativeValue);
388 #ifdef TRWS_DEBUG_OUTPUT
389  _fout <<"smoothDualBound="<<smoothDualBound<<std::endl;
390 #endif
391  }while (!SmoothingMustBeDecreased(smoothing,primalBound,dualBound,smoothDualBound,derivativeValue,0));
392  smoothing/=2.0;
393 
394 
395 #ifdef TRWS_DEBUG_OUTPUT
396  _fout << "Smoothing := "<<smoothing<<std::endl;
397 #endif
398  _initializationStage= false;
399  return smoothing;
400 }
401 
402 //-----------------------------------------------------------------------
403 
404 template<class GM,class ACC>
406 {
407 public:
408  typedef ACC AccumulationType;
409  typedef GM GraphicalModelType;
413 
414  WorstCasePrecisionOrientedSmoothing(ValueType smoothingMultiplier,const Parameter& param=Parameter(), std::ostream& fout=std::cout)
415  :parent(smoothingMultiplier,param,fout)
416  {
417  if (param.smoothingValue_ > 0 )
418  throw std::runtime_error("WorstCasePrecisionOrientedSmoothing()::WorstCasePrecisionOrientedSmoothing(): Error! Starting smoothing value can not be selected manually! parameter.smoothingValue_ must be<0.");
419  };
420  virtual ValueType SmoothingEstimateValue(
421  ValueType primalBound,
422  ValueType smoothDualBound,
423  ValueType smoothingDerivative)const
425 };
426 
427 //-----------------------------------------------------------------------
428 
429 template<class GM,class ACC>
431 {
432 public:
433  typedef ACC AccumulationType;
434  typedef GM GraphicalModelType;
438 
439  AdaptivePrecisionOrientedSmoothing(ValueType smoothingMultiplier,const Parameter& param=Parameter(), std::ostream& fout=std::cout):parent(smoothingMultiplier,param,fout){};
440 
441  ValueType SmoothingEstimateValue(ValueType primalBound,
442  ValueType smoothDualBound,
443  ValueType smoothingDerivative)const
444  {return parent::_parameters.precision_/2.0/fabs(smoothingDerivative);}
445 
446 };
447 
448 //-----------------------------------------------------------------------
449 
450 template<class GM,class ACC>
452 {
453 public:
454  typedef ACC AccumulationType;
455  typedef GM GraphicalModelType;
459 
460  WorstCaseDiminishingSmoothing(ValueType smoothingMultiplier,const Parameter& param=Parameter(), std::ostream& fout=std::cout):parent(smoothingMultiplier,param,fout){};
461 
463  ValueType primalBound,
464  ValueType smoothDualBound,
465  ValueType smoothingDerivative)const
466  {return fabs(primalBound-smoothDualBound)/parent::_parameters.smoothingGapRatio_/fabs(parent::_smoothingMultiplier);}
467 
468 };
469 
470 
471 //-----------------------------------------------------------------------
472 template<class GM,class ACC>
474 {
475 public:
476  typedef ACC AccumulationType;
477  typedef GM GraphicalModelType;
481 
482  AdaptiveDiminishingSmoothing(ValueType smoothingMultiplier,const Parameter& param=Parameter(), std::ostream& fout=std::cout)
483  :parent(smoothingMultiplier,param,fout){};
484 
486  ValueType primalBound,
487  ValueType smoothDualBound,
488  ValueType smoothingDerivative)const
489  {return fabs(primalBound-smoothDualBound)/parent::_parameters.smoothingGapRatio_/fabs(smoothingDerivative);}
490 };
491 
492 //-----------------------------------------------------------------------
493 
494 template<class GM,class ACC>
495 class FixedSmoothing : public SmoothingStrategy<GM,ACC>
496 {
497 public:
498  typedef ACC AccumulationType;
499  typedef GM GraphicalModelType;
503 
504  FixedSmoothing(ValueType smoothingMultiplier,const Parameter& param=Parameter(), std::ostream& fout=std::cout)
505  :parent(smoothingMultiplier,param,fout)
506  {
507  if (param.smoothingValue_ <= 0 )
508  throw std::runtime_error("FixedSmoothing()::FixedSmoothing(): Smoothing value must be positive!");
509  };
510 
511  ValueType SmoothingEstimateValue(ValueType primalBound,
512  ValueType smoothDualBound,
513  ValueType smoothingDerivative)const
515 };
516 
517 //==============================================================
518 template<class GM, class ACC>
519 class SmoothingBasedInference : public Inference<GM, ACC>
520 {
521 public:
523  typedef ACC AccumulationType;
524  typedef GM GraphicalModelType;
526 
531 
532  //typedef typename MaxSumSolver::ReparametrizerType ReparametrizerType;
535 
537 
538  SmoothingBasedInference(const GraphicalModelType& gm, const Parameter& param
539 #ifdef TRWS_DEBUG_OUTPUT
540  ,std::ostream& fout=std::cout
541 #endif
542  ):
543  _parameters(param),
544  _storage(gm,param.decompositionType_),
545  _sumprodsolver(_storage,param.getSumProdSolverParameters()
546 #ifdef TRWS_DEBUG_OUTPUT
547  ,fout
548 #endif
549  ),
550  _maxsumsolver(_storage,param.getMaxSumSolverParameters()
551 #ifdef TRWS_DEBUG_OUTPUT
552  ,fout//fout
553 #endif
554  ),
555  _estimator(gm,param.getPrimalLPEstimatorParameters()),
556 #ifdef TRWS_DEBUG_OUTPUT
557  _fout(fout),
558 #endif
559  _bestPrimalLPbound(ACC::template neutral<ValueType>()),
560  _bestPrimalBound(ACC::template neutral<ValueType>()),
561  _bestDualBound(ACC::template ineutral<ValueType>()),
562  _bestIntegerBound(ACC::template neutral<ValueType>()),
563  _bestIntegerLabeling(_storage.masterModel().numberOfVariables(),0.0)
564  {
565  ValueType smoothingMultiplier=SmoothingStrategyType::ComputeSmoothingMultiplier(_storage);
566 
567  if ( (param.smoothingStrategy()==Parameter::SmoothingParametersType::WC_PRECISIONORIENTED) ||
568  (param.smoothingStrategy()==Parameter::SmoothingParametersType::ADAPTIVE_PRECISIONORIENTED))
569  if (!param.isAbsolutePrecision())
570  throw std::runtime_error("SmoothingBasedInference: Error: relative precision can be used only with diminishing smoothing.");
571 
572  switch (param.smoothingStrategy())
573  {
574  case Parameter::SmoothingParametersType::ADAPTIVE_DIMINISHING:
575  psmoothingStrategy=new typename trws_base::AdaptiveDiminishingSmoothing<GM,ACC>(smoothingMultiplier,param.getSmoothingParameters()
576 #ifdef TRWS_DEBUG_OUTPUT
577  ,_fout
578 #endif
579  );
580  break;
581  case Parameter::SmoothingParametersType::WC_DIMINISHING:
582  psmoothingStrategy=new typename trws_base::WorstCaseDiminishingSmoothing<GM,ACC>(smoothingMultiplier,param.getSmoothingParameters()
583 #ifdef TRWS_DEBUG_OUTPUT
584  ,_fout
585 #endif
586  );
587  break;
588  case Parameter::SmoothingParametersType::ADAPTIVE_PRECISIONORIENTED:
589  psmoothingStrategy=new typename trws_base::AdaptivePrecisionOrientedSmoothing<GM,ACC>(smoothingMultiplier,param.getSmoothingParameters()
590 #ifdef TRWS_DEBUG_OUTPUT
591  ,_fout
592 #endif
593  );
594  break;
595  case Parameter::SmoothingParametersType::WC_PRECISIONORIENTED:
596  psmoothingStrategy=new typename trws_base::WorstCasePrecisionOrientedSmoothing<GM,ACC>(smoothingMultiplier,param.getSmoothingParameters()
597 #ifdef TRWS_DEBUG_OUTPUT
598  ,_fout
599 #endif
600  );
601  break;
602  case Parameter::SmoothingParametersType::FIXED:
603  psmoothingStrategy=new typename trws_base::FixedSmoothing<GM,ACC>(smoothingMultiplier,param.getSmoothingParameters()
604  #ifdef TRWS_DEBUG_OUTPUT
605  ,_fout
606  #endif
607  );
608  break;
609  default: throw std::runtime_error("SmoothingBasedInference: Error: Unknown smoothing strategy type");
610  };
611  }
612 
614 
615  InferenceTermination arg(std::vector<LabelType>& out, const size_t = 1) const
616  {out = _bestIntegerLabeling;
617  return opengm::NORMAL;}
618  const GraphicalModelType& graphicalModel() const { return _storage.masterModel(); }
619 
620  ValueType bound() const{return _bestDualBound;}
621  ValueType value() const{return _bestIntegerBound;}
622 
623  ValueType UpdateSmoothDualEstimates(ValueType smoothingValue,ValueType* pderivativeValue);
624 
625  void getTreeAgreement(std::vector<bool>& out,std::vector<LabelType>* plabeling=0,std::vector<std::vector<LabelType> >* ptreeLabelings=0){_maxsumsolver.getTreeAgreement(out,plabeling,ptreeLabelings);}
626  Storage& getDecompositionStorage(){return _storage;}
628 
629  ReparametrizerType * getReparametrizer(const typename ReparametrizerType::Parameter& params=typename ReparametrizerType::Parameter())//const //TODO: make it constant
631 
633  {
634  _marginalsTemp.resize(_storage.numberOfLabels(varID));
636  // OPENGM_ASSERT(_marginalsTemp.size() == out.size());
637  out.assign(graphicalModel(), &varID, &varID+1, ACC::template neutral<ValueType>());
638  for (LabelType i=0;i<out.size();++i)
639  out(i)=_marginalsTemp[i];
640  }
641 protected:
642  template<class VISITOR>
643  InferenceTermination _Presolve(VISITOR& visitor,size_t* piterCounter=0);
644  template<class VISITOR>
645  size_t _EstimateStartingSmoothing(VISITOR& visitor);
646  bool _UpdateSmoothing(ValueType primalBound,ValueType dualBound, ValueType smoothDualBound, ValueType derivativeValue,size_t iterationCounterPlus1=0);
647  ValueType _EstimateRhoDerivative()const;
649  void _UpdatePrimalEstimator();
651  bool _isLPBoundComputed()const;
653 
654  Parameter _parameters;
655  Storage _storage;
656  SumProdSolver _sumprodsolver;
657  MaxSumSolver _maxsumsolver;
658  PrimalBoundEstimator _estimator;
659 #ifdef TRWS_DEBUG_OUTPUT
660  std::ostream& _fout;
661 #endif
663  ValueType _bestPrimalBound;//best primal bound overall
664 
665  ValueType _bestDualBound;
666  ValueType _bestIntegerBound;
667  std::vector<LabelType> _bestIntegerLabeling;
668 
669  SmoothingStrategyType* psmoothingStrategy;
670  /*
671  * optimization of computations
672  */
673  //mutable
675 };
676 
677 template<class GM,class ACC>
679 SmoothingBasedInference<GM,ACC>::UpdateSmoothDualEstimates(ValueType smoothingValue,ValueType* pderivativeValue)
680 {
681  _sumprodsolver.SetSmoothing(smoothingValue);
682  _sumprodsolver.ForwardMove();
683  _sumprodsolver.GetMarginalsAndDerivativeMove();
684  *pderivativeValue=_EstimateRhoDerivative();
685  return _sumprodsolver.bound();
686 };
687 
688 template<class GM,class ACC>
689 template<class VISITOR>
691 {
692  ValueType smoothingValue= psmoothingStrategy->InitSmoothing(*this,_maxsumsolver.value(),_maxsumsolver.bound());
693  _sumprodsolver.SetSmoothing(smoothingValue);
694  return psmoothingStrategy->getOracleCallsCounter();
695 };
696 
697 template<class GM,class ACC>
698 bool SmoothingBasedInference<GM,ACC>::_UpdateSmoothing(ValueType primalBound,ValueType dualBound, ValueType smoothDualBound, ValueType derivativeValue,size_t iterationCounterPlus1)
699 {
700  ValueType newSmoothing=psmoothingStrategy->UpdateSmoothing(_sumprodsolver.GetSmoothing(),primalBound,dualBound,smoothDualBound,derivativeValue,iterationCounterPlus1-1);
701  if (newSmoothing != _sumprodsolver.GetSmoothing())
702  {
703  _sumprodsolver.SetSmoothing(newSmoothing);
704  return true;
705  }
706  else
707  return false;
708 }
709 
710 template<class GM,class ACC>
713 {
714  ValueType derivative=0.0;
715 
716  for (size_t i=0;i<_storage.numberOfModels();++i)
717  {
718  ValueType delta;
719 
720  ACC::op(_sumprodsolver.getDerivative(i),(_sumprodsolver.getBound(i)-_maxsumsolver.getBound(i))/_sumprodsolver.GetSmoothing(),delta);
721 
722  derivative+=delta;
723  }
724 
725  return derivative;
726 }
727 
728 template<class GM,class ACC>
730 {
731  std::pair<ValueType,ValueType> bestNorms=std::make_pair((ValueType)0.0,(ValueType)0.0);
732  ValueType numberOfVariables=_storage.masterModel().numberOfVariables();
733  for (IndexType var=0;var<numberOfVariables;++var)
734  {
735  _marginalsTemp.resize(_storage.numberOfLabels(var));
736  std::pair<ValueType,ValueType> norms=_sumprodsolver.GetMarginals(var, _marginalsTemp.begin());
737 
738  bestNorms.second=std::max(bestNorms.second,norms.second);
739  bestNorms.first+=norms.first*norms.first;
740 
741  transform_inplace(_marginalsTemp.begin(),_marginalsTemp.end(),trws_base::make0ifless<ValueType>(norms.second));
742 
743  TransportSolver::_Normalize(_marginalsTemp.begin(),_marginalsTemp.end(),(ValueType)0.0);
744  _estimator.setVariable(var,_marginalsTemp.begin());
745  }
746 #ifdef TRWS_DEBUG_OUTPUT
747  _fout << "l2 gradient norm="<<sqrt(bestNorms.first)<<", "<<"l_inf gradient norm="<<bestNorms.second<<std::endl;
748 #endif
749 };
750 
751 template<class GM,class ACC>
753 {
754  //Best integer bound...
755  if (ACC::bop(_sumprodsolver.value(),_maxsumsolver.value()))
756  {
757  _bestIntegerLabeling=_sumprodsolver.arg();
758  _bestIntegerBound=_sumprodsolver.value();
759  }else
760  {
761  _bestIntegerLabeling=_maxsumsolver.arg();
762  _bestIntegerBound=_maxsumsolver.value();
763  }
764 
765  //Best primalBound
766  ACC::op(_bestPrimalLPbound,_bestIntegerBound,_bestPrimalBound);
767 #ifdef TRWS_DEBUG_OUTPUT
768  _fout << "_bestPrimalBound=" <<_bestPrimalBound<<std::endl;
769 #endif
770 
771  //Best dual bound...
772  if (ACC::ibop(_sumprodsolver.bound(),_maxsumsolver.bound()))
773  _bestDualBound=_sumprodsolver.bound();
774  else
775  _bestDualBound=_maxsumsolver.bound();
776 
777 }
778 
779 template<class GM,class ACC>
780 template<class VISITOR>
782 {
783 #ifdef TRWS_DEBUG_OUTPUT
784  _fout << "Running TRWS presolve..."<<std::endl;
785 #endif
786  return _maxsumsolver.infer_visitor_updates(visitor,piterCounter);
787 }
788 
789 template<class GM,class ACC>
791 {
792  return (!_parameters.lazyLPPrimalBoundComputation_ || !ACC::bop(_sumprodsolver.value(),_bestPrimalBound) );
793 }
794 
795 template<class GM,class ACC>
797 {
798  if( _isLPBoundComputed())
799  {
800  _UpdatePrimalEstimator();
801 
802  ACC::op(_estimator.getTotalValue(),_bestPrimalLPbound);
803 #ifdef TRWS_DEBUG_OUTPUT
804  _fout << "_primalLPbound=" <<_estimator.getTotalValue()<<std::endl;
805 #endif
806  }
807  _SelectOptimalBoundsAndLabeling();
808 
809  if (_maxsumsolver.CheckTreeAgreement(preturncode)) return true;
810 
811  if (_sumprodsolver.CheckDualityGap(_bestPrimalBound,_maxsumsolver.bound()))
812  {
813 #ifdef TRWS_DEBUG_OUTPUT
814  _fout << "SmoothingBasedInference::_CheckStoppingCondition(): Precision attained! Problem solved!"<<std::endl;
815 #endif
816  *preturncode=CONVERGENCE;
817  return true;
818  }
819 
820  return false;
821 }
822 //====================================================
823 
824 }//trws_base
825 }//opengm
826 
827 #endif /* SMOOTHING_STRATEGY_HXX_ */
SmoothingStrategy(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
The OpenGM namespace.
Definition: config.hxx:43
SmoothingBasedInference_Parameter< GM > Parameter
parent::MaxSumSolverParametersType MaxSumSolverParametersType
ReparametrizerType * getReparametrizer(const typename ReparametrizerType::Parameter &params=typename ReparametrizerType::Parameter())
ValueType InitSmoothing(SmoothingBasedInference< GM, ACC > &smoothInference, ValueType primalBound, ValueType dualBound)
AdaptivePrecisionOrientedSmoothing(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
ValueType getStartingSmoothing(ValueType primalBound, ValueType dualBound) const
SumProdSolver::OutputContainerType _marginalsTemp
SmoothingParameters< ValueType > Parameter
std::pair< ValueType, ValueType > GetMarginals(IndexType variable, OutputIteratorType begin)
Definition: trws_base.hxx:1272
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
bool _CheckStoppingCondition(InferenceTermination *)
ValueType _InitAdaptiveSmoothing(SmoothingBasedInference< GM, ACC > &smoothInference, ValueType primalBound, ValueType dualBound, ValueType initialSmoothing)
parent::SumProdSolverParametersType SumProdSolverParametersType
const FactorProperties & getFactorProperties() const
Definition: trws_base.hxx:265
trws_base::SmoothingBasedInference_Parameter_Base< GM > parent
SmoothingParametersType::SmoothingStrategyType SmoothingStrategyType
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
parent::PrimalLPEstimatorParametersType PrimalLPEstimatorParametersType
void getTreeAgreement(std::vector< bool > &out, std::vector< LabelType > *plabeling=0, std::vector< std::vector< LabelType > > *ptreeLabelings=0)
Definition: trws_base.hxx:1152
InputIterator transform_inplace(InputIterator first, InputIterator last, UnaryOperator op)
Definition: utilities2.hxx:79
WorstCaseDiminishingSmoothing(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
const SmoothingParametersType & getSmoothingParameters() const
LabelType numberOfLabels(IndexType varId) const
Definition: trws_base.hxx:57
SumProdTRWS_Parameters< ValueType > SumProdSolverParametersType
const SumProdSolverParametersType & getSumProdSolverParameters() const
ValueType UpdateSmoothDualEstimates(ValueType smoothingValue, ValueType *pderivativeValue)
GraphicalModelType::IndexType IndexType
Definition: inference.hxx:40
trws_base::DecompositionStorage< GM > Storage
const SmoothingStrategyType & smoothingStrategy() const
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
SmoothingBasedInference_Parameter_Base(typename Storage::StructureType decompositionType, bool lazyLPPrimalBoundComputation, const SmoothingParametersType &smoothingParameters, const SumProdSolverParametersType &sumProdSolverParameters, const MaxSumSolverParametersType &maxSumSolverParameters, const PrimalLPEstimatorParametersType &primalLPEstimatorParameters)
ValueType value() const
return the solution (value)
GraphicalModelType::ValueType ValueType
Definition: inference.hxx:41
FixedSmoothing(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
Inference algorithm interface.
Definition: inference.hxx:34
InferenceTermination marginal(const IndexType varID, IndependentFactorType &out)
InferenceTermination arg(std::vector< LabelType > &out, const size_t=1) const
output a solution
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
T _Normalize(Iterator begin, Iterator end, T initialValue)
bool SmoothingMustBeDecreased(ValueType smoothingValue, ValueType primalBound, ValueType dualBound, ValueType smoothDualBound, ValueType smoothingDerivative, size_t iterationCounter)
const MaxSumSolver::FactorProperties & getFactorProperties() const
trws_base::SumProdTRWS< GM, ACC > SumProdSolver
bool _UpdateSmoothing(ValueType primalBound, ValueType dualBound, ValueType smoothDualBound, ValueType derivativeValue, size_t iterationCounterPlus1=0)
returns number of oracle calls;
SmoothingStrategy< GM, ACC > SmoothingStrategyType
SmoothingStrategy< GM, ACC > parent
parent::OutputContainerType OutputContainerType
Definition: trws_base.hxx:367
virtual ValueType bound() const
Definition: trws_base.hxx:235
InferenceTermination _Presolve(VISITOR &visitor, size_t *piterCounter=0)
PrimalLPBound_Parameter< ValueType > PrimalLPEstimatorParametersType
SmoothingParameters< ValueType > Parameter
ValueType GetSmoothing() const
Definition: trws_base.hxx:392
const MaxSumSolverParametersType & getMaxSumSolverParameters() const
SmoothingParameters(ValueType smoothingGapRatio=4, ValueType smoothingValue=0.0, ValueType smoothingDecayMultiplier=-1.0, ValueType precision=0, SmoothingStrategyType smoothingStrategy=ADAPTIVE_DIMINISHING)
static std::string getString(SmoothingStrategyType strategytype)
WorstCasePrecisionOrientedSmoothing(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
SmoothingBasedInference(const GraphicalModelType &gm, const Parameter &param)
TRWS_Reparametrizer< Storage, ACC > ReparametrizerType
GraphicalModelType::LabelType LabelType
Definition: inference.hxx:39
static ValueType ComputeSmoothingMultiplier(const DualDecompositionStorage &storage)
ValueType bound() const
return a bound on the solution
const GraphicalModelType & graphicalModel() const
static SmoothingStrategyType getSmoothingStrategyType(const std::string &name)
ValueType UpdateSmoothing(ValueType smoothingValue, ValueType primalBound, ValueType dualBound, ValueType smoothDualBound, ValueType smoothingDerivative, size_t iterationCounter)
const Storage::StructureType & decompositionType() const
virtual ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
trws_base::MaxSumTRWS< GM, ACC > MaxSumSolver
virtual ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const =0
static std::string getString(StructureType structure)
Definition: trws_base.hxx:37
AdaptiveDiminishingSmoothing(ValueType smoothingMultiplier, const Parameter &param=Parameter(), std::ostream &fout=std::cout)
SmoothingBasedInference_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, ValueType smoothingDecayMultiplier=-1.0, SmoothingStrategyType smoothingStrategy=SmoothingParametersType::ADAPTIVE_DIMINISHING, bool fastComputations=true, bool verbose=false)
[class primallpbound] PrimalLPBound - estimating primal local polytope bound and feasible primal solu...
const PrimalLPEstimatorParametersType & getPrimalLPEstimatorParameters() const
void getTreeAgreement(std::vector< bool > &out, std::vector< LabelType > *plabeling=0, std::vector< std::vector< LabelType > > *ptreeLabelings=0)
InferenceTermination
Definition: inference.hxx:24
GraphicalModelType::IndependentFactorType IndependentFactorType
Definition: inference.hxx:44