|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "markdown", |
| 5 | + "id": "f2147e34-ce39-4d8c-a7db-e8acec2b63e0", |
| 6 | + "metadata": {}, |
| 7 | + "source": [ |
| 8 | + "# Generator exercise\n", |
| 9 | + "\n", |
| 10 | + "To see the solutions please see the [generator exercise solutions notebook](./03b_exercise1_solutions.ipynb)\n" |
| 11 | + ] |
| 12 | + }, |
| 13 | + { |
| 14 | + "cell_type": "markdown", |
| 15 | + "id": "c034caf3-6766-4c69-af8f-949f45283b37", |
| 16 | + "metadata": {}, |
| 17 | + "source": [ |
| 18 | + "## Imports" |
| 19 | + ] |
| 20 | + }, |
| 21 | + { |
| 22 | + "cell_type": "code", |
| 23 | + "execution_count": null, |
| 24 | + "id": "85fd4a85-bb77-498c-96ee-c14de89994a2", |
| 25 | + "metadata": {}, |
| 26 | + "outputs": [], |
| 27 | + "source": [ |
| 28 | + "import simpy\n", |
| 29 | + "import numpy as np" |
| 30 | + ] |
| 31 | + }, |
| 32 | + { |
| 33 | + "cell_type": "markdown", |
| 34 | + "id": "bfab7f97-9cad-419a-8d75-a2ba190edee8", |
| 35 | + "metadata": {}, |
| 36 | + "source": [ |
| 37 | + "## Example code\n", |
| 38 | + "\n", |
| 39 | + "The code below is taken from the simple pharmacy example. In this code arrivals occur with an IAT of exactly 5 minutes." |
| 40 | + ] |
| 41 | + }, |
| 42 | + { |
| 43 | + "cell_type": "code", |
| 44 | + "execution_count": null, |
| 45 | + "id": "ee2439f2-0d35-41fd-a5e2-4b95954dd5c5", |
| 46 | + "metadata": {}, |
| 47 | + "outputs": [], |
| 48 | + "source": [ |
| 49 | + "def prescription_arrival_generator(env):\n", |
| 50 | + " '''\n", |
| 51 | + " Prescriptions arrive with a fixed duration of\n", |
| 52 | + " 5 minutes.\n", |
| 53 | + "\n", |
| 54 | + " Parameters:\n", |
| 55 | + " ------\n", |
| 56 | + " env: simpy.Environment\n", |
| 57 | + " '''\n", |
| 58 | + " \n", |
| 59 | + " # don't worry about the infinite while loop, simpy will\n", |
| 60 | + " # exit at the correct time.\n", |
| 61 | + " while True:\n", |
| 62 | + " \n", |
| 63 | + " # sample an inter-arrival time.\n", |
| 64 | + " inter_arrival_time = 5.0\n", |
| 65 | + " \n", |
| 66 | + " # we use the yield keyword instead of return\n", |
| 67 | + " yield env.timeout(inter_arrival_time)\n", |
| 68 | + " \n", |
| 69 | + " # print out the time of the arrival\n", |
| 70 | + " print(f'Prescription arrives at: {env.now}')" |
| 71 | + ] |
| 72 | + }, |
| 73 | + { |
| 74 | + "cell_type": "code", |
| 75 | + "execution_count": null, |
| 76 | + "id": "40c495d5-6f55-4c93-99e3-5bfa6cdff36d", |
| 77 | + "metadata": {}, |
| 78 | + "outputs": [], |
| 79 | + "source": [ |
| 80 | + "# model parameters\n", |
| 81 | + "RUN_LENGTH = 25\n", |
| 82 | + "\n", |
| 83 | + "# create the simpy environment object\n", |
| 84 | + "env = simpy.Environment()\n", |
| 85 | + "\n", |
| 86 | + "# tell simpy that the `prescription_arrival_generator` is a process\n", |
| 87 | + "env.process(prescription_arrival_generator(env))\n", |
| 88 | + "\n", |
| 89 | + "# run the simulation model\n", |
| 90 | + "env.run(until=RUN_LENGTH)\n", |
| 91 | + "print(f'end of run. simulation clock time = {env.now}')" |
| 92 | + ] |
| 93 | + }, |
| 94 | + { |
| 95 | + "cell_type": "markdown", |
| 96 | + "id": "6b1bd501-1614-4891-a876-d07bd40c0496", |
| 97 | + "metadata": {}, |
| 98 | + "source": [ |
| 99 | + "### Exercise: modelling a poisson arrival process for prescriptions\n", |
| 100 | + "\n", |
| 101 | + "**Task:**\n", |
| 102 | + "\n", |
| 103 | + "* Update `prescription_arrival_generator()` so that inter-arrival times follow an exponential distribution with a mean of 5.0 minutes between arrivals.\n", |
| 104 | + "* Use a run length of 25 minutes.\n", |
| 105 | + "\n", |
| 106 | + "> **Bonus**: try this initially **without** setting a random seed. Then update the method choosing an approach to control random sampling.\n", |
| 107 | + "\n", |
| 108 | + "**Hints:**\n", |
| 109 | + "\n", |
| 110 | + "We learnt how to sample using a `numpy` random number generator in the [sampling notebook](./01_sampling.ipynb). The basic form to draw a single sample followed this pattern (note this excludes a random seed).\n", |
| 111 | + "\n", |
| 112 | + "```python\n", |
| 113 | + "rng = np.random.default_rng()\n", |
| 114 | + "sample = rng.exponential(scale=12.0)\n", |
| 115 | + "```" |
| 116 | + ] |
| 117 | + }, |
| 118 | + { |
| 119 | + "cell_type": "code", |
| 120 | + "execution_count": null, |
| 121 | + "id": "65823eff-8d0f-4eaf-a531-173c8ab6290c", |
| 122 | + "metadata": {}, |
| 123 | + "outputs": [], |
| 124 | + "source": [ |
| 125 | + "# your code here." |
| 126 | + ] |
| 127 | + } |
| 128 | + ], |
| 129 | + "metadata": { |
| 130 | + "kernelspec": { |
| 131 | + "display_name": "Python 3 (ipykernel)", |
| 132 | + "language": "python", |
| 133 | + "name": "python3" |
| 134 | + }, |
| 135 | + "language_info": { |
| 136 | + "codemirror_mode": { |
| 137 | + "name": "ipython", |
| 138 | + "version": 3 |
| 139 | + }, |
| 140 | + "file_extension": ".py", |
| 141 | + "mimetype": "text/x-python", |
| 142 | + "name": "python", |
| 143 | + "nbconvert_exporter": "python", |
| 144 | + "pygments_lexer": "ipython3", |
| 145 | + "version": "3.11.9" |
| 146 | + } |
| 147 | + }, |
| 148 | + "nbformat": 4, |
| 149 | + "nbformat_minor": 5 |
| 150 | +} |
0 commit comments