-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.tex
15811 lines (12664 loc) · 846 KB
/
index.tex
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
% Options for packages loaded elsewhere
\PassOptionsToPackage{unicode}{hyperref}
\PassOptionsToPackage{hyphens}{url}
\PassOptionsToPackage{dvipsnames,svgnames,x11names}{xcolor}
%
\documentclass[
letterpaper,
DIV=11,
numbers=noendperiod]{scrreprt}
\usepackage{amsmath,amssymb}
\usepackage{iftex}
\ifPDFTeX
\usepackage[T1]{fontenc}
\usepackage[utf8]{inputenc}
\usepackage{textcomp} % provide euro and other symbols
\else % if luatex or xetex
\usepackage{unicode-math}
\defaultfontfeatures{Scale=MatchLowercase}
\defaultfontfeatures[\rmfamily]{Ligatures=TeX,Scale=1}
\fi
\usepackage{lmodern}
\ifPDFTeX\else
% xetex/luatex font selection
\fi
% Use upquote if available, for straight quotes in verbatim environments
\IfFileExists{upquote.sty}{\usepackage{upquote}}{}
\IfFileExists{microtype.sty}{% use microtype if available
\usepackage[]{microtype}
\UseMicrotypeSet[protrusion]{basicmath} % disable protrusion for tt fonts
}{}
\makeatletter
\@ifundefined{KOMAClassName}{% if non-KOMA class
\IfFileExists{parskip.sty}{%
\usepackage{parskip}
}{% else
\setlength{\parindent}{0pt}
\setlength{\parskip}{6pt plus 2pt minus 1pt}}
}{% if KOMA class
\KOMAoptions{parskip=half}}
\makeatother
\usepackage{xcolor}
\usepackage{svg}
\setlength{\emergencystretch}{3em} % prevent overfull lines
\setcounter{secnumdepth}{5}
% Make \paragraph and \subparagraph free-standing
\ifx\paragraph\undefined\else
\let\oldparagraph\paragraph
\renewcommand{\paragraph}[1]{\oldparagraph{#1}\mbox{}}
\fi
\ifx\subparagraph\undefined\else
\let\oldsubparagraph\subparagraph
\renewcommand{\subparagraph}[1]{\oldsubparagraph{#1}\mbox{}}
\fi
\usepackage{color}
\usepackage{fancyvrb}
\newcommand{\VerbBar}{|}
\newcommand{\VERB}{\Verb[commandchars=\\\{\}]}
\DefineVerbatimEnvironment{Highlighting}{Verbatim}{commandchars=\\\{\}}
% Add ',fontsize=\small' for more characters per line
\newenvironment{Shaded}{}{}
\newcommand{\AlertTok}[1]{\textcolor[rgb]{1.00,0.33,0.33}{\textbf{#1}}}
\newcommand{\AnnotationTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\AttributeTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\BaseNTok}[1]{\textcolor[rgb]{0.00,0.36,0.77}{#1}}
\newcommand{\BuiltInTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\CharTok}[1]{\textcolor[rgb]{0.01,0.18,0.38}{#1}}
\newcommand{\CommentTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\CommentVarTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\ConstantTok}[1]{\textcolor[rgb]{0.00,0.36,0.77}{#1}}
\newcommand{\ControlFlowTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\DataTypeTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\DecValTok}[1]{\textcolor[rgb]{0.00,0.36,0.77}{#1}}
\newcommand{\DocumentationTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\ErrorTok}[1]{\textcolor[rgb]{1.00,0.33,0.33}{\underline{#1}}}
\newcommand{\ExtensionTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{\textbf{#1}}}
\newcommand{\FloatTok}[1]{\textcolor[rgb]{0.00,0.36,0.77}{#1}}
\newcommand{\FunctionTok}[1]{\textcolor[rgb]{0.44,0.26,0.76}{#1}}
\newcommand{\ImportTok}[1]{\textcolor[rgb]{0.01,0.18,0.38}{#1}}
\newcommand{\InformationTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\KeywordTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\NormalTok}[1]{\textcolor[rgb]{0.14,0.16,0.18}{#1}}
\newcommand{\OperatorTok}[1]{\textcolor[rgb]{0.14,0.16,0.18}{#1}}
\newcommand{\OtherTok}[1]{\textcolor[rgb]{0.44,0.26,0.76}{#1}}
\newcommand{\PreprocessorTok}[1]{\textcolor[rgb]{0.84,0.23,0.29}{#1}}
\newcommand{\RegionMarkerTok}[1]{\textcolor[rgb]{0.42,0.45,0.49}{#1}}
\newcommand{\SpecialCharTok}[1]{\textcolor[rgb]{0.00,0.36,0.77}{#1}}
\newcommand{\SpecialStringTok}[1]{\textcolor[rgb]{0.01,0.18,0.38}{#1}}
\newcommand{\StringTok}[1]{\textcolor[rgb]{0.01,0.18,0.38}{#1}}
\newcommand{\VariableTok}[1]{\textcolor[rgb]{0.89,0.38,0.04}{#1}}
\newcommand{\VerbatimStringTok}[1]{\textcolor[rgb]{0.01,0.18,0.38}{#1}}
\newcommand{\WarningTok}[1]{\textcolor[rgb]{1.00,0.33,0.33}{#1}}
\providecommand{\tightlist}{%
\setlength{\itemsep}{0pt}\setlength{\parskip}{0pt}}\usepackage{longtable,booktabs,array}
\usepackage{multirow}
\usepackage{calc} % for calculating minipage widths
% Correct order of tables after \paragraph or \subparagraph
\usepackage{etoolbox}
\makeatletter
\patchcmd\longtable{\par}{\if@noskipsec\mbox{}\fi\par}{}{}
\makeatother
% Allow footnotes in longtable head/foot
\IfFileExists{footnotehyper.sty}{\usepackage{footnotehyper}}{\usepackage{footnote}}
\makesavenoteenv{longtable}
\usepackage{graphicx}
\makeatletter
\def\maxwidth{\ifdim\Gin@nat@width>\linewidth\linewidth\else\Gin@nat@width\fi}
\def\maxheight{\ifdim\Gin@nat@height>\textheight\textheight\else\Gin@nat@height\fi}
\makeatother
% Scale images if necessary, so that they will not overflow the page
% margins by default, and it is still possible to overwrite the defaults
% using explicit options in \includegraphics[width, height, ...]{}
\setkeys{Gin}{width=\maxwidth,height=\maxheight,keepaspectratio}
% Set default figure placement to htbp
\makeatletter
\def\fps@figure{htbp}
\makeatother
% definitions for citeproc citations
\NewDocumentCommand\citeproctext{}{}
\NewDocumentCommand\citeproc{mm}{%
\begingroup\def\citeproctext{#2}\cite{#1}\endgroup}
\makeatletter
% allow citations to break across lines
\let\@cite@ofmt\@firstofone
% avoid brackets around text for \cite:
\def\@biblabel#1{}
\def\@cite#1#2{{#1\if@tempswa , #2\fi}}
\makeatother
\newlength{\cslhangindent}
\setlength{\cslhangindent}{1.5em}
\newlength{\csllabelwidth}
\setlength{\csllabelwidth}{3em}
\newenvironment{CSLReferences}[2] % #1 hanging-indent, #2 entry-spacing
{\begin{list}{}{%
\setlength{\itemindent}{0pt}
\setlength{\leftmargin}{0pt}
\setlength{\parsep}{0pt}
% turn on hanging indent if param 1 is 1
\ifodd #1
\setlength{\leftmargin}{\cslhangindent}
\setlength{\itemindent}{-1\cslhangindent}
\fi
% set entry spacing
\setlength{\itemsep}{#2\baselineskip}}}
{\end{list}}
\usepackage{calc}
\newcommand{\CSLBlock}[1]{\hfill\break\parbox[t]{\linewidth}{\strut\ignorespaces#1\strut}}
\newcommand{\CSLLeftMargin}[1]{\parbox[t]{\csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLRightInline}[1]{\parbox[t]{\linewidth - \csllabelwidth}{\strut#1\strut}}
\newcommand{\CSLIndent}[1]{\hspace{\cslhangindent}#1}
\KOMAoption{captions}{tableheading}
\makeatletter
\@ifpackageloaded{tcolorbox}{}{\usepackage[skins,breakable]{tcolorbox}}
\@ifpackageloaded{fontawesome5}{}{\usepackage{fontawesome5}}
\definecolor{quarto-callout-color}{HTML}{909090}
\definecolor{quarto-callout-note-color}{HTML}{0758E5}
\definecolor{quarto-callout-important-color}{HTML}{CC1914}
\definecolor{quarto-callout-warning-color}{HTML}{EB9113}
\definecolor{quarto-callout-tip-color}{HTML}{00A047}
\definecolor{quarto-callout-caution-color}{HTML}{FC5300}
\definecolor{quarto-callout-color-frame}{HTML}{acacac}
\definecolor{quarto-callout-note-color-frame}{HTML}{4582ec}
\definecolor{quarto-callout-important-color-frame}{HTML}{d9534f}
\definecolor{quarto-callout-warning-color-frame}{HTML}{f0ad4e}
\definecolor{quarto-callout-tip-color-frame}{HTML}{02b875}
\definecolor{quarto-callout-caution-color-frame}{HTML}{fd7e14}
\makeatother
\makeatletter
\@ifpackageloaded{bookmark}{}{\usepackage{bookmark}}
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\AtBeginDocument{%
\ifdefined\contentsname
\renewcommand*\contentsname{Table of contents}
\else
\newcommand\contentsname{Table of contents}
\fi
\ifdefined\listfigurename
\renewcommand*\listfigurename{List of Figures}
\else
\newcommand\listfigurename{List of Figures}
\fi
\ifdefined\listtablename
\renewcommand*\listtablename{List of Tables}
\else
\newcommand\listtablename{List of Tables}
\fi
\ifdefined\figurename
\renewcommand*\figurename{Figure}
\else
\newcommand\figurename{Figure}
\fi
\ifdefined\tablename
\renewcommand*\tablename{Table}
\else
\newcommand\tablename{Table}
\fi
}
\@ifpackageloaded{float}{}{\usepackage{float}}
\floatstyle{ruled}
\@ifundefined{c@chapter}{\newfloat{codelisting}{h}{lop}}{\newfloat{codelisting}{h}{lop}[chapter]}
\floatname{codelisting}{Listing}
\newcommand*\listoflistings{\listof{codelisting}{List of Listings}}
\makeatother
\makeatletter
\makeatother
\makeatletter
\@ifpackageloaded{caption}{}{\usepackage{caption}}
\@ifpackageloaded{subcaption}{}{\usepackage{subcaption}}
\makeatother
\makeatletter
\@ifpackageloaded{tikz}{}{\usepackage{tikz}}
\makeatother
\newcommand*\circled[1]{\tikz[baseline=(char.base)]{
\node[shape=circle,draw,inner sep=1pt] (char) {{\scriptsize#1}};}}
\ifLuaTeX
\usepackage{selnolig} % disable illegal ligatures
\fi
\usepackage{bookmark}
\IfFileExists{xurl.sty}{\usepackage{xurl}}{} % add URL line breaks if available
\urlstyle{same} % disable monospaced font for URLs
\hypersetup{
pdftitle={HSMA - the little book of DES},
pdfauthor={Dan Chalk and Sammi Rosser},
colorlinks=true,
linkcolor={blue},
filecolor={Maroon},
citecolor={Blue},
urlcolor={Blue},
pdfcreator={LaTeX via pandoc}}
\title{HSMA - the little book of DES}
\usepackage{etoolbox}
\makeatletter
\providecommand{\subtitle}[1]{% add subtitle to \maketitle
\apptocmd{\@title}{\par {\large #1 \par}}{}{}
}
\makeatother
\subtitle{An introduction to writing discrete event simulation models
for healthcare (and beyond) in SimPy}
\author{Dan Chalk and Sammi Rosser}
\date{2024-02-26}
\begin{document}
\maketitle
\renewcommand*\contentsname{Table of contents}
{
\hypersetup{linkcolor=}
\setcounter{tocdepth}{1}
\tableofcontents
}
\bookmarksetup{startatroot}
\chapter*{Licencing, Attribution and
Acknowledgements}\label{licencing-attribution-and-acknowledgements}
\addcontentsline{toc}{chapter}{Licencing, Attribution and
Acknowledgements}
\markboth{Licencing, Attribution and Acknowledgements}{Licencing,
Attribution and Acknowledgements}
\section*{Licencing}\label{licencing}
\addcontentsline{toc}{section}{Licencing}
\markright{Licencing}
\subsection*{Code Snippets}\label{code-snippets}
\addcontentsline{toc}{subsection}{Code Snippets}
All code in this book is provided under the MIT Licence.
\begin{quote}
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
\end{quote}
\begin{quote}
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
\end{quote}
\begin{quote}
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\end{quote}
\subsection*{Text}\label{text}
\addcontentsline{toc}{subsection}{Text}
The accompanying text in this book is licenced under a
\href{http://creativecommons.org/licenses/by-nc-sa/4.0/}{Creative
Commons Attribution-NonCommercial-ShareAlike 4.0 International License}.
\href{http://creativecommons.org/licenses/by-nc-sa/4.0/}{\includesvg{index_files/mediabag/License-CC-BY--NC--S.svg}}
\href{http://creativecommons.org/licenses/by-nc-sa/4.0/}{\includegraphics{index_files/mediabag/88x31.png}}
\section*{Attribution and
Acknowledgements}\label{attribution-and-acknowledgements}
\addcontentsline{toc}{section}{Attribution and Acknowledgements}
\markright{Attribution and Acknowledgements}
The content in this book is a combined effort, bringing together the
knowledge of the Peninsula Collaborative for Operational Research and
Design (PenCHORD) on the topic.
The content in part 1 and a large amount of part 2 is from a series of
sessions given on discrete event simulation as part of the HSMA
programme. These lectures and accompanying code examples were written by
Dr Daniel Chalk and made available under the \textbf{CC BY-NC-SA 4.0}
licence. The chapters often replicate the wording of these lectures
nearly exactly, with minor tweaks and additions for readability and to
better suit the format of a book made by Sammi Rosser.
The content in parts 3 and 4 is more of a mixture of content from the
HSMA lectures along with brand new content written for this book,
drawing on a range of work done by the wider PenCHORD team.
All work that has been adapted is licenced under the MIT Licence. To
comply with the terms of this licence, the MIT licence is provided
below.
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-note-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-note-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-note-color}{\faInfo}\hspace{0.5em}{MIT Licence of adapted code}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
``Software''), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED ``AS IS'', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
\end{tcolorbox}
Adaptation into book format has been done throughout by Sammi Rosser.
\textbf{HSMA 6: Lecture 1} -
\href{https://docs.google.com/presentation/d/1HO0wXTocHRZgeuLadkGvqf5yC8BEmQzTcgo8QHOd0yY/edit?usp=sharing}{Slides}
- \href{https://github.com/hsma-programme/h6_2a_intro_to_des}{Github}
\textbf{HSMA 6: Lecture 2} -
\href{https://docs.google.com/presentation/d/15bQX0yxfDgjNVJSQORNzBCWVvbKI_LaTcbtYVKsvYcU/edit?usp=sharing}{Slides}
- \href{https://github.com/hsma-programme/h6_simpy_part_1}{Github}
\textbf{HSMA 6: Lecture 3}-
\href{https://docs.google.com/presentation/d/1y7S7bjJtdLQCquzzZ2vjYzRx9-RwXE-SWgWf88fLc4o/edit?usp=sharing}{Slides}
- \href{https://github.com/hsma-programme/h6_simpy_part_2}{Github}
\part{Part 1 - DES Concepts and Design}
\chapter{Introduction to DES
Concepts}\label{introduction-to-des-concepts}
Discrete Event Simulation (DES) is a simulation modelling approach that
allows us to model queuing problems. Queuing problems are those in which
``things'' (usually people, but not always) are waiting for a service or
services.
In a Discrete Event Simulation, entities flow through (and queue for)
discrete sequential processes that use resources. The queues may be
physical (people physically waiting) or more abstract (people on a
waiting list, waiting for an appointment etc).
\includegraphics{images/example.png}
In healthcare, DES models can be used to model :
\begin{itemize}
\tightlist
\item
Patient pathways
\item
Phone systems
\item
The transit of test results And more!
\end{itemize}
DES models are extremely useful for asking what if? questions about
process / pathway changes.
\section{Why use DES?}\label{why-use-des}
Discrete event simulation allows you to
\begin{itemize}
\tightlist
\item
Test changes in a risk-free, low-cost way
\item
Explore the impact of changes in demand
\item
See whether a system can cope on bad days as well as good days
\item
Predict how long it will take to clear an existing backlog
\end{itemize}
This can allow you to optimize a system, leading to better balance and
better flow, which can in turn lead to
\begin{itemize}
\tightlist
\item
A safer environment
\item
Less stress for staff
\item
Improved patient experience
\item
Meeting targets
\end{itemize}
\section{An example}\label{an-example}
Imagine being able to create a model of an emergency department.
In this model, you can change all sorts of things
\begin{itemize}
\tightlist
\item
how many doctors, nurses and receptionists there are at each step
\item
how long it takes for people to be seen
\item
how many people go into the trauma pathway versus the non-trauma
pathway
\end{itemize}
Then sprinkle in a dose of randomness - because in real life, you're not
going to have each appointment taking the exact same amount of time, or
people arriving exactly every five minutes - and then you can start to
explore just how well a system will perform, what changes might have the
most impact, and what configuration is likely to perform best. Then you
can run it 1000 times with slightly different random days to see how
well it performs on both good days and bad.
You can polish it all off by visualising the individual entities moving
through the system so people with little understanding of discrete event
simulation can get a sense of what's going on, and you can give them
access to all of the controls - the number of nurses and doctors, the
average consultation length, and more - so that they can explore the
impact of theser changes themselves.
\url{https://github.com/hsma-programme/Teaching_DES_Concepts_Streamlit/assets/29951987/1adc36a0-7bc0-4808-8d71-2d253a855b31}
\section{Runs and Trials}\label{runs-and-trials}
In a stochastic model, it is important that we do not just run a model
once if we're looking to draw insights from our results. This is because
every run of the simulation will have different random samples for
inter-arrival times, activity times etc.
What if you had a run with unusually long activity times sampled (a run
of ``bad luck'')? Or unusually long inter-arrival times (a run of ``good
luck'')?
We need to run a stochastic simulation many times and take summary
statistics over the results from each run to get more representative
results from the model.
A single run of a model for a simulated period of time is known as a
\textbf{run}. A batch of multiple runs with the same parameter values is
known as a \textbf{trial}.
\section{Key DES Terminology}\label{key-des-terminology}
\includegraphics{images/example_des_simple.png}
\textbf{Entities} are the things that are flowing through the sequential
processes in the model (eg patients, test results, callers on a phone)
\textbf{Generators} are the way in which entities enter the model and
come into being (eg arriving at ED by ambulance, self-presenting,
referral from GP)
\textbf{Inter-Arrival Times} specify the time between entities being
generated in the generators (ie the time between arrivals into the
modelled system)
\textbf{Activities} (sometimes referred to as Servers) are the bits of
process that the entities are queuing up for (eg triaged, seen at
reception, speak to doctor etc)
\textbf{Activity Time} represents the amount of time it takes for an
activity to happen to an entity - this is normally stochastic (random)
and drawn from a distribution for each entity (eg time spent with nurse,
time to be treated etc)
\textbf{Resources} are the ``stuff'' and / or ``staff'' required for an
activity to happen to an entity (eg nurse to triage, bed for patient,
consultation room for GP to see patient etc, X-Ray machine and
Radiographer to be free for X-Ray etc). Important - resources may be
shared between activities (eg the same nurse may be required to run
multiple activities in our model, or even things we haven't explicitly
modelled)
\textbf{Queues} hold entities that are waiting for an activity. Entities
wait in a queue until the activity has both the capacity and all
required resources.
\textbf{Sinks} are how entities leave the model (the bit of system we're
modelling)
\includegraphics{images/des_steps.png}
\subsection{Entities}\label{entities}
Each entity may have certain ``attributes'' that it ``carries with
them'' to help determine its journey through the modelled system. For
example :
\begin{itemize}
\tightlist
\item
whether it goes down path A or B
\item
how long it spends in an activity
\item
its priority in a queue for an activity
\end{itemize}
There may also be more than one type of entity in a model at the same
time. For example, patients in a clinic, their test results, and phone
calls into the clinic are all entities that we may want to capture when
modelling the clinic.
\includegraphics{images/des_entities.png}
\subsection{Generators and
Inter-Arrival}\label{generators-and-inter-arrival}
A generator creates new entities to bring into the system. The rate at
which new entities are generated is determined by an inter-arrival time.
The inter-arrival time determines the time between one entity being
generated, and the next one being generated.
Inter-arrival times may be fixed, but are typically sampled (drawn)
stochastically (randomly) from a distribution to capture variability
(even if the variability is small).
An Exponential Distribution is often used to sample inter-arrival times.
More than one distribution may be used for the same generator (e.g.~for
different times of the day, day of week etc). You may also (often) have
more than one generator in a system.
\includegraphics{images/generators_iat.png}
\subsection{Queues}\label{queues}
Each activity in a Discrete Event Simulation has an associated queue.
The queue holds entities whilst they wait for the activity to become
available for them.
Each queue has a queuing policy. This determines the order in which
entities are released from the queue into the activity. The two most
common queuing policies are:
\begin{itemize}
\tightlist
\item
First In First Out (FIFO) : entities are seen in the order they
arrive. This is the default.
\item
Priority-based : entities are seen according to some priority
attribute. Ties often resolved using FIFO
\end{itemize}
\includegraphics{images/queue_types.png}
\subsection{Activities and Activity
Times}\label{activities-and-activity-times}
Each activity in a DES describes a process -- this may be a simple
atomic task, or a set of tasks bundled together. For an activity to take
place, it needs :
\begin{itemize}
\tightlist
\item
An entity (drawn from the queue)
\item
The required type and number of resource to be available
\end{itemize}
Once the above conditions have been met, the activity begins. The
entity, and the resource(s) are then locked in place for an amount of
time -- the Activity Time. The resource(s) cannot be used elsewhere
until the activity time has passed.
Activity times may be fixed, but are typically sampled stochastically
from a distribution.
\includegraphics{images/activity_activity_times.png}
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-tip-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-tip-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-tip-color}{\faLightbulb}\hspace{0.5em}{Tip}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
The common distribution for process times is the Log Normal
distributions. However, Exponential Distributions can be a good starting
point, as it's easy to change the ``mean'' when playing around with
things. You can then change to something like a Log Normal once you (and
the stakeholders) are happy
\end{tcolorbox}
\subsection{Resources}\label{resources}
Resources are needed to undertake activities. An activity may require
just a single resource, more than one resource of the same type, or
multiple resources of different types.
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-tip-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-tip-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-tip-color}{\faLightbulb}\hspace{0.5em}{Tip}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
An activity may not require a resource at all, but think carefully to
ensure that it really is either ``resourceless'' or there is no
constraint on the resource (and so doesn't need to be modelled).
Resources can include - ``staff'' (e.g.~doctors, nurses, officers etc) -
``stuff'' (beds, test equipment, detention cell etc)
\end{tcolorbox}
Resources can (and often are) shared across a system, so may be required
for more than one activity. Therefore, a resource drain in one part of
the system can affect another.
All required resources are needed for an activity to take place.
In some activities, having optional additional resource may speed up the
activity (though rarely linearly).
\includegraphics{images/resources_simple.png}
\subsection{Sinks}\label{sinks}
Sinks are how entities leave the system, or part of the system, being
modelled. Sinks might include :
\begin{itemize}
\tightlist
\item
an entity physically leaving a system (e.g.~discharge from hospital)
\item
an entity no longer existing (e.g.~death, use of sample, end of
telephone call)
\item
an entity no longer needing to access activities that we're interested
in (e.g.~they leave the bit of the system that we're modelling)
\end{itemize}
The most important thing to remember about a sink is that it doesn't
necessarily represent an entity leaving the system entirely.
For example, the scope of your model may only cover the triage aspect of
an Emergency Department. Therefore, a valid sink might be placed after
their triage - they've left the scope of our model
\includegraphics{images/sinks_simple.png}
\subsection{Branching Paths}\label{branching-paths}
Real world systems (and the models of those systems) are rarely linear.
Often, different things will happen to different entities. In a Discrete
Event Simulation, this means different entities flowing to different
activities, or different sinks.
We might differentiate based on :
\begin{itemize}
\tightlist
\item
an attribute of the patient (e.g.~patients with a higher priority
value flow through a different set of activities)
\item
probability (e.g.~we know that approx 60\% of these patients end up
being admitted, so we'll randomly select for them to be admitted 60\%
of the time)
\item
time (e.g.~after a certain time of day, entities flow through a
different set of activities)
\end{itemize}
\includegraphics{images/branching_paths_simple.png}
\subsection{Outputs}\label{outputs}
As with any type of model, it's important to think about what outputs
you need your DES model to generate to answer your modelling questions.
As a DES model is used to model queuing and resourcing problems, typical
DES model outputs include average, min, max, xth percentile of :
\begin{itemize}
\tightlist
\item
time entities are in system
\item
queue length and duration for queues of interest
\item
rate of resource utilisation (ie \% of time a resource is in use for
activities in the model)
\item
probability of exceeding a defined queue length / queue time /
resource utilisation threshold (e.g.~4 hour wait in ED, overcrowding
thresholds)
\end{itemize}
\includegraphics{images/output_example_simple.png}
\chapter{Exercise - Designing a DES}\label{exercise---designing-a-des}
Design a Discrete Event Simulation for a system of your choosing.
Think about some different possibilities (and these don't have to be
health-related, they can be anything! A restaurant? Airport? Customer
service line?).
You should then draw up a design for the model. This should include :
\begin{itemize}
\tightlist
\item
The ``what if?'' question(s) you would use the model to answer
\item
A process map of the system you are looking to model
\item
A conceptual model for the proposed Discrete Event Simulation (which
may not include everything in the process map).
\end{itemize}
Identify the types of entities, generators, activities, queues,
resources and sinks.
Describe what each of your inter-arrival times and activity times
represent, and from where you might draw the data.
Consider the scope, level of detail etc when designing your model. What
do you need to model to answer your question? How can you simplify your
model?
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-tip-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-tip-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-tip-color}{\faLightbulb}\hspace{0.5em}{Tip}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
The website \href{https://app.diagrams.net/}{draw.io (also known as
diagrams.net)} is a great free resource for creating process maps.
\end{tcolorbox}
\part{Part 2 - Your First SimPy Model}
\chapter{An Introduction to SimPy}\label{an-introduction-to-simpy}
SimPy is a Python package that allows us to create powerful Discrete
Event Simulation (DES) models.
You can read SimPy's own tutorials and reference guides on its website :
https://simpy.readthedocs.io/en/latest/ - but we'd recommend working
through at least the first few chapters in this book first.
To install SimPy, we need to \texttt{pip\ install\ simpy}. However, it
is recommended that you use a separate environment. Make sure you switch
to this environment for any DES work you do - or, even better, set up a
separate environment for every DES project you undertake!
Before we look at how we put together a SimPy model, there's a couple
concepts we need to cover first that are important to understand.
\section{Simulation Time}\label{simulation-time}
SimPy simulations run in time units. These units of time can represent
any real world amount of time we like as long as we are consistent
within the same model.
Our time units should represent the lowest level of real world time that
we need to represent in the model. In models of pathways where people
arrive for a service, this will likely be minutes (seconds is too much,
and hours is probably not enough, unless all the processes are slow).
But we may have pathways where we measure time in days or weeks
(e.g.~referral pathways).
For example, in an ED model, our time units may represent minutes. So we
specify everything in minutes - inter-arrival times, activity times etc.
Strictly speaking, SimPy doesn't run in time units ticking away one by
one. Instead, it schedules events jumps to the next event. But don't
worry about that for your purposes. Just know that, because of this, you
will see current simulation time as floating point numbers (eg the
current time unit could be 3.6 etc).
\section{Generator Functions}\label{generator-functions}
SimPy is built around a special type of function in Python known as a
Generator Function.
So let's have a look at what we mean by a Generator Function.
Conventional functions in Python are called, then run with some
(optional) inputs, and then finish (usually by returning some output).
When we call the function again, it runs again, from scratch.
Generator functions remember where they were and what they did when
control is passed back (they retain their local state), so that they can
continue where they left off, and can be used as powerful iterators (for
and while loops are other examples of iterators).
This is very useful where we want state to be maintained, so we can
remember how long until we generate the next entity, or where an entity
is in a pathway\ldots{}
Let's look at a very simple example of a generator function to see how
they work.
\includegraphics{images/generator_func_example.png}
In SimPy, we use Generator Functions in two different places :
To model the DES generators (arrival points) To model the individual
journey of each entity
Let's imagine we are modelling patients in a patient pathway.
For 1, the generator function basically creates a patient, sets them off
on their pathway, then freezes in place for an amount of time
representing the inter-arrival time to the next patient. Then it does it
all over again.
For 2, the generator function requests a resource and freezes until that
resource is available (the queue). When the resource is available it
freezes in place for an amount of time with it (the activity). It will
then either move on to the next activity (and request the resource for
it, as above) or end if there are no further activities.
\chapter{The Recommended Structure for DES
Models}\label{the-recommended-structure-for-des-models}
There are many different ways to structure SimPy models.
You will often find different coders have their own preferred approach!
We're going to structure our SimPy model in an Object Oriented way.
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-note-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-note-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-note-color}{\faInfo}\hspace{0.5em}{Note}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
There are two main ways of programming in python - \textbf{functional}
and \textbf{object oriented}. These are known as coding
\textbf{paradigms}.
In functional programming, you write \textbf{functions} that are
(ideally) designed to do one thing and do it well.
You then use these functions in some sort of sequence to achieve what
you're trying to do.
This can make a lot of sense for a lot of data analysis and data science
workflows.
In comparison, in object oriented programming (OOP) everything is
centred around objects that have their own
\begin{itemize}
\tightlist
\item
attributes (variables that belong to them) and
\item
methods (which are what functions are called when they belong to
objects).
\end{itemize}
\textbf{Objects} are created as \textbf{instances} of \textbf{classes}.
Classes are essentially generalised blueprints for how certain types of
objects should work!
This can be really useful in situations where you have either
\begin{itemize}
\tightlist
\item
a lot of logic that makes sense to attach and organise with the thing
that uses that logic (like a process)
\item
a need to make copies of a very similar thing (like lots of patients
to populate a model)
\end{itemize}
Let's look at an example.
Let's say we want to write code that defines how an ambulance works.
There will be properties the ambulance has. Things like :
\begin{itemize}
\tightlist
\item
The organisation it belongs to
\item
The registration number
\item
Whether a patient is currently on board
\item
Whether the siren is currently going off
\end{itemize}
There will also be things the ambulance does :
\begin{itemize}
\tightlist
\item
Driving
\item
Parking
\item
Having patients loaded into it / out of it
\item
Switching the siren on and off
\end{itemize}
In Object Oriented Programming, the things it \textbf{has} are known as
\textbf{attributes} and the things it \emph{does} are \emph{methods}.
\end{tcolorbox}
Specifically, we're going to have 4 different classes: g, entity, model,
and trial.
While object oriented code can feel a bit cumbersome for small models,
following the same sort of pattern each time makes it really easy to
keep track of what your model is doing. It also makes it a lot easier to
expand and modify your model over time.
The other benefit is that you will find other models from people who
have done the HSMA course are likely to have a similar structure - so
you should find it easier to read, reuse and tweak their models too!
\begin{tcolorbox}[enhanced jigsaw, colframe=quarto-callout-note-color-frame, bottomtitle=1mm, breakable, rightrule=.15mm, coltitle=black, colbacktitle=quarto-callout-note-color!10!white, opacityback=0, leftrule=.75mm, arc=.35mm, toptitle=1mm, title=\textcolor{quarto-callout-note-color}{\faInfo}\hspace{0.5em}{Note}, titlerule=0mm, colback=white, toprule=.15mm, bottomrule=.15mm, left=2mm, opacitybacktitle=0.6]
Even among people using an object oriented approach, you will find some
variations.
Again, there's nothing wrong with that!
Another common pattern you may come across in time is the Scenario,
entity, model approach with single\_run and multiple\_run functions that
don't exist inside a class.
Over time, you'll likely find a style used by someone else that you find
particularly easy to follow and you want to adopt and adapt.
\end{tcolorbox}
\textbf{g Class}
This is a special class that will store our global level parameters for
the model.
Unlike most OOP cases, we won't create an instance of this class (hence
the lower case) - we'll just refer to the blueprint directly.
\textbf{Entity}
This class will represent our entity.
An entity could be a customer, a passenger, etc., but often in our
healthcare models, our entities will be patients.
Entities will carry with them information that we can record to and / or
read from (e.g.~an ID, how long they spent queuing, their condition
etc).
If we had more than one entity, we'd need a class for each.
Multiple entities could be different types of patients - e.g.~trauma
patients and non-trauma patients.