A grid at 100 steps per joint needs 1012 cells for a 6-joint arm. A random tree never builds that grid. It throws random samples, grows toward them, and reaches the goal with a few hundred nodes.
Pick a planner and watch the tree grow from the teal start to the yellow goal. Then we build, piece by piece, the sampler, the roadmap, the tree, its guarantees, and the rewiring that makes the path short.
You need a little Python and the distance between two points. Lesson 3's grid search helps but is not required.
Lesson 4 of 15 in our follow-along of Stanford AA 274A, Principles of Robot Autonomy I. Every idea is re-taught here in our own words and devices. Before: Lesson 3, C-Space and A*. Next: Lesson 5, Trajectory Optimization.
Planner
Illustrative toy in a 1.6 by 1.0 field. Step size η = 0.05. Bias sends 1 sample in 10 to the goal. RRT* keeps sampling after it reaches the goal. The dotted line is the straight line from start to goal, a lower bound that passes through walls.
Chapter 0
Add joints to a robot and watch the grid outgrow every memory chip
In Lesson 3 a floor robot planned on a grid. Its world has two numbers, x and y. A 100 by 100 grid has 10,000 cells, and A* searches it in milliseconds.
Now take a robot arm with six joints. Its configuration is six joint angles. The set of all configurations is the configuration space, or C-space. A grid over that space needs a cell for every mix of six angles.
How many cells is that? Call the number of buckets on each axis k. Call the number of dimensions d: one per joint. Call the total cell count N.
N equals k to the d.
Multiply k by itself once for each of the d axes. Each new joint multiplies N by k again.
k = 100 and d = 2: N = 10,000. Keep k = 100 and set d = 6: N = 1012, a trillion cells. At one byte a cell, the empty grid fills a terabyte.
Each bar is one robot with d joints. The scale is logarithmic: each gridline is 1,000 times the last. Set the buckets per axis and find where the bars cross the 1 GB line.
Exact counts N = kd. The 1 GB line assumes one byte per cell, before any collision check.
This growth is the curse of dimensionality: the cost has the dimension in its exponent. A* also must collision-check each cell it visits, and each check costs real time.
Sampling-based planners give up the grid. They pick random configurations, ask "is this one free?", and connect the free ones. They never store the space; they probe it.
The symptom is a planner that runs out of memory or time as soon as the robot gains joints. To measure it, compute kd before you build the grid. The fix for high d is to sample instead.
Chapter 1
Throw darts at a field and keep only the ones that land in free space
To find a route you do not need every cell. You need a few stepping stones in free space that link the start to the goal. Every sampling planner builds them from three cheap operations.
Press Sample 30. Blue dots passed the collision check. Red crosses landed inside a pink obstacle, and the planner throws them away. Compare the free share of your samples with the free share of the field.
Illustrative field. The free share of the field comes from a 400 by 250 point count.
The checker is the only code that reads the obstacle shapes. It works in the workspace, the real room, so it never reasons about the C-space as a whole. Swap a box test for an arm-against-mesh test and the planner above it stays the same.
A line has infinitely many points, so we check a few of them. Call the two free configurations a and b. Call the fraction of the way from a to b t, from 0 to 1. Call the point at that fraction p(t).
p of t equals a, plus t times the quantity b minus a.
Find the step from a to b. Take the fraction t of that step and add it to a. Check p(t) for several values of t; if any point is in collision, reject the line.
With made-up numbers: a = (1, 1), b = (4, 5), so b − a = (3, 4). At t = 0.25, p = (1.75, 2.0). At t = 0.5, p = (2.5, 3.0). Five points at t = 0, 0.25, 0.5, 0.75, 1 cover the line.
The symptom is a path that cuts through a thin wall. To measure it, compare the gap between check points with the thinnest obstacle. The fix is a gap smaller than that obstacle, for example 0.006 in this page's toys.
Chapter 2
Wire free samples into a graph, then search it like Lesson 3's grid
A warehouse robot fetches from 50 shelves in one fixed layout. It pays once to map the free space, then answers each trip fast. That map is a probabilistic roadmap (PRM), introduced by Kavraki, Švestka, Latombe and Overmars in 1996.
A PRM is a graph. Its vertices are free configurations. Its edges are free straight lines between nearby vertices. The course's version has two phases.
"Closer than r" needs a distance. Call two configurations x and x′, with coordinates x1, x2 and x′1, x′2.
The distance between x and x-prime equals the square root of x-one minus x-prime-one, squared, plus x-two minus x-prime-two, squared.
Subtract x′ from x one coordinate at a time. Square each difference and add them. The square root of the sum is the distance; if it is r or less, test the line for an edge.
With made-up numbers: x = (1, 1), x′ = (4, 5), r = 6. The distance is √(9 + 16) = 5. 5 ≤ 6, so we check the five points from Chapter 1. All are free, so the edge goes in.
Press Sample, then Connect, then Query. Then drag r down until the roadmap breaks into islands and the query fails, though a path plainly exists.
Illustrative field, 1.6 by 1.0. An island is a group of vertices with no edge to the rest. The query runs Dijkstra on edge lengths.
Why a radius and not every pair? With n vertices there are n(n − 1)/2 pairs. For n = 1,000 that is 499,500 line checks. The radius keeps only short lines, which are cheap to check and likely free.
The symptom is a query that fails while a path exists. To measure it, count the islands and see if the start and the goal sit on different ones. The fix is a larger r or more samples, at the price of more checks.
Chapter 3
Step the tree one sample at a time and watch it rush into open space
For one query in a world that may change, a whole roadmap wastes work. A rapidly-exploring random tree (RRT), from LaValle in 1998, grows one tree from the start until it touches the goal. A tree has one path between any two vertices, so the route back to the start is unique.
Each iteration adds at most one node, in four steps.
x-new equals x-near, plus eta times the quantity x-rand minus x-near, divided by the distance between x-rand and x-near.
Point from x-near at x-rand. Shrink that arrow to length 1, then stretch it to length eta. Add it to x-near to get x-new. If x-rand is closer than eta, x-new is x-rand itself.
With made-up numbers: the tree holds (1, 1), (2, 1.5) and (1.5, 3). The sample is xrand = (5, 4). Distances: 5.00, 3.91, 3.64, so xnear = (1.5, 3). The arrow is (3.5, 1.0) with length 3.64; as a direction it is (0.962, 0.275). With η = 1.0, xnew = (2.462, 3.275), exactly 1.0 from xnear.
Press Step. The blue star is xrand, the ringed node is xnear, and the bright dot is xnew. Turn on Voronoi cells to see which nodes own the most empty space.
Illustrative field, 1.6 by 1.0, no goal: pure exploration. Voronoi cells show only while the tree has 400 nodes or fewer.
Why does the tree rush outward? Draw the Voronoi cell of each node: the region of points closer to it than to any other node. Nodes on the frontier own large cells; nodes inside own small ones. A uniform sample lands in a large cell more often, so frontier nodes get extended most. This pull into empty space is the Voronoi bias.
A large η makes long edges that need many checks and fail often near walls. A small η makes the tree crawl. To measure it, count the rejected steps per added node. The fix is an η near the width of the narrowest gap you must pass.
Chapter 4
Turn the pull up and down, and find where the tree reaches the goal fastest
Plain RRT explores evenly. It can wander for thousands of iterations before a sample falls near the goal. Goal bias fixes this with one change to the Sample step.
Nearest, steer and check stay the same. With p = 0.05, about 1 iteration in 20 reaches for the goal.
| Iteration | z | z < 0.05? | xrand is |
|---|---|---|---|
| 1 | 0.71 | no | a random sample |
| 2 | 0.03 | yes | the goal |
| 3 | 0.88 | no | a random sample |
| 4 | 0.42 | no | a random sample |
| 5 | 0.01 | yes | the goal |
Two of five aim at the goal in this made-up run. That is luck over a short run; the long-run share is 5%.
A pink cup sits between the start and the goal. Set the bias p and press Grow. Then press Average 20 runs to compare settings. Try p = 0, p = 0.1 and p = 0.9.
Illustrative toy: η = 0.05, at most 3,000 iterations per run. The averages use 20 fixed random seeds, so a setting gives the same answer each time.
Why not p = 1? Then every iteration extends the node closest to the goal, straight at the goal. The tree marches into the cup and every step hits the wall. The random samples are what let it find the way around.
It succeeds when xnew lands within η of the goal and the last line is free. Then it walks the parent links back to the start. It gives up after an iteration limit K. That "failure" means "not found in K tries", not "no path exists".
The symptom is a tree that piles up against one wall. To measure it, count the share of rejected steps among goal-aimed iterations. The fix is a smaller p; common values are 0.05 to 0.1.
Chapter 5
Run many trials through a gap and watch the chance of success climb toward one
A planner is complete if it finds a path whenever one exists and reports failure when none does. Grid A* is resolution-complete: complete at the grid's resolution. RRT gives a weaker promise.
RRT is probabilistically complete. Suppose a path exists with some clearance from every obstacle. Then the chance that RRT has found a path rises toward 1 as the samples grow. Call the number of samples n. Call the problem's own speed constant a, a positive number that depends on the free space and the goal.
The probability of a path found within n samples is greater than one minus e to the minus a n.
Multiply a by n and raise e to minus that. Subtract from 1. The chance of failure, e to the minus a n, shrinks by the same factor for every extra a-worth of samples.
With a made-up a = 0.01: at n = 100, the bound is 1 − e−1 = 0.63. At n = 300 it is 0.95. At n = 500 it is 0.993. No finite n makes it exactly 1.
A wall with one gap stands between the start and the goal. Press Run 30 trials. The green curve is the share of trees that found a path by each sample count. Then narrow the gap and run again.
Illustrative toy: η = 0.05, goal bias 0.05, 30 fixed seeds, at most 1,500 samples each. We measure the curve; it is not the bound above.
This bound is Theorem 16 in Karaman and Frazzoli (2011), who credit LaValle and Kuffner (2001). Two limits follow from it. If no path exists, RRT can never say so; it only keeps searching. And it says nothing about how long the path is.
Karaman and Frazzoli also proved that RRT is not asymptotically optimal. The cost of the best path RRT returns converges to a value above the optimum, with probability one. A node's parent never changes once set. Later samples cannot repair an early detour.
| Planner | Promise | Catch |
|---|---|---|
| Grid A* | resolution-complete; optimal on the grid | kd cells |
| RRT | probabilistically complete | path cost stays above optimal; cannot prove "no path" |
| RRT* | probabilistically complete and asymptotically optimal | more work per node; optimal only in the limit |
The symptom is a planner that times out in a narrow passage. To measure it, run many seeds and plot the success share against samples, as above. Fixes: more samples, or samples placed near obstacles.
Chapter 6
Race RRT against RRT* on the same samples and watch one path keep shrinking
RRT*, from Karaman and Frazzoli (2011), adds two repair steps to every iteration. Each node stores its cost-to-come c(x): the length of the tree path from the start to x.
Call the set of nodes within r of xnew Xnear.
c of x-new equals the minimum, over x in X-near, of c of x plus the distance between x-new and x.
For each neighbour x, add its cost c(x) to the edge length from x to x-new. The neighbour with the smallest sum becomes the parent, and that sum becomes c(x-new).
With made-up numbers, xnew = (4, 4). The nearest node (3, 4) has c = 6.0 and is 1.0 away: 7.0. Node (4, 2.5) has c = 4.5 and is 1.5 away: 6.0. Node (2.5, 3) has c = 5.0 and is 1.80 away: 6.80. RRT* picks (4, 2.5): c(xnew) = 6.0, one unit below RRT's 7.0.
Rewiring, with the same numbers: a neighbour at (5, 5) has c = 8.0. Through xnew it would cost 6.0 + √2 = 7.41. That beats 8.0, so (5, 5) takes xnew as its parent, and its whole subtree gets 0.59 cheaper.
Both trees draw the same random samples. Press Grow both. RRT's cost line goes flat; RRT*'s keeps falling toward the dotted lower bound.
Illustrative toy: η = 0.05, goal bias 0.05. RRT* uses r = min(1.0 × (log n / n)1/2, 0.15). The toy caps r at 0.15 instead of η, so rewiring shows early.
How big is r? It shrinks as the tree fills. Call the node count n, the dimension d, and a constant γ (gamma).
r-n equals the smaller of gamma times log n over n, to the power one over d, and eta.
Divide log n by n. Take the d-th root and multiply by gamma. If that is larger than eta, use eta. The radius shrinks, yet each ball still holds about log n neighbours.
With γ = 1.0, d = 2 and a large η: n = 100 gives r = 0.215. n = 1,000 gives 0.083. n = 10,000 gives 0.030. For a 1.6 by 1.0 field that is 80% free, the theorem's lower limit on γ is 0.87, so 1.0 qualifies.
With γ above that limit, RRT* is asymptotically optimal (Theorem 38 in the paper): its best path cost converges to the optimum, with probability one. Its total work stays within a constant factor of RRT's.
The symptom is an RRT* whose cost stops falling. To measure it, log the neighbour count per iteration. The usual cause is a fixed small radius or a γ below the limit; use the shrinking radius above.
Chapter 7
Snip needless corners off a raw path, then meet edges that obey the wheels
Sometimes a short path is enough, and the optimum is not needed. Shortcutting cleans up a raw RRT path cheaply. The rule fits on one line: if a node's neighbours on the path see each other through free space, delete the node.
Worked by hand: a raw path runs A = (0, 0), B = (2, 3), C = (4, 0). Its length is √13 + √13 = 7.21. If the line from A to C is free, B goes. The new length is 4.00, which is 45% shorter.
Press Find for a raw RRT path. Then press Shortcut and watch corners vanish one by one as the path pulls tight against the obstacles.
Illustrative toy: η = 0.05, goal bias 0.05. Each removal checks one line at a gap of 0.006.
Shortcutting is local. It tightens the path on its own side of each obstacle. It never jumps to a shorter route on the far side of a wall; for that you need RRT*.
Lesson 2's wheeled robot cannot slide sideways. A path with a 90 degree corner makes it stop, turn in place and start again. Kinodynamic planning puts the robot's motion limits into every edge. The state now holds heading, and maybe speed, as well as position.
| Way to grow an edge | How | Trade-off |
|---|---|---|
| Forward propagation | pick a random control and duration; simulate the robot from xnear | every edge is drivable; it cannot aim at a chosen state |
| Steering function | solve "drive from x to x′" exactly | aims well; needs a solver for the robot model |
Two car models have exact steering solutions. The Dubins car drives forward only, at fixed speed, with a minimum turning radius. The Reeds-Shepp car may also reverse. Lesson 5 then turns the path into a timed trajectory the controller can track.
The symptom is a robot that stops and spins at every waypoint. To measure it, compute the turn angle at each path node. The fix is Dubins steering, or a smooth trajectory fit in Lesson 5.
Chapter 8
Drop your own obstacles and run every knob of the lesson at once
This is the full planner: sample, goal bias, nearest, steer, check, add, and RRT*'s choose-parent and rewire. Tap empty space to drop an obstacle. There is no grid anywhere in it.
Tap the field to drop a pink disc. Choose RRT or RRT*, set η and p, and press Grow. Step adds one sample.
Illustrative toy, 1.6 by 1.0, at most 3,000 samples per run. RRT* uses the Chapter 6 radius with the toy cap of 0.15.
Things to try, one per chapter:
Chapter 9
Keep the terms, the numbers, the code and the choice of planner in one place
A grid has kd cells, so high-dimensional robots sample instead. Sampling planners use three operations: sample, collision-check, connect. A PRM wires samples into a reusable graph. An RRT grows a tree one step of η at a time and finds a path with probability rising to 1. RRT* adds choose-parent and rewiring, and its path cost converges to the optimum.
| Term | Meaning | Number to remember |
|---|---|---|
| Grid size | N = kd | k = 100, d = 6: 1012 cells |
| Line check | p(t) = a + t(b − a), t from 0 to 1 | (1, 1) to (4, 5): 5 points, length 5 |
| PRM edge | distance ≤ r and the line is free | 1,000 vertices: 499,500 pairs without r |
| Steer | xnew = xnear + η times the unit direction | (1.5, 3) toward (5, 4), η = 1: (2.462, 3.275) |
| Goal bias | with chance p, xrand = goal | p = 0.05 to 0.1 |
| Probabilistic completeness | P(found within n) > 1 − e−an | a = 0.01: 0.63 at n = 100, 0.993 at n = 500 |
| Choose parent | c(xnew) = min [c(x) + edge] | 7.0 by nearest, 6.0 by cheapest |
| RRT* radius | rn = min{γ(log n / n)1/d, η} | γ = 1, d = 2: 0.215, 0.083, 0.030 at n = 102, 103, 104 |
| Shortcut | drop a node if its neighbours see each other | 7.21 to 4.00 |
| Situation | Pick | Why |
|---|---|---|
| Floor robot, 2 or 3 dimensions | grid A* (Lesson 3) | the grid fits; optimal on the grid; can report "no path at this resolution" |
| Fixed layout, many queries | PRM | one build serves every query |
| One query, any dimension | RRT with goal bias | fast first path |
| Arm in clutter, time to spare | RRT* | usable path early, shorter the longer it runs |
| Car-like robot | RRT or RRT* with Dubins or Reeds-Shepp steering | every edge is drivable |
python import numpy as np def rrt(start, goal, sample, is_free_line, eta, p, K, star=False, gamma=1.0): V, parent, cost = [np.array(start)], [-1], [0.0] for k in range(K): x_rand = np.array(goal) if np.random.rand() < p else sample() # goal bias d = [np.linalg.norm(v - x_rand) for v in V] i = int(np.argmin(d)) # nearest step = x_rand - V[i] x_new = x_rand if d[i] <= eta else V[i] + eta * step / d[i] # steer if not is_free_line(V[i], x_new): continue best, c_new = i, cost[i] + np.linalg.norm(x_new - V[i]) near = [] if star: # choose parent n = len(V) + 1 r = min(gamma * (np.log(n) / n) ** (1 / len(x_new)), eta) near = [j for j, v in enumerate(V) if np.linalg.norm(v - x_new) <= r] for j in near: c = cost[j] + np.linalg.norm(x_new - V[j]) if c < c_new and is_free_line(V[j], x_new): best, c_new = j, c V.append(x_new); parent.append(best); cost.append(c_new) new = len(V) - 1 for j in near: # rewire c = c_new + np.linalg.norm(V[j] - x_new) if c < cost[j] and is_free_line(x_new, V[j]): parent[j] = new # a full version also updates the costs of j's subtree cost[j] = c if not star and np.linalg.norm(x_new - goal) <= eta and is_free_line(x_new, goal): break # RRT stops at the first path return V, parent, cost
Based on Stanford AA 274A, Principles of Robot Autonomy I. This page re-teaches the course's fourth lesson in our own words, devices and toy numbers.
Now press Present or Teach and explain from memory: k to the d, the steer step and the goal-bias coin. Then explain what probabilistic completeness promises, and choose-parent with its 7.0 against 6.0. Then go back to the hero, pick RRT*, and say why its path keeps getting shorter.