Three.js Genetic Programming (Symbolic Regression)

Evolve mathematical programs in Three.js, where a population of expression trees uses tournament selection, subtree crossover and mutation to rediscover a target function from data, shown with the best program's curve and a live tree diagram.

Key topics: genetic programming, symbolic regression, expression trees, evolutionary algorithm, three.js ai, code evolution

What this code does

- Programs as Trees: functions (+ − × ÷ sin cos) are inner nodes and terminals (x and constants) are leaves; evaluation is a recursive walk.
- Fitness: RMSE against 41 samples of the target, plus a parsimony penalty per node to fight bloat.
- Selection: tournaments of five pick parents, and the best four survive unchanged (elitism).
- Crossover & Mutation: grafting a random subtree from one parent into another recombines building blocks; mutation replaces a subtree with a fresh random one.
- Protected Ops: division by ~0 returns 1 so every program is valid.
- Controls: target, population, mutation rate, parsimony, generations per second and new population.

Continue Learning

JavaScript Implementation

ES6+
// Genetic programming: evolve expression trees by selection, crossover and mutation
const fitness = (tree) => Math.sqrt(xs.reduce((s, x, i) => s + (evalTree(tree, x) - ys[i]) ** 2, 0) / xs.length)
let population = Array.from({ length: 300 }, () => randomTree(4))
for (let gen = 0; gen < 200; gen++) {
  const scored = population.map(t => ({ t, f: fitness(t) })).sort((a, b) => a.f - b.f)
  const pick = () => Array.from({ length: 5 }, () => scored[Math.random() * scored.length | 0]).sort((a, b) => a.f - b.f)[0].t
  population = [scored[0].t, ...Array.from({ length: 299 }, () => {
    const child = clone(pick())
    replaceRandomNode(child, randomSubtree(pick()))     // crossover
    if (Math.random() < 0.2) replaceRandomNode(child, randomTree(2)) // mutation
    return child
  })]
}