I’ve already posted about NetLogo, but I want to return to it, because I’ve been very impressed with how ridiculously easy it is to construct sophisticated simulations of interesting models with it. My son Adam has been having a lot of fun with it, and he is able in the space of an hour or two, to create simulations of interesting models of his own devising, complete with easy-to-use GUI’s.
To illustrate how easy it is to build models, I’m going to use a familiar model that comes in the NetLogo library: the Ising Model of ferromagnetism on a square lattice, simulated using the Metropolis algorithm. (I simplified the code very slightly for clarity.)
In this model, there are spins at each node of a square lattice, that can point “up” or “down.” Spins like to have their neighbors point in the same direction. To compute the energy of a configuration of spins, we look at all pairs of neighboring spins, and add an energy of -1 if the two spins point in the same direction, and an energy of +1 if the two spins point in opposite directions. Boltzmann’s law tells us that each configuration should have a probability proportional to the exp(-Energy[configuration] / T), where T is the temperature.
The Metropolis algorithm is an algorithm for dynamically generating configurations of the model with the correct probability. One starts at some configuration, and picks a spin at random, and considers flipping it. If flipping it would reduce the energy or leave the energy unchanged, one makes the flip. Otherwise, one flips the spin anyways with a probability of exp(-Ediff/T), where Ediff is the amount that the energy will be increased by making the flip. Then pick another spin at random and continue.
It is not immediately obvious that the Metropolis algorithm generates configurations with probability given by Boltzmann’s Law, but one can prove that it does. The Metropolis algorithm is not the most efficient algorithm for generating samples from the correct probability distribution; much more efficient algorithms from that point of view are discussed in Werner Krauth’s book, which I previously reviewed.
There are many things to say about the Ising Model, but first let’s look at the NetLogo code for this model. It’s extremely short; the code (including the code controlling the GUI) is about the same length as the copyright notice that I’m appending because I only made small modifications to Uri Wilensky‘s code:
In a future post (Edit: it’s here), I’ll step through this code and show you how it works (you do also need to click on a few buttons to set up the GUI). For now, though, I want to show you what an applet running this code looks like, and make some more comments about the Ising model that are best illustrated by running the applet. So please (assuming you have Java installed; and if you’re on Mac OS X Leopard, use Safari, there’s some problem when using Firefox) CONTINUE HERE.