|
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: keir@google.com (Keir Mierle)
|
|
Packit |
ea1746 |
// sameeragarwal@google.com (Sameer Agarwal)
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// End-to-end tests for Ceres using Powell's function.
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include <cmath>
|
|
Packit |
ea1746 |
#include <cstdlib>
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#include "ceres/autodiff_cost_function.h"
|
|
Packit |
ea1746 |
#include "ceres/problem.h"
|
|
Packit |
ea1746 |
#include "ceres/solver.h"
|
|
Packit |
ea1746 |
#include "ceres/test_util.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 |
// This class implements the SystemTestProblem interface and provides
|
|
Packit |
ea1746 |
// access to an implementation of Powell's singular function.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// F = 1/2 (f1^2 + f2^2 + f3^2 + f4^2)
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// f1 = x1 + 10*x2;
|
|
Packit |
ea1746 |
// f2 = sqrt(5) * (x3 - x4)
|
|
Packit |
ea1746 |
// f3 = (x2 - 2*x3)^2
|
|
Packit |
ea1746 |
// f4 = sqrt(10) * (x1 - x4)^2
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// The starting values are x1 = 3, x2 = -1, x3 = 0, x4 = 1.
|
|
Packit |
ea1746 |
// The minimum is 0 at (x1, x2, x3, x4) = 0.
|
|
Packit |
ea1746 |
//
|
|
Packit |
ea1746 |
// From: Testing Unconstrained Optimization Software by Jorge J. More, Burton S.
|
|
Packit |
ea1746 |
// Garbow and Kenneth E. Hillstrom in ACM Transactions on Mathematical Software,
|
|
Packit |
ea1746 |
// Vol 7(1), March 1981.
|
|
Packit |
ea1746 |
class PowellsFunction {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
PowellsFunction() {
|
|
Packit |
ea1746 |
x_[0] = 3.0;
|
|
Packit |
ea1746 |
x_[1] = -1.0;
|
|
Packit |
ea1746 |
x_[2] = 0.0;
|
|
Packit |
ea1746 |
x_[3] = 1.0;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
problem_.AddResidualBlock(
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), NULL, &x_[0], &x_[1]);
|
|
Packit |
ea1746 |
problem_.AddResidualBlock(
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), NULL, &x_[2], &x_[3]);
|
|
Packit |
ea1746 |
problem_.AddResidualBlock(
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL, &x_[1], &x_[2]);
|
|
Packit |
ea1746 |
problem_.AddResidualBlock(
|
|
Packit |
ea1746 |
new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), NULL, &x_[0], &x_[3]);
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
// Settings for the reference solution.
|
|
Packit |
ea1746 |
options_.linear_solver_type = ceres::DENSE_QR;
|
|
Packit |
ea1746 |
options_.max_num_iterations = 10;
|
|
Packit |
ea1746 |
options_.num_threads = 1;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
Problem* mutable_problem() { return &problem_; }
|
|
Packit |
ea1746 |
Solver::Options* mutable_solver_options() { return &options_; }
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
static double kResidualTolerance;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
private:
|
|
Packit |
ea1746 |
// Templated functions used for automatically differentiated cost
|
|
Packit |
ea1746 |
// functions.
|
|
Packit |
ea1746 |
class F1 {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
template <typename T> bool operator()(const T* const x1,
|
|
Packit |
ea1746 |
const T* const x2,
|
|
Packit |
ea1746 |
T* residual) const {
|
|
Packit |
ea1746 |
// f1 = x1 + 10 * x2;
|
|
Packit |
ea1746 |
*residual = *x1 + 10.0 * *x2;
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class F2 {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
template <typename T> bool operator()(const T* const x3,
|
|
Packit |
ea1746 |
const T* const x4,
|
|
Packit |
ea1746 |
T* residual) const {
|
|
Packit |
ea1746 |
// f2 = sqrt(5) (x3 - x4)
|
|
Packit |
ea1746 |
*residual = sqrt(5.0) * (*x3 - *x4);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class F3 {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
template <typename T> bool operator()(const T* const x2,
|
|
Packit |
ea1746 |
const T* const x4,
|
|
Packit |
ea1746 |
T* residual) const {
|
|
Packit |
ea1746 |
// f3 = (x2 - 2 x3)^2
|
|
Packit |
ea1746 |
residual[0] = (x2[0] - 2.0 * x4[0]) * (x2[0] - 2.0 * x4[0]);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
class F4 {
|
|
Packit |
ea1746 |
public:
|
|
Packit |
ea1746 |
template <typename T> bool operator()(const T* const x1,
|
|
Packit |
ea1746 |
const T* const x4,
|
|
Packit |
ea1746 |
T* residual) const {
|
|
Packit |
ea1746 |
// f4 = sqrt(10) (x1 - x4)^2
|
|
Packit |
ea1746 |
residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
|
|
Packit |
ea1746 |
return true;
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double x_[4];
|
|
Packit |
ea1746 |
Problem problem_;
|
|
Packit |
ea1746 |
Solver::Options options_;
|
|
Packit |
ea1746 |
};
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
double PowellsFunction::kResidualTolerance = 1e-8;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
typedef SystemTest<PowellsFunction> PowellTest;
|
|
Packit |
ea1746 |
const bool kAutomaticOrdering = true;
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(PowellTest, DenseQR) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(DENSE_QR, NO_SPARSE));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(PowellTest, DenseNormalCholesky) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(DENSE_NORMAL_CHOLESKY));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(PowellTest, DenseSchur) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(DENSE_SCHUR));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
TEST_F(PowellTest, IterativeSchurWithJacobi) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(ITERATIVE_SCHUR, NO_SPARSE, kAutomaticOrdering, JACOBI));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_NO_SUITESPARSE
|
|
Packit |
ea1746 |
TEST_F(PowellTest, SparseNormalCholeskyUsingSuiteSparse) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(SPARSE_NORMAL_CHOLESKY, SUITE_SPARSE, kAutomaticOrdering));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
#endif // CERES_NO_SUITESPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifndef CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
TEST_F(PowellTest, SparseNormalCholeskyUsingCXSparse) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(SPARSE_NORMAL_CHOLESKY, CX_SPARSE, kAutomaticOrdering));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
#endif // CERES_NO_CXSPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
#ifdef CERES_USE_EIGEN_SPARSE
|
|
Packit |
ea1746 |
TEST_F(PowellTest, SparseNormalCholeskyUsingEigenSparse) {
|
|
Packit |
ea1746 |
RunSolverForConfigAndExpectResidualsMatch(
|
|
Packit |
ea1746 |
SolverConfig(SPARSE_NORMAL_CHOLESKY, EIGEN_SPARSE, kAutomaticOrdering));
|
|
Packit |
ea1746 |
}
|
|
Packit |
ea1746 |
#endif // CERES_USE_EIGEN_SPARSE
|
|
Packit |
ea1746 |
|
|
Packit |
ea1746 |
} // namespace internal
|
|
Packit |
ea1746 |
} // namespace ceres
|