MUQ  0.4.3
TransitionKernel.h
Go to the documentation of this file.
1 #ifndef TRANSITIONKERNEL_H_
2 #define TRANSITIONKERNEL_H_
3 
4 #include <iostream>
5 #include <map>
6 
7 #include <boost/function.hpp>
8 #include <boost/property_tree/ptree.hpp>
9 
10 #include "MUQ/config.h"
11 
12 #if MUQ_HAS_PARCER
13 #include <parcer/Communicator.h>
14 #endif
15 
17 
18 #include "MUQ/Modeling/WorkPiece.h"
19 
22 
23 namespace muq {
24  namespace SamplingAlgorithms {
25 
35  class TransitionKernel { //: public muq::Modeling::WorkPiece {
36  public:
37 
38  TransitionKernel(boost::property_tree::ptree const& pt, std::shared_ptr<AbstractSamplingProblem> problem);
39 
40  virtual ~TransitionKernel() = default;
41 
42 #if MUQ_HAS_PARCER
43  virtual void SetCommunicator(std::shared_ptr<parcer::Communicator> newcomm);
44 #endif
45 
47 
52  static std::shared_ptr<TransitionKernel> Construct(boost::property_tree::ptree const& pt, std::shared_ptr<AbstractSamplingProblem> problem);
53  typedef boost::function<std::shared_ptr<TransitionKernel>(boost::property_tree::ptree, std::shared_ptr<AbstractSamplingProblem>)> TransitionKernelConstructor;
54  typedef std::map<std::string, TransitionKernelConstructor> TransitionKernelMap;
55  static std::shared_ptr<TransitionKernelMap> GetTransitionKernelMap();
56 
58 
63  virtual inline void PreStep(unsigned int const t, std::shared_ptr<SamplingState> state) {};
64 
66 
71  virtual inline void PostStep(unsigned int const t, std::vector<std::shared_ptr<SamplingState>> const& state) {};
72 
77  virtual std::vector<std::shared_ptr<SamplingState>> Step(unsigned int const t, std::shared_ptr<SamplingState> prevState) = 0;
78 
79  virtual inline void PrintStatus() const {PrintStatus("");};
80  virtual inline void PrintStatus(std::string prefix) const {};
81 
82  // What block of the state does this kernel work on?
83 
84  virtual void SetBlockInd(int newBlockInd){blockInd = newBlockInd;};
85  virtual int GetBlockInd() const{return blockInd;};
86 
88  virtual std::shared_ptr<AbstractSamplingProblem> const& Problem() const{return problem;};
89 
90  protected:
91 
92  int blockInd = 0;
93 
95  std::shared_ptr<AbstractSamplingProblem> problem;
96 
97 #if MUQ_HAS_PARCER
98  std::shared_ptr<parcer::Communicator> comm;
99 #endif
100 
102 
105  const bool reeval;
106 
107  private:
108  };
109  } // namespace SamplingAlgorithms
110 } // namespace muq
111 
112 #define REGISTER_TRANSITION_KERNEL(NAME) static auto reg ##NAME \
113  = muq::SamplingAlgorithms::TransitionKernel::GetTransitionKernelMap()->insert(std::make_pair(#NAME, muq::Utilities::shared_factory<NAME>()));
114 
115 #endif
Defines the transition kernel used by an MCMC algorithm.
std::map< std::string, TransitionKernelConstructor > TransitionKernelMap
virtual void PrintStatus(std::string prefix) const
virtual void PreStep(unsigned int const t, std::shared_ptr< SamplingState > state)
Allow the kernel to preprocess the current step.
std::shared_ptr< parcer::Communicator > comm
static std::shared_ptr< TransitionKernel > Construct(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem)
Static constructor for the transition kernel.
virtual std::vector< std::shared_ptr< SamplingState > > Step(unsigned int const t, std::shared_ptr< SamplingState > prevState)=0
boost::function< std::shared_ptr< TransitionKernel >boost::property_tree::ptree, std::shared_ptr< AbstractSamplingProblem >)> TransitionKernelConstructor
std::shared_ptr< AbstractSamplingProblem > problem
The sampling problem that evaluates/samples the target distribution.
const bool reeval
true: reevaluate the log density (even if one already exists), false: use stored log density
virtual void SetCommunicator(std::shared_ptr< parcer::Communicator > newcomm)
static std::shared_ptr< TransitionKernelMap > GetTransitionKernelMap()
virtual void SetBlockInd(int newBlockInd)
virtual std::shared_ptr< AbstractSamplingProblem > const & Problem() const
virtual void PostStep(unsigned int const t, std::vector< std::shared_ptr< SamplingState >> const &state)
Allow the kernel to adapt given a new state.
TransitionKernel(boost::property_tree::ptree const &pt, std::shared_ptr< AbstractSamplingProblem > problem)