Hill climbing

Also known as: hill-climbing algorithm · greedy local search

On this page 8

Hill climbing is an iterative local search method that starts from one candidate solution and repeatedly moves to a better neighboring solution until no neighbor improves the objective function. In mathematical optimization and artificial intelligence, it is the simplest member of the greedy local search family and the baseline against which metaheuristics such as simulated annealing and tabu search are measured.

The method needs only two ingredients: a way to score a candidate and a way to generate neighbors. It requires no gradients, no convexity and no model of the search space, which makes it a natural choice for combinatorial and derivative-free problems. Its central weakness is equally simple: it stops at the first local optimum it reaches, which may be far from the global optimum.

How hill climbing moves through the search space

Hill climbing works by evaluating neighbors of the current state and accepting a move only when it improves the score. The algorithm treats the objective as a landscape: maximization means climbing toward peaks, while minimization is the same procedure applied to valleys. Each accepted move becomes the new current state, and the loop ends when the neighborhood contains no better candidate or a step budget runs out.

  1. Choose an initial state, often at random or from a quick heuristic.
  2. Generate the neighborhood with a defined move operator.
  3. Evaluate the objective for one, several or all neighbors.
  4. Move to an improving neighbor, or stop if none exists.
  5. Return the best state found.

The neighborhood definition shapes everything. For a traveling salesman tour, a neighbor might differ by a 2-opt edge swap; for a bit string, by one flipped bit; for a continuous vector, by a small random perturbation of one coordinate. A larger neighborhood finds better local optima but costs more evaluations per step, so the move operator is the main design decision in any hill climbing implementation.

Variants of hill climbing

Several variants change how the next move is selected or how the search restarts. The table below compares the forms most often used in practice and the situation each one suits.

VariantMove selectionBest suited to
Simple hill climbingFirst improving neighbor foundLarge neighborhoods, cheap scoring
Steepest-ascent hill climbingBest neighbor after full scanSmall neighborhoods, precise steps
Stochastic hill climbingRandom improving neighborReducing bias from neighbor order
Random-restart hill climbingRepeated runs from new startsMany local optima of varied quality
First-choice hill climbingRandom neighbors until one improvesVery large or implicit neighborhoods
Common hill climbing variants

Random-restart hill climbing is the most important practical extension. If each run reaches the global optimum with probability p, the expected number of runs is about 1/p, so even a modest success rate per run becomes reliable after enough restarts. Many production solvers combine restarts with a stochastic move rule to keep each run inexpensive.

Local optima, plateaus and ridges

Hill climbing fails in predictable ways because it never accepts a worse move. Three landscape features cause most of these failures, and recognizing them explains why more elaborate metaheuristics exist.

FeatureWhat happensTypical remedy
Local optimumAll neighbors score worseRandom restarts or annealing
PlateauNeighbors score equallyAllow sideways moves with a limit
RidgeImprovement requires a diagonal stepCombine moves or rotate coordinates
Saddle regionProgress slows in flat directionsLarger perturbations
Landscape features that stop the search

A plateau is especially common in combinatorial problems with integer-valued objectives, where many neighbors tie. Allowing a bounded number of sideways moves, often a few hundred, lets the search cross a flat region without cycling forever. On a ridge, every single-coordinate move looks worse even though a combined move would improve the score, so the move operator itself must be redesigned.

Hill climbing sits at the greedy end of a spectrum of local search methods. Related techniques differ mainly in whether and how they accept worse moves, and in how much memory they keep.

MethodAccepts worse movesMemoryTypical use
Hill climbingNeverCurrent state onlyFast baseline, local refinement
Simulated annealingWith decreasing probabilityCurrent state, temperatureRugged landscapes
Tabu searchYes, best non-tabu moveRecent move listScheduling, routing
Gradient descentNoCurrent point, gradientDifferentiable continuous objectives
Genetic algorithmThrough selection pressurePopulationBroad global exploration
Hill climbing and neighboring optimization methods

Hill climbing is often described as the discrete counterpart of gradient descent. Both follow the locally best direction, but gradient descent computes it analytically, while hill climbing discovers it by sampling and comparing neighbors. In continuous spaces with available derivatives, gradient-based methods are usually far more efficient.

Where hill climbing is used in AI practice

Hill climbing remains useful wherever a good solution quickly matters more than a proven optimum. Typical applications include feature subset selection by adding or removing one feature at a time, structure learning for Bayesian networks by adding, deleting or reversing edges, and local refinement of schedules and routes produced by a constructive heuristic.

  • Polishing the output of a genetic algorithm or other global method, an approach called memetic search.
  • Tuning a small number of discrete hyperparameters when evaluations are cheap.
  • Solving constraint satisfaction problems with min-conflicts moves, such as large n-queens instances.

When evaluations are expensive, as in training a deep network for each hyperparameter setting, hill climbing becomes wasteful because it spends many evaluations on small steps. Bayesian optimization or random search is then usually the better use of the budget.

Common mistakes and practical guidance

Most poor results come from design choices rather than the algorithm itself. The table lists frequent symptoms and how experienced practitioners address them.

SymptomLikely causeFix
Stops after a few stepsNeighborhood too smallEnrich the move operator
Same result every runDeterministic start and orderRandomize start and neighbor order
Very slow progressFull objective recomputed per moveUse incremental delta evaluation
Endless cyclingUnlimited sideways movesCap sideways moves
Symptoms, causes and fixes

Incremental evaluation is the most effective speedup: for a 2-opt move on a tour, the change in length depends on only four edges, so each neighbor costs constant time instead of linear time. Reporting the best score across restarts, together with the number of evaluations, gives a fair comparison with other methods.

The idea in brief

Imagine standing on a hillside in thick fog. You cannot see the summit. You can only feel the ground around your feet. So you take a step in whichever direction goes up. You repeat this until every direction goes down. That spot is a peak, but it may not be the highest one.

Hill climbing does the same with possible answers to a problem. It tries small changes and keeps the better ones. It is fast and simple. Its weakness is that it can get stuck on a small hill. Starting again from many different places is the usual cure.

A practical scenario

A regional delivery company plans daily routes for one van serving 60 stops. A dispatcher first builds a quick route with a nearest-neighbor rule, which produces a tour of about 210 kilometers. The planning software then applies hill climbing with 2-opt moves: it reverses a segment of the route whenever doing so shortens the total distance.

Each run evaluates thousands of swaps in under a second because only the changed edges are recomputed. The software repeats the process from 50 randomized starting routes and keeps the best result. The final tour measures about 182 kilometers, roughly 13 percent shorter than the initial route, which saves fuel and nearly half an hour of driving every day without requiring an exact solver.