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: sameeragarwal@google.com (Sameer Agarwal)
300ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
310ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#ifndef CERES_INTERNAL_COORDINATE_DESCENT_MINIMIZER_H_
320ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#define CERES_INTERNAL_COORDINATE_DESCENT_MINIMIZER_H_
330ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
341d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include <string>
351d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling#include <vector>
361d2624a10e2c559f8ba9ef89eaa30832c0a83a96Sascha Haeberling
370ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/evaluator.h"
380ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/minimizer.h"
390ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/problem_impl.h"
400ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#include "ceres/solver.h"
410ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
420ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace ceres {
430ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongnamespace internal {
440ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
4579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandezclass Program;
4679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
470ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// Given a Program, and a ParameterBlockOrdering which partitions
480ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// (non-exhaustively) the Hessian matrix into independent sets,
490ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// perform coordinate descent on the parameter blocks in the
500ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// ordering. The independent set structure allows for all parameter
510ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// blocks in the same independent set to be optimized in parallel, and
520ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// the order of the independent set determines the order in which the
530ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// parameter block groups are optimized.
540ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong//
550ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// The minimizer assumes that none of the parameter blocks in the
560ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong// program are constant.
570ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kongclass CoordinateDescentMinimizer : public Minimizer {
580ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong public:
590ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  bool Init(const Program& program,
600ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong            const ProblemImpl::ParameterMap& parameter_map,
610ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong            const ParameterBlockOrdering& ordering,
620ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong            string* error);
630ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
640ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // Minimizer interface.
650ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual ~CoordinateDescentMinimizer();
660ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  virtual void Minimize(const Minimizer::Options& options,
670ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                        double* parameters,
680ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong                        Solver::Summary* summary);
690ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
7079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Verify that each group in the ordering forms an independent set.
7179397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  static bool IsOrderingValid(const Program& program,
7279397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                              const ParameterBlockOrdering& ordering,
7379397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez                              string* message);
7479397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
7579397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // Find a recursive decomposition of the Hessian matrix as a set
7679397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // of independent sets of decreasing size and invert it. This
7779397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // seems to work better in practice, i.e., Cameras before
7879397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  // points.
7979397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez  static ParameterBlockOrdering* CreateOrdering(const Program& program);
8079397c21138f54fcff6ec067b44b847f1f7e0e98Carlos Hernandez
810ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong private:
820ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  void Solve(Program* program,
830ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong             LinearSolver* linear_solver,
840ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong             double* parameters,
850ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong             Solver::Summary* summary);
860ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
870ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<ParameterBlock*> parameter_blocks_;
880ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<vector<ResidualBlock*> > residual_blocks_;
890ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // The optimization is performed in rounds. In each round all the
900ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // parameter blocks that form one independent set are optimized in
910ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // parallel. This array, marks the boundaries of the independent
920ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  // sets in parameter_blocks_.
930ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  vector<int> independent_set_offsets_;
940ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
950ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong  Evaluator::Options evaluator_options_;
960ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong};
970ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
980ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace internal
990ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong}  // namespace ceres
1000ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong
1010ae28bd5885b5daa526898fcf7c323dc2c3e1963Angus Kong#endif  // CERES_INTERNAL_COORDINATE_DESCENT_MINIMIZER_H_
102