blob: 82563c820ef439867f917983e870f47ac0532cb2 [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)
30//
31// Purpose: See .h file.
32
33#include "ceres/loss_function.h"
34
35#include <algorithm>
36#include <cmath>
37#include <cstddef>
38#include <limits>
39
40namespace ceres {
41
Austin Schuh3de38b02024-06-25 18:25:10 -070042LossFunction::~LossFunction() = default;
43
Austin Schuh70cc9552019-01-21 19:46:48 -080044void TrivialLoss::Evaluate(double s, double rho[3]) const {
45 rho[0] = s;
46 rho[1] = 1.0;
47 rho[2] = 0.0;
48}
49
50void HuberLoss::Evaluate(double s, double rho[3]) const {
51 if (s > b_) {
52 // Outlier region.
53 // 'r' is always positive.
54 const double r = sqrt(s);
55 rho[0] = 2.0 * a_ * r - b_;
56 rho[1] = std::max(std::numeric_limits<double>::min(), a_ / r);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080057 rho[2] = -rho[1] / (2.0 * s);
Austin Schuh70cc9552019-01-21 19:46:48 -080058 } else {
59 // Inlier region.
60 rho[0] = s;
61 rho[1] = 1.0;
62 rho[2] = 0.0;
63 }
64}
65
66void SoftLOneLoss::Evaluate(double s, double rho[3]) const {
67 const double sum = 1.0 + s * c_;
68 const double tmp = sqrt(sum);
69 // 'sum' and 'tmp' are always positive, assuming that 's' is.
70 rho[0] = 2.0 * b_ * (tmp - 1.0);
71 rho[1] = std::max(std::numeric_limits<double>::min(), 1.0 / tmp);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080072 rho[2] = -(c_ * rho[1]) / (2.0 * sum);
Austin Schuh70cc9552019-01-21 19:46:48 -080073}
74
75void CauchyLoss::Evaluate(double s, double rho[3]) const {
76 const double sum = 1.0 + s * c_;
77 const double inv = 1.0 / sum;
78 // 'sum' and 'inv' are always positive, assuming that 's' is.
79 rho[0] = b_ * log(sum);
80 rho[1] = std::max(std::numeric_limits<double>::min(), inv);
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080081 rho[2] = -c_ * (inv * inv);
Austin Schuh70cc9552019-01-21 19:46:48 -080082}
83
84void ArctanLoss::Evaluate(double s, double rho[3]) const {
85 const double sum = 1 + s * s * b_;
86 const double inv = 1 / sum;
87 // 'sum' and 'inv' are always positive.
88 rho[0] = a_ * atan2(s, a_);
89 rho[1] = std::max(std::numeric_limits<double>::min(), inv);
90 rho[2] = -2.0 * s * b_ * (inv * inv);
91}
92
93TolerantLoss::TolerantLoss(double a, double b)
Austin Schuh1d1e6ea2020-12-23 21:56:30 -080094 : a_(a), b_(b), c_(b * log(1.0 + exp(-a / b))) {
Austin Schuh70cc9552019-01-21 19:46:48 -080095 CHECK_GE(a, 0.0);
96 CHECK_GT(b, 0.0);
97}
98
99void TolerantLoss::Evaluate(double s, double rho[3]) const {
100 const double x = (s - a_) / b_;
101 // The basic equation is rho[0] = b ln(1 + e^x). However, if e^x is too
102 // large, it will overflow. Since numerically 1 + e^x == e^x when the
103 // x is greater than about ln(2^53) for doubles, beyond this threshold
104 // we substitute x for ln(1 + e^x) as a numerically equivalent approximation.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800105
106 // ln(MathLimits<double>::kEpsilon).
107 static constexpr double kLog2Pow53 = 36.7;
Austin Schuh70cc9552019-01-21 19:46:48 -0800108 if (x > kLog2Pow53) {
109 rho[0] = s - a_ - c_;
110 rho[1] = 1.0;
111 rho[2] = 0.0;
112 } else {
113 const double e_x = exp(x);
114 rho[0] = b_ * log(1.0 + e_x) - c_;
115 rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x));
116 rho[2] = 0.5 / (b_ * (1.0 + cosh(x)));
117 }
118}
119
120void TukeyLoss::Evaluate(double s, double* rho) const {
121 if (s <= a_squared_) {
122 // Inlier region.
123 const double value = 1.0 - s / a_squared_;
124 const double value_sq = value * value;
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800125 rho[0] = a_squared_ / 3.0 * (1.0 - value_sq * value);
126 rho[1] = value_sq;
127 rho[2] = -2.0 / a_squared_ * value;
Austin Schuh70cc9552019-01-21 19:46:48 -0800128 } else {
129 // Outlier region.
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800130 rho[0] = a_squared_ / 3.0;
Austin Schuh70cc9552019-01-21 19:46:48 -0800131 rho[1] = 0.0;
132 rho[2] = 0.0;
133 }
134}
135
Austin Schuh1d1e6ea2020-12-23 21:56:30 -0800136ComposedLoss::ComposedLoss(const LossFunction* f,
137 Ownership ownership_f,
138 const LossFunction* g,
139 Ownership ownership_g)
140 : f_(f), g_(g), ownership_f_(ownership_f), ownership_g_(ownership_g) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800141 CHECK(f_ != nullptr);
142 CHECK(g_ != nullptr);
143}
144
145ComposedLoss::~ComposedLoss() {
146 if (ownership_f_ == DO_NOT_TAKE_OWNERSHIP) {
147 f_.release();
148 }
149 if (ownership_g_ == DO_NOT_TAKE_OWNERSHIP) {
150 g_.release();
151 }
152}
153
154void ComposedLoss::Evaluate(double s, double rho[3]) const {
155 double rho_f[3], rho_g[3];
156 g_->Evaluate(s, rho_g);
157 f_->Evaluate(rho_g[0], rho_f);
158 rho[0] = rho_f[0];
159 // f'(g(s)) * g'(s).
160 rho[1] = rho_f[1] * rho_g[1];
161 // f''(g(s)) * g'(s) * g'(s) + f'(g(s)) * g''(s).
162 rho[2] = rho_f[2] * rho_g[1] * rho_g[1] + rho_f[1] * rho_g[2];
163}
164
165void ScaledLoss::Evaluate(double s, double rho[3]) const {
Austin Schuh3de38b02024-06-25 18:25:10 -0700166 if (rho_.get() == nullptr) {
Austin Schuh70cc9552019-01-21 19:46:48 -0800167 rho[0] = a_ * s;
168 rho[1] = a_;
169 rho[2] = 0.0;
170 } else {
171 rho_->Evaluate(s, rho);
172 rho[0] *= a_;
173 rho[1] *= a_;
174 rho[2] *= a_;
175 }
176}
177
178} // namespace ceres