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 | // |
| 31 | // An example program that minimizes Powell's singular function. |
| 32 | // |
| 33 | // F = 1/2 (f1^2 + f2^2 + f3^2 + f4^2) |
| 34 | // |
| 35 | // f1 = x1 + 10*x2; |
| 36 | // f2 = sqrt(5) * (x3 - x4) |
| 37 | // f3 = (x2 - 2*x3)^2 |
| 38 | // f4 = sqrt(10) * (x1 - x4)^2 |
| 39 | // |
| 40 | // The starting values are x1 = 3, x2 = -1, x3 = 0, x4 = 1. |
| 41 | // The minimum is 0 at (x1, x2, x3, x4) = 0. |
| 42 | // |
| 43 | // From: Testing Unconstrained Optimization Software by Jorge J. More, Burton S. |
| 44 | // Garbow and Kenneth E. Hillstrom in ACM Transactions on Mathematical Software, |
| 45 | // Vol 7(1), March 1981. |
| 46 | |
| 47 | #include <vector> |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 48 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 49 | #include "ceres/ceres.h" |
| 50 | #include "gflags/gflags.h" |
| 51 | #include "glog/logging.h" |
| 52 | |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 53 | struct F1 { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 54 | template <typename T> |
| 55 | bool operator()(const T* const x1, const T* const x2, T* residual) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 56 | // f1 = x1 + 10 * x2; |
| 57 | residual[0] = x1[0] + 10.0 * x2[0]; |
| 58 | return true; |
| 59 | } |
| 60 | }; |
| 61 | |
| 62 | struct F2 { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 63 | template <typename T> |
| 64 | bool operator()(const T* const x3, const T* const x4, T* residual) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 65 | // f2 = sqrt(5) (x3 - x4) |
| 66 | residual[0] = sqrt(5.0) * (x3[0] - x4[0]); |
| 67 | return true; |
| 68 | } |
| 69 | }; |
| 70 | |
| 71 | struct F3 { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 72 | template <typename T> |
| 73 | bool operator()(const T* const x2, const T* const x3, T* residual) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 74 | // f3 = (x2 - 2 x3)^2 |
| 75 | residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]); |
| 76 | return true; |
| 77 | } |
| 78 | }; |
| 79 | |
| 80 | struct F4 { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 81 | template <typename T> |
| 82 | bool operator()(const T* const x1, const T* const x4, T* residual) const { |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 83 | // f4 = sqrt(10) (x1 - x4)^2 |
| 84 | residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]); |
| 85 | return true; |
| 86 | } |
| 87 | }; |
| 88 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 89 | DEFINE_string(minimizer, |
| 90 | "trust_region", |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 91 | "Minimizer type to use, choices are: line_search & trust_region"); |
| 92 | |
| 93 | int main(int argc, char** argv) { |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 94 | GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 95 | google::InitGoogleLogging(argv[0]); |
| 96 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 97 | double x1 = 3.0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 98 | double x2 = -1.0; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 99 | double x3 = 0.0; |
| 100 | double x4 = 1.0; |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 101 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 102 | ceres::Problem problem; |
| 103 | // Add residual terms to the problem using the autodiff |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 104 | // wrapper to get the derivatives automatically. The parameters, x1 through |
| 105 | // x4, are modified in place. |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 106 | problem.AddResidualBlock( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 107 | new ceres::AutoDiffCostFunction<F1, 1, 1, 1>(), nullptr, &x1, &x2); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 108 | problem.AddResidualBlock( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 109 | new ceres::AutoDiffCostFunction<F2, 1, 1, 1>(), nullptr, &x3, &x4); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 110 | problem.AddResidualBlock( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 111 | new ceres::AutoDiffCostFunction<F3, 1, 1, 1>(), nullptr, &x2, &x3); |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 112 | problem.AddResidualBlock( |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 113 | new ceres::AutoDiffCostFunction<F4, 1, 1, 1>(), nullptr, &x1, &x4); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 114 | |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 115 | ceres::Solver::Options options; |
| 116 | LOG_IF(FATAL, |
| 117 | !ceres::StringToMinimizerType(CERES_GET_FLAG(FLAGS_minimizer), |
| 118 | &options.minimizer_type)) |
| 119 | << "Invalid minimizer: " << CERES_GET_FLAG(FLAGS_minimizer) |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 120 | << ", valid options are: trust_region and line_search."; |
| 121 | |
| 122 | options.max_num_iterations = 100; |
| 123 | options.linear_solver_type = ceres::DENSE_QR; |
| 124 | options.minimizer_progress_to_stdout = true; |
| 125 | |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 126 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 127 | std::cout << "Initial x1 = " << x1 |
| 128 | << ", x2 = " << x2 |
| 129 | << ", x3 = " << x3 |
| 130 | << ", x4 = " << x4 |
| 131 | << "\n"; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 132 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 133 | |
| 134 | // Run the solver! |
Austin Schuh | 3de38b0 | 2024-06-25 18:25:10 -0700 | [diff] [blame^] | 135 | ceres::Solver::Summary summary; |
| 136 | ceres::Solve(options, &problem, &summary); |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 137 | |
| 138 | std::cout << summary.FullReport() << "\n"; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 139 | // clang-format off |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 140 | std::cout << "Final x1 = " << x1 |
| 141 | << ", x2 = " << x2 |
| 142 | << ", x3 = " << x3 |
| 143 | << ", x4 = " << x4 |
| 144 | << "\n"; |
Austin Schuh | 1d1e6ea | 2020-12-23 21:56:30 -0800 | [diff] [blame] | 145 | // clang-format on |
Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame] | 146 | return 0; |
| 147 | } |