The tree of the first run growing until it reaches the goal, its path, 12.14 m, the path shortened, 10.39 m, and the path of A* on an occupancy grid, 10.76 m.
and it stops when a new node is close enough to the goal and the last stretch to the goal is free. The path is then the branches from the root to that node.
The tree needs only one thing about the world: whether a point is free. Here a point is free if the robot's centre there is at least its radius from every shelf and wall, worked out from the rectangles directly. A branch is free if its points are, checked every 2 cm.
1. A tree, its path, and the path shortened
384 iterations, 219 nodes; the path has 62 points, shortened 6
| ## |
| . ## |
| . ## . ### G |
| . . ## .... ### ** |
| ... .. .## . . ### **. |
| . . ### . ## . .. . ### *. |
| ...### . .... . ### **.. |
| . ### . ....... ### **. |
| . . ### *****.... ### *. |
| . ...### .. *. *****. ### **.. |
| .. ### ....*.## .*** ** . |
| .. ### **.## . .**. * ... |
| . ..### *..## .. ..****.. |
| .. ### * .## .. . **. . |
| .. .. ### **..## . . |
| .. ### *.. ## . ########. |
| ... ### *.. ## ######## |
| ... ### * . ## ######## |
| .. ### *... ## |
| .. ### *....## |
| . .*. ## |
| ....******* ## |
| .S****.. .. . ## |
| .. .. ## |
| ## |
path of the tree 12.14 m
shortened 10.39 m
A* on the grid 10.76 m
2. Every seed finds another path
seed iterations nodes path [m] shortened [m]
1 1657 517 13.44 10.67
2 633 224 12.40 10.21
3 321 192 12.01 10.15
4 345 207 11.68 10.32
5 507 218 11.20 10.00
3. The length of the branches
step [m] iterations nodes path [m]
0.05 2514 1152 12.09
0.20 384 219 12.14
0.50 363 163 12.87
1.00 98 45 11.21
Every seed grows a different tree and finds a different path: an RRT is quick, and finds a path, not the shortest. Short branches explore carefully but need many iterations; long ones grow fast, but near obstacles most of them are blocked.
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
{
void draw(
const RRT &tree,
const std::vector<RRT::Point> &path)
{
const int cols =
static_cast<int>(
WORLD.
width() / 0.2);
const int rows =
static_cast<int>(
WORLD.
height() / 0.2);
std::vector<std::string> canvas(rows, std::string(cols, ' '));
const auto put = [&](
const RRT::Point &p,
char ch)
{
const int c = std::min(cols - 1, static_cast<int>(p[0] / 0.2));
const int r = std::min(rows - 1, static_cast<int>(p[1] / 0.2));
canvas[r][c] = ch;
};
for (int r = 0; r < rows; ++r)
{
for (int c = 0; c < cols; ++c)
{
const RRT::Point centre{(c + 0.5) * 0.2, (r + 0.5) * 0.2};
for (
const auto &b :
WORLD.obstacles())
{
if (centre[0] >= b.x0 && centre[0] <= b.x1 && centre[1] >= b.y0 &&
centre[1] <= b.y1)
{
canvas[r][c] = '#';
}
}
}
}
for (
const auto &node : tree.
tree())
{
put(node.p, '.');
}
for (std::size_t i = 1; i < path.size(); ++i)
{
const real_t d = std::hypot(path[i][0] - path[i - 1][0], path[i][1] - path[i - 1][1]);
const int n = std::max(1, static_cast<int>(d / 0.05));
for (int k = 0; k <= n; ++k)
{
put({path[i - 1][0] + t * (path[i][0] - path[i - 1][0]),
path[i - 1][1] + t * (path[i][1] - path[i - 1][1])},
'*');
}
}
for (int r = rows - 1; r >= 0; --r)
{
std::cout << " |" << canvas[r] << "|\n";
}
}
{
{
std::cout << " the tree did not reach the goal\n";
return;
}
const auto path = tree.
path();
const auto shorter = tree.
shortcut(path);
std::cout <<
" " << tree.
n_iterations() <<
" iterations, " << tree.
tree().size()
<< " nodes; the path has " << path.size() << " points, shortened " << shorter.size()
<< "\n\n";
std::cout << std::fixed << std::setprecision(2) <<
"\n path of the tree " <<
RRT::length(path)
<<
" m\n shortened " <<
RRT::length(shorter) <<
" m\n A* on the grid "
std::cout << " seed iterations nodes path [m] shortened [m]\n";
for (
uint_t seed = 1; seed <= 5; ++seed)
{
{
std::cout << std::setw(6) << seed << " no path\n";
continue;
}
std::cout << std::setw(6) << seed << std::setw(12) <<
run.n_iterations() << std::setw(8)
}
std::cout << " step [m] iterations nodes path [m]\n";
for (
const real_t step : {0.05, 0.2, 0.5, 1.0})
{
RRT::Parameters parameters;
std::cout << std::setw(10) <<
step << std::setw(12) <<
run.n_iterations() << std::setw(8)
if (found)
{
}
else
{
std::cout << " no path";
}
std::cout << "\n";
}
}
}
{
return 0;
}
The basic types of bitsim. Code outside this file names Eigen's matrix and vector types only through ...
A rectangular slice of the plane with axis-aligned box obstacles, a start and a goal.
Definition box_world.h:29
bool is_free(const Point &p) const
True if a disc of the given radius centred at p touches no obstacle and no outer wall....
Definition box_world.h:68
static BoxWorld warehouse()
The warehouse of the grid_planning and rrt examples: 8 x 5 m, a 0.25 m robot, five shelves and a wall...
Definition box_world.cpp:152
const Point & goal() const noexcept
Definition box_world.h:62
real_t width() const noexcept
Definition box_world.h:57
real_t height() const noexcept
Definition box_world.h:58
const Point & start() const noexcept
Definition box_world.h:61
A rectangle of the plane cut into square cells, each free or occupied.
Definition occupancy_grid.h:43
A rapidly exploring random tree in the plane.
Definition rrt.h:31
std::vector< Point > shortcut(const std::vector< Point > &path) const
A shorter path through the same free space: from each point of the path, go straight to the furthest ...
Definition rrt.cpp:115
static real_t length(const std::vector< Point > &path)
The length of a path [m].
Definition rrt.cpp:136
bool plan(const Point &start, const Point &goal)
Grow a tree from start to goal. Returns whether it reached the goal.
Definition rrt.cpp:35
std::array< real_t, 2 > Point
Definition rrt.h:33
std::vector< Point > path() const
The path of the last plan() from the start to the goal, both included; empty if the goal was not reac...
Definition rrt.cpp:81
uint_t n_iterations() const noexcept
The iterations the last plan() took.
Definition rrt.h:88
const std::vector< Node > & tree() const noexcept
The tree of the last plan().
Definition rrt.h:77
int main()
Definition discounted_returns.cpp:19
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
void print_title(const std::string &title, std::ostream &out)
An empty line and then the title.
Definition print_utils.cpp:17
std::size_t uint_t
uint_t
Definition bitsim_types.h:40
double real_t
real_t
Definition bitsim_types.h:20
grid(layout_path, episode_path, out, title)
Definition multi_agent_gif.py:36
Vector step(const QuadrotorDynamicsConfig &config, const Vector &x, const Vector &u, real_t max_rotor_speed)
Definition quadrotor_lqr.cpp:114
void draw(const RRT &tree, const std::vector< RRT::Point > &path)
Definition rrt.cpp:40
const RRT::Point START
Definition rrt.cpp:34
bool is_free(const RRT::Point &p)
Definition rrt.cpp:37
const BoxWorld WORLD
Definition rrt.cpp:33
void run()
Definition rrt.cpp:91
const real_t CELL
Definition rrt.cpp:30
const RRT::Point GOAL
Definition rrt.cpp:35
Print labelled values, one per line, as the examples do: two spaces, the label padded to a width,...
real_t step
the longest branch [m]
Definition rrt.h:42