Austin Schuh | 70cc955 | 2019-01-21 19:46:48 -0800 | [diff] [blame^] | 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 <algorithm> |
| 36 | #include <cmath> |
| 37 | #include <cstddef> |
| 38 | #include <limits> |
| 39 | |
| 40 | namespace ceres { |
| 41 | |
| 42 | void TrivialLoss::Evaluate(double s, double rho[3]) const { |
| 43 | rho[0] = s; |
| 44 | rho[1] = 1.0; |
| 45 | rho[2] = 0.0; |
| 46 | } |
| 47 | |
| 48 | void HuberLoss::Evaluate(double s, double rho[3]) const { |
| 49 | if (s > b_) { |
| 50 | // Outlier region. |
| 51 | // 'r' is always positive. |
| 52 | const double r = sqrt(s); |
| 53 | rho[0] = 2.0 * a_ * r - b_; |
| 54 | rho[1] = std::max(std::numeric_limits<double>::min(), a_ / r); |
| 55 | rho[2] = - rho[1] / (2.0 * s); |
| 56 | } else { |
| 57 | // Inlier region. |
| 58 | rho[0] = s; |
| 59 | rho[1] = 1.0; |
| 60 | rho[2] = 0.0; |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | void SoftLOneLoss::Evaluate(double s, double rho[3]) const { |
| 65 | const double sum = 1.0 + s * c_; |
| 66 | const double tmp = sqrt(sum); |
| 67 | // 'sum' and 'tmp' are always positive, assuming that 's' is. |
| 68 | rho[0] = 2.0 * b_ * (tmp - 1.0); |
| 69 | rho[1] = std::max(std::numeric_limits<double>::min(), 1.0 / tmp); |
| 70 | rho[2] = - (c_ * rho[1]) / (2.0 * sum); |
| 71 | } |
| 72 | |
| 73 | void CauchyLoss::Evaluate(double s, double rho[3]) const { |
| 74 | const double sum = 1.0 + s * c_; |
| 75 | const double inv = 1.0 / sum; |
| 76 | // 'sum' and 'inv' are always positive, assuming that 's' is. |
| 77 | rho[0] = b_ * log(sum); |
| 78 | rho[1] = std::max(std::numeric_limits<double>::min(), inv); |
| 79 | rho[2] = - c_ * (inv * inv); |
| 80 | } |
| 81 | |
| 82 | void ArctanLoss::Evaluate(double s, double rho[3]) const { |
| 83 | const double sum = 1 + s * s * b_; |
| 84 | const double inv = 1 / sum; |
| 85 | // 'sum' and 'inv' are always positive. |
| 86 | rho[0] = a_ * atan2(s, a_); |
| 87 | rho[1] = std::max(std::numeric_limits<double>::min(), inv); |
| 88 | rho[2] = -2.0 * s * b_ * (inv * inv); |
| 89 | } |
| 90 | |
| 91 | TolerantLoss::TolerantLoss(double a, double b) |
| 92 | : a_(a), |
| 93 | b_(b), |
| 94 | c_(b * log(1.0 + exp(-a / b))) { |
| 95 | CHECK_GE(a, 0.0); |
| 96 | CHECK_GT(b, 0.0); |
| 97 | } |
| 98 | |
| 99 | void 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. |
| 105 | static const double kLog2Pow53 = 36.7; // ln(MathLimits<double>::kEpsilon). |
| 106 | if (x > kLog2Pow53) { |
| 107 | rho[0] = s - a_ - c_; |
| 108 | rho[1] = 1.0; |
| 109 | rho[2] = 0.0; |
| 110 | } else { |
| 111 | const double e_x = exp(x); |
| 112 | rho[0] = b_ * log(1.0 + e_x) - c_; |
| 113 | rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x)); |
| 114 | rho[2] = 0.5 / (b_ * (1.0 + cosh(x))); |
| 115 | } |
| 116 | } |
| 117 | |
| 118 | void TukeyLoss::Evaluate(double s, double* rho) const { |
| 119 | if (s <= a_squared_) { |
| 120 | // Inlier region. |
| 121 | const double value = 1.0 - s / a_squared_; |
| 122 | const double value_sq = value * value; |
| 123 | rho[0] = a_squared_ / 6.0 * (1.0 - value_sq * value); |
| 124 | rho[1] = 0.5 * value_sq; |
| 125 | rho[2] = -1.0 / a_squared_ * value; |
| 126 | } else { |
| 127 | // Outlier region. |
| 128 | rho[0] = a_squared_ / 6.0; |
| 129 | rho[1] = 0.0; |
| 130 | rho[2] = 0.0; |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | ComposedLoss::ComposedLoss(const LossFunction* f, Ownership ownership_f, |
| 135 | const LossFunction* g, Ownership ownership_g) |
| 136 | : f_(f), |
| 137 | g_(g), |
| 138 | ownership_f_(ownership_f), |
| 139 | ownership_g_(ownership_g) { |
| 140 | CHECK(f_ != nullptr); |
| 141 | CHECK(g_ != nullptr); |
| 142 | } |
| 143 | |
| 144 | ComposedLoss::~ComposedLoss() { |
| 145 | if (ownership_f_ == DO_NOT_TAKE_OWNERSHIP) { |
| 146 | f_.release(); |
| 147 | } |
| 148 | if (ownership_g_ == DO_NOT_TAKE_OWNERSHIP) { |
| 149 | g_.release(); |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | void ComposedLoss::Evaluate(double s, double rho[3]) const { |
| 154 | double rho_f[3], rho_g[3]; |
| 155 | g_->Evaluate(s, rho_g); |
| 156 | f_->Evaluate(rho_g[0], rho_f); |
| 157 | rho[0] = rho_f[0]; |
| 158 | // f'(g(s)) * g'(s). |
| 159 | rho[1] = rho_f[1] * rho_g[1]; |
| 160 | // f''(g(s)) * g'(s) * g'(s) + f'(g(s)) * g''(s). |
| 161 | rho[2] = rho_f[2] * rho_g[1] * rho_g[1] + rho_f[1] * rho_g[2]; |
| 162 | } |
| 163 | |
| 164 | void ScaledLoss::Evaluate(double s, double rho[3]) const { |
| 165 | if (rho_.get() == NULL) { |
| 166 | rho[0] = a_ * s; |
| 167 | rho[1] = a_; |
| 168 | rho[2] = 0.0; |
| 169 | } else { |
| 170 | rho_->Evaluate(s, rho); |
| 171 | rho[0] *= a_; |
| 172 | rho[1] *= a_; |
| 173 | rho[2] *= a_; |
| 174 | } |
| 175 | } |
| 176 | |
| 177 | } // namespace ceres |