Resurrected tests

release/4.3a0
Frank Dellaert 2019-04-04 23:01:47 -04:00
parent 6b637bda9e
commit 3889b29305
1 changed files with 122 additions and 72 deletions

View File

@ -17,12 +17,11 @@
#include <CppUnitLite/TestHarness.h> #include <CppUnitLite/TestHarness.h>
#if 0
#include <tests/smallExample.h> #include <tests/smallExample.h>
#include <gtsam/inference/Symbol.h> #include <gtsam/inference/Symbol.h>
#include <gtsam/linear/iterative.h> #include <gtsam/linear/iterative.h>
#include <gtsam/linear/GaussianFactorGraph.h> #include <gtsam/linear/GaussianFactorGraph.h>
#include <gtsam/linear/GaussianEliminationTree.h>
#include <gtsam/linear/SubgraphPreconditioner.h> #include <gtsam/linear/SubgraphPreconditioner.h>
#include <gtsam/inference/Ordering.h> #include <gtsam/inference/Ordering.h>
#include <gtsam/base/numericalDerivative.h> #include <gtsam/base/numericalDerivative.h>
@ -49,7 +48,7 @@ TEST( SubgraphPreconditioner, planarOrdering ) {
key(3, 3), key(2, 3), key(1, 3), key(3, 3), key(2, 3), key(1, 3),
key(3, 2), key(2, 2), key(1, 2), key(3, 2), key(2, 2), key(1, 2),
key(3, 1), key(2, 1), key(1, 1); key(3, 1), key(2, 1), key(1, 1);
CHECK(assert_equal(expected,ordering)); EXPECT(assert_equal(expected,ordering));
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -73,9 +72,9 @@ TEST( SubgraphPreconditioner, planarGraph )
DOUBLES_EQUAL(0,error(A,xtrue),1e-9); // check zero error for xtrue DOUBLES_EQUAL(0,error(A,xtrue),1e-9); // check zero error for xtrue
// Check that xtrue is optimal // Check that xtrue is optimal
GaussianBayesNet::shared_ptr R1 = GaussianSequentialSolver(A).eliminate(); GaussianBayesNet::shared_ptr R1 = A.eliminateSequential();
VectorValues actual = optimize(*R1); VectorValues actual = R1->optimize();
CHECK(assert_equal(xtrue,actual)); EXPECT(assert_equal(xtrue,actual));
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -87,19 +86,18 @@ TEST( SubgraphPreconditioner, splitOffPlanarTree )
boost::tie(A, xtrue) = planarGraph(3); boost::tie(A, xtrue) = planarGraph(3);
// Get the spanning tree and constraints, and check their sizes // Get the spanning tree and constraints, and check their sizes
GaussianFactorGraph T, C; GaussianFactorGraph::shared_ptr T, C;
boost::tie(T, C) = splitOffPlanarTree(3, A); boost::tie(T, C) = splitOffPlanarTree(3, A);
LONGS_EQUAL(9,T.size()); LONGS_EQUAL(9,T->size());
LONGS_EQUAL(4,C.size()); LONGS_EQUAL(4,C->size());
// Check that the tree can be solved to give the ground xtrue // Check that the tree can be solved to give the ground xtrue
GaussianBayesNet::shared_ptr R1 = GaussianSequentialSolver(T).eliminate(); GaussianBayesNet::shared_ptr R1 = T->eliminateSequential();
VectorValues xbar = optimize(*R1); VectorValues xbar = R1->optimize();
CHECK(assert_equal(xtrue,xbar)); EXPECT(assert_equal(xtrue,xbar));
} }
/* ************************************************************************* */ /* ************************************************************************* */
TEST( SubgraphPreconditioner, system ) TEST( SubgraphPreconditioner, system )
{ {
// Build a planar graph // Build a planar graph
@ -108,71 +106,128 @@ TEST( SubgraphPreconditioner, system )
size_t N = 3; size_t N = 3;
boost::tie(Ab, xtrue) = planarGraph(N); // A*x-b boost::tie(Ab, xtrue) = planarGraph(N); // A*x-b
// Get the spanning tree and corresponding ordering // Get the spanning tree and remaining graph
GaussianFactorGraph Ab1_, Ab2_; // A1*x-b1 and A2*x-b2 GaussianFactorGraph::shared_ptr Ab1, Ab2; // A1*x-b1 and A2*x-b2
boost::tie(Ab1_, Ab2_) = splitOffPlanarTree(N, Ab); boost::tie(Ab1, Ab2) = splitOffPlanarTree(N, Ab);
SubgraphPreconditioner::sharedFG Ab1(new GaussianFactorGraph(Ab1_));
SubgraphPreconditioner::sharedFG Ab2(new GaussianFactorGraph(Ab2_));
// Eliminate the spanning tree to build a prior // Eliminate the spanning tree to build a prior
SubgraphPreconditioner::sharedBayesNet Rc1 = GaussianSequentialSolver(Ab1_).eliminate(); // R1*x-c1 const Ordering ord = planarOrdering(N);
VectorValues xbar = optimize(*Rc1); // xbar = inv(R1)*c1 auto Rc1 = Ab1->eliminateSequential(ord); // R1*x-c1
VectorValues xbar = Rc1->optimize(); // xbar = inv(R1)*c1
// Create Subgraph-preconditioned system // Create Subgraph-preconditioned system
VectorValues::shared_ptr xbarShared(new VectorValues(xbar)); // TODO: horrible VectorValues::shared_ptr xbarShared(new VectorValues(xbar)); // TODO: horrible
SubgraphPreconditioner system(Ab2, Rc1, xbarShared); const SubgraphPreconditioner system(Ab2, Rc1, xbarShared);
// Get corresponding matrices for tests. Add dummy factors to Ab2 to make
// sure it works with the ordering.
Ordering ordering = Rc1->ordering(); // not ord in general!
Ab2->add(key(1,1),Z_2x2, Z_2x1);
Ab2->add(key(1,2),Z_2x2, Z_2x1);
Ab2->add(key(1,3),Z_2x2, Z_2x1);
Matrix A, A1, A2;
Vector b, b1, b2;
std::tie(A,b) = Ab.jacobian(ordering);
std::tie(A1,b1) = Ab1->jacobian(ordering);
std::tie(A2,b2) = Ab2->jacobian(ordering);
Matrix R1 = Rc1->matrix(ordering).first;
Matrix Abar(13 * 2, 9 * 2);
Abar.topRows(9 * 2) = Matrix::Identity(9 * 2, 9 * 2);
Abar.bottomRows(4 * 2) = A2 * R1.inverse();
// Helper function to vectorize in correct order, which is the order in which
// we eliminated the spanning tree.
auto vec = [ordering](const VectorValues& x) { return x.vector(ordering);};
// Create zero config // Create zero config
VectorValues zeros = VectorValues::Zero(xbar); const VectorValues zeros = VectorValues::Zero(xbar);
// Set up y0 as all zeros // Set up y0 as all zeros
VectorValues y0 = zeros; const VectorValues y0 = zeros;
// y1 = perturbed y0 // y1 = perturbed y0
VectorValues y1 = zeros; VectorValues y1 = zeros;
y1[1] = Vector2(1.0, -1.0); y1[key(3,3)] = Vector2(1.0, -1.0);
// Check corresponding x values // Check backSubstituteTranspose works with R1
VectorValues expected_x1 = xtrue, x1 = system.x(y1); VectorValues actual = Rc1->backSubstituteTranspose(y1);
expected_x1[1] = Vector2(2.01, 2.99); Vector expected = R1.transpose().inverse() * vec(y1);
expected_x1[0] = Vector2(3.01, 2.99); EXPECT(assert_equal(expected, vec(actual)));
CHECK(assert_equal(xtrue, system.x(y0)));
CHECK(assert_equal(expected_x1,system.x(y1))); // Check corresponding x values
// for y = 0, we get xbar:
EXPECT(assert_equal(xbar, system.x(y0)));
// for non-zero y, answer is x = xbar + inv(R1)*y
const Vector expected_x1 = vec(xbar) + R1.inverse() * vec(y1);
const VectorValues x1 = system.x(y1);
EXPECT(assert_equal(expected_x1, vec(x1)));
// Check errors // Check errors
DOUBLES_EQUAL(0,error(Ab,xtrue),1e-9); DOUBLES_EQUAL(0,error(Ab,xbar),1e-9);
DOUBLES_EQUAL(3,error(Ab,x1),1e-9); DOUBLES_EQUAL(0,system.error(y0),1e-9);
DOUBLES_EQUAL(0,error(system,y0),1e-9); DOUBLES_EQUAL(2,error(Ab,x1),1e-9);
DOUBLES_EQUAL(3,error(system,y1),1e-9); DOUBLES_EQUAL(2,system.error(y1),1e-9);
// Test gradient in x // Check that transposeMultiplyAdd <=> y += alpha * Abar' * e
VectorValues expected_gx0 = zeros; // We check for e1 =[1;0] and e2=[0;1] corresponding to T and C
VectorValues expected_gx1 = zeros; const double alpha = 0.5;
CHECK(assert_equal(expected_gx0,gradient(Ab,xtrue))); Errors e1,e2;
expected_gx1[2] = Vector2(-100., 100.); for (size_t i=0;i<13;i++) {
expected_gx1[4] = Vector2(-100., 100.); e1 += i<9 ? Vector2(1, 1) : Vector2(0, 0);
expected_gx1[1] = Vector2(200., -200.); e2 += i>=9 ? Vector2(1, 1) : Vector2(0, 0);
expected_gx1[3] = Vector2(-100., 100.); }
expected_gx1[0] = Vector2(100., -100.); Vector ee1(13*2), ee2(13*2);
CHECK(assert_equal(expected_gx1,gradient(Ab,x1))); ee1 << Vector::Ones(9*2), Vector::Zero(4*2);
ee2 << Vector::Zero(9*2), Vector::Ones(4*2);
// Check transposeMultiplyAdd for e1
VectorValues y = zeros;
system.transposeMultiplyAdd(alpha, e1, y);
Vector expected_y = alpha * Abar.transpose() * ee1;
EXPECT(assert_equal(expected_y, vec(y)));
// Check transposeMultiplyAdd for e2
y = zeros;
system.transposeMultiplyAdd(alpha, e2, y);
expected_y = alpha * Abar.transpose() * ee2;
EXPECT(assert_equal(expected_y, vec(y)));
// Test gradient in y // Test gradient in y
VectorValues expected_gy0 = zeros; auto g = system.gradient(y0);
VectorValues expected_gy1 = zeros; Vector expected_g = Vector::Zero(18);
expected_gy1[2] = Vector2(2., -2.); EXPECT(assert_equal(expected_g, vec(g)));
expected_gy1[4] = Vector2(-2., 2.); }
expected_gy1[1] = Vector2(3., -3.);
expected_gy1[3] = Vector2(-1., 1.);
expected_gy1[0] = Vector2(1., -1.);
CHECK(assert_equal(expected_gy0,gradient(system,y0)));
CHECK(assert_equal(expected_gy1,gradient(system,y1)));
// Check it numerically for good measure /* ************************************************************************* */
// TODO use boost::bind(&SubgraphPreconditioner::error,&system,_1) // Test raw vector interface
// Vector numerical_g1 = numericalGradient<VectorValues> (error, y1, 0.001); TEST( SubgraphPreconditioner, RawVectorAPI )
// Vector expected_g1 = (Vector(18) << 0., 0., 0., 0., 2., -2., 0., 0., -2., 2., {
// 3., -3., 0., 0., -1., 1., 1., -1.); // Build a planar graph
// CHECK(assert_equal(expected_g1,numerical_g1)); GaussianFactorGraph Ab;
VectorValues xtrue;
size_t N = 3;
boost::tie(Ab, xtrue) = planarGraph(N); // A*x-b
SubgraphPreconditioner system;
// Call build, a non-const method needed to make solve work :-(
KeyInfo keyInfo(Ab);
std::map<Key,Vector> lambda;
system.build(Ab, keyInfo, lambda);
const auto ordering1 = system.Rc1()->ordering(); // build changed R1 !
const auto ordering2 = keyInfo.ordering();
const Matrix R1 = system.Rc1()->matrix(ordering1).first;
// Test that 'solve' does implement x = R^{-1} y
Vector y2 = Vector::Zero(18), x2(18), x3(18);
y2.head(2) << 100, -100;
system.solve(y2, x2);
EXPECT(assert_equal(R1.inverse() * y2, x2));
// I can't get test below to pass!
// Test that transposeSolve does implement x = R^{-T} y
// system.transposeSolve(y2, x3);
// EXPECT(assert_equal(R1.transpose().inverse() * y2, x3));
} }
/* ************************************************************************* */ /* ************************************************************************* */
@ -184,16 +239,13 @@ TEST( SubgraphPreconditioner, conjugateGradients )
size_t N = 3; size_t N = 3;
boost::tie(Ab, xtrue) = planarGraph(N); // A*x-b boost::tie(Ab, xtrue) = planarGraph(N); // A*x-b
// Get the spanning tree and corresponding ordering // Get the spanning tree
GaussianFactorGraph Ab1_, Ab2_; // A1*x-b1 and A2*x-b2 GaussianFactorGraph::shared_ptr Ab1, Ab2; // A1*x-b1 and A2*x-b2
boost::tie(Ab1_, Ab2_) = splitOffPlanarTree(N, Ab); boost::tie(Ab1, Ab2) = splitOffPlanarTree(N, Ab);
SubgraphPreconditioner::sharedFG Ab1(new GaussianFactorGraph(Ab1_));
SubgraphPreconditioner::sharedFG Ab2(new GaussianFactorGraph(Ab2_));
// Eliminate the spanning tree to build a prior // Eliminate the spanning tree to build a prior
Ordering ordering = planarOrdering(N); SubgraphPreconditioner::sharedBayesNet Rc1 = Ab1->eliminateSequential(); // R1*x-c1
SubgraphPreconditioner::sharedBayesNet Rc1 = GaussianSequentialSolver(Ab1_).eliminate(); // R1*x-c1 VectorValues xbar = Rc1->optimize(); // xbar = inv(R1)*c1
VectorValues xbar = optimize(*Rc1); // xbar = inv(R1)*c1
// Create Subgraph-preconditioned system // Create Subgraph-preconditioned system
VectorValues::shared_ptr xbarShared(new VectorValues(xbar)); // TODO: horrible VectorValues::shared_ptr xbarShared(new VectorValues(xbar)); // TODO: horrible
@ -203,22 +255,20 @@ TEST( SubgraphPreconditioner, conjugateGradients )
VectorValues y0 = VectorValues::Zero(xbar); VectorValues y0 = VectorValues::Zero(xbar);
VectorValues y1 = y0; VectorValues y1 = y0;
y1[1] = Vector2(1.0, -1.0); y1[key(2, 2)] = Vector2(1.0, -1.0);
VectorValues x1 = system.x(y1); VectorValues x1 = system.x(y1);
// Solve for the remaining constraints using PCG // Solve for the remaining constraints using PCG
ConjugateGradientParameters parameters; ConjugateGradientParameters parameters;
VectorValues actual = conjugateGradients<SubgraphPreconditioner, VectorValues actual = conjugateGradients<SubgraphPreconditioner,
VectorValues, Errors>(system, y1, parameters); VectorValues, Errors>(system, y1, parameters);
CHECK(assert_equal(y0,actual)); EXPECT(assert_equal(y0,actual));
// Compare with non preconditioned version: // Compare with non preconditioned version:
VectorValues actual2 = conjugateGradientDescent(Ab, x1, parameters); VectorValues actual2 = conjugateGradientDescent(Ab, x1, parameters);
CHECK(assert_equal(xtrue,actual2,1e-4)); EXPECT(assert_equal(xtrue,actual2,1e-4));
} }
#endif
/* ************************************************************************* */ /* ************************************************************************* */
int main() { TestResult tr; return TestRegistry::runAllTests(tr); } int main() { TestResult tr; return TestRegistry::runAllTests(tr); }
/* ************************************************************************* */ /* ************************************************************************* */