10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer 20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2010, 2011, 2012 Google Inc. All rights reserved. 30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/ 40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without 60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met: 70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice, 90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer. 100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice, 110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// this list of conditions and the following disclaimer in the documentation 120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// and/or other materials provided with the distribution. 130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be 140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// used to endorse or promote products derived from this software without 150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// specific prior written permission. 160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE 210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE. 280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: sameeragarwal@google.com (Sameer Agarwal) 300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// 310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// TODO(sameeragarwal): Add support for larger, more complicated and 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// poorly conditioned problems both for correctness testing as well as 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// benchmarking. 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/iterative_schur_complement_solver.h" 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include <cstddef> 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "Eigen/Dense" 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_random_access_dense_matrix.h" 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_sparse_matrix.h" 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/casts.h" 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/eigen.h" 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/scoped_ptr.h" 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_least_squares_problems.h" 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_solver.h" 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/schur_eliminator.h" 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/triplet_sparse_matrix.h" 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h" 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "glog/logging.h" 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "gtest/gtest.h" 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongusing testing::AssertionResult; 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongconst double kEpsilon = 1e-14; 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass IterativeSchurComplementSolverTest : public ::testing::Test { 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong protected : 61399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger void SetUpProblem(int problem_id) { 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<LinearLeastSquaresProblem> problem( 63399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger CreateLinearLeastSquaresProblemFromId(problem_id)); 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_NOTNULL(problem.get()); 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A_.reset(down_cast<BlockSparseMatrix*>(problem->A.release())); 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong b_.reset(problem->b.release()); 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong D_.reset(problem->D.release()); 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_cols_ = A_->num_cols(); 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_rows_ = A_->num_rows(); 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_eliminate_blocks_ = problem->num_eliminate_blocks; 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong AssertionResult TestSolver(double* D) { 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong TripletSparseMatrix triplet_A(A_->num_rows(), 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A_->num_cols(), 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A_->num_nonzeros()); 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A_->ToTripletSparseMatrix(&triplet_A); 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong DenseSparseMatrix dense_A(triplet_A); 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Options options; 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.type = DENSE_QR; 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<LinearSolver> qr(LinearSolver::Create(options)); 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::PerSolveOptions per_solve_options; 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong per_solve_options.D = D; 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Vector reference_solution(num_cols_); 900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong qr->Solve(&dense_A, b_.get(), per_solve_options, reference_solution.data()); 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.elimination_groups.push_back(num_eliminate_blocks_); 93399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger options.elimination_groups.push_back(0); 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.max_num_iterations = num_cols_; 95399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger options.preconditioner_type = SCHUR_JACOBI; 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong IterativeSchurComplementSolver isc(options); 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong Vector isc_sol(num_cols_); 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong per_solve_options.r_tolerance = 1e-12; 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong isc.Solve(A_.get(), b_.get(), per_solve_options, isc_sol.data()); 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong double diff = (isc_sol - reference_solution).norm(); 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (diff < kEpsilon) { 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return testing::AssertionSuccess(); 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } else { 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong return testing::AssertionFailure() 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << "The reference solution differs from the ITERATIVE_SCHUR" 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong << " solution by " << diff << " which is more than " << kEpsilon; 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_rows_; 1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_cols_; 1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_eliminate_blocks_; 1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<BlockSparseMatrix> A_; 1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> b_; 1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> D_; 1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 119399f7d09e0c45af54b77b4ab9508d6f23759b927Scott EttingerTEST_F(IterativeSchurComplementSolverTest, NormalProblem) { 120399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger SetUpProblem(2); 121399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger EXPECT_TRUE(TestSolver(NULL)); 122399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger EXPECT_TRUE(TestSolver(D_.get())); 123399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger} 124399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger 125399f7d09e0c45af54b77b4ab9508d6f23759b927Scott EttingerTEST_F(IterativeSchurComplementSolverTest, ProblemWithNoFBlocks) { 126399f7d09e0c45af54b77b4ab9508d6f23759b927Scott Ettinger SetUpProblem(3); 1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong EXPECT_TRUE(TestSolver(NULL)); 1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong EXPECT_TRUE(TestSolver(D_.get())); 1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 133