Tomographer  v5.0
Tomographer C++ Framework Documentation
mhrwvalueerrorbinsconvergedcontroller.h
Go to the documentation of this file.
1 /* This file is part of the Tomographer project, which is distributed under the
2  * terms of the MIT license.
3  *
4  * The MIT License (MIT)
5  *
6  * Copyright (c) 2016 ETH Zurich, Institute for Theoretical Physics, Philippe Faist
7  * Copyright (c) 2017 Caltech, Institute for Quantum Information and Matter, Philippe Faist
8  *
9  * Permission is hereby granted, free of charge, to any person obtaining a copy
10  * of this software and associated documentation files (the "Software"), to deal
11  * in the Software without restriction, including without limitation the rights
12  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13  * copies of the Software, and to permit persons to whom the Software is
14  * furnished to do so, subject to the following conditions:
15  *
16  * The above copyright notice and this permission notice shall be included in
17  * all copies or substantial portions of the Software.
18  *
19  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22  * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25  * SOFTWARE.
26  */
27 
28 #ifndef _TOMOGRAPHER_MHRWVALUEERRORBINSCONVERGEDCONTROLLER_H
29 #define _TOMOGRAPHER_MHRWVALUEERRORBINSCONVERGEDCONTROLLER_H
30 
31 #include <cstddef>
32 #include <cmath>
33 #include <cstdlib>
34 
35 #include <algorithm> // std::max
36 #include <limits>
37 #include <random>
38 #include <iomanip>
39 
41 #include <tomographer/tools/fmt.h>
44 #include <tomographer/mhrw.h>
46 
47 
57 namespace Tomographer {
58 
59 
66 template<typename ValueHistogramWithBinningMHRWStatsCollectorType_,
67  typename IterCountIntType_,
68  typename BaseLoggerType_>
69 class TOMOGRAPHER_EXPORT MHRWValueErrorBinsConvergedController
70 {
71 public:
72  // we never have to adjust the params, we just forbid from stopping too early in the
73  // allowDoneRuns() callback
74  enum { AdjustmentStrategy = MHRWControllerDoNotAdjust };
75 
76  typedef ValueHistogramWithBinningMHRWStatsCollectorType_
77  ValueHistogramWithBinningMHRWStatsCollectorType;
78 
79  typedef IterCountIntType_ IterCountIntType;
80 
81  typedef BaseLoggerType_ BaseLoggerType;
82 
83 private:
84  const ValueHistogramWithBinningMHRWStatsCollectorType & value_stats_collector;
85 
86  const IterCountIntType check_frequency_sweeps;
87 
88  IterCountIntType last_forbidden_iter_number;
89 
90  const Eigen::Index max_allowed_unknown;
91  const Eigen::Index max_allowed_unknown_notisolated;
92  const Eigen::Index max_allowed_not_converged;
93 
95 
96 public:
103  const ValueHistogramWithBinningMHRWStatsCollectorType & value_stats_collector_,
104  BaseLoggerType & baselogger_,
105  IterCountIntType check_frequency_sweeps_ = 1024,
106  Eigen::Index max_allowed_unknown_ = 0,
107  Eigen::Index max_allowed_unknown_notisolated_ = 0,
108  Eigen::Index max_allowed_not_converged_ = 0
109  )
110  : value_stats_collector(value_stats_collector_),
111  check_frequency_sweeps(check_frequency_sweeps_),
112  last_forbidden_iter_number(0),
113  max_allowed_unknown(max_allowed_unknown_),
114  max_allowed_unknown_notisolated(max_allowed_unknown_notisolated_),
115  max_allowed_not_converged(max_allowed_not_converged_),
116  llogger("Tomographer::MHRWValueErrorBinsConvergedAdjuster", baselogger_)
117  {
118  }
119 
120  template<typename MHRWParamsType, typename MHWalker, typename MHRandomWalkType>
121  inline void init(MHRWParamsType & /*params*/, const MHWalker & /*mhwalker*/,
122  const MHRandomWalkType & /*mhrw*/) const
123  {
124  }
125 
126  template<typename MHRWParamsType, typename MHWalker, typename CountIntType, typename MHRandomWalkType>
127  bool allowDoneThermalization(const MHRWParamsType & /*params*/, const MHWalker & /*mhwalker*/,
128  CountIntType /*iter_k*/, const MHRandomWalkType & /*mhrw*/) const
129  {
130  return true;
131  }
132 
133  template<typename MHRWParamsType, typename MHWalker, typename MHRandomWalkType>
134  bool allowDoneRuns(const MHRWParamsType & params, const MHWalker & /*mhwalker*/,
135  IterCountIntType iter_k, const MHRandomWalkType & /*mhrw*/)
136  {
137 
138  if (check_frequency_sweeps == 0) {
139  // controller is manually disabled by setting check_frequency_sweeps=0
140  return true;
141  }
142 
143  auto logger = llogger.subLogger(TOMO_ORIGIN);
144 
145  if (last_forbidden_iter_number > 0 &&
146  (iter_k-last_forbidden_iter_number) < params.n_sweep*check_frequency_sweeps) {
147  // not enough new samples since last time we rejected to finish the random walk
148  return false;
149  }
150 
151  // re-check if the error bars have converged
152 
153  // do a convergence analysis of the error bars
154  const auto& binning_analysis = value_stats_collector.getBinningAnalysis();
155  const auto binmeans = value_stats_collector.binMeans();
156 
157  const auto error_levels = binning_analysis.calcErrorLevels(binmeans);
158  const auto conv_status = binning_analysis.determineErrorConvergence(error_levels);
159 
160  const auto conv_summary = BinningErrorBarConvergenceSummary::fromConvergedStatus(conv_status);
161 
162  logger.longdebug([&](std::ostream & stream) { stream << "Convergence summary = " << conv_summary; }) ;
163 
164  if (conv_summary.n_not_converged > max_allowed_not_converged ||
165  conv_summary.n_unknown > max_allowed_unknown ||
166  (conv_summary.n_unknown-conv_summary.n_unknown_isolated) > max_allowed_unknown_notisolated) {
167  // too many unconverged error bars, continue running
168  return false;
169  }
170 
171  // all ok
172  return true;
173  }
174 
175  template<typename MHRWParamsType, typename MHWalker, typename MHRandomWalkType>
176  inline void thermalizingDone(const MHRWParamsType & /*params*/, const MHWalker & /*mhwalker*/,
177  const MHRandomWalkType & /*mhrw*/) const
178  {
179  }
180 
181  template<typename MHRWParamsType, typename MHWalker, typename MHRandomWalkType>
182  inline void done(MHRWParamsType & /*params*/, const MHWalker & /*mhwalker*/,
183  const MHRandomWalkType & /*mhrw*/) const
184  {
185  }
186 };
187 
188 
194 template<typename IterCountIntType_ = int,
195  // these types are deduced from the args anyway:
196  typename ValueHistogramWithBinningMHRWStatsCollectorType_ = void,
197  typename BaseLoggerType_ = void>
198 inline MHRWValueErrorBinsConvergedController<ValueHistogramWithBinningMHRWStatsCollectorType_,
199  IterCountIntType_,
200  BaseLoggerType_>
202  const ValueHistogramWithBinningMHRWStatsCollectorType_ & value_stats_collector_,
203  BaseLoggerType_ & baselogger_,
204  IterCountIntType_ check_frequency_sweeps_ = 1024,
205  Eigen::Index max_allowed_unknown_ = 0,
206  Eigen::Index max_allowed_unknown_notisolated_ = 0,
207  Eigen::Index max_allowed_not_converged_ = 0
208  )
209 {
210  return MHRWValueErrorBinsConvergedController<ValueHistogramWithBinningMHRWStatsCollectorType_,
211  IterCountIntType_,
212  BaseLoggerType_>(
213  value_stats_collector_,
214  baselogger_,
215  check_frequency_sweeps_,
216  max_allowed_unknown_,
217  max_allowed_unknown_notisolated_,
218  max_allowed_not_converged_
219  ) ;
220 }
221 
222 
223 
224 
225 } // namespace Tomographer
226 
227 
228 
229 #endif
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...
Definition: mhrw_bin_err.h:131
A MHRWController Interface which stops the random walk after enough samples have been taken to make a...
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)
Convenience function to create a MHRWValueErrorBinsConvergedController (using template argument deduc...
Base namespace for the Tomographer project.
Definition: densellh.h:45
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)
Constructor.
#define TOMO_ORIGIN
Use this as argument for a Tomographer::Logger::LocalLogger constructor .
Definition: loggers.h:1669
LocalLogger< LocalLogger< BaseLoggerType > > subLogger(const std::string &new_prefix)
Create a sub-logger.
Definition: loggers.h:1907
Never adjust the parameters of the random walk.
Definition: mhrw.h:190
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&#39;s.
Routines for performing a Metropolis-Hastings random walk.
STL class.
Utilities for logging messages.