/* * SubgraphSolver-inl.h * * Created on: Jan 17, 2010 * Author: nikai * Description: subgraph preconditioning conjugate gradient solver */ #pragma once #include #include #include #include #include using namespace std; namespace gtsam { /* ************************************************************************* */ template SubgraphSolver::SubgraphSolver(const Graph& G, const Config& theta0) { initialize(G,theta0); } /* ************************************************************************* */ template void SubgraphSolver::initialize(const Graph& G, const Config& theta0) { // generate spanning tree PredecessorMap tree = G.template findMinimumSpanningTree(); list keys = predecessorMap2Keys(tree); // split the graph if (verbose_) cout << "generating spanning tree and split the graph ..."; G.template split(tree, T_, C_); if (verbose_) cout << T_.size() << " and " << C_.size() << " factors" << endl; // make the ordering list symbols; symbols.resize(keys.size()); std::transform(keys.begin(), keys.end(), symbols.begin(), key2symbol); ordering_ = boost::shared_ptr(new Ordering(symbols)); // compose the approximate solution Key root = keys.back(); theta_bar_ = composePoses (T_, tree, theta0[root]); } /* ************************************************************************* */ template boost::shared_ptr SubgraphSolver::linearize(const Graph& G, const Config& theta_bar) const { SubgraphPreconditioner::sharedFG Ab1 = T_.linearize(theta_bar); SubgraphPreconditioner::sharedFG Ab2 = C_.linearize(theta_bar); #ifdef TIMING SubgraphPreconditioner::sharedBayesNet Rc1; SubgraphPreconditioner::sharedConfig xbar; #else GaussianFactorGraph sacrificialAb1 = *Ab1; // duplicate !!!!! SubgraphPreconditioner::sharedBayesNet Rc1 = sacrificialAb1.eliminate_(*ordering_); SubgraphPreconditioner::sharedConfig xbar = gtsam::optimize_(*Rc1); #endif // TODO: there does not seem to be a good reason to have Ab1_ // It seems only be used to provide an ordering for creating sparse matrices return boost::shared_ptr(new SubgraphPreconditioner(Ab1, Ab2, Rc1, xbar)); } /* ************************************************************************* */ template VectorConfig SubgraphSolver::optimize(SubgraphPreconditioner& system) const { VectorConfig zeros = system.zero(); // Solve the subgraph PCG VectorConfig ybar = conjugateGradients (system, zeros, verbose_, epsilon_, epsilon_abs_, maxIterations_); VectorConfig xbar = system.x(ybar); return xbar; } }