Skip to content

Commit

Permalink
Rename importance_sample method to reweight (#158)
Browse files Browse the repository at this point in the history
Fixes #151.

The term "importance sample" means the act of drawing samples with
replacement with probabilities given by the importance factors, and that
is not what this method does in code.

Signed-off-by: Nahuel Espinosa <[email protected]>
  • Loading branch information
nahueespinosa authored Apr 3, 2023
1 parent 5db2870 commit a7b42cf
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 15 deletions.
22 changes: 11 additions & 11 deletions beluga/include/beluga/algorithm/particle_filter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@
*
* The following is satisfied:
* - `b.sample()` updates the particles based on the latest motion update.
* - `b.importance_sample()` updates the particles weight based on the latest sensor update.
* - `b.reweight()` updates the particles weight based on the latest sensor update.
* - `b.resample()` generates new particles from the old ones based on their importance weights.
*
* \section BaseParticleFilterLinks See also
Expand Down Expand Up @@ -75,19 +75,19 @@ struct BaseParticleFilterInterface {
* to incorporate measurements. The importance is proportional to the
* probability of seeing the measurement given the current particle state.
*/
virtual void importance_sample() = 0;
virtual void reweight() = 0;

/**
* \overload
* It allows specifying a sequenced execution policy.
*/
virtual void importance_sample(std::execution::sequenced_policy) { return this->importance_sample(); };
virtual void reweight(std::execution::sequenced_policy) { return this->reweight(); };

/**
* \overload
* It allows specifying a parallel execution policy.
*/
virtual void importance_sample(std::execution::parallel_policy) { return this->importance_sample(); };
virtual void reweight(std::execution::parallel_policy) { return this->reweight(); };

/// Resample particles based on their weights.
/**
Expand Down Expand Up @@ -151,18 +151,18 @@ class BootstrapParticleFilter : public Mixin {
void sample(std::execution::parallel_policy policy) final { this->sample_impl(policy); }

/**
* \copydoc BaseParticleFilterInterface::importance_sample()
* \copydoc BaseParticleFilterInterface::reweight()
*
* The update will be done based on the `Mixin` implementation of the
* \ref SensorModelPage "SensorModel" named requirements.
*/
void importance_sample() final { this->importance_sample_impl(std::execution::seq); }
void reweight() final { this->reweight_impl(std::execution::seq); }

/// \copydoc BaseParticleFilterInterface::importance_sample(std::execution::sequenced_policy policy)
void importance_sample(std::execution::sequenced_policy policy) final { this->importance_sample_impl(policy); }
/// \copydoc BaseParticleFilterInterface::reweight(std::execution::sequenced_policy policy)
void reweight(std::execution::sequenced_policy policy) final { this->reweight_impl(policy); }

/// \copydoc BaseParticleFilterInterface::importance_sample(std::execution::parallel_policy policy)
void importance_sample(std::execution::parallel_policy policy) final { this->importance_sample_impl(policy); }
/// \copydoc BaseParticleFilterInterface::reweight(std::execution::parallel_policy policy)
void reweight(std::execution::parallel_policy policy) final { this->reweight_impl(policy); }

/**
* \copydoc BaseParticleFilterInterface::resample()
Expand Down Expand Up @@ -191,7 +191,7 @@ class BootstrapParticleFilter : public Mixin {
}

template <typename ExecutionPolicy>
void importance_sample_impl(ExecutionPolicy&& policy) {
void reweight_impl(ExecutionPolicy&& policy) {
auto states = this->self().states() | ranges::views::common;
auto weights = this->self().weights() | ranges::views::common;
std::transform(
Expand Down
4 changes: 2 additions & 2 deletions beluga/test/beluga/algorithm/test_particle_filter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ TEST(BootstrapParticleFilter, UpdateWithoutResampling) {
ASSERT_THAT(filter.weights() | ranges::to<std::vector>, Each(expected_initial_weight));

// updating particle weights, particles will have updated weights, but unchanged state
filter.importance_sample();
filter.reweight();
ASSERT_THAT(filter.states() | ranges::to<std::vector>, Each(expected_final_state));
ASSERT_THAT(filter.weights() | ranges::to<std::vector>, Each(expected_final_weight));

Expand Down Expand Up @@ -162,7 +162,7 @@ TEST(BootstrapParticleFilter, UpdateWithResampling) {
ASSERT_THAT(filter.weights() | ranges::to<std::vector>, Each(expected_initial_weight));

// updating particle weights, particles will have updated weights, but unchanged state
filter.importance_sample();
filter.reweight();
ASSERT_THAT(filter.states() | ranges::to<std::vector>, Each(expected_final_state));
ASSERT_THAT(filter.weights() | ranges::to<std::vector>, Each(expected_final_weight));

Expand Down
2 changes: 1 addition & 1 deletion beluga_amcl/src/amcl_node.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -846,7 +846,7 @@ void AmclNode::laser_callback(
static_cast<std::size_t>(get_parameter("max_beams").as_int()),
static_cast<float>(get_parameter("laser_min_range").as_double()),
static_cast<float>(get_parameter("laser_max_range").as_double())));
particle_filter_->importance_sample(exec_policy);
particle_filter_->reweight(exec_policy);
const auto time3 = std::chrono::high_resolution_clock::now();
particle_filter_->resample();
const auto time4 = std::chrono::high_resolution_clock::now();
Expand Down
2 changes: 1 addition & 1 deletion beluga_system_tests/test/test_system.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ TEST_P(BelugaSystemTest, testEstimatedPath) {
pf.sample();
auto scan_copy = data_point.scan;
pf.update_sensor(std::move(scan_copy));
pf.importance_sample();
pf.reweight();
pf.resample();
auto estimation = pf.estimate();
auto error = data_point.ground_truth.inverse() * estimation.first;
Expand Down

0 comments on commit a7b42cf

Please sign in to comment.