From 457bc42cff608bd60d18ccd2416349cd98c6cda9 Mon Sep 17 00:00:00 2001 From: Pruthviraj Gojare Date: Sun, 22 Nov 2026 00:22:14 +0530 Subject: [PATCH 1/2] Add seeded TSP generation doctest Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- .../travelling_salesman_problem.py | 28 +++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/genetic_algorithm/travelling_salesman_problem.py b/genetic_algorithm/travelling_salesman_problem.py index 44aa27fbc539..b22b48b076d8 100644 --- a/genetic_algorithm/travelling_salesman_problem.py +++ b/genetic_algorithm/travelling_salesman_problem.py @@ -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") From 8f3e3241ef9e13d4a202ee35e9c905d3d29372e7 Mon Sep 17 00:00:00 2001 From: Pruthviraj Gojare Date: Sun, 22 Nov 2026 00:22:21 +0530 Subject: [PATCH 2/2] Add seeded TSP execution trace Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- work/R23-T049/TRACE.md | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) create mode 100644 work/R23-T049/TRACE.md diff --git a/work/R23-T049/TRACE.md b/work/R23-T049/TRACE.md new file mode 100644 index 000000000000..5f030fdd81c4 --- /dev/null +++ b/work/R23-T049/TRACE.md @@ -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).