1#ifndef BITSIM_PLANNING_A_STAR_SEARCH_H
2#define BITSIM_PLANNING_A_STAR_SEARCH_H
10#include <unordered_map>
50template <
typename Node,
typename Neighbours,
typename Cost,
typename Heuristic>
52 Heuristic heuristic,
real_t weight = 1.0)
59 bool operator>(
const Open &other)
const {
return f > other.f; }
62 std::priority_queue<Open, std::vector<Open>, std::greater<Open>> open;
63 std::unordered_map<Node, real_t> best_g;
64 std::unordered_map<Node, Node> came_from;
68 open.push({weight * heuristic(start, goal), 0.0, start});
72 const Open current = open.top();
77 if (current.g > best_g[current.node])
83 if (current.node == goal)
86 result.
cost = current.g;
87 for (Node n = goal;; n = came_from.at(n))
89 result.
path.push_back(n);
95 std::reverse(result.
path.begin(), result.
path.end());
99 for (
const Node &next : neighbours(current.node))
101 const real_t g = current.g + cost(current.node, next);
102 const auto known = best_g.find(next);
103 if (known == best_g.end() || g < known->second)
106 came_from.insert_or_assign(next, current.node);
107 open.push({g + weight * heuristic(next, goal), g, next});
120SearchResult<GridCell>
a_star_grid(
const OccupancyGrid &grid,
const GridCell &start,
121 const GridCell &goal,
bool diagonal =
true,
real_t weight = 1.0);
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
SearchResult< Node > a_star(const Node &start, const Node &goal, Neighbours neighbours, Cost cost, Heuristic heuristic, real_t weight=1.0)
A* search from start to goal.
Definition a_star_search.h:51
SearchResult< GridCell > a_star_grid(const OccupancyGrid &grid, const GridCell &start, const GridCell &goal, bool diagonal, real_t weight)
A* on an occupancy grid, moving to the neighbours of a cell (see OccupancyGrid::neighbours) with the ...
Definition a_star_search.cpp:10
Definition a_star_search.cpp:8
Definition dc_motor_backend_base.h:9
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
What a search found.
Definition a_star_search.h:23
uint_t n_expanded
how many nodes the search took off its open list
Definition a_star_search.h:27
bool found
whether the goal was reached
Definition a_star_search.h:24
real_t cost
the cost of the path
Definition a_star_search.h:26
std::vector< Node > path
from the start to the goal, both included; empty if not found
Definition a_star_search.h:25