|
Packit |
ea1746 |
// Ceres Solver - A fast non-linear least squares minimizer
|
|
Packit |
ea1746 |
// Copyright 2015 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/internal/eigen.h"
|
|
Packit |
ea1746 |
#include "ceres/internal/scoped_ptr.h"
|
|
Packit |
ea1746 |
#include "ceres/levenberg_marquardt_strategy.h"
|
|
Packit |
ea1746 |
#include "ceres/linear_solver.h"
|
|
Packit |
ea1746 |
#include "ceres/trust_region_strategy.h"
|
|
Packit |
ea1746 |
#include "glog/logging.h"
|
|
Packit |
ea1746 |
#include "gmock/gmock.h"
|
|
Packit |
ea1746 |
#include "gmock/mock-log.h"
|
|
Packit |
ea1746 |
#include "gtest/gtest.h"
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
using testing::AllOf;
|
|
Packit |
ea1746 |
using testing::AnyNumber;
|
|
Packit |
ea1746 |
using testing::HasSubstr;
|
|
Packit |
ea1746 |
using testing::ScopedMockLog;
|
|
Packit |
ea1746 |
using testing::_;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
namespace ceres {
|
|
Packit |
ea1746 |
namespace internal {
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const double kTolerance = 1e-16;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Linear solver that takes as input a vector and checks that the
|
|
Packit |
ea1746 |
// caller passes the same vector as LinearSolver::PerSolveOptions.D.
|
|
Packit |
ea1746 |
class RegularizationCheckingLinearSolver : public DenseSparseMatrixSolver {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
RegularizationCheckingLinearSolver(const int num_cols, const double* diagonal)
|
|
Packit |
ea1746 |
: num_cols_(num_cols),
|
|
Packit |
ea1746 |
diagonal_(diagonal) {
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
virtual ~RegularizationCheckingLinearSolver() {}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
virtual LinearSolver::Summary SolveImpl(
|
|
Packit |
ea1746 |
DenseSparseMatrix* A,
|
|
Packit |
ea1746 |
const double* b,
|
|
Packit |
ea1746 |
const LinearSolver::PerSolveOptions& per_solve_options,
|
|
Packit |
ea1746 |
double* x) {
|
|
Packit |
ea1746 |
CHECK_NOTNULL(per_solve_options.D);
|
|
Packit |
ea1746 |
for (int i = 0; i < num_cols_; ++i) {
|
|
Packit |
ea1746 |
EXPECT_NEAR(per_solve_options.D[i], diagonal_[i], kTolerance)
|
|
Packit |
ea1746 |
<< i << " " << per_solve_options.D[i] << " " << diagonal_[i];
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
return LinearSolver::Summary();
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
const int num_cols_;
|
|
Packit |
ea1746 |
const double* diagonal_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(LevenbergMarquardtStrategy, AcceptRejectStepRadiusScaling) {
|
|
Packit |
ea1746 |
TrustRegionStrategy::Options options;
|
|
Packit |
ea1746 |
options.initial_radius = 2.0;
|
|
Packit |
ea1746 |
options.max_radius = 20.0;
|
|
Packit |
ea1746 |
options.min_lm_diagonal = 1e-8;
|
|
Packit |
ea1746 |
options.max_lm_diagonal = 1e8;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// We need a non-null pointer here, so anything should do.
|
|
Packit |
ea1746 |
scoped_ptr<LinearSolver> linear_solver(
|
|
Packit |
ea1746 |
new RegularizationCheckingLinearSolver(0, NULL));
|
|
Packit |
ea1746 |
options.linear_solver = linear_solver.get();
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LevenbergMarquardtStrategy lms(options);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), options.initial_radius);
|
|
Packit |
ea1746 |
lms.StepRejected(0.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 1.0);
|
|
Packit |
ea1746 |
lms.StepRejected(-1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25);
|
|
Packit |
ea1746 |
lms.StepAccepted(1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25 * 3.0);
|
|
Packit |
ea1746 |
lms.StepAccepted(1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0);
|
|
Packit |
ea1746 |
lms.StepAccepted(0.25);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125);
|
|
Packit |
ea1746 |
lms.StepAccepted(1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0);
|
|
Packit |
ea1746 |
lms.StepAccepted(1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), 0.25 * 3.0 * 3.0 / 1.125 * 3.0 * 3.0);
|
|
Packit |
ea1746 |
lms.StepAccepted(1.0);
|
|
Packit |
ea1746 |
EXPECT_EQ(lms.Radius(), options.max_radius);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST(LevenbergMarquardtStrategy, CorrectDiagonalToLinearSolver) {
|
|
Packit |
ea1746 |
Matrix jacobian(2, 3);
|
|
Packit |
ea1746 |
jacobian.setZero();
|
|
Packit |
ea1746 |
jacobian(0, 0) = 0.0;
|
|
Packit |
ea1746 |
jacobian(0, 1) = 1.0;
|
|
Packit |
ea1746 |
jacobian(1, 1) = 1.0;
|
|
Packit |
ea1746 |
jacobian(0, 2) = 100.0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double residual = 1.0;
|
|
Packit |
ea1746 |
double x[3];
|
|
Packit |
ea1746 |
DenseSparseMatrix dsm(jacobian);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TrustRegionStrategy::Options options;
|
|
Packit |
ea1746 |
options.initial_radius = 2.0;
|
|
Packit |
ea1746 |
options.max_radius = 20.0;
|
|
Packit |
ea1746 |
options.min_lm_diagonal = 1e-2;
|
|
Packit |
ea1746 |
options.max_lm_diagonal = 1e2;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double diagonal[3];
|
|
Packit |
ea1746 |
diagonal[0] = options.min_lm_diagonal;
|
|
Packit |
ea1746 |
diagonal[1] = 2.0;
|
|
Packit |
ea1746 |
diagonal[2] = options.max_lm_diagonal;
|
|
Packit |
ea1746 |
for (int i = 0; i < 3; ++i) {
|
|
Packit |
ea1746 |
diagonal[i] = sqrt(diagonal[i] / options.initial_radius);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
RegularizationCheckingLinearSolver linear_solver(3, diagonal);
|
|
Packit |
ea1746 |
options.linear_solver = &linear_solver;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
LevenbergMarquardtStrategy lms(options);
|
|
Packit |
ea1746 |
TrustRegionStrategy::PerSolveOptions pso;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
{
|
|
Packit |
ea1746 |
ScopedMockLog log;
|
|
Packit |
ea1746 |
EXPECT_CALL(log, Log(_, _, _)).Times(AnyNumber());
|
|
Packit |
ea1746 |
// This using directive is needed get around the fact that there
|
|
Packit |
ea1746 |
// are versions of glog which are not in the google namespace.
|
|
Packit |
ea1746 |
using namespace google;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#if defined(_MSC_VER)
|
|
Packit |
ea1746 |
// Use GLOG_WARNING to support MSVC if GLOG_NO_ABBREVIATED_SEVERITIES
|
|
Packit |
ea1746 |
// is defined.
|
|
Packit |
ea1746 |
EXPECT_CALL(log, Log(GLOG_WARNING, _,
|
|
Packit |
ea1746 |
HasSubstr("Failed to compute a step")));
|
|
Packit |
ea1746 |
#else
|
|
Packit |
ea1746 |
EXPECT_CALL(log, Log(google::WARNING, _,
|
|
Packit |
ea1746 |
HasSubstr("Failed to compute a step")));
|
|
Packit |
ea1746 |
#endif
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TrustRegionStrategy::Summary summary =
|
|
Packit |
ea1746 |
lms.ComputeStep(pso, &dsm, &residual, x);
|
|
Packit |
ea1746 |
EXPECT_EQ(summary.termination_type, LINEAR_SOLVER_FAILURE);
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|