blob: 105402e90d7d9c8ea577c77a23099be2e2c63c3f [file] [log] [blame]
Austin Schuh70cc9552019-01-21 19:46:48 -08001// Ceres Solver - A fast non-linear least squares minimizer
Austin Schuh3de38b02024-06-25 18:25:10 -07002// Copyright 2023 Google Inc. All rights reserved.
Austin Schuh70cc9552019-01-21 19:46:48 -08003// 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)
Austin Schuh3de38b02024-06-25 18:25:10 -070030//
31// This example fits the curve f(x;m,c) = e^(m * x + c) to data, minimizing the
32// sum squared loss.
Austin Schuh70cc9552019-01-21 19:46:48 -080033
34#include "ceres/ceres.h"
35#include "glog/logging.h"
36
Austin Schuh70cc9552019-01-21 19:46:48 -080037// Data generated using the following octave code.
38// randn('seed', 23497);
39// m = 0.3;
40// c = 0.1;
41// x=[0:0.075:5];
42// y = exp(m * x + c);
43// noise = randn(size(x)) * 0.2;
44// y_observed = y + noise;
45// data = [x', y_observed'];
46
47const int kNumObservations = 67;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080048// clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080049const double data[] = {
50 0.000000e+00, 1.133898e+00,
51 7.500000e-02, 1.334902e+00,
52 1.500000e-01, 1.213546e+00,
53 2.250000e-01, 1.252016e+00,
54 3.000000e-01, 1.392265e+00,
55 3.750000e-01, 1.314458e+00,
56 4.500000e-01, 1.472541e+00,
57 5.250000e-01, 1.536218e+00,
58 6.000000e-01, 1.355679e+00,
59 6.750000e-01, 1.463566e+00,
60 7.500000e-01, 1.490201e+00,
61 8.250000e-01, 1.658699e+00,
62 9.000000e-01, 1.067574e+00,
63 9.750000e-01, 1.464629e+00,
64 1.050000e+00, 1.402653e+00,
65 1.125000e+00, 1.713141e+00,
66 1.200000e+00, 1.527021e+00,
67 1.275000e+00, 1.702632e+00,
68 1.350000e+00, 1.423899e+00,
69 1.425000e+00, 1.543078e+00,
70 1.500000e+00, 1.664015e+00,
71 1.575000e+00, 1.732484e+00,
72 1.650000e+00, 1.543296e+00,
73 1.725000e+00, 1.959523e+00,
74 1.800000e+00, 1.685132e+00,
75 1.875000e+00, 1.951791e+00,
76 1.950000e+00, 2.095346e+00,
77 2.025000e+00, 2.361460e+00,
78 2.100000e+00, 2.169119e+00,
79 2.175000e+00, 2.061745e+00,
80 2.250000e+00, 2.178641e+00,
81 2.325000e+00, 2.104346e+00,
82 2.400000e+00, 2.584470e+00,
83 2.475000e+00, 1.914158e+00,
84 2.550000e+00, 2.368375e+00,
85 2.625000e+00, 2.686125e+00,
86 2.700000e+00, 2.712395e+00,
87 2.775000e+00, 2.499511e+00,
88 2.850000e+00, 2.558897e+00,
89 2.925000e+00, 2.309154e+00,
90 3.000000e+00, 2.869503e+00,
91 3.075000e+00, 3.116645e+00,
92 3.150000e+00, 3.094907e+00,
93 3.225000e+00, 2.471759e+00,
94 3.300000e+00, 3.017131e+00,
95 3.375000e+00, 3.232381e+00,
96 3.450000e+00, 2.944596e+00,
97 3.525000e+00, 3.385343e+00,
98 3.600000e+00, 3.199826e+00,
99 3.675000e+00, 3.423039e+00,
100 3.750000e+00, 3.621552e+00,
101 3.825000e+00, 3.559255e+00,
102 3.900000e+00, 3.530713e+00,
103 3.975000e+00, 3.561766e+00,
104 4.050000e+00, 3.544574e+00,
105 4.125000e+00, 3.867945e+00,
106 4.200000e+00, 4.049776e+00,
107 4.275000e+00, 3.885601e+00,
108 4.350000e+00, 4.110505e+00,
109 4.425000e+00, 4.345320e+00,
110 4.500000e+00, 4.161241e+00,
111 4.575000e+00, 4.363407e+00,
112 4.650000e+00, 4.161576e+00,
113 4.725000e+00, 4.619728e+00,
114 4.800000e+00, 4.737410e+00,
115 4.875000e+00, 4.727863e+00,
116 4.950000e+00, 4.669206e+00,
117};
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800118// clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800119
120struct ExponentialResidual {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800121 ExponentialResidual(double x, double y) : x_(x), y_(y) {}
Austin Schuh70cc9552019-01-21 19:46:48 -0800122
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800123 template <typename T>
124 bool operator()(const T* const m, const T* const c, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800125 residual[0] = y_ - exp(m[0] * x_ + c[0]);
126 return true;
127 }
128
129 private:
130 const double x_;
131 const double y_;
132};
133
134int main(int argc, char** argv) {
135 google::InitGoogleLogging(argv[0]);
136
Austin Schuh3de38b02024-06-25 18:25:10 -0700137 const double initial_m = 0.0;
138 const double initial_c = 0.0;
139 double m = initial_m;
140 double c = initial_c;
Austin Schuh70cc9552019-01-21 19:46:48 -0800141
Austin Schuh3de38b02024-06-25 18:25:10 -0700142 ceres::Problem problem;
Austin Schuh70cc9552019-01-21 19:46:48 -0800143 for (int i = 0; i < kNumObservations; ++i) {
144 problem.AddResidualBlock(
Austin Schuh3de38b02024-06-25 18:25:10 -0700145 new ceres::AutoDiffCostFunction<ExponentialResidual, 1, 1, 1>(
146 data[2 * i], data[2 * i + 1]),
147 nullptr,
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800148 &m,
149 &c);
Austin Schuh70cc9552019-01-21 19:46:48 -0800150 }
151
Austin Schuh3de38b02024-06-25 18:25:10 -0700152 ceres::Solver::Options options;
Austin Schuh70cc9552019-01-21 19:46:48 -0800153 options.max_num_iterations = 25;
154 options.linear_solver_type = ceres::DENSE_QR;
155 options.minimizer_progress_to_stdout = true;
156
Austin Schuh3de38b02024-06-25 18:25:10 -0700157 ceres::Solver::Summary summary;
158 ceres::Solve(options, &problem, &summary);
Austin Schuh70cc9552019-01-21 19:46:48 -0800159 std::cout << summary.BriefReport() << "\n";
Austin Schuh3de38b02024-06-25 18:25:10 -0700160 std::cout << "Initial m: " << initial_m << " c: " << initial_c << "\n";
Austin Schuh70cc9552019-01-21 19:46:48 -0800161 std::cout << "Final m: " << m << " c: " << c << "\n";
162 return 0;
163}