blob: c75ad24d3ba269fd72519cc1ba1e05981a2648eb [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
2// Copyright 2015 Google Inc. All rights reserved.
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 Schuh1d1e6ea2020-12-23 21:56:30 -080048
Austin Schuh70cc9552019-01-21 19:46:48 -080049#include "ceres/ceres.h"
50#include "gflags/gflags.h"
51#include "glog/logging.h"
52
53using ceres::AutoDiffCostFunction;
54using ceres::CostFunction;
55using ceres::Problem;
Austin Schuh70cc9552019-01-21 19:46:48 -080056using ceres::Solve;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080057using ceres::Solver;
Austin Schuh70cc9552019-01-21 19:46:48 -080058
59struct F1 {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080060 template <typename T>
61 bool operator()(const T* const x1, const T* const x2, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080062 // f1 = x1 + 10 * x2;
63 residual[0] = x1[0] + 10.0 * x2[0];
64 return true;
65 }
66};
67
68struct F2 {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080069 template <typename T>
70 bool operator()(const T* const x3, const T* const x4, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080071 // f2 = sqrt(5) (x3 - x4)
72 residual[0] = sqrt(5.0) * (x3[0] - x4[0]);
73 return true;
74 }
75};
76
77struct F3 {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080078 template <typename T>
79 bool operator()(const T* const x2, const T* const x3, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080080 // f3 = (x2 - 2 x3)^2
81 residual[0] = (x2[0] - 2.0 * x3[0]) * (x2[0] - 2.0 * x3[0]);
82 return true;
83 }
84};
85
86struct F4 {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080087 template <typename T>
88 bool operator()(const T* const x1, const T* const x4, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -080089 // f4 = sqrt(10) (x1 - x4)^2
90 residual[0] = sqrt(10.0) * (x1[0] - x4[0]) * (x1[0] - x4[0]);
91 return true;
92 }
93};
94
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080095DEFINE_string(minimizer,
96 "trust_region",
Austin Schuh70cc9552019-01-21 19:46:48 -080097 "Minimizer type to use, choices are: line_search & trust_region");
98
99int main(int argc, char** argv) {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800100 GFLAGS_NAMESPACE::ParseCommandLineFlags(&argc, &argv, true);
Austin Schuh70cc9552019-01-21 19:46:48 -0800101 google::InitGoogleLogging(argv[0]);
102
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800103 double x1 = 3.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800104 double x2 = -1.0;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105 double x3 = 0.0;
106 double x4 = 1.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800107
108 Problem problem;
109 // Add residual terms to the problem using the using the autodiff
110 // wrapper to get the derivatives automatically. The parameters, x1 through
111 // x4, are modified in place.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800112 problem.AddResidualBlock(
113 new AutoDiffCostFunction<F1, 1, 1, 1>(new F1), NULL, &x1, &x2);
114 problem.AddResidualBlock(
115 new AutoDiffCostFunction<F2, 1, 1, 1>(new F2), NULL, &x3, &x4);
116 problem.AddResidualBlock(
117 new AutoDiffCostFunction<F3, 1, 1, 1>(new F3), NULL, &x2, &x3);
118 problem.AddResidualBlock(
119 new AutoDiffCostFunction<F4, 1, 1, 1>(new F4), NULL, &x1, &x4);
Austin Schuh70cc9552019-01-21 19:46:48 -0800120
121 Solver::Options options;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800122 LOG_IF(
123 FATAL,
124 !ceres::StringToMinimizerType(FLAGS_minimizer, &options.minimizer_type))
Austin Schuh70cc9552019-01-21 19:46:48 -0800125 << "Invalid minimizer: " << FLAGS_minimizer
126 << ", valid options are: trust_region and line_search.";
127
128 options.max_num_iterations = 100;
129 options.linear_solver_type = ceres::DENSE_QR;
130 options.minimizer_progress_to_stdout = true;
131
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800132 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800133 std::cout << "Initial x1 = " << x1
134 << ", x2 = " << x2
135 << ", x3 = " << x3
136 << ", x4 = " << x4
137 << "\n";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800138 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800139
140 // Run the solver!
141 Solver::Summary summary;
142 Solve(options, &problem, &summary);
143
144 std::cout << summary.FullReport() << "\n";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800145 // clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -0800146 std::cout << "Final x1 = " << x1
147 << ", x2 = " << x2
148 << ", x3 = " << x3
149 << ", x4 = " << x4
150 << "\n";
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800151 // clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800152 return 0;
153}