Search in an agent's world
When an agent tries to solve a problem, it is often searching through a state space: every possible situation it could be in. Each state has neighbors, which are the states reachable by one action.
The agent wants to find a good state, such as a solution, a lower cost, or a higher reward. Search algorithms tell it which neighbor to try next.
Key idea: many AI problems are search problems. The art is choosing where to look and when to stop.
Imagine a coding assistant that wants to rewrite a function so it runs faster. Every version of the function is a state. Its neighbors are small edits: rename a variable, reorder a loop, replace a list with a set. The agent needs a way to score each version and a way to move toward better ones.
Search problems everywhere
Hill climbing and search show up whenever you are trying to improve something step by step. Here are a few everyday problems dressed up as search:
Route planning
A delivery robot is at intersection A and needs to reach intersection B. Each state is an intersection. Each neighbor is a connected street. A score might be negative distance, so the robot climbs toward the destination.
Exam scheduling
A school must schedule hundreds of exams into a few rooms without conflicts. A state is one possible schedule. Neighbors swap two exams. The score counts the number of conflicts, so the school wants to climb toward zero conflicts.
Hyperparameter tuning
Training a neural network means choosing values like the learning rate and batch size. Each state is a different set of values. Neighbors change one value by a small amount. The score is the model's accuracy on a validation set.
Code refactoring
An AI agent wants to make a function faster. A state is the source code. Neighbors are tiny edits. The score could be measured in milliseconds, test coverage, or linter warnings.
Why the same algorithm works for all of these: each problem has states, neighbors, and a score. Once you represent the problem that way, you can plug in a search algorithm.
The hill-climbing idea
Hill climbing is greedy: always move to the best neighbor right now. Here is the basic loop:
- Start at some state.
- Look at all neighboring states.
- Move to the neighbor with the best value.
- Stop when no neighbor is better.
It is fast because it never plans far ahead. But because it never plans far ahead, it can get stuck.
Worked example: one-dimensional landscape
Imagine a mountain range drawn as a single line. Higher is better. You start at the left, and you can only see a short distance. You walk uphill until there is nowhere left to go.
If you start on the left and always walk uphill, you will stop at the first small peak. To find the highest peak, you need a way to explore beyond the nearest hill.
Pseudocode
function hillClimb(start):
current = start
while True:
neighbor = bestNeighbor(current)
if score(neighbor) <= score(current):
return current // no better move
current = neighbor
See it in Python
The same idea fits into just a few functions. Here is a tiny one-dimensional landscape with a hill-climbing search and a random-restart wrapper. The code always moves to the taller neighbor and stops when it reaches a local peak.
This code is intentionally simple, but it has the same parts as the interactive terrain demo: a state (the position), neighbors (left and right), and a score (height). Random restart runs the search from many starting points to improve the chance of finding the highest peak.
Flavors of hill climbing
Not every hill climber behaves the same way. The version above always picks the best neighbor, but there are useful variants:
- Steepest-ascent hill climbing: look at every neighbor, then move to the very best one. This is the classic version.
- Stochastic hill climbing: pick a random neighbor that is better than the current state. It is cheaper because it does not compare all neighbors, but it can wander more.
- First-choice hill climbing: keep generating neighbors until you find one that is better, then move there immediately. Good when generating neighbors is easy but scoring many of them is expensive.
- Random-restart hill climbing: run hill climbing many times from random starting points, then return the best result found. This is one of the simplest ways to escape local maxima.
Each flavor trades off exploration (trying new places) and exploitation (improving the current place). First-choice and stochastic are lighter on computation. Random-restart spends more compute to search more broadly.
Local vs global maxima
A local maximum is the best state in a small region, but not the best overall. A global maximum is the best possible state. Hill climbing can easily get trapped on a local maximum.
There are other traps too:
- Plateau: a flat area where many neighbors have the same score. The search can wander with no clear direction.
- Ridge: a narrow path uphill where direct neighbors do not look better, but diagonal moves do. Plain hill climbing may crawl slowly or miss the ridge.
- Shoulder: a flat region that eventually rises again. The algorithm may stop too early if it does not allow sideways moves.
Escaping local optima
Computer scientists use tricks to escape local maxima without searching every possible state. Here are the most common ones:
- Random restart
- Start over from a random place and run hill climbing again. Simple, effective, and easy to parallelize.
- Simulated annealing
- Occasionally accept a worse move, especially early in the search. As time passes, the algorithm becomes pickier, cooling down like a metal.
- Beam search
- Keep the best k candidates at once instead of one. This spreads the search across several hills at the same time.
- Tabu search
- Remember recent states and temporarily forbid returning to them. This prevents the search from cycling around the same small peak.
Key idea: every trick either looks farther ahead, starts in more places, or temporarily accepts worse moves. Each costs something, but they make local search much more powerful.
Climb the terrain
Click anywhere on the grid to place the starting position. Then press Climb one step to move uphill, or Run hill climbing to keep going until the agent gets stuck. Try Random restart to start over from a new spot, or Run 20 restarts to see how different starting points produce different peaks.
Check yourself
1. Why is random restart a useful fix for hill climbing?
Sample answer: Random restart lets the search start from a different position, increasing the chance of finding a taller peak and escaping a local maximum.
2. Name a real-world problem from the examples above that can be framed as hill climbing. What would the state, neighbor, and score be?
Sample answer: Exam scheduling: the state is a full schedule, a neighbor swaps two exams, and the score is the number of conflicts. Lower conflicts are better, so the algorithm climbs toward a conflict-free schedule.
3. What is the difference between a plateau and a ridge?
Sample answer: A plateau is a flat area where neighbors have the same score, so the search has no clear direction. A ridge is a narrow uphill path where direct neighbors do not look better, but diagonal moves lead higher.
Quick quiz
1. What does hill climbing do at each step?
2. What is a local maximum?
3. Which technique can help hill climbing escape a local maximum?
Keep learning
Hill climbing is a fast way to improve a solution, but it needs tricks to escape local peaks. The next tutorial introduces stronger search ideas like simulated annealing, beam search, and genetic algorithms.
Part of the “Agentic Engineering & AI Software Topics” series.