Move some routines to solver_utils.h/cc This moves a couple of routines from solver.cc into solver_utils.h/cc so that they can also be used by the upcoming GradientProblemSolver. Change-Id: I627b32ad3dc639422aacde78a8e391459d947e99
diff --git a/internal/ceres/CMakeLists.txt b/internal/ceres/CMakeLists.txt index ec9dee3..9c49503 100644 --- a/internal/ceres/CMakeLists.txt +++ b/internal/ceres/CMakeLists.txt
@@ -98,6 +98,7 @@ scratch_evaluate_preparer.cc single_linkage_clustering.cc solver.cc + solver_utils.cc sparse_matrix.cc sparse_normal_cholesky_solver.cc split.cc
diff --git a/internal/ceres/solver.cc b/internal/ceres/solver.cc index 2c5be80..209cfe3 100644 --- a/internal/ceres/solver.cc +++ b/internal/ceres/solver.cc
@@ -34,16 +34,16 @@ #include <algorithm> #include <sstream> // NOLINT #include <vector> +#include "ceres/gradient_checking_cost_function.h" #include "ceres/internal/port.h" #include "ceres/parameter_block_ordering.h" #include "ceres/preprocessor.h" -#include "ceres/gradient_checking_cost_function.h" #include "ceres/problem.h" #include "ceres/problem_impl.h" #include "ceres/program.h" +#include "ceres/solver_utils.h" #include "ceres/stringprintf.h" #include "ceres/types.h" -#include "ceres/version.h" #include "ceres/wall_time.h" namespace ceres { @@ -311,16 +311,6 @@ internal::StringAppendF(report, "%d", ordering.back()); } -void SetSummaryFinalCost(Solver::Summary* summary) { - summary->final_cost = summary->initial_cost; - // We need the loop here, instead of just looking at the last - // iteration because the minimizer maybe making non-monotonic steps. - for (int i = 0; i < summary->iterations.size(); ++i) { - const IterationSummary& iteration_summary = summary->iterations[i]; - summary->final_cost = min(iteration_summary.cost, summary->final_cost); - } -} - void SummarizeGivenProgram(const internal::Program& program, Solver::Summary* summary) { summary->num_parameter_blocks = program.NumParameterBlocks(); @@ -357,6 +347,7 @@ summary->line_search_type = options.line_search_type; summary->linear_solver_type_given = options.linear_solver_type; summary->max_lbfgs_rank = options.max_lbfgs_rank; + summary->minimizer_type = options.minimizer_type; summary->nonlinear_conjugate_gradient_type = options.nonlinear_conjugate_gradient_type; // NOLINT summary->num_linear_solver_threads_given = options.num_linear_solver_threads; // NOLINT @@ -380,7 +371,7 @@ summary->num_threads_used = pp.options.num_threads; summary->preconditioner_type_used = pp.options.preconditioner_type; // NOLINT - SetSummaryFinalCost(summary); + internal::SetSummaryFinalCost(summary); if (pp.reduced_program.get() != NULL) { SummarizeReducedProgram(*pp.reduced_program, summary); @@ -441,44 +432,6 @@ } } -string VersionString() { - string value = string(CERES_VERSION_STRING); - -#ifdef CERES_NO_LAPACK - value += "-no_lapack"; -#else - value += "-lapack"; -#endif - -#ifndef CERES_NO_SUITESPARSE - value += "-suitesparse"; -#endif - -#ifndef CERES_NO_CXSPARSE - value += "-cxsparse"; -#endif - -#ifdef CERES_USE_EIGEN_SPARSE - value += "-eigensparse"; -#endif - -#ifdef CERES_RESTRUCT_SCHUR_SPECIALIZATIONS - value += "-no_schur_specializations"; -#endif - -#ifdef CERES_USE_OPENMP - value += "-openmp"; -#else - value += "-no_openmp"; -#endif - -#ifdef CERES_NO_CUSTOM_BLAS - value += "-no_custom_blas"; -#endif - - return value; -} - } // namespace bool Solver::Options::IsValid(string* error) const { @@ -640,6 +593,8 @@ }; string Solver::Summary::FullReport() const { + using internal::VersionString; + string report = string("\nSolver Summary (v " + VersionString() + ")\n\n"); StringAppendF(&report, "%45s %21s\n", "Original", "Reduced"); @@ -840,9 +795,7 @@ }; bool Solver::Summary::IsSolutionUsable() const { - return (termination_type == CONVERGENCE || - termination_type == NO_CONVERGENCE || - termination_type == USER_SUCCESS); + return internal::IsSolutionUsable(*this); } } // namespace ceres
diff --git a/internal/ceres/solver_utils.cc b/internal/ceres/solver_utils.cc new file mode 100644 index 0000000..bd304e7 --- /dev/null +++ b/internal/ceres/solver_utils.cc
@@ -0,0 +1,78 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/ceres-solver/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: sameeragarwal@google.com (Sameer Agarwal) + +#include <string> + +#include "ceres/internal/port.h" +#include "ceres/version.h" + +namespace ceres { +namespace internal { + +string VersionString() { + string value = string(CERES_VERSION_STRING); + +#ifdef CERES_NO_LAPACK + value += "-no_lapack"; +#else + value += "-lapack"; +#endif + +#ifndef CERES_NO_SUITESPARSE + value += "-suitesparse"; +#endif + +#ifndef CERES_NO_CXSPARSE + value += "-cxsparse"; +#endif + +#ifdef CERES_USE_EIGEN_SPARSE + value += "-eigensparse"; +#endif + +#ifdef CERES_RESTRUCT_SCHUR_SPECIALIZATIONS + value += "-no_schur_specializations"; +#endif + +#ifdef CERES_USE_OPENMP + value += "-openmp"; +#else + value += "-no_openmp"; +#endif + +#ifdef CERES_NO_CUSTOM_BLAS + value += "-no_custom_blas"; +#endif + + return value; +} + +} // namespace internal +} // namespace ceres
diff --git a/internal/ceres/solver_utils.h b/internal/ceres/solver_utils.h new file mode 100644 index 0000000..41c8a6e --- /dev/null +++ b/internal/ceres/solver_utils.h
@@ -0,0 +1,58 @@ +// Ceres Solver - A fast non-linear least squares minimizer +// Copyright 2014 Google Inc. All rights reserved. +// http://code.google.com/p/ceres-solver/ +// +// Redistribution and use in source and binary forms, with or without +// modification, are permitted provided that the following conditions are met: +// +// * Redistributions of source code must retain the above copyright notice, +// this list of conditions and the following disclaimer. +// * Redistributions in binary form must reproduce the above copyright notice, +// this list of conditions and the following disclaimer in the documentation +// and/or other materials provided with the distribution. +// * Neither the name of Google Inc. nor the names of its contributors may be +// used to endorse or promote products derived from this software without +// specific prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE +// POSSIBILITY OF SUCH DAMAGE. +// +// Author: sameeragarwal@google.com (Sameer Agarwal) + +#include <algorithm> +#include <string> + +namespace ceres { +namespace internal { + +template <typename SummaryType> +bool IsSolutionUsable(const SummaryType& summary) { + return (summary.termination_type == CONVERGENCE || + summary.termination_type == NO_CONVERGENCE || + summary.termination_type == USER_SUCCESS); +} + +template <typename SummaryType> +void SetSummaryFinalCost(SummaryType* summary) { + summary->final_cost = summary->initial_cost; + // We need the loop here, instead of just looking at the last + // iteration because the minimizer maybe making non-monotonic steps. + for (int i = 0; i < summary->iterations.size(); ++i) { + const IterationSummary& iteration_summary = summary->iterations[i]; + summary->final_cost = min(iteration_summary.cost, summary->final_cost); + } +} + +string VersionString(); + +} // namespace internal +} // namespace ceres