fugue-evo 0.3.1

An implementation of fugue for running evolutionary algorithms as Bayesian inference: priors and likelihoods as probabilistic programs, tempered SMC in trace space, annealed optimization, Pareto posteriors - plus a standalone classical EC toolkit
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
// docs/viz/inline.js — the fugue-evo micro-widget family (ambient, inline).
//
// Five small figures embeddable in tutorial prose via
//   <div class="fugue-explorable fv-inline" data-viz="NAME" data-...></div>
//
//   mini-convergence  best-so-far raw fitness vs generation   (ExploreGa)
//   mini-diversity    diversity collapse, scrubbable k        (ExploreGa)
//   mini-sigma        CMA-ES step size σ vs generation        (ExploreCma)
//   mini-front        a Pareto front forming, objective space (ExploreNsga2)
//   mini-migration    island plateaus + migration drops       (ExploreIsland)
//
// WASM-ONLY compute: every number drawn here comes from the real fugue-evo
// crate (crates/fugue-evo-wasm/src/explore.rs) via FV.wasmReady. At init each
// widget records a bounded generation window from its seed — the engines are
// deterministic, so the recording IS the run — then loops a cursor over that
// recording forever (seamless replay from the same seed, zero per-frame wasm).
// Without the wasm pkg the widget renders a short notice instead of animating
// (data-fugue-backend="none"); with it, data-fugue-backend="wasm".
//
// Fitness values are RAW benchmark values, lower is better. Seeds are u64
// (BigInt at the boundary), default 11 via data-seed. Under reduced motion the
// full window is already computed, so the finished final frame is drawn once.
//
// Self-contained ES5 IIFE; assumes fugue-viz.js + fugue-evo-wasm-loader.js
// loaded first (book.toml order). Ambient chrome only: canvas ≤ 140px, one
// pause glyph, at most one scrub, in-canvas readouts.
(function () {
  "use strict";
  if (typeof window === "undefined" || !window.FugueViz) return;
  var FV = window.FugueViz;

  var HZ = 7;          // ~6–8 generations per second (FV.pace)
  var HOLD_TICKS = 9;  // linger on the final frame before replaying

  // ==========================================================================
  // Small helpers
  // ==========================================================================

  function clampNum(v, a, b) { return v < a ? a : v > b ? b : v; }

  var _coarse = null;
  function coarsePointer() {
    if (_coarse === null) _coarse = !!(window.matchMedia && window.matchMedia("(pointer: coarse)").matches);
    return _coarse;
  }
  function glyphClear() { return coarsePointer() ? 34 : 24; }

  function fmtNum(v) {
    if (v == null || !isFinite(v)) return "";
    var a = Math.abs(v);
    if (a >= 100) return v.toFixed(0);
    if (a >= 10) return v.toFixed(1);
    return v.toFixed(2);
  }
  function fmtSig(v) {
    if (v == null || !isFinite(v)) return "";
    if (v >= 10) return v.toFixed(1);
    if (v >= 0.1) return v.toFixed(2);
    if (v >= 0.001) return v.toFixed(4);
    return v.toExponential(1);
  }

  function label(g, txt, x, y, c, role) {
    g.save(); g.fillStyle = role ? c[role] : c.ink; g.globalAlpha = role ? 0.95 : 0.65;
    g.font = "11px var(--mono-font, monospace)"; g.textBaseline = "top"; g.textAlign = "left";
    g.fillText(txt, x, y); g.restore();
  }
  function labelRight(g, txt, x, y, c, role) {
    g.save(); g.fillStyle = role ? c[role] : c.ink; g.globalAlpha = role ? 0.95 : 0.65;
    g.font = "11px var(--mono-font, monospace)"; g.textBaseline = "top"; g.textAlign = "right";
    g.fillText(txt, x, y); g.restore();
  }
  function baseline(g, x0, x1, y, c) {
    g.save(); g.strokeStyle = c.ink; g.globalAlpha = 0.22; g.lineWidth = 1;
    g.beginPath(); g.moveTo(x0, y); g.lineTo(x1, y); g.stroke(); g.restore();
  }
  function hotDot(g, x, y, c) {
    g.save(); g.globalAlpha = 0.28; g.fillStyle = c.hot;
    g.beginPath(); g.arc(x, y, 7, 0, 6.2832); g.fill();
    g.globalAlpha = 1;
    g.beginPath(); g.arc(x, y, 3, 0, 6.2832); g.fill();
    g.restore();
  }

  // ---- attribute parsing -----------------------------------------------------

  var LANDSCAPES = { sphere: 1, rastrigin: 1, rosenbrock: 1, ackley: 1, styblinski: 1 };
  var PROBLEMS = { zdt1: 1, zdt2: 1, zdt3: 1, schaffer: 1 };
  // Fallback bounds (from src/fitness/benchmarks.rs) should landscape_info fail.
  var BOUNDS = {
    sphere: [-5.12, 5.12], rastrigin: [-5.12, 5.12], rosenbrock: [-5, 10],
    ackley: [-32.768, 32.768], styblinski: [-5, 5]
  };

  function seedAttr(root) {
    var v = parseInt(root.getAttribute("data-seed"), 10);
    return v >= 0 ? v : 11;
  }
  function intAttr(root, name, dflt, lo, hi) {
    var v = parseInt(root.getAttribute(name), 10);
    if (!isFinite(v)) return dflt;
    return Math.round(clampNum(v, lo, hi));
  }
  function landAttr(root, dflt) {
    var v = (root.getAttribute("data-landscape") || "").toLowerCase();
    return LANDSCAPES[v] ? v : dflt;
  }
  function problemAttr(root, dflt) {
    var v = (root.getAttribute("data-problem") || "").toLowerCase();
    return PROBLEMS[v] ? v : dflt;
  }

  // ==========================================================================
  // WASM gating — every widget defers on FV.wasmReady; no JS fallback math.
  // ==========================================================================

  function registerWasm(name, initFn) {
    FV.register(name, function (root) {
      var p = FV.wasmReady || Promise.resolve(null);
      p.then(function (mod) {
        if (!mod) {
          root.setAttribute("data-fugue-backend", "none");
          var d = document.createElement("div");
          d.className = "fv-pg-notice";
          d.textContent = "This figure runs the real fugue-evo crate compiled to WebAssembly — the wasm package isn't available in this build.";
          root.appendChild(d);
          return;
        }
        root.setAttribute("data-fugue-backend", "wasm");
        try {
          initFn(root, mod);
        } catch (e) {
          if (typeof console !== "undefined") console.error("[fugue-viz] " + name + " init failed", e);
        }
      });
    });
  }

  function freeEngine(eng) {
    try { if (eng && typeof eng.free === "function") eng.free(); } catch (e) { /* best-effort */ }
  }

  // ==========================================================================
  // Recorders — run the real engine for a bounded window, keep only what the
  // canvas needs. Deterministic: same seed, same recording, forever.
  // ==========================================================================

  function recordGa(mod, landscape, seed, gens, k) {
    var eng = new mod.ExploreGa(landscape, 40, BigInt(seed));
    if (k > 0) eng.setTournamentSize(k);
    var frames = [], i, s;
    for (i = 0; i < gens; i++) {
      s = JSON.parse(eng.step());
      frames.push({ gen: s.gen, best: s.best[2], div: s.diversity });
    }
    freeEngine(eng);
    return frames;
  }

  function recordCma(mod, landscape, seed, gens) {
    var lo, hi;
    try {
      var info = JSON.parse(mod.explore_landscape_info(landscape));
      lo = info.lo; hi = info.hi;
    } catch (e) {
      var b = BOUNDS[landscape] || BOUNDS.rosenbrock;
      lo = b[0]; hi = b[1];
    }
    // Start in the far corner of the box; σ0 at the standard 0.3·range.
    var eng = new mod.ExploreCma(landscape, 0.7 * hi, 0.7 * lo, 0.3 * (hi - lo), 0, BigInt(seed));
    var frames = [], i, s;
    for (i = 0; i < gens; i++) {
      try { s = JSON.parse(eng.step()); } catch (e2) { break; }
      if (!(s.sigma > 0)) break;
      frames.push({ gen: s.gen, sigma: s.sigma, best: s.best[2] });
      if (s.converged) break;
    }
    freeEngine(eng);
    return frames;
  }

  function recordNsga(mod, problem, seed, gens) {
    var eng = new mod.ExploreNsga2(problem, 40, BigInt(seed));
    var frames = [], i, s;
    s = JSON.parse(eng.snapshot());          // gen 0: the unranked initial cloud
    frames.push({ gen: s.gen, pts: s.points });
    for (i = 0; i < gens; i++) {
      s = JSON.parse(eng.step());
      frames.push({ gen: s.gen, pts: s.points });
    }
    freeEngine(eng);
    return frames;
  }

  function recordIsland(mod, seed, interval, gens) {
    var eng = new mod.ExploreIsland("ackley", 4, 16, interval, "ring", BigInt(seed));
    var frames = [], i, j, s;
    for (i = 0; i < gens; i++) {
      s = JSON.parse(eng.step());
      var bests = [];
      for (j = 0; j < s.islands.length; j++) {
        var b = s.islands[j].best;
        bests.push(b ? b[2] : NaN);
      }
      frames.push({
        gen: s.gen,
        migrated: !!s.migrated,
        bests: bests,
        global: s.global_best ? s.global_best[2] : NaN
      });
    }
    freeEngine(eng);
    return frames;
  }

  // ==========================================================================
  // Mount scaffold — a cursor over a recording: ambient loop, pause glyph,
  // optional caption, reduced-motion static final frame, theme/resize repaint.
  // ==========================================================================

  function mount(root, spec) {
    var S = { frames: spec.frames || [], cursor: 0, hold: 0 };
    var ready = false;
    var cv = FV.canvas(root, {
      height: spec.height || 120,
      onResize: function () { if (ready) render(); }
    });
    var g = cv.ctx;

    var capText = root.getAttribute("data-caption");
    if (capText) {
      var cap = document.createElement("div");
      cap.className = "fv-caption";
      cap.textContent = capText;
      root.appendChild(cap);
    }

    var pacer = FV.pace(spec.hz || HZ);
    function render() {
      cv.clear();
      try { spec.draw(g, S, cv.w, cv.h, FV.theme().colors); } catch (e) { /* keep the page quiet */ }
    }
    function advance() {
      if (S.frames.length < 2) return;
      if (S.cursor >= S.frames.length - 1) {
        S.hold++;
        if (S.hold > HOLD_TICKS) { S.hold = 0; S.cursor = 0; } // replay: same seed, same recording
      } else {
        S.cursor++;
      }
    }
    var loopApi = FV.loop(root, function (dt) {
      for (var n = pacer(dt); n-- > 0;) advance();
      render();
    });

    var glyph = document.createElement("button");
    glyph.type = "button";
    glyph.className = "fv-glyph";
    glyph.setAttribute("aria-label", "Pause or play this animation");
    function glyphUpdate() {
      glyph.textContent = loopApi.playing ? "" : "";
      glyph.title = loopApi.playing ? "Pause" : "Play";
    }
    glyph.addEventListener("click", function () {
      if (loopApi.playing) loopApi.pause(); else loopApi.play();
      glyphUpdate(); render();
    });
    root.appendChild(glyph);

    FV.onThemeChange(function () { render(); });

    ready = true;
    if (loopApi.reduced) {
      // Full window already computed — show the finished picture, no motion.
      S.cursor = Math.max(0, S.frames.length - 1);
      render();
      glyph.style.display = "none";
    } else {
      render();
      loopApi.play();
      glyphUpdate();
    }

    return {
      root: root,
      setFrames: function (frames) {
        S.frames = frames || [];
        S.hold = 0;
        S.cursor = loopApi.reduced ? Math.max(0, S.frames.length - 1) : 0;
        render();
      }
    };
  }

  // Shared plot inset for the series widgets.
  function inset(w, h) { return { x0: 12, x1: w - 10, y0: 16, y1: h - 10 }; }

  // ==========================================================================
  // 1. mini-convergence — best-so-far raw fitness falling, sqrt-scaled.
  //    data-landscape (sphere|rastrigin|rosenbrock|ackley|styblinski, default
  //    rastrigin), data-seed (default 11).
  // ==========================================================================

  registerWasm("mini-convergence", function (root, mod) {
    var landscape = landAttr(root, "rastrigin");
    var seed = seedAttr(root);
    var frames = recordGa(mod, landscape, seed, 80, 0);

    mount(root, {
      height: 120,
      frames: frames,
      draw: function (g, S, w, h, c) {
        var F = S.frames, N = F.length;
        if (!N) return;
        var r = inset(w, h);
        // sqrt-scaled y over values ≥ 0 (best-so-far is monotone: max at F[0];
        // shift only if a landscape's raw optimum dips below zero).
        var base = Math.min(0, F[N - 1].best);
        function tf(v) { return Math.sqrt(Math.max(v - base, 0)); }
        var ymax = tf(F[0].best) || 1;
        var xs = FV.scale([0, N - 1], [r.x0, r.x1]);
        var ys = FV.scale([0, ymax * 1.08], [r.y1, r.y0]);
        baseline(g, r.x0, r.x1, r.y1, c);
        g.save();
        g.beginPath(); g.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.clip();
        var pts = [], i;
        for (i = 0; i <= S.cursor && i < N; i++) pts.push([xs(i), ys(tf(F[i].best))]);
        FV.curve(g, pts, { color: c.post, width: 1.6 });
        var cur = F[Math.min(S.cursor, N - 1)];
        hotDot(g, xs(Math.min(S.cursor, N - 1)), ys(tf(cur.best)), c);
        g.restore();
        label(g, landscape + " · gen " + cur.gen, r.x0, 2, c);
        labelRight(g, "best " + fmtNum(cur.best), w - 10 - glyphClear(), 2, c, "post");
      }
    });
  });

  // ==========================================================================
  // 2. mini-diversity — population diversity collapsing under selection
  //    pressure; tournament size k scrubbable in the caption row.
  //    data-seed (default 11), data-k (1..8, default 3).
  // ==========================================================================

  registerWasm("mini-diversity", function (root, mod) {
    var seed = seedAttr(root);
    var k = intAttr(root, "data-k", 3, 1, 8);

    var api = mount(root, {
      height: 120,
      frames: recordGa(mod, "rastrigin", seed, 80, k),
      draw: function (g, S, w, h, c) {
        var F = S.frames, N = F.length;
        if (!N) return;
        var r = inset(w, h);
        var ymax = 0, i;
        for (i = 0; i < N; i++) if (F[i].div > ymax) ymax = F[i].div;
        if (!(ymax > 0)) ymax = 1;
        var xs = FV.scale([0, N - 1], [r.x0, r.x1]);
        var ys = FV.scale([0, ymax * 1.1], [r.y1, r.y0]);
        baseline(g, r.x0, r.x1, r.y1, c);
        g.save();
        g.beginPath(); g.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.clip();
        var pts = [];
        for (i = 0; i <= S.cursor && i < N; i++) pts.push([xs(i), ys(F[i].div)]);
        FV.curve(g, pts, { color: c.flow, width: 1.6 });
        var cur = F[Math.min(S.cursor, N - 1)];
        hotDot(g, xs(Math.min(S.cursor, N - 1)), ys(cur.div), c);
        g.restore();
        label(g, "gen " + cur.gen, r.x0, 2, c);
        labelRight(g, "diversity " + fmtNum(cur.div), w - 10 - glyphClear(), 2, c, "flow");
      }
    });

    // The one control: a Victor scrub for tournament size k in the caption row.
    // Scrubbing rebuilds the whole recording from the same seed — bigger k,
    // faster collapse: selection pressure in one picture.
    var row = document.createElement("div");
    row.className = "fv-caption";
    row.appendChild(document.createTextNode("selection pressure: tournament k = "));
    var span = document.createElement("span");
    row.appendChild(span);
    row.appendChild(document.createTextNode(" — bigger k collapses diversity faster"));
    root.appendChild(row);
    FV.scrub(span, {
      min: 1, max: 8, step: 1, value: k,
      onInput: function (v) {
        api.setFrames(recordGa(mod, "rastrigin", seed, 80, v));
      }
    });
  });

  // ==========================================================================
  // 3. mini-sigma — CMA-ES global step size σ shrinking, log-scaled.
  //    data-landscape (default rosenbrock), data-seed (default 11).
  // ==========================================================================

  registerWasm("mini-sigma", function (root, mod) {
    var landscape = landAttr(root, "rosenbrock");
    var seed = seedAttr(root);
    var frames = recordCma(mod, landscape, seed, 100);

    mount(root, {
      height: 120,
      frames: frames,
      draw: function (g, S, w, h, c) {
        var F = S.frames, N = F.length;
        if (!N) return;
        var r = inset(w, h);
        var lo = Infinity, hi = -Infinity, i, s;
        for (i = 0; i < N; i++) {
          s = F[i].sigma;
          if (s < lo) lo = s;
          if (s > hi) hi = s;
        }
        if (!(lo > 0)) lo = 1e-12;
        if (!(hi > lo)) hi = lo * 10;
        var xs = FV.scale([0, N - 1], [r.x0, r.x1]);
        var ys = FV.scale([Math.log(lo) - 0.15, Math.log(hi) + 0.15], [r.y1, r.y0]);
        baseline(g, r.x0, r.x1, r.y1, c);
        g.save();
        g.beginPath(); g.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.clip();
        var pts = [];
        for (i = 0; i <= S.cursor && i < N; i++) pts.push([xs(i), ys(Math.log(F[i].sigma))]);
        FV.curve(g, pts, { color: c.prior, width: 1.6 });
        var cur = F[Math.min(S.cursor, N - 1)];
        hotDot(g, xs(Math.min(S.cursor, N - 1)), ys(Math.log(cur.sigma)), c);
        g.restore();
        label(g, landscape + " · gen " + cur.gen + " · log σ", r.x0, 2, c);
        labelRight(g, "σ " + fmtSig(cur.sigma), w - 10 - glyphClear(), 2, c, "prior");
      }
    });
  });

  // ==========================================================================
  // 4. mini-front — NSGA-II objective-space scatter; the Pareto front forms
  //    against the analytic reference. data-problem (zdt1|zdt2|zdt3|schaffer,
  //    default zdt1), data-seed (default 11). Fixed axes f1∈[0,1.05], f2∈[0,4.5].
  // ==========================================================================

  function refFront(problem) {
    // Analytic reference fronts where a single smooth curve exists on these
    // axes. zdt1: f2 = 1 − sqrt(f1); zdt2: f2 = 1 − f1².
    if (problem !== "zdt1" && problem !== "zdt2") return null;
    var pts = [], i, t;
    for (i = 0; i <= 80; i++) {
      t = i / 80;
      pts.push([t, problem === "zdt1" ? 1 - Math.sqrt(t) : 1 - t * t]);
    }
    return pts;
  }

  registerWasm("mini-front", function (root, mod) {
    var problem = problemAttr(root, "zdt1");
    var seed = seedAttr(root);
    var frames = recordNsga(mod, problem, seed, 60);
    var ref = refFront(problem);

    mount(root, {
      height: 140,
      frames: frames,
      draw: function (g, S, w, h, c) {
        var F = S.frames, N = F.length;
        if (!N) return;
        var r = inset(w, h);
        var xs = FV.scale([0, 1.05], [r.x0, r.x1]);
        var ys = FV.scale([0, 4.5], [r.y1, r.y0]);
        // frame
        g.save(); g.strokeStyle = c.ink; g.globalAlpha = 0.22; g.lineWidth = 1;
        g.strokeRect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.restore();
        g.save();
        g.beginPath(); g.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.clip();
        // faint analytic reference front (data role)
        if (ref) {
          var rp = [], i;
          for (i = 0; i < ref.length; i++) rp.push([xs(ref[i][0]), ys(ref[i][1])]);
          g.save(); g.globalAlpha = 0.45;
          FV.curve(g, rp, { color: c.data, width: 1.2, dash: [3, 3] });
          g.restore();
        }
        var cur = F[Math.min(S.cursor, N - 1)];
        var pts = cur.pts, j, p;
        // dominated / unranked points first — faint chrome
        g.save(); g.fillStyle = c.ink; g.globalAlpha = 0.25;
        for (j = 0; j < pts.length; j++) {
          p = pts[j];
          if (p[2] === 0) continue;
          g.beginPath(); g.arc(xs(p[0]), ys(p[1]), 1.8, 0, 6.2832); g.fill();
        }
        g.restore();
        // rank-0 (the front) — post
        g.save(); g.fillStyle = c.post; g.globalAlpha = 0.9;
        for (j = 0; j < pts.length; j++) {
          p = pts[j];
          if (p[2] !== 0) continue;
          g.beginPath(); g.arc(xs(p[0]), ys(p[1]), 2.4, 0, 6.2832); g.fill();
        }
        g.restore();
        g.restore(); // end clip
        label(g, problem + " · f1 × f2", r.x0, 2, c);
        labelRight(g, "gen " + cur.gen, w - 10 - glyphClear(), 2, c, "post");
      }
    });
  });

  // ==========================================================================
  // 5. mini-migration — 4 ring islands on ackley: separated plateaus, violet
  //    migration ticks, drops after each tick. data-seed (default 11),
  //    data-interval (1..40, default 8).
  // ==========================================================================

  registerWasm("mini-migration", function (root, mod) {
    var seed = seedAttr(root);
    var interval = intAttr(root, "data-interval", 8, 1, 40);
    var frames = recordIsland(mod, seed, interval, 80);
    var ALPHAS = [0.35, 0.5, 0.7, 0.9];

    mount(root, {
      height: 130,
      frames: frames,
      draw: function (g, S, w, h, c) {
        var F = S.frames, N = F.length;
        if (!N) return;
        var r = inset(w, h);
        // sqrt-scaled best fitness (ackley raw ≥ 0) so late drops stay visible.
        function tf(v) { return Math.sqrt(Math.max(v, 0)); }
        var ymax = 0, i, j, v;
        for (i = 0; i < N; i++) {
          for (j = 0; j < F[i].bests.length; j++) {
            v = F[i].bests[j];
            if (isFinite(v) && tf(v) > ymax) ymax = tf(v);
          }
        }
        if (!(ymax > 0)) ymax = 1;
        var xs = FV.scale([0, N - 1], [r.x0, r.x1]);
        var ys = FV.scale([0, ymax * 1.08], [r.y1, r.y0]);
        baseline(g, r.x0, r.x1, r.y1, c);
        g.save();
        g.beginPath(); g.rect(r.x0, r.y0, r.x1 - r.x0, r.y1 - r.y0); g.clip();
        // migration ticks (flow), revealed with the timeline
        g.save(); g.strokeStyle = c.flow; g.globalAlpha = 0.4; g.lineWidth = 1;
        for (i = 0; i <= S.cursor && i < N; i++) {
          if (!F[i].migrated) continue;
          g.beginPath(); g.moveTo(xs(i), r.y0); g.lineTo(xs(i), r.y1); g.stroke();
        }
        g.restore();
        // 4 per-island best traces (post, layered alphas)
        for (j = 0; j < 4; j++) {
          var pts = [];
          for (i = 0; i <= S.cursor && i < N; i++) {
            v = F[i].bests[j];
            pts.push([xs(i), isFinite(v) ? ys(tf(v)) : NaN]);
          }
          g.save(); g.globalAlpha = ALPHAS[j];
          FV.curve(g, pts, { color: c.post, width: 1.2 });
          g.restore();
        }
        var cur = F[Math.min(S.cursor, N - 1)];
        if (isFinite(cur.global)) {
          var gi = Math.min(S.cursor, N - 1);
          hotDot(g, xs(gi), ys(tf(cur.global)), c);
        }
        g.restore(); // end clip
        label(g, "4 islands · ring · gen " + cur.gen, r.x0, 2, c);
        labelRight(g, "best " + fmtNum(cur.global), w - 10 - glyphClear(), 2, c, "post");
      }
    });
  });
})();