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

AutoDiff Attempt #360

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,6 @@
[submodule "third_party/nlopt"]
path = third_party/nlopt
url = https://github.com/swift-nav/nlopt.git
[submodule "third_party/autodiff"]
path = third_party/autodiff
url = [email protected]:autodiff/autodiff.git
5 changes: 5 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ swift_create_project_options(
TEST_PACKAGES "Googletest" "GFlags"
)

include(LanguageStandards)

include(ClangFormat)
swift_setup_clang_format()

Expand All @@ -27,6 +29,7 @@ find_package(FastCSV REQUIRED)
find_package(Gzip-Hpp REQUIRED)
find_package(Variant REQUIRED)
find_package(Nlopt REQUIRED)
find_package(Autodiff REQUIRED)

add_library(albatross INTERFACE)
target_include_directories(albatross INTERFACE
Expand All @@ -40,9 +43,11 @@ target_link_libraries(albatross
fast-csv
gzip-hpp
variant
autodiff
)

set(albatross_COMPILE_OPTIONS
-std=c++17
-Werror
-Wall
-Wno-unused-value
Expand Down
1 change: 1 addition & 0 deletions include/albatross/Common
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <Eigen/Dense>

#include <mapbox/variant.hpp>
#include <autodiff/reverse/var.hpp>

#include <math.h>
#include <iomanip>
Expand Down
3 changes: 2 additions & 1 deletion include/albatross/src/core/declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,10 @@ using ParameterKey = std::string;
// a corresponding cereal type included or you'll get some
// really impressive compilation errors.
using ParameterPrior = PriorContainer;
using ParameterValue = double;
using ParameterValue = autodiff::var;

using ParameterStore = std::map<ParameterKey, Parameter>;
using ParameterConstReferences = std::map<ParameterKey, const Parameter &>;

/*
* Distributions
Expand Down
78 changes: 38 additions & 40 deletions include/albatross/src/covariance_functions/callers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,15 +120,15 @@ struct DirectCaller {
template <typename CovFunc, typename X, typename Y,
typename std::enable_if<has_valid_call_impl<CovFunc, X, Y>::value,
int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return cov_func._call_impl(x, y);
}

// Mean Functions
template <typename MeanFunc, typename X,
typename std::enable_if<has_valid_call_impl<MeanFunc, X>::value,
int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
return mean_func._call_impl(x);
}
};
Expand All @@ -146,7 +146,7 @@ template <typename SubCaller> struct SymmetricCaller {
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return SubCaller::call(cov_func, x, y);
}

Expand All @@ -156,7 +156,7 @@ template <typename SubCaller> struct SymmetricCaller {
(has_valid_cov_caller<CovFunc, SubCaller, Y, X>::value &&
!has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value),
int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return SubCaller::call(cov_func, y, x);
}

Expand All @@ -165,7 +165,7 @@ template <typename SubCaller> struct SymmetricCaller {
typename MeanFunc, typename X,
typename std::enable_if<
has_valid_mean_caller<MeanFunc, SubCaller, X>::value, int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
return SubCaller::call(mean_func, x);
}
};
Expand All @@ -185,7 +185,7 @@ template <typename SubCaller> struct MeasurementForwarder {
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return SubCaller::call(cov_func, x, y);
}

Expand All @@ -195,8 +195,8 @@ template <typename SubCaller> struct MeasurementForwarder {
!has_valid_cov_caller<CovFunc, SubCaller, Measurement<X>,
Measurement<Y>>::value),
int>::type = 0>
static double call(const CovFunc &cov_func, const Measurement<X> &x,
const Measurement<Y> &y) {
static auto call(const CovFunc &cov_func, const Measurement<X> &x,
const Measurement<Y> &y) {
return SubCaller::call(cov_func, x.value, y.value);
}

Expand All @@ -206,8 +206,8 @@ template <typename SubCaller> struct MeasurementForwarder {
(has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value &&
!has_valid_cov_caller<CovFunc, SubCaller, Measurement<X>, Y>::value),
int>::type = 0>
static double call(const CovFunc &cov_func, const Measurement<X> &x,
const Y &y) {
static auto call(const CovFunc &cov_func, const Measurement<X> &x,
const Y &y) {
return SubCaller::call(cov_func, x.value, y);
}

Expand All @@ -217,8 +217,8 @@ template <typename SubCaller> struct MeasurementForwarder {
(has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value &&
!has_valid_cov_caller<CovFunc, SubCaller, X, Measurement<Y>>::value),
int>::type = 0>
static double call(const CovFunc &cov_func, const X &x,
const Measurement<Y> &y) {
static auto call(const CovFunc &cov_func, const X &x,
const Measurement<Y> &y) {
return SubCaller::call(cov_func, x, y.value);
}

Expand All @@ -227,7 +227,7 @@ template <typename SubCaller> struct MeasurementForwarder {
typename MeanFunc, typename X,
typename std::enable_if<
has_valid_mean_caller<MeanFunc, SubCaller, X>::value, int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
return SubCaller::call(mean_func, x);
}

Expand All @@ -237,7 +237,7 @@ template <typename SubCaller> struct MeasurementForwarder {
!has_valid_mean_caller<MeanFunc, SubCaller,
Measurement<X>>::value,
int>::type = 0>
static double call(const MeanFunc &mean_func, const Measurement<X> &x) {
static auto call(const MeanFunc &mean_func, const Measurement<X> &x) {
return SubCaller::call(mean_func, x.value);
}
};
Expand All @@ -249,16 +249,16 @@ template <typename SubCaller> struct LinearCombinationCaller {
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return SubCaller::call(cov_func, x, y);
}

template <
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const LinearCombination<X> &xs,
const LinearCombination<Y> &ys) {
static auto call(const CovFunc &cov_func, const LinearCombination<X> &xs,
const LinearCombination<Y> &ys) {

auto sub_caller = [&](const auto &x, const auto &y) {
return SubCaller::call(cov_func, x, y);
Expand All @@ -273,8 +273,8 @@ template <typename SubCaller> struct LinearCombinationCaller {
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const X &x,
const LinearCombination<Y> &ys) {
static auto call(const CovFunc &cov_func, const X &x,
const LinearCombination<Y> &ys) {
double sum = 0.;
for (std::size_t i = 0; i < ys.values.size(); ++i) {
sum += ys.coefficients[i] * SubCaller::call(cov_func, x, ys.values[i]);
Expand All @@ -286,8 +286,8 @@ template <typename SubCaller> struct LinearCombinationCaller {
typename CovFunc, typename X, typename Y,
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value, int>::type = 0>
static double call(const CovFunc &cov_func, const LinearCombination<X> &xs,
const Y &y) {
static auto call(const CovFunc &cov_func, const LinearCombination<X> &xs,
const Y &y) {
double sum = 0.;
for (std::size_t i = 0; i < xs.values.size(); ++i) {
sum += xs.coefficients[i] * SubCaller::call(cov_func, xs.values[i], y);
Expand All @@ -300,16 +300,15 @@ template <typename SubCaller> struct LinearCombinationCaller {
typename MeanFunc, typename X,
typename std::enable_if<
has_valid_mean_caller<MeanFunc, SubCaller, X>::value, int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
return SubCaller::call(mean_func, x);
}

template <
typename MeanFunc, typename X,
typename std::enable_if<
has_valid_mean_caller<MeanFunc, SubCaller, X>::value, int>::type = 0>
static double call(const MeanFunc &mean_func,
const LinearCombination<X> &xs) {
static auto call(const MeanFunc &mean_func, const LinearCombination<X> &xs) {
double sum = 0.;
for (std::size_t i = 0; i < xs.values.size(); ++i) {
sum += xs.coefficients[i] * SubCaller::call(mean_func, xs.values[i]);
Expand Down Expand Up @@ -349,7 +348,7 @@ template <typename SubCaller> struct VariantForwarder {
!is_variant<X>::value && !is_variant<Y>::value &&
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value,
int>::type = 0>
static double call(const CovFunc &cov_func, const X &x, const Y &y) {
static auto call(const CovFunc &cov_func, const X &x, const Y &y) {
return SubCaller::call(cov_func, x, y);
}

Expand All @@ -366,15 +365,15 @@ template <typename SubCaller> struct VariantForwarder {
typename std::enable_if<
has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value,
int>::type = 0>
double operator()(const Y &y) const {
auto operator()(const Y &y) const {
return SubCaller::call(cov_func_, x_, y);
};

template <typename Y,
typename std::enable_if<
!has_valid_cov_caller<CovFunc, SubCaller, X, Y>::value,
int>::type = 0>
double operator()(const Y &y) const {
auto operator()(const Y &y) const {
return 0.;
};

Expand All @@ -389,8 +388,8 @@ template <typename SubCaller> struct VariantForwarder {
has_valid_variant_cov_caller<CovFunc, SubCaller, A,
variant<Ts...>>::value,
int>::type = 0>
static double call(const CovFunc &cov_func, const A &x,
const variant<Ts...> &y) {
static auto call(const CovFunc &cov_func, const A &x,
const variant<Ts...> &y) {
CallVisitor<CovFunc, A> visitor(cov_func, x);
return apply_visitor(visitor, y);
}
Expand All @@ -402,8 +401,8 @@ template <typename SubCaller> struct VariantForwarder {
has_valid_variant_cov_caller<CovFunc, SubCaller, A,
variant<Ts...>>::value,
int>::type = 0>
static double call(const CovFunc &cov_func, const variant<Ts...> &x,
const A &y) {
static auto call(const CovFunc &cov_func, const variant<Ts...> &x,
const A &y) {
CallVisitor<CovFunc, A> visitor(cov_func, y);
return apply_visitor(visitor, x);
}
Expand All @@ -414,8 +413,8 @@ template <typename SubCaller> struct VariantForwarder {
has_valid_cov_caller<CovFunc, SubCaller, variant<Xs...>,
variant<Ys...>>::value,
int>::type = 0>
static double call(const CovFunc &cov_func, const variant<Xs...> &x,
const variant<Ys...> &y) {
static auto call(const CovFunc &cov_func, const variant<Xs...> &x,
const variant<Ys...> &y) {
return x.match(
[&y, &cov_func](const auto &xx) { return call(cov_func, xx, y); });
}
Expand All @@ -434,15 +433,15 @@ template <typename SubCaller> struct VariantForwarder {
typename std::enable_if<
has_valid_mean_caller<MeanFunc, SubCaller, X>::value,
int>::type = 0>
double operator()(const X &x) const {
auto operator()(const X &x) const {
return SubCaller::call(mean_func_, x);
};

template <typename X,
typename std::enable_if<
!has_valid_mean_caller<MeanFunc, SubCaller, X>::value,
int>::type = 0>
double operator()(const X &x) const {
auto operator()(const X &x) const {
return 0.;
};

Expand All @@ -454,7 +453,7 @@ template <typename SubCaller> struct VariantForwarder {
!is_variant<X>::value &&
has_valid_mean_caller<MeanFunc, SubCaller, X>::value,
int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
return SubCaller::call(mean_func, x);
}

Expand All @@ -463,7 +462,7 @@ template <typename SubCaller> struct VariantForwarder {
has_valid_variant_mean_caller<
MeanFunc, SubCaller, X>::value,
int>::type = 0>
static double call(const MeanFunc &mean_func, const X &x) {
static auto call(const MeanFunc &mean_func, const X &x) {
MeanCallVisitor<MeanFunc> visitor(mean_func);
return apply_visitor(visitor, x);
}
Expand All @@ -480,9 +479,8 @@ using DefaultCaller = internal::VariantForwarder<internal::MeasurementForwarder<

template <typename Caller, typename CovFunc, typename... Args>
class caller_has_valid_call
: public has_call_with_return_type<Caller, double,
typename const_ref<CovFunc>::type,
typename const_ref<Args>::type...> {};
: public has_call<Caller, typename const_ref<CovFunc>::type,
typename const_ref<Args>::type...> {};

template <typename CovFunc, typename... Args>
class has_valid_caller
Expand Down
Loading