Genetic Algorithms › Visualizing Genetic Algorithms
Machine Learning from ScratchAll chapters 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
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]Expand
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]Expand
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]Expand

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]Expand
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]Expand

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]Expand
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]Expand

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]Expand

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; 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]Expand

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!