Genetic Algorithms › Example
Machine Learning from ScratchAll chapters Genetic Algorithms › ExampleSection view · Sections in this chapter5 / 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 formulas are only convincing if they reproduce on a single generation. We therefore work through one generation by hand on the shared instance, with a population of four so the arithmetic stays visible. The costs below are computed from the distance matrix; the implementation re-derives them from code, which lets us check that the hand calculation and the program agree.
Step 1: decode and score four individuals
Take the population
| Individual | Permutation | Vehicle 1 | Vehicle 2 | Cost |
|---|---|---|---|---|
| (1, 7, 8, 3, 6, 5, 4, 2) | 0→1→7→8→3→0 | 0→6→5→4→2→0 | 40.8563 | |
| (1, 2, 3, 8, 6, 5, 4, 7) | 0→1→2→3→8→0 | 0→6→5→4→7→0 | 38.2665 | |
| (1, 2, 3, 7, 4, 5, 6, 8) | 0→1→2→3→7→0 | 0→4→5→6→8→0 | 39.1233 | |
| (2, 6, 1, 5, 3, 7, 4, 8) | 0→2→6→1→5→0 | 0→3→7→4→8→0 | 60.0227 |
Distances and totals below are rounded independently from full-precision values, so summing the displayed arc lengths can differ in the final decimal place. The first individual is the nearest-neighbour construction, which is where every method in this chapter starts. Its cost is the sum of the arcs , , , , for the first vehicle, giving , and , , , , for the second, giving . In total . The other rows are decoded the same way: split the permutation after position four, close each half with the depot, and sum the five arcs in each route.
Step 2: select parents by tournament
For this illustrative update, two size- tournaments select the intended parents. The first pits against and returns , since . The second pits against and returns , since . Thus and .
Each tournament returns its cheaper contender. The two sampled pairs are supplied for the hand calculation, rather than a claim about which individuals a random run will select. The example uses four scored parents and one illustrated offspring before survival.
Step 3: order crossover with cuts ,
The block is copied first. Note that positions , , of hold the values , so .
| Position | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 |
|---|---|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 8 | 6 | 5 | 4 | 7 | |
| 1 | 2 | 3 | 7 | 4 | 5 | 6 | 8 | |
| child after copy | – | – | – | – | 6 | 5 | 4 | – |
Next, list cyclically starting at position : the values are , then , , , , and then , , which are all in and are skipped. The filtered list is therefore . The empty positions in cyclic order from are , so we assign
giving the child .
The important point is that the child is a permutation without any repair. The copied block supplies ; the filtered list supplies the remaining values in the order had them, after removing the copied ones. Nothing is duplicated and nothing is lost.
Step 4: score the child
The child decodes to the tours and . Vehicle 1 costs , and vehicle 2 costs . The total is , which is shorter than either parent ( and ).
That is recombination doing real work. The child keeps the left block in 's order and appends the value that had right after it, and it keeps 's descending block and closes it with from . Neither parent contained both blocks in that arrangement. The example is small, but it shows the mechanism the population is supposed to exploit: crossover can assemble a combination that neither parent had.
Step 5: mutation, and elitist replacement
The loop applies swap mutation to a child with probability (the per-child mutation rate, here 0.5) before scoring it, so only one of the two versions, the mutated one or the unmutated one, is charged to the budget. For illustration, swapping positions and of the child gives with cost , which is worse than the unmutated child; mutation is a perturbation, not an improvement operator. This one swap is worse; it does not establish how often all swaps lose.
Survivor selection is with . The pool is the four parents plus the child, and the four cheapest are kept: the child at , then at , at , and at . at is discarded. The population mean falls from
Whether this child is optimal is a question for enumeration, which we run in the implementation; on this instance enumeration confirms that the child is optimal, with minimum cost approximately (rounded to four decimal places). The enumeration is an independent computation excluded from every search budget; the optimization figures and the summary table report that measured result. Scoring the four parents and both displayed child variants costs six illustrative evaluations, separate from every search budget. In the actual loop only the final, possibly mutated version of each child is scored. The hand example has shown the mechanics; the next section turns those mechanics into code and holds the result to the same budget as the other methods.
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!