28 #ifndef _TOMOGRAPHER_MHRWVALUEERRORBINSCONVERGEDCONTROLLER_H 29 #define _TOMOGRAPHER_MHRWVALUEERRORBINSCONVERGEDCONTROLLER_H 66 template<
typename ValueHistogramWithBinningMHRWStatsCollectorType_,
67 typename IterCountIntType_,
68 typename BaseLoggerType_>
76 typedef ValueHistogramWithBinningMHRWStatsCollectorType_
77 ValueHistogramWithBinningMHRWStatsCollectorType;
79 typedef IterCountIntType_ IterCountIntType;
81 typedef BaseLoggerType_ BaseLoggerType;
84 const ValueHistogramWithBinningMHRWStatsCollectorType & value_stats_collector;
86 const IterCountIntType check_frequency_sweeps;
88 IterCountIntType last_forbidden_iter_number;
90 const Eigen::Index max_allowed_unknown;
91 const Eigen::Index max_allowed_unknown_notisolated;
92 const Eigen::Index max_allowed_not_converged;
94 const double max_add_run_iters;
105 const ValueHistogramWithBinningMHRWStatsCollectorType & value_stats_collector_,
106 BaseLoggerType & baselogger_,
107 IterCountIntType check_frequency_sweeps_ = 1024,
108 Eigen::Index max_allowed_unknown_ = 0,
109 Eigen::Index max_allowed_unknown_notisolated_ = 0,
110 Eigen::Index max_allowed_not_converged_ = 0,
111 double max_add_run_iters_ = 1.5
113 : value_stats_collector(value_stats_collector_),
114 check_frequency_sweeps( maybeadjust_check_freq_seeps(check_frequency_sweeps_,
115 value_stats_collector,
117 last_forbidden_iter_number(0),
118 max_allowed_unknown(max_allowed_unknown_),
119 max_allowed_unknown_notisolated(max_allowed_unknown_notisolated_),
120 max_allowed_not_converged(max_allowed_not_converged_),
121 max_add_run_iters(max_add_run_iters_),
122 llogger(
"Tomographer::MHRWValueErrorBinsConvergedAdjuster", baselogger_)
124 const auto binning_samples_size = value_stats_collector.getBinningAnalysis().effectiveSampleSize();
125 if ((check_frequency_sweeps % binning_samples_size) != 0) {
127 stream <<
"check_frequency_sweeps (="<<check_frequency_sweeps_<<
") is not a multiple of the " 128 <<
"binning analysis sample size (="<<binning_samples_size<<
"), this could lead to samples " 129 <<
"being ignored by the error analysis (avoid this)!";
134 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
135 inline void init(MHRWParamsType & ,
const MHWalker & ,
136 const MHRandomWalkType & )
const 140 template<
typename MHRWParamsType,
typename MHWalker,
typename CountIntType,
typename MHRandomWalkType>
141 bool allowDoneThermalization(
const MHRWParamsType & ,
const MHWalker & ,
142 CountIntType ,
const MHRandomWalkType & )
const 147 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
148 bool allowDoneRuns(
const MHRWParamsType & params,
const MHWalker & ,
149 IterCountIntType iter_k,
const MHRandomWalkType & )
152 if (check_frequency_sweeps == 0) {
159 if (last_forbidden_iter_number > 0 &&
160 (iter_k-last_forbidden_iter_number) < params.n_sweep*check_frequency_sweeps) {
165 if (iter_k % (params.n_sweep*check_frequency_sweeps) != 0) {
173 stream <<
"params=" << params <<
", iter_k=" << iter_k
174 <<
", max_add_run_iters=" << max_add_run_iters;
179 if (max_add_run_iters > 0 && iter_k > max_add_run_iters * params.n_run * params.n_sweep) {
181 stream <<
"Ending random walk after reaching maximum sweep number " 182 << iter_k/params.n_sweep <<
" (" 183 << 100.0*iter_k/(params.n_sweep*params.n_run) <<
"% of set run length)";
191 const auto& binning_analysis = value_stats_collector.getBinningAnalysis();
192 const auto binmeans = value_stats_collector.binMeans();
194 const auto error_levels = binning_analysis.calcErrorLevels(binmeans);
195 const auto conv_status = binning_analysis.determineErrorConvergence(error_levels);
199 logger.longdebug([&](
std::ostream & stream) { stream <<
"Convergence summary = " << conv_summary; }) ;
201 if (conv_summary.n_not_converged > max_allowed_not_converged ||
202 conv_summary.n_unknown > max_allowed_unknown ||
203 (conv_summary.n_unknown-conv_summary.n_unknown_isolated) > max_allowed_unknown_notisolated) {
205 last_forbidden_iter_number = iter_k;
213 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
214 inline void thermalizingDone(
const MHRWParamsType & ,
const MHWalker & ,
215 const MHRandomWalkType & )
const 219 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
220 inline void done(MHRWParamsType & ,
const MHWalker & ,
221 const MHRandomWalkType & )
const 227 inline static IterCountIntType maybeadjust_check_freq_seeps(
228 IterCountIntType check_frequency_sweeps_,
229 const ValueHistogramWithBinningMHRWStatsCollectorType & valstats,
230 BaseLoggerType & logger)
232 if (check_frequency_sweeps_ == 0) {
235 IterCountIntType binning_samples_size = (IterCountIntType)valstats.getBinningAnalysis().effectiveSampleSize();
236 if ((check_frequency_sweeps_ % binning_samples_size) == 0) {
238 return check_frequency_sweeps_;
241 IterCountIntType corrected = ( check_frequency_sweeps_ / binning_samples_size + 1) * binning_samples_size;
242 logger.debug(
"Tomographer::MHRWValueErrorBinsConvergedController", [&](
std::ostream & stream) {
243 stream <<
"check_frequency_sweeps (="<<check_frequency_sweeps_<<
") is not a multiple of the " 244 "binning analysis sample size (="<<binning_samples_size<<
"), correcting to " << corrected;
256 template<
typename IterCountIntType_ = int,
258 typename ValueHistogramWithBinningMHRWStatsCollectorType_ = void,
259 typename BaseLoggerType_ =
void>
264 const ValueHistogramWithBinningMHRWStatsCollectorType_ & value_stats_collector_,
265 BaseLoggerType_ & baselogger_,
266 IterCountIntType_ check_frequency_sweeps_ = 1024,
267 Eigen::Index max_allowed_unknown_ = 0,
268 Eigen::Index max_allowed_unknown_notisolated_ = 0,
269 Eigen::Index max_allowed_not_converged_ = 0,
270 double max_add_run_iters = 1.5
276 value_stats_collector_,
278 check_frequency_sweeps_,
279 max_allowed_unknown_,
280 max_allowed_unknown_notisolated_,
281 max_allowed_not_converged_,
Utilities for formatting strings.
static BinningErrorBarConvergenceSummary fromConvergedStatus(const Eigen::Ref< const Eigen::ArrayXi > &converged_status)
Construct a summary object from a list of converged status obtained from BinningAnalysis::determineEr...
A MHRWController Interface which stops the random walk after enough samples have been taken to make a...
void warning(const char *fmt,...)
Generate a log message with level Logger::WARNING (printf-like syntax)
Base namespace for the Tomographer project.
#define TOMO_ORIGIN
Use this as argument for a Tomographer::Logger::LocalLogger constructor .
LocalLogger< LocalLogger< BaseLoggerType > > subLogger(std::string new_prefix)
Create a sub-logger.
Never adjust the parameters of the random walk.
MHRWValueErrorBinsConvergedController(const ValueHistogramWithBinningMHRWStatsCollectorType &value_stats_collector_, BaseLoggerType &baselogger_, IterCountIntType check_frequency_sweeps_=1024, Eigen::Index max_allowed_unknown_=0, Eigen::Index max_allowed_unknown_notisolated_=0, Eigen::Index max_allowed_not_converged_=0, double max_add_run_iters_=1.5)
Constructor.
Some C++ utilities, with a tad of C++11 tricks.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
MHRWValueErrorBinsConvergedController< ValueHistogramWithBinningMHRWStatsCollectorType_, IterCountIntType_, BaseLoggerType_ > mkMHRWValueErrorBinsConvergedController(const ValueHistogramWithBinningMHRWStatsCollectorType_ &value_stats_collector_, BaseLoggerType_ &baselogger_, IterCountIntType_ check_frequency_sweeps_=1024, Eigen::Index max_allowed_unknown_=0, Eigen::Index max_allowed_unknown_notisolated_=0, Eigen::Index max_allowed_not_converged_=0, double max_add_run_iters=1.5)
Convenience function to create a MHRWValueErrorBinsConvergedController (using template argument deduc...
Definitions for MHRWStatsCollector Interface's.
Routines for performing a Metropolis-Hastings random walk.
Utilities for logging messages.