28 #ifndef _TOMOGRAPHER_MHRWACCEPTRATIOWALKERPARAMSCONTROLLER_H 29 #define _TOMOGRAPHER_MHRWACCEPTRATIOWALKERPARAMSCONTROLLER_H 61 namespace MHRWAcceptRatioWalkerParamsControllerDefaults {
63 static constexpr
double AcceptableAcceptanceRatioMin =
65 static constexpr
double AcceptableAcceptanceRatioMax =
67 static constexpr
double DesiredAcceptanceRatioMin =
68 (0.9 * AcceptableAcceptanceRatioMin + 0.1 * AcceptableAcceptanceRatioMax);
69 static constexpr
double DesiredAcceptanceRatioMax =
70 (0.35 * AcceptableAcceptanceRatioMin + 0.65 * AcceptableAcceptanceRatioMax);
71 static constexpr
double EnsureNThermFixedParamsFraction = 0.5;
85 template<
typename MHRWAcceptanceRatioBasedParamsAdjusterType_,
86 typename MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
88 typename IterCountIntType_ =
int>
94 typedef MHRWAcceptanceRatioBasedParamsAdjusterType_ MHRWAcceptanceRatioBasedParamsAdjusterType;
95 typedef MHRWMovingAverageAcceptanceRatioStatsCollectorType_
96 MHRWMovingAverageAcceptanceRatioStatsCollectorType;
97 typedef BaseLoggerType_ BaseLoggerType;
98 typedef IterCountIntType_ IterCountIntType;
101 const MHRWMovingAverageAcceptanceRatioStatsCollectorType & accept_ratio_stats_collector;
103 MHRWAcceptanceRatioBasedParamsAdjusterType & params_adjuster;
105 const double desired_accept_ratio_min;
106 const double desired_accept_ratio_max;
107 const double acceptable_accept_ratio_min;
108 const double acceptable_accept_ratio_max;
110 IterCountIntType orig_n_therm;
111 IterCountIntType last_corrected_unacceptable_iter_k;
117 const double ensure_n_therm_fixed_params_fraction;
123 const MHRWMovingAverageAcceptanceRatioStatsCollectorType & accept_ratio_stats_collector_,
124 BaseLoggerType & baselogger_,
125 MHRWAcceptanceRatioBasedParamsAdjusterType & params_adjuster_,
126 double desired_accept_ratio_min_ =
127 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMin,
128 double desired_accept_ratio_max_ =
129 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMax,
130 double acceptable_accept_ratio_min_ =
131 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMin,
132 double acceptable_accept_ratio_max_ =
133 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMax,
134 double ensure_n_therm_fixed_params_fraction_ =
135 MHRWAcceptRatioWalkerParamsControllerDefaults::EnsureNThermFixedParamsFraction
137 : accept_ratio_stats_collector(accept_ratio_stats_collector_),
138 params_adjuster(params_adjuster_),
139 desired_accept_ratio_min(desired_accept_ratio_min_),
140 desired_accept_ratio_max(desired_accept_ratio_max_),
141 acceptable_accept_ratio_min(acceptable_accept_ratio_min_),
142 acceptable_accept_ratio_max(acceptable_accept_ratio_max_),
144 last_corrected_unacceptable_iter_k(0),
145 ensure_n_therm_fixed_params_fraction(ensure_n_therm_fixed_params_fraction_),
146 llogger(
"Tomographer::MHRWAcceptRatioWalkerParamsController", baselogger_)
151 inline double desiredAcceptRatioMin()
const {
return desired_accept_ratio_min; }
152 inline double desiredAcceptRatioMax()
const {
return desired_accept_ratio_max; }
153 inline double acceptableAcceptRatioMin()
const {
return acceptable_accept_ratio_min; }
154 inline double acceptableAcceptRatioMax()
const {
return acceptable_accept_ratio_max; }
156 inline double ensureNThermFixedParamsFraction()
const {
return ensure_n_therm_fixed_params_fraction; }
158 inline IterCountIntType originalNTherm()
const {
return orig_n_therm; }
161 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
162 inline void init(MHRWParamsType & params,
const MHWalker & mhwalker,
const MHRandomWalkType & mhrw)
166 orig_n_therm = params.n_therm;
168 params_adjuster.initParams(params, mhwalker, mhrw) ;
171 template<
bool IsThermalizing,
bool IsAfterSample,
172 typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType,
173 TOMOGRAPHER_ENABLED_IF_TMPL(IsThermalizing)>
174 inline void adjustParams(MHRWParamsType & params,
const MHWalker & mhwalker,
175 IterCountIntType iter_k,
const MHRandomWalkType & mhrw)
180 stream <<
"cur params = " << params <<
" and accept_ratio = " 181 << accept_ratio_stats_collector.movingAverageAcceptanceRatio();
185 if ( ! accept_ratio_stats_collector.hasMovingAverageAcceptanceRatio() ||
186 (iter_k % (IterCountIntType)accept_ratio_stats_collector.bufferSize()) != 0 ) {
191 stream <<
"will consider correction. iter_k = " << iter_k
192 <<
", last_corrected_unacceptable_iter_k = " << last_corrected_unacceptable_iter_k;
195 const double accept_ratio = accept_ratio_stats_collector.movingAverageAcceptanceRatio();
198 (accept_ratio >= desired_accept_ratio_min &&
199 accept_ratio <= desired_accept_ratio_max)) {
204 stream <<
"will adjust.";
208 if (accept_ratio < acceptable_accept_ratio_min || accept_ratio > acceptable_accept_ratio_max) {
209 last_corrected_unacceptable_iter_k = iter_k;
212 params_adjuster.adjustParamsForAcceptRatio( params, accept_ratio, *
this, mhwalker, iter_k, mhrw ) ;
215 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
216 bool allowDoneThermalization(
const MHRWParamsType & params,
const MHWalker & ,
217 IterCountIntType iter_k,
const MHRandomWalkType & )
221 const double accept_ratio = accept_ratio_stats_collector.movingAverageAcceptanceRatio();
223 stream <<
"iter_k=" << iter_k <<
", accept_ratio=" << accept_ratio;
226 accept_ratio < desired_accept_ratio_min || accept_ratio > desired_accept_ratio_max) {
227 logger.longdebug(
"not allowing, based on accept_ratio=%.4g", accept_ratio);
231 if ((iter_k - last_corrected_unacceptable_iter_k)
232 < params.n_sweep*(ensure_n_therm_fixed_params_fraction*orig_n_therm)) {
234 stream <<
"not allowing, based on iter_k=" << iter_k
235 <<
" & last_corrected_unacceptable_iter_k=" << last_corrected_unacceptable_iter_k;
240 logger.longdebug(
"all fine, can return");
244 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
245 bool allowDoneRuns(
const MHRWParamsType & ,
const MHWalker & ,
246 IterCountIntType ,
const MHRandomWalkType & )
251 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
252 inline void thermalizingDone(MHRWParamsType & ,
const MHWalker & ,
253 const MHRandomWalkType & )
const 257 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
258 inline void done(MHRWParamsType & ,
const MHWalker & ,
259 const MHRandomWalkType & )
const Utilities for formatting strings.
Basic functionality for a MHRWController Interface to adjust MHWalkerParams based on keeping the acce...
Base namespace for the Tomographer project.
Logger that discards all messages.
constexpr const double MHRWAcceptanceRatioRecommendedMax
Maximal recommended acceptance ratio.
constexpr const double MHRWAcceptanceRatioRecommendedMin
Minimal recommended acceptance ratio.
#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.
Adjustemnts should be performed only while thermalizing, after each individual iteration.
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) ...
Definitions for MHRWStatsCollector Interface's.
Routines for performing a Metropolis-Hastings random walk.
Utilities for logging messages.