blob: e08b0dfc3dc93ff13772c8f6be973239e4d73476 [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. However unlike
32// the data in curve_fitting.cc, the data here has outliers in it, so minimizing
33// the sum squared loss will result in a bad fit. So this example illustrates
34// the use of a robust loss function (CauchyLoss) to reduce the influence of the
35// outliers on the fit.
Austin Schuh70cc9552019-01-21 19:46:48 -080036
37#include "ceres/ceres.h"
38#include "glog/logging.h"
39
40// Data generated using the following octave code.
41// randn('seed', 23497);
42// m = 0.3;
43// c = 0.1;
44// x=[0:0.075:5];
45// y = exp(m * x + c);
46// noise = randn(size(x)) * 0.2;
47// outlier_noise = rand(size(x)) < 0.05;
48// y_observed = y + noise + outlier_noise;
49// data = [x', y_observed'];
50
51const int kNumObservations = 67;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080052// clang-format off
Austin Schuh70cc9552019-01-21 19:46:48 -080053const double data[] = {
540.000000e+00, 1.133898e+00,
557.500000e-02, 1.334902e+00,
561.500000e-01, 1.213546e+00,
572.250000e-01, 1.252016e+00,
583.000000e-01, 1.392265e+00,
593.750000e-01, 1.314458e+00,
604.500000e-01, 1.472541e+00,
615.250000e-01, 1.536218e+00,
626.000000e-01, 1.355679e+00,
636.750000e-01, 1.463566e+00,
647.500000e-01, 1.490201e+00,
658.250000e-01, 1.658699e+00,
669.000000e-01, 1.067574e+00,
679.750000e-01, 1.464629e+00,
681.050000e+00, 1.402653e+00,
691.125000e+00, 1.713141e+00,
701.200000e+00, 1.527021e+00,
711.275000e+00, 1.702632e+00,
721.350000e+00, 1.423899e+00,
731.425000e+00, 5.543078e+00, // Outlier point
741.500000e+00, 5.664015e+00, // Outlier point
751.575000e+00, 1.732484e+00,
761.650000e+00, 1.543296e+00,
771.725000e+00, 1.959523e+00,
781.800000e+00, 1.685132e+00,
791.875000e+00, 1.951791e+00,
801.950000e+00, 2.095346e+00,
812.025000e+00, 2.361460e+00,
822.100000e+00, 2.169119e+00,
832.175000e+00, 2.061745e+00,
842.250000e+00, 2.178641e+00,
852.325000e+00, 2.104346e+00,
862.400000e+00, 2.584470e+00,
872.475000e+00, 1.914158e+00,
882.550000e+00, 2.368375e+00,
892.625000e+00, 2.686125e+00,
902.700000e+00, 2.712395e+00,
912.775000e+00, 2.499511e+00,
922.850000e+00, 2.558897e+00,
932.925000e+00, 2.309154e+00,
943.000000e+00, 2.869503e+00,
953.075000e+00, 3.116645e+00,
963.150000e+00, 3.094907e+00,
973.225000e+00, 2.471759e+00,
983.300000e+00, 3.017131e+00,
993.375000e+00, 3.232381e+00,
1003.450000e+00, 2.944596e+00,
1013.525000e+00, 3.385343e+00,
1023.600000e+00, 3.199826e+00,
1033.675000e+00, 3.423039e+00,
1043.750000e+00, 3.621552e+00,
1053.825000e+00, 3.559255e+00,
1063.900000e+00, 3.530713e+00,
1073.975000e+00, 3.561766e+00,
1084.050000e+00, 3.544574e+00,
1094.125000e+00, 3.867945e+00,
1104.200000e+00, 4.049776e+00,
1114.275000e+00, 3.885601e+00,
1124.350000e+00, 4.110505e+00,
1134.425000e+00, 4.345320e+00,
1144.500000e+00, 4.161241e+00,
1154.575000e+00, 4.363407e+00,
1164.650000e+00, 4.161576e+00,
1174.725000e+00, 4.619728e+00,
1184.800000e+00, 4.737410e+00,
1194.875000e+00, 4.727863e+00,
1204.950000e+00, 4.669206e+00
121};
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800122// clang-format on
Austin Schuh70cc9552019-01-21 19:46:48 -0800123
Austin Schuh70cc9552019-01-21 19:46:48 -0800124struct ExponentialResidual {
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800125 ExponentialResidual(double x, double y) : x_(x), y_(y) {}
Austin Schuh70cc9552019-01-21 19:46:48 -0800126
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800127 template <typename T>
128 bool operator()(const T* const m, const T* const c, T* residual) const {
Austin Schuh70cc9552019-01-21 19:46:48 -0800129 residual[0] = y_ - exp(m[0] * x_ + c[0]);
130 return true;
131 }
132
133 private:
134 const double x_;
135 const double y_;
136};
137
138int main(int argc, char** argv) {
139 google::InitGoogleLogging(argv[0]);
140
Austin Schuh3de38b02024-06-25 18:25:10 -0700141 const double initial_m = 0.0;
142 const double initial_c = 0.0;
143 double m = initial_m;
144 double c = initial_c;
Austin Schuh70cc9552019-01-21 19:46:48 -0800145
Austin Schuh3de38b02024-06-25 18:25:10 -0700146 ceres::Problem problem;
Austin Schuh70cc9552019-01-21 19:46:48 -0800147 for (int i = 0; i < kNumObservations; ++i) {
Austin Schuh3de38b02024-06-25 18:25:10 -0700148 ceres::CostFunction* cost_function =
149 new ceres::AutoDiffCostFunction<ExponentialResidual, 1, 1, 1>(
150 data[2 * i], data[2 * i + 1]);
151 problem.AddResidualBlock(cost_function, new ceres::CauchyLoss(0.5), &m, &c);
Austin Schuh70cc9552019-01-21 19:46:48 -0800152 }
153
Austin Schuh3de38b02024-06-25 18:25:10 -0700154 ceres::Solver::Options options;
155 options.max_num_iterations = 25;
Austin Schuh70cc9552019-01-21 19:46:48 -0800156 options.linear_solver_type = ceres::DENSE_QR;
157 options.minimizer_progress_to_stdout = true;
158
Austin Schuh3de38b02024-06-25 18:25:10 -0700159 ceres::Solver::Summary summary;
160 ceres::Solve(options, &problem, &summary);
Austin Schuh70cc9552019-01-21 19:46:48 -0800161 std::cout << summary.BriefReport() << "\n";
Austin Schuh3de38b02024-06-25 18:25:10 -0700162 std::cout << "Initial m: " << initial_m << " c: " << initial_c << "\n";
Austin Schuh70cc9552019-01-21 19:46:48 -0800163 std::cout << "Final m: " << m << " c: " << c << "\n";
164 return 0;
165}