|
| 1 | +/* |
| 2 | + * Copyright (C) 2020 Swift Navigation Inc. |
| 3 | + * Contact: Swift Navigation <[email protected]> |
| 4 | + * |
| 5 | + * This source is subject to the license found in the file 'LICENSE' which must |
| 6 | + * be distributed together with this source. All other rights reserved. |
| 7 | + * |
| 8 | + * THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, |
| 9 | + * EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED |
| 10 | + * WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR PURPOSE. |
| 11 | + */ |
| 12 | + |
| 13 | +#ifndef INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ |
| 14 | +#define INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ |
| 15 | + |
| 16 | +namespace albatross { |
| 17 | + |
| 18 | +auto interpolation_cov_func() { |
| 19 | + SquaredExponential<EuclideanDistance> sqr_exp; |
| 20 | + IndependentNoise<double> noise; |
| 21 | + return sqr_exp + measurement_only(noise); |
| 22 | +} |
| 23 | + |
| 24 | +using InterpolationFunction = decltype(interpolation_cov_func()); |
| 25 | + |
| 26 | +/* |
| 27 | + * Generic Gaussian Process Implementation. |
| 28 | + */ |
| 29 | +class GaussianProcessInterpolator |
| 30 | + : public GaussianProcessBase<InterpolationFunction, ZeroMean, |
| 31 | + GaussianProcessInterpolator> { |
| 32 | +public: |
| 33 | + using Base = GaussianProcessBase<InterpolationFunction, ZeroMean, |
| 34 | + GaussianProcessInterpolator>; |
| 35 | +}; |
| 36 | + |
| 37 | +using GPInterpolatorFitType = |
| 38 | + typename fit_type<GaussianProcessInterpolator, double>::type; |
| 39 | + |
| 40 | +template <> |
| 41 | +class Prediction<GaussianProcessInterpolator, double, GPInterpolatorFitType> { |
| 42 | + |
| 43 | +public: |
| 44 | + Prediction(const GaussianProcessInterpolator &model, |
| 45 | + const GPInterpolatorFitType &fit, |
| 46 | + const std::vector<double> &features) |
| 47 | + : model_(model), fit_(fit), features_(features) {} |
| 48 | + |
| 49 | + Prediction(GaussianProcessInterpolator &&model, GPInterpolatorFitType &&fit, |
| 50 | + const std::vector<double> &features) |
| 51 | + : model_(std::move(model)), fit_(std::move(fit)), features_(features) {} |
| 52 | + |
| 53 | + // Mean |
| 54 | + Eigen::VectorXd mean() const { |
| 55 | + return MeanPredictor()._mean(model_, fit_, features_); |
| 56 | + } |
| 57 | + |
| 58 | + Eigen::VectorXd derivative() const { |
| 59 | + |
| 60 | + std::vector<Derivative<double>> derivative_features; |
| 61 | + for (const auto &f : features_) { |
| 62 | + derivative_features.emplace_back(f); |
| 63 | + } |
| 64 | + |
| 65 | + return MeanPredictor()._mean(model_, fit_, derivative_features); |
| 66 | + } |
| 67 | + |
| 68 | + Eigen::VectorXd second_derivative() const { |
| 69 | + |
| 70 | + std::vector<SecondDerivative<double>> derivative_features; |
| 71 | + for (const auto &f : features_) { |
| 72 | + derivative_features.emplace_back(f); |
| 73 | + } |
| 74 | + return MeanPredictor()._mean(model_, fit_, derivative_features); |
| 75 | + } |
| 76 | + |
| 77 | + const GaussianProcessInterpolator model_; |
| 78 | + const GPInterpolatorFitType fit_; |
| 79 | + const std::vector<double> features_; |
| 80 | +}; |
| 81 | + |
| 82 | +} // namespace albatross |
| 83 | +#endif /* INCLUDE_ALBATROSS_SRC_MODELS_INTERPOLATE_HPP_ */ |
0 commit comments