Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Enable GNC Optimizer #1572

Merged
merged 35 commits into from
Dec 29, 2023
Merged
Show file tree
Hide file tree
Changes from 20 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
d0b3f1d
code for computing quantile of chi-squared distribution
varunagrawal May 10, 2023
8201c77
refactor IncompleteGamma class
varunagrawal May 10, 2023
d577160
Simplified IncompleteGamma
varunagrawal May 10, 2023
7ce5684
remove recursion for Halley update
varunagrawal May 10, 2023
a807127
update GncOptimizer and make it available
varunagrawal May 11, 2023
6fb3f0f
use templated is_nan check
varunagrawal May 11, 2023
d614fda
try older version
varunagrawal May 11, 2023
64c2850
switch from IndexVector to FastVector<size_t> now that pybind/stl.h i…
varunagrawal Jul 10, 2023
a5fd9c1
fix chi_squared_quantile
varunagrawal Jul 10, 2023
3641dd1
Merge branch 'develop' into chi-squared-quantile
varunagrawal Jul 10, 2023
00f5596
Revert "switch from IndexVector to FastVector<size_t> now that pybind…
varunagrawal Jul 10, 2023
7c935d9
small update to GNC IndexVector
varunagrawal Jul 10, 2023
1d9b83b
Merge branch 'develop' into chi-squared-quantile
varunagrawal Oct 18, 2023
dcb4299
rename GncHelpers to chiSquaredInverse and move to internal directory
varunagrawal Oct 19, 2023
e8817ae
rename other files accordingly
varunagrawal Oct 19, 2023
a46a78d
update paths
varunagrawal Oct 19, 2023
c8a0cdc
much improved chi_squared_quantile, incremental update
varunagrawal Oct 20, 2023
bebb275
compute initial guess for inverse gamma value
varunagrawal Oct 20, 2023
6f38616
gamma inverse functional
varunagrawal Oct 20, 2023
25ebdd5
add gamma_p_inverse_func
varunagrawal Oct 22, 2023
203a84d
add more gamma functions
varunagrawal Dec 27, 2023
87c5729
more code
varunagrawal Dec 27, 2023
4326195
cephes source code
varunagrawal Dec 28, 2023
3538488
refactor Cephes CMakeLists.txt to allow building from parent directory
varunagrawal Dec 28, 2023
5481159
Link to cephes from gtsam
varunagrawal Dec 28, 2023
ea81675
minor refactor to be consistent
varunagrawal Dec 28, 2023
3cde40d
OS-based improved support
varunagrawal Dec 28, 2023
5806f5f
add M_PI definition if unavailable (e.g. in Windows)
varunagrawal Dec 28, 2023
8db9e01
additional M_PI definitions
varunagrawal Dec 28, 2023
b1ce501
hopefully last of M_ definitions
varunagrawal Dec 28, 2023
70f1d4a
mark GTSAM_EXPORT and update docstring
varunagrawal Dec 28, 2023
98444ab
another windows fix
varunagrawal Dec 28, 2023
e0b8c52
kill testChiSquaredInverse
varunagrawal Dec 28, 2023
ba93dec
only used built in version of Cephes since there doesn't seem to be a…
varunagrawal Dec 29, 2023
687667a
minor formatting
varunagrawal Dec 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion gtsam/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,6 @@ endif()
# if GTSAM_USE_BOOST_FEATURES is not set, then we need to exclude the following:
if(NOT GTSAM_USE_BOOST_FEATURES)
list (APPEND excluded_sources
"${CMAKE_CURRENT_SOURCE_DIR}/nonlinear/GncOptimizer.h"
"${CMAKE_CURRENT_SOURCE_DIR}/inference/graph.h"
"${CMAKE_CURRENT_SOURCE_DIR}/inference/graph-inl.h"
)
Expand Down
5 changes: 2 additions & 3 deletions gtsam/nonlinear/GncOptimizer.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,16 +28,15 @@

#include <gtsam/nonlinear/GncParams.h>
#include <gtsam/nonlinear/NonlinearFactorGraph.h>
#include <boost/math/distributions/chi_squared.hpp>
#include <gtsam/nonlinear/internal/chiSquaredInverse.h>

namespace gtsam {
/*
* Quantile of chi-squared distribution with given degrees of freedom at probability alpha.
* Equivalent to chi2inv in Matlab.
*/
static double Chi2inv(const double alpha, const size_t dofs) {
boost::math::chi_squared_distribution<double> chi2(dofs);
return boost::math::quantile(chi2, alpha);
return internal::chi_squared_quantile(dofs, alpha);
}

/* ************************************************************************* */
Expand Down
4 changes: 2 additions & 2 deletions gtsam/nonlinear/GncParams.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ class GncParams {
/// Use IndexVector for inliers and outliers since it is fast
using IndexVector = FastVector<uint64_t>;
///< Slots in the factor graph corresponding to measurements that we know are inliers
IndexVector knownInliers = IndexVector();
IndexVector knownInliers;
///< Slots in the factor graph corresponding to measurements that we know are outliers
IndexVector knownOutliers = IndexVector();
IndexVector knownOutliers;

/// Set the robust loss function to be used in GNC (chosen among the ones in GncLossType).
void setLossType(const GncLossType type) {
Expand Down
94 changes: 94 additions & 0 deletions gtsam/nonlinear/internal/Gamma.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
/* ----------------------------------------------------------------------------

* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)

* See LICENSE for the license information

* -------------------------------------------------------------------------- */

/**
* @file Gamma.h
* @brief Gamma and Gamma Inverse functions
*
* A lot of this code has been picked up from
* https://www.boost.org/doc/libs/1_83_0/boost/math/special_functions/detail/igamma_inverse.hpp
*
* @author Varun Agrawal
*/

#pragma once

#include <gtsam/nonlinear/internal/Utils.h>

#include <boost/math/special_functions/gamma.hpp>

namespace gtsam {

namespace internal {

/**
* @brief Functional to compute the gamma inverse.
* Mainly used with Halley iteration.
*
* @tparam T
*/
template <class T>
struct gamma_p_inverse_func {
gamma_p_inverse_func(T a_, T p_, bool inv) : a(a_), p(p_), invert(inv) {
/*
If p is too near 1 then P(x) - p suffers from cancellation
errors causing our root-finding algorithms to "thrash", better
to invert in this case and calculate Q(x) - (1-p) instead.

Of course if p is *very* close to 1, then the answer we get will
be inaccurate anyway (because there's not enough information in p)
but at least we will converge on the (inaccurate) answer quickly.
*/
if (p > T(0.9)) {
p = 1 - p;
invert = !invert;
}
}

std::tuple<T, T, T> operator()(const T& x) const {
// Calculate P(x) - p and the first two derivates, or if the invert
// flag is set, then Q(x) - q and it's derivatives.
T f, f1;
T ft;
boost::math::policies::policy<> pol;
f = static_cast<T>(boost::math::detail::gamma_incomplete_imp(
a, x, true, invert, pol, &ft));
f1 = ft;
T f2;
T div = (a - x - 1) / x;
f2 = f1;

if (fabs(div) > 1) {
if (internal::LIM<T>::max() / fabs(div) < f2) {
// overflow:
f2 = -internal::LIM<T>::max() / 2;
} else {
f2 *= div;
}
} else {
f2 *= div;
}

if (invert) {
f1 = -f1;
f2 = -f2;
}

return std::make_tuple(static_cast<T>(f - p), f1, f2);
}

private:
T a, p;
bool invert;
};

} // namespace internal
} // namespace gtsam
49 changes: 49 additions & 0 deletions gtsam/nonlinear/internal/Utils.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/* ----------------------------------------------------------------------------

* GTSAM Copyright 2010, Georgia Tech Research Corporation,
* Atlanta, Georgia 30332-0415
* All Rights Reserved
* Authors: Frank Dellaert, et al. (see THANKS for the full author list)

* See LICENSE for the license information

* -------------------------------------------------------------------------- */

/**
* @file Utils.h
* @brief Utilities for the Chi Squared inverse and related operations.
* @author Varun Agrawal
*/

#pragma once

namespace gtsam {
namespace internal {

/// Template type for numeric limits
template <class T>
using LIM = std::numeric_limits<T>;

template <typename T>
using return_t =
typename std::conditional<std::is_integral<T>::value, double, T>::type;

/// Get common type amongst all arguments
template <typename... T>
using common_t = typename std::common_type<T...>::type;

/// Helper template for finding common return type
template <typename... T>
using common_return_t = return_t<common_t<T...>>;

/// Check if integer is odd
constexpr bool is_odd(const long long int x) noexcept { return (x & 1U) != 0; }

/// Templated check for NaN
template <typename T>
constexpr bool is_nan(const T x) noexcept {
return x != x;
}

} // namespace internal
} // namespace gtsam
Loading