|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2017 Google Inc. All rights reserved.
|
|
Packit |
ea1746 |
// http://ceres-solver.org/
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Redistribution and use in source and binary forms, with or without
|
|
Packit |
ea1746 |
// modification, are permitted provided that the following conditions are met:
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// * Redistributions of source code must retain the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer.
|
|
Packit |
ea1746 |
// * Redistributions in binary form must reproduce the above copyright notice,
|
|
Packit |
ea1746 |
// this list of conditions and the following disclaimer in the documentation
|
|
Packit |
ea1746 |
// and/or other materials provided with the distribution.
|
|
Packit |
ea1746 |
// * Neither the name of Google Inc. nor the names of its contributors may be
|
|
Packit |
ea1746 |
// used to endorse or promote products derived from this software without
|
|
Packit |
ea1746 |
// specific prior written permission.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
|
|
Packit |
ea1746 |
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
Packit |
ea1746 |
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
Packit |
ea1746 |
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
|
|
Packit |
ea1746 |
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
Packit |
ea1746 |
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
|
|
Packit |
ea1746 |
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
|
|
Packit |
ea1746 |
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
|
|
Packit |
ea1746 |
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
Packit |
ea1746 |
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
|
Packit |
ea1746 |
// POSSIBILITY OF SUCH DAMAGE.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// Author: sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/sparse_cholesky.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <numeric>
|
|
Packit |
ea1746 |
#include <vector>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "Eigen/Dense"
|
|
Packit |
ea1746 |
#include "Eigen/SparseCore"
|
|
Packit |
ea1746 |
#include "ceres/block_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/compressed_row_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/inner_product_computer.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/random.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
BlockSparseMatrix* CreateRandomFullRankMatrix(const int num_col_blocks,
|
|
Packit |
ea1746 |
const int min_col_block_size,
|
|
Packit |
ea1746 |
const int max_col_block_size,
|
|
Packit |
ea1746 |
const double block_density) {
|
|
Packit |
ea1746 |
// Create a random matrix
|
|
Packit |
ea1746 |
BlockSparseMatrix::RandomMatrixOptions options;
|
|
Packit |
ea1746 |
options.num_col_blocks = num_col_blocks;
|
|
Packit |
ea1746 |
options.min_col_block_size = min_col_block_size;
|
|
Packit |
ea1746 |
options.max_col_block_size = max_col_block_size;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
options.num_row_blocks = 2 * num_col_blocks;
|
|
Packit |
ea1746 |
options.min_row_block_size = 1;
|
|
Packit |
ea1746 |
options.max_row_block_size = max_col_block_size;
|
|
Packit |
ea1746 |
options.block_density = block_density;
|
|
Packit |
ea1746 |
scoped_ptr<BlockSparseMatrix> random_matrix(
|
|
Packit |
ea1746 |
BlockSparseMatrix::CreateRandomMatrix(options));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Add a diagonal block sparse matrix to make it full rank.
|
|
Packit |
ea1746 |
Vector diagonal = Vector::Ones(random_matrix->num_cols());
|
|
Packit |
ea1746 |
scoped_ptr<BlockSparseMatrix> block_diagonal(
|
|
Packit |
ea1746 |
BlockSparseMatrix::CreateDiagonalMatrix(
|
|
Packit |
ea1746 |
diagonal.data(), random_matrix->block_structure()->cols));
|
|
Packit |
ea1746 |
random_matrix->AppendRows(*block_diagonal);
|
|
Packit |
ea1746 |
return random_matrix.release();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
bool ComputeExpectedSolution(const CompressedRowSparseMatrix& lhs,
|
|
Packit |
ea1746 |
const Vector& rhs,
|
|
Packit |
ea1746 |
Vector* solution) {
|
|
Packit |
ea1746 |
Matrix eigen_lhs;
|
|
Packit |
ea1746 |
lhs.ToDenseMatrix(&eigen_lhs);
|
|
Packit |
ea1746 |
if (lhs.storage_type() == CompressedRowSparseMatrix::UPPER_TRIANGULAR) {
|
|
Packit |
ea1746 |
Matrix full_lhs = eigen_lhs.selfadjointView<Eigen::Upper>();
|
|
Packit |
ea1746 |
Eigen::LLT<Matrix, Eigen::Upper> llt =
|
|
Packit |
ea1746 |
eigen_lhs.selfadjointView<Eigen::Upper>().llt();
|
|
Packit |
ea1746 |
if (llt.info() != Eigen::Success) {
|
|
Packit |
ea1746 |
return false;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
*solution = llt.solve(rhs);
|
|
Packit |
ea1746 |
return (llt.info() == Eigen::Success);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Matrix full_lhs = eigen_lhs.selfadjointView<Eigen::Lower>();
|
|
Packit |
ea1746 |
Eigen::LLT<Matrix, Eigen::Lower> llt =
|
|
Packit |
ea1746 |
eigen_lhs.selfadjointView<Eigen::Lower>().llt();
|
|
Packit |
ea1746 |
if (llt.info() != Eigen::Success) {
|
|
Packit |
ea1746 |
return false;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
*solution = llt.solve(rhs);
|
|
Packit |
ea1746 |
return (llt.info() == Eigen::Success);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
void SparseCholeskySolverUnitTest(
|
|
Packit |
ea1746 |
const SparseLinearAlgebraLibraryType sparse_linear_algebra_library_type,
|
|
Packit |
ea1746 |
const OrderingType ordering_type,
|
|
Packit |
ea1746 |
const bool use_block_structure,
|
|
Packit |
ea1746 |
const int num_blocks,
|
|
Packit |
ea1746 |
const int min_block_size,
|
|
Packit |
ea1746 |
const int max_block_size,
|
|
Packit |
ea1746 |
const double block_density) {
|
|
Packit |
ea1746 |
scoped_ptr<SparseCholesky> sparse_cholesky(SparseCholesky::Create(
|
|
Packit |
ea1746 |
sparse_linear_algebra_library_type, ordering_type));
|
|
Packit |
ea1746 |
const CompressedRowSparseMatrix::StorageType storage_type =
|
|
Packit |
ea1746 |
sparse_cholesky->StorageType();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<BlockSparseMatrix> m(CreateRandomFullRankMatrix(
|
|
Packit |
ea1746 |
num_blocks, min_block_size, max_block_size, block_density));
|
|
Packit |
ea1746 |
scoped_ptr<InnerProductComputer> inner_product_computer(
|
|
Packit |
ea1746 |
InnerProductComputer::Create(*m, storage_type));
|
|
Packit |
ea1746 |
inner_product_computer->Compute();
|
|
Packit |
ea1746 |
CompressedRowSparseMatrix* lhs = inner_product_computer->mutable_result();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
if (!use_block_structure) {
|
|
Packit |
ea1746 |
lhs->mutable_row_blocks()->clear();
|
|
Packit |
ea1746 |
lhs->mutable_col_blocks()->clear();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector rhs = Vector::Random(lhs->num_rows());
|
|
Packit |
ea1746 |
Vector expected(lhs->num_rows());
|
|
Packit |
ea1746 |
Vector actual(lhs->num_rows());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EXPECT_TRUE(ComputeExpectedSolution(*lhs, rhs, &expected));
|
|
Packit |
ea1746 |
std::string message;
|
|
Packit |
ea1746 |
EXPECT_EQ(sparse_cholesky->FactorAndSolve(
|
|
Packit |
ea1746 |
lhs, rhs.data(), actual.data(), &message),
|
|
Packit |
ea1746 |
LINEAR_SOLVER_SUCCESS);
|
|
Packit |
ea1746 |
Matrix eigen_lhs;
|
|
Packit |
ea1746 |
lhs->ToDenseMatrix(&eigen_lhs);
|
|
Packit |
ea1746 |
EXPECT_NEAR((actual - expected).norm() / actual.norm(),
|
|
Packit |
ea1746 |
0.0,
|
|
Packit |
ea1746 |
std::numeric_limits<double>::epsilon() * 10)
|
|
Packit |
ea1746 |
<< "\n"
|
|
Packit |
ea1746 |
<< eigen_lhs;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
typedef ::testing::tuple<SparseLinearAlgebraLibraryType, OrderingType, bool>
|
|
Packit |
ea1746 |
Param;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
std::string ParamInfoToString(testing::TestParamInfo<Param> info) {
|
|
Packit |
ea1746 |
Param param = info.param;
|
|
Packit |
ea1746 |
std::stringstream ss;
|
|
Packit |
ea1746 |
ss << SparseLinearAlgebraLibraryTypeToString(::testing::get<0>(param)) << "_"
|
|
Packit |
ea1746 |
<< (::testing::get<1>(param) == AMD ? "AMD" : "NATURAL") << "_"
|
|
Packit |
ea1746 |
<< (::testing::get<2>(param) ? "UseBlockStructure" : "NoBlockStructure");
|
|
Packit |
ea1746 |
return ss.str();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class SparseCholeskyTest : public ::testing::TestWithParam<Param> {};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_P(SparseCholeskyTest, FactorAndSolve) {
|
|
Packit |
ea1746 |
SetRandomState(2982);
|
|
Packit |
ea1746 |
const int kMinNumBlocks = 1;
|
|
Packit |
ea1746 |
const int kMaxNumBlocks = 10;
|
|
Packit |
ea1746 |
const int kNumTrials = 10;
|
|
Packit |
ea1746 |
const int kMinBlockSize = 1;
|
|
Packit |
ea1746 |
const int kMaxBlockSize = 5;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
for (int num_blocks = kMinNumBlocks; num_blocks < kMaxNumBlocks;
|
|
Packit |
ea1746 |
++num_blocks) {
|
|
Packit |
ea1746 |
for (int trial = 0; trial < kNumTrials; ++trial) {
|
|
Packit |
ea1746 |
const double block_density = std::max(0.1, RandDouble());
|
|
Packit |
ea1746 |
Param param = GetParam();
|
|
Packit |
ea1746 |
SparseCholeskySolverUnitTest(::testing::get<0>(param),
|
|
Packit |
ea1746 |
::testing::get<1>(param),
|
|
Packit |
ea1746 |
::testing::get<2>(param),
|
|
Packit |
ea1746 |
num_blocks,
|
|
Packit |
ea1746 |
kMinBlockSize,
|
|
Packit |
ea1746 |
kMaxBlockSize,
|
|
Packit |
ea1746 |
block_density);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_NO_SUITESPARSE
|
|
Packit |
ea1746 |
INSTANTIATE_TEST_CASE_P(SuiteSparseCholesky,
|
|
Packit |
ea1746 |
SparseCholeskyTest,
|
|
Packit |
ea1746 |
::testing::Combine(::testing::Values(SUITE_SPARSE),
|
|
Packit |
ea1746 |
::testing::Values(AMD, NATURAL),
|
|
Packit |
ea1746 |
::testing::Values(true, false)),
|
|
Packit |
ea1746 |
ParamInfoToString);
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
INSTANTIATE_TEST_CASE_P(CXSparseCholesky,
|
|
Packit |
ea1746 |
SparseCholeskyTest,
|
|
Packit |
ea1746 |
::testing::Combine(::testing::Values(CX_SPARSE),
|
|
Packit |
ea1746 |
::testing::Values(AMD, NATURAL),
|
|
Packit |
ea1746 |
::testing::Values(true, false)),
|
|
Packit |
ea1746 |
ParamInfoToString);
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifdef CERES_USE_EIGEN_SPARSE
|
|
Packit |
ea1746 |
INSTANTIATE_TEST_CASE_P(EigenSparseCholesky,
|
|
Packit |
ea1746 |
SparseCholeskyTest,
|
|
Packit |
ea1746 |
::testing::Combine(::testing::Values(EIGEN_SPARSE),
|
|
Packit |
ea1746 |
::testing::Values(AMD, NATURAL),
|
|
Packit |
ea1746 |
::testing::Values(true, false)),
|
|
Packit |
ea1746 |
ParamInfoToString);
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|