28 #ifndef TOMOGRAPHER_MHWALKER_TASKS_H 29 #define TOMOGRAPHER_MHWALKER_TASKS_H 63 template<
typename IterCountIntType_ =
int,
typename StepRealType_ =
double>
72 template<
typename MHRWParamsType>
126 return base_seed + k;
135 <<
"\t# iter. / sweep = " << mhrw_params.
n_sweep <<
"\n" 136 <<
"\t# therm. sweeps = " << mhrw_params.
n_therm <<
"\n" 137 <<
"\t# run sweeps = " << mhrw_params.
n_run <<
"\n";
161 template<
typename MHRWStatsCollectorResultType_,
typename IterCountIntType,
typename StepRealType>
178 acceptance_ratio(
std::numeric_limits<double>::quiet_NaN())
189 template<
typename MHRWStatsCollectorResultTypeInit,
190 typename MHRWParamsType2>
192 MHRWParamsType2 && mhrw_params_,
193 double acceptance_ratio_)
194 : stats_collector_result(
std::forward<MHRWStatsCollectorResultTypeInit>(stats_collector_result_)),
196 acceptance_ratio(acceptance_ratio_)
205 template<
typename MHRWStatsCollectorResultTypeInit,
typename MHRandomWalkType>
207 const MHRandomWalkType & mhrandomwalk)
208 : stats_collector_result(
std::forward<MHRWStatsCollectorResultTypeInit>(stats_collector_result_)),
210 acceptance_ratio(mhrandomwalk.hasAcceptanceRatio() ?
211 mhrandomwalk.acceptanceRatio() :
212 std::numeric_limits<double>::quiet_NaN())
236 template<
typename MHRandomWalkTaskCData,
267 IterCountIntType kstep_ = 0, MHRWParamsType mhrw_params_ =
MHRWParamsType(),
268 double acceptance_ratio_ = 0.0)
269 : MultiProc::TaskStatusReport(fdone, msg),
272 acceptance_ratio(acceptance_ratio_),
310 typename Rng::result_type _seed;
328 template<
typename LoggerType>
330 : _seed(inputseed), result(NULL)
332 logger.longdebug(
"MHRandomWalkTask",
"() inputseed=%d", inputseed);
337 if (result != NULL) {
352 template<
typename LoggerType,
typename TaskManagerIface>
353 inline void run(
const MHRandomWalkTaskCData * pcdata, LoggerType & logger,
354 TaskManagerIface * tmgriface)
358 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"about to construct stats collector.");
361 auto stats = pcdata->createStatsCollector(logger);
362 typedef decltype(stats) MHRWStatsCollectorType;
364 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"stats collector constructed.");
367 typedef StatusReportCheck<TaskManagerIface, MHRWStatsCollectorType> OurStatusReportCheck;
368 OurStatusReportCheck statreportcheck(
this, &stats, tmgriface);
372 OurStatsCollectors ourstatscollectors(stats, statreportcheck);
374 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"about to create MH walker object.");
376 auto mhwalker = pcdata->createMHWalker(rng, logger);
377 typedef decltype(pcdata->createMHWalker(rng, logger)) MHWalkerType;
379 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHWalker object created.");
394 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHRandomWalk object created, running...");
398 logger.longdebug(
"Tomographer::MHRWTasks::run()",
"MHRandomWalk run finished.");
400 result =
new ResultType(stats.getResult(), rwalk);
403 inline const ResultType & getResult()
const 414 template<
typename TaskManagerIface,
typename MHRWStatsCollectorType>
415 struct StatusReportCheck
417 StatusReportCheck(
MHRandomWalkTask * mhrwtask_, MHRWStatsCollectorType * stats_, TaskManagerIface *tmgriface_)
418 : mhrwtask(mhrwtask_), stats(stats_), tmgriface(tmgriface_)
422 MHRWStatsCollectorType *stats;
423 TaskManagerIface *tmgriface;
425 inline void init() { }
426 inline void thermalizingDone() { }
427 inline void done() { }
429 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
431 IterCountIntType k,
bool is_thermalizing,
bool,
bool,
double,
const PointType &, FnValueType,
438 if ((k % rw.
nSweep() == 0) && tmgriface->statusReportRequested()) {
442 IterCountIntType kreal = is_thermalizing ? k : k + rw.
nSweep()*rw.
nTherm();
443 double fdone = (double)kreal/totiters;
445 bool warn_accept_ratio =
false;
452 "%s %lu/(%lu=%lu*(%lu+%lu)) : %5.2f%% done [%saccept ratio=%.2f%s]",
456 (
unsigned long)kreal, (
unsigned long)totiters, (
unsigned long)rw.
nSweep(),
457 (
unsigned long)rw.
nTherm(), (
unsigned long)rw.
nRun(),
459 warn_accept_ratio ?
"!!** " :
"",
461 warn_accept_ratio ?
" **!!" :
"" 464 if (MHRWStatsCollectorStatusType::CanProvideStatus) {
467 std::string s = MHRWStatsCollectorStatusType::getStatus(stats);
481 template<
typename Po
intType,
typename FnValueType,
typename MHRandomWalk>
482 inline void processSample(IterCountIntType, IterCountIntType,
const PointType &, FnValueType,
MHRandomWalk &)
A Metropolis-Hastings Random Walk.
MHRandomWalkTaskResult< typename MHRandomWalkTaskCData::MHRWStatsCollectorResultType, IterCountIntType, StepRealType > ResultType
Result type of a single task run.
Utilities for formatting strings.
int getTaskInput(int k) const
Returns a random seed to seed the random number generator with for run number k.
void printBasicCDataMHRWInfo(std::ostream &str) const
Get some human-readable info about the random walk as a string.
StatusReport(double fdone=0.0, const std::string &msg=std::string(), IterCountIntType kstep_=0, MHRWParamsType mhrw_params_=MHRWParamsType(), double acceptance_ratio_=0.0)
Constructor which initializes all fields.
CountIntType nTherm() const
Number of thermalizing sweeps.
MHRWParamsType mhrw_params
the parameters of the random walk
double acceptance_ratio
the current acceptance ratio of the random walk (see Tomographer::MHRandomWalk::acceptanceRatio() ) ...
Base namespace for the Tomographer project.
const MHRWParamsType mhrw_params
The parameters of the random walk (see MHRWParams<IterCountIntType,StepRealType>) ...
CountIntType n_therm
Number of thermalization sweeps.
CountIntType n_run
Number of live sweeps.
const double MHRWAcceptanceRatioRecommendedMin
Minimal recommended acceptance ratio.
CountIntType nSweep() const
Number of iterations in a sweep.
std::string getBasicCDataMHRWInfo() const
Get some human-readable info about the random walk as a string, see printBasicCDataMHRWInfo() ...
const MHRWParamsType mhrw_params
Parameters of the random walk.
MHRandomWalkTask(int inputseed, const MHRandomWalkTaskCData *, LoggerType &logger)
Constructs the MHRandomWalkTask.
void run(const MHRandomWalkTaskCData *pcdata, LoggerType &logger, TaskManagerIface *tmgriface)
Run this task.
StepRealType step_size
The step size of the random walk.
Random Walk task, collecting statistics.
MHRandomWalkTaskResult(MHRWStatsCollectorResultTypeInit &&stats_collector_result_, const MHRandomWalkType &mhrandomwalk)
Constructor with mhrw_params initialized from a random walk instance.
MHRandomWalkTaskResult()
Construct an empty task result.
CountIntType nRun() const
Number of live run sweeps.
MHRWParams< IterCountIntType, StepRealType > MHRWParamsType
Type to store the parameters of the Metropolis-Hastings random walk (number of runs, sweep size, etc.)
Data needed to be accessible to the working code.
MHRWParams< IterCountIntType, StepRealType > MHRWParamsType
Type to stores the parameters of the random walk.
const double MHRWAcceptanceRatioRecommendedMax
Maximal recommended acceptance ratio.
MHRandomWalkTaskCData::StepRealType StepRealType
The type used to describe a step size (see MHRWParams)
const MHRWStatsCollectorResultType stats_collector_result
The result furnished by the stats collector itself.
MHRWStatsCollectorResultType_ MHRWStatsCollectorResultType
The specified result type of the MHRWStatsCollector the task will be looking at.
RatioType acceptanceRatio() const
Return the acceptance ratio so far.
MHRandomWalkTaskResult(MHRWStatsCollectorResultTypeInit &&stats_collector_result_, MHRWParamsType2 &&mhrw_params_, double acceptance_ratio_)
Constructor, initializes fields to the given values.
CDataBase(MHRWParamsType &&p, int base_seed_=0)
Constructor.
Basic status report class.
IterCountIntType_ IterCountIntType
Type used to count the number of iterations.
MHRWParams< IterCountIntType, StepRealType > MHRWParamsType
The type to use to store the parameters of the random walk.
void run()
Run the random walk. (pun intended)
StepRealType_ StepRealType
Type used to specify the step size.
Status Report for a MHRandomWalkTask.
MHRWParamsType mhrwParams() const
The parameters of the random walk.
Managing the need for specific overrides to operator new() for some types (especially Eigen types) ...
CountIntType n_sweep
The number of individual updates to collect together in a "sweep".
const double acceptance_ratio
The acceptance ratio of the Metropolis-Hastings random walk.
Some common definitions for multiprocessing interfaces.
Template, specializable class to get status reports from stats collectors.
StatusReport StatusReportType
Typedef for StatusReport. This is needed by, e.g. MultiProc::OMP::TaskDispatcher. ...
Definitions for MHRWStatsCollector Interface's.
Routines for performing a Metropolis-Hastings random walk.
T setprecision(T... args)
MHRandomWalkTaskCData::IterCountIntType IterCountIntType
The type used to count iterations (see MHRWParams)
IterCountIntType kstep
the current iteration number
bool hasAcceptanceRatio() const
Query whether we have any statistics about acceptance ratio. This is false, for example, during the thermalizing runs.
const int base_seed
A base random seed from which each run seed will be derived.
IterCountIntType n_total_iters
the total number of iterations required for this random walk
A simple MHRWStatsCollector interface which combines several stats collectors.