8 #ifndef SMOOTHING_STRATEGY_HXX_
9 #define SMOOTHING_STRATEGY_HXX_
17 template<
class VALUETYPE>
29 else if (name.compare(
"FIXED")==0)
return FIXED;
41 case FIXED:
return std::string(
"FIXED");
42 default:
return std::string(
"UNKNOWN");
47 ValueType smoothingValue=0.0,
48 ValueType smoothingDecayMultiplier=-1.0,
49 ValueType precision=0,
81 bool lazyLPPrimalBoundComputation,
82 const SmoothingParametersType& smoothingParameters,
83 const SumProdSolverParametersType& sumProdSolverParameters,
84 const MaxSumSolverParametersType& maxSumSolverParameters,
85 const PrimalLPEstimatorParametersType& primalLPEstimatorParameters):
124 bool absolutePrecision=
true,
125 size_t numOfInternalIterations=3,
129 ValueType primalBoundPrecision=std::numeric_limits<ValueType>::epsilon(),
131 size_t presolveMaxIterNumber=100,
222 #ifdef TRWS_DEBUG_OUTPUT
223 void print(std::ostream& fout)
const
227 fout <<
"precision=" <<
precision()<<std::endl;
251 fout <<
"verbose="<<
verbose()<<std::endl;
259 template<
class GM,
class ACC>
262 template<
class GM,
class ACC>
279 ValueType primalBound,
280 ValueType dualBound){
285 ValueType primalBound,
287 ValueType smoothDualBound,
288 ValueType smoothingDerivative,
289 size_t iterationCounter)
291 if (
SmoothingMustBeDecreased(smoothingValue,primalBound,dualBound,smoothDualBound,smoothingDerivative,iterationCounter))
293 smoothingValue= std::max((ValueType)(smoothingValue/2.0),
SmoothingEstimateValue(primalBound,smoothDualBound,smoothingDerivative));
295 #ifdef TRWS_DEBUG_OUTPUT
296 _fout <<
"Smoothing decreased to = "<<smoothingValue<<std::endl;
299 return smoothingValue;
303 ValueType primalBound,
305 ValueType smoothDualBound,
306 ValueType smoothingDerivative,
307 size_t iterationCounter)
309 ValueType rhs=fabs(primalBound-smoothDualBound);
317 template<
class DualDecompositionStorage >
321 typename DualDecompositionStorage::LabelType numOfLabels=0;
322 for (
size_t i=0;i<storage.numberOfSharedVariables();++i)
323 numOfLabels=std::max(numOfLabels,storage.numberOfLabels(i));
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;
337 ValueType primalBound,
338 ValueType smoothDualBound,
339 ValueType smoothingDerivative)
const=0;
354 ValueType primalBound,
356 ValueType initialSmoothing);
365 template<
class GM,
class ACC>
368 ValueType primalBound,
372 #ifdef TRWS_DEBUG_OUTPUT
373 _fout <<
"_maxsumsolver.value()="<<primalBound<<
", _maxsumsolver.bound()="<<dualBound<<std::endl;
374 _fout <<
"Initial smoothing="<<smoothing<<std::endl;
377 ValueType derivativeValue;
380 _oracleCallsCounter=1;
382 ++_oracleCallsCounter;
384 #ifdef TRWS_DEBUG_OUTPUT
385 _fout <<
"test smoothing = "<<smoothing<<std::endl;
388 #ifdef TRWS_DEBUG_OUTPUT
389 _fout <<
"smoothDualBound="<<smoothDualBound<<std::endl;
391 }
while (!SmoothingMustBeDecreased(smoothing,primalBound,dualBound,smoothDualBound,derivativeValue,0));
395 #ifdef TRWS_DEBUG_OUTPUT
396 _fout <<
"Smoothing := "<<smoothing<<std::endl;
398 _initializationStage=
false;
404 template<
class GM,
class ACC>
415 :parent(smoothingMultiplier,param,fout)
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.");
421 ValueType primalBound,
422 ValueType smoothDualBound,
423 ValueType smoothingDerivative)
const
429 template<
class GM,
class ACC>
442 ValueType smoothDualBound,
443 ValueType smoothingDerivative)
const
450 template<
class GM,
class ACC>
463 ValueType primalBound,
464 ValueType smoothDualBound,
465 ValueType smoothingDerivative)
const
472 template<
class GM,
class ACC>
483 :parent(smoothingMultiplier,param,fout){};
486 ValueType primalBound,
487 ValueType smoothDualBound,
488 ValueType smoothingDerivative)
const
494 template<
class GM,
class ACC>
505 :parent(smoothingMultiplier,param,fout)
507 if (param.smoothingValue_ <= 0 )
508 throw std::runtime_error(
"FixedSmoothing()::FixedSmoothing(): Smoothing value must be positive!");
512 ValueType smoothDualBound,
513 ValueType smoothingDerivative)
const
518 template<
class GM,
class ACC>
519 class SmoothingBasedInference :
public Inference<GM, ACC>
539 #ifdef TRWS_DEBUG_OUTPUT
540 ,std::ostream& fout=std::cout
544 _storage(gm,param.decompositionType_),
546 #ifdef TRWS_DEBUG_OUTPUT
551 #ifdef TRWS_DEBUG_OUTPUT
555 _estimator(gm,param.getPrimalLPEstimatorParameters()),
556 #ifdef TRWS_DEBUG_OUTPUT
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.");
572 switch (param.smoothingStrategy())
574 case Parameter::SmoothingParametersType::ADAPTIVE_DIMINISHING:
576 #ifdef TRWS_DEBUG_OUTPUT
581 case Parameter::SmoothingParametersType::WC_DIMINISHING:
583 #ifdef TRWS_DEBUG_OUTPUT
588 case Parameter::SmoothingParametersType::ADAPTIVE_PRECISIONORIENTED:
590 #ifdef TRWS_DEBUG_OUTPUT
595 case Parameter::SmoothingParametersType::WC_PRECISIONORIENTED:
597 #ifdef TRWS_DEBUG_OUTPUT
602 case Parameter::SmoothingParametersType::FIXED:
604 #ifdef TRWS_DEBUG_OUTPUT
609 default:
throw std::runtime_error(
"SmoothingBasedInference: Error: Unknown smoothing strategy type");
637 out.assign(
graphicalModel(), &varID, &varID+1, ACC::template neutral<ValueType>());
642 template<
class VISITOR>
644 template<
class VISITOR>
646 bool _UpdateSmoothing(ValueType primalBound,ValueType dualBound, ValueType smoothDualBound, ValueType derivativeValue,
size_t iterationCounterPlus1=0);
659 #ifdef TRWS_DEBUG_OUTPUT
677 template<
class GM,
class ACC>
681 _sumprodsolver.SetSmoothing(smoothingValue);
682 _sumprodsolver.ForwardMove();
683 _sumprodsolver.GetMarginalsAndDerivativeMove();
684 *pderivativeValue=_EstimateRhoDerivative();
685 return _sumprodsolver.bound();
688 template<
class GM,
class ACC>
689 template<
class VISITOR>
692 ValueType smoothingValue= psmoothingStrategy->InitSmoothing(*
this,_maxsumsolver.value(),_maxsumsolver.bound());
693 _sumprodsolver.SetSmoothing(smoothingValue);
694 return psmoothingStrategy->getOracleCallsCounter();
697 template<
class GM,
class ACC>
700 ValueType newSmoothing=psmoothingStrategy->UpdateSmoothing(_sumprodsolver.GetSmoothing(),primalBound,dualBound,smoothDualBound,derivativeValue,iterationCounterPlus1-1);
701 if (newSmoothing != _sumprodsolver.GetSmoothing())
703 _sumprodsolver.SetSmoothing(newSmoothing);
710 template<
class GM,
class ACC>
714 ValueType derivative=0.0;
716 for (
size_t i=0;i<_storage.numberOfModels();++i)
720 ACC::op(_sumprodsolver.getDerivative(i),(_sumprodsolver.getBound(i)-_maxsumsolver.getBound(i))/_sumprodsolver.GetSmoothing(),delta);
728 template<
class GM,
class ACC>
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)
735 _marginalsTemp.resize(_storage.numberOfLabels(var));
736 std::pair<ValueType,ValueType> norms=_sumprodsolver.GetMarginals(var, _marginalsTemp.begin());
738 bestNorms.second=std::max(bestNorms.second,norms.second);
739 bestNorms.first+=norms.first*norms.first;
744 _estimator.setVariable(var,_marginalsTemp.begin());
746 #ifdef TRWS_DEBUG_OUTPUT
747 _fout <<
"l2 gradient norm="<<sqrt(bestNorms.first)<<
", "<<
"l_inf gradient norm="<<bestNorms.second<<std::endl;
751 template<
class GM,
class ACC>
755 if (ACC::bop(_sumprodsolver.value(),_maxsumsolver.value()))
757 _bestIntegerLabeling=_sumprodsolver.arg();
758 _bestIntegerBound=_sumprodsolver.value();
761 _bestIntegerLabeling=_maxsumsolver.arg();
762 _bestIntegerBound=_maxsumsolver.value();
766 ACC::op(_bestPrimalLPbound,_bestIntegerBound,_bestPrimalBound);
767 #ifdef TRWS_DEBUG_OUTPUT
768 _fout <<
"_bestPrimalBound=" <<_bestPrimalBound<<std::endl;
772 if (ACC::ibop(_sumprodsolver.bound(),_maxsumsolver.bound()))
773 _bestDualBound=_sumprodsolver.bound();
775 _bestDualBound=_maxsumsolver.bound();
779 template<
class GM,
class ACC>
780 template<
class VISITOR>
783 #ifdef TRWS_DEBUG_OUTPUT
784 _fout <<
"Running TRWS presolve..."<<std::endl;
786 return _maxsumsolver.infer_visitor_updates(visitor,piterCounter);
789 template<
class GM,
class ACC>
792 return (!_parameters.lazyLPPrimalBoundComputation_ || !ACC::bop(_sumprodsolver.value(),_bestPrimalBound) );
795 template<
class GM,
class ACC>
798 if( _isLPBoundComputed())
800 _UpdatePrimalEstimator();
802 ACC::op(_estimator.getTotalValue(),_bestPrimalLPbound);
803 #ifdef TRWS_DEBUG_OUTPUT
804 _fout <<
"_primalLPbound=" <<_estimator.getTotalValue()<<std::endl;
807 _SelectOptimalBoundsAndLabeling();
809 if (_maxsumsolver.CheckTreeAgreement(preturncode))
return true;
811 if (_sumprodsolver.CheckDualityGap(_bestPrimalBound,_maxsumsolver.bound()))
813 #ifdef TRWS_DEBUG_OUTPUT
814 _fout <<
"SmoothingBasedInference::_CheckStoppingCondition(): Precision attained! Problem solved!"<<std::endl;
void setStartSmoothingValue(ValueType val)
ValueType smoothingGapRatio_
SmoothingStrategy(ValueType smoothingMultiplier, const Parameter ¶m=Parameter(), std::ostream &fout=std::cout)
const bool & lazyLPPrimalBoundComputation() const
ValueType _EstimateRhoDerivative() const
std::vector< LabelType > _bestIntegerLabeling
virtual ~SmoothingStrategy()
SmoothingBasedInference_Parameter< GM > Parameter
size_t & maxNumberOfIterations()
parent::MaxSumSolverParametersType MaxSumSolverParametersType
ReparametrizerType * getReparametrizer(const typename ReparametrizerType::Parameter ¶ms=typename ReparametrizerType::Parameter())
ValueType smoothingDecayMultiplier_
size_t getOracleCallsCounter() const
void _SelectOptimalBoundsAndLabeling()
const ValueType & precision() const
void setPrecision(ValueType precision)
const ValueType & primalBoundRelativePrecision() const
ValueType InitSmoothing(SmoothingBasedInference< GM, ACC > &smoothInference, ValueType primalBound, ValueType dualBound)
void _UpdatePrimalEstimator()
AdaptivePrecisionOrientedSmoothing(ValueType smoothingMultiplier, const Parameter ¶m=Parameter(), std::ostream &fout=std::cout)
trws_base::DecompositionStorage< GM > Storage
ValueType getStartingSmoothing(ValueType primalBound, ValueType dualBound) const
SumProdSolver::OutputContainerType _marginalsTemp
SmoothingParameters< ValueType > Parameter
ValueType & smoothingDecayMultiplier()
std::pair< ValueType, ValueType > GetMarginals(IndexType variable, OutputIteratorType begin)
const GM & masterModel() const
MaxSumSolver _maxsumsolver
Inference< GM, ACC > parent
SmoothingStrategy< GM, ACC > parent
Storage::StructureType & decompositionType()
ValueType smoothingValue_
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
bool _CheckStoppingCondition(InferenceTermination *)
SmoothingStrategyType & smoothingStrategy()
bool & lazyLPPrimalBoundComputation()
ValueType _smoothingMultiplier
const size_t & numberOfInternalIterations() const
ValueType _InitAdaptiveSmoothing(SmoothingBasedInference< GM, ACC > &smoothInference, ValueType primalBound, ValueType dualBound, ValueType initialSmoothing)
virtual ~SmoothingBasedInference()
SmoothingParametersType smoothingParameters_
size_t & maxPrimalBoundIterationNumber()
parent::SumProdSolverParametersType SumProdSolverParametersType
const FactorProperties & getFactorProperties() const
SmoothingParameters< ValueType > Parameter
trws_base::SmoothingBasedInference_Parameter_Base< GM > parent
SmoothingParameters< ValueType > SmoothingParametersType
const bool & canonicalNormalization() const
SumProdSolver _sumprodsolver
SmoothingParametersType::SmoothingStrategyType SmoothingStrategyType
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
SmoothingParameters< ValueType > Parameter
Storage & getDecompositionStorage()
parent::PrimalLPEstimatorParametersType PrimalLPEstimatorParametersType
ValueType & smoothingGapRatio()
const ValueType & smoothingGapRatio() const
SmoothingStrategyType * psmoothingStrategy
void getTreeAgreement(std::vector< bool > &out, std::vector< LabelType > *plabeling=0, std::vector< std::vector< LabelType > > *ptreeLabelings=0)
size_t numOfExternalIterations_
InputIterator transform_inplace(InputIterator first, InputIterator last, UnaryOperator op)
PrimalBoundEstimator _estimator
const size_t & maxNumberOfPresolveIterations() const
WorstCaseDiminishingSmoothing(ValueType smoothingMultiplier, const Parameter ¶m=Parameter(), std::ostream &fout=std::cout)
bool lazyLPPrimalBoundComputation_
const size_t & maxPrimalBoundIterationNumber() const
virtual ~SmoothingBasedInference_Parameter_Base()
const SmoothingParametersType & getSmoothingParameters() const
LabelType numberOfLabels(IndexType varId) const
ValueType & primalBoundRelativePrecision()
DecompositionStorage< GM > Storage
ValueType _bestPrimalLPbound
MaxSumSolverParametersType maxSumSolverParameters_
const bool & fastComputations() const
MaxSumTRWS_Parameters< ValueType > MaxSumSolverParametersType
SumProdTRWS_Parameters< ValueType > SumProdSolverParametersType
ValueType _FastEstimateRhoDerivative() const
const SumProdSolverParametersType & getSumProdSolverParameters() const
ValueType UpdateSmoothDualEstimates(ValueType smoothingValue, ValueType *pderivativeValue)
GraphicalModelType::IndexType IndexType
const size_t & maxNumberOfIterations() const
trws_base::DecompositionStorage< GM > Storage
size_t _EstimateStartingSmoothing(VISITOR &visitor)
const SmoothingStrategyType & smoothingStrategy() const
ValueType _bestPrimalBound
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
ValueType & presolveMinRelativeDualImprovement()
size_t & numberOfInternalIterations()
SmoothingStrategy< GM, ACC > parent
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)
const ValueType & startSmoothingValue() const
GraphicalModelType::ValueType ValueType
FixedSmoothing(ValueType smoothingMultiplier, const Parameter ¶m=Parameter(), std::ostream &fout=std::cout)
Inference algorithm interface.
InferenceTermination marginal(const IndexType varID, IndependentFactorType &out)
InferenceTermination arg(std::vector< LabelType > &out, const size_t=1) const
output a solution
size_t & maxNumberOfPresolveIterations()
PrimalLPEstimatorParametersType primalLPEstimatorParameters_
ValueType SmoothingEstimateValue(ValueType primalBound, ValueType smoothDualBound, ValueType smoothingDerivative) const
ValueType getWorstCaseSmoothing() const
PrimalLPBound< GM, ACC > PrimalBoundEstimator
SmoothingStrategy< GM, ACC > parent
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
SumProdSolverParametersType sumProdSolverParameters_
trws_base::SumProdTRWS< GM, ACC > SumProdSolver
Storage::StructureType decompositionType_
SmoothingParameters< ValueType > Parameter
ValueType _bestIntegerBound
bool _UpdateSmoothing(ValueType primalBound, ValueType dualBound, ValueType smoothDualBound, ValueType derivativeValue, size_t iterationCounterPlus1=0)
returns number of oracle calls;
SmoothingStrategy< GM, ACC > SmoothingStrategyType
bool _initializationStage
SmoothingStrategy< GM, ACC > parent
SmoothingStrategy< GM, ACC > parent
parent::OutputContainerType OutputContainerType
virtual ValueType bound() const
InferenceTermination _Presolve(VISITOR &visitor, size_t *piterCounter=0)
PrimalLPBound_Parameter< ValueType > PrimalLPEstimatorParametersType
SmoothingParameters< ValueType > Parameter
void setCanonicalNormalization(bool canonical)
ValueType GetSmoothing() const
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 ¶m=Parameter(), std::ostream &fout=std::cout)
SmoothingBasedInference(const GraphicalModelType &gm, const Parameter ¶m)
const bool & verbose() const
bool & isAbsolutePrecision()
size_t _oracleCallsCounter
const bool & isAbsolutePrecision() const
TRWS_Reparametrizer< Storage, ACC > ReparametrizerType
GraphicalModelType::LabelType LabelType
static ValueType ComputeSmoothingMultiplier(const DualDecompositionStorage &storage)
ValueType bound() const
return a bound on the solution
const GraphicalModelType & graphicalModel() const
bool _isLPBoundComputed() const
const ValueType & presolveMinRelativeDualImprovement() const
static SmoothingStrategyType getSmoothingStrategyType(const std::string &name)
ValueType UpdateSmoothing(ValueType smoothingValue, ValueType primalBound, ValueType dualBound, ValueType smoothDualBound, ValueType smoothingDerivative, size_t iterationCounter)
void setFastComputations(bool fast)
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
SmoothingStrategyType smoothingStrategy_
static std::string getString(StructureType structure)
AdaptiveDiminishingSmoothing(ValueType smoothingMultiplier, const Parameter ¶m=Parameter(), std::ostream &fout=std::cout)
FunctionParameters< GM > FactorProperties
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...
SmoothingParameters< ValueType > Parameter
const PrimalLPEstimatorParametersType & getPrimalLPEstimatorParameters() const
void getTreeAgreement(std::vector< bool > &out, std::vector< LabelType > *plabeling=0, std::vector< std::vector< LabelType > > *ptreeLabelings=0)
parent::SmoothingParametersType SmoothingParametersType
const ValueType & smoothingDecayMultiplier() const
GraphicalModelType::IndependentFactorType IndependentFactorType