Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 1 | // Ceres Solver - A fast non-linear least squares minimizer |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 2 | // Copyright 2023 Google Inc. All rights reserved. |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 3 | // http://ceres-solver.org/ |
| 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: sameeragarwal@google.com (Sameer Agarwal) |
| 30 | // tbennun@gmail.com (Tal Ben-Nun) |
| 31 | |
| 32 | #include "ceres/numeric_diff_test_utils.h" |
| 33 | |
| 34 | #include <algorithm> |
| 35 | #include <cmath> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 36 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 37 | #include "ceres/cost_function.h" |
| 38 | #include "ceres/test_util.h" |
| 39 | #include "ceres/types.h" |
| 40 | #include "gtest/gtest.h" |
| 41 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 42 | namespace ceres::internal { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 43 | |
| 44 | bool EasyFunctor::operator()(const double* x1, |
| 45 | const double* x2, |
| 46 | double* residuals) const { |
| 47 | residuals[0] = residuals[1] = residuals[2] = 0; |
| 48 | for (int i = 0; i < 5; ++i) { |
| 49 | residuals[0] += x1[i] * x2[i]; |
| 50 | residuals[2] += x2[i] * x2[i]; |
| 51 | } |
| 52 | residuals[1] = residuals[0] * residuals[0]; |
| 53 | return true; |
| 54 | } |
| 55 | |
| 56 | void EasyFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 57 | const CostFunction& cost_function, NumericDiffMethodType method) const { |
| 58 | // The x1[0] is made deliberately small to test the performance near zero. |
| 59 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 60 | double x1[] = { 1e-64, 2.0, 3.0, 4.0, 5.0 }; |
| 61 | double x2[] = { 9.0, 9.0, 5.0, 5.0, 1.0 }; |
| 62 | double *parameters[] = { &x1[0], &x2[0] }; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 63 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 64 | |
| 65 | double dydx1[15]; // 3 x 5, row major. |
| 66 | double dydx2[15]; // 3 x 5, row major. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 67 | double* jacobians[2] = {&dydx1[0], &dydx2[0]}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 68 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 69 | double residuals[3] = {-1e-100, -2e-100, -3e-100}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 70 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 71 | ASSERT_TRUE( |
| 72 | cost_function.Evaluate(¶meters[0], &residuals[0], &jacobians[0])); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 73 | |
| 74 | double expected_residuals[3]; |
| 75 | EasyFunctor functor; |
| 76 | functor(x1, x2, expected_residuals); |
| 77 | EXPECT_EQ(expected_residuals[0], residuals[0]); |
| 78 | EXPECT_EQ(expected_residuals[1], residuals[1]); |
| 79 | EXPECT_EQ(expected_residuals[2], residuals[2]); |
| 80 | |
| 81 | double tolerance = 0.0; |
| 82 | switch (method) { |
| 83 | default: |
| 84 | case CENTRAL: |
| 85 | tolerance = 3e-9; |
| 86 | break; |
| 87 | |
| 88 | case FORWARD: |
| 89 | tolerance = 2e-5; |
| 90 | break; |
| 91 | |
| 92 | case RIDDERS: |
| 93 | tolerance = 1e-13; |
| 94 | break; |
| 95 | } |
| 96 | |
| 97 | for (int i = 0; i < 5; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 98 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 99 | ExpectClose(x2[i], dydx1[5 * 0 + i], tolerance); // y1 |
| 100 | ExpectClose(x1[i], dydx2[5 * 0 + i], tolerance); |
| 101 | ExpectClose(2 * x2[i] * residuals[0], dydx1[5 * 1 + i], tolerance); // y2 |
| 102 | ExpectClose(2 * x1[i] * residuals[0], dydx2[5 * 1 + i], tolerance); |
| 103 | ExpectClose(0.0, dydx1[5 * 2 + i], tolerance); // y3 |
| 104 | ExpectClose(2 * x2[i], dydx2[5 * 2 + i], tolerance); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 105 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | bool TranscendentalFunctor::operator()(const double* x1, |
| 110 | const double* x2, |
| 111 | double* residuals) const { |
| 112 | double x1x2 = 0; |
| 113 | for (int i = 0; i < 5; ++i) { |
| 114 | x1x2 += x1[i] * x2[i]; |
| 115 | } |
| 116 | residuals[0] = sin(x1x2); |
| 117 | residuals[1] = exp(-x1x2 / 10); |
| 118 | return true; |
| 119 | } |
| 120 | |
| 121 | void TranscendentalFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 122 | const CostFunction& cost_function, NumericDiffMethodType method) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 123 | struct TestParameterBlocks { |
| 124 | double x1[5]; |
| 125 | double x2[5]; |
| 126 | }; |
| 127 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 128 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 129 | std::vector<TestParameterBlocks> kTests = { |
| 130 | { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // No zeros. |
| 131 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 132 | }, |
| 133 | { { 0.0, 2.0, 3.0, 0.0, 5.0 }, // Some zeros x1. |
| 134 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 135 | }, |
| 136 | { { 1.0, 2.0, 3.0, 1.0, 5.0 }, // Some zeros x2. |
| 137 | { 0.0, 9.0, 0.0, 5.0, 0.0 }, |
| 138 | }, |
| 139 | { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros x1. |
| 140 | { 9.0, 9.0, 5.0, 5.0, 1.0 }, |
| 141 | }, |
| 142 | { { 1.0, 2.0, 3.0, 4.0, 5.0 }, // All zeros x2. |
| 143 | { 0.0, 0.0, 0.0, 0.0, 0.0 }, |
| 144 | }, |
| 145 | { { 0.0, 0.0, 0.0, 0.0, 0.0 }, // All zeros. |
| 146 | { 0.0, 0.0, 0.0, 0.0, 0.0 }, |
| 147 | }, |
| 148 | }; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 149 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 150 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 151 | for (auto& test : kTests) { |
| 152 | double* x1 = &(test.x1[0]); |
| 153 | double* x2 = &(test.x2[0]); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 154 | double* parameters[] = {x1, x2}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 155 | |
| 156 | double dydx1[10]; |
| 157 | double dydx2[10]; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 158 | double* jacobians[2] = {&dydx1[0], &dydx2[0]}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 159 | |
| 160 | double residuals[2]; |
| 161 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 162 | ASSERT_TRUE( |
| 163 | cost_function.Evaluate(¶meters[0], &residuals[0], &jacobians[0])); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 164 | double x1x2 = 0; |
| 165 | for (int i = 0; i < 5; ++i) { |
| 166 | x1x2 += x1[i] * x2[i]; |
| 167 | } |
| 168 | |
| 169 | double tolerance = 0.0; |
| 170 | switch (method) { |
| 171 | default: |
| 172 | case CENTRAL: |
| 173 | tolerance = 2e-7; |
| 174 | break; |
| 175 | |
| 176 | case FORWARD: |
| 177 | tolerance = 2e-5; |
| 178 | break; |
| 179 | |
| 180 | case RIDDERS: |
| 181 | tolerance = 3e-12; |
| 182 | break; |
| 183 | } |
| 184 | |
| 185 | for (int i = 0; i < 5; ++i) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 186 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 187 | ExpectClose( x2[i] * cos(x1x2), dydx1[5 * 0 + i], tolerance); |
| 188 | ExpectClose( x1[i] * cos(x1x2), dydx2[5 * 0 + i], tolerance); |
| 189 | ExpectClose(-x2[i] * exp(-x1x2 / 10.) / 10., dydx1[5 * 1 + i], tolerance); |
| 190 | ExpectClose(-x1[i] * exp(-x1x2 / 10.) / 10., dydx2[5 * 1 + i], tolerance); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 191 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 192 | } |
| 193 | } |
| 194 | } |
| 195 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 196 | bool ExponentialFunctor::operator()(const double* x1, double* residuals) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 197 | residuals[0] = exp(x1[0]); |
| 198 | return true; |
| 199 | } |
| 200 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 201 | void ExponentialFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( |
| 202 | const CostFunction& cost_function) const { |
| 203 | // Evaluating the functor at specific points for testing. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 204 | std::vector<double> kTests = {1.0, 2.0, 3.0, 4.0, 5.0}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 205 | |
| 206 | // Minimal tolerance w.r.t. the cost function and the tests. |
| 207 | const double kTolerance = 2e-14; |
| 208 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 209 | for (double& test : kTests) { |
| 210 | double* parameters[] = {&test}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 211 | double dydx; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 212 | double* jacobians[1] = {&dydx}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 213 | double residual; |
| 214 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 215 | ASSERT_TRUE( |
| 216 | cost_function.Evaluate(¶meters[0], &residual, &jacobians[0])); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 217 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 218 | double expected_result = exp(test); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 219 | |
| 220 | // Expect residual to be close to exp(x). |
| 221 | ExpectClose(residual, expected_result, kTolerance); |
| 222 | |
| 223 | // Check evaluated differences. dydx should also be close to exp(x). |
| 224 | ExpectClose(dydx, expected_result, kTolerance); |
| 225 | } |
| 226 | } |
| 227 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 228 | bool RandomizedFunctor::operator()(const double* x1, double* residuals) const { |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 229 | double random_value = uniform_distribution_(*prng_); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 230 | residuals[0] = x1[0] * x1[0] + random_value; |
| 231 | return true; |
| 232 | } |
| 233 | |
| 234 | void RandomizedFunctor::ExpectCostFunctionEvaluationIsNearlyCorrect( |
| 235 | const CostFunction& cost_function) const { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 236 | std::vector<double> kTests = {0.0, 1.0, 3.0, 4.0, 50.0}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 237 | |
| 238 | const double kTolerance = 2e-4; |
| 239 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 240 | for (double& test : kTests) { |
| 241 | double* parameters[] = {&test}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 242 | double dydx; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 243 | double* jacobians[1] = {&dydx}; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 244 | double residual; |
| 245 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 246 | ASSERT_TRUE( |
| 247 | cost_function.Evaluate(¶meters[0], &residual, &jacobians[0])); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 248 | |
| 249 | // Expect residual to be close to x^2 w.r.t. noise factor. |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 250 | ExpectClose(residual, test * test, noise_factor_); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 251 | |
| 252 | // Check evaluated differences. (dy/dx = ~2x) |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 253 | ExpectClose(dydx, 2 * test, kTolerance); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 254 | } |
| 255 | } |
| 256 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 257 | } // namespace ceres::internal |