28 #ifndef _TOMOGRAPHER_MHRWSTEPSIZECONTROLLER_H 29 #define _TOMOGRAPHER_MHRWSTEPSIZECONTROLLER_H 68 template<
typename MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
69 typename BaseLoggerType_ = Logger::VacuumLogger,
70 typename StepRealType_ = double,
71 typename IterCountIntType_ =
int>
75 MHRWStepSizeController<MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
76 BaseLoggerType_, StepRealType_, IterCountIntType_>,
78 MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
87 BaseLoggerType_, StepRealType_, IterCountIntType_>,
89 MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
94 using Base::AdjustmentStrategy;
96 typedef MHRWMovingAverageAcceptanceRatioStatsCollectorType_
97 MHRWMovingAverageAcceptanceRatioStatsCollectorType;
98 typedef BaseLoggerType_ BaseLoggerType;
99 typedef StepRealType_ StepRealType;
100 typedef IterCountIntType_ IterCountIntType;
104 StepRealType last_set_step_size;
106 StepRealType orig_step_times_sweep;
112 const MHRWMovingAverageAcceptanceRatioStatsCollectorType & accept_ratio_stats_collector_,
113 BaseLoggerType & baselogger_,
114 double desired_accept_ratio_min_ =
115 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMin,
116 double desired_accept_ratio_max_ =
117 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMax,
118 double acceptable_accept_ratio_min_ =
119 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMin,
120 double acceptable_accept_ratio_max_ =
121 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMax,
122 double ensure_n_therm_fixed_params_fraction_ =
123 MHRWAcceptRatioWalkerParamsControllerDefaults::EnsureNThermFixedParamsFraction
125 : Base(accept_ratio_stats_collector_,
128 desired_accept_ratio_min_,
129 desired_accept_ratio_max_,
130 acceptable_accept_ratio_min_,
131 acceptable_accept_ratio_max_,
132 ensure_n_therm_fixed_params_fraction_),
134 orig_step_times_sweep(0),
135 llogger(
"Tomographer::MHRWStepSizeController", baselogger_)
142 template<
typename MHRWParamsType,
typename MHWalker,
typename MHRandomWalkType>
143 inline void initParams(MHRWParamsType & params,
const MHWalker & ,
const MHRandomWalkType & )
148 params.mhwalker_params.step_size > 0) {
150 orig_step_times_sweep = params.n_sweep * params.mhwalker_params.step_size;
153 const StepRealType default_start_step_size = StepRealType(0.01);
155 stream <<
"Invalid step_size = " << params.mhwalker_params.step_size
156 <<
", set default of = " << default_start_step_size;
158 params.mhwalker_params.step_size = default_start_step_size;
159 params.n_sweep = (
typename MHRWParamsType::CountIntType)(StepRealType(1)/default_start_step_size) + 1;
160 orig_step_times_sweep = 1;
164 template<
typename MHRWParamsType,
typename MeType,
typename MHWalker,
typename MHRandomWalkType>
165 inline void adjustParamsForAcceptRatio(MHRWParamsType & params,
double accept_ratio,
const MeType & ,
166 const MHWalker & , IterCountIntType iter_k,
167 const MHRandomWalkType & )
173 StepRealType new_step_size = params.mhwalker_params.step_size;
175 const auto desired_accept_ratio_min = Base::desiredAcceptRatioMin();
176 const auto desired_accept_ratio_max = Base::desiredAcceptRatioMax();
181 if (accept_ratio >= 2*desired_accept_ratio_max) {
182 new_step_size *= StepRealType(1.5);
183 }
else if (accept_ratio >= StepRealType(1.3)*desired_accept_ratio_max) {
184 new_step_size *= StepRealType(1.2);
185 }
else if (accept_ratio >= desired_accept_ratio_max) {
186 new_step_size *= StepRealType(1.05);
187 }
else if (accept_ratio <= StepRealType(0.5)*desired_accept_ratio_min) {
188 new_step_size *= StepRealType(0.5);
189 }
else if (accept_ratio <= StepRealType(0.75)*desired_accept_ratio_min) {
190 new_step_size *= StepRealType(0.8);
192 new_step_size *= StepRealType(0.95);
196 stream <<
"Corrected step_size to " << new_step_size;
200 const auto cur_step_size = params.mhwalker_params.step_size;
201 if (new_step_size < StepRealType(0.7)*cur_step_size) {
202 new_step_size = StepRealType(0.7)*cur_step_size;
204 if (new_step_size > StepRealType(1.5)*cur_step_size) {
205 new_step_size = StepRealType(1.5)*cur_step_size;
208 params.mhwalker_params.step_size = new_step_size;
211 last_set_step_size = new_step_size;
214 params.n_sweep = (
typename MHRWParamsType::CountIntType)(orig_step_times_sweep / new_step_size + 1);
217 const typename MHRWParamsType::CountIntType n_therm_min =
218 (
typename MHRWParamsType::CountIntType)(
219 (iter_k/params.n_sweep) + 1 + (Base::ensureNThermFixedParamsFraction() * Base::originalNTherm())
221 if (params.n_therm < n_therm_min) {
223 stream <<
"There aren't enough thermalization sweeps. I'm setting n_therm = " << n_therm_min;
225 params.n_therm = n_therm_min;
229 stream <<
"New params = " << params;
233 inline StepRealType getLastSetStepSize()
const {
return last_set_step_size; }
238 template<
typename MHRWParamsType,
239 typename MHRWMovingAverageAcceptanceRatioStatsCollectorType_,
240 typename BaseLoggerType_
245 typename MHRWParamsType::MHWalkerParams::StepRealType,
246 typename MHRWParamsType::CountIntType>
247 mkMHRWStepSizeController(
248 const MHRWMovingAverageAcceptanceRatioStatsCollectorType_ & accept_ratio_stats_collector_,
249 BaseLoggerType_ & baselogger_,
250 double desired_accept_ratio_min_ =
251 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMin,
252 double desired_accept_ratio_max_ =
253 MHRWAcceptRatioWalkerParamsControllerDefaults::DesiredAcceptanceRatioMax,
254 double acceptable_accept_ratio_min_ =
255 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMin,
256 double acceptable_accept_ratio_max_ =
257 MHRWAcceptRatioWalkerParamsControllerDefaults::AcceptableAcceptanceRatioMax,
258 double ensure_n_therm_fixed_params_fraction_ =
259 MHRWAcceptRatioWalkerParamsControllerDefaults::EnsureNThermFixedParamsFraction
264 typename MHRWParamsType::MHWalkerParams::StepRealType,
265 typename MHRWParamsType::CountIntType>(
266 accept_ratio_stats_collector_,
268 desired_accept_ratio_min_,
269 desired_accept_ratio_max_,
270 acceptable_accept_ratio_min_,
271 acceptable_accept_ratio_max_,
272 ensure_n_therm_fixed_params_fraction_
283 template<
typename MHRWMovingAverageAcceptanceRatioStatsCollectorType,
284 typename BaseLoggerType,
285 typename StepRealType,
286 typename IterCountIntType>
287 struct TOMOGRAPHER_EXPORT
289 BaseLoggerType, StepRealType, IterCountIntType> >
294 static constexpr
bool CanProvideStatusLine =
true;
296 static inline std::string getStatusLine(
const StatusableObject * obj) {
297 double last_step = (double)obj->getLastSetStepSize();
Utilities for formatting strings.
Basic functionality for a MHRWController Interface to adjust MHWalkerParams based on keeping the acce...
Base namespace for the Tomographer project.
A MHRWController Interface dynamically adjusting the step size to keep a good 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.
Tools for automatically and dynamically adjusting the step size of the random walk.
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.