Skip to content
Darwin's Sandbox
Simulation

Darwin's Sandbox

2025-2026Solo project
RustWebAssembly (wasm-bindgen)Next.js 14TypeScriptTailwind CSSZustandHTML Canvas 2DWeb Workers

Live Demo

Live preview of Darwin's Sandbox

Problem

A dual-purpose simulation platform: (1) a real-time evolution simulator where neural network creatures compete, mutate, and undergo natural selection, and (2) an STH (Soil-Transmitted Helminth) infection/deworming simulation built for my girlfriend's thesis on parasite transmission dynamics in Cebu City. Agent-based models with Ascaris, Trichuris, and Hookworm parasites, stochastic SIS transmission with WHO EPG thresholds, KAP behavioral models, 6 intervention types, environmental contamination grids, and urban vs rural comparison mode. 500 agents at 60fps in <1ms per tick.

Constraints

  • -Must maintain 60fps with 500 agents (<1ms per tick) regardless of population size
  • -Deterministic given a seed across browsers
  • -Closed energy budget: total world energy conserved to create natural carrying capacity
  • -STH model must calibrate against real thesis data from Cebu City
  • -WASM binary must be under 200KB for reasonable page load
  • -No transcendental math (sin/cos/tanh differ across WASM engines)

Architecture

Rust simulation engine compiled to WASM via wasm-pack. Evolution sim: slotmap DenseSlotMap for O(1) insert/remove, two-pass counting spatial hash with zero per-tick allocations, 12-input/8-hidden/3-output feedforward neural networks with polynomial tanh for cross-browser determinism. STH sim: agent-based SIS infection model with stochastic transmission, WHO EPG thresholds for intensity classification, KAP (Knowledge, Attitudes, Practices) behavioral model, environmental contamination grid with decay/rain dynamics, and 6 intervention types (MDA, WASH, Education, BHW, Budget, Policy). Shared infra: Web Worker owns WASM instance, Float32Array render data via transferable ArrayBuffers (zero-copy), Zustand throttled to 5Hz, Canvas renderer with 4-tier LOD and per-species Path2D batching.

Tech Choices & Why

Rust to WASM

Tight simulation loop with thousands of creatures needs predictable performance. JS GC pauses would cause frame drops.

Polynomial tanh

Standard tanh differs between V8/SpiderMonkey/JSC. Polynomial approximation (max error ~0.004) uses only +,-,*,/ for bit-identical results.

Two-pass spatial hash

Zero allocation per tick. Count, prefix-sum, place. O(1) neighbor queries for vision cone checks.

Transferable ArrayBuffers

Zero-copy Worker to main thread. Flat Float32Array (12 floats/creature) pre-sorted by species for canvas batching.

Stochastic SIS model

Agent-based SIS with WHO EPG thresholds matches real-world helminth transmission dynamics. KAP behavioral model drives agent hygiene decisions.

Challenges & Solutions

Problem

Standard tanh produces different floating-point results across browser engines, breaking determinism.

Solution

Polynomial approximation: tanh(x) = x*(27+x^2)/(27+9x^2). Only +,-,*,/ operations. Verified: same seed produces identical positions after 1,000 ticks.

Problem

Brute-force O(n^2) neighbor checks for vision become too slow above 500 creatures.

Solution

Two-pass counting spatial hash. Pass 1: count per cell. Prefix-sum for offsets. Pass 2: place contiguously. Cell size = max vision range, queries check 3x3 cells. 50-100us for 1,000 creatures.

Problem

Putting Float32Array render buffers in React state triggers 60 re-renders/second.

Solution

Render data in useRef, never React state. Stats throttled to 5Hz. Canvas renders via requestAnimationFrame reading refs directly. Transferable ArrayBuffers for zero-copy transfer.

Problem

STH infection model needed to calibrate against real epidemiological data from Cebu City while supporting 6 intervention types and urban/rural comparison.

Solution

Agent-based stochastic SIS model with WHO EPG thresholds for infection intensity classification. KAP behavioral model drives agent hygiene decisions. Environmental contamination grid with decay and rain dynamics. 6 interventions (MDA, WASH, Education, BHW, Budget, Policy) each modify different transmission parameters. Urban vs rural comparison mode uses different population density, sanitation, and access parameters calibrated against thesis field data.

Trade-offs

  • ~Polynomial tanh trades ~0.004 accuracy for determinism. Acceptable for evolution, not for scientific simulation.
  • ~Flat spatial hash wastes memory on empty cells but avoids hash map overhead.
  • ~Web Worker isolation means all communication is async.
  • ~Canvas 2D instead of WebGL limits max creatures (~10K) but avoids shader complexity.
  • ~STH model uses stochastic SIS rather than deterministic SIR β€” better matches real-world helminth reinfection patterns.

Impact

Live at darwins-sandbox.vercel.app. Dual-purpose platform: evolution simulator and STH infection/deworming simulation calibrated against real thesis data from Cebu City. 500 agents at 60fps in <1ms per tick. Demonstrates Rust/WASM pipeline, agent-based modeling, epidemiological simulation, and performant browser rendering.

What I'd Improve

  • +Add speciation visualization (phylogenetic tree over time)
  • +Add creature inspector (click to see neural network weights)
  • +Add save/load simulation state for sharing evolutions
  • +Expand STH model with age-stratified infection dynamics

This project was reviewed by Luka (Principal Research Consultant) and Nala (UI/UX Compliance Auditor). They had no comments, which we interpret as approval.

You found the bottom! 🎯