Dark theme

Cellular Automata


As you can see, the core code for a CA is very simple. Other than adding in better boundary condition rules, here are some things you could do to improve the model.


Add a stopping condition

Our model currently runs for more iterations than need be. We could add a stopping condition and print out the number of iterations it takes to end. To do this, at the end of the iteration code, we could check whether all the cells are zero or not:

environment = results
print_environment()
total = 0
for h in range(1, height - 1):
    for w in range(1, width - 1):
        total += environment[h][w]
if (total == 0):
    print("ends at iteration ", step)
    break


Adding realism

We could import real data, making the fuel_amount value encode a wider range of realistic conditions, such as firebreaks and different ages and types of tree. One subtle thing to think about with real data is what the time iteration should represent. We may need to calibrate our model, looking at real fires and seeing how fast they spread, adjusting the meaning of the iteration time (so, for example, we could change an iteration from meaning 1min of time, to 5mins of time) and the amount of fuel to gain realistic spreading speeds.

We could also add a wind direction and speed to our model. Direction is a matter of adding a probability to each fire decision:

if (environment[h-1][w-1] < fuel_amount) & (random.random() < probability_spread_from_NW): fire = True

Wind speed is tricker as we can't easily process more than one cell without a considerable re-write. A simple way of doing it is to adjust the relationship between fuel, directionalal probabilities, and iteration time meaning – if an iteration goes from being 2 minutes to being 1 minute, the model will represent fire spreading twice as fast.


Improving the display

At the moment, our model has a text display. This is fine, and does mean we can redirect the results to a text file at the command line:

python ca.py >> output.txt

However, it could look nicer for visual analysis, and we could either a) add command line arguments or b) a Graphical User Interface. Towards the end of the core course we saw how to display a model using matplotlib, including displaying a running model and a running model in a GUI. We could do the same thing here.

If you've got some time left in the practical, give some of these things a go. There's no need to upload this practical to GitHub for Assessment 1, though you're welcome to upload it if you like.


  1. Introduction
  2. Building the environment
  3. Iterating
  4. This page