Genetic Algorithm (Evolutionary Art)

also: evolutionary art, interactive evolution, genetic art, simulated evolution

First appeared 1975 · John Holland — Genetic algorithms were formalised by John Holland ("Adaptation in Natural and Artificial Systems", 1975); applied to visual form via Richard Dawkins's Biomorphs (1986) and Karl Sims's "Artificial Evolution for Computer Graphics" (1991). · timeline ↗

inventor-coined Name · coined by John Holland (1975) — Coined/popularized by John Holland (1975), who established "genetic algorithms" as a field.

In one line A search/generation method modelled on biological evolution: a population of candidate artworks, each encoded as a "genome", is repeatedly evaluated by a fitness function — often the live choices of a human audience — and the fittest are bred (recombined) and mutated to form the next generation, so form emerges by selection rather than by direct design.

Genetic Algorithm (Evolutionary Art) · Open in new tab ↗ · Motion begins only when you press Run.

The code

The actual sketch behind the demo above — sandboxed and reproducible. This is the whole thing.

genetic-algorithm/index.html
// Genetic algorithm — evolve a population of genomes (each a set of translucent
// triangles) toward a TARGET image. Real GA loop: evaluate fitness (how closely
// a genome's rendering matches the target), then breed the next generation by
// tournament SELECTION → one-point CROSSOVER → MUTATION, with elitism. The best
// genome, shown upscaled, visibly sharpens toward the target over generations.
const SEED = (Math.random() * 1e9) | 0;
let s = SEED || 1;
const rnd = () => (s = (s * 1103515245 + 12345) & 0x7fffffff) / 0x7fffffff;

const DIM = Math.floor(Math.min(window.innerWidth, window.innerHeight));
const cv = document.getElementById('c');
cv.width = cv.height = DIM; cv.style.width = cv.style.height = DIM + 'px';
const view = cv.getContext('2d');

const F = 72;                       // fitness canvas (small = fast pixel compare)
const off = new OffscreenCanvas(F, F), octx = off.getContext('2d', { willReadFrequently: true });
const tgt = new OffscreenCanvas(F, F), tctx = tgt.getContext('2d');

// --- procedural target: a few bold shapes (self-contained, no external image) ---
tctx.fillStyle = '#12203a'; tctx.fillRect(0, 0, F, F);
const shapes = 4 + (rnd() * 3 | 0);
for (let i = 0; i < shapes; i++) {
  tctx.fillStyle = `hsl(${rnd()*360|0} ${60+rnd()*30|0}% ${45+rnd()*25|0}%)`;
  tctx.beginPath();
  tctx.arc(rnd()*F, rnd()*F, F*(0.12 + rnd()*0.22), 0, 7);
  tctx.fill();
}
const target = tctx.getImageData(0, 0, F, F).data;

// --- GA parameters ---
const POP = 26, GENES = 34, ELITE = 2, MUT = 0.08;
const randTri = () => ({
  x1: rnd(), y1: rnd(), x2: rnd(), y2: rnd(), x3: rnd(), y3: rnd(),
  r: rnd()*255|0, g: rnd()*255|0, b: rnd()*255|0, a: 0.15 + rnd()*0.4,
});
const randGenome = () => Array.from({ length: GENES }, randTri);
let pop = Array.from({ length: POP }, randGenome);

function draw(genome, c) {
  c.fillStyle = '#12203a'; c.fillRect(0, 0, F, F);
  for (const t of genome) {
    c.fillStyle = `rgba(${t.r},${t.g},${t.b},${t.a})`;
    c.beginPath();
    c.moveTo(t.x1*F, t.y1*F); c.lineTo(t.x2*F, t.y2*F); c.lineTo(t.x3*F, t.y3*F);
    c.closePath(); c.fill();
  }
}
function fitness(genome) {
  draw(genome, octx);
  const px = octx.getImageData(0, 0, F, F).data;
  let err = 0;
  for (let i = 0; i < px.length; i += 4) {
    const dr = px[i]-target[i], dg = px[i+1]-target[i+1], db = px[i+2]-target[i+2];
    err += dr*dr + dg*dg + db*db;
  }
  return -err;                        // higher (less error) is fitter
}
const clone = (g) => g.map((t) => ({ ...t }));
function mutate(g) {
  for (const t of g) {
    if (rnd() < MUT) { const k = ['x1','y1','x2','y2','x3','y3'][rnd()*6|0]; t[k] = Math.min(1, Math.max(0, t[k] + (rnd()-0.5)*0.3)); }
    if (rnd() < MUT) { t.r = (t.r + (rnd()-0.5)*90)&255|0; t.g = (t.g + (rnd()-0.5)*90)&255|0; t.b = (t.b + (rnd()-0.5)*90)&255|0; }
    if (rnd() < MUT) t.a = Math.min(0.6, Math.max(0.05, t.a + (rnd()-0.5)*0.2));
  }
  return g;
}
function tournament(scored) {
  let best = scored[rnd()*POP|0];
  for (let i = 0; i < 3; i++) { const c = scored[rnd()*POP|0]; if (c.f > best.f) best = c; }
  return best.g;
}

let gen = 0;
const hud = document.getElementById('hud');
function stepGen() {
  const scored = pop.map((g) => ({ g, f: fitness(g) })).sort((a, b) => b.f - a.f);
  // draw current best, upscaled
  draw(scored[0].g, octx);
  view.imageSmoothingEnabled = true;
  view.drawImage(off, 0, 0, DIM, DIM);
  hud.textContent = 'gen ' + gen;
  // breed next generation: elitism + crossover + mutation
  const nxt = scored.slice(0, ELITE).map((s) => clone(s.g));
  while (nxt.length < POP) {
    const a = tournament(scored), b = tournament(scored);
    const cut = rnd() * GENES | 0;
    const child = a.slice(0, cut).concat(b.slice(cut)).map((t) => ({ ...t }));
    nxt.push(mutate(child));
  }
  pop = nxt; gen++;
}
function loop() { stepGen(); requestAnimationFrame(loop); }
loop();

Open the demo source ↗

How it works

Represent each candidate as a genome — e.g. a string of parameters or a symbolic expression tree that, when evaluated, renders an image or 3D form. Then iterate: 1. Express: render each genome in the population to a phenotype (the artwork). 2. Select: score each by a fitness function — frequently interactive (viewers pick favourites), sometimes automatic. 3. Breed: recombine ("crossover") the genomes of selected parents and apply random mutation to produce the next generation. Over generations the population drifts toward whatever the fitness pressure rewards. "Interactive evolution" makes the audience the fitness function.

Parameters

Lineage

Precedents
Karl Sims, William Latham
Related
Particle System, Fractal / Recursive Subdivision

Frequently combined with

Works using this algorithm (9)

Breed
driessens-verstappen
Electric Sheep
scott-draves
Evolved Virtual Creatures
karl-sims
Fifty Sisters
jon-mccormack
Galápagos
karl-sims
Genetic Images
karl-sims
Panspermia
karl-sims
Primordial Dance
karl-sims
Tube Horn with Egg
william-latham

References

Last updated 2026-07-16