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#include <cstddef> 320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_sparse_matrix.h" 330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/block_structure.h" 340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/casts.h" 350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/internal/scoped_ptr.h" 360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_least_squares_problems.h" 370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/linear_solver.h" 380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/schur_complement_solver.h" 390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/triplet_sparse_matrix.h" 400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/types.h" 410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "glog/logging.h" 420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "gtest/gtest.h" 430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres { 450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal { 460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass SchurComplementSolverTest : public ::testing::Test { 480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong protected: 490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong void SetUpFromProblemId(int problem_id) { 500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<LinearLeastSquaresProblem> problem( 510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CreateLinearLeastSquaresProblemFromId(problem_id)); 520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong CHECK_NOTNULL(problem.get()); 540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A.reset(down_cast<BlockSparseMatrix*>(problem->A.release())); 550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong b.reset(problem->b.release()); 560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong D.reset(problem->D.release()); 570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_cols = A->num_cols(); 590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_rows = A->num_rows(); 600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong num_eliminate_blocks = problem->num_eliminate_blocks; 610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong x.reset(new double[num_cols]); 630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sol.reset(new double[num_cols]); 640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sol_d.reset(new double[num_cols]); 650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Options options; 670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.type = DENSE_QR; 680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<LinearSolver> qr(LinearSolver::Create(options)); 700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 710ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong TripletSparseMatrix triplet_A(A->num_rows(), 720ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A->num_cols(), 730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A->num_nonzeros()); 740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A->ToTripletSparseMatrix(&triplet_A); 750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 760ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Gold standard solutions using dense QR factorization. 770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong DenseSparseMatrix dense_A(triplet_A); 780ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Summary summary1 = 790ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong qr->Solve(&dense_A, 800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong b.get(), 810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::PerSolveOptions(), 820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sol.get()); 830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // Gold standard solution with appended diagonal. 850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::PerSolveOptions per_solve_options; 860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong per_solve_options.D = D.get(); 870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Summary summary2 = 880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong qr->Solve(&dense_A, 890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong b.get(), 900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong per_solve_options, 910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong sol_d.get()); 920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong void ComputeAndCompareSolutions( 950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int problem_id, 960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong bool regularization, 970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ceres::LinearSolverType linear_solver_type, 980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ceres::SparseLinearAlgebraLibraryType sparse_linear_algebra_library) { 990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong SetUpFromProblemId(problem_id); 1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Options options; 1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.elimination_groups.push_back(num_eliminate_blocks); 1020ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.elimination_groups.push_back( 1030ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong A->block_structure()->cols.size() - num_eliminate_blocks); 1040ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.type = linear_solver_type; 1050ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong options.sparse_linear_algebra_library = sparse_linear_algebra_library; 1060ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1070ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<LinearSolver> solver(LinearSolver::Create(options)); 1080ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1090ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::PerSolveOptions per_solve_options; 1100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong LinearSolver::Summary summary; 1110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (regularization) { 1120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong per_solve_options.D = D.get(); 1130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong summary = solver->Solve(A.get(), b.get(), per_solve_options, x.get()); 1160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong if (regularization) { 1180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0; i < num_cols; ++i) { 1190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ASSERT_NEAR(sol_d.get()[i], x[i], 1e-10); 1200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } else { 1220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong for (int i = 0; i < num_cols; ++i) { 1230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ASSERT_NEAR(sol.get()[i], x[i], 1e-10); 1240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong } 1270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_rows; 1290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_cols; 1300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong int num_eliminate_blocks; 1310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_ptr<BlockSparseMatrix> A; 1330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> b; 1340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> x; 1350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> D; 1360ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> sol; 1370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong scoped_array<double> sol_d; 1380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}; 1390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_NO_SUITESPARSE 1410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongTEST_F(SchurComplementSolverTest, SparseSchurWithSuiteSparse) { 1420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, false, SPARSE_SCHUR, SUITE_SPARSE); 1430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, false, SPARSE_SCHUR, SUITE_SPARSE); 1440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, true, SPARSE_SCHUR, SUITE_SPARSE); 1450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, true, SPARSE_SCHUR, SUITE_SPARSE); 1460ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_NO_SUITESPARSE 1480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_NO_CXSPARSE 1500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongTEST_F(SchurComplementSolverTest, SparseSchurWithCXSparse) { 1510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, false, SPARSE_SCHUR, CX_SPARSE); 1520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, false, SPARSE_SCHUR, CX_SPARSE); 1530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, true, SPARSE_SCHUR, CX_SPARSE); 1540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, true, SPARSE_SCHUR, CX_SPARSE); 1550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif // CERES_NO_CXSPARSE 1570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongTEST_F(SchurComplementSolverTest, DenseSchur) { 1590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // The sparse linear algebra library type is ignored for 1600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong // DENSE_SCHUR. 1610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, false, DENSE_SCHUR, SUITE_SPARSE); 1620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, false, DENSE_SCHUR, SUITE_SPARSE); 1630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(2, true, DENSE_SCHUR, SUITE_SPARSE); 1640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong ComputeAndCompareSolutions(3, true, DENSE_SCHUR, SUITE_SPARSE); 1650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} 1660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong 1670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace internal 1680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong} // namespace ceres 169