remove the printf
parent
2174057578
commit
0e17310e23
|
|
@ -22,17 +22,14 @@ namespace gtsam {
|
||||||
template<class S, class V, class E>
|
template<class S, class V, class E>
|
||||||
V conjugateGradients(const S& Ab, V x, bool verbose, double epsilon, double epsilon_abs,
|
V conjugateGradients(const S& Ab, V x, bool verbose, double epsilon, double epsilon_abs,
|
||||||
size_t maxIterations, bool steepest = false) {
|
size_t maxIterations, bool steepest = false) {
|
||||||
//GTSAM_PRINT(Ab);
|
|
||||||
if (maxIterations == 0) maxIterations = dim(x) * (steepest ? 10 : 1);
|
if (maxIterations == 0) maxIterations = dim(x) * (steepest ? 10 : 1);
|
||||||
size_t reset = (size_t)(sqrt(dim(x))+0.5); // when to reset
|
size_t reset = (size_t)(sqrt(dim(x))+0.5); // when to reset
|
||||||
|
|
||||||
// Start with g0 = A'*(A*x0-b), d0 = - g0
|
// Start with g0 = A'*(A*x0-b), d0 = - g0
|
||||||
// i.e., first step is in direction of negative gradient
|
// i.e., first step is in direction of negative gradient
|
||||||
V g = Ab.gradient(x);
|
V g = Ab.gradient(x);
|
||||||
//print(g, "g");
|
|
||||||
V d = -g;
|
V d = -g;
|
||||||
double dotg0 = dot(g, g), prev_dotg = dotg0;
|
double dotg0 = dot(g, g), prev_dotg = dotg0;
|
||||||
//printf("dotg0:%g epsilon_abs:%g\n", dotg0, epsilon_abs);
|
|
||||||
if (dotg0 < epsilon_abs) return x;
|
if (dotg0 < epsilon_abs) return x;
|
||||||
double threshold = epsilon * epsilon * dotg0;
|
double threshold = epsilon * epsilon * dotg0;
|
||||||
|
|
||||||
|
|
@ -45,16 +42,15 @@ namespace gtsam {
|
||||||
|
|
||||||
// calculate optimal step-size
|
// calculate optimal step-size
|
||||||
E Ad = Ab * d;
|
E Ad = Ab * d;
|
||||||
//printf("dot(d, g):%g dot(Ad, Ad):%g\n", dot(d, g), dot(Ad, Ad));
|
|
||||||
double alpha = -dot(d, g) / dot(Ad, Ad);
|
double alpha = -dot(d, g) / dot(Ad, Ad);
|
||||||
//printf("alpha:%g\n", alpha);
|
|
||||||
|
|
||||||
// do step in new search direction
|
// do step in new search direction
|
||||||
x = x + alpha * d;
|
x = x + alpha * d;
|
||||||
if (k==maxIterations) break;
|
if (k==maxIterations) break;
|
||||||
|
|
||||||
// update gradient (or re-calculate at reset time)
|
// update gradient (or re-calculate at reset time)
|
||||||
g = (k%reset==0) ? Ab.gradient(x) : g + alpha * (Ab ^ Ad);
|
// g = (k%reset==0) ? Ab.gradient(x) : g + alpha * (Ab ^ Ad);
|
||||||
|
g = g + alpha * (Ab ^ Ad);
|
||||||
|
|
||||||
// check for convergence
|
// check for convergence
|
||||||
double dotg = dot(g, g);
|
double dotg = dot(g, g);
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue