33 typedef boost::shared_ptr<ConjugateGradientParameters> shared_ptr;
47 : minIterations_(1), maxIterations_(500), reset_(501), epsilon_rel_(1e-3),
48 epsilon_abs_(1e-3), blas_kernel_(GTSAM) {}
50 ConjugateGradientParameters(
size_t minIterations,
size_t maxIterations,
size_t reset,
51 double epsilon_rel,
double epsilon_abs, BLASKernel blas)
52 : minIterations_(minIterations), maxIterations_(maxIterations), reset_(reset),
53 epsilon_rel_(epsilon_rel), epsilon_abs_(epsilon_abs), blas_kernel_(blas) {}
55 ConjugateGradientParameters(
const ConjugateGradientParameters &p)
56 : Base(p), minIterations_(p.minIterations_), maxIterations_(p.maxIterations_), reset_(p.reset_),
57 epsilon_rel_(p.epsilon_rel_), epsilon_abs_(p.epsilon_abs_), blas_kernel_(GTSAM) {}
60 inline size_t minIterations()
const {
return minIterations_; }
61 inline size_t maxIterations()
const {
return maxIterations_; }
62 inline size_t reset()
const {
return reset_; }
63 inline double epsilon()
const {
return epsilon_rel_; }
64 inline double epsilon_rel()
const {
return epsilon_rel_; }
65 inline double epsilon_abs()
const {
return epsilon_abs_; }
67 inline size_t getMinIterations()
const {
return minIterations_; }
68 inline size_t getMaxIterations()
const {
return maxIterations_; }
69 inline size_t getReset()
const {
return reset_; }
70 inline double getEpsilon()
const {
return epsilon_rel_; }
71 inline double getEpsilon_rel()
const {
return epsilon_rel_; }
72 inline double getEpsilon_abs()
const {
return epsilon_abs_; }
74 inline void setMinIterations(
size_t value) { minIterations_ = value; }
75 inline void setMaxIterations(
size_t value) { maxIterations_ = value; }
76 inline void setReset(
size_t value) { reset_ = value; }
77 inline void setEpsilon(
double value) { epsilon_rel_ = value; }
78 inline void setEpsilon_rel(
double value) { epsilon_rel_ = value; }
79 inline void setEpsilon_abs(
double value) { epsilon_abs_ = value; }
83 virtual void print(std::ostream &os)
const;
85 static std::string blasTranslator(
const BLASKernel k) ;
86 static BLASKernel blasTranslator(
const std::string &s) ;
99 template<
class S,
class V>
100 V preconditionedConjugateGradient(
const S &system,
const V &initial,
101 const ConjugateGradientParameters ¶meters) {
103 V estimate, residual, direction, q1, q2;
104 estimate = residual = direction = q1 = q2 = initial;
106 system.residual(estimate, q1);
107 system.leftPrecondition(q1, residual);
108 system.rightPrecondition(residual, direction);
110 double currentGamma = system.dot(residual, residual), prevGamma, alpha, beta;
112 const size_t iMaxIterations = parameters.maxIterations(),
113 iMinIterations = parameters.minIterations(),
114 iReset = parameters.reset() ;
115 const double threshold = std::max(parameters.epsilon_abs(),
116 parameters.epsilon() * parameters.epsilon() * currentGamma);
118 if (parameters.verbosity() >= ConjugateGradientParameters::COMPLEXITY )
119 std::cout <<
"[PCG] epsilon = " << parameters.epsilon()
120 <<
", max = " << parameters.maxIterations()
121 <<
", reset = " << parameters.reset()
122 <<
", ||r0||^2 = " << currentGamma
123 <<
", threshold = " << threshold << std::endl;
126 for ( k = 1 ; k <= iMaxIterations && (currentGamma > threshold || k <= iMinIterations) ; k++ ) {
128 if ( k % iReset == 0 ) {
129 system.residual(estimate, q1);
130 system.leftPrecondition(q1, residual);
131 system.rightPrecondition(residual, direction);
132 currentGamma = system.dot(residual, residual);
134 system.multiply(direction, q1);
135 alpha = currentGamma / system.dot(direction, q1);
136 system.axpy(alpha, direction, estimate);
137 system.leftPrecondition(q1, q2);
138 system.axpy(-alpha, q2, residual);
139 prevGamma = currentGamma;
140 currentGamma = system.dot(residual, residual);
141 beta = currentGamma / prevGamma;
142 system.rightPrecondition(residual, q1);
143 system.scal(beta, direction);
144 system.axpy(1.0, q1, direction);
146 if (parameters.verbosity() >= ConjugateGradientParameters::ERROR )
147 std::cout <<
"[PCG] k = " << k
148 <<
", alpha = " << alpha
149 <<
", beta = " << beta
150 <<
", ||r||^2 = " << currentGamma
155 if (parameters.verbosity() >= ConjugateGradientParameters::COMPLEXITY )
156 std::cout <<
"[PCG] iterations = " << k
157 <<
", ||r||^2 = " << currentGamma
parameters for the conjugate gradient method
Definition: ConjugateGradientSolver.h:29
void print(const Matrix &A, const string &s, ostream &stream)
print without optional string, must specify cout yourself
Definition: Matrix.cpp:141
BLASKernel
Definition: ConjugateGradientSolver.h:42
Some support classes for iterative solvers.
size_t reset_
number of iterations before reset
Definition: ConjugateGradientSolver.h:37
parameters for iterative linear solvers
Definition: IterativeSolver.h:44
Global functions in a separate testing namespace.
Definition: chartTesting.h:28
size_t maxIterations_
maximum number of cg iterations
Definition: ConjugateGradientSolver.h:36
double epsilon_abs_
threshold for absolute error decrease
Definition: ConjugateGradientSolver.h:39
size_t minIterations_
minimum number of cg iterations
Definition: ConjugateGradientSolver.h:35
double epsilon_rel_
threshold for relative error decrease
Definition: ConjugateGradientSolver.h:38