Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions genetic_algorithm/travelling_salesman_problem.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,34 @@ def main(
Traceback (most recent call last):
...
IndexError: list assignment index out of range

A seeded generation on four square cities makes the intermediate steps
reproducible:

>>> import random
>>> small_cities = {0: [0, 0], 1: [1, 0], 2: [1, 1], 3: [0, 1]}
>>> random.seed(23)
>>> population, cities_list = init(small_cities, 4)
>>> fitness_values, best_path, best_distance = fitness(
... small_cities, population, [], float("inf")
... )
>>> [round(value, 4) for value in fitness_values]
[0.2071, 0.25, 0.2071, 0.2071]
>>> best_path, best_distance
([0, 3, 2, 1, 0], 4.0)
>>> random.seed(23)
>>> population = chose_rws(fitness_values, population, 4)
>>> population
[[0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 2, 1, 3, 0]]
>>> for index in range(2):
... population[index], population[index + 2] = crossing(
... population[index], population[index + 2], 1.0, cities_list
... )
>>> population
[[0, 3, 1, 2, 0], [0, 2, 1, 3, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0]]
>>> population = [mutate(chromosome, 1.0) for chromosome in population]
>>> population
[[0, 3, 2, 1, 0], [0, 2, 1, 3, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0]]
"""
best_path: list[int] = []
best_distance = float("inf")
Expand Down
37 changes: 37 additions & 0 deletions work/R23-T049/TRACE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
# Seeded TSP generation trace

I traced `init`, `fitness`, both selection helpers, `crossing`, and `mutate`
with `python -m pdb` using four cities on a unit square, population size 4,
and seed 23. The trace helper was temporary; the seeded doctest in
`genetic_algorithm/travelling_salesman_problem.py` now reproduces the roulette
selection generation steps.

| Step | Seed / operation | Values observed |
| --- | --- | --- |
| Initial setup | `random.seed(23)`; cities `{0: [0, 0], 1: [1, 0], 2: [1, 1], 3: [0, 1]}` | `cities_list = [1, 2, 3]`; population size = 4 |
| Initial population | `init(cities, 4)` | `[[0, 2, 1, 3, 0], [0, 3, 2, 1, 0], [0, 2, 3, 1, 0], [0, 3, 1, 2, 0]]` |
| Fitness | `fitness(cities, population, [], inf)` | Distances `[4.828427, 4.0, 4.828427, 4.828427]`; fitness `[0.207107, 0.25, 0.207107, 0.207107]`; best so far `([0, 3, 2, 1, 0], 4.0)` |
| Tournament selection | Reset `random.seed(23)`; `chose_ts(...)` | `[[0, 2, 3, 1, 0], [0, 2, 1, 3, 0], [0, 3, 1, 2, 0], [0, 3, 2, 1, 0]]` |
| Roulette selection | Reset `random.seed(23)`; `chose_rws(...)` | `[[0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0], [0, 2, 1, 3, 0]]` |
| Crossover, pair 0 and 2 | `crossover_probability=1.0` | Both parents were `[0, 3, 1, 2, 0]`; offspring remained `[0, 3, 1, 2, 0]` and `[0, 3, 1, 2, 0]`. |
| Crossover, pair 1 and 3 | `crossover_probability=1.0` | Parents `[0, 3, 1, 2, 0]` and `[0, 2, 1, 3, 0]`; offspring `[0, 2, 1, 3, 0]` and `[0, 3, 1, 2, 0]`. |
| Mutation | `mutation_probability=1.0` for each offspring | New population `[[0, 3, 2, 1, 0], [0, 2, 1, 3, 0], [0, 3, 1, 2, 0], [0, 3, 1, 2, 0]]` |

The two selection operators were compared separately from the roulette-based
generation used for crossover and mutation: each selection call started with
the same seed so its output is directly reproducible.

## What surprised me

Roulette selection selected the same lower-fitness tour three times and did not
select the current best tour in this draw. The best-so-far result remained
available separately as `best_path` / `best_distance`, but the population itself
does not guarantee that its best individual survives selection. The seeded
doctest records the exact selected population and its later crossover and
mutation results.

## Validation

- Baseline: `uv run pytest genetic_algorithm/travelling_salesman_problem.py` — passed; 8 pytest items.
- Baseline direct doctests: `uv run python -m doctest -v genetic_algorithm/travelling_salesman_problem.py` — 39 passed.
- After adding the seeded generation doctest: the same pytest command passed; direct doctests passed (53 examples).