1 // Ceres Solver - A fast non-linear least squares minimizer
2 // Copyright 2015 Google Inc. All rights reserved.
3 // http://ceres-solver.org/
5 // Redistribution and use in source and binary forms, with or without
6 // modification, are permitted provided that the following conditions are met:
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.
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.
29 // Authors: keir@google.com (Keir Mierle),
30 // dgossow@google.com (David Gossow)
32 #ifndef CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
33 #define CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_
37 #include "ceres/cost_function.h"
38 #include "ceres/iteration_callback.h"
39 #include "ceres/local_parameterization.h"
40 #include "ceres/mutex.h"
47 // Callback that collects information about gradient checking errors, and
48 // will abort the solve as soon as an error occurs.
49 class GradientCheckingIterationCallback : public IterationCallback {
51 GradientCheckingIterationCallback();
53 // Will return SOLVER_CONTINUE until a gradient error has been detected,
54 // then return SOLVER_ABORT.
55 virtual CallbackReturnType operator()(const IterationSummary& summary);
57 // Notify this that a gradient error has occurred (thread safe).
58 void SetGradientErrorDetected(std::string& error_log);
60 // Retrieve error status (not thread safe).
61 bool gradient_error_detected() const { return gradient_error_detected_; }
62 const std::string& error_log() const { return error_log_; }
64 bool gradient_error_detected_;
65 std::string error_log_;
66 // Mutex protecting member variables.
67 ceres::internal::Mutex mutex_;
70 // Creates a CostFunction that checks the Jacobians that cost_function computes
71 // with finite differences. This API is only intended for unit tests that intend
72 // to check the functionality of the GradientCheckingCostFunction
73 // implementation directly.
74 CostFunction* CreateGradientCheckingCostFunction(
75 const CostFunction* cost_function,
76 const std::vector<const LocalParameterization*>* local_parameterizations,
77 double relative_step_size,
78 double relative_precision,
79 const std::string& extra_info,
80 GradientCheckingIterationCallback* callback);
82 // Create a new ProblemImpl object from the input problem_impl, where all
83 // cost functions are wrapped so that each time their Evaluate method is called,
84 // an additional check is performed that compares the Jacobians computed by
85 // the original cost function with alternative Jacobians computed using
86 // numerical differentiation. If local parameterizations are given for any
87 // parameters, the Jacobians will be compared in the local space instead of the
88 // ambient space. For details on the gradient checking procedure, see the
89 // documentation of the GradientChecker class. If an error is detected in any
90 // iteration, the respective cost function will notify the
91 // GradientCheckingIterationCallback.
93 // The caller owns the returned ProblemImpl object.
95 // Note: This is quite inefficient and is intended only for debugging.
97 // relative_step_size and relative_precision are parameters to control
98 // the numeric differentiation and the relative tolerance between the
99 // jacobian computed by the CostFunctions in problem_impl and
100 // jacobians obtained by numerically differentiating them. See the
101 // documentation of 'numeric_derivative_relative_step_size' in solver.h for a
102 // better explanation.
103 ProblemImpl* CreateGradientCheckingProblemImpl(
104 ProblemImpl* problem_impl,
105 double relative_step_size,
106 double relative_precision,
107 GradientCheckingIterationCallback* callback);
109 } // namespace internal
112 #endif // CERES_INTERNAL_GRADIENT_CHECKING_COST_FUNCTION_H_