44 : m_cost{
std::move(cost)} {}
58 template <
size_t Poses>
65 for (
int i = 0; i <
state.rows(); ++i) {
67 m_cost(poses[
static_cast<int>(
state(i))],
68 poses[
static_cast<int>(
state((i + 1) % poses.size()))]);
73 Eigen::Vector<double, Poses> initial;
74 for (
int i = 0; i < initial.rows(); ++i) {
78 auto indices = solver.Solve(initial, iterations);
81 for (
size_t i = 0; i < poses.size(); ++i) {
82 solution[i] = poses[
static_cast<int>(indices[i])];
87 std::find(solution.begin(), solution.end(), poses[0]),
104 std::vector<Pose2d>
Solve(std::span<const Pose2d> poses,
int iterations) {
106 1.0, &Neighbor<Eigen::Dynamic>, [&](
const Eigen::VectorXd&
state) {
109 for (
int i = 0; i <
state.rows(); ++i) {
111 m_cost(poses[
static_cast<int>(
state(i))],
112 poses[
static_cast<int>(
state((i + 1) % poses.size()))]);
117 Eigen::VectorXd initial{poses.size()};
118 for (
int i = 0; i < initial.rows(); ++i) {
122 auto indices = solver.Solve(initial, iterations);
124 std::vector<Pose2d> solution;
125 for (
size_t i = 0; i < poses.size(); ++i) {
126 solution.emplace_back(poses[
static_cast<int>(indices[i])]);
131 std::find(solution.begin(), solution.end(), poses[0]),
139 std::function<double(
const Pose2d&,
const Pose2d&)> m_cost =
154 static Eigen::Vector<double, Poses> Neighbor(
155 const Eigen::Vector<double, Poses>&
state) {
156 Eigen::Vector<double, Poses> proposedState =
state;
158 std::random_device
rd;
159 std::mt19937 gen{
rd()};
160 std::uniform_int_distribution<> distr{0,
161 static_cast<int>(
state.rows()) - 1};
163 int rangeStart = distr(gen);
164 int rangeEnd = distr(gen);
165 if (rangeEnd < rangeStart) {
169 for (
int i = rangeStart; i <= (rangeStart + rangeEnd) / 2; ++i) {
170 double temp = proposedState(i, 0);
171 proposedState(i, 0) =
state(rangeEnd - (i - rangeStart), 0);
172 proposedState(rangeEnd - (i - rangeStart), 0) = temp;
175 return proposedState;
Represents a 2D pose containing translational and rotational elements.
Definition: Pose2d.h:23
constexpr units::meter_t Y() const
Returns the Y component of the pose's translation.
Definition: Pose2d.h:96
constexpr units::meter_t X() const
Returns the X component of the pose's translation.
Definition: Pose2d.h:89
An implementation of the Simulated Annealing stochastic nonlinear optimization method.
Definition: SimulatedAnnealing.h:30
Given a list of poses, this class finds the shortest possible route that visits each pose exactly onc...
Definition: TravelingSalesman.h:28
TravelingSalesman(std::function< double(Pose2d, Pose2d)> cost)
Constructs a traveling salesman problem solver with a user-provided cost function.
Definition: TravelingSalesman.h:43
wpi::array< Pose2d, Poses > Solve(const wpi::array< Pose2d, Poses > &poses, int iterations)
Finds the path through every pose that minimizes the cost.
Definition: TravelingSalesman.h:59
std::vector< Pose2d > Solve(std::span< const Pose2d > poses, int iterations)
Finds the path through every pose that minimizes the cost.
Definition: TravelingSalesman.h:104
constexpr TravelingSalesman()=default
Constructs a traveling salesman problem solver with a cost function defined as the 2D distance betwee...
This class is a wrapper around std::array that does compile time size checking.
Definition: array.h:26
UnitTypeLhs hypot(const UnitTypeLhs &x, const UnitTypeRhs &y)
Computes the square root of the sum-of-squares of x and y.
Definition: math.h:505
FMT_CONSTEXPR auto find(Ptr first, Ptr last, T value, Ptr &out) -> bool
Definition: core.h:2120
state
Definition: core.h:2271
Definition: AprilTagPoseEstimator.h:15
Eigen::Vector< double, Size > Vectord
Definition: EigenCore.h:12
WPI_BASIC_JSON_TPL_DECLARATION void swap(wpi::WPI_BASIC_JSON_TPL &j1, wpi::WPI_BASIC_JSON_TPL &j2) noexcept(//NOLINT(readability-inconsistent-declaration-parameter-name) is_nothrow_move_constructible< wpi::WPI_BASIC_JSON_TPL >::value &&//NOLINT(misc-redundant-expression) is_nothrow_move_assignable< wpi::WPI_BASIC_JSON_TPL >::value)
exchanges the values of two JSON objects
Definition: json.h:5219
compound_unit< energy::joules, inverse< mass::kilogram > > rd
Definition: radiation.h:61
uint64_t rotate(uint64_t val, size_t shift)
Bitwise right rotate.
Definition: Hashing.h:182
constexpr empty_array_t empty_array
Definition: array.h:16