Guide to AI and Data Courses · Lecture Notes
Chapter 10: Evolutionary and Metaheuristic Optimization
A courier company wants the shortest tour through 50 stops. The number of possible tours is 49!/2, about 3 × 1062; trying them all is impossible, and taking a derivative makes no sense. Yet by generating a few hundred random tours, then selecting and recombining the short ones, we can reach a good tour quickly. Evolutionary and metaheuristic optimization is the course about this "try, select, vary" loop. These notes introduce the course and work through its three core ideas with small examples you can compute by hand.
Learning outcomes
- The search problem
- Define a problem by its representation, fitness function and search space; explain why a derivative-based or closed-form solution is not always possible.
- Genetic algorithm
- Apply roulette selection, one-point crossover and mutation by hand to a small population for one generation.
- Local search
- Show why hill climbing gets stuck in a local optimum; compute the acceptance probability in simulated annealing.
- Multi-objective search
- Decide dominance between solutions; compute a Pareto front and crowding distances.
- Place of the course
- State what separates evolutionary methods from optimization, state-space search in AI, reinforcement learning and neural networks.
10.1What question does the course ask?
How do we find a good solution when there is no gradient or the search space is huge?
Evolutionary and metaheuristic optimization searches for a good solution to problems without derivatives or with huge search spaces through a loop of trying, selecting and varying. Inspired by processes such as natural selection, annealing of metals or ant colonies, these methods do not guarantee the optimum but find a good solution quickly.
In classical optimization the derivative or the convex structure of the objective guides us: we walk against the gradient. In many real problems this road is closed. The solution may be a tour, a schedule or a circuit layout; the objective value comes not from a closed formula but from running a simulation; the search space is huge and rugged. Then we have only one thing: a black box that gives a score when we hand it a solution.
Evolutionary and metaheuristic methods query this black box with a loop: evaluate one or several candidate solutions, keep the good ones, produce new candidates from them by small changes, repeat. The question of the course is which parts of this loop (representation, selection, variation operators, stopping criterion) work on which problem. The methods do not guarantee the optimum; in return they ask for no derivatives, convexity or smoothness. This trade-off is the main line that separates this course from Optimization.
10.1.1Exploration and exploitation
Every search method balances two urges. Exploration tries regions not yet visited; exploitation examines and improves a good region already found. A method that only explores turns into random search; a method that only exploits gets stuck on the first hill it finds. The three core ideas below each tune this balance with a different knob: mutation rate and selection pressure in the genetic algorithm, temperature in annealing, and the crowding distance that preserves diversity in multi-objective search.
10.1.2At a glance
| Criterion | Evolutionary and Metaheuristic Optimization |
|---|---|
| Approach | population-based heuristic search |
| Input | objective, solution encoding |
| Output | good, not proven optimal, solution |
| Success measure | quality, evaluation count |
| Math load | low-medium |
| Data needs | none; evaluations suffice |
10.2A short history
| Year | Milestone |
|---|---|
| 1975 | Holland publishes "Adaptation in Natural and Artificial Systems", laying the basis of genetic algorithms. |
| 1983 | Kirkpatrick, Gelatt and Vecchi introduce simulated annealing. |
| 1992 | Dorigo proposes ant colony optimization in his PhD thesis. |
| 1995 | Kennedy and Eberhart publish particle swarm optimization (PSO). |
| 1997 | Wolpert and Macready publish the "no free lunch" theorem: no method wins on all problems. |
| 2002 | Deb and colleagues publish NSGA-II for multi-objective search. |
10.3Core concepts
The rest of the course is built on the concepts below. The following sections work through the most important ones with examples.
| Concept | Meaning |
|---|---|
| Representation | How a solution is encoded: bit string, permutation, real vector. |
| Fitness | The objective value that says how good a solution is. |
| Selection | Giving good solutions more offspring: tournament, roulette. |
| Crossover | Combining parts of two parents into a new solution. |
| Mutation | Changing a solution by small random steps to add diversity. |
| Exploration vs. exploitation | Balance between searching new regions and refining a good one. |
| Local search | Hill climbing, simulated annealing, tabu search. |
| Pareto front | Solutions in a multi-objective problem that do not dominate each other. |
10.4Genetic algorithm: selection, crossover, mutation
A genetic algorithm (GA) improves a population of solutions together. Each solution is encoded by a representation; the simplest is a bit string. A fitness function gives each solution a score. Every generation applies three operations: selection, which gives high scorers more offspring; crossover, which combines parts of two parents; and mutation, which changes a solution by a small random step.
In a population with fitnesses fi, solution i is chosen as a parent with probability pi = fi / Σ fj. In one-point crossover a random cut point is chosen; the parts of the two parents before and after the cut are exchanged to make two children.
The OneMax problem: maximize the number of ones in an 8-bit string (fitness = number of ones). Take a population of four solutions (the starting values are illustrative):
| Solution | String | Fitness | Selection probability |
|---|---|---|---|
| S1 | 10110100 | 4 | 4/17 ≈ 0.235 |
| S2 | 01101101 | 5 | 5/17 ≈ 0.294 |
| S3 | 11100111 | 6 | 6/17 ≈ 0.353 |
| S4 | 00010010 | 2 | 2/17 ≈ 0.118 |
- Total and mean. Total fitness 4 + 5 + 6 + 2 = 17, mean 17/4 = 4.25.
- Selection. The wheel is spun twice and S2 and S3 come out as parents (a random outcome; S3 was the most likely, S4 the least likely candidate).
- Crossover. Cut point 3: S2 = 011|01101, S3 = 111|00111. The children are 011|00111 = 01100111 (fitness 5) and 111|01101 = 11101101 (fitness 6).
- Mutation. The 4th bit of the first child is flipped: 01100111 → 01110111, fitness 6.
The children have fitness 6 and 6; the parents had 5 and 6, and the population mean was 4.25. Crossover brought good parts of two parents together; mutation can restore a bit that is missing from the population. After a few generations the population drifts toward all ones.
In real problems the representation is the heart of the matter. In the traveling salesman problem a solution is a permutation; one-point crossover would repeat stops, so special order-preserving operators are needed. In continuous problems a solution is a real vector and mutation adds Gaussian noise. The same skeleton works in all of them, with the representation and operators changed.
Choosing a large mutation rate on the theory that more exploration is better. For a string of length L a common starting rate is 1/L: on average one bit changes per string. At a rate of 0.5 every bit is flipped like a coin toss, solutions lose their relation to their parents and the GA degenerates into random search. A rate that is too small leaves the population without diversity and leads to premature convergence.
10.5Local search and simulated annealing
Local search works with a single solution: it looks at the solution's "neighbors" and moves if one is better. Hill climbing moves only to an improving neighbor. It is simple and fast, but when it reaches a local optimum, a point all of whose neighbors are worse, it stops, although a better solution may lie elsewhere.
We want to minimize the cost. The solution x is an integer from 0 to 7 and its neighbors are x − 1 and x + 1 (the values are illustrative):
| x | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| cost | 5 | 3 | 4 | 5 | 2 | 0 | 1 | 4 |
Hill climbing started at x = 1 looks at the neighbors: costs 5 and 4, both worse than 3. It stops and reports the local optimum with cost 3, although the best solution is x = 5 (cost 0). To get there it must first take steps that increase the cost.
Simulated annealing permits such steps with a probability. A worsening neighbor, with cost increase Δ and temperature T, is accepted by the following rule:
A better neighbor is always accepted. A worse one is accepted with probability p = e−Δ/T: a number u is drawn at random from [0, 1) and the move is made if u < p. When the temperature is high almost every step is accepted (exploration); as it falls only improvements remain (exploitation). The temperature is lowered by a cooling schedule; the most common is geometric cooling: T ← α · T, 0 < α < 1.
In the table above we start at x = 1. To get out we first need x = 2 (Δ = 4 − 3 = 1), then x = 3 (Δ = 5 − 4 = 1); x = 4 lowers the cost to 2, and from there only downhill steps lead to x = 5.
- T = 2. For each uphill step p = e−1/2 ≈ 0.607; both steps together 0.6072 = e−1 ≈ 0.368.
- T = 0.2. Per step e−5 ≈ 0.0067; two steps e−10 ≈ 4.5 × 10−5. Annealing this cold is hardly different from hill climbing.
- One decision. At T = 2 with Δ = 1 the draw is u = 0.5: 0.5 < 0.607, so the worse neighbor is accepted. With u = 0.7 it would be rejected.
- Cooling. With T0 = 10 and α = 0.9, after 10 steps T = 10 × 0.910 ≈ 3.49.
Early on, with the temperature high, uphill moves are crossed often; as the temperature falls the search locks into the best valley found. For the same Δ the acceptance probability drops from 0.607 to 0.0067 as the temperature falls.
Cooling too fast, or starting too cold. The search turns into hill climbing before it has crossed even the first hill and stays in the first valley it found. The schedule is tuned to the typical Δ values of the problem: at the start most worsening moves (for example 80 percent) should be accepted, and at the end very few.
10.6Multi-objective search and the Pareto front
Real problems often have more than one objective: cost and time, return and risk, accuracy and model size. When objectives conflict, there is no single "best" solution. Solutions are compared by dominance.
Assume all objectives are minimized. Solution A dominates B if A is no worse than B in every objective and better in at least one. Solutions that no other solution dominates are called non-dominated; their set in objective space is the Pareto front.
Six route candidates have (cost, time) values; we want to minimize both (the values are illustrative): A (2, 9), B (4, 6), C (5, 7), D (6, 3), E (8, 4), F (9, 1).
- Dominance. B dominates C (4 ≤ 5 and 6 ≤ 7). D dominates E (6 ≤ 8 and 3 ≤ 4). There is no other dominance.
- Front. The non-dominated solutions are A, B, D and F. C and E form the second front.
- Weighted sum. With equal weights (0.5 · cost + 0.5 · time) the values are A 5.5; B 5; C 6; D 4.5; E 6; F 5. A single-objective search returns only D; A, B and F, which are also good trade-offs, stay invisible.
- Crowding distance (NSGA-II). The solutions on the front are sorted by the first objective: A, B, D, F. The ends (A and F) get an infinite value. For each inner solution the difference of its two neighbors is divided by the objective range and summed (ranges: cost 9 − 2 = 7, time 9 − 1 = 8). For B, (6 − 2)/7 + (9 − 3)/8 ≈ 0.571 + 0.750 = 1.321; for D, (9 − 4)/7 + (6 − 1)/8 ≈ 0.714 + 0.625 = 1.339.
NSGA-II first looks at which front a solution is on and, within the same front, prefers the larger crowding distance: this moves toward the front and keeps diversity along it. Here D is preferred because it is slightly more "alone" than B.
Multi-objective search returns not a single solution but a set; the decision maker picks the trade-off from this set according to their own priorities. In portfolio selection it is the return-risk curve; in hyperparameter search it is the accuracy-latency curve.
Collapsing the objectives into one number with weights and calling the result "the optimum". The choice of weights decides the answer in advance, and a weighted sum cannot find solutions in the non-convex parts of the front with any weights. Extracting the front first and choosing afterwards makes the preferences explicit.
10.7How is the course taught?
10.7.1A typical workflow
A courier company wants the shortest tour through 50 stops. 100 random tours are generated, each tour's length is measured, short tours are selected as parents, their orders are combined and a few stops are swapped. After hundreds of generations a tour close to the best known solution is found.
10.7.2Topics over the semester
- Optimization problems, search spaces, representations
- Local search: hill climbing, tabu search
- Simulated annealing and cooling schedules
- Genetic algorithms: selection, crossover, mutation
- Evolution strategies and CMA-ES
- Differential evolution
- Genetic programming
- Ant colony optimization
- Particle swarm optimization
- Constraint handling: penalties and repair
- Multi-objective search: Pareto, NSGA-II
- Experimental design and statistical comparison
This is a typical semester; the order and coverage vary between universities.
10.7.3Skills you will gain
- building evolutionary algorithms with DEAP
- multi-objective search with pymoo
- hyperparameter search with Optuna
- choosing a representation for a problem
- fair comparisons over many runs
10.7.4Try it yourself
The Try It: Evolve the Delivery Van's Route slide in the presentation lets you solve a typical problem of this course yourself: The delivery van leaves the depot (D), visits 11 addresses and returns. A genetic algorithm improves a population of 40 tours by crossover and mutation. Advance generations, change the mutation rate and compare.
10.8Are you ready for this course?
10.8.1Prerequisites
| Course | Why it is needed |
|---|---|
| Artificial Intelligence | heuristic search |
| Algorithms | complexity, NP-hard problems and greedy methods |
| Programming | arrays, random number generation and loops |
10.8.2Ask yourself
For every "no", the right-hand column shows where to start. If you can say "yes" to most of them, you are ready.
| Question | If not, start with |
|---|---|
| Can you state the time complexity of an algorithm in big-O notation? | Complexity and NP-hardness from an algorithms course |
| Can you find the minimum of a function by taking its derivative? | Optimization |
| Can you explain mean, standard deviation and a hypothesis test? | Probability and Statistics |
| Can you write a small Python program with arrays, loops and random numbers? | An intro programming course, then a short NumPy tutorial |
10.9Neighboring courses
10.9.1Where it overlaps
| Relation | Course | Shared ground |
|---|---|---|
| subfield | This course is a subfield of Artificial Intelligence | heuristic search |
| shares methods | Shares methods with Artificial Neural Networks | neuroevolution, hyperparameter search |
| shares methods | Shares methods with Optimization | derivative-free and global optimization |
10.9.2Where it differs
- Optimization: Optimization reaches a provable optimum using derivatives and convexity; this course teaches derivative-free search with no guarantee but wide applicability.
- Artificial Intelligence: Searches such as A* in AI find a path to a goal in a state space; this course searches directly for a good complete solution.
- Reinforcement Learning: RL learns a policy from states to actions by interacting with an environment; this course seeks a good solution for a single objective function.
- Artificial Neural Networks: Neural networks train weights with gradients; this course searches without gradients, for example evolving network structure or hyperparameters.
10.10In practice
10.10.1Applications
| Area | What is done |
|---|---|
| Vehicle routing | Shortening courier and delivery tours. |
| Scheduling | Timetables, production and shift plans. |
| Engineering design | Designing antenna, wing and structural shapes. |
| Hyperparameter search | Finding settings for learning models. |
| Layout | Circuit component and facility placement. |
| Portfolio selection | Multi-objective trade-off between return and risk. |
10.10.2Risks and ethics
| Risk | Explanation |
|---|---|
| Loopholes in the fitness function | Search finds and exploits flaws in the objective: the score rises but the intended behavior never appears; evolution runs often show this. |
| Unfair comparisons | A single run, unequal evaluation budgets or tuning only your own method make a new algorithm look better than it is. |
| Designs that stay in simulation | An antenna or structure that looks great in simulation can fail in reality because of effects the model leaves out. |
| Compute and energy cost | Running expensive simulations over thousands of generations can burn days of processor time and a lot of energy. |
These issues are discussed in depth in Explainable AI and AI Ethics.
10.10.3Tools and datasets
Tools: DEAP, pymoo, jMetal, PyGAD, ECJ, MATLAB Global Optimization Toolbox, Optuna.
Classic benchmark problems: TSPLIB (traveling salesman), Knapsack problem, OneMax, Rastrigin and Ackley functions, ZDT and DTLZ multi-objective tests, CEC benchmark functions.
This course uses benchmark problems instead of datasets: methods are compared on the same problems with the same evaluation budget.
10.11Common misconceptions
A genetic algorithm finds the optimal solution.
Metaheuristics do not guarantee the optimum; they aim for a good solution in reasonable time.
Nature-inspired methods are more powerful.
The inspiration does not determine performance; many newly named methods are minor variants of older ones.
One method is best for every problem.
By the "no free lunch" theorem, no method is better on average over all problems.
Use evolutionary methods even when derivatives exist.
When derivatives are available, gradient methods usually reach the result with far fewer evaluations.
10.12Summary
- Evolutionary and metaheuristic methods search with a derivative-free, guarantee-free but widely applicable "try, select, vary" loop; a problem is defined by a representation and a fitness function.
- In a genetic algorithm selection gives good solutions more offspring, crossover combines parents and mutation adds diversity; the representation is chosen to fit the problem.
- Hill climbing gets stuck in local optima; simulated annealing gets past them by accepting a worsening step with probability e−Δ/T, and the temperature tunes the exploration-exploitation balance.
- With several objectives there is no single best; dominance gives the Pareto front, and NSGA-II approaches the front while keeping diversity through the crowding distance.
- Comparisons use the same evaluation budget and many runs; when derivatives exist, gradient methods usually reach the result with fewer evaluations.
10.13Test Yourself
Three solutions have fitness 3, 1 and 6. What is each one's selection probability under roulette selection? If 100 parents are chosen, how many copies of the best solution are expected?
Answer
The total is 3 + 1 + 6 = 10. The probabilities are 0.3, 0.1 and 0.6. In 100 selections the expected number of copies of the best solution is 100 × 0.6 = 60.
The parents are 110011 and 001100, with the cut point at 2 (after the second bit). Write the children of one-point crossover and give their fitness if fitness is the number of ones.
Answer
Child 1 = first 2 bits of the first parent + the rest of the second = 11|1100 = 111100 (fitness 4). Child 2 = 00|0011 = 000011 (fitness 2). The parents had fitness 4 and 2; the total did not change, the ones were redistributed between the parents.
In simulated annealing, what is the probability of moving to a worse neighbor with cost increase Δ = 3 at T = 1.5 and at T = 0.5? If u = 0.2 is drawn, what happens in each case?
Answer
T = 1.5: e−3/1.5 = e−2 ≈ 0.135. T = 0.5: e−6 ≈ 0.0025. u = 0.2 is larger than both probabilities (0.2 > 0.135 and 0.2 > 0.0025), so the neighbor is rejected in both cases.
Minimizing cost and time, the candidates are (3, 5), (4, 4), (5, 6), (2, 8). Which are on the Pareto front?
Answer
(4, 4) dominates (5, 6) (4 < 5 and 4 < 6). There is no dominance among the other pairs: (3, 5) and (4, 4) are each better in one objective and worse in the other; (2, 8) is best in cost. The front is (3, 5), (4, 4) and (2, 8).
In particle swarm optimization (PSO) a particle has position x = 2, velocity v = 1, personal best p = 3 and swarm best g = 5. Inertia w = 0.5, c1 = c2 = 1, random numbers r1 = 0.4 and r2 = 0.5. What are the new velocity and position? The update is v ← w·v + c1r1(p − x) + c2r2(g − x), x ← x + v.
Answer
v = 0.5·1 + 1·0.4·(3 − 2) + 1·0.5·(5 − 2) = 0.5 + 0.4 + 1.5 = 2.4. x = 2 + 2.4 = 4.4. The particle is pulled both toward the good place it remembers and toward the best of the swarm.
A simulation evaluates one solution in 0.2 seconds. With a population of 40, 250 generations and the evaluation of the initial population ignored, how many evaluations does a run need and how long does it take? What does parallel evaluation on 8 cores do to the time? What should you watch when comparing a new method against it?
Answer
The number of evaluations is 40 × 250 = 10,000; the time is 10,000 × 0.2 = 2000 s ≈ 33.3 minutes. On 8 cores, if the work divides evenly, about 2000/8 = 250 s ≈ 4.2 minutes. In a comparison every method should get the same evaluation budget (10,000), and because of randomness each method should be reported over many independent runs, with mean and spread.
10.14After this course
This course goes beyond where classical optimization reaches. Next you can take Optimization for provable methods, Artificial Neural Networks to evolve network structures, or Multi-Agent Systems for an agent view of swarm behavior.
Related careers: Operations researcher, Logistics and planning engineer, Design optimization engineer, Machine learning engineer.
10.15Further reading
- A. E. Eiben and J. E. Smith, Introduction to Evolutionary Computing (2nd ed., 2015): teaches genetic algorithms, evolution strategies and multi-objective search in one orderly framework.
- E.-G. Talbi, Metaheuristics: From Design to Implementation (2009): compares local search, annealing and population methods from the design and implementation side.
- S. Luke, Essentials of Metaheuristics (2nd ed., 2013): a short book that gives the algorithms in pseudocode.
10.16References
- Goldberg, Genetic Algorithms in Search, Optimization, and Machine Learning, Addison-Wesley, 1989.
- Eiben and Smith, Introduction to Evolutionary Computing, 2nd ed., Springer, 2015.
- Talbi, Metaheuristics: From Design to Implementation, Wiley, 2009.
- Luke, Essentials of Metaheuristics, 2nd ed., Lulu, 2013.
Related pages: Slides · Slides (PDF) · Course page