blob: c05483021a4608eefa7cc3186173e52df77754ac [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2010, 2011, 2012 Google Inc. All rights reserved.
3// http://code.google.com/p/ceres-solver/
4//
5// Redistribution and use in source and binary forms, with or without
6// modification, are permitted provided that the following conditions are met:
7//
8// * Redistributions of source code must retain the above copyright notice,
9// this list of conditions and the following disclaimer.
10// * Redistributions in binary form must reproduce the above copyright notice,
11// this list of conditions and the following disclaimer in the documentation
12// and/or other materials provided with the distribution.
13// * Neither the name of Google Inc. nor the names of its contributors may be
14// used to endorse or promote products derived from this software without
15// specific prior written permission.
16//
17// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27// POSSIBILITY OF SUCH DAMAGE.
28//
29// Author: keir@google.com (Keir Mierle)
30
31#ifndef CERES_INTERNAL_SOLVER_IMPL_H_
32#define CERES_INTERNAL_SOLVER_IMPL_H_
33
Sameer Agarwal509f68c2013-02-20 01:39:03 -080034#include <set>
Sameer Agarwal4997cbc2012-07-02 12:44:34 -070035#include <string>
36#include <vector>
37#include "ceres/internal/port.h"
Sameer Agarwal2c94eed2012-10-01 16:34:37 -070038#include "ceres/ordered_groups.h"
Sameer Agarwal9123e2f2012-09-18 21:49:06 -070039#include "ceres/problem_impl.h"
Sameer Agarwalba8d9672012-10-02 00:48:57 -070040#include "ceres/solver.h"
Keir Mierle8ebb0732012-04-30 23:09:08 -070041
42namespace ceres {
43namespace internal {
44
Sameer Agarwalba8d9672012-10-02 00:48:57 -070045class CoordinateDescentMinimizer;
Keir Mierle8ebb0732012-04-30 23:09:08 -070046class Evaluator;
47class LinearSolver;
Keir Mierle8ebb0732012-04-30 23:09:08 -070048class Program;
49
50class SolverImpl {
51 public:
52 // Mirrors the interface in solver.h, but exposes implementation
53 // details for testing internally.
54 static void Solve(const Solver::Options& options,
Keir Mierle6196cba2012-06-18 11:03:40 -070055 ProblemImpl* problem_impl,
Keir Mierle8ebb0732012-04-30 23:09:08 -070056 Solver::Summary* summary);
57
Sameer Agarwalf4d01642012-11-26 12:55:58 -080058 static void TrustRegionSolve(const Solver::Options& options,
59 ProblemImpl* problem_impl,
60 Solver::Summary* summary);
61
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070062 // Run the TrustRegionMinimizer for the given evaluator and configuration.
63 static void TrustRegionMinimize(
64 const Solver::Options &options,
65 Program* program,
66 CoordinateDescentMinimizer* inner_iteration_minimizer,
67 Evaluator* evaluator,
68 LinearSolver* linear_solver,
69 double* parameters,
70 Solver::Summary* summary);
71
72#ifndef CERES_NO_LINE_SEARCH_MINIMIZER
Sameer Agarwalf4d01642012-11-26 12:55:58 -080073 static void LineSearchSolve(const Solver::Options& options,
74 ProblemImpl* problem_impl,
75 Solver::Summary* summary);
76
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070077 // Run the LineSearchMinimizer for the given evaluator and configuration.
78 static void LineSearchMinimize(const Solver::Options &options,
79 Program* program,
80 Evaluator* evaluator,
81 double* parameters,
82 Solver::Summary* summary);
Sameer Agarwal700d50d2013-03-12 16:12:42 -070083#endif // CERES_NO_LINE_SEARCH_MINIMIZER
Sameer Agarwal8140f0f2013-03-12 09:45:08 -070084
Keir Mierle8ebb0732012-04-30 23:09:08 -070085 // Create the transformed Program, which has all the fixed blocks
86 // and residuals eliminated, and in the case of automatic schur
87 // ordering, has the E blocks first in the resulting program, with
88 // options.num_eliminate_blocks set appropriately.
Sameer Agarwal9123e2f2012-09-18 21:49:06 -070089 //
Markus Moll8de78db2012-07-14 15:49:11 +020090 // If fixed_cost is not NULL, the residual blocks that are removed
91 // are evaluated and the sum of their cost is returned in fixed_cost.
Keir Mierle8ebb0732012-04-30 23:09:08 -070092 static Program* CreateReducedProgram(Solver::Options* options,
93 ProblemImpl* problem_impl,
Markus Moll8de78db2012-07-14 15:49:11 +020094 double* fixed_cost,
Keir Mierle8ebb0732012-04-30 23:09:08 -070095 string* error);
96
97 // Create the appropriate linear solver, taking into account any
98 // config changes decided by CreateTransformedProgram(). The
99 // selected linear solver, which may be different from what the user
100 // selected; consider the case that the remaining elimininated
101 // blocks is zero after removing fixed blocks.
102 static LinearSolver* CreateLinearSolver(Solver::Options* options,
103 string* error);
104
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700105 // Reorder the parameter blocks in program using the ordering. A
106 // return value of true indicates success and false indicates an
107 // error was encountered whose cause is logged to LOG(ERROR).
108 static bool ApplyUserOrdering(const ProblemImpl::ParameterMap& parameter_map,
Sameer Agarwal2c94eed2012-10-01 16:34:37 -0700109 const ParameterBlockOrdering* ordering,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700110 Program* program,
111 string* error);
112
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700113
Keir Mierle8ebb0732012-04-30 23:09:08 -0700114 // Reorder the residuals for program, if necessary, so that the
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700115 // residuals involving e block (i.e., the first num_eliminate_block
116 // parameter blocks) occur together. This is a necessary condition
Sameer Agarwalba8d9672012-10-02 00:48:57 -0700117 // for the Schur eliminator.
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700118 static bool LexicographicallyOrderResidualBlocks(
119 const int num_eliminate_blocks,
120 Program* program,
121 string* error);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700122
123 // Create the appropriate evaluator for the transformed program.
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700124 static Evaluator* CreateEvaluator(
125 const Solver::Options& options,
126 const ProblemImpl::ParameterMap& parameter_map,
127 Program* program,
128 string* error);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700129
Keir Mierle8ebb0732012-04-30 23:09:08 -0700130 // Remove the fixed or unused parameter blocks and residuals
131 // depending only on fixed parameters from the problem. Also updates
132 // num_eliminate_blocks, since removed parameters changes the point
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700133 // at which the eliminated blocks is valid. If fixed_cost is not
134 // NULL, the residual blocks that are removed are evaluated and the
135 // sum of their cost is returned in fixed_cost.
Keir Mierle8ebb0732012-04-30 23:09:08 -0700136 static bool RemoveFixedBlocksFromProgram(Program* program,
Sameer Agarwal2c94eed2012-10-01 16:34:37 -0700137 ParameterBlockOrdering* ordering,
Markus Moll8de78db2012-07-14 15:49:11 +0200138 double* fixed_cost,
Keir Mierle8ebb0732012-04-30 23:09:08 -0700139 string* error);
Sameer Agarwal65625f72012-09-17 12:06:57 -0700140
141 static bool IsOrderingValid(const Solver::Options& options,
142 const ProblemImpl* problem_impl,
143 string* error);
Sameer Agarwal9123e2f2012-09-18 21:49:06 -0700144
145 static bool IsParameterBlockSetIndependent(
Sameer Agarwalba8d9672012-10-02 00:48:57 -0700146 const set<double*>& parameter_block_ptrs,
147 const vector<ResidualBlock*>& residual_blocks);
Sameer Agarwal67a107b2012-10-08 14:35:41 -0700148
149 static CoordinateDescentMinimizer* CreateInnerIterationMinimizer(
150 const Solver::Options& options,
151 const Program& program,
152 const ProblemImpl::ParameterMap& parameter_map,
Sameer Agarwal977be7c2013-01-26 16:01:54 -0800153 Solver::Summary* summary);
Keir Mierle8ebb0732012-04-30 23:09:08 -0700154};
155
156} // namespace internal
157} // namespace ceres
158
159#endif // CERES_INTERNAL_SOLVER_IMPL_H_