0% found this document useful (0 votes)
3 views

Computer Simulation A Foundational Approach Using Python Yahya Esmail Osais download

The document is a promotional listing for the book 'Computer Simulation: A Foundational Approach Using Python' by Yahya E. Osais, detailing its contents and structure. It includes chapters on simulation fundamentals, building conceptual models, and statistical analysis of simulated data. Additionally, it provides links to other related books and resources on computer simulation.

Uploaded by

fajlurozochi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Computer Simulation A Foundational Approach Using Python Yahya Esmail Osais download

The document is a promotional listing for the book 'Computer Simulation: A Foundational Approach Using Python' by Yahya E. Osais, detailing its contents and structure. It includes chapters on simulation fundamentals, building conceptual models, and statistical analysis of simulated data. Additionally, it provides links to other related books and resources on computer simulation.

Uploaded by

fajlurozochi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 90

Computer Simulation A Foundational Approach

Using Python Yahya Esmail Osais download

https://ebookbell.com/product/computer-simulation-a-foundational-
approach-using-python-yahya-esmail-osais-20668778

Explore and download more ebooks at ebookbell.com


Here are some recommended products that we believe you will be
interested in. You can click the link to download.

Computer Simulation A Foundational Approach Using Python Yahya Esmail


Osais

https://ebookbell.com/product/computer-simulation-a-foundational-
approach-using-python-yahya-esmail-osais-56234846

Computer Simulations In Condensed Matter Systems From Materials To


Chemical Biology Mauro Ferrario Giovanni Ciccotti K Binder Ettore
Majorana Foundation And Centre For Scientific Culture

https://ebookbell.com/product/computer-simulations-in-condensed-
matter-systems-from-materials-to-chemical-biology-mauro-ferrario-
giovanni-ciccotti-k-binder-ettore-majorana-foundation-and-centre-for-
scientific-culture-4099310

The Phenomena Of Argyll Stone Rows Revealed Through 3d Computer


Simulation David A Fisher

https://ebookbell.com/product/the-phenomena-of-argyll-stone-rows-
revealed-through-3d-computer-simulation-david-a-fisher-49984346

The Process Of Question Answering A Computer Simulation Of Cognition


Wendy G Lehnert

https://ebookbell.com/product/the-process-of-question-answering-a-
computer-simulation-of-cognition-wendy-g-lehnert-50448848
Handbook Of Computer Simulation In Radio Engineering Communications
And Radar 1st Sergey A Leonov

https://ebookbell.com/product/handbook-of-computer-simulation-in-
radio-engineering-communications-and-radar-1st-sergey-a-leonov-5479170

Simulating Nature A Philosophical Study Of Computersimulation


Uncertainties And Their Role In Climate Science And Policy Advice 2nd
Arthur C Petersen

https://ebookbell.com/product/simulating-nature-a-philosophical-study-
of-computersimulation-uncertainties-and-their-role-in-climate-science-
and-policy-advice-2nd-arthur-c-petersen-4422206

Simulating Nature A Philosophical Study Of Computersimulation


Uncertainties And Their Role In Climate Science And Policy Advice
Second Edition 2nd Edition Arthur C Petersen

https://ebookbell.com/product/simulating-nature-a-philosophical-study-
of-computersimulation-uncertainties-and-their-role-in-climate-science-
and-policy-advice-second-edition-2nd-edition-arthur-c-petersen-4748638

Reliable Methods For Computer Simulation Error Control And A


Posteriori Estimates P Neittaanmki And S Repin Eds

https://ebookbell.com/product/reliable-methods-for-computer-
simulation-error-control-and-a-posteriori-estimates-p-neittaanmki-and-
s-repin-eds-4425476

Magneticallycontrolled Shunt Reactors Historical Overview Operating


Principles Computer Simulation And Return Of Experience Ga Evdokunin

https://ebookbell.com/product/magneticallycontrolled-shunt-reactors-
historical-overview-operating-principles-computer-simulation-and-
return-of-experience-ga-evdokunin-50252346
Computer Simulation
A Foundational Approach Using Python
CHAPMAN & HALL/CRC
COMPUTER and INFORMATION SCIENCE SERIES
Series Editor: Sartaj Sahni

PUBLISHED TITLES

ADVERSARIAL REASONING: COMPUTATIONAL APPROACHES TO READING THE OPPONENT’S MIND


Alexander Kott and William M. McEneaney
COMPUTER-AIDED GRAPHING AND SIMULATION TOOLS FOR AUTOCAD USERS
P. A. Simionescu
COMPUTER SIMULATION: A FOUNDATIONAL APPROACH USING PYTHON
Yahya E. Osais
DELAUNAY MESH GENERATION
Siu-Wing Cheng, Tamal Krishna Dey, and Jonathan Richard Shewchuk
DISTRIBUTED SENSOR NETWORKS, SECOND EDITION
S. Sitharama Iyengar and Richard R. Brooks
DISTRIBUTED SYSTEMS: AN ALGORITHMIC APPROACH, SECOND EDITION
Sukumar Ghosh
ENERGY-AWARE MEMORY MANAGEMENT FOR EMBEDDED MULTIMEDIA SYSTEMS:
A COMPUTER-AIDED DESIGN APPROACH
Florin Balasa and Dhiraj K. Pradhan
ENERGY EFFICIENT HARDWARE-SOFTWARE CO-SYNTHESIS USING RECONFIGURABLE HARDWARE
Jingzhao Ou and Viktor K. Prasanna
EVOLUTIONARY MULTI-OBJECTIVE SYSTEM DESIGN: THEORY AND APPLICATIONS
Nadia Nedjah, Luiza De Macedo Mourelle, and Heitor Silverio Lopes
FROM ACTION SYSTEMS TO DISTRIBUTED SYSTEMS: THE REFINEMENT APPROACH
Luigia Petre and Emil Sekerinski
FROM INTERNET OF THINGS TO SMART CITIES: ENABLING TECHNOLOGIES
Hongjian Sun, Chao Wang, and Bashar I. Ahmad
FUNDAMENTALS OF NATURAL COMPUTING: BASIC CONCEPTS, ALGORITHMS, AND APPLICATIONS
Leandro Nunes de Castro
HANDBOOK OF ALGORITHMS FOR WIRELESS NETWORKING AND MOBILE COMPUTING
Azzedine Boukerche
PUBLISHED TITLES CONTINUED
HANDBOOK OF APPROXIMATION ALGORITHMS AND METAHEURISTICS
Teofilo F. Gonzalez
HANDBOOK OF BIOINSPIRED ALGORITHMS AND APPLICATIONS
Stephan Olariu and Albert Y. Zomaya
HANDBOOK OF COMPUTATIONAL MOLECULAR BIOLOGY
Srinivas Aluru
HANDBOOK OF DATA STRUCTURES AND APPLICATIONS
Dinesh P. Mehta and Sartaj Sahni
HANDBOOK OF DYNAMIC SYSTEM MODELING
Paul A. Fishwick
HANDBOOK OF ENERGY-AWARE AND GREEN COMPUTING
Ishfaq Ahmad and Sanjay Ranka
HANDBOOK OF GRAPH THEORY, COMBINATORIAL OPTIMIZATION, AND ALGORITHMS
Krishnaiyan “KT” Thulasiraman, Subramanian Arumugam, Andreas Brandstädt, and Takao Nishizeki
HANDBOOK OF PARALLEL COMPUTING: MODELS, ALGORITHMS AND APPLICATIONS
Sanguthevar Rajasekaran and John Reif
HANDBOOK OF REAL-TIME AND EMBEDDED SYSTEMS
Insup Lee, Joseph Y-T. Leung, and Sang H. Son
HANDBOOK OF SCHEDULING: ALGORITHMS, MODELS, AND PERFORMANCE ANALYSIS
Joseph Y.-T. Leung
HIGH PERFORMANCE COMPUTING IN REMOTE SENSING
Antonio J. Plaza and Chein-I Chang
HUMAN ACTIVITY RECOGNITION: USING WEARABLE SENSORS AND SMARTPHONES
Miguel A. Labrador and Oscar D. Lara Yejas
IMPROVING THE PERFORMANCE OF WIRELESS LANs: A PRACTICAL GUIDE
Nurul Sarkar
INTEGRATION OF SERVICES INTO WORKFLOW APPLICATIONS
Paweł Czarnul
INTRODUCTION TO NETWORK SECURITY
Douglas Jacobson
LOCATION-BASED INFORMATION SYSTEMS: DEVELOPING REAL-TIME TRACKING APPLICATIONS
Miguel A. Labrador, Alfredo J. Pérez, and Pedro M. Wightman
PUBLISHED TITLES CONTINUED

METHODS IN ALGORITHMIC ANALYSIS


Vladimir A. Dobrushkin
MULTICORE COMPUTING: ALGORITHMS, ARCHITECTURES, AND APPLICATIONS
Sanguthevar Rajasekaran, Lance Fiondella, Mohamed Ahmed, and Reda A. Ammar
NETWORKS OF THE FUTURE: ARCHITECTURES, TECHNOLOGIES, AND IMPLEMENTATIONS
Mahmoud Elkhodr, Qusay F. Hassan, and Seyed Shahrestani
PERFORMANCE ANALYSIS OF QUEUING AND COMPUTER NETWORKS
G. R. Dattatreya
THE PRACTICAL HANDBOOK OF INTERNET COMPUTING
Munindar P. Singh
SCALABLE AND SECURE INTERNET SERVICES AND ARCHITECTURE
Cheng-Zhong Xu
SOFTWARE APPLICATION DEVELOPMENT: A VISUAL C++®, MFC, AND STL TUTORIAL
Bud Fox, Zhang Wenzu, and Tan May Ling
SPECULATIVE EXECUTION IN HIGH PERFORMANCE COMPUTER ARCHITECTURES
David Kaeli and Pen-Chung Yew
TRUSTWORTHY CYBER-PHYSICAL SYSTEMS ENGINEERING
Alexander Romanovsky and Fuyuki Ishikawa
VEHICULAR NETWORKS: FROM THEORY TO PRACTICE
Stephan Olariu and Michele C. Weigle
X-MACHINES FOR AGENT-BASED MODELING: FLAME PERSPECTIVES
Mariam Kiran
Computer Simulation
A Foundational Approach Using Python

Yahya E. Osais
CRC Press
Taylor & Francis Group
6000 Broken Sound Parkway NW, Suite 300
Boca Raton, FL 33487-2742
© 2018 by Taylor & Francis Group, LLC
CRC Press is an imprint of Taylor & Francis Group, an Informa business

No claim to original U.S. Government works

Printed on acid-free paper


Version Date: 20171024

International Standard Book Number-13: 978-1-498-72682-5 (Hardback)

This book contains information obtained from authentic and highly regarded sources. Reasonable
efforts have been made to publish reliable data and information, but the author and publisher cannot
assume responsibility for the validity of all materials or the consequences of their use. The authors and
publishers have attempted to trace the copyright holders of all material reproduced in this publication
and apologize to copyright holders if permission to publish in this form has not been obtained. If any
copyright material has not been acknowledged please write and let us know so we may rectify in any
future reprint.

Except as permitted under U.S. Copyright Law, no part of this book may be reprinted, reproduced,
transmitted, or utilized in any form by any electronic, mechanical, or other means, now known or
hereafter invented, including photocopying, microfilming, and recording, or in any information
storage or retrieval system, without written permission from the publishers.

For permission to photocopy or use material electronically from this work, please access
www.copyright.com (http://www.copyright.com/) or contact the Copyright Clearance Center, Inc.
(CCC), 222 Rosewood Drive, Danvers, MA 01923, 978-750-8400. CCC is a not-for-profit organization
that provides licenses and registration for a variety of users. For organizations that have been granted
a photocopy license by the CCC, a separate system of payment has been arranged.

Trademark Notice: Product or corporate names may be trademarks or registered trademarks, and
are used only for identification and explanation without intent to infringe.
Visit the Taylor & Francis Web site at
http://www.taylorandfrancis.com
and the CRC Press Web site at
http://www.crcpress.com
To my wife, Asmahan,
and my daughters, Renad, Retal, and Remas.
Contents

List of Programs xv

List of Figures xix

List of Tables xxvii


Foreword xxix

Preface xxxi

About the Author xxxiii


Abbreviations xxxv

Symbols xxxvii

Part I The Fundamentals

Chapter 1  Introduction 3
1.1 THE PILLARS OF SCIENCE AND ENGINEERING 3
1.2 STUDYING THE QUEUEING PHENOMENON 4
1.3 WHAT IS SIMULATION? 5
1.4 LIFECYCLE OF A SIMULATION STUDY 6
1.5 ADVANTAGES AND LIMITATIONS OF SIMULATION 9
1.6 OVERVIEW OF THE BOOK 10
1.7 SUMMARY 11

Chapter 2  Building Conceptual Models 13


2.1 WHAT IS A CONCEPTUAL MODEL? 13
2.2 ELEMENTS OF A CONCEPTUAL MODEL 15
2.2.1 Entities 15

ix
x  Contents

2.2.2 Attributes 15
2.2.3 State Variables 16
2.2.4 Events 17
2.2.5 Activities 17
2.3 THE SINGLE-SERVER QUEUEING SYSTEM 18
2.4 STATE DIAGRAMS 22
2.5 ACTUAL TIME VERSUS SIMULATED TIME 23
2.6 SUMMARY 24
2.7 EXERCISES 24

Chapter 3  Simulating Probabilities 27


3.1 RANDOM EXPERIMENTS AND EVENTS 27
3.2 WHAT IS PROBABILITY? 28
3.3 COMPUTING PROBABILITIES 30
3.4 PROBABILITY AS A SAMPLE MEAN 32
3.5 SUMMARY 36
3.6 EXERCISES 36

Chapter 4  Simulating Random Variables and Stochastic Pro-


cesses 39
4.1 WHAT ARE RANDOM VARIABLES? 39
4.1.1 Probability Mass Functions 40
4.1.2 Cumulative Distribution Functions 41
4.1.3 Probability Density Functions 43
4.1.4 Histograms 44
4.2 SOME USEFUL RANDOM VARIABLES 46
4.2.1 Bernoulli 46
4.2.2 Binomial 47
4.2.3 Geometric 48
4.2.4 Poisson 49
4.2.5 Uniform 50
4.2.6 Exponential 53
4.2.7 Erlang 54
4.2.8 Normal 54
4.2.9 Triangular 55
4.3 STOCHASTIC PROCESSES 56
Contents  xi

4.4 DYNAMIC SYSTEM EVOLUTION 58


4.5 SIMULATING QUEUEING PROCESSES 60
4.5.1 Discrete-Time Markov Chains 62
4.5.2 Continuous-Time Markov Chains 64
4.6 SUMMARY 67
4.7 EXERCISES 67

Chapter 5  Simulating the Single-Server Queueing System 69


5.1 SIMULATION MODEL 69
5.2 COLLECTING SIMULATED DATA 75
5.3 PERFORMANCE LAWS 76
5.3.1 Throughput 76
5.3.2 Utilization 76
5.3.3 Response Time 77
5.3.4 E[N(t)] 79
5.3.5 P[N] 82
5.4 INDEPENDENT SIMULATION RUNS 84
5.5 TRANSIENT AND STEADY PHASES 86
5.6 SUMMARY 91
5.7 EXERCISES 91

Chapter 6  Statistical Analysis of Simulated Data 93


6.1 POPULATIONS AND SAMPLES 93
6.2 PROBABILITY DISTRIBUTION OF THE SAMPLE MEAN 95
6.3 CONFIDENCE INTERVALS 97
6.3.1 Interpretations 100
6.3.2 Why Not Always Use a 99% Confidence Interval? 102
6.4 COMPARING TWO SYSTEM DESIGNS 104
6.5 SUMMARY 105
6.6 EXERCISES 105

Part II Managing Complexity

Chapter 7  Event Graphs 109


7.1 WHAT IS AN EVENT GRAPH? 109
7.2 EXAMPLES 111
xii  Contents

7.2.1 The Arrival Process 111


7.2.2 Single-Server Queueing System 112
7.2.3 Multiple-Server Queueing System 114
7.2.4 Single-Server Queueing System with a Limited
Queue Capacity 114
7.2.5 Single-Server Queuing System with Failure 115
7.2.6 Single-Server Queuing System with Reneging 116
7.2.7 Single-Server Queuing System with Balking 116
7.3 TRANSLATING EVENT GRAPHS INTO CODE 117
7.4 SUMMARY 120
7.5 EXERCISES 120

Chapter 8  Building Simulation Programs 123


8.1 TIME-DRIVEN SIMULATION 123
8.2 EVENT-DRIVEN SIMULATION 126
8.3 WRITING EVENT-DRIVEN SIMULATION PROGRAMS 127
8.4 PROGRAMMING ISSUES 134
8.4.1 Event Collision 134
8.4.2 Identifiers for Packets 134
8.4.3 Stopping Conditions for the Simulation Loop 134
8.5 SUMMARY 135
8.6 EXERCISES 135

Part III Problem-Solving

Chapter 9  The Monte Carlo Method 139


9.1 ESTIMATING THE VALUE OF π 139
9.2 NUMERICAL INTEGRATION 142
9.3 ESTIMATING A PROBABILITY 144
9.3.1 Buffon’s Needle Problem 144
9.3.2 Reliability 146
9.4 VARIANCE REDUCTION TECHNIQUES 149
9.4.1 Control Variates 149
9.4.2 Stratified Sampling 151
9.4.3 Antithetic Sampling 153
9.4.4 Dagger Sampling 156
9.4.5 Importance Sampling 158
Contents  xiii

9.5 SUMMARY 161


9.6 EXERCISES 161

Part IV Sources of Randomness

Chapter 10  Random Variate Generation 165


10.1 THE INVERSION METHOD 165
10.1.1 Continuous Random Variables 167
10.1.2 Discrete Random Variables 169
10.1.2.1 Generating a Bernoulli Variate 171
10.1.2.2 Generating a Binomial Variate 172
10.1.2.3 Generating a Geometric Variate 173
10.2 THE REJECTION METHOD 173
10.3 THE COMPOSITION METHOD 177
10.4 THE CONVOLUTION METHOD 179
10.5 SPECIALIZED METHODS 182
10.5.1 The Poisson Distribution 182
10.5.2 The Normal Distribution 184
10.6 SUMMARY 186
10.7 EXERCISES 186

Chapter 11  Random Number Generation 187


11.1 PSEUDO-RANDOM NUMBERS 187
11.2 CHARACTERISTICS OF A GOOD GENERATOR 189
11.3 JUST ENOUGH NUMBER THEORY 190
11.3.1 Prime Numbers 190
11.3.2 The Modulo Operation 190
11.3.3 Primitive Roots for a Prime Number 191
11.4 THE LINEAR CONGRUENTIAL METHOD 192
11.5 THE MULTIPLICATIVE CONGRUENTIAL METHOD 193
11.5.1 2k Modulus 193
11.5.2 Prime Modulus 194
11.6 LINEAR FEEDBACK SHIFT REGISTERS 194
11.7 STATISTICAL TESTING OF RNGs 199
11.7.1 The Chi-Squared Test 199
11.7.2 The Poker Test 201
xiv  Contents

11.7.3 The Spectral Test 202


11.7.4 The Lag Plot 204
11.8 SUMMARY 205
11.9 EXERCISES 205

Part V Case Studies

Chapter 12  Case Studies 209


12.1 NETWORK RELIABILITY 209
12.2 PACKET DELIVERY OVER A WIRELESS CHANNEL 218
12.3 SIMPLE ARQ PROTOCOL 226
12.4 SUMMARY 233
12.5 EXERCISES 233

Appendix A  Overview of Python 235


A.1 BASICS 235
A.2 INPUT AND OUTPUT 237
A.3 BITWISE OPERATORS 238
A.4 LISTS 239
A.5 LIST FUNCTIONS 240
A.6 GENERATING RANDOM NUMBERS AND RANDOM VARI-
ATES 241
A.7 IMPLEMENTING THE EVENT LIST 242
A.7.1 Priority Queue 242
A.7.2 Heap Queue 242
A.7.3 Sorting a List 243
A.8 PASSING A FUNCTION NAME AS AN ARGUMENT 244
A.9 TUPLES AS RECORDS 245
A.10 PLOTTING 245

Appendix B  An Object-Oriented Simulation Framework 251


Appendix C  The Chi-Squared Table 267
Appendix D  The t-Distribution Table 269

Bibliography 271
Index 273
List of Programs

3.1 Simulating the experiment of throwing a die. The output is


shown as a comment on each line. 31
3.2 Approximating the probability of an outcome in the experiment
of throwing a die. 31
3.3 Simulation program for studying the running mean of the ran-
dom experiment of tossing a coin. This program is also used to
generate Figure 3.4. 35
4.1 Python program for generating the histogram from an exponen-
tial data set (see Figure 4.6). 44
4.2 Python program for plotting the CDF and PDF of a uniform
random variable (see Figures 4.9(a) and 4.9(b)). 51
4.3 Simulating a two-state discrete-time Markov chain given its
probability transition matrix and an initial state. 63
4.4 Simulating a Poisson process. 64
4.5 Simulating a birth-death process and plotting its sample path
(see Figure 4.21). 66
5.1 Simulation program of the single-server queueing system. 71
5.2 Estimating the average response time of the system. 77
5.3 Estimating the average number of customers in the sytem
(E[N(t)]). 80
5.4 Estimating the steady-state probability distribution (P[N = k]). 82
5.5 Performing multiple independent simulation runs of the simula-
tion model of the single-server queueing system. 86
5.6 Determining a good trunction point using the average of several
realizations of an output variable. 90
6.1 Calculating the confidence interval using Python. 99
6.2 Plotting confidence intervals and population mean. 101
7.1 Python implementation of the event graph in Figure 7.4. 118
8.1 A time-driven simulation program for the discrete-time single-
server queueing system. 124

xv
xvi  Contents

8.2 An event-driven simulation program for the single-server queue-


ing system. 129
9.1 Python procedure for estimating π using MC simulation. 141
9.2 Python procedure for estimating a one-dimensional integral. 143
9.3 Python procedure for the Buffon’s needle experiment. 145
9.4 Estimating the reliability of the system in Figure 9.6(b). 148
9.5 Estimating an integral in Eqn. (9.19) using the method of con-
trol variates. 150
R 1 −x
9.6 Estimating the integral 0 e dx using the crude Monte Carlo
and stratified methods. 152
9.7 Estimating the mean of a uniform random variable using anti-
thetic sampling. 154
9.8 Estimating the value of the integral in Eqn. (9.25) using CMC
and antithetic sampling. The reduction in variance is about 12%. 155
9.9 Estimating the reliability of the system in Figure 9.6(b) using
dagger sampling. 157
9.10 Estimating the average of a function using importance sampling. 160
10.1 Generating random variates using the information in Figure
10.2(a). 170
10.2 Generating Bernoulli random variates. 171
10.3 Generating binomial random variates. 172
10.4 Generating geometric random variates. 173
10.5 Generating random variates based on the rejection method. 176
10.6 Generating an Erlang random variate using the convolution
method. 179
10.7 Generating a standard normal random variate using the convo-
lution method. 181
10.8 Generating a Poisson random variate. 183
10.9 Generating a random variate from a standard normal distribution. 185
11.1 Testing a set of random numbers if they are uniformly distributed. 188
11.2 Generating the maximum-length random sequence from the
four-bit LFSR shown in Figure 11.3. 197
11.3 Generating the maximum-length random sequence from an
eight-bit LFSR. 198
11.4 Python program for generating a 3D scatter plot for the spectral
test. 202
11.5 Python procedure for generating a lag plot for a random sequence. 204
12.1 Computing unreliability for the graph in Figure 12.2 using the
exact expression in Eqn. (12.1). 212
Contents  xvii

12.2 Computing unreliability for the graph in Figure 12.2 using crude
Monte Carlo simulation. 213
12.3 Computing unreliability for the graph in Figure 12.2 using strat-
ified sampling. 214
12.4 Computing unreliability for the graph in Figure 12.2 using an-
tithetic sampling. 215
12.5 Computing unreliability for the graph in Figure 12.2 using dag-
ger sampling. The number of samples is significantly less. 216
12.6 Python implementation of the event graph in Figure 12.4 220
12.7 Python implementation of the event graph of the simple stop-
and-wait ARQ protocol in Figure 12.8. 228
A.1.1 Starting a new Python interactive session. 235
A.1.2 Running a Python program from the command line. 236
A.1.3 A Python source file. It can also be referred to as a Python script. 236
A.2.1 Input and output functions. 237
A.3.1 Binary operations on integer numbers. 238
A.3.2 Handling unsigned binary numbers. 239
A.4.1 Lists and some of their operations. 239
A.5.1 Transposing a matrix using the zip function. Matrix is first un-
packed using the start (*) operator. 240
A.6.1 Importing the random module and calling some of the functions
inside it. 241
A.7.1 Implementing the event list using the queue module. 242
A.7.2 Implementing the event list using the hqueue module. 243
A.7.3 Implementing the event list by sorting a list. 243
A.8.1 The name of the function can be stored in a list and then used
to call the function. 244
A.8.2 The name of the function can be passed as an argument to
another function. 244
A.9.1 A tuple can be used as a record that represents an item in the
event list. 245
A.10.1 Code for generating Figure 4.12(b). 245
A.10.2 Code for generating Figure 10.6(a). 247
A.10.3 Code for generating Figure 10.6(b). 248
B.1 Event. 251
B.2 Simulation Entity. 252
B.3 Event list and scheduler. 252
B.4 Example 1. 254
xviii  Contents

B.5 Example 2. 255


B.6 Example 3. 256
B.7 Example 4. 258
B.8 M/M/1. 259
B.9 State. 262
B.10 State Machine. 263
B.11 Simple Protocol. 263
List of Figures

1.1 The three pillars of science and engineering: Observation (O),


Experimentation (E), and Computation (C). By analogy, the
table needs the three legs to stay up. 4
1.2 A queue at a checkout counter in a supermarket. A phenomenon
arising whenever there is a shared resource (i.e., the cashier) and
multiple users (i.e., the shoppers). 5
1.3 Types of models and the data generated from them. 6
1.4 Phases of a simulation study. 6

2.1 A mental image of the system and its behavior must be devel-
oped before a conceptual model can be constructed. 14
2.2 Different mental images can be developed for the same system.
They include different levels of details. Complexity increases as
you add more details. 14
2.3 A continuous state variable takes values from a continuous set
(e.g., [0, 5] in (a)). A discrete state variable, on the other hand,
takes values from a discrete set (e.g., {0, 1, 2, 3, 4, 5} in (b)). 16
2.4 Events are used to move dynamic entities through a system.
A packet is moved from a source to a destination through two
routers using eight events. 17
2.5 An activity is delimited by two events and lasts for a random
duration of time. 18
2.6 A queueing phenomenon emerges whenever there is a shared
resource and multiple users. 19
2.7 Conceptual model of the queueing situation in Figure 2.6. 20
2.8 A sample path of the state variable Q which represents the
number of persons in the single-server queueing system. Note
the difference in the time between every two consecutive arrival
events. 20
2.9 Four activities occur inside the single-server queueing system:
(a) Generation, (b) Waiting, (c) Service, and (d) Delay. The
length of each activity is a random variable of time. 21

xix
xx  LIST OF FIGURES

2.10 A simple electrical circuit and its state diagram. Only the switch
and lamp are modeled. Events are generated by the switch to
change the state of the lamp. 22
2.11 State diagrams of the state variables associated with the queue
and server in the single-server queueing system in Figure 2.7. A
portion of the state space of the system is shown in (c). 23

3.1 A random experiment of tossing a coin. There are two possible


outcomes. 28
3.2 A random experiment of throwing a die. There are six possible
outcomes. 28
3.3 Three different samples for the random experiment of tossing
a coin 10 times. The running mean is computed for the third
sample using cumulative sums. The last value at position 10
of the list of running means is equal to the sample mean. The
sample mean is the probability of seeing a head. 33
3.4 Running mean for the random experiment of tossing a coin. The
mean eventually converges to the true value as more samples are
generated. 34

4.1 Sample space for the random experiment of throwing two dice.
The outcome of the experiment is a random variable X ∈
{2, 3, ..., 12}. 40
4.2 The PMF of a discrete random variable representing the out-
come of the random experiment of throwing two dice. 41
4.3 The cumulative distribution function of a discrete random vari-
able representing the outcome of the random experiment of
throwing two dice. 42
4.4 Probability density function of a continuous random variable. 43
4.5 Elements of a histogram. Bins can be of different widths. Length
of a bar could represent frequency or relative frequency. 45
4.6 Histogram for an exponential data set. This figure is generated
using Listing 4.1. 46
4.7 The situation of observing four successes in a sequence of seven
Bernoulli trials can be modeled as a binomial random variable. 48
4.8 The PMF of the Poisson random variable for λ = 10. Notice
that P (x) approaches zero as x increases. 49
4.9 Probability distribution functions for the uniform random vari-
able where a = 3 and b = 10. 50
4.10 Probability distribution functions of the exponential random
variable where µ = 1.5. 53
LIST OF FIGURES  xxi

4.11 The PDF of the normal random variable with µ = 30 and σ =


10. 55
4.12 Probability distribution functions of the triangular random vari-
able with a = 1, b = 10, and c = 7. 56
4.13 A stochastic process maps each outcome in the sample space to
a time function. Time functions are combined (convoluted) to
produce two sample paths: g1 and g2 . Two kinds of means can
be defined for a stochastic process. 57
4.14 The Bernoulli random process: (a) sample space, (b) random
variable, (c) time functions, (d) result of running the random
experiment in each slot, and (e) final sample path. 58
4.15 A sample path through the state space of a dynamic system.
Entry and exit points are random. Data is generated along this
sample path and a time average is computed as an estimate of
the performance metric of interest. 59
4.16 A sample path through the state space of the single-server
queueing system. The initial state does not have to be (0, ‘F’). 59
4.17 A sample path of a discrete-time Markov chain over nine time
units. Events occur at integer times only. N(1) is the number of
entities in the system during the first time slot. 61
4.18 A sample path of a continuous-time Markov chain. Events occur
at random times. The time spent in a state has an exponential
distribution. 61
4.19 A graphical representation of a two-state, discrete-time Markov
chain. 62
4.20 Sample path of a Poisson process. Only arrival events occur
inside a Poisson process. 64
4.21 Sample path of a birth-death process. 65

5.1 Physical structure of the single-server queueing system. 70


5.2 Graphical representation of the relationship between random
variables and simulation events. 72
5.3 A sample path of the random process N (t). 72
5.4 Random processes present in the single-server queueing system.
Both the arrival and departure processes are Poisson processes.
(a) Places where the random processes are defined. (b) Total
number of arrivals which have occurred up to time t1 is three.
(c) The sample path of the departure process is a shifted version
of the sample path of the arrival process. (d) Sample path of
the queueing (birth-death) process which tracks the number of
packets in the system. 73
xxii  LIST OF FIGURES

5.5 A simulation experiment represents an execution of a simulation


model with a specific set of parameters, inputs, and outputs. 76
5.6 A sample path of the number of packets in the single-server
queueing system. There are eight rectangles under the curve of
the sample path. 80
5.7 Raw data generated when running a simulation program. 85
5.8 Cumulative average versus number of simulated packets. The
theoretical value is Wavg = 10. After the transient phase is over,
the cumulative average starts approaching the theoretical value. 87
5.9 Z is the average of the five output sequences Y[0]-Y[4]. A trun-
cation point can visually be determined by using the curve of
Z. In this example, a good truncation point is n = 3000. 89
5.10 The first two steps in the Welch’s method. In step 1, multiple
realizations of the output variable are generated. These realiza-
tions are combined into one sequence in step 2. 89
5.11 A two-server queueing system with a finite buffer of size three. 91

6.1 Population and samples for the simulation experiment of esti-


mating the delay through the single-server queueing system by
simulating five packets. The population is (0, ∞). 94
6.2 Probability distribution of the sample mean is normal. 95
6.3 Frequency distribution of the average delay D through the
single-server queueing system with λ = 1 and µ = 1.25. The
population mean is 4. 96
6.4 The empirical rule for the distribution of samples around the
population mean. 95% of the area under the curve of the normal
distribution lies within two standard deviations (equal to 1.96)
of the mean. 97
6.5 Two of the calculated confidence intervals do not include the
population mean. The population mean is 15. 101

7.1 Types of edges in event graphs. 110


7.2 State diagram (a) and event graph (b) for the Poisson arrival
process. 112
7.3 Event graph for the single-server queueing system. 113
7.4 Reduced event graph for the single-server queueing system. 113
7.5 Event graph for the K-server queueing system. 114
7.6 Event graph for the single-server queueing system with a limited
queue capacity. 114
7.7 Event graph for the single-server queueing system with a server
that fails. 115
LIST OF FIGURES  xxiii

7.8 Event graph for the single-server queueing system with reneging. 116
7.9 Event graph for the single-server queueing system with balking. 116
7.10 A template for synthesizing simulation programs from event
graphs. 121
7.11 Two parallel single-server queueing systems with one shared
traffic source. 122
7.12 A simple network setup where a user communicates with a server
in a data center over a communication channel created inside a
network. Propagation delay (Pd ) and rate (R) are two important
characteristics of a channel. 122

8.1 In time-driven simulation, simulated time evolves in increments


of size ∆t. 124
8.2 Arrival and departure processes and their random variables in
continuous- and discrete-time queues. 126
8.3 In event-driven simulation, simulated time evolves in steps of
random sizes (∆t1 6= ∆t2 ). 127
8.4 An event-driven simulation program has two independent com-
ponents: simulator and model. 128
8.5 How a random number u is used to generate an event. 128
8.6 A flowchart of the event-driven simulation program. 129
8.7 Two single-server queueing systems in series with external ar-
rivals. 136

9.1 Setup used for performing MC simulation to estimate π. 140


9.2 Setup used for performing MC simulation to estimate a one-
dimensional integral. 142
9.3 The goal of the Buffon’s needle experiment is to compute the
probability that a needle of length l will intersect a horizontal
line in a set of horizontal lines separated by a distance equal to d. 144
9.4 Two random variables (a and φ) are used in the simulation. The
needle will intersect with the closest horizontal line if b ≥ a. 145
9.5 According to trigonometry, the length of the line segment b is
equal to the value of the y-coordinate of the upper tip of the
needle. 146
9.6 Reliability is the probability that the input is connected to the
output. (a) The input is connected to the output if the swtich is
closed. (b) Reliability of the overall system is a function of the
reliabilities of the individual blocks. In this case, Relsys = R3 ,
where R is the reliability of a block. 147
xxiv  LIST OF FIGURES

9.7 Sample space of the random experiment of throwing two dice.


For the random variate 4, 4+10 2 = 7 is generated instead if anti-
thetic sampling is used. 154
9.8 With dagger sampling, three trials are performed using a single
random number. Hence, three samples are generated. 157
9.9 Values of g(x) are very close to zero over region 1. Probabil-
ity distribution of x is very close to zero over region 1. Another
probability distribution has to be used in order to generate sam-
ples from region 1 where the values of the function g(x) are more
interesting. 159
9.10 Estimating the shortest path between nodes A and D (see Ex-
ercise 9.2). 161

10.1 Generating random variates from cumulative distribution func-


tions. 166
10.2 Generating random variates using the PMF of a discrete random
variable. 170
10.3 Generating a random variate from the PDF f(x) using the aux-
iliary PDF g(x). x = 3 is accepted if u ≤ fg(3)
(3)
. 174
10.4 Random variates generated using the rejection method. 175
10.5 Triangular distribution. f (x) is composed of two functions each
of which is defined over half the interval over which f (x) exists. 177
10.6 The shape of the histogram constructed using the random vari-
ates generated using the convolution method resembles that of
the PDF of the Erlang random variable. 180
10.7 Histogram constructed from standard normal variates generated
using the convolution method in Listing 10.7. 181
10.8 Arrivals during a time slot can be modeled as a Poisson random
variable. 183
10.9 The shape of the histogram constructed using simulated Poisson
random variates resembles that of the PMF of a Poisson random
variable. 184
10.10 Using the procedure in Listing 10.9, the uniform random num-
bers are transformed into random standard normal variates. 185

11.1 Probability distribution of u. 188


11.2 Multiple seeds are used to make different simulation runs. Differ-
ent paths in the system state space are explored and the average
is computed using the values resulting from these different paths. 193
11.3 A four-bit linear feedback shift register with characteristic poly-
nomial c(x) = 1 + x3 + x4 . 195
LIST OF FIGURES  xxv

11.4 Computing the first intermediate binary number on line 13 in


Listing 11.2. 196
11.5 An eight-bit linear feedback shift register with characteristic
polynomial c(x) = 1 + x4 + x5 + x6 + x8 . 197
11.6 104 triplets of successive random numbers generated using List-
ing 11.4. Planes can be seen when the figure is viewed from the
right angle. 202
11.7 The lag plot for a sequence of sinusoidal values. An elliptical
pattern can be clearly identified. 204
11.8 The lag plot generated by the code in Listing 11.5. The sequence
uniformly fills the 2D space. 205

12.1 A graph consisting of eight vertices and 11 edges. 210


12.2 Network fails if nodes v1 and v4 become disconnected. The
event will occur if any of the following groups of links fail:
{(e1 , e2 ), (e1 , e3 ), (e2 , e4 ), (e3 , e4 )}. 210
12.3 A point-to-point wireless system. The transmitter has a buffer
which can store up to B packets. The probability that a trans-
mission attempt is successful is 1 − Perr . 218
12.4 Event graph for the system in Figure 12.3. 220
12.5 Average packet delay increases as the quality of the wireless
channel degrades. 225
12.6 Percentage of delivered packets drops as the quality of the wire-
less channel degrades. 226
12.7 Behavior of the simple stop-and-wait ARQ protocol with two
possibilities: acknowledgment and frame loss. 227
12.8 Event graph for the simple stop-and-wait ARQ protocol. 228
12.9 Throughput deteriorates as the packet error rate increases. 232
List of Tables

1.1 Description of the phases of a simulation study of the system in


Figure 1.2. 8

2.1 Details of the conceptual model of the queueing situation in


Figure 2.6. 19

5.1 Manual simulation of the single-server queueing system using a


simulation table. 74
5.2 IATs and STs for Exercise 5.1. 91

6.1 Notation for the sample and population statistics. 95

7.1 Event table for the event graph in Figure 7.4. 118

8.1 Mapping concepts to code in Listing 8.2. 130

9.1 Sample space of the system in Figure 9.6(b) with probability of


each sample point. 148

11.1 Random variates are repeated after a cycle of size 3. This is


due to the repetition in the generated sequence of the random
numbers. 190
11.2 Maximum-length sequence of random numbers generated by the
LFSR in Figure 11.3. 196
11.3 Types of five-digit numbers according to the poker rules. 201

12.1 Sample space of the system in Figure 12.2 along with the status
of the network for each possible system state. 211
12.2 Restructuring the sample space of the system in Figure 12.2
along with the probability of each stratum. The first row indi-
cates the number of UP links. 212
12.3 State variables of the event graph in Figure 12.4. 219

xxvii
Foreword

Computer simulation is an effective and popular universal tool. It can be


applied to almost all disciplines. Typically, simulation involves two key steps:
modeling and implementation, which are highlighted in this book. Modeling
can performed using event graphs, which are revived by this book. As for
implementation, complete Python programs are given, which is considered a
new effort. This is an interesting combination as the translation process from
models to programs is straightforward.
The book also dedicates a complete chapter on the popular Monte Carlo
simulation method. This chapter covers several variance-reduction techniques
along with their implementation in Python. Three interesting case studies are
discussed in detail. The book features good examples and exercises for readers
and students.
This book is highly recommended for a graduate course in modeling and
simulation. It is also recommended for an introductory course in modeling and
simulation for a senior undergraduate course. In addition, it can be a good
reference for researchers and working engineers and scientists who work on
modeling and simulation and optimization. This book is a good addition to
the field of modeling and simulation. I hope you will enjoy the book as much
as I have enjoyed reviewing it.

Mohammad S. Obaidat, Fellow of IEEE and Fellow of SCS


Past President of the Society for Modeling and Simulation International,
SCS
Founding Editor in Chief, Security and Privacy Journal, Wiley
Editor-in-Chief, International Journal of Communication Systems
June 2017

xxix
Preface

This book is not just another book on discrete-event simulation. It empha-


sizes modeling and programming without sacrificing mathematical rigor. The
book will be of great interest to senior undergraduate and starting graduate
students in the fields of computer science and engineering and industrial engi-
neering. The book is designed for a one-semester graduate course on computer
simulation. Each chapter can be covered in about one week. The instructor is
also encouraged to dedicate one week for learning the Python programming
language. Appendix A can be used for this purpose. A basic knowledge of
programming, mathematics, statistics, and probability theory is required to
understand this book.
The book has the following features. First, a simulation program is clearly
divided into two parts: simulator and model. In this way, implementation
details based on a specific programming language will not coexist with the
modeling techniques in the same chapter. As a result, student confusion is
minimized. The second feature of the book is the use of the Python pro-
gramming language. Python is becoming the tool of choice for scientists and
engineers due to its short learning curve and many open-source libraries. In
addition, Python has a REPL1 which makes experimentation much faster.
The third feature is the use of event graphs for building simulation models.
This formalism will aid students in mastering the important skill of simulation
modeling. A complete chapter is dedicated to it. The book also features a com-
plete chapter on the Monte Carlo method and variance-reduction techniques.
Several examples are given along with complete programs.
The book is divided into four parts. The first part represents a complete
course on the fundamentals of discrete-event simulation. It is comprised of
chapters 1 to 6. This first part is appropriate for an undergraduate course
on discrete-event simulation. Materials from other chapters can be added to
this course. For example, chapter 10 and 11 should be covered in full if time
permits. For an advanced course on computer simulation, the second and third
part should be fully covered. The case studies in the fourth part can be covered
if time permits. In such a course, the emphasis should be on model building
and programming.
1 REPL = Read-Evaluate-Print Loop

xxxi
xxxii  Preface

To the Reader
While writing this book, I had assumed that nothing is obvious. Hence, all
the necessary details that you may need are included in the book. However,
you can always skip ahead and return to what you skip if something is not
clear. Also, note that throughout this book, “he” is used to to refer to both
genders. I find the use of “he or she” disruptive and awkward. Finally, the
source code is deliberately inefficient and serves only as an illustration of the
mathematical calculation. Use it at your own risk.

Website
The author maintains a website for the book. The address is http:
//faculty.kfupm.edu.sa/coe/yosais/simbook. Presentations, pro-
grams, and other materials can be downloaded from this website. A code
repository is also available on Github at https://github.com/yosais/
Computer-Simulation-Book.

Acknowledgments
I would like to thank all the graduate students who took the course with
me while developing the material of the book between 2012 and 2017. Their
understanding and enthusiasm were very helpful.
I would also like to thank King Fahd University of Petroleum and Minerals
(KFUPM) for financially supporting the writing of this book through project
number BW151001.
Last but not least, I would like to thank my wife for her understanding
and extra patience.

Yahya Osais
Dhahran, Saudi Arabia
2017
About the Author

Yahya E. Osais is a faculty member in the department of computer engineer-


ing at King Fahd University of Petroleum and Minerals (KFUPM), Dhahran,
Saudi Arabia. He earned his B.Sc. and M.Sc. from the same department in
2000 and 2003, respectively. In 2010, he obtained his Ph.D. from the depart-
ment of systems and computer engineering at Carleton University, Ontario,
Canada.
Dr. Osais regularly teaches a graduate course in computer simulation for
students in the college of computer science and engineering at KFUPM. He
also teaches courses on computer engineering design and web technologies. His
current research interest includes stochastic modeling and simulation, cyber-
physical systems, and the Internet of things.

xxxiii
Abbreviations

RV Random Variable
CDF Cumulative Distribution Function
iCDF Inverse CDF
PDF Probability Distribution Function
PMF Probability Mass Function
BD Birth-Death
LFSR Linear Feedback Shift Registers
RNG Random Number Generator
RVG Random Variate Generator
REG Random Event Generator
IID Independent and Identically Distributed

xxxv
Symbols

Variable that are used only in specific chapters are explained directly at their
occurrence and are not mentioned here.

λ Average Arrival Rate Pronounced as “lambda”


µ Average Service Rate Pronounced as “meu”

IAT Average Inter-Arrival Time


between any Two Consecutive
Packets IAT = λ1
ST Average Service Time of a Packet ST = µ1

T Total Simulation Time


clock Current Simulation Time

IATi Time until the arrival of Packet i


STi Service Time of Packet i
Ai Arrival Time of Packet i Ai = clock + IATi
Di Departure Time of Packet i Di = clock + STi

µ Population Mean
σ2 Population Variance
σ Population Standard Deviation

x̄ Sample Mean
s2 Sample Variance
s Sample Standard Deviation

xxxvii
I
The Fundamentals
CHAPTER 1

Introduction

“The purpose of computing is insight, not numbers.”


−Richard Hamming

The purpose of this chapter is to motivate the importance of simulation as


a scientific tool. The chapter also introduces some essential concepts which are
needed in the rest of the book. The lifecycle of a simulation study is described
here along with an example. In addition, the advantages and limitations of
simulation are discussed. The reader is urged to carefully read this chapter
before moving on to the next ones.

1.1 THE PILLARS OF SCIENCE AND ENGINEERING


Science and engineering are based on three pillars: observation, experimenta-
tion, and computation. Figure 1.1 uses the analogy of a table with three legs
to show the relationship between these three tools and science and engineer-
ing. Historically, humans have been using observation and experimentation
to acquire new knowledge (i.e., science) and then apply the newly acquired
knowledge to solve problems (i.e., engineering). This approach is very effective
because the actual phenomenon (system) is observed (utilized). However, as
the complexity increases, observation and experimentation become very costly
and cumbersome. This is when computation becomes the only tool that can
be used.
The outcome of an observational study is a set of facts. For example, if
a burning candle is covered with a glass cup, it will eventually go out on its
own. This is the observation. Scientists had to do research before they could
realize the reason for this phenomenon. The reason is that there is still oxygen
inside the glass cup which will eventually be used up by the flame. Once all
the oxygen is consumed, the candle goes out.
On the other hand, experimentation is the act of making an experiment.

3
4  Computer Simulation: A Foundational Approach Using Python

Science & Engineering

E
O C

Figure 1.1
The three pillars of science and engineering: Observation (O), Experimenta-
tion (E), and Computation (C). By analogy, the table needs the three legs to
stay up.

An experiment is a physical setup. It is performed to make measurements.


Measurements are raw data. Experimentation is popular among scientists.
The output of the system is recorded as it occurs in an observational
study. Furthermore, the response of the system is not influenced in any way
and the environment in which the system operates cannot be manipulated. In
experimentation, however, we can manipulate the environment in which the
system operates and influence the response of the system.
A computation is a representation of the phenomenon or system under
study in the form of a computer program. This representation can be as sim-
ple as a single mathematical equation or as complex as a program with a
million lines of code. For mathematical equations, there are tools like calculus
and queueing theory that can be used to obtain closed-form solutions. If a
closed-form solution, on the other hand, cannot be obtained, approximation
techniques can be used. If even an approximate solution cannot be obtained
analytically, then computation has to be used.
In this book, we are interested in the use of computation as a tool for
understanding the behavior of systems under different conditions. This goal
is achieved by generating time-stamped data which is then statistically ana-
lyzed to produce performance summaries, like means and variances. The type
of computation performed by the program which generates this type of data
is referred to as event-oriented simulation. Developing such simulation pro-
grams is an art. The good news is that you can acquire this skill by practice.
Therefore, it is recommended that you carefully study the examples in the
book.

1.2 STUDYING THE QUEUEING PHENOMENON


Consider the situation in Figure 1.2 where five people have to wait in a queue
at the checkout counter in a supermarket. This situation arises because there
is only one cashier and more than one person wants to have access to him.
This phenomenon is referred to as queueing. Let us see how observation, ex-
perimentation, and computation can be used to study this phenomenon.
Introduction  5

Waiting Line (Queue) Checkout Counter (Server)

Figure 1.2
A queue at a checkout counter in a supermarket. A phenomenon arising when-
ever there is a shared resource (i.e., the cashier) and multiple users (i.e., the
shoppers).

If we want, for example, to estimate the average time a customer spends


at the checkout counter, we should manually record the time each customer
spends waiting to be served plus the service time. Therefore, for each customer,
we have to keep track of two times: (1) time customer joins the queue (arrival
time) and (2) time customer leaves the system (departure time) . As shown in
Figure 1.2, the system is made up of the waiting line and checkout counter.
Clearly, performing this observational study is costly and cumbersome.
For example, we can control the maximum number of people who should
wait in the queue to shorten the time of the experiment. We can also introduce
a policy that only customers with no more than seven items can join the
queue. We can also ask customers to help us in collecting the necessary data.
For example, each customer can be asked to record the times at which he joins
and leaves the queue. This will surely reduce the cost of the experiment.
A study based solely on computation (i.e., simulation) is significantly less
costly. It requires only developing a computer program based on a sound
model of the situation under study. A computational approach is also the
most flexible one since it gives us a full control over both the environment and
system.

1.3 WHAT IS SIMULATION?


Simulation is the process of representing a system1 by a model and then
executing this model to generate raw data. The raw data is not useful by itself.
It must be statistically processed to produce insights about the performance
of the system. These four activities are represented by four gray boxes in
Figure 1.4. They are part of a larger framework of activities that constitute
1 Fromnow on, we are going to use the word “system” as a synonym for the words “phe-
nomenon” and “situation.”
6  Computer Simulation: A Foundational Approach Using Python

Mathematical Theoretical
Model Data

Simulation
Simulation
(Synthetic)
Model
Data

Measurements
Physical Model
(Actual Data)

Figure 1.3
Types of models and the data generated from them.

1 2 3
Formal
Problem System Model Description

Problem Statement Mental Image

6 5 4
Performance Statistical Computer
Summary Analysis Program

Report Statistics Python Program

Figure 1.4
Phases of a simulation study.

the elements of a simulation study. More about this will be said in the next
section.
The model is a conceptual representation of the system. It represents a
modeler’s understanding of the system and how it works. A computer is used
to execute the model. Therefore, the model must first be translated into a
computer program using a programming language like Python.2 The execution
of the computer program results in the raw data.
The raw data is also referred to as simulation data. It is synthetic because it
is not actual data. Actual data is collected from a physical model of the system.
There is another type of data called theoretical data which is generated from
a mathematical model of the system. Figure 1.3 shows these types of models
and the types of data generated from them.

1.4 LIFECYCLE OF A SIMULATION STUDY


Simulation is just a tool. When you study a system using simulation, you
conduct a simulation study. As Figure 1.4 shows, there are six phases in a
simulation study. In the first phase, a detailed description of the problem you
2 https://www.python.org.
Introduction  7

want to solve using simulation is developed. A good problem description helps


in defining the boundaries of the system, scope of the study, and guide the
model development. The problem description should include a set of questions
and objectives. Also, it should include a description of the assumptions and
performance metrics which will be used in making decisions in the last phase of
the study. The raw data which is needed to compute the performance metrics
must also be clearly defined. A more formal name for the outcome of this
phase is problem statement.
The system and environment in which it operates should be described in
the second phase. Only the details which are relevant to the simulation study
should be captured. The skill of abstraction, which is discussed in Chapter
2, becomes very handy in this stage. The outcome of this phase is a mental
image of the system and its operation. A mental image of a system represents
one’s understanding of how the system works.
The third phase is about developing a conceptual model of the system.
Using elementary concepts, which is covered in Chapter 2 and the formal
language of event graphs covered in Chapter 6, a model of the system can be
developed. Modeling is still an art and not a science. Thus, it is recommended
that you carry out this phase iteratively. That is, you start with a simple
model and you keep improving it until you are confident that your model
captures the actual system you intend to study. The outcome of this phase is
a formal description of the system.
In the fourth phase, the model is encoded in a computer language. In this
book, we use the Python programming language for this purpose. Translating
a model into a computer program is not an easy task. You also need to write
your code in a certain way to ease collection of the data necessary for the next
phase. In order to succeed in this phase, you need to familiarize yourself with
the programming language and its capabilities. In this book, we cover Python
in sufficient depth. Many implementation examples are given throughout the
book. The outcome of this phase is a Python implementation of the model.
The result of executing the model is a set of raw data which should be
statistically analyzed. Statistics like the mean, variance, and confidence inter-
vals should be derived in this phase. These statistics are only approximations
of the values of the performance metrics stated in the problem statement.
Their reliability depends on quality and size of the raw data collected in the
previous phase. You may need to execute the model several times and/or for
a longer time to get a good data set. You will also have to make sure your
data set is IID. These details will be discussed further in Chapter 11. The
outcome of this phase is a set of statistics which summarize the performance
of the system.
Finally, in the last phase, a summary in the form of a report must be
prepared. This report should include answers for the questions stated in the
problem statement. The simulation study is a failure if the report does not
achieve the intended objectives declared in the problem statement. The report
should also include a set of conclusions and recommendations based on the
8  Computer Simulation: A Foundational Approach Using Python

Table 1.1
Description of the phases of a simulation study of the system in Figure 1.2.

No. Phase Description


1 Problem Customers experience delays longer
than 5 minutes. The checkout process
has to be speeded up. Potential solu-
tions include changing the cashier and
installing a new software system. The
raw data to be collected include the de-
lay experienced by each customer i (Di )
which is defined as the difference be-
tween his departure time (Di ) and ar-
rival time (Ai ).
2 System Customers, waiting line, and cashier.
3 Model A customer arrives at the system. If the
cashier is free, he will be served immedi-
ately. Otherwise, he has to wait. Service
time of each customer is random.
4 Computer Program Model is expressed in Python code.
5 Statistical Analysis Response time of the system (i.e., the
PN
D
average delay). Tavg = iN i , where
N is the number of participating cus-
tomers.
6 Performance Summary Response time for each possible solu-
tion. Pick the one that gives the best
response time as the optimal solution.

statistical analysis of the simulation data. A conclusion which sums up the


evidence for the decision maker must be given. The summary report is the
outcome of this phase and the overall study.
Table 1.1 contains the details of a simulation study of the situation in
Figure 1.2. The goal of the study is to find the best solution from a set of
suggested solutions for speeding up the checkout process. The system has been
defined and a conceptual model has been developed as explained in the table.
Only one performance metric is used in this study, which is the average delay
through the system. The raw data needed to compute this metric has been
decided in the problem statement. The performance summary will include the
delay introduced by each possible solution. The management will pick the one
that causes the least delay if it can be afforded, of course.
Introduction  9

1.5 ADVANTAGES AND LIMITATIONS OF SIMULATION


As the complexity of the system under study increases, analytical tools may
fail to capture the details that are of interest to the modeler. Even for simple
systems, simulation can sometimes provide very insightful information. The
following are some of the reasons why we need simulation.

1. There is no need to build the physical system under study and then
observe it. Thus, knowledge about the behavior of the system can be
acquired with a minimum cost.
2. Critical scenarios can be investigated through simulation with less cost
and no risk.

3. Using a simulation model, the effect of changing values of system vari-


ables can be studied with no interruption to the physical system.
4. Simulation is more flexible and convenient than mathematical analysis.
Also, the modeler can avoid the hassle of dealing with mathematical
equations.
5. In simulation, there is no need for simplifying assumptions like in math-
ematical models where such assumptions are needed to make the models
tractable.
6. Simulation allows us to compress and expand the behavior of the sys-
tem under study. For example, several years’ worth of system evolution
can be studied in a few minutes of computer time. Also, one second of
simulation time can be expanded to several hours of computer time.
Like any other tool, simulation has limitations. The following are some of
them:

1. The outcome of a simulation study is an estimate subject to a statistical


error. For example, different simulation runs typically produce different
numbers although the same simulation model is used.
2. Simulation can become costly and time consuming. For example, very
powerful computers and skillful people are required.
3. Simulation models are not easy to develop. Existing methodologies are
not universal. This is why development of simulation models is still an
art, not a science.
4. Existing programming languages are not designed to support simulation.
Thus, a lot of programming is involved.
10  Computer Simulation: A Foundational Approach Using Python

1.6 OVERVIEW OF THE BOOK


The rest of the book is structured as follows:

Chapter 2 is about building conceptual models. These conceptual mod-


els are what we call simulation models. Also, in this chapter, state di-
agrams are introduced as a tool for describing the dynamic behavior
of systems. The main example used in this chapter is the single-server
queueing system, which also serves as our running example throughout
the book.
Chapter 3 is a review of probability using a computational approach.
In this chapter, the reader is exposed to the Python programming lan-
guage for the first time in the book. Therefore, the reader is strongly
encouraged to go through Appendix A thoroughly.

Chapter 4 is a review of random variables and stochastic processes


using also a computational approach. In this chapter, the queueing phe-
nomenon is discussed again. Also, the notion of a state space of a dy-
namic system is explained.
Chapter 5 discusses the simplest queueing system which is the single-
server, single-queue system. In this chapter, some basic performance laws
are introduced. Manual simulation is also covered in this chapter.
Chapter 6 is about the collection and statistical analysis of data that
results from executing the simulation model. The notion of an output
variable as a mechanism for collecting data is introduced in this chapter.
In addition, all the necessary statistical concepts such as point estimates
and confidence intervals are discussed in sufficient detail. The method
of independent replications and how to deal with the bias due to the
warm-up period are also discussed.
Chapter 7 is about modeling using event graphs. This is a very im-
portant intermediate step that helps the reader to develop his modeling
skills. An event graph shows how events interact inside a simulation
model.
Chapter 8 explains the difference between time-driven and event-driven
simulation. It also describes in detail how an event-driven simulation
program is constructed. All the necessary concepts and language features
are covered. Complete programs are shown and discussed in depth.
Chapter 9 covers the Monte Carlo method. This method is used for
solving problems that do not require a full-blown simulation model. Di-
verse examples are used to demonstrate the practicality of the method.
Further, the notion of variance reduction is introduced and several tech-
niques are discussed.
Introduction  11

Chapter 10 is about generating random numbers from non-uniform


probability distributions. Such numbers are referred to as random vari-
ates. These numbers are used to represent the lifetimes of random phe-
nomena that occur inside the simulation model. Examples of such ran-
dom phenomena are the time until the next failure or departure.
Chapter 11 is about generating random numbers from a uniform prob-
ability distribution over the interval (0, 1). This procedure is the source
of randomness in the simulation program. It drives the process of gen-
erating random variates. Several tests for randomness are covered to
ensure the quality of the generated random numbers.
Chapter 12 contains several case studies. The purpose of these case
studies is to show how the concepts and skills explained throughout the
book can be applied. Each case study represents a complete simulation
study.
Four appendices are added to complement the core material of the
book. Appendix A serves as an introduction to the Python program-
ming language. Appendix B describes an object-oriented framework
for discrete-event simulation. The object-oriented paradigm is very pop-
ular among software developers. This is because it enables code reuse
and easy code maintenance. Finally, appendices C and D both contain
standard statistical tables.

1.7 SUMMARY
Simulation is a tool that can be used for performing scientific studies. It may
not be the first choice. But, it is definitely the last resort if a physical or
mathematical model of the system under study cannot be constructed. The
main challenge in simulation is developing a sound model of the system and
translating this model to an efficient computer program. In this book, you will
learn the skills that will help you to overcome this challenge.
Other documents randomly have
different content
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
back
Welcome to our website – the perfect destination for book lovers and
knowledge seekers. We believe that every book holds a new world,
offering opportunities for learning, discovery, and personal growth.
That’s why we are dedicated to bringing you a diverse collection of
books, ranging from classic literature and specialized publications to
self-development guides and children's books.

More than just a book-buying platform, we strive to be a bridge


connecting you with timeless cultural and intellectual values. With an
elegant, user-friendly interface and a smart search system, you can
quickly find the books that best suit your interests. Additionally,
our special promotions and home delivery services help you save time
and fully enjoy the joy of reading.

Join us on a journey of knowledge exploration, passion nurturing, and


personal growth every day!

ebookbell.com

You might also like