Blame internal/ceres/loss_function.cc

Packit ea1746
// Ceres Solver - A fast non-linear least squares minimizer
Packit ea1746
// Copyright 2015 Google Inc. All rights reserved.
Packit ea1746
// http://ceres-solver.org/
Packit ea1746
//
Packit ea1746
// Redistribution and use in source and binary forms, with or without
Packit ea1746
// modification, are permitted provided that the following conditions are met:
Packit ea1746
//
Packit ea1746
// * Redistributions of source code must retain the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer.
Packit ea1746
// * Redistributions in binary form must reproduce the above copyright notice,
Packit ea1746
//   this list of conditions and the following disclaimer in the documentation
Packit ea1746
//   and/or other materials provided with the distribution.
Packit ea1746
// * Neither the name of Google Inc. nor the names of its contributors may be
Packit ea1746
//   used to endorse or promote products derived from this software without
Packit ea1746
//   specific prior written permission.
Packit ea1746
//
Packit ea1746
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
Packit ea1746
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
Packit ea1746
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
Packit ea1746
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
Packit ea1746
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
Packit ea1746
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
Packit ea1746
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
Packit ea1746
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
Packit ea1746
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
Packit ea1746
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Packit ea1746
// POSSIBILITY OF SUCH DAMAGE.
Packit ea1746
//
Packit ea1746
// Author: sameeragarwal@google.com (Sameer Agarwal)
Packit ea1746
//
Packit ea1746
// Purpose: See .h file.
Packit ea1746
Packit ea1746
#include "ceres/loss_function.h"
Packit ea1746
Packit ea1746
#include <cmath>
Packit ea1746
#include <cstddef>
Packit ea1746
#include <limits>
Packit ea1746
Packit ea1746
namespace ceres {
Packit ea1746
Packit ea1746
void TrivialLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  rho[0] = s;
Packit ea1746
  rho[1] = 1.0;
Packit ea1746
  rho[2] = 0.0;
Packit ea1746
}
Packit ea1746
Packit ea1746
void HuberLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  if (s > b_) {
Packit ea1746
    // Outlier region.
Packit ea1746
    // 'r' is always positive.
Packit ea1746
    const double r = sqrt(s);
Packit ea1746
    rho[0] = 2.0 * a_ * r - b_;
Packit ea1746
    rho[1] = std::max(std::numeric_limits<double>::min(), a_ / r);
Packit ea1746
    rho[2] = - rho[1] / (2.0 * s);
Packit ea1746
  } else {
Packit ea1746
    // Inlier region.
Packit ea1746
    rho[0] = s;
Packit ea1746
    rho[1] = 1.0;
Packit ea1746
    rho[2] = 0.0;
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
void SoftLOneLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  const double sum = 1.0 + s * c_;
Packit ea1746
  const double tmp = sqrt(sum);
Packit ea1746
  // 'sum' and 'tmp' are always positive, assuming that 's' is.
Packit ea1746
  rho[0] = 2.0 * b_ * (tmp - 1.0);
Packit ea1746
  rho[1] = std::max(std::numeric_limits<double>::min(), 1.0 / tmp);
Packit ea1746
  rho[2] = - (c_ * rho[1]) / (2.0 * sum);
Packit ea1746
}
Packit ea1746
Packit ea1746
void CauchyLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  const double sum = 1.0 + s * c_;
Packit ea1746
  const double inv = 1.0 / sum;
Packit ea1746
  // 'sum' and 'inv' are always positive, assuming that 's' is.
Packit ea1746
  rho[0] = b_ * log(sum);
Packit ea1746
  rho[1] = std::max(std::numeric_limits<double>::min(), inv);
Packit ea1746
  rho[2] = - c_ * (inv * inv);
Packit ea1746
}
Packit ea1746
Packit ea1746
void ArctanLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  const double sum = 1 + s * s * b_;
Packit ea1746
  const double inv = 1 / sum;
Packit ea1746
  // 'sum' and 'inv' are always positive.
Packit ea1746
  rho[0] = a_ * atan2(s, a_);
Packit ea1746
  rho[1] = std::max(std::numeric_limits<double>::min(), inv);
Packit ea1746
  rho[2] = -2.0 * s * b_ * (inv * inv);
Packit ea1746
}
Packit ea1746
Packit ea1746
TolerantLoss::TolerantLoss(double a, double b)
Packit ea1746
    : a_(a),
Packit ea1746
      b_(b),
Packit ea1746
      c_(b * log(1.0 + exp(-a / b))) {
Packit ea1746
  CHECK_GE(a, 0.0);
Packit ea1746
  CHECK_GT(b, 0.0);
Packit ea1746
}
Packit ea1746
Packit ea1746
void TolerantLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  const double x = (s - a_) / b_;
Packit ea1746
  // The basic equation is rho[0] = b ln(1 + e^x).  However, if e^x is too
Packit ea1746
  // large, it will overflow.  Since numerically 1 + e^x == e^x when the
Packit ea1746
  // x is greater than about ln(2^53) for doubles, beyond this threshold
Packit ea1746
  // we substitute x for ln(1 + e^x) as a numerically equivalent approximation.
Packit ea1746
  static const double kLog2Pow53 = 36.7;  // ln(MathLimits<double>::kEpsilon).
Packit ea1746
  if (x > kLog2Pow53) {
Packit ea1746
    rho[0] = s - a_ - c_;
Packit ea1746
    rho[1] = 1.0;
Packit ea1746
    rho[2] = 0.0;
Packit ea1746
  } else {
Packit ea1746
    const double e_x = exp(x);
Packit ea1746
    rho[0] = b_ * log(1.0 + e_x) - c_;
Packit ea1746
    rho[1] = std::max(std::numeric_limits<double>::min(), e_x / (1.0 + e_x));
Packit ea1746
    rho[2] = 0.5 / (b_ * (1.0 + cosh(x)));
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
void TukeyLoss::Evaluate(double s, double* rho) const {
Packit ea1746
  if (s <= a_squared_) {
Packit ea1746
    // Inlier region.
Packit ea1746
    const double value = 1.0 - s / a_squared_;
Packit ea1746
    const double value_sq = value * value;
Packit ea1746
    rho[0] = a_squared_ / 6.0 * (1.0 - value_sq * value);
Packit ea1746
    rho[1] = 0.5 * value_sq;
Packit ea1746
    rho[2] = -1.0 / a_squared_ * value;
Packit ea1746
  } else {
Packit ea1746
    // Outlier region.
Packit ea1746
    rho[0] = a_squared_ / 6.0;
Packit ea1746
    rho[1] = 0.0;
Packit ea1746
    rho[2] = 0.0;
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
ComposedLoss::ComposedLoss(const LossFunction* f, Ownership ownership_f,
Packit ea1746
                           const LossFunction* g, Ownership ownership_g)
Packit ea1746
    : f_(CHECK_NOTNULL(f)),
Packit ea1746
      g_(CHECK_NOTNULL(g)),
Packit ea1746
      ownership_f_(ownership_f),
Packit ea1746
      ownership_g_(ownership_g) {
Packit ea1746
}
Packit ea1746
Packit ea1746
ComposedLoss::~ComposedLoss() {
Packit ea1746
  if (ownership_f_ == DO_NOT_TAKE_OWNERSHIP) {
Packit ea1746
    f_.release();
Packit ea1746
  }
Packit ea1746
  if (ownership_g_ == DO_NOT_TAKE_OWNERSHIP) {
Packit ea1746
    g_.release();
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
void ComposedLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  double rho_f[3], rho_g[3];
Packit ea1746
  g_->Evaluate(s, rho_g);
Packit ea1746
  f_->Evaluate(rho_g[0], rho_f);
Packit ea1746
  rho[0] = rho_f[0];
Packit ea1746
  // f'(g(s)) * g'(s).
Packit ea1746
  rho[1] = rho_f[1] * rho_g[1];
Packit ea1746
  // f''(g(s)) * g'(s) * g'(s) + f'(g(s)) * g''(s).
Packit ea1746
  rho[2] = rho_f[2] * rho_g[1] * rho_g[1] + rho_f[1] * rho_g[2];
Packit ea1746
}
Packit ea1746
Packit ea1746
void ScaledLoss::Evaluate(double s, double rho[3]) const {
Packit ea1746
  if (rho_.get() == NULL) {
Packit ea1746
    rho[0] = a_ * s;
Packit ea1746
    rho[1] = a_;
Packit ea1746
    rho[2] = 0.0;
Packit ea1746
  } else {
Packit ea1746
    rho_->Evaluate(s, rho);
Packit ea1746
    rho[0] *= a_;
Packit ea1746
    rho[1] *= a_;
Packit ea1746
    rho[2] *= a_;
Packit ea1746
  }
Packit ea1746
}
Packit ea1746
Packit ea1746
}  // namespace ceres