More on the topic…
The author took a weakly electric fish simulation from a published biology paper and optimized it to run 250 times faster—from 4,300 steps per second to 1.85 million. The original work used reinforcement learning to train fish policies that naturally develop complex behaviors like foraging and biting without explicit programming, relying instead on biophysical constraints and reward signals. The author's goal was to explore how techniques from autonomous driving (fast simulation + simple on-policy RL) could apply to biology, where environment modeling is messier and experimental data collection is slow.
The optimization work came down to practical systems engineering. Switching from double-precision to single-precision floats cut memory overhead and improved speed by 30%. The real bottleneck was computing electric field interactions—each fish has 72 sensory organs, and the naive approach calculated every fish-to-fish interaction. The breakthrough was using the paper's own data: they noted that different receptor types have different sensing ranges (4–10 cm for some, 100 cm for others). By only computing field measurements between fish within relevant distances, the author eliminated massive numbers of unnecessary calculations. These changes let them train a working fish policy in under 10 seconds on a single GPU.
The experiments used the original paper's setup with minor tweaks: randomized arena sizes, variable food distributions, and fish with different body sizes that affect their speed. The reward function pushes fish to eat food and bite competitors, with cooldown periods to prevent exploitation. The simulation is compact—just 1,200 lines of C code—and the implementation is open-sourced in PufferLib. The whole exercise demonstrates that even in biology, where the physics is complex, you can still apply the core principle that matters: make your simulation fast enough to iterate quickly.
Questions about this article
No questions yet.