Made CG state a class

release/4.3a0
Frank Dellaert 2010-02-14 05:52:20 +00:00
parent 43f9baf77a
commit 1f165a9f85
3 changed files with 88 additions and 44 deletions

View File

@ -15,68 +15,111 @@ using namespace std;
namespace gtsam { namespace gtsam {
/* ************************************************************************* */ /* ************************************************************************* */
/** // state for CG method
* conjugate gradient method.
* S: linear system, V: step vector, E: errors
*/
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, struct CGState {
size_t maxIterations, bool steepest = false) {
if (maxIterations == 0) maxIterations = dim(x) * (steepest ? 10 : 1);
size_t reset = (size_t)(sqrt(dim(x))+0.5); // when to reset
// Start with g0 = A'*(A*x0-b), d0 = - g0 bool steepest, verbose;
// i.e., first step is in direction of negative gradient double gamma, threshold;
V g = Ab.gradient(x); size_t k, maxIterations, reset;
V d = g; // instead of negating gradient, alpha will be negated V g, d;
double gamma0 = dot(g, g), gamma_old = gamma0; E Ad;
if (gamma0 < epsilon_abs) return x;
double threshold = epsilon * epsilon * gamma0;
if (verbose) cout << "CG: epsilon = " << epsilon << ", maxIterations = " /** constructor */
<< maxIterations << ", ||g0||^2 = " << gamma0 << ", threshold = " CGState(const S& Ab, const V& x, bool verb, double epsilon,
<< threshold << endl; double epsilon_abs, size_t maxIt, bool steep) {
k = 0;
verbose = verb;
steepest = steep;
maxIterations == maxIt ? maxIt : dim(x) * (steepest ? 10 : 1);
reset = (size_t) (sqrt(dim(x)) + 0.5); // when to reset
// Allocate and calculate A*d for first iteration // Start with g0 = A'*(A*x0-b), d0 = - g0
E Ad = Ab * d; // i.e., first step is in direction of negative gradient
g = Ab.gradient(x);
d = g; // instead of negating gradient, alpha will be negated
// loop maxIterations times // init gamma and calculate threshold
for (size_t k = 1;; k++) { gamma = dot(g, g);
threshold = ::max(epsilon_abs, epsilon * epsilon * gamma);
// calculate optimal step-size // Allocate and calculate A*d for first iteration
double alpha = - dot(d, g) / dot(Ad, Ad); if (gamma > epsilon) Ad = Ab * d;
}
// do step in new search direction /** print */
axpy(alpha, d, x); // x += alpha*d void print() {
if (k==maxIterations) break; cout << "iteration = " << k << endl;
cout << "dotg = " << gamma << endl;
gtsam::print(g,"g");
gtsam::print(d,"d");
gtsam::print(Ad,"Ad");
}
/** step the solution */
double takeOptimalStep(V& x) {
double alpha = -dot(d, g) / dot(Ad, Ad); // calculate optimal step-size
axpy(alpha, d, x); // // do step in new search direction, x += alpha*d
return alpha;
}
/** take a step, return true if converged */
bool step(const S& Ab, V& x) {
k += 1; // increase iteration number
double alpha = takeOptimalStep(x);
if (k == maxIterations) return true; //---------------------------------->
// update gradient (or re-calculate at reset time) // update gradient (or re-calculate at reset time)
if (k%reset==0) if (k % reset == 0)
g = Ab.gradient(x); g = Ab.gradient(x);
else else
// axpy(alpha, Ab ^ Ad, g); // g += alpha*(Ab^Ad) // axpy(alpha, Ab ^ Ad, g); // g += alpha*(Ab^Ad)
Ab.transposeMultiplyAdd(alpha, Ad, g); Ab.transposeMultiplyAdd(alpha, Ad, g);
// check for convergence // check for convergence
double gamma = dot(g, g); double new_gamma = dot(g, g);
if (verbose) cout << "iteration " << k << ": alpha = " << alpha if (verbose) cout << "iteration " << k << ": alpha = " << alpha << ", dotg = " << new_gamma << endl;
<< ", dotg = " << gamma << endl; // print();
if (gamma < threshold) break; if (new_gamma < threshold) return true; //---------------------------------->
// calculate new search direction // calculate new search direction
if (steepest) if (steepest)
d = g; d = g;
else { else {
double beta = gamma / gamma_old; double beta = new_gamma / gamma;
gamma_old = gamma; gamma = new_gamma;
// d = g + d*beta; // d = g + d*beta;
scal(beta,d); scal(beta, d);
axpy(1.0, g, d); axpy(1.0, g, d);
} }
// In-place recalculation Ad <- A*d to avoid re-allocating Ad // In-place recalculation Ad <- A*d to avoid re-allocating Ad
Ab.multiplyInPlace(d,Ad); Ab.multiplyInPlace(d, Ad);
return false;
} }
};
/**
* conjugate gradient method.
* S: linear system, V: step vector, E: errors
*/
template<class S, class V, class E>
V conjugateGradients(const S& Ab, V x, bool verbose, double epsilon,
double epsilon_abs, size_t maxIterations, bool steepest = false) {
CGState<S, V, E> state(Ab, x, verbose, epsilon, epsilon_abs, maxIterations, steepest);
if (state.gamma < state.threshold) return x;
if (verbose) cout << "CG: epsilon = " << epsilon << ", maxIterations = "
<< maxIterations << ", ||g0||^2 = " << state.gamma
<< ", threshold = " << state.threshold << endl;
// loop maxIterations times
while (!state.step(Ab, x))
;
return x; return x;
} }

View File

@ -26,6 +26,8 @@ using namespace std;
using namespace gtsam; using namespace gtsam;
using namespace example; using namespace example;
static bool verbose = false;
/* ************************************************************************* */ /* ************************************************************************* */
TEST( Iterative, steepestDescent ) TEST( Iterative, steepestDescent )
{ {
@ -38,7 +40,6 @@ TEST( Iterative, steepestDescent )
// Do gradient descent // Do gradient descent
GaussianFactorGraph fg2 = createGaussianFactorGraph(); GaussianFactorGraph fg2 = createGaussianFactorGraph();
VectorConfig zero = createZeroDelta(); VectorConfig zero = createZeroDelta();
bool verbose = false;
VectorConfig actual = steepestDescent(fg2, zero, verbose); VectorConfig actual = steepestDescent(fg2, zero, verbose);
CHECK(assert_equal(expected,actual,1e-2)); CHECK(assert_equal(expected,actual,1e-2));
} }
@ -62,20 +63,20 @@ TEST( Iterative, conjugateGradientDescent )
// Do conjugate gradient descent, System version // Do conjugate gradient descent, System version
System Ab(A, b); System Ab(A, b);
Vector actualX = conjugateGradientDescent(Ab, x0); Vector actualX = conjugateGradientDescent(Ab, x0, verbose);
CHECK(assert_equal(expectedX,actualX,1e-9)); CHECK(assert_equal(expectedX,actualX,1e-9));
// Do conjugate gradient descent, Matrix version // Do conjugate gradient descent, Matrix version
Vector actualX2 = conjugateGradientDescent(A, b, x0); Vector actualX2 = conjugateGradientDescent(A, b, x0, verbose);
CHECK(assert_equal(expectedX,actualX2,1e-9)); CHECK(assert_equal(expectedX,actualX2,1e-9));
// Do conjugate gradient descent on factor graph // Do conjugate gradient descent on factor graph
VectorConfig zero = createZeroDelta(); VectorConfig zero = createZeroDelta();
VectorConfig actual = conjugateGradientDescent(fg2, zero); VectorConfig actual = conjugateGradientDescent(fg2, zero, verbose);
CHECK(assert_equal(expected,actual,1e-2)); CHECK(assert_equal(expected,actual,1e-2));
// Test method // Test method
VectorConfig actual2 = fg2.conjugateGradientDescent(zero); VectorConfig actual2 = fg2.conjugateGradientDescent(zero, verbose);
CHECK(assert_equal(expected,actual2,1e-2)); CHECK(assert_equal(expected,actual2,1e-2));
} }
@ -122,7 +123,7 @@ TEST( Iterative, conjugateGradientDescent_soft_constraint )
zeros.insert("x2",zero(3)); zeros.insert("x2",zero(3));
GaussianFactorGraph fg = graph.linearize(config); GaussianFactorGraph fg = graph.linearize(config);
VectorConfig actual = conjugateGradientDescent(fg, zeros, false, 1e-3, 1e-5, 100); VectorConfig actual = conjugateGradientDescent(fg, zeros, verbose, 1e-3, 1e-5, 100);
VectorConfig expected; VectorConfig expected;
expected.insert("x1", zero(3)); expected.insert("x1", zero(3));
@ -169,7 +170,7 @@ TEST( Iterative, subgraphPCG )
// Solve the subgraph PCG // Solve the subgraph PCG
VectorConfig ybar = conjugateGradients<SubgraphPreconditioner, VectorConfig, VectorConfig ybar = conjugateGradients<SubgraphPreconditioner, VectorConfig,
Errors> (system, zeros, false, 1e-5, 1e-5, 100); Errors> (system, zeros, verbose, 1e-5, 1e-5, 100);
VectorConfig actual = system.x(ybar); VectorConfig actual = system.x(ybar);
VectorConfig expected; VectorConfig expected;

View File

@ -210,7 +210,7 @@ TEST( SubgraphPreconditioner, conjugateGradients )
// Compare with non preconditioned version: // Compare with non preconditioned version:
VectorConfig actual2 = conjugateGradientDescent(Ab, x1, verbose, epsilon, VectorConfig actual2 = conjugateGradientDescent(Ab, x1, verbose, epsilon,
maxIterations); maxIterations);
CHECK(assert_equal(xtrue,actual2,1e-5)); CHECK(assert_equal(xtrue,actual2,1e-4));
} }
/* ************************************************************************* */ /* ************************************************************************* */