Keir Mierle | 8ebb073 | 2012-04-30 23:09:08 -0700 | [diff] [blame] | 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 | // Based on the tests in numeric_diff_cost_function.cc. |
| 32 | // |
| 33 | // TODO(keir): See about code duplication. |
| 34 | |
| 35 | #include "ceres/runtime_numeric_diff_cost_function.h" |
| 36 | |
| 37 | #include <algorithm> |
| 38 | #include <cmath> |
| 39 | #include <string> |
| 40 | #include <vector> |
| 41 | |
| 42 | #include <glog/logging.h> |
| 43 | #include "gtest/gtest.h" |
| 44 | #include "ceres/stringprintf.h" |
| 45 | #include "ceres/test_util.h" |
| 46 | #include "ceres/cost_function.h" |
| 47 | #include "ceres/internal/macros.h" |
| 48 | #include "ceres/internal/scoped_ptr.h" |
| 49 | |
| 50 | namespace ceres { |
| 51 | namespace internal { |
| 52 | |
| 53 | const double kRelativeEps = 1e-6; |
| 54 | |
| 55 | // y1 = x1'x2 -> dy1/dx1 = x2, dy1/dx2 = x1 |
| 56 | // y2 = (x1'x2)^2 -> dy2/dx1 = 2 * x2 * (x1'x2), dy2/dx2 = 2 * x1 * (x1'x2) |
| 57 | // y3 = x2'x2 -> dy3/dx1 = 0, dy3/dx2 = 2 * x2 |
| 58 | class TestCostFunction : public CostFunction { |
| 59 | public: |
| 60 | TestCostFunction() { |
| 61 | set_num_residuals(3); |
| 62 | mutable_parameter_block_sizes()->push_back(5); // x1. |
| 63 | mutable_parameter_block_sizes()->push_back(5); // x2. |
| 64 | } |
| 65 | virtual bool Evaluate(double const* const* parameters, |
| 66 | double* residuals, |
| 67 | double** jacobians) const { |
| 68 | (void) jacobians; // Ignored. |
| 69 | |
| 70 | residuals[0] = residuals[1] = residuals[2] = 0; |
| 71 | for (int i = 0; i < 5; ++i) { |
| 72 | residuals[0] += parameters[0][i] * parameters[1][i]; |
| 73 | residuals[2] += parameters[1][i] * parameters[1][i]; |
| 74 | } |
| 75 | residuals[1] = residuals[0] * residuals[0]; |
| 76 | return true; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | TEST(NumericDiffCostFunction, EasyCase) { |
| 81 | // Try both central and forward difference. |
| 82 | TestCostFunction term; |
| 83 | scoped_ptr<CostFunction> cfs[2]; |
| 84 | cfs[0].reset( |
| 85 | CreateRuntimeNumericDiffCostFunction(&term, CENTRAL, kRelativeEps)); |
| 86 | |
| 87 | cfs[1].reset( |
| 88 | CreateRuntimeNumericDiffCostFunction(&term, FORWARD, kRelativeEps)); |
| 89 | |
| 90 | |
| 91 | for (int c = 0; c < 2; ++c) { |
| 92 | CostFunction *cost_function = cfs[c].get(); |
| 93 | |
| 94 | double x1[] = { 1.0, 2.0, 3.0, 4.0, 5.0 }; |
| 95 | double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 }; |
| 96 | double *parameters[] = { &x1[0], &x2[0] }; |
| 97 | |
| 98 | double dydx1[15]; // 3 x 5, row major. |
| 99 | double dydx2[15]; // 3 x 5, row major. |
| 100 | double *jacobians[2] = { &dydx1[0], &dydx2[0] }; |
| 101 | |
| 102 | double residuals[3] = {-1e-100, -2e-100, -3e-100 }; |
| 103 | |
| 104 | ASSERT_TRUE(cost_function->Evaluate(¶meters[0], |
| 105 | &residuals[0], |
| 106 | &jacobians[0])); |
| 107 | |
| 108 | EXPECT_EQ(residuals[0], 67); |
| 109 | EXPECT_EQ(residuals[1], 4489); |
| 110 | EXPECT_EQ(residuals[2], 213); |
| 111 | |
| 112 | for (int i = 0; i < 5; ++i) { |
| 113 | LOG(INFO) << "c = " << c << " i = " << i; |
| 114 | const double kEps = c == 0 ? /* central */ 3e-9 : /* forward */ 2e-5; |
| 115 | |
| 116 | ExpectClose(x2[i], dydx1[5 * 0 + i], kEps); // y1 |
| 117 | ExpectClose(x1[i], dydx2[5 * 0 + i], kEps); |
| 118 | ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], kEps); // y2 |
| 119 | ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], kEps); |
| 120 | ExpectClose(0.0, dydx1[5 * 2 + i], kEps); // y3 |
| 121 | ExpectClose(2 * x2[i], dydx2[5 * 2 + i], kEps); |
| 122 | } |
| 123 | } |
| 124 | } |
| 125 | |
| 126 | // y1 = sin(x1'x2) |
| 127 | // y2 = exp(-x1'x2 / 10) |
| 128 | // |
| 129 | // dy1/dx1 = x2 * cos(x1'x2), dy1/dx2 = x1 * cos(x1'x2) |
| 130 | // dy2/dx1 = -x2 * exp(-x1'x2 / 10) / 10, dy2/dx2 = -x2 * exp(-x1'x2 / 10) / 10 |
| 131 | class TranscendentalTestCostFunction : public CostFunction { |
| 132 | public: |
| 133 | TranscendentalTestCostFunction() { |
| 134 | set_num_residuals(2); |
| 135 | mutable_parameter_block_sizes()->push_back(5); // x1. |
| 136 | mutable_parameter_block_sizes()->push_back(5); // x2. |
| 137 | } |
| 138 | virtual bool Evaluate(double const* const* parameters, |
| 139 | double* residuals, |
| 140 | double** jacobians) const { |
| 141 | (void) jacobians; // Ignored. |
| 142 | |
| 143 | double x1x2 = 0; |
| 144 | for (int i = 0; i < 5; ++i) { |
| 145 | x1x2 += parameters[0][i] * parameters[1][i]; |
| 146 | } |
| 147 | residuals[0] = sin(x1x2); |
| 148 | residuals[1] = exp(-x1x2 / 10); |
| 149 | return true; |
| 150 | } |
| 151 | }; |
| 152 | |
| 153 | TEST(NumericDiffCostFunction, TransendentalOperationsInCostFunction) { |
| 154 | // Try both central and forward difference. |
| 155 | TranscendentalTestCostFunction term; |
| 156 | scoped_ptr<CostFunction> cfs[2]; |
| 157 | cfs[0].reset( |
| 158 | CreateRuntimeNumericDiffCostFunction(&term, CENTRAL, kRelativeEps)); |
| 159 | |
| 160 | cfs[1].reset( |
| 161 | CreateRuntimeNumericDiffCostFunction(&term, FORWARD, kRelativeEps)); |
| 162 | |
| 163 | for (int c = 0; c < 2; ++c) { |
| 164 | CostFunction *cost_function = cfs[c].get(); |
| 165 | |
| 166 | struct { |
| 167 | double x1[5]; |
| 168 | double x2[5]; |
| 169 | } kTests[] = { |
| 170 | { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // No zeros. |
| 171 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 172 | }, |
| 173 | { { 0.0, 2.0, 3.0, 0.0, 5.0 }, // Some zeros x1. |
| 174 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 175 | }, |
| 176 | { { 1.0, 2.0, 3.0, 1.0, 5.0 }, // Some zeros x2. |
| 177 | { 0.0, 9.0, 0.0, 5.0, 0.0 }, |
| 178 | }, |
| 179 | { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros x1. |
| 180 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 181 | }, |
| 182 | { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // All zeros x2. |
| 183 | { 0.0, 0.0, 0.0, 0.0, 0.0 }, |
| 184 | }, |
| 185 | { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros. |
| 186 | { 0.0, 0.0, 0.0, 0.0, 0.0 }, |
| 187 | }, |
| 188 | }; |
| 189 | for (int k = 0; k < ARRAYSIZE(kTests); ++k) { |
| 190 | double *x1 = &(kTests[k].x1[0]); |
| 191 | double *x2 = &(kTests[k].x2[0]); |
| 192 | double *parameters[] = { x1, x2 }; |
| 193 | |
| 194 | double dydx1[10]; |
| 195 | double dydx2[10]; |
| 196 | double *jacobians[2] = { &dydx1[0], &dydx2[0] }; |
| 197 | |
| 198 | double residuals[2]; |
| 199 | |
| 200 | ASSERT_TRUE(cost_function->Evaluate(¶meters[0], |
| 201 | &residuals[0], |
| 202 | &jacobians[0])); |
| 203 | LOG(INFO) << "Ran evaluate for test k=" << k << " c=" << c; |
| 204 | |
| 205 | double x1x2 = 0; |
| 206 | for (int i = 0; i < 5; ++i) { |
| 207 | x1x2 += x1[i] * x2[i]; |
| 208 | } |
| 209 | |
| 210 | for (int i = 0; i < 5; ++i) { |
| 211 | const double kEps = (c == 0 ? /* central */ 3e-9 : /* forward */ 2e-5); |
| 212 | |
| 213 | ExpectClose( x2[i] * cos(x1x2), dydx1[5 * 0 + i], kEps); // NOLINT |
| 214 | ExpectClose( x1[i] * cos(x1x2), dydx2[5 * 0 + i], kEps); // NOLINT |
| 215 | ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], kEps); |
| 216 | ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], kEps); |
| 217 | } |
| 218 | } |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | } // namespace internal |
| 223 | } // namespace ceres |