{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# 5. Spatial SIR model" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We are now ready to build the spatial model. It will consist of walkers moving in a 2D box." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Confinement inside a box" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "We need the agents to live inside a box so that they don't disperse." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise\n", "\n", "1. Make a `ConfinedWalker2D` type. Its fields are a `Walker2D` object and a box size, `L`. \n", "\n", "\n", "2. 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.\n", "\n", "\n", "3. Make a confined `Agent` and calculate and draw its trajectory to make sure it stays inside the box." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Initialization" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "1. Write a function `initialize` that takes parameters ๐ฟ, the box length, and ๐‘, the number of agents.\n", "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.\n", "All of the agents should have state `S`, except for one infectious individual (`I`).\n", "\n", " To do this you should write a function `check_occupied` that checks if a particular position is occupied." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. 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.\n", "\n", " 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`." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Run these functions to visualize the initial condition." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Dynamics" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "The dynamics has parameters $p_I$ and $p_R$, the probabilities of infection and recovery at each time step, respectively.\n" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "#### Exercise\n", "\n", "1. Make an immutable `struct` `SIRSimulation` containing fields `L`, `p_I`, `p_R` and a `Vector` `agents` of `Agent`s. " ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "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.\n", "\n", "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." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "2. Write a function `step!` that does one step of the dynamics of the model. The rules are as follows:\n", "\n", " 1. Choose a single agent at random; call it $i$.\n", " \n", " 2. Propose a new position for that agent.\n", "\n", " 3. If that new position is not occupied, move agent $i$ there.\n", "\n", " 4. If the new position *is* occupied, by agent $j$, then *neither* of them move, but they interact as follows:\n", "\n", " - If agent $i$ is infected and agent $j$ is susceptible then agent $j$ gets infected with probability $p_I$.\n", "\n", " 5. If agent $i$ is infected, it recovers with probability $p_R$.\n", " \n", " You should write helper functions when necessary." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "3. Make an interactive visualization to display the agents after each step, to check visually that the implementation is correct. You will need to *pre-compute* the whole evolution of the system before visualizing it.\n", "Use the function `deepcopy` to copy the state of the whole system each time you store it." ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "4. Add to your visualization the history of $S$, $I$ and $R$ from time $0$ up to time $n$. To do this, make two separate plot objects $p_1$ and $p_2$ and use the `hbox` or `vbox` function (from `Interact.jl`) to put them together horizontally or vertically into a single plot." ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ " " ] } ], "metadata": { "@webio": { "lastCommId": null, "lastKernelId": null }, "kernelspec": { "display_name": "Julia 1.4.2", "language": "julia", "name": "julia-1.4" }, "language_info": { "file_extension": ".jl", "mimetype": "application/julia", "name": "julia", "version": "1.4.2" } }, "nbformat": 4, "nbformat_minor": 4 }