test_thrun_recovery_probability_estimator.cpp
Go to the documentation of this file.
1 // Copyright 2024 Ekumen, Inc.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #include <gtest/gtest.h>
16 
17 #include <tuple>
18 #include <vector>
19 
21 #include "beluga/primitives.hpp"
22 
23 namespace {
24 
25 TEST(ThrunRecoveryProbabilityEstimator, InvalidAlphaSlow) {
26  const double alpha_slow = -0.2;
27  const double alpha_fast = 0.4;
28  ASSERT_DEBUG_DEATH(beluga::ThrunRecoveryProbabilityEstimator(alpha_slow, alpha_fast), "Assertion");
29 }
30 
31 TEST(ThrunRecoveryProbabilityEstimator, InvalidAlphaFast) {
32  const double alpha_slow = 0.2;
33  const double alpha_fast = 0.1;
34  ASSERT_DEBUG_DEATH(beluga::ThrunRecoveryProbabilityEstimator(alpha_slow, alpha_fast), "Assertion");
35 }
36 
37 TEST(ThrunRecoveryProbabilityEstimator, ProbabilityWithNoParticles) {
38  // Test the probability when the particle set is empty.
39  const double alpha_slow = 0.2;
40  const double alpha_fast = 0.4;
41  auto estimator = beluga::ThrunRecoveryProbabilityEstimator{alpha_slow, alpha_fast};
42  ASSERT_EQ(estimator(std::vector<std::tuple<int, beluga::Weight>>{}), 0.0);
43 }
44 
45 TEST(ThrunRecoveryProbabilityEstimator, ProbabilityWithZeroWeight) {
46  // Test the probability when the total weight is zero.
47  const double alpha_slow = 0.2;
48  const double alpha_fast = 0.4;
49  auto estimator = beluga::ThrunRecoveryProbabilityEstimator{alpha_slow, alpha_fast};
50  ASSERT_EQ(estimator(std::vector<std::tuple<int, beluga::Weight>>{{1, 0.0}, {2, 0.0}}), 0.0);
51 }
52 
53 TEST(ThrunRecoveryProbabilityEstimator, ProbabilityAfterUpdateAndReset) {
54  const double alpha_slow = 0.5;
55  const double alpha_fast = 1.0;
56  auto estimator = beluga::ThrunRecoveryProbabilityEstimator{alpha_slow, alpha_fast};
57 
58  // Test the probability after updating the estimator with particle weights.
59  auto input = std::vector<std::tuple<int, beluga::Weight>>{{5, 1.0}, {6, 2.0}, {7, 3.0}};
60  ASSERT_EQ(estimator(input), 0.0);
61 
62  input = std::vector<std::tuple<int, beluga::Weight>>{{5, 0.5}, {6, 1.0}, {7, 1.5}};
63  ASSERT_NEAR(estimator(input), 0.33, 0.01);
64 
65  input = std::vector<std::tuple<int, beluga::Weight>>{{5, 0.5}, {6, 1.0}, {7, 1.5}};
66  ASSERT_NEAR(estimator(input), 0.20, 0.01);
67 
68  estimator.reset();
69  ASSERT_EQ(estimator(input), 0.0); // Test the probability after resetting the estimator.
70 }
71 
72 class ThrunRecoveryProbabilityWithParam : public ::testing::TestWithParam<std::tuple<double, double, double>> {};
73 
74 TEST_P(ThrunRecoveryProbabilityWithParam, Probabilities) {
75  const auto [initial_weight, final_weight, expected_probability] = GetParam();
76 
77  const double alpha_slow = 0.001;
78  const double alpha_fast = 0.1;
79  auto estimator = beluga::ThrunRecoveryProbabilityEstimator{alpha_slow, alpha_fast};
80  auto particles = std::vector<std::tuple<int, beluga::Weight>>{{1, initial_weight}};
81 
82  ASSERT_NEAR(estimator(particles), 0.0, 0.01);
83 
84  beluga::weight(particles.front()) = final_weight;
85 
86  ASSERT_NEAR(estimator(particles), expected_probability, 0.01);
87 }
88 
89 INSTANTIATE_TEST_SUITE_P(
90  ThrunRecoveryProbability,
91  ThrunRecoveryProbabilityWithParam,
92  testing::Values(
93  std::make_tuple(1.0, 1.5, 0.00),
94  std::make_tuple(1.0, 2.0, 0.00),
95  std::make_tuple(1.0, 0.5, 0.05),
96  std::make_tuple(0.5, 0.1, 0.08),
97  std::make_tuple(0.5, 0.0, 0.10)));
98 
99 } // namespace
primitives.hpp
Implementation of library primitives to abstract member access.
thrun_recovery_probability_estimator.hpp
beluga::weight
constexpr weight_detail::weight_fn weight
Customization point object for accessing the weight of a particle.
Definition: primitives.hpp:264
beluga::ThrunRecoveryProbabilityEstimator
Random particle probability estimator.
Definition: thrun_recovery_probability_estimator.hpp:38
beluga::TEST
TEST(Bresenham, MultiPassGuarantee)
Definition: test_bresenham.cpp:27


beluga
Author(s):
autogenerated on Tue Jul 16 2024 02:59:53