Genetic Algorithms › Visualizing Genetic Algorithms

Michael BrenndoerferOctober 29, 20268 min read
Machine Learning from ScratchAll chapters
Part 15 of 15Heuristic and Evolutionary Optimization
Chapter 91Genetic Algorithms › Visualizing Genetic Algorithms
Reading section 7 of 10 · Visualizing Genetic Algorithms · 8 minSection 7 of 10 · 8 min

Section 7 of 10 · 8 min read

Genetic Algorithms › Visualizing Genetic AlgorithmsSection view · Sections in this chapter7 / 10 ↓
Reading tools Definitions, links & notes

Choose your expertise level to adjust how many terms are explained. Beginners see more tooltips, experts see fewer to maintain reading flow. Hover over underlined terms for instant definitions.

Article links

Make inline references clickable

Notes & comments

The figures below use runs that were already computed. The first pair shows what happens inside one population; the second pair compares methods at the same evaluation budget; the last shows the tours themselves.

What one population does across its generations

We reuse the genetic algorithm history with seed 0 and record, for each generation, the best, median, and worst cost in the population plus two diversity diagnostics. This is a diagnostic run on the same budget, not an extra budget for the comparison.

PythonIn [26]
CollapseExpand
demo_history = ga_histories[0]
demo_frame = pd.DataFrame(demo_history)
demo_frame.insert(0, "generation", np.arange(1, len(demo_frame) + 1))
ConsoleOut [27]
CollapseExpand
    generation    best  median   worst  distinct  mean_hamming
0            1  40.508  47.226  49.915        17         6.647
1            2  40.508  44.398  46.649        16         6.474
2            3  38.731  41.239  44.398        13         6.221
3            4  38.393  40.912  43.411        10         5.747
4            5  38.267  40.449  40.856         8         4.247
5            6  38.267  38.393  40.508         6         2.868
6            7  38.267  38.393  38.393         2         1.074
7            8  38.197  38.393  38.393         3         2.079
8            9  38.000  38.267  38.393         4         2.358
9           10  37.356  38.267  38.393         5         2.168
10          11  37.356  38.267  38.267         4         2.542
11          12  37.356  38.197  38.267         4         3.016
12          13  37.356  38.000  38.267         4         3.332
13          14  37.356  37.356  38.000         2         1.768
VisualizationOut [28]
CollapseExpand
Line chart with three series across fourteen generations: population best, median, and worst tour length. The best line is flat or decreasing, the worst line starts far higher and steps down.
Population best, median, and worst tour length by generation for one seed-0 run. For this fixed deterministic objective, elitist replacement keeps the retained best, median, and worst costs non-increasing. The gap between the three lines is the spread that selection is acting on.

For this fixed deterministic objective, elitist survival makes the retained best non-increasing. The best-to-worst range describes cost spread; it is not the selection differential, which would compare selected parents with the original population. Similar costs do not imply identical permutations or a single canonical tour pair. Read the cost curves together with both diversity measures.

Diversity and the mutation rate

The following diagnostic compares the same seed at mutation rates 0.1 and 0.5. The 0.5 history is reused from the budgeted comparison; the 0.1 history is one additional 300-evaluation run, reported separately from the four-method benchmark.

PythonIn [29]
CollapseExpand
diversity_rows = []
for rate in (0.1, 0.5):
    if rate == 0.5:
        history = ga_histories[0]
    else:
        rng = np.random.default_rng(0)
        _, _, _, history = genetic_algorithm(
            rng, BUDGET, NN_PERM, mutation_rate=rate
        )
    for step, row in enumerate(history, start=1):
        diversity_rows.append(
            {"generation": step, "mutation_rate": rate, **row}
        )
diversity_frame = pd.DataFrame(diversity_rows)
VisualizationOut [30]
CollapseExpand
Line chart of the number of distinct canonical solutions in the population across fourteen generations, drawn for mutation rate 0.1 and 0.5.
Distinct canonical solutions per generation for one seed at mutation rates 0.1 and 0.5. In this recorded seed the 0.1 run reaches one signature while the 0.5 run retains several for longer; this is a measured trajectory, not a general rate law. The signature treats vehicles as interchangeable and tours as reversible under this instance's symmetric distances and wide windows; genotype diversity is a separate diagnostic.

For this seed, the 0.1 run reaches one canonical signature at generation 8 and stays there; the 0.5 run finishes with two. The counts can increase as new offspring survive. A single signature also permits several different permutations, and crossing such permutations can produce a different phenotype. Identical parents always produce a copy; a matching canonical signature alone supplies no such guarantee. These two runs do not establish a general mutation-rate ranking.

Incumbent traces at equal budget

The following traces compare complete-route evaluation effort. Runtime is a separate useful comparison; neither axis alone controls all computational costs. All four methods start from the same nearest-neighbour value, so all four curves begin at the same height.

PythonIn [31]
CollapseExpand
trace_length = min(min(len(t) for t in traces[name]) for name in RUNNERS)
trace_matrix = {
    name: np.vstack([t[:trace_length] for t in traces[name]])
    for name in RUNNERS
}
VisualizationOut [32]
CollapseExpand
Line chart of mean best-so-far tour length versus evaluation index for four search methods, each with a shaded percentile band, all starting at the same value and declining at different rates.
Best-so-far objective against the number of complete-route evaluations for four search methods, averaged over 20 seeds. Lines are means and bands span the 10th to 90th percentile. All methods start from the shared nearest-neighbour score at evaluation 1, so differences are due to how each spends its remaining 299 evaluations.

The traces record the incumbent after every scored candidate, including rejected moves. A hill-climbing sweep scores 28 neighbours before selecting its next current state, but its incumbent can improve during that sweep. SA's current state may worsen while its best-so-far trace never rises. The GA also records improvements during initialization, before its first generation. These curves are means across 20 runs; the bands are seed quantiles, not confidence intervals for a mean.

Final costs across seeds

VisualizationOut [33]
CollapseExpand
Strip plot of final tour length per seed for four methods, with points spread vertically in a narrow range and a dashed horizontal line marking the exact optimum.
Final best objective for each method across the 20 seeds, with a small deterministic horizontal offset so overlapping points stay visible. The vertical spread shows seed-to-seed outcomes; horizontal offsets are deterministic display positions, and the dashed line is the exact optimum from enumerating all 40,320 permutations. A single seed would be a poor summary of any of these methods.

The vertical spread displays variation over these 20 seeds. Overlap does not establish equivalence or statistical indistinguishability of the methods. The mean marker is descriptive; s/Rs/\sqrt{R} estimates the standard error of a sample mean under independent, identically distributed runs with finite variance, but no confidence interval or hypothesis test is shown here.

The tours themselves

VisualizationOut [34]
CollapseExpand
Scatter plot of eight customer coordinates plus a depot at the origin, with two closed loops departing from and returning to the depot.
The two depot tours of the exact optimum found by enumeration, with squares marking the depot, circles the customers, and two colours distinguishing the vehicles. The two colored sequences show the enumerated reference routes; every customer is visited exactly once and both tours return to the depot.

The route picture makes the decoded reference visible. It does not prove optimality by its appearance; the exhaustive computation supplies the reference. OX copies positions, so a block crossing the vehicle split can change the assignment of several customers. Whether such a child is better requires scoring it.

Comments

Add to the conversation

A question, a correction, or an example from your own work.

No comments yet. Be the first to share your thoughts!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy