1 #ifndef HOPS_COORDINATEHITANDRUNPROPOSAL_HPP
2 #define HOPS_COORDINATEHITANDRUNPROPOSAL_HPP
5 #include "../IsSetStepSizeAvailable.hpp"
6 #include "../../RandomNumberGenerator/RandomNumberGenerator.hpp"
8 #include "../../FileWriter/CsvWriter.hpp"
11 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution = UniformStepDistribution<
typename MatrixType::Scalar>>
34 void setStepSize(
typename MatrixType::Scalar stepSize);
39 return chordStepDistribution.computeInverseNormalizationConstant(0, backwardDistance, forwardDistance)
40 - chordStepDistribution.computeInverseNormalizationConstant(0, backwardDistance - step,
41 forwardDistance - step);
53 long coordinateToUpdate = 0;
54 typename MatrixType::Scalar step = 0;
55 ChordStepDistribution chordStepDistribution;
56 typename MatrixType::Scalar forwardDistance;
57 typename MatrixType::Scalar backwardDistance;
60 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
67 state(std::move(currentState_)) {
68 slacks = this->b - this->A * this->state;
71 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
74 ++coordinateToUpdate %= state.rows();
76 inverseDistances = A.col(coordinateToUpdate).cwiseQuotient(slacks);
77 forwardDistance = 1. / inverseDistances.maxCoeff();
78 backwardDistance = 1. / inverseDistances.minCoeff();
79 assert(backwardDistance < 0 && forwardDistance > 0);
80 assert(((b - A * state).array() > 0).all());
82 step = chordStepDistribution.draw(randomNumberGenerator, backwardDistance, forwardDistance);
85 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
88 state(coordinateToUpdate) += step;
89 slacks.noalias() -= A.col(coordinateToUpdate) * step;
93 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
99 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
103 m_proposal(coordinateToUpdate) += step;
107 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
109 CoordinateHitAndRunProposal::state = std::move(newState);
110 slacks = b - A * CoordinateHitAndRunProposal::state;
113 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
115 typename MatrixType::Scalar stepSize) {
117 chordStepDistribution.setStepSize(stepSize);
120 throw std::runtime_error(
"Step size not available.");
124 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
125 typename MatrixType::Scalar
128 return chordStepDistribution.getStepSize();
130 throw std::runtime_error(
"Step size not available.");
133 template<
typename MatrixType,
typename VectorType,
typename ChordStepDistribution>
135 return "Coordinate Hit-and-Run";
139 #endif //HOPS_COORDINATEHITANDRUNPROPOSAL_HPP