A robot needs a map, a start, and a goal
Think about the map app on a phone. You say where you want to go. The app already knows where you are. It looks at the roads and gives you a route. A robot does the same thing. This job is called path planning: picking a safe route from where you are to where you want to be.
Every plan needs three things.
Where does the map come from? Sometimes people give it to the robot. Often the robot builds its own with sensors. A sensor is a part that measures the world. A camera sees. A laser sensor called lidar measures how far away things are. A bump sensor feels a wall.
Turn the world into a grid
A real room is smooth and messy. Computers like things simple. So we chop the room into a grid, like graph paper. Each square is a cell. A cell is either free (the robot can stand there) or blocked (an obstacle fills it).
From any cell, the robot can move to its neighbors: the cells touching it on the top, right, bottom, and left. We skip diagonals to keep things simple. A cell that is blocked is never a neighbor you can enter.
Now the hard question is simple to say: which chain of neighbor-to-neighbor moves gets from the start to the goal? A robot could try random moves, but that wastes time. Smart search methods try moves in a careful order. We will look at two famous ones.
Breadth-first search: ripples in a pond
Drop a pebble in a pond. A ripple spreads out in a ring. It reaches everything 1 step away, then everything 2 steps away, then 3, and so on. It never skips ahead. Breadth-first search (BFS) works like that ripple.
- Put the start cell in a waiting line. This line is called the frontier: cells we have found but not looked at yet.
- Take the first cell from the line. It is now explored.
- Add its free, never-seen neighbors to the back of the line. Remember which cell each one came from.
- Repeat until you take the goal out of the line. Then follow the "came from" notes backward to rebuild the path.
Because the line is first-in, first-out, the nearest cells always get checked first. So the first time BFS reaches the goal, it has found a path with the fewest steps. That is a great promise. The price is that BFS looks in every direction, even away from the goal. In a big room that is a lot of wasted looking.
Where we are going: in the Maze lab below, the glowing amber cells are the frontier and the blue-green cells are the explored ones. You will see BFS grow as a ring.
A*: search with a hint
Imagine finding a friend in a big building. You know she is somewhere to the east. Would you check every room to the west first? No! You use a hint about where she probably is. A* (say "A-star") gives the robot a hint too.
The hint is called a heuristic, which just means a quick smart guess. Here it is the number of blocks left to the goal if there were no walls, like counting city blocks. For each cell, A* adds two numbers:
f = g + h
- g is the cost of the path from the start to this cell. It is what we know.
- h is the hint: our guess of how much is left to reach the goal.
- f is the total guess for a route that goes through this cell. A* always checks the cell with the smallest f next.
Cells pointing toward the goal have a small h, so they get checked first. The search stretches toward the goal like a beam of light instead of a ring. If the hint hits a wall, g keeps growing and A* switches to other cells.
There is one rule. The hint may never guess more than the real distance. Counting blocks never does, because a real path can only be as long or longer than the no-walls count. Follow that rule and A* still finds the best path. It just looks at far fewer cells.
Not every step costs the same
"Shortest" is not always "best." A robot rolling on smooth pavement moves fast and saves battery. A robot in mud moves slowly and uses much more power. We write this as a cost, a number for how hard each step is. In this lesson, a pavement step costs 1 and a mud step costs 5.
The total path cost is the sum of all step costs. Now a short path can be worse than a long one:
| Route | Steps | Path cost |
|---|---|---|
| Straight through the mud | 12 | 12 + 4 × 4 extra = 28 |
| Around the mud, on pavement | 24 | 24 |
Plain BFS only counts steps, so it takes the 12-step road through the mud. To handle costs, we change the waiting line. Instead of "first in, first out," we take the cell with the lowest total cost so far (g). This idea is called Dijkstra's algorithm (say "DYKE-struh"). It is A* with no hint. A* adds the hint on top and gets the same best answer, usually faster. The lab has all three so you can compare.
Maze lab: watch the search think
Paint walls and mud. Move the start and goal. Pick a search method and step through it one cell at a time, or press Play. The real algorithm is running in your browser.
Try this: load "Mud river" and run BFS, then A*. BFS charges straight through the mud because it only counts steps. A* walks around. Next load "Open field" and compare the number of cells explored. The hint saves A* a lot of work.
Now you plan the route
Computers check thousands of cells in a blink. Can you beat that with your eyes? Drive the robot from S to G. Each pavement step costs 1 and each mud step costs 5. Walls stop you, and each bump gets counted. When you reach the goal, we show the best possible route in blue so you can compare.
Tip: turn on the hint. The numbers show the A* guess for each cell. Following the smaller numbers is exactly what A* does, but A* also adds up the real cost so far.
Replanning: the world will not hold still
A plan is only as good as the map it was made from. People walk by. Boxes fall. A door closes. A robot that follows its first plan no matter what will crash or get stuck.
So robots re-plan. As the robot moves, its sensors keep checking the next square. If the square is now blocked, the robot updates its map and runs the search again from where it stands now. It does not go back to the start. Real robots do this many times each second.
Fancier planners such as D* Lite reuse work from the old plan, so they re-plan faster. The idea is the same: sense, update the map, plan again.
Quick answers
What is robot path planning?
Path planning is how a robot picks a route from where it is to where it wants to be without hitting anything. The robot needs a map, a start, and a goal. A planning algorithm then searches the map for a safe route, often the shortest or cheapest one.
What is the difference between breadth-first search and A*?
Breadth-first search explores in rings, like ripples in a pond, checking every square at distance 1, then distance 2, and so on. A* adds a hint, an estimate of how far each square is from the goal, so it explores squares that look closer to the goal first. A* usually checks far fewer squares.
Does A* always find the shortest path?
Yes, as long as its hint never guesses a distance that is bigger than the real one. Straight-line or block-counting distance never overestimates on a grid, so A* with that hint finds a path with the lowest total cost.
What is a heuristic?
A heuristic is a quick, smart guess. In path planning it is an estimate of the distance left to the goal. It is cheap to compute and it helps the search decide which square to try next.
How does a robot know what the map looks like?
Some robots are given a map. Others build one with sensors such as cameras, lidar (laser distance sensors), or bump sensors. They mark each square as free or blocked, and many robots keep updating the map as they move.
What happens when something blocks the robot's planned path?
The robot re-plans. It updates the map with the new obstacle, then runs the search again from its current position to the same goal. Real robots do this many times per second.
Quick quiz
1. How does breadth-first search explore a grid?
2. What does A* add that plain BFS does not have?
3. A box lands on the next square of the robot's planned route. What should it do?
Keep learning about robots and search
You just ran real search algorithms. Explore what makes a machine a robot, other ways computers search for good answers, and how a different kind of algorithm picks what you see next.
The warehouse and its robot are made-up examples created for this tutorial.