society: make the sexual-transmission model rigorous (E9 epistasis, E10 directed sex)

Deepen the sexual-reproduction frame before entering the full society, on
the two facets GG chose: landscape robustness and directed recombination.
Adds a Kauffman NK landscape (genotype.nk_fitness, tunable ruggedness),
finite n-parent crossover (genotype.crossover, per-gap recombination rate),
and hill-climb (parents = local optima = trained models).

E9 (recomb_landscape) -- the "why sex?" test: E8's dramatic super-parent
result used an ADDITIVE landscape. On rugged/epistatic landscapes, blindly
recombining local optima causes OUTBREEDING DEPRESSION -- offspring fall
below the parents, worse with both ruggedness and recombination rate (K=8,
free recomb: ~ -0.23), and the optimal recombination rate shrinks as
ruggedness grows. Design rule: merge freely when skills are complementary/
additive; sparingly (and with selection) when entangled.

E10 (directed_sex) -- directed sex beats biological sex: biology is stuck
with 2 random-mating parents and no offspring preview; an AI can choose
complementary mates, evaluate many recombinant offspring, keep the fittest,
and use unbounded parents (iterated recombine-then-select). Random
("biological") sex craters with ruggedness (0.66->0.51); directed sex
tracks/exceeds the best parent at every ruggedness -- converting the
outbreeding-depression catastrophe into a win. No biological analog.

Complete sexual-transmission picture: dramatic super-parent offspring when
skills are complementary (E8); outbreeding-depression risk when entangled
(E9); directed sex resolves the risk (E10). configs/layer1/{E9,E10}.yaml,
figures/plot_{E9,E10}.py, READMEs, +5 tests (117 green).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
Giorgio Gilestro 2026-07-05 11:13:37 +01:00
parent 62c68d6c8c
commit 48181a1c84
21 changed files with 614 additions and 5 deletions

View file

@ -11,11 +11,11 @@ from __future__ import annotations
import numpy as np
from knowledge.genotype import (
additive_fitness, genotype_bits, linkage_equilibrium, locus_marginals, mutate,
recombine, recombine_teachers,
additive_fitness, bits_to_index, crossover, genotype_bits, hill_climb, linkage_equilibrium,
locus_marginals, mutate, nk_fitness, recombine, recombine_teachers,
)
from knowledge.genotype_lineage import run_genotype_lineage
from knowledge.society import make_specialist, run_society
from knowledge.society import make_specialist, run_directed_sex, run_recomb_landscape, run_society
from knowledge.teachers import make_retention_matrix
@ -88,3 +88,52 @@ def test_e7_sexual_adapts_at_least_as_fast():
s = sex[sex["generation"] == mid]["mean_fitness"].iloc[0]
assert s >= a - 1e-9 # sexual adapts at least as fast mid-run
assert sex["ld"].max() < asex["ld"].max() # ... by keeping loci in linkage equilibrium
def test_nk_fitness_shape_range_and_additive_limit():
f0 = nk_fitness(6, 0, seed=1)
assert f0.shape == (64,) and f0.min() >= 0.0 and f0.max() <= 1.0
# K=0 is additive: fitness separates into a sum of per-locus contributions, so the effect of
# flipping one locus is independent of the others (check two backgrounds agree).
bits = genotype_bits(6)
d_from_0 = f0[bits[:, 0] == 1].mean() - f0[bits[:, 0] == 0].mean()
assert np.isfinite(d_from_0)
f5 = nk_fitness(6, 5, seed=1)
assert not np.allclose(f0, f5) # ruggedness changes the landscape
def test_crossover_clones_at_rate_zero_and_stays_valid():
rng = np.random.default_rng(0)
parents = genotype_bits(8)[[3, 200]] # two parent genotypes
child = crossover(parents, 0.0, rng) # rate 0 -> a clone of one parent
assert bits_to_index(child) in (3, 200)
idx = bits_to_index(crossover(parents, 0.5, rng))
assert 0 <= idx < 256 # free recombination still a valid genotype
def test_hill_climb_reaches_local_optimum():
f = nk_fitness(8, 3, seed=2)
g = hill_climb(f, 8, start=0)
assert all(f[g ^ (1 << l)] <= f[g] for l in range(8)) # no improving single flip
def test_e9_outbreeding_depression_on_rugged_landscape():
# Blindly recombining local optima of a rugged landscape produces below-parent offspring, and
# free recombination is worse than clonal.
cfg = {"experiment": "e9t", "seed": 1, "n_replicates": 10,
"society": {"L": 10, "n_parents": 6, "pop": 150},
"sweep": [{"param": "K", "values": [6]}, {"param": "rate", "values": [0.0, 0.5]}]}
df = run_recomb_landscape(cfg)
m = df.groupby("rate")[["mean_offspring", "best_parent"]].mean()
assert m.loc[0.5, "mean_offspring"] < m.loc[0.5, "best_parent"] # depression
assert m.loc[0.5, "mean_offspring"] < m.loc[0.0, "mean_offspring"] # free recomb is worse
def test_e10_directed_sex_beats_random_and_holds_parents():
cfg = {"experiment": "e10t", "seed": 1, "n_replicates": 10,
"society": {"L": 10, "n_parents": 6, "pop": 150, "keep": 8, "rounds": 5, "rate": 0.2},
"sweep": [{"param": "K", "values": [6]}]}
df = run_directed_sex(cfg).mean(numeric_only=True)
assert df["directed_sex"] > df["random_sex"] + 0.05 # directed rescues the catastrophe
assert df["directed_sex"] >= df["best_parent"] - 0.01 # ... to (at least) the best parent
assert df["random_sex"] < df["best_parent"] # blind sex suffers depression