hops
BallWalkProposal.hpp
Go to the documentation of this file.
1 #ifndef HOPS_BALLWALKPROPOSAL_HPP
2 #define HOPS_BALLWALKPROPOSAL_HPP
3 
4 #include "../IsSetStepSizeAvailable.hpp"
5 #include "../../RandomNumberGenerator/RandomNumberGenerator.hpp"
6 #include "../../FileWriter/CsvWriter.hpp"
7 #include <random>
8 
9 namespace hops {
10  template<typename MatrixType, typename VectorType>
12  public:
14 
22  BallWalkProposal(MatrixType A, VectorType b, StateType currentState, typename MatrixType::Scalar stepSize = 1);
23 
24  void propose(RandomNumberGenerator &randomNumberGenerator);
25 
26  void acceptProposal();
27 
28  StateType getState() const;
29 
30  StateType getProposal() const;
31 
32  void setState(StateType newState);
33 
34  void setStepSize(typename MatrixType::Scalar stepSize);
35 
36  typename MatrixType::Scalar getStepSize() const;
37 
38  [[nodiscard]] typename MatrixType::Scalar computeLogAcceptanceProbability() {
39  bool isProposalInteriorPoint = ((A * m_proposal - b).array() < 0).all();
40  if (!isProposalInteriorPoint) {
41  return -std::numeric_limits<typename MatrixType::Scalar>::infinity();
42  }
43  return 0;
44  }
45 
47 
48  private:
49  MatrixType A;
50  VectorType b;
51  StateType state;
52  StateType m_proposal;
53 
54  typename MatrixType::Scalar stepSize;
55 
56  std::uniform_real_distribution<typename MatrixType::Scalar> uniform;
57  std::normal_distribution<typename MatrixType::Scalar> normal;
58  };
59 
60  template<typename MatrixType, typename VectorType>
62  VectorType b_,
63  VectorType currentState_,
64  typename MatrixType::Scalar stepSize_) :
65  A(std::move(A_)),
66  b(std::move(b_)),
67  state(std::move(currentState_)),
68  m_proposal(this->state),
69  stepSize(stepSize_) {
70  // nothing to do
71  }
72 
73  template<typename MatrixType, typename VectorType>
75  RandomNumberGenerator &randomNumberGenerator) {
76  // Creates m_proposal on Ballsurface
77  for (long i = 0; i < m_proposal.rows(); ++i) {
78  m_proposal(i) = normal(randomNumberGenerator);
79  }
80  m_proposal.normalize();
81 
82  // Scale m_proposal to radius of Ball
83  m_proposal.noalias() = stepSize*m_proposal;
84 
85  // Scales m_proposal into Ball
86  m_proposal.noalias() = std::pow(uniform(randomNumberGenerator), 1. / m_proposal.rows()) * m_proposal;
87 
88  m_proposal.noalias() += state;
89  }
90 
91  template<typename MatrixType, typename VectorType>
92  void
94  state.swap(m_proposal);
95  }
96 
97  template<typename MatrixType, typename VectorType>
100  return state;
101  }
102 
103  template<typename MatrixType, typename VectorType>
106  return m_proposal;
107  }
108 
109  template<typename MatrixType, typename VectorType>
111  BallWalkProposal::state = std::move(newState);
112  }
113 
114  template<typename MatrixType, typename VectorType>
116  typename MatrixType::Scalar newStepSize) {
117  stepSize = newStepSize;
118  }
119 
120  template<typename MatrixType, typename VectorType>
121  typename MatrixType::Scalar
123  return stepSize;
124  }
125 
126  template<typename MatrixType, typename VectorType>
128  return "Ball Walk";
129  }
130 }
131 
132 #endif //HOPS_BALLWALKPROPOSAL_HPP
hops::MatrixType
Eigen::MatrixXd MatrixType
Definition: MatrixType.hpp:7
hops::BallWalkProposal::BallWalkProposal
BallWalkProposal(MatrixType A, VectorType b, StateType currentState, typename MatrixType::Scalar stepSize=1)
Constructs Ballwalk m_proposal mechanism on polytope defined as Ax<b.
Definition: BallWalkProposal.hpp:61
hops::BallWalkProposal::setState
void setState(StateType newState)
Definition: BallWalkProposal.hpp:110
hops::BallWalkProposal::getName
std::string getName()
Definition: BallWalkProposal.hpp:127
pcg_detail::engine
Definition: pcg_random.hpp:364
hops::BallWalkProposal::getProposal
StateType getProposal() const
Definition: BallWalkProposal.hpp:105
hops::BallWalkProposal::acceptProposal
void acceptProposal()
Definition: BallWalkProposal.hpp:93
hops::BallWalkProposal::setStepSize
void setStepSize(typename MatrixType::Scalar stepSize)
Definition: BallWalkProposal.hpp:115
hops::BallWalkProposal::getStepSize
MatrixType::Scalar getStepSize() const
Definition: BallWalkProposal.hpp:122
hops::BallWalkProposal::StateType
VectorType StateType
Definition: BallWalkProposal.hpp:13
hops::BallWalkProposal::propose
void propose(RandomNumberGenerator &randomNumberGenerator)
Definition: BallWalkProposal.hpp:74
hops::BallWalkProposal
Definition: BallWalkProposal.hpp:11
hops
Definition: CsvReader.hpp:8
hops::BallWalkProposal::getState
StateType getState() const
Definition: BallWalkProposal.hpp:99
string
NAME string(REPLACE ".cpp" "_bin" example_name ${example_filename}) if($
Definition: hops/Third-party/HighFive/src/examples/CMakeLists.txt:6
hops::VectorType
Eigen::VectorXd VectorType
Definition: VectorType.hpp:7
hops::BallWalkProposal::computeLogAcceptanceProbability
MatrixType::Scalar computeLogAcceptanceProbability()
Definition: BallWalkProposal.hpp:38