Robot Foundations · Module 5: Reaching a Goal
Finding a Path on a Grid
Spread outward from the start one step at a time. The step on which you reach the goal is the length of the shortest path, and the labels show the way back.
Picture dropping a pebble in a pond. Ripples spread in rings, each one a little further out, and no ring reaches a place before a shorter route would have. Now imagine the pond has walls in it. The ripples bend around them, and the first ripple to arrive anywhere has taken the shortest way. That picture is a complete path-finding method, simple enough to do on paper, and it is the core of how many real robots plan.
What you will be able to do
- Describe a map as a grid of free and blocked cells.
- Run the wave method by hand on a small grid and label every cell with its number of steps.
- Trace the shortest path back from the goal.
- Tell when there is no path, and say what the lab's planner does about diagonal steps and obstacle margins.
The idea
To plan on a computer, the map has to be something a program can step through. The simplest way is a grid: cut the floor into squares of equal size and mark each one free or blocked. In the lab the squares are 0.5 m on a side.
The robot moves from a square to one of its four neighbours: up, down, left or right. (Diagonal moves come later.) A path is then a chain of squares, each next to the last, and its length is the number of steps.
The wave method
- Write 0 in the start square.
- Look at every square labelled 0. Write 1 in each free neighbour that has no label yet.
- Look at every square labelled 1. Write 2 in each of their free, unlabelled neighbours.
- Carry on: squares labelled k give the label k + 1 to their free, unlabelled neighbours.
- Stop when the goal gets a label, or when a whole round adds nothing.
The label of a square is the smallest number of steps to reach it from the start. This is true because squares are labelled in order of distance: all the 3s are written before any 4. The first time the wave touches a square, it came the shortest way.
Tracing the path back
The labels hold the answer. Start at the goal. Among its neighbours, step to the one whose label is exactly one smaller. Repeat. Each step lowers the label by one, and you must arrive at 0, which is the start. Write the squares down in reverse order and you have the path.
Label outward from the start. Walk back from the goal, always to a neighbour one lower. That is a shortest path.
When there is no way
If a round of the wave adds no new square and the goal is still unlabelled, every square the robot can reach has been labelled, and the goal is not among them. There is no path. The method finds this out without being told: it simply runs out of wave. A robot should then stop and say so, not keep trying.
What the lab's planner does beyond this
- Diagonal steps. A move to a diagonal neighbour is allowed, but it covers 1.41 cell widths, not 1 (it is the diagonal of a square). Because steps then cost different amounts, the wave must spread by distance covered, not by number of steps. That version is called Dijkstra's algorithm. It is the wave method with prices.
- Margins. Real robots have width. The planner makes every wall one cell thicker ("inflates" it), so that a path through free squares keeps a margin of a cell from the true wall. With 0.5 m cells that is half a metre.
- Unknown squares. The planner treats squares it has not yet seen as free, which is optimistic. The last lesson of this module is about what happens when that is wrong.
- Speed. The wave explores in every direction, including away from the goal. Cleverer versions, such as A* (pronounced "A star"), use a guess at the remaining distance to explore less. When the World Changes: Obstacles and Replanning comes back to replanning, and the Navigation track on this site teaches A* and its relatives.
A grid trades accuracy for simplicity. Smaller squares follow the real world more closely, but there are four times as many squares each time the size is halved, and the wave takes proportionally longer.
Words to know
- Grid
- A map cut into equal squares called cells, each marked free or blocked. On Pip: cells of 0.5 m in the lab.
- Cell
- One square of the grid. On Pip: 0.5 m by 0.5 m, a quarter of a square metre.
- Neighbour
- A cell that touches another. Four if only up, down, left and right count, eight if diagonals do too. On Pip: its next move goes to a free neighbour.
- Wave (breadth-first search)
- Labelling cells in rings of equal step count outward from the start. On Pip: 14 rings reach the goal in the worked example.
- Label (distance)
- The number written in a cell: the fewest steps from the start. On Pip: the goal has the label of the whole path.
- Dijkstra's algorithm
- The wave method for steps with different costs, such as diagonals. On Pip: used in the lab to plan with diagonal moves.
- Inflation
- Making obstacles bigger on the map so that paths keep a margin. On Pip: one cell, half a metre, in the lab.
A worked example: the wave in the figure
The grid in the figure has 54 cells, of which 38 are free. Follow the method.
- Step 0. The start S, bottom left, gets 0. It is the only cell of its ring.
- The first rings. The number of cells first reached at each step count, from 0 upward, is 1, 2, 3, 3, 4, 3, 2, 2, and so on. The rings are small because the start sits in a corridor between walls: the wave has only a few ways to go.
- The wall. The cells to the right of S fill in along the bottom row up to the wall, where the wave stops: it cannot go through a wall. The only way on is up the left side, then along the third row, which is the gap in the wall.
- Reaching the goal. The goal G gets the label 14. The wave has now spread over 37 of the 38 free cells. Everything it labelled after that is of no use.
- Tracing back. From G, step to the neighbour labelled 13, then 12, and so on down to 0. The path has 14 steps and 4 turns. Its length in metres is 14 × 0.5 = 7 m, compared with a direct distance from S to G of 4 m: the wall costs 3 m of detour.
A grid with no way through. In a grid whose rows are S.#.., ..#.., ..#.G (S at top left, G at bottom right, a full wall in the middle column), the wave labels 6 cells on the left side and then stops. The goal is never labelled: no path.
Try it yourself
Copy this small grid onto squared paper. S is the start, G the goal, # is a wall. Run the wave method by hand and write the labels in.
S . . # . # . . . # # G
Answer
The labels, row by row, are: 0 1 2 #, then 1 # 3 4, then 2 # # 5. The two cells under S get 1 and 2 but lead nowhere: the wall closes them in. The wave that matters runs along the top row to the 2, drops to 3 below it, then goes right to 4 and down to G, which gets 5. Tracing back from G through 4, 3, 2, 1 gives the path: right, right, down, right, down. It has 5 steps.
Common mistakes
"The wave takes a step on every cell, so it takes too long."
It labels each cell once and then never looks at it again, so the work is proportional to the number of cells, not to the number of paths. A grid of a million cells is a matter of milliseconds on a modern computer.
"I will always choose the neighbour closest to the goal in a straight line."
That is a guess, not a method, and it walks into dead ends. The wave method does not guess: it labels everything in order, and the labels tell the truth.
"The grid is the real world."
It is a simplified copy. A cell that is "free" may hold a thin cable, and a "blocked" cell may be half empty. Margins and a sensible cell size keep the copy safe to use.
Remember this
- A grid map is cells marked free or blocked. A path is a chain of neighbouring free cells.
- The wave method labels cells with their steps from the start, in rings.
- Trace back from the goal by always moving to a neighbour with a label one lower.
- If the wave runs out before reaching the goal, there is no path. Diagonal steps and margins refine the method.
Check yourself
Three questions. Write an answer in your own words, then reveal the explanation. Nothing you type leaves your browser.
Question 1
In the wave method, a cell far from the start gets the label 12. What does that number mean?
Explanation
The shortest path from the start to that cell has 12 steps. Squares are labelled in order of distance, so the first time the wave reached the cell was by the shortest route.
Question 2
The wave labels every reachable cell, and the goal is still unlabelled. What should the robot conclude, and what should it do?
Explanation
There is no path from the start to the goal on this map. It should stop and report that the goal is unreachable. It could also try again after the map changes, for instance when something blocking the way moves or when more of the map is discovered.
Question 3
A grid uses 0.5 m cells. The wave reaches the goal at label 18. How long is the path, and how long would it be if the diagonal moves had been allowed and cut off six corners (each saving about 0.59 of a cell width)?
Explanation
Without diagonals: 18 × 0.5 = 9.0 m. Each corner cut replaces two steps (2 cell widths) by one diagonal (1.41 cell widths), saving about 0.59 cell widths, which is about 0.29 m. Six of them save about 1.8 m, giving roughly 7.2 m.
What’s next
A path through cells is a list of squares. The next lesson turns it into steering: how the controller of module 3 follows a path instead of aiming at a single goal.
Where this goes on the site
Nothing below is needed to continue this course. These pages take the topic of this module further, and most assume more mathematics or Python than this course does.
- LaterNavigation
Sixty lessons on planning and driving, from costmaps to fleets.
- LaterA* Search with a Heuristic
The grid search of this module, made fast.