What is this book about?#

Have you ever wondered…

Each driver brakes a beat late, and that small delay ripples backward through the cars as a wave. The jam drifts backward while every single car is still trying to move forward. Nobody creates the jam; it emerges.
No ant is in charge and none can see the whole nest. Each follows local cues such as scent trails, and the colony's structure is the sum of thousands of tiny, independent choices. The order is built from the bottom up.
Each walker only steps aside to avoid the people directly ahead. Those small sidesteps add up across the crowd, and smooth lanes appear that no one planned or drew. You can watch exactly this happen further down the page.
One case seeds two, two seed four, and four seed eight. Local contacts compound day after day, so a handful of infections early in the week can reach a whole population by its end. Small local events scale into a global outcome.
Notice the pattern. In every case no one is in charge. The outcome emerges from many individuals following simple rules and reacting to those around them. That single idea is what this book is about.

Studying exactly this, how order that no one designed arises from many simple individuals interacting, is what agent-based modelling (ABM) is for. It is the big idea of this book.

In a nutshell

This book teaches you to build computer simulations in Python where many simple “agents” interact, and to watch complex, surprising, real-world behaviour emerge from the bottom up.

Watch emergence happen#

Imagine a busy station concourse at rush hour. Two crowds pour through the same corridor in opposite directions, and no one tells anyone where to walk. Each person follows a single, self-interested rule: head for the far side, and step aside to avoid bumping into whoever is in front. From nothing but that rule, the crowd sorts itself into smooth walking lanes, one flowing one way and the next flowing back. No sign, no planner, and no leader ever draws those lanes.

This is a genuine agent-based model, not a grid of cells switching on and off. Every walker is an autonomous agent with its own goal and its own limited view of the handful of people nearby. There is no global recipe for the pattern anywhere in the model. There are only individuals, each reacting to those immediately around them.

Nobody plans the lanes, and each walker senses only the few people closest to them. Press Run and watch the crowd organise itself:

A crowd finds its own lanes
Two crowds walk through the same corridor, blue heading right and orange heading left. Every walker follows one rule: aim for the far side, and step aside to avoid the people in front. Press Run, drag the Crowd slider, or grab any walker and pull them through the crowd to feel the others move around you.
lane order 0.00people 96
Start the two crowds mixed together. Watch them sort into alternating lanes that no one drew.

The two crowds start fully mixed, and each walker only avoids the people ahead. Within seconds they organise into alternating lanes that no one designed, and the lane-order reading climbs as the pattern sharpens. Grab any walker and pull them through the crowd to feel the others give way.

Nobody arranges those lanes; they emerge. No single walker can see the whole crowd, no one coordinates the flow, and you cannot find the pattern inside any one person. It lives only in the way the walkers interact. This gap between simple local behaviour and surprising collective order is exactly what agent-based modelling lets you build, measure, and explain. By the end of this book, writing a model like this one will feel routine.

Who is this book for?#

If that gap between what individuals intend and what the group produces intrigues you, this book is written for you: for students, researchers, and practitioners who want to understand complex systems by simulating them.

Are you ready for this book?
Tick each one that sounds like you.
✓I know a little Python: variables, loops, functions, and maybe a first idea of classes.
✓I have little or no modelling experience, and that is completely fine.
✓I am curious how individual behaviour scales up to collective outcomes.
Tick the boxes above to see where you stand.

What you will learn#

The book takes you from why we model at all to building your own ABMs with Mesa, the leading Python framework for agent-based modelling. The sequence is deliberate: first you learn how to frame and judge a model, then how agents, environments, time, and interactions fit together, and only then how to implement and investigate those ideas in code. By the end, you should be able not only to run an ABM, but to explain its assumptions, analyse the behaviour it produces, and judge how far its conclusions can be trusted.

Chapter 1 · Modelling and simulation

What models are, why we build them, the modelling cycle, and what separates a good model from a misleading one.

Chapter 1 - Modelling and simulation
Chapter 2 · Fundamentals of ABM

The core ingredients of every agent-based model (agents, environment, time, interactions, and the model that ties them together), and when ABM beats other modelling approaches.

Chapter 2 - Fundamentals of agent-based modelling
Chapter 3 · Your first Mesa model

A hands-on tutorial where you build a complete ABM, step by step, about households coordinating their washing.

Chapter 3 - Your first Mesa model
Chapter 4 · Classic models, rebuilt by you

Simple economies, the Game of Life, traffic flow, a virus on a network, and event scheduling: a first look at how ABM can also drive discrete-event simulation. Each model comes with exercises.

Chapter 4 - Examples of ABMs built with Mesa
Chapter 5 · Towards more complex models

Bringing real data into your models, and good practices for serious ABM work.

Chapter 5 - Towards empirical ABMs of complex systems

How this book works#

This is a do-first book. The concept chapters (1, 2 and 5) pair short explanations with interactive activities you complete on the page, and the hands-on chapters (3 and 4) give you runnable code and exercises. You run everything in the browser: see How to use this book for the two-minute tour of the rocket and the Export/Import/Reset buttons that keep your progress safe.

Ready? Then off to How to use this book.