Genealogy and stepping stones ============================= When an elite appears in a new niche, its genealogy shows which earlier solutions contributed to it. Use ``qdpy.lineage`` to collect actual parent IDs and ``qdpy.genealogy`` to project those relationships onto descriptor space. For test-based search, ``qdpy.phylogeny`` can also use measured case errors from close relatives during selection. Run the three CPU examples together to generate the figures below, the MAP-Elites JSONL traces, and a JSON summary. .. code-block:: sh python examples/genealogy.py results/genealogy Record MAP-Elites ancestry -------------------------- Enable ``record_measurements`` when you need logged fitness values and descriptors. The recorder supports the native polynomial, Gaussian, curiosity, Iso+LineDD, and IsoLineCross emitters. Logs without the flag still support tree drawing and cell transitions wherever cells were recorded. A warm-start archive contributes founders with its current measurements. Use a new JSONL file for each run. .. code-block:: python from math import prod from qdpy.genealogy import ancestor_archive, goal_switches, plot_ancestry_archive from qdpy.lineage import LineageRecorder, plot_lineage, read_lineage with LineageRecorder("lineage.jsonl", record_measurements=True).attach(search) as recorder: search.optimise(evaluate) elite = recorder.id_for(search.best()) nodes = read_lineage("lineage.jsonl") plot_lineage(nodes, elite=elite) plot_ancestry_archive(nodes, elite, grid=reporting_grid) projected = ancestor_archive(nodes, elite, grid=reporting_grid) coverage = projected.filled_bins / prod(projected.shape) score = projected.qd_score() switches = goal_switches(nodes, elite=elite) The reporting grid can be a fixed ``Grid`` or ``CVTGrid`` with the same descriptor definitions and objective weights. The projection keeps one representative per cell and considers shared crossover ancestors once. By default the focal elite contributes to the arrows and is excluded from the archive score. Use ``include_elite=True`` to score it together with its ancestors. An ancestor that was later replaced can still contribute. Logs contain no genomes. The path overlay follows Figure 4 of Mouret and Clune. The ancestor-only archive and its coverage and QD score follow the genealogical analysis in Nordmoen et al. They describe the ancestry of one solution. For a run-level summary, compute them for each chosen elite on the same grid. The example uses QDpy's roulette curiosity emitter on Rastrigin. `Mouret and Clune (2015) `__. `Nordmoen et al. (2021), section 3.3 `__. Find stepping stones across runs -------------------------------- Build a search trajectory network by grouping ancestor locations across runs. A node is a location such as an archive cell. A directed edge records an actual parent–child move. Use the same locations for all runs. Pass ``targets`` to keep only the ancestry of the chosen elites. A mutation-only run provides a single ancestry path per target. Crossover can contribute multiple paths and the graph retains both parent edges. .. code-block:: python from qdpy.genealogy import ancestry, lineage_trajectory_network, plot_stepping_stones runs = {"seed-11": nodes} targets = {"seed-11": (elite,)} graph = lineage_trajectory_network(runs, targets=targets) starts = [item.cell for item in ancestry(nodes, elite, include_elite=True).values() if not item.parents and item.cell is not None] report = graph.stepping_stones([(start, nodes[elite].cell) for start in starts]) plot_stepping_stones(graph, analysis=report) Following Nadizar et al., ``stepping_stones`` counts the shortest directed paths between specified start and final locations. It flags intermediate locations strictly above the 75th percentile by default. Tied shortest paths all count. After aggregation, a shortest path can join segments from different runs. Check the original lineage to see whether any one run followed that entire path. ``betweenness()`` measures the corresponding all-pairs centrality. ``worsening_fraction`` reports how many measured transitions reduce the selected objective, using its optimization direction. Independent trajectories can be supplied to ``search_trajectory_network`` as sequences of state labels. `Ochoa et al. (2021) `__. `Nadizar et al. (2025), sections 3–4 `__. .. figure:: benchmarks/archive-genealogy.png :alt: A mutation chain, its paths over a descriptor grid, and the pooled trajectory network. The left and middle panels trace one retained elite through five ancestor cells. The right panel pools the best elites from six Rastrigin runs and rings the one cell above the stepping-stone cutoff. .. _classifier-guided-genealogy: Trace goal switches in classifier-guided search ----------------------------------------------- ``goal_switches`` reports observed parent–child changes between archive cells or custom categorical labels. When one candidate can be champion for several classes, use ``classifier_illumination``. Each evaluation can improve any class. A switch is credited when a child improves a class other than the class sampled for its parent. The result records the source and target classes and the parent and child IDs for each switch. .. code-block:: python from qdpy.algorithms import classifier_illumination result = classifier_illumination(initialise, mutate, classify, classes=4, budget=180, initial_samples=5, seed=29) for event in result.switches: print(event.source, event.target, event.parent, event.child) The example uses a small continuous image encoding and an entirely local template classifier. To use another model, make ``classify`` return one score per class in a fixed order. Class scores are maximized. This implements the fixed-class selection and archive mechanism from the Innovation Engine. ImageNet, CPPN evolution and the other experiments in the paper require their own representation and evaluator. `Nguyen, Yosinski, and Clune (2016), sections 4.2 and 5.2 `__. .. figure:: benchmarks/classifier-goals.png :alt: A goal switch highlighted on a lineage beside the four evolved image champions. A fixed-seed run produced four image champions and observed one goal switch in 180 classifier calls. Several class records can change in the same call. .. _phylogeny-informed-lexicase: Estimate missing lexicase cases from relatives ---------------------------------------------- Measure the case subsets returned by ``case_cohorts``. Keep those values by node ID, using NaN for unmeasured cases. Estimate the rest once before drawing parents. Ancestor mode searches parents only. Relative mode also follows child edges and can find peers or cousins. The caller supplies the genealogy known when the selection occurs and chooses the finite fallback error for unresolved cases. .. code-block:: python from qdpy.algorithms import lexicase_index from qdpy.phylogeny import case_cohorts, estimate_case_errors groups = case_cohorts(len(candidate_ids), case_count, rng, cohorts=3) # Evaluate only the returned row/column blocks and update measured_errors. estimates = estimate_case_errors(nodes, measured_errors, candidate_ids, mode="relative", max_distance=4, missing_error=1.) pool, _ = groups[rng.integers(len(groups))] parent_id = candidate_ids[pool[lexicase_index(estimates.errors[pool], rng)]] Set ``sample_size=`` in place of ``cohorts=`` to use a shared down-sample. In both cases lexicase receives all case columns after estimation. ``estimates.donors`` identifies the original measured node for each score. A self-ID means the candidate was directly measured, and ``-1`` means the supplied fallback was used. Keep estimates separate from the measured error table. Recombining ancestry is supported by searching both recorded parents. The paper's experiments used asexual reproduction. `Lalejini et al. (2024) `__. .. figure:: benchmarks/phylogenetic-estimation.png :alt: Two candidate-by-case matrices distinguishing direct measurements, estimates, and fallbacks. Both examples used 240 direct case evaluations where full evaluation would use 720. The final estimates were checked against separately evaluated full case scores. This illustrates estimation under two sampling designs.