Beyond hill climbing
Hill climbing is fast and easy to understand, but it is also greedy and short-sighted. When the landscape is bumpy — with many small peaks surrounding a taller one — plain hill climbing often stops too early. The rest of this tutorial introduces stronger search strategies and when to use them.
Key idea: smarter search balances exploration (trying new areas) and exploitation (improving where you already are). The right balance depends on the problem.
Simulated annealing
Simulated annealing borrows its name from metallurgy: a metal is heated and then slowly cooled so its atoms settle into a strong, low-energy structure. In search, that means accepting worse moves early on, and becoming pickier as the search cools down.
The acceptance rule
Imagine a current state with a score of 50 and a neighbor with a score of 45. Plain hill climbing would reject the neighbor. Simulated annealing might accept it, especially if the algorithm is still "hot." The probability of accepting a worse move is based on how much worse it is and on the current temperature.
// probability of accepting a worse move
e^((newScore - currentScore) / temperature)
At high temperature, even a large drop is likely to be accepted. At low temperature, the algorithm acts almost like hill climbing. This controlled randomness helps the search escape local maxima without wandering forever.
When to use it: simulated annealing is a good first upgrade from hill climbing when the landscape is rough but you can afford many iterations. It is used for circuit board layout, traveling-salesperson problems, and scheduling.
Beam search
Instead of tracking one candidate, beam search keeps the best k candidates at the same time. At each step, every candidate is expanded into its neighbors, all candidates are scored, and the top k survive to the next round.
Think of it as running hill climbing on several starting points in parallel, but with a twist: the candidates can merge toward the same good region, so the beam often converges on the best hills. A wider beam explores more; a narrower beam is cheaper.
Beam search is popular in machine learning because it is easy to parallelize. Large language models often use beam search (or related sampling techniques) to generate high-probability text one token at a time.
Genetic algorithms
Genetic algorithms mimic natural selection. A population of candidate solutions evolves over many generations. The fittest candidates are more likely to produce offspring, and occasional mutations introduce new traits.
- Encode a solution as a string or list, like a genome. For a class schedule, the genome might be the ordered list of (exam, room, time) triples.
- Evaluate each candidate with a fitness score. Fewer conflicts means higher fitness.
- Select parents from the population, weighted by fitness.
- Crossover combines two parents, and mutation randomly tweaks the result.
- Repeat for many generations.
Genetic algorithms shine when solutions have structure that can be mixed and matched. They are less useful when small changes completely break a solution.
Heuristics and A* search
So far we have treated every neighbor as equally promising. A heuristic is an estimate that tells the algorithm which neighbors are most likely to lead to a good solution. A good heuristic is fast to compute and never overestimates the true cost.
A* search uses two numbers for each state:
- g(n): the actual cost from the start to the current state.
- h(n): the heuristic estimate from the current state to the goal.
A* picks the state with the lowest f(n) = g(n) + h(n). When the heuristic is admissible — it never overestimates — A* is guaranteed to find the shortest path. This is why A* dominates route planning in games and maps.
Why it matters for agents: an agent that plans a route or a sequence of tool calls can use A* to avoid exploring obviously bad branches. The heuristic is like a cheap preview of the future.
Anytime and memory-bounded search
Some search algorithms keep improving if you give them more time. An anytime algorithm returns the best answer found so far whenever you ask for it. This is useful for agents that have to decide when to stop thinking and start acting.
Memory-bounded search limits how many states the algorithm can remember. In very large spaces, even storing every visited state is impossible. Techniques like iterative deepening and recursive best-first search trade time for memory by throwing away states and recomputing them later.
The practical question is not "which algorithm is best?" but "what can we afford to compute, store, and wait for?"
Python versions
Simulated annealing
This function walks along a 1D list of values. It always accepts an uphill move, and it sometimes accepts a downhill move based on the current temperature. As the temperature cools, the search becomes pickier.
A* search
This function searches a grid where each open cell has a cost. It uses a priority queue ordered by f = g + h, where g is the cost so far and h is the Manhattan-distance heuristic.
Search playground
Click on the terrain to set a starting position, choose an algorithm, and press Run one step or Run search. Simulated annealing may take downhill steps while it is hot. Beam search keeps three candidates and tracks the best of them.
Check yourself
1. Why does simulated annealing sometimes accept a worse move?
Sample answer: Accepting worse moves lets the algorithm escape local maxima and explore other parts of the landscape, especially early when the temperature is high. As the temperature drops, it becomes less likely to accept bad moves.
2. A* uses f(n) = g(n) + h(n). Explain each piece in your own words.
Sample answer: g(n) is the cost already spent from the start to the current state. h(n) is an estimate of the remaining cost from the current state to the goal. f(n) is the estimated total cost of a path through that state.
3. Name a problem where a genetic algorithm would be a better fit than hill climbing.
Sample answer: A genetic algorithm fits well when solutions are structured like lists or genomes and can be combined. For example, finding a good exam schedule or a route for a delivery truck, where combining pieces of two good solutions can produce an even better one.
Quick quiz
1. What does simulated annealing borrow from metallurgy?
2. How does beam search differ from hill climbing?
3. For A* search, what property should a good heuristic have?
Keep learning
Now that you have seen stronger search tools, the next step is to learn the everyday Git commands that keep a project's history safe.
Part of the “Agentic Engineering & AI Software Topics” series.