blob: eb5026784ddff0806d17d34a42d344a84dc5176f [file] [log] [blame]
Keir Mierle8ebb0732012-04-30 23:09:08 -07001// Ceres Solver - A fast non-linear least squares minimizer
Keir Mierle7492b0d2015-03-17 22:30:16 -07002// Copyright 2015 Google Inc. All rights reserved.
3// http://ceres-solver.org/
Keir Mierle8ebb0732012-04-30 23:09:08 -07004//
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 <cmath>
36#include <cstddef>
Alex Stewart54893ba2014-08-11 19:04:18 +010037#include <limits>
Keir Mierle8ebb0732012-04-30 23:09:08 -070038
39namespace ceres {
40
41void TrivialLoss::Evaluate(double s, double rho[3]) const {
42 rho[0] = s;
Sameer Agarwal1b171452014-07-30 10:14:15 -070043 rho[1] = 1.0;
44 rho[2] = 0.0;
Keir Mierle8ebb0732012-04-30 23:09:08 -070045}
46
47void HuberLoss::Evaluate(double s, double rho[3]) const {
48 if (s > b_) {
49 // Outlier region.
50 // 'r' is always positive.
51 const double r = sqrt(s);
Sameer Agarwal1b171452014-07-30 10:14:15 -070052 rho[0] = 2.0 * a_ * r - b_;
53 rho[1] = std::max(std::numeric_limits<double>::min(), a_ / r);
54 rho[2] = - rho[1] / (2.0 * s);
Keir Mierle8ebb0732012-04-30 23:09:08 -070055 } else {
56 // Inlier region.
57 rho[0] = s;
Sameer Agarwal1b171452014-07-30 10:14:15 -070058 rho[1] = 1.0;
59 rho[2] = 0.0;
Keir Mierle8ebb0732012-04-30 23:09:08 -070060 }
61}
62
63void SoftLOneLoss::Evaluate(double s, double rho[3]) const {
Sameer Agarwal1b171452014-07-30 10:14:15 -070064 const double sum = 1.0 + s * c_;
Keir Mierle8ebb0732012-04-30 23:09:08 -070065 const double tmp = sqrt(sum);
66 // 'sum' and 'tmp' are always positive, assuming that 's' is.
Sameer Agarwal1b171452014-07-30 10:14:15 -070067 rho[0] = 2.0 * b_ * (tmp - 1.0);
68 rho[1] = std::max(std::numeric_limits<double>::min(), 1.0 / tmp);
69 rho[2] = - (c_ * rho[1]) / (2.0 * sum);
Keir Mierle8ebb0732012-04-30 23:09:08 -070070}
71
72void CauchyLoss::Evaluate(double s, double rho[3]) const {
Sameer Agarwal1b171452014-07-30 10:14:15 -070073 const double sum = 1.0 + s * c_;
74 const double inv = 1.0 / sum;
Keir Mierle8ebb0732012-04-30 23:09:08 -070075 // 'sum' and 'inv' are always positive, assuming that 's' is.
76 rho[0] = b_ * log(sum);
Sameer Agarwal1b171452014-07-30 10:14:15 -070077 rho[1] = std::max(std::numeric_limits<double>::min(), inv);
Keir Mierle8ebb0732012-04-30 23:09:08 -070078 rho[2] = - c_ * (inv * inv);
79}
80
Sameer Agarwalad1f7b72012-08-20 11:10:34 -070081void ArctanLoss::Evaluate(double s, double rho[3]) const {
82 const double sum = 1 + s * s * b_;
83 const double inv = 1 / sum;
84 // 'sum' and 'inv' are always positive.
85 rho[0] = a_ * atan2(s, a_);
Sameer Agarwal1b171452014-07-30 10:14:15 -070086 rho[1] = std::max(std::numeric_limits<double>::min(), inv);
87 rho[2] = -2.0 * s * b_ * (inv * inv);
Sameer Agarwalad1f7b72012-08-20 11:10:34 -070088}
89
90TolerantLoss::TolerantLoss(double a, double b)
91 : a_(a),
92 b_(b),
93 c_(b * log(1.0 + exp(-a / b))) {
94 CHECK_GE(a, 0.0);
95 CHECK_GT(b, 0.0);
96}
97
98void TolerantLoss::Evaluate(double s, double rho[3]) const {
99 const double x = (s - a_) / b_;
100 // The basic equation is rho[0] = b ln(1 + e^x). However, if e^x is too
101 // large, it will overflow. Since numerically 1 + e^x == e^x when the
102 // x is greater than about ln(2^53) for doubles, beyond this threshold
103 // we substitute x for ln(1 + e^x) as a numerically equivalent approximation.
104 static const double kLog2Pow53 = 36.7; // ln(MathLimits<double>::kEpsilon).
105 if (x > kLog2Pow53) {
106 rho[0] = s - a_ - c_;
107 rho[1] = 1.0;
108 rho[2] = 0.0;
109 } else {
110 const double e_x = exp(x);
111 rho[0] = b_ * log(1.0 + e_x) - c_;
Sameer Agarwal1b171452014-07-30 10:14:15 -0700112 rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x));
Sameer Agarwalad1f7b72012-08-20 11:10:34 -0700113 rho[2] = 0.5 / (b_ * (1.0 + cosh(x)));
114 }
115}
116
Mike Vitus7c0ac8f2014-11-13 14:46:34 -0800117void TukeyLoss::Evaluate(double s, double* rho) const {
118 if (s <= a_squared_) {
119 // Inlier region.
120 const double value = 1.0 - s / a_squared_;
121 const double value_sq = value * value;
122 rho[0] = a_squared_ / 6.0 * (1.0 - value_sq * value);
123 rho[1] = 0.5 * value_sq;
124 rho[2] = -1.0 / a_squared_ * value;
125 } else {
126 // Outlier region.
127 rho[0] = a_squared_ / 6.0;
128 rho[1] = 0.0;
129 rho[2] = 0.0;
130 }
131}
132
Sameer Agarwalad1f7b72012-08-20 11:10:34 -0700133ComposedLoss::ComposedLoss(const LossFunction* f, Ownership ownership_f,
134 const LossFunction* g, Ownership ownership_g)
135 : f_(CHECK_NOTNULL(f)),
136 g_(CHECK_NOTNULL(g)),
137 ownership_f_(ownership_f),
138 ownership_g_(ownership_g) {
139}
140
141ComposedLoss::~ComposedLoss() {
142 if (ownership_f_ == DO_NOT_TAKE_OWNERSHIP) {
143 f_.release();
144 }
145 if (ownership_g_ == DO_NOT_TAKE_OWNERSHIP) {
146 g_.release();
147 }
148}
149
150void ComposedLoss::Evaluate(double s, double rho[3]) const {
151 double rho_f[3], rho_g[3];
152 g_->Evaluate(s, rho_g);
153 f_->Evaluate(rho_g[0], rho_f);
154 rho[0] = rho_f[0];
155 // f'(g(s)) * g'(s).
156 rho[1] = rho_f[1] * rho_g[1];
157 // f''(g(s)) * g'(s) * g'(s) + f'(g(s)) * g''(s).
158 rho[2] = rho_f[2] * rho_g[1] * rho_g[1] + rho_f[1] * rho_g[2];
159}
160
Keir Mierle8ebb0732012-04-30 23:09:08 -0700161void ScaledLoss::Evaluate(double s, double rho[3]) const {
162 if (rho_.get() == NULL) {
163 rho[0] = a_ * s;
164 rho[1] = a_;
165 rho[2] = 0.0;
166 } else {
167 rho_->Evaluate(s, rho);
168 rho[0] *= a_;
169 rho[1] *= a_;
170 rho[2] *= a_;
171 }
172}
173
174} // namespace ceres