Archive for the ‘clustering’ tag
Clumping and clustering
I've had a fun weekend playing with Processing and improving the clumping visualisation I posted earlier this week. The new version has been uploaded to OpenProcessing.org, a site which contains over 1000 visualisations, many of them containing beautiful and interesting mathematics:
A brief reminder of the situation: We have a grid of squares, of several different types. These squares are happy when they are surrounded by enough squares of the same type as them, and unhappy otherwise. We take two unhappy squares, swap them over, and repeat until bored!
There are two key parameters here: the number of different types of cell/person/house/whatever these squares are representing, and the happiness threshold: how many neighbours need to be the same as a cell for it to be happy.
Let's look at the types of behaviour that emerge for different values of the parameters:
A behaviour catalogue
| Neighbours needed for happiness | ||||||
|---|---|---|---|---|---|---|
| 2 | 3 | 4 | 5 | 6 | ||
| Types | 2 | ![]() |
|
|
|
|
| 3 | |
|
|
|
|
|
| 4 | |
|
|
|
|
|
| 5 | |
|
|
|
|
|
| 6 | |
|
|
|
|
|
You find that it takes longer and longer for the state to settle down as you increase the number of types of cell. With 6 cells, and a happiness threshold of 4, for example, you can see from the table that it does eventually settle into a fairly stable clustered pattern, but it takes a very long time. After 50,000 swaps the grid looked like this:

Apart from the special case of two types, with happiness threshold five or six, the cells always seem to settle into a relatively small small clustered pattern (with small threshold), or to not stabilise at all (large threshold).
2 types; threshold 5: this is the case explored in the earlier post on this clustering behaviour. The cells cluster together fairly quickly, and then over the next 20,000 swaps or so the clusters join together into larger features, with the borders being long stretches of horizontal/vertical/diagonal lines.
2 types; threshold 6: this is perhaps even more interesting then the previous case. We do get some clustering, but the clusters don't hold together, but fray, and move around.
As with the previous post on this topic, I have wrapped left-right and top-bottom. This doesn't change the behaviour significantly. If you'd like to explore what happens without this wrapping, then go get the source code and dive in!
Emergent Behaviour.
This 'clumping' behaviour is just one of many examples of emergent behaviour, where small-scale local actions add up to a sometimes surprising global behaviour. Sometimes, as the old aphorism says, the whole is more than the sum of its parts.
One of the best known examples of this emergent behaviour is flocking, where groups of birds fly together in flocks that seem to require some collective intelligence, but are actually generated from purely local actions. This behaviour was modelled in Boids, a classic Artificial Life simulation from 1986. Here's an excellent 3D Boids simulation.
Clustering and seating plans
For the last few days, I've been diving into Processing -- basically an integrated Java wrapper which makes it easy to create simple graphical visualisations (see here for some beautiful examples). Coming from Python, I particularly like not being forced not to write the 500 pages of OO wrapping that turned me off Java in the first place a few years ago.
My first Processing visualisation demonstrates how seating plans degrade over time!
From mixed to clumped
Imagine you have a square array of seats, and a group of boys and girls sit in them, alternately boy then girl. If we highlight one of the sexes we will get a checkerboard pattern.
Now imagine that boys like to have more boys than girls around them (and vice versa for girls). To try and make everyone happy, we implement the following scheme:
- Identify at random two people who aren't happy with their seat.
- Swap them.
- Repeat until everyone's happy.
If we repeat this procedure thousands of times, what will happen to the layout? Click below to find out:
Clicking again will restart the procedure.
You've probably noticed that I've wrapped left-right and top-bottom... which does move it away a little from the classroom context! This was basically to make the calculations easier.
Here are some examples of what happens if you let this run for a while:
Each frame of these animations represents 2000 swaps, and each animation covers 50000. You can see that there are similar phases in each one: a quick 'clumping', and then a much slower phase where the boundaries settle down into long straight/diagonal lines.
Notes and Extensions
There are many ways to push this visualisation further -- we could change the number of types (English, Welsh, Scottish?); change the 'happiness threshold' (which is currently 5 out of the 8 neighbours); perhaps even have different thresholds for each group. You find really interesting behaviour as you change the threshold -- too high, for example, and the pattern doesn't settle down because there are too many unhappy people.
If you'd like to use my visualisation as a starting point to explore these extensions, then the source is here: clump2.pde. Have fun!



