10ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Ceres Solver - A fast non-linear least squares minimizer
20ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Copyright 2012 Google Inc. All rights reserved.
30ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// http://code.google.com/p/ceres-solver/
40ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
50ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Redistribution and use in source and binary forms, with or without
60ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// modification, are permitted provided that the following conditions are met:
70ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
80ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions of source code must retain the above copyright notice,
90ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer.
100ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Redistributions in binary form must reproduce the above copyright notice,
110ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   this list of conditions and the following disclaimer in the documentation
120ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   and/or other materials provided with the distribution.
130ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// * Neither the name of Google Inc. nor the names of its contributors may be
140ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   used to endorse or promote products derived from this software without
150ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//   specific prior written permission.
160ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
170ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
180ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
190ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
200ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
210ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
220ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
230ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
240ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
250ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
260ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
270ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// POSSIBILITY OF SUCH DAMAGE.
280ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
290ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Author: keir@google.com (Keir Mierle)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/cgnr_solver.h"
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
331d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/block_jacobi_preconditioner.h"
340ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/cgnr_linear_operator.h"
350ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/conjugate_gradients_solver.h"
3679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez#include "ceres/internal/eigen.h"
371d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/linear_solver.h"
381d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "ceres/wall_time.h"
391d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include "glog/logging.h"
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus KongCgnrSolver::CgnrSolver(const LinearSolver::Options& options)
450ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  : options_(options),
461d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    preconditioner_(NULL) {
4779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  if (options_.preconditioner_type != JACOBI &&
4879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez      options_.preconditioner_type != IDENTITY) {
4979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez    LOG(FATAL) << "CGNR only supports IDENTITY and JACOBI preconditioners.";
5079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  }
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
531d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha HaeberlingLinearSolver::Summary CgnrSolver::SolveImpl(
541d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    BlockSparseMatrix* A,
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const double* b,
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    const LinearSolver::PerSolveOptions& per_solve_options,
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    double* x) {
581d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  EventLogger event_logger("CgnrSolver::Solve");
591d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Form z = Atb.
6179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  Vector z(A->num_cols());
6279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  z.setZero();
6379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  A->LeftMultiply(b, z.data());
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Precondition if necessary.
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  LinearSolver::PerSolveOptions cg_per_solve_options = per_solve_options;
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  if (options_.preconditioner_type == JACOBI) {
681d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    if (preconditioner_.get() == NULL) {
691d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling      preconditioner_.reset(new BlockJacobiPreconditioner(*A));
700ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong    }
711d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    preconditioner_->Update(*A, per_solve_options.D);
721d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling    cg_per_solve_options.preconditioner = preconditioner_.get();
730ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  }
740ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
750ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Solve (AtA + DtD)x = z (= Atb).
7679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  VectorRef(x, A->num_cols()).setZero();
770ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  CgnrLinearOperator lhs(*A, per_solve_options.D);
781d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  event_logger.AddEvent("Setup");
791d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
800ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  ConjugateGradientsSolver conjugate_gradient_solver(options_);
811d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  LinearSolver::Summary summary =
8279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez      conjugate_gradient_solver.Solve(&lhs, z.data(), cg_per_solve_options, x);
831d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  event_logger.AddEvent("Solve");
841d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling  return summary;
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
89