|
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/casts.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/linear_least_squares_problems.h"
|
|
Packit |
ea1746 |
#include "ceres/linear_solver.h"
|
|
Packit |
ea1746 |
#include "ceres/triplet_sparse_matrix.h"
|
|
Packit |
ea1746 |
#include "ceres/types.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 |
typedef ::testing::
|
|
Packit |
ea1746 |
tuple<LinearSolverType, DenseLinearAlgebraLibraryType, bool, int>
|
|
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 << LinearSolverTypeToString(::testing::get<0>(param)) << "_"
|
|
Packit |
ea1746 |
<< DenseLinearAlgebraLibraryTypeToString(::testing::get<1>(param)) << "_"
|
|
Packit |
ea1746 |
<< (::testing::get<2>(param) ? "Regularized" : "Unregularized") << "_"
|
|
Packit |
ea1746 |
<< ::testing::get<3>(param);
|
|
Packit |
ea1746 |
return ss.str();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class DenseLinearSolverTest : public ::testing::TestWithParam<Param> {};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_P(DenseLinearSolverTest, _) {
|
|
Packit |
ea1746 |
Param param = GetParam();
|
|
Packit |
ea1746 |
const bool regularized = testing::get<2>(param);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
scoped_ptr<LinearLeastSquaresProblem> problem(
|
|
Packit |
ea1746 |
CreateLinearLeastSquaresProblemFromId(testing::get<3>(param)));
|
|
Packit |
ea1746 |
DenseSparseMatrix lhs(*down_cast<TripletSparseMatrix*>(problem->A.get()));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int num_cols = lhs.num_cols();
|
|
Packit |
ea1746 |
const int num_rows = lhs.num_rows();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector rhs = Vector::Zero(num_rows + num_cols);
|
|
Packit |
ea1746 |
rhs.head(num_rows) = ConstVectorRef(problem->b.get(), num_rows);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::Options options;
|
|
Packit |
ea1746 |
options.type = ::testing::get<0>(param);
|
|
Packit |
ea1746 |
options.dense_linear_algebra_library_type = ::testing::get<1>(param);
|
|
Packit |
ea1746 |
scoped_ptr<LinearSolver> solver(LinearSolver::Create(options));
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LinearSolver::PerSolveOptions per_solve_options;
|
|
Packit |
ea1746 |
if (regularized) {
|
|
Packit |
ea1746 |
per_solve_options.D = problem->D.get();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector solution(num_cols);
|
|
Packit |
ea1746 |
LinearSolver::Summary summary =
|
|
Packit |
ea1746 |
solver->Solve(&lhs, rhs.data(), per_solve_options, solution.data());
|
|
Packit |
ea1746 |
EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_SUCCESS);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// If solving for the regularized solution, add the diagonal to the
|
|
Packit |
ea1746 |
// matrix. This makes subsequent computations simpler.
|
|
Packit |
ea1746 |
if (testing::get<2>(param)) {
|
|
Packit |
ea1746 |
lhs.AppendDiagonal(problem->D.get());
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector tmp = Vector::Zero(num_rows + num_cols);
|
|
Packit |
ea1746 |
lhs.RightMultiply(solution.data(), tmp.data());
|
|
Packit |
ea1746 |
Vector actual_normal_rhs = Vector::Zero(num_cols);
|
|
Packit |
ea1746 |
lhs.LeftMultiply(tmp.data(), actual_normal_rhs.data());
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Vector expected_normal_rhs = Vector::Zero(num_cols);
|
|
Packit |
ea1746 |
lhs.LeftMultiply(rhs.data(), expected_normal_rhs.data());
|
|
Packit |
ea1746 |
const double residual = (expected_normal_rhs - actual_normal_rhs).norm() /
|
|
Packit |
ea1746 |
expected_normal_rhs.norm();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
EXPECT_NEAR(residual, 0.0, 10 * std::numeric_limits<double>::epsilon());
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// TODO(sameeragarwal): Should we move away from hard coded linear
|
|
Packit |
ea1746 |
// least squares problem to randomly generated ones?
|
|
Packit |
ea1746 |
#ifndef CERES_NO_LAPACK
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
INSTANTIATE_TEST_CASE_P(
|
|
Packit |
ea1746 |
DenseLinearSolver,
|
|
Packit |
ea1746 |
DenseLinearSolverTest,
|
|
Packit |
ea1746 |
::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
|
|
Packit |
ea1746 |
::testing::Values(EIGEN, LAPACK),
|
|
Packit |
ea1746 |
::testing::Values(true, false),
|
|
Packit |
ea1746 |
::testing::Values(0, 1)),
|
|
Packit |
ea1746 |
ParamInfoToString);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#else
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
INSTANTIATE_TEST_CASE_P(
|
|
Packit |
ea1746 |
DenseLinearSolver,
|
|
Packit |
ea1746 |
DenseLinearSolverTest,
|
|
Packit |
ea1746 |
::testing::Combine(::testing::Values(DENSE_QR, DENSE_NORMAL_CHOLESKY),
|
|
Packit |
ea1746 |
::testing::Values(EIGEN),
|
|
Packit |
ea1746 |
::testing::Values(true, false),
|
|
Packit |
ea1746 |
::testing::Values(0, 1)),
|
|
Packit |
ea1746 |
ParamInfoToString);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|