This site uses cookies. To see our cookie policy and how to control cookies click hereCookies

Using agent-based simulation

Traditionally modelling and simulation has been carried out using differential equations and discrete event simulations, and with good cause as they are very effective methods. Another approach, which has become more popular as computing power grows, is to use an agent-based method, where agents represent individuals or groups of individuals and are allowed to move and react to circumstances. This allows for the capture of complex behaviour such as, for example, details of population movement and allows for reactive behaviour to changes in circumstances.The simulation below is an agent-based representation of a virus epidemic. It considers 25 population centres, each of which is considered "well-mixed", i.e. once an infection takes hold it spreads throughout the population centre. Additionally, people can travel between adjacent population centres, but with a frequency that is much lower than travel within a population centre. In this simplified simulation travel is only possible to adjacent centres along the black lines.

Each population centre is simulated using 10,000 agents and initially 0.1% of one of the centres if infected with the virus. The transmission and recovery characteristics are approximately those of the COVID 19 virus and each agent is characterised as being in one of three groups; the S group - those that are

In the above \(\alpha\) is a parameter that describes how infectious the disease is, while \(\beta\) is a parameter that describes how long people are infectious for after they have become infected (they are assumed to be infectious until they recover or they die).

Pseudo-code for an algorithm to perform the basic simulation model described is given below.

// U is a uniform random deviate on the interval (0,1]

For each day:

\(\quad\)For each agent A:

\(\quad \quad\) if A \(\in\) I and U \(\leqslant\) \(\beta\):

\(\quad \quad \quad\) A: I \(\rightarrow\) R

\(\quad \quad\) else if A \(\in\) S:

\(\quad \quad \quad\) B \(\leftarrow \) random agent from population

\(\quad \quad \quad\) if B \(\in\) I and U \(\leqslant\) \(\alpha\):

\(\quad \quad \quad \quad\) A: S \(\rightarrow\) I

For each day:

\(\quad\)For each agent A:

\(\quad \quad\) if A \(\in\) I and U \(\leqslant\) \(\beta\):

\(\quad \quad \quad\) A: I \(\rightarrow\) R

\(\quad \quad\) else if A \(\in\) S:

\(\quad \quad \quad\) B \(\leftarrow \) random agent from population

\(\quad \quad \quad\) if B \(\in\) I and U \(\leqslant\) \(\alpha\):

\(\quad \quad \quad \quad\) A: S \(\rightarrow\) I

There are some gotcha's to consider when using agent-based simulation.

- If the number of agents is small there could be significant variations in results from one simulation run to another and many simulation runs should be made.
- You are at the mercy of random numbers. If you have a small number of agents and only one is initially in the infected group then, depending on the value of \(\beta\), it is possible that many cases will just see the epidemic die out.
- Get a good random number generator!

To allow for travel between population centres, the probabily of a susceptible person coming into contact with an infected person from the same population centre \(i\), or from a different population centre \(j\) can be given as: $$ P(S \rightarrow I)^i = \begin{cases} \alpha P(I)^j, & \text{if}\ U \leqslant \gamma \\ \\ \alpha P(I)^i, & \text{otherwise} \\ \end{cases} $$ where U is a uniform random deviate on the interval (0,1] and \(\gamma\) is the probabilty that a person will meet a person from a connected population centre. The pseudo-code above can be adapted to allow for this as follows.

// U is a uniform random deviate on the interval (0,1]

For each day:

\(\quad\)For each population centre \(i\):

\(\quad\)\(\quad\)For each agent in \(i\), A\(^i\):

\(\quad\)\(\quad \quad\)if A\(^i\) \(\in\) I and U \(\leqslant\) \(\beta\):

\(\quad\)\(\quad \quad \quad\)A\(^i\): I \(\rightarrow\) R

\(\quad\)\(\quad \quad\)else if A\(^i\) \(\in\) S:

\(\quad\)\(\quad \quad \quad\)if U \(\leqslant\) \(\gamma\):

\(\quad\)\(\quad \quad \quad \quad\)j \(\leftarrow \) random connected population centre

\(\quad\)\(\quad \quad \quad \quad\)B \(\leftarrow \) random agent from \(j\)

\(\quad\)\(\quad \quad \quad\)else:

\(\quad\)\(\quad \quad \quad \quad\)B \(\leftarrow \) random agent from population centre \(i\)

\(\quad\)\(\quad \quad \quad\)if B \(\in\) I and U \(\leqslant\) \(\alpha\):

\(\quad\)\(\quad \quad \quad \quad\) A\(^i\): S \(\rightarrow\) I

For each day:

\(\quad\)For each population centre \(i\):

\(\quad\)\(\quad\)For each agent in \(i\), A\(^i\):

\(\quad\)\(\quad \quad\)if A\(^i\) \(\in\) I and U \(\leqslant\) \(\beta\):

\(\quad\)\(\quad \quad \quad\)A\(^i\): I \(\rightarrow\) R

\(\quad\)\(\quad \quad\)else if A\(^i\) \(\in\) S:

\(\quad\)\(\quad \quad \quad\)if U \(\leqslant\) \(\gamma\):

\(\quad\)\(\quad \quad \quad \quad\)j \(\leftarrow \) random connected population centre

\(\quad\)\(\quad \quad \quad \quad\)B \(\leftarrow \) random agent from \(j\)

\(\quad\)\(\quad \quad \quad\)else:

\(\quad\)\(\quad \quad \quad \quad\)B \(\leftarrow \) random agent from population centre \(i\)

\(\quad\)\(\quad \quad \quad\)if B \(\in\) I and U \(\leqslant\) \(\alpha\):

\(\quad\)\(\quad \quad \quad \quad\) A\(^i\): S \(\rightarrow\) I

In the simulation below, select whether there is high or low contact spread between the population centres, then press the start button. Each population centre will follow its own agent-based simulation with occasional population mixing between horizontally and vertically adjacent population centres.

Note, in the simulation below the results are pre-calculated rather than generated in real time. This is because agent-based simulations are typically computationally intensive and generating results at the frame rates they are viewed on this page is not feasible.

When the simulation runs notice how each population centre follows its own epidemic curve, each of which is broadly similar to the results of the

In this simulation it is assumed that population centres are connected by a simple network, and that within each population centre the agents are well mixed; i.e. any agent can meet any other agent with equal probability. This was set up to simplfy the problem and to provide an equal modelling footing to the