Chemotaxis
Here’s a little bacteria simulation I’ve built in ClojureScript. I took a lazy approach and instead of starting from scratch I modified Julian Gamble’s ClojureScript port of Rich Hickey’s ants colony demo.
One common behaviour of bacteria is movement in response to chemical stimuli (chemotaxis). Wikipedia lists many types of taxis or movement towards and away from stimuli such as phototaxis (stimulation by light) or thermotaxis (stimulation by heat), but I decided to simulate chemotaxis only.
The organisms in question obviously cannot acquire and process a lot of information about the environment. Some bacteria (e.g. E. coli) observe the concentration of chemicals as they move and continue moving forward if the concentration of attracting chemicals increases (or repelling ones decreases). Otherwise they change direction randomly by flipping over.
The simulation is probably not very realistic because of scale, speed etc. and it lacks negative stimuli (poison), but it nicely demonstrates the effectiveness of the very simple movement strategy.
Click the image below to see a working demo:
Starting point: ants colony in ClojureScript
Rich Hickey’s ants colony simulation is a demo of Clojure’s concurrency features from more than 10 years ago. I decided to use a ClojureScript port to have something runnable in a browser. The ported version I found on github differs a lot from the original. In particular:
-
CLJS has no agents or refs. Concurrency in the CLJS version is introduced with Communicating Sequential Processes (CSP).
-
For performance reasons the CLJS version makes heavy use of JavaScript mutable arrays in place of Clojure persistent data structures. This makes the code far from idiomatic. I believe it was Lau Jensen’s blog post that inspired the array-based solution.
CSP on the front end is a nice change from typical callbacks-heavy JavaScript code. E.g. instead of calling setTimeout you can read from timeout channels. Fun fact: ClojureScript’s core.async library has been ported to JavaScript so you can use CSP channels with ES6 generators.
Chemotaxis-cljs:
I’ve cleaned the code, added figwheel and replaced all ant-specific things with bacteria and chemotaxis. Changes include closing the world borders for more realism (the world is no longer a torus).
The main part is in this function:
It’s amazing how such a simple strategy can work so well (see the demo above).