bitsim
Model, simulate, estimate and control robots in C++
Loading...
Searching...
No Matches
Planning a path with a random tree

Planning a path on a grid cut the floor into cells and searched them. That gets slow when there are many dimensions, such as the six joints of an arm: the number of cells grows with the power of the dimension. A rapidly exploring random tree (RRT) does not cut the space up: it samples it. This example plans across the same warehouse, BoxWorld::warehouse, with one.

The tree growing, its path, the path shortened, and A*

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.

What you need

Nothing beyond bitsim itself. See Installing bitsim.

The tree

RRT grows a tree from the start. At each iteration it

  1. picks a random point of the warehouse, or the goal itself with a small probability, the goal bias, 5 % here;
  2. finds the node of the tree nearest to that point;
  3. grows a branch from that node towards the point, at most a step long, 20 cm here, if the branch is free;

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.

Random branches zigzag, so the path is long. shortcut takes most of the zigzag out: from each point of the path it goes straight to the furthest later point that a free segment reaches.

Running the example

./rrt

Output

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

Each character is 20 cm: # a shelf or the wall, . a node of the tree, * the shortened path. The tree spreads over the whole warehouse before a branch finds the door. Its path is 12.1 m, and shortened 10.4 m. That is a little shorter than the 10.76 m of A* on the same grid, because the tree moves in straight lines at any angle while A* is tied to the grid and its 45° diagonals, the same reason Theta* beats A* in Planning a path on a grid.

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.

Things to try

  1. Set the goal bias to 0 and to 0.5. How many iterations does the tree need, and why can a high bias be slower?
  2. Close the door. What does the tree do until it gives up?
  3. RRT* rewires the tree to make paths shorter as it grows. Where in plan() would it go?

The code

// Planning a path across the warehouse of the grid_planning example with a rapidly exploring
// random tree. The tree does not cut the floor into cells: it grows branches into random points
// of the free space until one reaches the goal. The example draws the tree and its path, shortens
// the path, and compares runs with different seeds and branch lengths with the path of A*.
//
// The warehouse is bitsim::planning::BoxWorld::warehouse(), shared with the grid_planning example.
//
// Usage: rrt
#include <cmath>
#include <iomanip>
#include <iostream>
#include <string>
#include <vector>
namespace rrt
{
using namespace bitsim::planning;
const real_t CELL = 0.1;
// The warehouse of the grid_planning example; the tree needs only its point test, is_free()
bool is_free(const RRT::Point &p) { return WORLD.is_free(p); }
// The warehouse, 0.2 m a character: # an obstacle, . a node of the tree, * the path
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, '.');
}
// The path, every 5 cm along each segment
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)
{
const real_t t = static_cast<real_t>(k) / n;
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])},
'*');
}
}
put(START, 'S');
put(GOAL, 'G');
for (int r = rows - 1; r >= 0; --r)
{
std::cout << " |" << canvas[r] << "|\n";
}
}
void run()
{
// A* on the same warehouse, cut into cells and grown by the robot's radius, to compare with
const OccupancyGrid grid = WORLD.occupancy_grid(CELL);
const auto a_star =
a_star_grid(grid, grid.to_cell(START[0], START[1]), grid.to_cell(GOAL[0], GOAL[1]));
bitsim::utils::io::print_title("1. A tree, its path, and the path shortened");
RRT tree({0.0, 0.0}, {WORLD.width(), WORLD.height()}, is_free, {});
if (!tree.plan(START, GOAL))
{
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";
draw(tree, shorter);
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 "
<< a_star.cost << " m\n";
bitsim::utils::io::print_title("2. Every seed finds another path");
std::cout << " seed iterations nodes path [m] shortened [m]\n";
for (uint_t seed = 1; seed <= 5; ++seed)
{
RRT run({0.0, 0.0}, {WORLD.width(), WORLD.height()}, is_free, {}, seed);
if (!run.plan(START, GOAL))
{
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)
<< run.tree().size() << std::setw(11) << RRT::length(run.path()) << std::setw(16)
<< RRT::length(run.shortcut(run.path())) << "\n";
}
bitsim::utils::io::print_title("3. The length of the branches");
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;
parameters.step = step;
RRT run({0.0, 0.0}, {WORLD.width(), WORLD.height()}, is_free, parameters);
const bool found = run.plan(START, GOAL);
std::cout << std::setw(10) << step << std::setw(12) << run.n_iterations() << std::setw(8)
<< run.tree().size();
if (found)
{
std::cout << std::setw(11) << RRT::length(run.path());
}
else
{
std::cout << " no path";
}
std::cout << "\n";
}
}
} // namespace rrt
int main()
{
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
Definition rrt.cpp:24
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