Imported Upstream version ceres 1.13.0
[platform/upstream/ceres-solver.git] / internal / ceres / loss_function.cc
1 // 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 // Purpose: See .h file.
32
33 #include "ceres/loss_function.h"
34
35 #include <cmath>
36 #include <cstddef>
37 #include <limits>
38
39 namespace ceres {
40
41 void TrivialLoss::Evaluate(double s, double rho[3]) const {
42   rho[0] = s;
43   rho[1] = 1.0;
44   rho[2] = 0.0;
45 }
46
47 void 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);
52     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);
55   } else {
56     // Inlier region.
57     rho[0] = s;
58     rho[1] = 1.0;
59     rho[2] = 0.0;
60   }
61 }
62
63 void SoftLOneLoss::Evaluate(double s, double rho[3]) const {
64   const double sum = 1.0 + s * c_;
65   const double tmp = sqrt(sum);
66   // 'sum' and 'tmp' are always positive, assuming that 's' is.
67   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);
70 }
71
72 void CauchyLoss::Evaluate(double s, double rho[3]) const {
73   const double sum = 1.0 + s * c_;
74   const double inv = 1.0 / sum;
75   // 'sum' and 'inv' are always positive, assuming that 's' is.
76   rho[0] = b_ * log(sum);
77   rho[1] = std::max(std::numeric_limits<double>::min(), inv);
78   rho[2] = - c_ * (inv * inv);
79 }
80
81 void 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_);
86   rho[1] = std::max(std::numeric_limits<double>::min(), inv);
87   rho[2] = -2.0 * s * b_ * (inv * inv);
88 }
89
90 TolerantLoss::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
98 void 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_;
112     rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x));
113     rho[2] = 0.5 / (b_ * (1.0 + cosh(x)));
114   }
115 }
116
117 void 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
133 ComposedLoss::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
141 ComposedLoss::~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
150 void 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
161 void 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