~ubuntu-branches/ubuntu/raring/ceres-solver/raring

« back to all changes in this revision

Viewing changes to include/ceres/numeric_diff_cost_function.h

  • Committer: Package Import Robot
  • Author(s): Koichi Akabe
  • Date: 2012-06-04 07:15:43 UTC
  • Revision ID: package-import@ubuntu.com-20120604071543-zx6uthupvmtqn3k2
Tags: upstream-1.1.1
ImportĀ upstreamĀ versionĀ 1.1.1

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
// 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
// Create CostFunctions as needed by the least squares framework with jacobians
 
32
// computed via numeric (a.k.a. finite) differentiation. For more details see
 
33
// http://en.wikipedia.org/wiki/Numerical_differentiation.
 
34
//
 
35
// To get a numerically differentiated cost function, define a subclass of
 
36
// CostFunction such that the Evaluate() function ignores the jacobian
 
37
// parameter. The numeric differentiation wrapper will fill in the jacobian
 
38
// parameter if nececssary by repeatedly calling the Evaluate() function with
 
39
// small changes to the appropriate parameters, and computing the slope. For
 
40
// performance, the numeric differentiation wrapper class is templated on the
 
41
// concrete cost function, even though it could be implemented only in terms of
 
42
// the virtual CostFunction interface.
 
43
//
 
44
// The numerically differentiated version of a cost function for a cost function
 
45
// can be constructed as follows:
 
46
//
 
47
//   CostFunction* cost_function
 
48
//       = new NumericDiffCostFunction<MyCostFunction, CENTRAL, 1, 4, 8>(
 
49
//           new MyCostFunction(...), TAKE_OWNERSHIP);
 
50
//
 
51
// where MyCostFunction has 1 residual and 2 parameter blocks with sizes 4 and 8
 
52
// respectively. Look at the tests for a more detailed example.
 
53
//
 
54
// The central difference method is considerably more accurate at the cost of
 
55
// twice as many function evaluations than forward difference. Consider using
 
56
// central differences begin with, and only after that works, trying forward
 
57
// difference to improve performance.
 
58
//
 
59
// TODO(keir): Characterize accuracy; mention pitfalls; provide alternatives.
 
60
 
 
61
#ifndef CERES_PUBLIC_NUMERIC_DIFF_COST_FUNCTION_H_
 
62
#define CERES_PUBLIC_NUMERIC_DIFF_COST_FUNCTION_H_
 
63
 
 
64
#include <cstring>
 
65
#include <glog/logging.h>
 
66
#include "Eigen/Dense"
 
67
#include "ceres/internal/scoped_ptr.h"
 
68
#include "ceres/sized_cost_function.h"
 
69
#include "ceres/types.h"
 
70
 
 
71
namespace ceres {
 
72
 
 
73
enum NumericDiffMethod {
 
74
  CENTRAL,
 
75
  FORWARD
 
76
};
 
77
 
 
78
// This is split from the main class because C++ doesn't allow partial template
 
79
// specializations for member functions. The alternative is to repeat the main
 
80
// class for differing numbers of parameters, which is also unfortunate.
 
81
template <typename CostFunctionNoJacobian,
 
82
          int num_residuals,
 
83
          int parameter_block_size,
 
84
          int parameter_block,
 
85
          NumericDiffMethod method>
 
86
struct Differencer {
 
87
  // Mutates parameters but must restore them before return.
 
88
  static bool EvaluateJacobianForParameterBlock(
 
89
      const CostFunctionNoJacobian *function,
 
90
      double const* residuals_at_eval_point,
 
91
      double **parameters,
 
92
      double **jacobians) {
 
93
    using Eigen::Map;
 
94
    using Eigen::Matrix;
 
95
    using Eigen::RowMajor;
 
96
 
 
97
    typedef Matrix<double, num_residuals, 1> ResidualVector;
 
98
    typedef Matrix<double, parameter_block_size, 1> ParameterVector;
 
99
    typedef Matrix<double, num_residuals, parameter_block_size, RowMajor>
 
100
        JacobianMatrix;
 
101
 
 
102
    Map<JacobianMatrix> parameter_jacobian(jacobians[parameter_block],
 
103
                                           num_residuals,
 
104
                                           parameter_block_size);
 
105
 
 
106
    // Mutate 1 element at a time and then restore.
 
107
    Map<ParameterVector> x_plus_delta(parameters[parameter_block],
 
108
                                      parameter_block_size);
 
109
    ParameterVector x(x_plus_delta);
 
110
 
 
111
    // TODO(keir): Pick a smarter number! In theory a good choice is sqrt(eps) *
 
112
    // x, which for doubles means about 1e-8 * x. However, I have found this
 
113
    // number too optimistic. This number should be exposed for users to change.
 
114
    const double kRelativeStepSize = 1e-6;
 
115
 
 
116
    ParameterVector step_size = x.array().abs() * kRelativeStepSize;
 
117
 
 
118
    // To handle cases where a parameter is exactly zero, instead use the mean
 
119
    // step_size for the other dimensions.
 
120
    double fallback_step_size = step_size.sum() / step_size.rows();
 
121
    if (fallback_step_size == 0.0) {
 
122
      // If all the parameters are zero, there's no good answer. Take
 
123
      // kRelativeStepSize as a guess and hope for the best.
 
124
      fallback_step_size = kRelativeStepSize;
 
125
    }
 
126
 
 
127
    // For each parameter in the parameter block, use finite differences to
 
128
    // compute the derivative for that parameter.
 
129
    for (int j = 0; j < parameter_block_size; ++j) {
 
130
      if (step_size(j) == 0.0) {
 
131
        // The parameter is exactly zero, so compromise and use the mean
 
132
        // step_size from the other parameters. This can break in many cases,
 
133
        // but it's hard to pick a good number without problem specific
 
134
        // knowledge.
 
135
        step_size(j) = fallback_step_size;
 
136
      }
 
137
      x_plus_delta(j) = x(j) + step_size(j);
 
138
 
 
139
      double residuals[num_residuals];  // NOLINT
 
140
      if (!function->Evaluate(parameters, residuals, NULL)) {
 
141
        // Something went wrong; bail.
 
142
        return false;
 
143
      }
 
144
 
 
145
      // Compute this column of the jacobian in 3 steps:
 
146
      // 1. Store residuals for the forward part.
 
147
      // 2. Subtract residuals for the backward (or 0) part.
 
148
      // 3. Divide out the run.
 
149
      parameter_jacobian.col(j) =
 
150
          Map<const ResidualVector>(residuals, num_residuals);
 
151
 
 
152
      double one_over_h = 1 / step_size(j);
 
153
      if (method == CENTRAL) {
 
154
        // Compute the function on the other side of x(j).
 
155
        x_plus_delta(j) = x(j) - step_size(j);
 
156
 
 
157
        if (!function->Evaluate(parameters, residuals, NULL)) {
 
158
          // Something went wrong; bail.
 
159
          return false;
 
160
        }
 
161
        parameter_jacobian.col(j) -=
 
162
            Map<ResidualVector>(residuals, num_residuals, 1);
 
163
        one_over_h /= 2;
 
164
      } else {
 
165
        // Forward difference only; reuse existing residuals evaluation.
 
166
        parameter_jacobian.col(j) -=
 
167
            Map<const ResidualVector>(residuals_at_eval_point, num_residuals);
 
168
      }
 
169
      x_plus_delta(j) = x(j);  // Restore x_plus_delta.
 
170
 
 
171
      // Divide out the run to get slope.
 
172
      parameter_jacobian.col(j) *= one_over_h;
 
173
    }
 
174
    return true;
 
175
  }
 
176
};
 
177
 
 
178
// Prevent invalid instantiations.
 
179
template <typename CostFunctionNoJacobian,
 
180
          int num_residuals,
 
181
          int parameter_block,
 
182
          NumericDiffMethod method>
 
183
struct Differencer<CostFunctionNoJacobian,
 
184
                  num_residuals,
 
185
                  0 /* parameter_block_size */,
 
186
                  parameter_block,
 
187
                  method> {
 
188
  static bool EvaluateJacobianForParameterBlock(
 
189
      const CostFunctionNoJacobian *function,
 
190
      double const* residuals_at_eval_point,
 
191
      double **parameters,
 
192
      double **jacobians) {
 
193
    LOG(FATAL) << "Shouldn't get here.";
 
194
    return true;
 
195
  }
 
196
};
 
197
 
 
198
template <typename CostFunctionNoJacobian,
 
199
         NumericDiffMethod method = CENTRAL, int M = 0,
 
200
         int N0 = 0, int N1 = 0, int N2 = 0, int N3 = 0, int N4 = 0, int N5 = 0>
 
201
class NumericDiffCostFunction
 
202
    : public SizedCostFunction<M, N0, N1, N2, N3, N4, N5> {
 
203
 public:
 
204
  NumericDiffCostFunction(CostFunctionNoJacobian* function,
 
205
                          Ownership ownership)
 
206
      : function_(function), ownership_(ownership) {}
 
207
 
 
208
  virtual ~NumericDiffCostFunction() {
 
209
    if (ownership_ != TAKE_OWNERSHIP) {
 
210
      function_.release();
 
211
    }
 
212
  }
 
213
 
 
214
  virtual bool Evaluate(double const* const* parameters,
 
215
                        double* residuals,
 
216
                        double** jacobians) const {
 
217
    // Get the function value (residuals) at the the point to evaluate.
 
218
    bool success = function_->Evaluate(parameters, residuals, NULL);
 
219
    if (!success) {
 
220
      // Something went wrong; ignore the jacobian.
 
221
      return false;
 
222
    }
 
223
    if (!jacobians) {
 
224
      // Nothing to do; just forward.
 
225
      return true;
 
226
    }
 
227
 
 
228
    // Create a copy of the parameters which will get mutated.
 
229
    const int kParametersSize = N0 + N1 + N2 + N3 + N4 + N5;
 
230
    double parameters_copy[kParametersSize];
 
231
    double *parameters_references_copy[6];
 
232
    parameters_references_copy[0] = &parameters_copy[0];
 
233
    parameters_references_copy[1] = &parameters_copy[0] + N0;
 
234
    parameters_references_copy[2] = &parameters_copy[0] + N0 + N1;
 
235
    parameters_references_copy[3] = &parameters_copy[0] + N0 + N1 + N2;
 
236
    parameters_references_copy[4] = &parameters_copy[0] + N0 + N1 + N2 + N3;
 
237
    parameters_references_copy[5] =
 
238
        &parameters_copy[0] + N0 + N1 + N2 + N3 + N4;
 
239
 
 
240
#define COPY_PARAMETER_BLOCK(block) \
 
241
    if (N ## block) memcpy(parameters_references_copy[block], \
 
242
                           parameters[block], \
 
243
                           sizeof(double) * N ## block);  // NOLINT
 
244
    COPY_PARAMETER_BLOCK(0);
 
245
    COPY_PARAMETER_BLOCK(1);
 
246
    COPY_PARAMETER_BLOCK(2);
 
247
    COPY_PARAMETER_BLOCK(3);
 
248
    COPY_PARAMETER_BLOCK(4);
 
249
    COPY_PARAMETER_BLOCK(5);
 
250
#undef COPY_PARAMETER_BLOCK
 
251
 
 
252
#define EVALUATE_JACOBIAN_FOR_BLOCK(block) \
 
253
    if (N ## block && jacobians[block]) { \
 
254
      if (!Differencer<CostFunctionNoJacobian, /* NOLINT */ \
 
255
                       M, \
 
256
                       N ## block, \
 
257
                       block, \
 
258
                       method>::EvaluateJacobianForParameterBlock( \
 
259
          function_.get(), \
 
260
          residuals, \
 
261
          parameters_references_copy, \
 
262
          jacobians)) { \
 
263
        return false; \
 
264
      } \
 
265
    }
 
266
    EVALUATE_JACOBIAN_FOR_BLOCK(0);
 
267
    EVALUATE_JACOBIAN_FOR_BLOCK(1);
 
268
    EVALUATE_JACOBIAN_FOR_BLOCK(2);
 
269
    EVALUATE_JACOBIAN_FOR_BLOCK(3);
 
270
    EVALUATE_JACOBIAN_FOR_BLOCK(4);
 
271
    EVALUATE_JACOBIAN_FOR_BLOCK(5);
 
272
#undef EVALUATE_JACOBIAN_FOR_BLOCK
 
273
    return true;
 
274
  }
 
275
 
 
276
 private:
 
277
  internal::scoped_ptr<CostFunctionNoJacobian> function_;
 
278
  Ownership ownership_;
 
279
};
 
280
 
 
281
}  // namespace ceres
 
282
 
 
283
#endif  // CERES_PUBLIC_NUMERIC_DIFF_COST_FUNCTION_H_