The differential biology reader

 
« Back to blog

Clojure for individual-based modeling

I have been toying with 

Clojure, a modern Lisp dialect that runs on the Java Virtual Machine. It has been almost 10 years since I've worked properly with Lisp-style functional programming (in my case, Scheme). As all of my programming ventures these days center around R and Ruby (with the occasional frightening encounters with my first programming language, C), it is refreshing to get back into real functional programming.

I am examining Clojure as a language for individual-based modeling. The big daddy of these methods is Swarm, which is written in Objective-C. As a language, Clojure provides a number of features that are particularly suitable for individual- or agent-based simulation.

The first of these is the iterative nature of REPL (the Read-Evaluate-Print Loop) which makes it possible to interact with and reprogram your system on the fly. You can examine and modify your code while it is running. Compare this with the write-compile-run-debug-compile-run cycle of a language like Objective-C.

Second, functional languages in general are suited to the task of modeling. When building models, your focus should be on small units of input–output that build upon each other.

Yet Clojure offers many advantages over its dialectical ancestors. The first is immutable data structures. This forces you to think about everything as input and output (good for modeling) while making it easier to think and reason about (good for understanding your model) your program. The differentiation that Clojure makes between identity and state is also a good fit for modeling. Clojure is also built for a concurrent world. Clojure can handle a lot of different parts of a program simultaneously reading and writing changes to the world, allowing you to focus on each part of your system as individuals rather than worrying about the mechanics of making them all work together. Clojure also keeps vectors and hash tables as built-in, both convenient data structures for modeling tasks.

There are a number of other features in Clojure that I don't completely understand but look promising, like watchers for exploring and reporting program state.

The primary disadvantage is having to start from scratch. With mature systems like Swarm is that lot of the hard thinking has been done for you. Raw ingredients provide the tastiest environment for bugs. Another problem with Clojure is that its specification is still under development; it is a programming language in flux.

But…the demo program for concurrency in Clojure is an ants simulation. That has to be a good sign for future individual-based modelers.

photo cc-by gwen 

Comments (0)

Leave a comment...