-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Gerardo Puga <[email protected]>
- Loading branch information
Showing
20 changed files
with
979 additions
and
22 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
beluga/include/beluga/algorithm/resampling_rate_policies/resample_interval_policy.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2023 Ekumen, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_RESAMPLE_INTERVAL_POLICY_HPP | ||
#define BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_RESAMPLE_INTERVAL_POLICY_HPP | ||
|
||
#include <optional> | ||
#include <utility> | ||
|
||
#include <ciabatta/ciabatta.hpp> | ||
|
||
namespace beluga { | ||
|
||
struct ResampleIntervalPolicyParam { | ||
std::size_t resample_interval_count{1}; | ||
}; | ||
|
||
template <class Mixin> | ||
struct ResampleIntervalPolicy : public Mixin { | ||
public: | ||
using param_type = ResampleIntervalPolicyParam; | ||
|
||
template <class... Args> | ||
explicit ResampleIntervalPolicy(const param_type& configuration, Args&&... rest) | ||
: Mixin(std::forward<Args>(rest)...), configuration_{configuration} { | ||
if (configuration_.resample_interval_count > 1) { | ||
this->self().register_sampling_voter([this]() { return do_resampling(); }); | ||
} | ||
} | ||
|
||
private: | ||
param_type configuration_; | ||
std::size_t filter_update_counter_{0}; | ||
|
||
[[nodiscard]] bool do_resampling() { | ||
filter_update_counter_ = (filter_update_counter_ + 1) % configuration_.resample_interval_count; | ||
return (filter_update_counter_ == 0); | ||
} | ||
}; | ||
|
||
} // namespace beluga | ||
|
||
#endif |
78 changes: 78 additions & 0 deletions
78
beluga/include/beluga/algorithm/resampling_rate_policies/resample_on_motion_policy.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// Copyright 2023 Ekumen, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_RESAMPLE_ON_MOTION_POLICY_HPP | ||
#define BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_RESAMPLE_ON_MOTION_POLICY_HPP | ||
|
||
#include <optional> | ||
#include <utility> | ||
|
||
#include <ciabatta/ciabatta.hpp> | ||
#include <sophus/se2.hpp> | ||
|
||
namespace beluga { | ||
|
||
struct ResampleOnMotionPolicyParam { | ||
double update_min_d{0.}; | ||
double update_min_a{0.}; | ||
}; | ||
|
||
template <class Mixin> | ||
struct ResampleOnMotionPolicy : public Mixin { | ||
public: | ||
using param_type = ResampleOnMotionPolicyParam; | ||
using motion_event = Sophus::SE2d; | ||
|
||
template <class... Args> | ||
explicit ResampleOnMotionPolicy(const param_type& configuration, Args&&... rest) | ||
: Mixin(std::forward<Args>(rest)...), configuration_{configuration} { | ||
this->self().register_sampling_voter([this]() { return do_resampling(); }); | ||
} | ||
|
||
private: | ||
param_type configuration_; | ||
std::optional<motion_event> latest_resample_pose_; | ||
|
||
[[nodiscard]] bool do_resampling() { | ||
// To avoid loss of diversity in the particle population, don't | ||
// resample when the state is known to be static. | ||
// See 'Probabilistic Robotics, Chapter 4.2.4'. | ||
auto current_pose = this->self().latest_motion_update(); | ||
|
||
// default to letting other policies decide | ||
bool must_do_resample{true}; | ||
|
||
if (current_pose && latest_resample_pose_) { | ||
// calculate relative transform between previous pose and the current one | ||
const auto delta = latest_resample_pose_->inverse() * current_pose.value(); | ||
|
||
// only resample if movement is above thresholds | ||
must_do_resample = // | ||
std::abs(delta.translation().x()) > configuration_.update_min_d || | ||
std::abs(delta.translation().y()) > configuration_.update_min_d || | ||
std::abs(delta.so2().log()) > configuration_.update_min_a; | ||
} | ||
|
||
// we always measure the distance travelled since the last time we did resample | ||
if (must_do_resample) { | ||
latest_resample_pose_ = current_pose; | ||
} | ||
|
||
return must_do_resample; | ||
} | ||
}; | ||
|
||
} // namespace beluga | ||
|
||
#endif |
59 changes: 59 additions & 0 deletions
59
beluga/include/beluga/algorithm/resampling_rate_policies/selective_resampling_policy.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// Copyright 2023 Ekumen, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_SELECTIVE_RESAMPLING_POLICY_HPP | ||
#define BELUGA_ALGORITHM_RESAMPLING_RATE_POLICIES_SELECTIVE_RESAMPLING_POLICY_HPP | ||
|
||
#include <utility> | ||
|
||
#include <range/v3/numeric/accumulate.hpp> | ||
#include <range/v3/view/transform.hpp> | ||
|
||
#include <ciabatta/ciabatta.hpp> | ||
|
||
namespace beluga { | ||
|
||
struct SelectiveResamplingPolicyParam { | ||
bool selective_resampling{false}; | ||
}; | ||
|
||
template <class Mixin> | ||
struct SelectiveResamplingPolicy : public Mixin { | ||
public: | ||
using param_type = SelectiveResamplingPolicyParam; | ||
|
||
template <class... Args> | ||
explicit SelectiveResamplingPolicy(const param_type& configuration, Args&&... rest) | ||
: Mixin(std::forward<Args>(rest)...), configuration_{configuration} { | ||
// only enable this if it's configured to be used | ||
if (configuration.selective_resampling) { | ||
this->self().register_sampling_voter([this]() { return do_resampling(); }); | ||
} | ||
} | ||
|
||
private: | ||
param_type configuration_; | ||
|
||
[[nodiscard]] bool do_resampling() const { | ||
const auto n_eff = | ||
1. / | ||
ranges::accumulate(this->self().weights() | ranges::views::transform([](const auto w) { return w * w; }), 0.); | ||
const auto n = static_cast<double>(std::size(this->self().weights())); | ||
return n_eff < n / 2.; | ||
} | ||
}; | ||
|
||
} // namespace beluga | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
50 changes: 50 additions & 0 deletions
50
beluga/include/beluga/runtime_dispatch/runtime_dispatch.hpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// Copyright 2023 Ekumen, Inc. | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef BELUGA_RUNTIME_DISPATCH_RUNTIME_DISPATCH_HPP | ||
#define BELUGA_RUNTIME_DISPATCH_RUNTIME_DISPATCH_HPP | ||
|
||
#include <functional> | ||
#include <vector> | ||
|
||
#include <ciabatta/ciabatta.hpp> | ||
|
||
namespace beluga { | ||
|
||
template <class Mixin> | ||
struct RuntimeDispatch : public Mixin { | ||
public: | ||
using resample_voter_function = std::function<bool()>; | ||
|
||
template <class... Args> | ||
explicit RuntimeDispatch(Args&&... rest) : Mixin(std::forward<Args>(rest)...) {} | ||
|
||
[[nodiscard]] bool do_resampling_vote() { | ||
// resampling voters are in cascade: if any of them votes "no", no resampling is done | ||
return std::all_of(voters_.begin(), voters_.end(), [](const auto& predicate) { return predicate(); }); | ||
} | ||
|
||
// do not split this function from the "protected" access clause. The method needs to be protected to | ||
// prevent code that tries call register_sampling_voter() before this mixin's constructor | ||
// has been executed from compiling. | ||
protected: | ||
void register_sampling_voter(resample_voter_function predicate) { voters_.push_back(std::move(predicate)); } | ||
|
||
private: | ||
std::vector<resample_voter_function> voters_; | ||
}; | ||
|
||
} // namespace beluga | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
add_subdirectory(benchmark) | ||
add_subdirectory(beluga) | ||
add_subdirectory(build_failure_tests) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.