Genetic Algorithm (Evolutionary Art)

別名: evolutionary art、interactive evolution、genetic art、simulated evolution

初出 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). · 年表 ↗

発明者による命名 名称 · 命名 John Holland (1975) — 「遺伝的アルゴリズム」は John Holland(1975) が命名・確立した。

ひとことで 生物進化を模した生成・探索手法。候補作品を「ゲノム」として符号化した集団を、適応度関数(多くは観客の選択)で評価し、適者を交叉・突然変異させて次世代を作る。これを繰り返すことで、直接設計ではなく「選択」によって形が立ち現れる。

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

コード

上のデモの実際のスケッチ(サンドボックスで再現可能)。これで全部です。

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();

デモのソースを開く ↗

仕組み

各候補を、評価すると画像や3D形態を描くゲノム(パラメータ列や式木)として表現し、「表現→選択(多くは観客が選ぶ対話的適応度)→交叉・突然変異で次世代生成」を繰り返す。世代を経て集団は適応圧の報いる方向へ進む。対話的進化では観客が適応度関数となる。

パラメータ

系譜

先行例
Karl Sims、William Latham
関連
Particle System、Fractal / Recursive Subdivision

よく組み合わされるアルゴリズム

このアルゴリズムを使う作品 (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

参考文献

最終更新 2026-07-16