// Population spawning — the pure-TS half of the breeder loop (no WebGL, no Svelte). Given a // parent genome (or none), produce a generation of candidate CPPNs to fill the breeder grid. // // newGeneration(null) -> a diverse spread of fresh random lineages (first grid) // spawnChildren([parent], n) -> n single-mutation children of one parent (refine a pick) // spawnChildren([a, b], n) -> n crossover+mutate children of two parents (a cross) // // All genome structure lives in CPPN; this module only orchestrates the NEAT-lite operators. // Morphology/environment are inherited unchanged elsewhere (morphology sigmas are 0 in v0), so // only the rule-encoding CPPN varies across a generation. import { CPPN } from "../genome/cppn"; import { Rng } from "../genome/rng"; import { N_IN, N_OUT } from "../genome/compile"; // A minimal CPPN is near-trivial (often dead) dynamics, so seed each fresh genome with a handful // of mutations — enough structure to render something living on first view. (Shared with the // single-genome editor in state/genome.svelte.ts.) export const INITIAL_MUTATIONS = 20; /** A fresh, deterministic genome for `seed`: minimal topology + INITIAL_MUTATIONS steps. */ export function freshGenome(seed: number): CPPN { const rng = new Rng(seed); let net = CPPN.minimal(N_IN, N_OUT, rng); for (let i = 0; i < INITIAL_MUTATIONS; i++) net = net.mutate(); return net; } /** A diverse generation: `n` independent fresh lineages from consecutive seeds off `seed0`. */ export function randomGenomes(n: number, seed0: number): CPPN[] { let s = seed0 >>> 0; const out: CPPN[] = []; for (let i = 0; i < n; i++) { out.push(freshGenome(s)); s = (Math.imul(s, 1664525) + 1013904223) >>> 0; // LCG hop so seeds aren't adjacent integers } return out; } /** * `n` children of the current selection. One parent → single-mutation variants; two parents → * crossover then a mutation (so siblings differ). Each call advances the parents' shared rng, so * the n children diverge from one another. */ export function spawnChildren(parents: CPPN[], n: number): CPPN[] { if (parents.length === 0) throw new Error("spawnChildren needs at least one parent"); const out: CPPN[] = []; for (let i = 0; i < n; i++) { const child = parents.length >= 2 ? parents[0].crossover(parents[1]).mutate() : parents[0].mutate(); out.push(child); } return out; }