bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
a_star_search.h
Go to the documentation of this file.
1#ifndef BITSIM_PLANNING_A_STAR_SEARCH_H
2#define BITSIM_PLANNING_A_STAR_SEARCH_H
3
6
7#include <algorithm>
8#include <functional>
9#include <queue>
10#include <unordered_map>
11#include <vector>
12
13namespace bitsim
14{
15namespace planning
16{
17
22template <typename Node> struct SearchResult
23{
24 bool found{false};
25 std::vector<Node> path;
26 real_t cost{0.0};
28};
29
50template <typename Node, typename Neighbours, typename Cost, typename Heuristic>
51SearchResult<Node> a_star(const Node &start, const Node &goal, Neighbours neighbours, Cost cost,
52 Heuristic heuristic, real_t weight = 1.0)
53{
54 struct Open
55 {
56 real_t f;
57 real_t g;
58 Node node;
59 bool operator>(const Open &other) const { return f > other.f; }
60 };
61
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;
65
66 SearchResult<Node> result;
67 best_g[start] = 0.0;
68 open.push({weight * heuristic(start, goal), 0.0, start});
69
70 while (!open.empty())
71 {
72 const Open current = open.top();
73 open.pop();
74
75 // A node can be on the list more than once, with the costs of the paths found to it.
76 // Only the cheapest counts
77 if (current.g > best_g[current.node])
78 {
79 continue;
80 }
81 ++result.n_expanded;
82
83 if (current.node == goal)
84 {
85 result.found = true;
86 result.cost = current.g;
87 for (Node n = goal;; n = came_from.at(n))
88 {
89 result.path.push_back(n);
90 if (n == start)
91 {
92 break;
93 }
94 }
95 std::reverse(result.path.begin(), result.path.end());
96 return result;
97 }
98
99 for (const Node &next : neighbours(current.node))
100 {
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)
104 {
105 best_g[next] = g;
106 came_from.insert_or_assign(next, current.node);
107 open.push({g + weight * heuristic(next, goal), g, next});
108 }
109 }
110 }
111 return result;
112}
113
120SearchResult<GridCell> a_star_grid(const OccupancyGrid &grid, const GridCell &start,
121 const GridCell &goal, bool diagonal = true, real_t weight = 1.0);
122
123} // namespace planning
124} // namespace bitsim
125
126#endif // BITSIM_PLANNING_A_STAR_SEARCH_H
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