A local search algorithm solves an optimisation problem by starting from one candidate solution and repeatedly moving to a better neighbouring solution until no neighbour improves the score. It keeps only the current state, so it needs very little memory, but it can get stuck in a local optimum instead of finding the global best.
In C++ you can write it generically: a state type, a neighbour function and an objective function, using <random> for reproducible randomness. The main variants are hill climbing, simulated annealing and tabu search.
A local search needs four things.
The loop generates a neighbour, applies the rule and stops after a fixed number of iterations or when nothing improves. It does not backtrack or keep a search tree.
| Algorithm | Move rule | Escapes local optima | Extra state |
|---|---|---|---|
| Hill climbing | Accept only better neighbours | No | None |
| Random-restart hill climbing | Repeat from random starts | Partly | Best result so far |
| Simulated annealing | Accept worse moves with probability exp(delta / T) | Yes | Temperature |
| Tabu search | Best neighbour not on a recent-move list | Yes | Tabu list |
The function below works for any state type. You supply the objective and the neighbour generator as callables, so the same code optimises a number, a vector or a permutation.
Build it with g++ -std=c++20 -O2 -Wall -Wextra hill.cpp. A typical run ends near the peak at x = 2, where f(x) = 5:
The values differ on every run because the generator is seeded from std::random_device. Pass a fixed seed to std::mt19937 when you need reproducible experiments or unit tests. The original approach of rand() % 3 - 1 only produces steps of -1, 0 and +1 times the step size, so it could never fine-tune the answer, and <random> is the better tool in modern C++.
Hill climbing stops on the first peak it reaches. Simulated annealing accepts a worse neighbour with probability exp(delta / T), where delta is the negative score change and T is the temperature, then cools T over time.
The program tracks the best state ever visited, because the current state may be worse than an earlier one. If you cool too fast it degrades into hill climbing, and if you cool too slowly it wastes iterations.
std::mt19937 and pass it around. Re-seeding in every call gives correlated numbers.No. It returns a local optimum. It reaches the global optimum only when the objective has a single peak or when the search happens to start in the right region.
Local search improves one current solution with small moves and keeps no record of the rest of the space. Global methods such as exhaustive search or branch and bound consider the whole space and can prove optimality, at far greater cost.
<random> separates the engine from the distribution, gives you better statistical quality, avoids modulo bias and lets you create independent, seedable generators per thread.
Yes. Random restarts are independent, so run each in its own thread with its own generator and keep the best result at the end.
A local search algorithm moves from one solution to a better neighbour until no improvement is possible. Hill climbing is the simplest form, and simulated annealing or tabu search help it escape local optima. In C++ keep the objective and neighbour logic as callables, use <random> with an explicit seed, track the best state seen and restart from several starting points.