Planning by Throwing Darts

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.

Grow a tree to the goal

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

The Grid That Ate Your Computer

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.

Say it aloud

N equals k to the d.

N
N the number of grid cells the planner must store and check.
k
k the buckets on each axis: 100 buckets cut a joint's turn into steps of 3.6 degrees.
d
to the d the number of dimensions, one per joint. It sits in the exponent.
In plain words

Multiply k by itself once for each of the d axes. Each new joint multiplies N by k again.

With numbers

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.

Add joints, count the cells

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.

20

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 failure, as symptom, measurement and fix

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.

The road

  1. Probe the space: sample, collision-check, connect.
  2. Probabilistic roadmaps: build one graph, answer many queries.
  3. RRT: grow a tree one step at a time (the hero's plain RRT).
  4. Goal bias: a small pull toward the target (the hero's middle button).
  5. The promises: what "probabilistically complete" means, and why RRT paths stay long.
  6. RRT*: choose the cheapest parent and rewire (the hero's RRT*).
  7. Smoothing: cut corners, then make the path drivable.
  8. The playground: every knob at once, on your own obstacles.
  9. The field guide: terms, numbers, code, which planner to pick, and the credit.
Why does grid A* stop working for a 6-joint arm?

Chapter 1

Do Not Map the Space, Probe It

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.

  1. Sample. Draw a random configuration from the C-space. Six random angles cost about as much as two.
  2. Collision-check. Ask the collision checker, a function that takes one configuration and returns free or not free.
  3. Connect. Join two free configurations with a short straight line, and check the line too.

Throw darts, keep the free ones

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).

Say it aloud

p of t equals a, plus t times the quantity b minus a.

p(t)
p of t the point at fraction t along the line, a configuration.
a
a the start of the segment, a free configuration.
b
b the end of the segment, another free configuration.
t
t how far along: 0 is a, 1 is b, 0.5 is the middle.
In plain words

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 numbers

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 failure, as symptom, measurement and fix

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.

What does the collision checker do in a sampling planner?

Chapter 2

Probabilistic Roadmaps: Build Once, Query Many

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.

  1. Build. Sample n configurations and keep the free ones. Join each pair closer than a connection radius r if the line between them is free.
  2. Query. Join the start and the goal to nearby vertices. Run Dijkstra or A* over the graph.

"Closer than r" needs a distance. Call two configurations x and x′, with coordinates x1, x2 and x′1, x′2.

Say it aloud

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.

x
x one free configuration, a vertex of the roadmap.
x′
x-prime a second free configuration.
x1
x-one the first coordinate of x. In a d-joint C-space there are d of them.
x2
x-two the second coordinate of x.
√
the square root of undoes the squares, so the result is a length.
In plain words

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 numbers

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.

Build a roadmap, then query it

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.

0.30

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 failure, as symptom, measurement and fix

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.

When does a PRM's up-front build cost pay off?

Chapter 3

RRT: Grow a Tree From the Start

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.

  1. Sample a random configuration, xrand.
  2. Nearest: find the tree node closest to it, xnear.
  3. Steer: step from xnear toward xrand by at most a step size η (eta). The step ends at xnew.
  4. Check and add: if the line from xnear to xnew is free, add xnew with xnear as its parent.
Say it aloud

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.

xnew
x-new the candidate node at the end of the step.
xnear
x-near the tree node closest to the sample.
η
eta the step size: the longest edge the tree adds, here 1.0.
xrand
x-rand the random sample this iteration drew.
‖…‖
the distance between x-rand and x-near dividing by it turns the step into a direction of length 1.
In plain words

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 numbers

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.

Step the tree one sample at a time

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.

0.08

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.

The failure, as symptom, measurement and fix

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.

Why does an RRT grow mostly into open, unexplored space?

Chapter 4

Goal Bias: A Small Pull Toward the Target

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.

  1. Draw a number z uniformly between 0 and 1.
  2. If z is less than the bias p, set xrand to the goal.
  3. Otherwise draw xrand at random as before.

Nearest, steer and check stay the same. With p = 0.05, about 1 iteration in 20 reaches for the goal.

Iterationzz < 0.05?xrand is
10.71noa random sample
20.03yesthe goal
30.88noa random sample
40.42noa random sample
50.01yesthe 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%.

Tune the pull toward the goal

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.

0.10

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.

When does it stop?

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 failure, as symptom, measurement and fix

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.

What happens when the goal bias p is close to 1?

Chapter 5

What RRT Promises, and What It Does Not

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.

Say it aloud

The probability of a path found within n samples is greater than one minus e to the minus a n.

P(…)
the probability of the share of runs, over many runs, in which it happens.
n
n the number of samples so far. The bound holds once n passes some problem-dependent start n0.
e
e the number 2.718. e to a large negative power is close to 0.
a
a the speed constant, above 0. A narrow gap makes it small.
In plain words

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 numbers

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.

Run 30 trees through a gap

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.

0.12

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.

RRT paths stay long

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.

PlannerPromiseCatch
Grid A*resolution-complete; optimal on the gridkd cells
RRTprobabilistically completepath cost stays above optimal; cannot prove "no path"
RRT*probabilistically complete and asymptotically optimalmore work per node; optimal only in the limit

The failure, as symptom, measurement and fix

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.

"RRT is probabilistically complete" means what?

Chapter 6

RRT*: Rewire Toward the Shortest Path

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.

  1. Choose parent. Among the nodes within a radius r of xnew, pick the parent that gives the lowest cost-to-come.
  2. Rewire. If a neighbour's route through xnew is shorter than its current one, and the line is free, make xnew its parent.

Call the set of nodes within r of xnew Xnear.

Say it aloud

c of x-new equals the minimum, over x in X-near, of c of x plus the distance between x-new and x.

c(xnew)
c of x-new the cost-to-come the new node gets: its path length from the start.
minx ∈ Xnear
the minimum over x in X-near try each neighbour within r and keep the smallest total. Only neighbours with a free line count.
c(x)
c of x the neighbour's own cost-to-come.
‖xnew − x‖
the distance between x-new and x the length of the new edge.
In plain words

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 numbers

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.

Race RRT against RRT* on the same samples

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).

Say it aloud

r-n equals the smaller of gamma times log n over n, to the power one over d, and eta.

rn
r-n the neighbour radius when the tree has n nodes.
γ
gamma a constant. The theorem needs it above a value set by d and by the free volume.
log n / n
log n over n natural log of n, divided by n. It falls as n grows.
1/d
to the one over d the d-th root, which turns a volume share into a length.
η
eta the step size. The radius never exceeds it.
In plain words

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 numbers

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 failure, as symptom, measurement and fix

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.

What makes RRT* asymptotically optimal when RRT is not?

Chapter 7

Shortcuts and Paths a Car Can Drive

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.

  1. Walk the interior nodes of the path, not the start or the goal.
  2. If the line from the previous node to the next node is free, remove this node.
  3. Repeat until a full pass removes nothing.

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.

Snip the needless corners

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*.

Edges the robot can drive

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 edgeHowTrade-off
Forward propagationpick a random control and duration; simulate the robot from xnearevery edge is drivable; it cannot aim at a chosen state
Steering functionsolve "drive from x to x′" exactlyaims 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 failure, as symptom, measurement and fix

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.

Why can a wheeled robot not follow a raw geometric RRT path directly?

Chapter 8

The RRT Playground

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.

Plan around your own obstacles

Tap the field to drop a pink disc. Choose RRT or RRT*, set η and p, and press Grow. Step adds one sample.

0.050
0.08

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:

You ring the goal with obstacles and RRT runs 3,000 samples with no path. What can you conclude?

Chapter 9

Field Guide

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.

TermMeaningNumber to remember
Grid sizeN = kdk = 100, d = 6: 1012 cells
Line checkp(t) = a + t(b − a), t from 0 to 1(1, 1) to (4, 5): 5 points, length 5
PRM edgedistance ≤ r and the line is free1,000 vertices: 499,500 pairs without r
Steerxnew = xnear + η times the unit direction(1.5, 3) toward (5, 4), η = 1: (2.462, 3.275)
Goal biaswith chance p, xrand = goalp = 0.05 to 0.1
Probabilistic completenessP(found within n) > 1 − e−ana = 0.01: 0.63 at n = 100, 0.993 at n = 500
Choose parentc(xnew) = min [c(x) + edge]7.0 by nearest, 6.0 by cheapest
RRT* radiusrn = min{γ(log n / n)1/d, η}γ = 1, d = 2: 0.215, 0.083, 0.030 at n = 102, 103, 104
Shortcutdrop a node if its neighbours see each other7.21 to 4.00

Which planner?

SituationPickWhy
Floor robot, 2 or 3 dimensionsgrid A* (Lesson 3)the grid fits; optimal on the grid; can report "no path at this resolution"
Fixed layout, many queriesPRMone build serves every query
One query, any dimensionRRT with goal biasfast first path
Arm in clutter, time to spareRRT*usable path early, shorter the longer it runs
Car-like robotRRT or RRT* with Dubins or Reeds-Shepp steeringevery edge is drivable

Checklist for any sampling planner you build

The core, in code

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

Sources

Keep going

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.

The takeaway. Do not build the space; probe it. A random tree explores by itself, goal bias aims it, and rewiring makes its path converge to the shortest one.

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.

Principles of Robot Autonomy I · Stanford AA 274A · Lesson 4: Sampling-Based Motion Planning and RRT
Previous: C-Space and A* · Next: Trajectory Optimization · Back to Gleams