We are now ready to build the spatial model. It will consist of walkers moving in a 2D box.
We need the agents to live inside a box so that they don't disperse.
Make a ConfinedWalker2D
type. Its fields are a Walker2D
object and a box size, L
.
Extend move
to ConfinedWalker2D
. If the walker tries to jump outside the box, i.e. outside the sites 1 to $L$, in either direction, then it remains where it is.
Make a confined Agent
and calculate and draw its trajectory to make sure it stays inside the box.
For simplicity we will impose in our model that there is at most one agent on each site at all times (i.e. there is "hard-core exclusion"). This models the fact that two people cannot be in the same place at the same time.
initialize
that takes parameters 𝐿, the box length, and 𝑁, the number of agents.It builds, one by one, a Vector
of agents, by proposing a position for each one and checking if that position is already occupied. If it is occupied, it should generate another one, and so on until it finds a free spot.
All of the agents should have state S
, except for one infectious individual (I
).
To do this you should write a function `check_occupied` that checks if a particular position is occupied.
Write a function visualize_agents
that takes in a collection of agents as argument. It should plot a point for each agent, coloured according to its status.
Hint: You can use the keyword argument c=cs
inside your call to the plotting function to set the colours of points to a vector of integers called cs
. Don’t forget to use ratio=1
.
The dynamics has parameters $p_I$ and $p_R$, the probabilities of infection and recovery at each time step, respectively.
struct
SIRSimulation
containing fields L
, p_I
, p_R
and a Vector
agents
of Agent
s.Since we will have many Agent
s, stored in a Vector
, we do not want to recreate the whole simulation at each time step. Instead we will now modify (mutate) the data structure, so our functions will now have !
at the end of their names.
Nonetheless, we can still use an immutable struct
, since the only things we will modify are inside the Vector
. That is, we will never reassign the fields of the struct
itself.
Write a function step!
that does one step of the dynamics of the model. The rules are as follows:
Choose a single agent at random; call it $i$.
Propose a new position for that agent.
If that new position is not occupied, move agent $i$ there.
If the new position is occupied, by agent $j$, then neither of them move, but they interact as follows:
If agent $i$ is infected, it recovers with probability $p_R$.
You should write helper functions when necessary.
Use the function deepcopy
to copy the state of the whole system each time you store it.
hbox
or vbox
function (from Interact.jl
) to put them together horizontally or vertically into a single plot.