37 #include <gtest/gtest.h> 61 size_t size()
const override 78 TEST(Pf, BayesianEstimation)
81 const float center_list[] =
86 const float abs_error = 2e-1;
87 const float sigma = 1.0;
88 const float sigma2 = 2.0;
90 for (
auto center : center_list)
92 for (
auto center2 : center_list)
98 ASSERT_NEAR(center, pf.
expectation()[0], abs_error);
99 ASSERT_NEAR(sigma, pf.
covariance()[0][0], abs_error);
101 auto likelihood = [center2, sigma2](
const State&
s) ->
float 103 return std::exp(-std::pow(
s[0] - center2, 2) / (2.0 * std::pow(sigma2, 2)));
108 const int HISTOGRAM_SIZE = 4000;
109 const float HISTOGRAM_RESOLUTION = 0.02;
111 float dist[HISTOGRAM_SIZE];
116 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
118 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION;
119 dist[i] = nd1(x - center) * nd2(x - center2);
126 for (
int i = 0; i < HISTOGRAM_SIZE; i++)
128 const float x = (i - HISTOGRAM_SIZE / 2.0) * HISTOGRAM_RESOLUTION - avg;
129 var += std::pow(x, 2) * dist[i];
135 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
140 ASSERT_NEAR(var, pf.
covariance()[0][0], abs_error);
143 ASSERT_NEAR(var, pf.
covariance(1.0, 0.5)[0][0], abs_error * 2);
150 const size_t size_num = 3;
151 const size_t size[size_num] =
157 const float center = 12.3;
158 const float sigma = 0.45;
163 for (
size_t i = 0; i < size_num; ++i)
169 ASSERT_LT(fabs(e[0] - center), 1e-1);
170 ASSERT_LT(fabs(std::sqrt(v[0][0]) - sigma), 1e-1);
175 const std::vector<State> v_r = pf.
covariance();
176 ASSERT_LT(fabs(e_r[0] - center), 1e-1);
177 ASSERT_LT(fabs(std::sqrt(v_r[0][0]) - sigma), 1e-1);
179 if (i + 1 != size_num)
186 TEST(Pf, ResampleFlatLikelihood)
189 const float center = 12.3;
190 const float sigma = 0.45;
195 std::vector<float> orig;
206 void testResample(
const std::vector<float>& probs,
const std::vector<float>& states,
207 const std::vector<float>& expected_resampled_states)
209 const size_t particle_num = probs.size();
211 auto it = pf.
begin();
212 for (
size_t i = 0; i < particle_num; ++i, ++it)
214 it->state_.x = states.at(i);
215 it->probability_ = probs.at(i);
222 EXPECT_FLOAT_EQ(expected_resampled_states.at(i), pf.
getParticle(i)[0]);
226 TEST(Pf, ResampleFirstAndLastParticle)
228 const float small_prob = 1.0e-06
f;
230 SCOPED_TRACE(
"ResampleFirstParticle");
231 const std::vector<float> probs =
233 small_prob, 0.2f, 0.2f, 0.2f, 0.4f - small_prob
235 const std::vector<float> states =
237 0.0f, 1.0f, 2.0f, 3.0f, 4.0f
239 const std::vector<float> expected_resampled_states =
241 1.0f, 2.0f, 3.0f, 4.0f, 4.0f
246 SCOPED_TRACE(
"ResampleLastParticle");
247 const std::vector<float> probs =
249 0.2f, 0.2f, 0.2f, 0.4f - small_prob, small_prob};
250 const std::vector<float> states =
252 0.0f, 1.0f, 2.0f, 3.0f, 4.0f
254 const std::vector<float> expected_resampled_states =
256 0.0f, 1.0f, 2.0f, 3.0f, 3.0f
265 const float val0 = 12.3;
266 const float val1 = 45.6;
271 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
273 ASSERT_EQ(it->state_[0], val0);
274 it->state_[0] = val1;
276 for (
auto it = pf.
begin(); it != pf.
end(); ++it)
277 ASSERT_EQ(it->state_[0], val1);
283 const float val0 = 12.3;
284 const float val1 = 45.6;
291 it->state_[0] = val1;
295 for (
size_t i = 0; i < 10; ++i)
297 for (
size_t i = 10; i < 20; ++i)
301 int main(
int argc,
char** argv)
303 testing::InitGoogleTest(&argc, argv);
305 return RUN_ALL_TESTS();
T getParticle(const size_t i) const
size_t getParticleSize() const
T expectation(const FLT_TYPE pass_ratio=1.0)
void testResample(const std::vector< float > &probs, const std::vector< float > &states, const std::vector< float > &expected_resampled_states)
void resizeParticle(const size_t num)
void init(T mean, T sigma)
std::vector< T > covariance(const FLT_TYPE pass_ratio=1.0, const FLT_TYPE random_sample_ratio=1.0)
int main(int argc, char **argv)
std::vector< Particle< T, FLT_TYPE > >::iterator appendParticle(const size_t num)
std::vector< Particle< T, FLT_TYPE > >::iterator begin()
float & operator[](const size_t i) override
std::vector< Particle< T, FLT_TYPE > >::iterator end()
size_t size() const override
TEST(Pf, BayesianEstimation)
void measure(std::function< FLT_TYPE(const T &)> likelihood)
const float & operator[](const size_t i) const