Computer Simulation Techniques: The Definitive Introduction!
Computer Simulation Techniques: The Definitive Introduction!
CPU
2 3
3 7
b
4 5 6
9 6 9
Yes Is CPU No
MCL = tarr queue MCL = MCL + 1
empty?
b
Is
MCL = tarr b
?
Yes
A new arrival
event occurs
i
Harry Perros
Simulation Techniques Harry Perros
ii
Harry Perros Simulation Techniques
Harry Perros
iii
Simulation Techniques Harry Perros
All rights reserved. This book or any portion thereof may not be reproduced or used in any
manner whatsoever without the express written permission of the author except for the use of
brief quotations in a book review or scholarly journal.
iv
Harry Perros Simulation Techniques
To Isabelle
v
Table of Contents
FORWARD ....................................................................................................................... XI
AUTHOR ........................................................................................................................ XIII
CHAPTER 1: INTRODUCTION ...................................................................................... 1
1.1. INTRODUCTION ....................................................................................................................... 1
1.2. BUILDING A SIMULATION MODEL ......................................................................................... 2
1.4. A TOKEN-BASED ACCESS SCHEME ......................................................................................... 9
1.5. A TWO-STAGE MANUFACTURING SYSTEM ........................................................................ 15
PROBLEMS ..................................................................................................................................... 21
COMPUTER PROJECTS .................................................................................................................. 22
CHAPTER 2: GENERATION OF PSEUDO-RANDOM NUMBERS ........................ 25
2.1. INTRODUCTION .................................................................................................................... 25
2.2. PSEUDO-RANDOM NUMBERS .............................................................................................. 26
2.3. THE CONGRUENTIAL METHOD ........................................................................................... 28
2.3.1. GENERAL CONGRUENTIAL METHODS ............................................................................ 30
2.3.2. COMPOSITE GENERATORS ............................................................................................... 30
2.4. TAUSWORTHE GENERATORS .............................................................................................. 31
2.5. THE LAGGED FIBONACCI GENERATORS ............................................................................ 31
2.6. STATISTICAL TESTS FOR PSEUDO-RANDOM NUMBER GENERATORS ............................. 33
2.6.1. HYPOTHESIS TESTING ...................................................................................................... 33
2.6.2. FREQUENCY TEST (MONOBIT TEST) .............................................................................. 40
2.6.3 SERIAL TEST ...................................................................................................................... 41
2.6.4. RUNS TEST ......................................................................................................................... 44
2.6.5. CHI-SQUARED TEST FOR GOODNESS OF FIT .................................................................. 44
PROBLEMS ..................................................................................................................................... 45
COMPUTER PROJECTS .................................................................................................................. 45
CHAPTER 3: GENERATING STOCHASTIC VARIATES ......................................... 47
3.1. INTRODUCTION .................................................................................................................... 47
3.2. THE INVERSE TRANSFORMATION METHOD ...................................................................... 47
3.3. SAMPLING FROM CONTINUOUS-TIME PROBABILITY DISTRIBUTIONS ........................... 50
3.3.1. SAMPLING FROM A UNIFORM DISTRIBUTION ............................................................... 50
3.3.2. SAMPLING FROM AN EXPONENTIAL DISTRIBUTION ..................................................... 51
3.3.3. SAMPLING FROM AN ERLANG DISTRIBUTION ............................................................... 52
3.3.4. SAMPLING FROM A NORMAL DISTRIBUTION ................................................................. 53
3.4. SAMPLING FROM DISCRETE-TIME PROBABILITY DISTRIBUTIONS ................................. 55
3.4.1. GENERATING BINOMIAL DISTRIBUTED STOCHASTIC VARIATES ................................. 55
3.4.2. GENERATING GEOMETRICALLY DISTRIBUTED STOCHASTIC VARIATES ..................... 55
3.4.3. GENERATING POISSON DISTRIBUTED STOCHASTIC VARIATES ................................... 56
3.5. GENERATING STOCHASTIC VARIATES FROM AN EMPIRICAL DISTRIBUTION ................ 58
3.6. THE REJECTION METHOD .................................................................................................... 60
vii
Simulation Techniques Harry Perros
COMPUTER PROJECTS ................................................................................................................... 61
CHAPTER 4: SIMULATION DESIGNS ....................................................................... 63
4.1. INTRODUCTION ..................................................................................................................... 63
4.2. EVENT-ADVANCE DESIGN .................................................................................................... 63
4.3. FUTURE EVENT LIST ............................................................................................................ 64
4.4. EVENT LIST STORED IN A SEQUENTIAL ARRAY ................................................................. 65
4.5. EVENT LIST STORED IN A LINKED LIST .............................................................................. 66
4.5.1. DEFINING A LINKED LIST ................................................................................................. 68
4.5.2. CREATION OF A NEW NODE ............................................................................................. 69
4.5.3. DELETION OF A NODE ....................................................................................................... 70
4.5.4. INSERTING A NODE IN A LINKED LIST ............................................................................. 70
4.5.5. REMOVING THE FIRST NODE OF A LINKED LIST ............................................................ 73
4.5.6. TIME COMPLEXITY ............................................................................................................ 74
4.5.7. DOUBY LINKED LISTS ....................................................................................................... 75
4.6. UNIT-TIME ADVANCE DESIGN ............................................................................................. 75
4.6.1. SELECTING A UNIT TIME .................................................................................................. 79
4.6.2. IMPLEMENTATION ............................................................................................................ 80
4.6.3. EVENT-ADVANCE VS. UNIT-TIME ADVANCE .................................................................. 80
4.7. ACTIVITY-BASED SIMULATION DESIGN ............................................................................. 80
4.8. EXAMPLES ............................................................................................................................. 83
4.8.1. AN INVENTORY SYSTEM ................................................................................................... 83
4.8.2. A ROUND-ROBIN QUEUE .................................................................................................. 85
PROBLEMS ..................................................................................................................................... 90
COMPUTER PROJECTS ................................................................................................................... 91
CHAPTER 5: ESTIMATION TECHNIQUES FOR ANALYZING
ENDOGENOUSLY CREATED DATA ........................................................................... 93
5.1. INTRODUCTION ..................................................................................................................... 93
5.2. COLLECTING ENDOGENOUSLY CREATED DATA ................................................................. 93
5.3. TRANSIENT STATE VS STEADY-STATE SIMULATION ........................................................ 96
5.3.1. TRANSIENT-STATE SIMULATION .................................................................................... 96
5.3.2. STEADY-STATE SIMULATION ........................................................................................... 96
5.4. ESTIMATION TECHNIQUES FOR STEADY-STATE SIMULATION ........................................ 97
5.5. ESTIMATION OF THE CONFIDENCE INTERVAL OF THE MEAN ......................................... 98
5.5.1. ESTIMATION OF THE AUTOCORRELATION FUNCTION (ACF) .................................. 103
5.5.2. THE BATCH MEANS METHOD ........................................................................................ 106
5.5.3. THE REPLICATION METHOD ......................................................................................... 107
5.6. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PERCENTILE ................................ 109
5.7. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PROBABILITY .............................. 110
5.8. ESTIMATION TECHNIQUES FOR TRANSIENT-STATE SIMULATION .............................. 111
COMPUTER PROJECT .................................................................................................................. 112
CHAPTER 6: VALIDATION OF A SIMULATION MODEL .................................. 121
COMPUTER PROJECTS ................................................................................................................ 123
viii
Harry Perros Simulation Techniques
CHAPTER 7: VARIANCE REDUCTION TECHNIQUES ........................................ 127
7.1. INTRODUCTION ................................................................................................................. 127
7.2. THE ANTITHETIC VARIATES TECHNIQUE ....................................................................... 128
7.3. THE CONTROL VARIATES TECHNIQUE ............................................................................ 133
COMPUTER ASSIGNMENTS ....................................................................................................... 136
CHAPTER 8: SIMULATION PROJECTS .................................................................. 137
8.1. A SIMULATION OF A CPU PRIORITY SCHEDULER ......................................................... 137
8.1.1. THE SIMULATION LOGIC ............................................................................................... 139
8.1.2. TASK 1: HAND SIMULATION ........................................................................................ 143
8.1.3. TASK 2: WRITE THE BASIC SIMULATION CODE ......................................................... 145
8.1.4. TASK 3: STATISTICAL ESTIMATION OF THE RESPONSE TIME ................................. 146
8.1.5. CORRECT SIMULATION RESULTS ................................................................................. 147
8.2. A SIMULATION OF THE FLOW OF SIP MESSAGES .......................................................... 150
8.2.1. THE SIMULATION LOGIC .............................................................................................. 151
8.2.2. IMPLEMENTATION AND RESULTS ............................................................................... 153
8.3. A SIMULATION MODEL OF A MULTIPLEXER ................................................................... 155
8.3.1. SIMULATION LOGIC ....................................................................................................... 156
8.3.2. RESULTS ......................................................................................................................... 157
8.3.3. AN EXTENSION OF THE SIMULATION MODEL ............................................................. 158
ix
Forward
This book is based on my lecture notes for an undergraduate course
on discrete-event simulation techniques that I used to teach back in
the early 80s. In the early 90s I prepared the first version of this book
and made it available through my web site free of charge to anyone
interested in discrete-event simulation. In a light-hearted moment over
a few beers, I decided to call it the “definitive introduction!”. I am not
sure how “definitive” it is, but the title stuck!
Over the years I have received many positive comments despite errors
and other issues that resulted from not putting enough time into the
writing of the book. In this version, I reformatted the manuscript, re-
moved errors and improved the flow of the presentation in many sec-
tions. I also removed some advanced topics on data structures and
random number generation methods, as I want to keep the book at an
introductory level.
I would like to emphasize that this book was written for people from
all disciplines that use discrete-event simulation. As such, I describe
topics that maybe well-known to computer science students. The
knowledgeable reader can skip these sections.
How to use the book: This is a self-study book. In Chapter 1 you will
find three examples. Select one of the three and do the corresponding
computer project given at the end of the Chapter. Then, after you read
each new Chapter, do the computer project that corresponds to the
example that you have selected, described at the end of the Chapter.
By the time you reach the end of the book, you will have developed a
very sophisticated simulation model! You can use any high-level pro-
gramming language you like. After you finish this book, I strongly
recommend that you learn one or two simulation languages.
Errors: I am not responsible for any errors in the book, and if you do
find any, please let me know ([email protected]).
xi
Simulation Techniques Harry Perros
Please acknowledge this book, if you use it in a course, or in a project,
or in a publication.
Enjoy!
xii
Author
xiii
Simulation Techniques Harry Perros
xiv
Harry Perros Simulation Techniques
Chapter 1: Introduction
1.1. Introduction
Simulation is a very popular technique used in many areas for model-
ing different types of systems. It is easy to apply since it does not re-
quire knowledge of advanced mathematics. The only requirement is
basic programming skills. There are also several powerful simulation
languages that can be used which alleviate the burden of having to
write a computer program. Many of these languages have also a
graphic interface that allows the user to describe the system under
study. In this book, we will not make use of simulation languages.
Rather, the reader will learn how to build a simulation model from
scratch. This allows one to better understand and use a simulation
language.
Levels of
Detail
Design Simulation
Experiments (alternatives)
Analyze Data
Implement Results
Collect Data
Debug
All the relevant variables of a system under study are either exoge-
nous or endogenous. An exogenous variable is a variable whose value
is fixed prior to running a simulation and it is provided as input to the
simulation program. An endogenous variable is one whose value is
estimated by the simulation.
3
Simulation Techniques Harry Perros
For instance, let us assume that we want to simulate a single queue
served by one or more servers. Such a queueing system arises at a
bank, where arriving customers form a single queue and one or more
tellers serve the customers waiting in the queue on a first-come-first-
bases. The arrival rate of customers at the bank, the service time of a
customer at a server, and the number of servers are examples of ex-
ogenous variables. The mean waiting time of a customer in the queue,
and the mean number of customers in the queue, are endogenous be-
cause they are estimated by running the simulation.
4
Harry Perros Simulation Techniques
In general, one has information regarding the operational time and the
repair time of a machine. However, in order to determine the down
time of a machine, one has to calculate the queueing time for the re-
pairman. If this quantity is known, then one can calculate the utiliza-
tion of a machine, and other quantities of interest, such as, the utiliza-
tion of the repairman.
Repairman
Finite
Population
of Machines
5
Simulation Techniques Harry Perros
of the status variables depends mainly upon the type of performance
measures that we wish to obtain.
The flow-charts given in figures 1.5, and 1.6 show what happens
when each of these events occur.
6
Harry Perros Simulation Techniques
A machine
breaks down
Repairman
becomes busy
Repair
starts
A machine
is repaired
Other Repairman
machines to be no
becomes
repaired? idle
A new repair
starts
Create
Queue operation time
no
empty? a
ye Queue yes
s empty? a
Generate a
new service
no
Generate a
a new service
We are now ready to carry out the hand simulation given below in
table 1.1. Let us assume that we have 3 machines. Let 𝐶𝐿1, 𝐶𝐿2, and
𝐶𝐿3 be the clocks associated with machine 1, 2, and 3 respectively
(arrival event clocks). Let 𝐶𝐿4 be the clock associated with the depar-
ture event. Finally, let 𝑀𝐶 be the master clock and let 𝑅 indicate
whether the repairman is busy or idle. We assume that at time zero all
three machines are operational and that 𝐶𝐿1 = 1, 𝐶𝐿2 = 4, 𝐶𝐿3 = 9.
These are known as initial conditions.
State of
𝑴𝑪 CL1 𝑪𝑳𝟐 𝑪𝑳𝟑 𝑪𝑳𝟒 𝒏
server
0 1 4 9 - 0 idle
1 - 4 9 6 1 busy
4 - - 9 6 2 busy
6 16 - 9 11 1 busy
9 16 - - 11 2 busy
11 16 21 - 16 1 busy
16 - 21 26 21 1 busy
There is a single token that visits the nodes in a certain sequence. The
nodes are logically connected so that they form a logical ring. In gen-
eral, the order in which the nodes are logically linked may not be the
same as the order in which they are attached to the network. We ass-
9
Simulation Techniques Harry Perros
MC=CLi MC=CL4
No CLi=MC+10
n=0 ? n=n+1
yes n=n-1
R=1
Yes
n=0 ? R=0
CL4=MC+5
No
A
A CL4=MC+5
10
Harry Perros Simulation Techniques
ume that the token never gets lost. A node cannot transmit unless it
has the token. When a node receives the token from its previous logi-
cal upstream node, it may keep it for a period of time up to 𝑇. During
this time, the node transmits packets. A packet is assumed to consist
of data and a header. The header consists of the address of the sender,
the address of the destination, and various control fields. The node
surrenders the token when: a) time 𝑇 has run out, or b) it has trans-
mitted out all the packets in its queue before 𝑇 expires, or c) it re-
ceives the token at a time when it has no packets in its queue to
transmit. If time 𝑇 runs out and the node is in the process of transmit-
ting a packet, it will complete the transmission and then it will sur-
render the token. Surrendering the token means, that the node will
transmit it to its next downstream logical neighbor.
token
...
11
Simulation Techniques Harry Perros
It is much simpler to use the queueing model given in figure 1.10
when constructing the simulation model. The following events have
to be taken into account in this simulation. For each queue, there is an
arrival event and service completion event. For the token, there is a
time of arrival at the next queue event and the time when the token
has to be surrendered to the next node, known as the time-out. For
each queue, we keep track of the time of arrival of the next packet,
the number of customers in the queue, and the time a packet is sched-
uled to depart, if it is being transmitted. For the token, we keep track
of the time of arrival at the next queue, the id of the queue, indicated
by a number, that holds the token, and the time-out.
In the hand simulation given below in table 1.2, we assume that the
token-based network consists of three nodes. That is, the queueing
system in figure 1.10 consists of three queues. The inter-arrival times
to queues 1, 2, and 3 are constant equal to 10, 15, and 20 unit times
respectively. 𝑇 is assumed to be equal to 15 unit times. The time it
takes to transmit a packet is assumed to be constant equal to 6 unit
times. The switch over time is set to 1 unit time. For initial conditions
we assume that the system is empty at time zero, and the first arrival
to queues 1, 2, and 3 will occur at time 2, 4, and 6 respectively. Also,
at time zero, the token is in queue 1. In case when an arrival and a
departure occur simultaneously at the same queue, we will assume
that the arrival occurs first. In addition, if the token and a packet ar-
rive at a queue at the same time, we will assume that the packet ar-
rives first. We define the following clocks:
Join the qi = qi + 1
queue
Return
Service is MC = DT i
completed
yes
Queue Pass H = (H+1) mod3
empty? token
no
Schedule arrival ANH =
Token time MC + switch over time
time out
? yes
Return
no
Return
13
Simulation Techniques Harry Perros
This means that
Time-out node is still
occurs transmitting.
Raise a
flag
Return
DT i = MC +
DT4 = MC + Schedule next
(new service time)
(new service time) service
completion
Return
14
Harry Perros Simulation Techniques
Initialize
simulation
Take
appropriate i. e. i.e. jumptotothe
branch theappropriate
appropriate part of the
part of(or
program theprocedure)
program
action
If newevent
Update events
list
are scheduled
if new
update nextare
events
schedule
event
Is
no simulation
over
?
yes
End
15
Simulation Techniques Harry Perros
Queue 𝟏 Queue 𝟐 Queue 𝟑 Token
Node
𝑴𝑪 𝑨𝑻𝟏 𝑫𝑷𝟏 Q 𝑨𝑻𝟐 𝑫𝑷𝟐 Q 𝑨𝑻𝟑 𝑫𝑷𝟑 Q 𝑻𝑶𝑼𝑻 𝑨𝑵𝑯
No
0 2 0 4 0 6 0 1 1
1 2 0 4 0 6 0 2 2
2 12 1 4 0 6 0 3 3
3 12 9 1 4 0 6 0 1 18
4 12 9 1 19 1 6 0 1 18
6 12 9 1 19 1 26 1 1 18
9 12 0 19 1 26 1 1 10
10 12 0 19 16 1 26 1 2 25
12 22 1 19 16 1 26 1 2 25
16 22 1 19 0 26 1 2 17
17 22 1 19 0 26 23 1 3 32
19 22 1 34 1 26 23 1 3 32
22 32 2 34 1 26 23 1 3 32
23 32 2 34 1 26 0 3 24
24 32 30 2 34 1 26 0 1 39
26 32 30 2 34 1 46 1 1 39
30 32 36 1 34 1 46 1 1 39
32 42 36 2 34 1 46 1 1 39
34 42 36 2 49 2 46 1 1 39
36 42 42 1 49 2 46 1 1 39
39 42 42 1 49 2 46 1 1 *
42 52 42 2 49 2 46 1 1 *
42 52 1 49 2 46 1 1 43
43 52 1 49 49 2 46 1 2 58
16
Harry Perros Simulation Techniques
ferred to as server 1. Stage 2 consists of a finite capacity queue, re-
ferred to as queue 2, served by a single server, referred to as server 2.
Stage 1 Stage 2
Each server may also break down. For simplicity, we will assume that
a server may break down independently of whether it is busy or idle.
A broken-down server cannot provide service until it is repaired. If a
customer was in service when the breakdown occurred, the customer
will resume its service after the server is repaired without any loss to
the service it received up to the time of the breakdown. That is, after
the server becomes operational again, the customer will receive the
balance of its service.
17
Simulation Techniques Harry Perros
Below, we identify the events that may be triggered when each of the
above events occurs.
1. Arrival to queue 1.
18
Harry Perros Simulation Techniques
5. Server 1 becomes operational:
a. Server 1 breaks down (new value for 𝐵𝑅1 clock): This event
gives the time in the future when the server will break down.
During this time the server is operational.
b. Service completion time (new value for 𝐷𝑇1 clock): If the
server was idle when it broke down and queue 1 is not empty
at the time when it becomes operational, then a new service
will begin.
If server 2 was busy when it broke down, update the clock of the
service completion event at server 2 to reflect the delay due to the
repair.
a. Server 2 breaks down (new value for 𝐵𝑅2 clock): This event
gives the time in the future when server 2 will break down.
During this time the server is operational.
b. Service completion time (new value for 𝐷𝑇2 clock): If the
server was idle when it broke down and queue 2 is not empty
at the time when it becomes operational, then a new service
will begin.
Stage 𝟏 Stage 𝟐
𝑴𝑪 𝑨𝑻 𝒏𝟏 𝑫𝑻𝟏 𝑩𝑹𝟏 𝑶𝑷𝟏 𝒔𝟏 𝒏𝟐 𝑫𝑻𝟐 𝑩𝑹𝟐 𝑶𝑷𝟐 𝒔𝟐
10 50 1 30 80 busy 90 idle
30 50 0 80 idle 1 60 90 busy
50 90 1 70 80 busy 1 60 90 busy
60 90 1 70 80 busy 0 90 idle
70 90 0 80 idle 1 100 90 busy
80 90 0 130 down 1 100 90 busy
90 90 0 130 down 1 250 240 down
90 130 1 150 130 down 1 250 240 down
130 170 2 150 130 down 1 250 240 down
130 170 2 150 330 busy 1 250 240 down
150 170 1 170 330 busy 2 250 240 down
170 210 2 170 330 busy 2 250 240 down
170 210 1 190 330 busy 3 250 240 down
190 210 0 330 idle 4 250 240 down
210 250 1 230 330 busy 4 250 240 down
20
Harry Perros Simulation Techniques
230 250 1 330 blocked 4 250 240 down
240 250 1 330 blocked 4 250 540 busy
250 290 2 330 blocked 4 250 540 busy
250 290 1 270 330 busy 4 280 540 busy
270 290 1 330 blocked 4 280 540 busy
280 290 0 330 idle 4 310 540 busy
290 330 1 310 330 busy 4 310 540 busy
310 330 1 310 330 busy 3 340 540 busy
310 330 0 330 idle 4 340 540 busy
330 370 1 350 330 busy 4 340 540 busy
330 370 1 400 380 down 4 340 540 busy
340 370 1 400 380 down 3 370 540 busy
370 410 2 400 380 down 3 370 540 busy
370 410 2 400 380 down 2 400 540 busy
380 410 2 400 580 busy 2 400 540 busy
Problems
1. Do the hand simulation of the machine interference problem, dis-
cussed in section 1.3, for the following cases:
a. The machines are not repaired in a FIFO manner, but the re-
pairman selects randomly a broken machine to fix.
b. There are two repairmen.
c. There are five machines and a single repairman.
21
Simulation Techniques Harry Perros
3. Do the hand simulation of the two-stage manufacturing system,
described in section 1.5, for the following cases:
a. The servers do not breakdown.
b. Add a third stage, and assume that the servers do not break-
down. In this case, server 2 gets blocked if queue 3 is full in
the same way that server 1 gets blocked if queue 2 is full.
Computer projects
Implement one of the following three simulation projects and use it
for the remaining projects described in the subsequent Chapters. At
the end, you will have developed a very sophisticated simulation
model. If you feel energetic you can implement all three simulation
projects!
22
Harry Perros Simulation Techniques
simulation advances from event to event correctly, and whether it
updates the clocks and the other status parameters correctly.
23
Harry Perros Simulation Techniques
Chapter 2:
Generation of pseudo-random numbers
2.1. Introduction
Numbers chosen at random are useful in a variety of applications. For
instance, in numerical analysis, random numbers are used in the solu-
tion of complicated integrals. In computer programming, random
numbers make a good source of data for testing the effectiveness of
computer algorithms. Random numbers also play an important role in
cryptography.
25
Simulation Techniques Harry Perros
of pseudo-random numbers is random. The generation of stochastic
variates is described in Chapter 3.
True random number generators are ideal for some applications, but
in general they are not very practical. An alternative approach to gen-
erating random numbers, which is the most common approach, is to
use a mathematical algorithm. Efficient algorithms have been devel-
oped that can be easily implemented in a computer program to gener-
ate a sequence of random numbers. These algorithms produce num-
bers in a deterministic fashion. That is, given a starting value, known
as the seed, the same sequence of random numbers can be produced
each time as long as the seed remains the same. Despite the determin-
istic way in which random numbers are created, these numbers appear
to be random since they pass a battery of statistical tests designed to
test various properties of random numbers. In view of this, these ran-
dom numbers are referred to as pseudo-random numbers.
26
Harry Perros Simulation Techniques
random numbers is produced each time we run a pseudo-random gen-
erator given that we use the same seed. This is helpful when debug-
ging a simulation program, beecause we want to reproduce the same
sequence of events in order to verify the accuracy of the simulation.
a. Uniformly distributed
b. Statistically independent
c. Reproducible
d. Non-repeating for any desired length.
27
Simulation Techniques Harry Perros
2.3. The congruential method
This is a very popular method and most of the random number gener-
ators in programming languages are based on some variation of it.
The advantage of this method is that it is very simple, fast, and it pro-
duces pseudo-random numbers that are statistically acceptable for
computer simulation.
The method using the above expression is known as the mixed con-
gruential method. A simpler variation of this method is the multipli-
cative congruential method. This method utilizes the relation 𝑥R[\ =
𝑎𝑥R (𝑚𝑜𝑑 𝑚). Historically, multiplicative congruential generators
came before the mixed congruential generators. Below we limit our
discussion to mixed congruential generators.
28
Harry Perros Simulation Techniques
where 𝑥R , 𝑖 = 1,2, …, and 𝑎R , 𝑖 = 1,2, … , 𝑛, are all binary. That is, the
generator produces a sequence of bits. 𝑥R is obtained from the above
expression by adding some of preceding bits and then carrying out a
modulo 2 operation. This is equivalent to the exclusive OR operation
(XOR) defined in the following table:
𝑨 𝑩 𝑨 𝐗𝐎𝐑 𝑩
1 1 0
0 1 1
1 0 1
0 0 0
31
Simulation Techniques Harry Perros
ment over the congruential generators, and they are widely used in
simulation. They are based on the well-known Fibonacci sequence, an
additive recurrence relation, whereby each element is computed using
the two previously computed elements, as shown below:
𝑥R = 𝑥Ri\ + 𝑥Rif
where 0 < 𝑗 < 𝑘, and appropriate initial conditions have been made.
In this generator, the next element is determined by combining two
previously calculated elements that lag behind the current element
utilizing an algebraic operation O. This operation O can be an addi-
tion, or a subtraction, or a multiplication as well as it can be a binary
𝑋𝑂𝑅 operation. If O is the addition operation, then the LFG is called
the additive LFG (ALFG). Likewise, if O is the multiplication opera-
tion, then it is called the multiplicative LFG (MLFG).
The additive LFG is the most frequently used generator. In this case,
the next element is calculated as follows:
1. Frequency test
2. Serial test
3. Runs test
4. Chi-squared test for goodness of fit.
For more information, see “A statistical test suite for random and
pseudorandom number generators for cryptographic applications”,
NIST special publication 800-22.
For example, let us consider the population of the weights of all men
in the USA of age 20 to 29. According to the 1970 census, the popu-
lation mean 𝜇 and standard deviation 𝜎 were 170 and 40 lbs respec-
tively. Now, let us assume that we want to test if the population mean
has changed in 2020. We formulate the null hypothesis that the popu-
lation mean 𝜇 is still 170, versus that it has changed (increased or de-
creased). That is,
𝐻c : 𝜇 = 170
𝐻~ : 𝜇 ≠ 170
33
Simulation Techniques Harry Perros
This is known as a two-tailed test or a two-sided test. Depending upon
the nature of the hypothesis, we may also formulate a one-tailed test
(or one-sided test) to the right. For instance, if we are only concerned
whether the average population weight 𝜇 has increased, then we for-
mulate the hypothesis:
𝐻c : 𝜇 = 170
𝐻~ : 𝜇 > 170
𝐻c : 𝜇 = 170
𝐻~ : 𝜇 < 170
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6
a 8 10
µ
12
b 14 16 18 20
"
!
We note that the correct way to say that “we accept the null hypothe-
sis” is to say that “we fail to reject the null hypothesis”. However, for
simplicity, the former expression is often used. In the opposite direc-
tion, if we reject the null hypothesis, then it is correct to say that “we
accept the alternative hypothesis”.
0.2
0.15
ffXX(t)
(t)
0.1
0.05
0 2 4 6 8 10 12 14 16 18 20
-1.96 0µ 1.96
z
2.5% 95% 2.5%
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6 8 10 12 14 16 18 20
0µ 1.645
z
95% 5%
In the case of the one-tailed test on the right, we are only concerned
with how far 𝑥̅ is to the right of the population mean 𝜇. That is, we
choose point 𝑏 so that the tail (𝑏, +∞) accounts for 5%, for a 95%
confidence. This point corresponds to 1.645 in the standard normal
36
Harry Perros Simulation Techniques
distribution, as shown in figure 2.3. Now, given a sample mean 𝑥̅ , we
calculate its corresponding 𝑧 value, i.e., 𝑧 = (𝑥̅ − 170)/5, and we
accept the null hypothesis if 𝑧 < 1.645. Otherwise, we reject it and
accept the alternative hypothesis. For instance, if 𝑥̅ = 175, then 𝑧 =
1<1.645, and we accept the null hypothesis. Likewise, if 𝑥̅ = 155,
for 𝑧 = −3 < 1.645. However, if 𝑥̅ = 180, then 𝑧 = 2 > 1.645, and
therefore we reject the null hypothesis and accept the alternative hy-
pothesis.
0.25
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6 8 10 12 14 16 18 20
-1.645 0µ
z
5% 95%
Finally, in the case of the one-tailed test on the left, we are only con-
cerned with how far 𝑥̅ is to the left of the population mean 𝜇. That is,
we choose point 𝑎 so that the tail (−∞, 𝑎) accounts for 5%, for a
95% confidence. This point corresponds to −1.645 in the standard
normal distribution, as shown in figure 2.4. Now, given a sample
mean 𝑥̅ , we calculate its corresponding 𝑧 value, i.e., 𝑧 = (𝑥̅ − 170)/
5, and we accept the null hypothesis if 𝑧 > −1.645. Otherwise, we
reject it and accept the alternative hypothesis. For instance, if 𝑥̅ =
165, then 𝑧 = −1 > −1.645, and we accept the null hypothesis.
Likewise, if 𝑥̅ = 180, for 𝑧 = 2 > −1.645. However, if 𝑥̅ = 155,
then 𝑧 = −3 < −1.645, and therefore we reject the null hypothesis
and accept the alternative hypothesis.
There are two errors associated with hypothesis testing, namely, type
I error and type II error. A type I error occurs when we reject the null
37
Simulation Techniques Harry Perros
assumption, whereas in fact it is correct. A type II error occurs when
we accept the null hypothesis when in fact it is not correct. Since we
do not know the truth, we do not know whether we have committed a
type I or a type II error. The type I error is commonly known as a
false positive, and the type II error is known as a false negative. Table
2.1 summarizes the type I and type II errors.
Decision
Real situation
Ho is accepted H0 is rejected
Ho is true Valid Type I error
Ho is not true Type II error Valid
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6 8 10 12 14 16 18 20
0µ 1.645
z
5%
p-value
38
Harry Perros Simulation Techniques
As discussed above, in order to carry out the hypothesis test, we need
to compare the 𝑧 value that corresponds to 𝑥̅ against the cut-off points
𝑎 and 𝑏 or one of them depending on whether we do a two-tailed test
or a one-tailed test. These points are determined using the standard
normal distribution or the 𝑡-student distribution for a given level of
confidence.
For example, let us say that for a one-tailed test on the right, we com-
pute a 𝑝-value of 0.3. At 95% confidence, the area (𝑏, ∞) of the right
tail is 0.05. Since 0.3 > 0.05, we conclude that 𝑧 < 1.645 and accept
the null hypothesis. On the other hand, if the 𝑝-value is 0.001, then
that means that 𝑧 falls on the right side of 1.645, and in this case, we
reject the null hypothesis.
In general, it is safer to make sure that the conclusion holds for all
three confidence levels. That is, if the reported 𝑝-value is greater than
0.10, then we accept the null hypothesis. If it is less than 0.01, then
39
Simulation Techniques Harry Perros
we reject the null hypothesis. However, if it falls in-between these
values, say it is 0.07, then the conclusion is questionable. For, we re-
ject the null hypothesis at 90% confidence but accept it at 95% and
99% confidence.
40
Harry Perros Simulation Techniques
For example, let us consider the string: 1011010101. Then, 𝑆l =
1 − 1 + 1 + 1 − 1 + 1 − 1 + 1 − 1 + 1 = 2 and 𝑆c—˜ = 0.6324. 𝑝-
value = 𝑒𝑟𝑓𝑐(0.6324) = 0.5271 > 0.01, and thus, we accept the
hypothesis that the sequence is random.
The test is based on the idea that if the overlapping bit combinations
are not uniformly distributed, then 𝛥𝑆kf and 𝛥f 𝑆kf become large which
causes the 𝑝 − values to become small.
42
Harry Perros Simulation Techniques
1. Append the 𝑘 − 1 = 2 bits from the beginning of the se-
quence e to the end of e, obtaining 𝑒\› = 001101110100.
We repeat the process by appending the first 𝑘 − 2 = 1 bits of
𝑒 to the end of 𝑒, obtaining 𝑒f› = 00110111010. Do not need
to repeat the process for 𝑘 − 3 since 𝑘 − 3 = 0.
2. Calculate the frequency of occurrence of each 3-bit, 2-bit and
1-bit overlapping combination.
a. For the 3-bit blocks we use 𝑒\› = 001101110100. The 3-
bit combinations are: 000, 001, 010, 011, 100, 101, 110,
111, and their corresponding frequency of occurrence is:
𝑓\,\ = 0, 𝑓\,f = 1, 𝑓\,e = 1, 𝑓\,{ = 2, 𝑓\,² = 1, 𝑓\,z = 2,
𝑓\,³ = 2, 𝑓\,´ = 1.
b. For the 2-bit blocks we use 𝑒f› = 00110111010. The 2 bit
combinations are: 00,01,10,11, and their corresponding
frequency of occurrence is: 𝑓f,\ = 1, 𝑓f,f = 3, 𝑓f,e =
3, 𝑓f,{ = 3
c. For the 1-bit blocks we use 𝑒 = 00110111010. The 1-bit
combinations are: 0,1 and their corresponding frequency
of occurrence is 𝑓e,\ = 4, 𝑓e,f = 6.
3. Compute the following statistics and differences:
2e
𝑆ef = (0 + 1 + 1 + 4 + 1 + 4 + 4 + 1) − 10 = 2.8
10
2f
𝑆ff = (0 + 9 + 9 + 9) − 10 = 1.2
10
2
𝑆\f = (16 + 36) − 10 = 0.4
10
𝛥𝑆ef = 𝑆ef − 𝑆ff = 2.8 − 1.2 = 1.6
2𝑛\ 𝑛f (2𝑛\ 𝑛f − 𝑛\ − 𝑛f )
𝑠¶ =
(𝑛\ +𝑛f )f (𝑛\ +𝑛f − 1)
For a sample, where 𝑛\ > 10 and 𝑛f > 10, the test statistic follows
the standard normal distribution. For instance, at 95% confidence, we
accept the null hypothesis that the bit string is random if 𝑧 > 1.96.
Problems
1. Consider the multiplicative congruential method for generating
random digits with 𝑚 = 10. Determine the length of the cycle for
the values of 𝑎 and 𝑥c : 𝑎 = 2, 𝑥c = 1, 3, 5.
Computer projects
1. Use the four statistical tests described in section 2.6, to test the
random number generator of the programming language that you
plan to use for your project.
45
Simulation Techniques Harry Perros
46
Harry Perros Simulation Techniques
Chapter 3:
Generating Stochastic Variates
3.1. Introduction
In the previous Chapter, we examined techniques for generating ran-
dom numbers. In this Chapter, we discuss techniques for generating
random numbers from a given distribution. These random numbers
are called stochastic variates or random variates. Pseudo-random
numbers are uniformly distributed in the space [0,1], and they are
typically referred to as random numbers. As will be seen in this
Chapter, random numbers are used in the generation of stochastic var-
iates.
There are many techniques for generating stochastic variates. The in-
verse transformation method is the most commonly used technique.
This is presented in section 3.2. In sections 3.3 and 3.4, we use the
inverse transformation method to generate stochastic variates from
known continuous and discrete theoretical distributions. In section
3.5, we describe how to generate stochastic variates from empirical
distributions. Finally, in section 3.6 we describe an alternative method
for generating stochastic variates known as the rejection method.
2
1
# "
! " r
0 0
" 1 " 1
a) 𝒇(𝒙) b) Inversion of 𝑭(𝒙)
Figure 3.1: Sampling from 𝒇(𝒙) = 𝟐𝒙
3𝑥 f , 0≤𝑥≤1
𝑓(𝑥) = Â .
0, otherwise
48
Harry Perros Simulation Techniques
Example 3: Generate random variates from
5𝑥, 0 ≤ 𝑥 ≤ 4
𝑓(𝑥) = Â .
𝑥 − 2, 4 < 𝑥 ≤ 10
1 f 4 𝑥f 10
= 5 𝑥 È + É − 2𝑥Ê È = 70.
2 0 2 4
Therefore
⎧ 1 5 f
⎪ 𝑥 0≤𝑥≤4
70 2
𝐹(𝑥) = 1 f .
⎨ É40 + 𝑥 − 2𝑥Ê 4 < 𝑥 ≤ 10
⎪70 2
⎩
Procedure:
𝑥, 0 ≤ 𝑥 ≤ 1/2
𝑓(𝑥) = Â .
1 − 𝑥, 1/2 < 𝑥 ≤ 1
This is the triangle distribution, and the area under the curve is equal
to 0.5 × 0.5 = 0.25. We divide 𝑓(𝑡) by 0.25 so it becomes a pdf. The
sampling procedure is as follows.
49
Simulation Techniques Harry Perros
3. Otherwise, set 𝑟 = (1 − 𝑥)/0.25 from where we obtain 𝑥 =
1 − 0.25𝑟.
1
𝑓(𝑥) = Ð𝑏 − 𝑎 𝑎 < 𝑥 < 𝑏 ,
0 otherwise
and it is shown graphically in figure 3.2.
1
"−$
$ "
Figure 3.2: The uniform distribution
50
Harry Perros Simulation Techniques
or, 𝑥 = 𝑎 + (𝑏 − 𝑎)𝑟. That is, 𝑟 is multiplied by (𝑏 − 𝑎) and
then shifted by 𝑎.
#fX(t)
" !"
FX(t)
X X
a) Probability density function b) Cumulative function
Figure 3.3: The exponential distribution and its cumulative
𝑟 = 1 − 𝑒 iÒ¿
or
1 − 𝑟 = 𝑒 iÒ¿
or
51
Simulation Techniques Harry Perros
1
𝑥 = − 𝑙𝑜𝑔(1 − 𝑟).
𝜆
We note that 1/𝜆 is the mean of the exponential distribution. Since
1 − 𝐹(𝑥) is also uniformly distributed in [0,1], we can simplify the
above expression by setting 𝑟 = 𝑒 iÒ¿ , or
1
𝑥 = − 𝑙𝑜𝑔(𝑟).
𝜆
3.3.3. Sampling from an Erlang distribution
In many cases the exponential distribution may not represent well the
duration of a real-life activity, such as, the service time at a bank
teller and the repair time of a broken-down machine. In this case, one
can model this time as a mixture of exponential distributions, such as
the Erlang distribution shown in figure 3.4. In this figure, the Erlang
distribution consists of four exponential stages, each with the same
mean 1/𝑎. That is the amount of time 𝑥 spent in each stage is expo-
nentially distributed, i.e., 𝑓(𝑥) = 𝑎𝑒 i~¿ . Let 𝑥\ , 𝑥f , 𝑥e , 𝑥{ be the
amount of time spent in stage 1,2,3, and 4 respectively. Then, the sum
𝑥 = 𝑥\ + 𝑥f + 𝑥e + 𝑥{ follows the Erlang distribution.
52
Harry Perros Simulation Techniques
Erlang variates can be generated very simply as follows. Let us as-
sume that we want to generate a stochastic variate 𝑥 from an Erlang
distribution with 𝑘 stages, each with a mean 1/𝑎. In this case, we
generate 𝑘 exponential variates 𝑥\ , 𝑥f , … , 𝑥k , and then set 𝑥 equal to
their sum, i.e., 𝑥 = 𝑥\ + 𝑥f + ⋯ + 𝑥k . Specifically, let 𝑟\ , 𝑟f , … , 𝑟k be
𝑘 random numbers. Then,
k
1
𝑥 = − ž log ( 𝑟R ) .
𝑎
R¢\
53
Simulation Techniques Harry Perros
0.25
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6 8 10 12 14 16 18 20
1 \ (¿iÖ)•
i
𝑓Ô (𝑥) = 𝑒 f ו , −∞ < 𝑥 < +∞,
𝜎√2𝜋
1 \ •
𝑓Ô (𝑡) = 𝑒 ifÇ , −∞ < 𝑡 < +∞.
√2𝜋
One way to generate variates from a normal distribution is to employ
the central limit theorem. An alternative popular method is to use the
Box-Muller algorithm that generates two normal variates at a time.
Let 𝑟\ and 𝑟f be two random numbers. Then, we can obtain two nor-
mal variates 𝑥\ and 𝑥f as follows:
and
𝑥f = Ï−2𝑙𝑜𝑔Ø 𝑟\ sin(2𝜋𝑟f ).
54
Harry Perros Simulation Techniques
3.4. Sampling from discrete-time probability distributions
In this section, we describe how to generate stochastic variates from a
a binomial distribution, a geometric distribution, and a Poisson distri-
bution.
55
Simulation Techniques Harry Perros
k[\
𝐹Ô (𝑘) = 1 − 𝑝 .
𝑟 = 𝑝k[\ ,
or
𝑙𝑜𝑔𝑟 = (𝑘 + 1)𝑙𝑜𝑔𝑝
or
𝑙𝑜𝑔𝑟
𝑘= − 1.
𝑙𝑜𝑔𝑞
𝑟 = 𝑝k ,
or
𝑙𝑜𝑔𝑟
𝑘= .
𝑙𝑜𝑔𝑝
𝜆R
𝑝(𝑋 = 𝑖) = 𝑒 iÒ , 𝑖 = 0,1,2, …
𝑖!
where 𝜆 is the average number of times the event occurs in a unit
time.
56
Harry Perros Simulation Techniques
There is an interesting relation between the Poisson distribution and
the exponential distribution, in that the inter-arrival time between
successive arrivals is exponentially distributed with a mean 1/𝜆.
Therefore, the easiest way to generate Poisson stochastic variates is to
generate exponentially distributed stochastic variates until their sum
exceeds the unit time. Let us assume that the unit time is equal to 1 (it
could be any value for that matter). Then, we generate exponentially
distributed stochastic variates 𝑥\ , 𝑥f , ⋯ , 𝑥l , 𝑥l[\ , so that
l l[\
ž 𝑥R ≤ 1 < ž 𝑥R .
R¢\ R¢\
or
l l[\
ž 𝑙𝑜𝑔Ø 𝑟R ≥ −𝜆 > ž 𝑙𝑜𝑔Ø 𝑟R
R¢\ R¢\
or
l l[\
𝑙𝑜𝑔Ø ÛÜ 𝑟R Ý ≥ −𝜆 > 𝑙𝑜𝑔Ø ÛÜ 𝑟R Ý,
R¢\ R¢\
or
l l[\
Ü 𝑟R ≥ 𝑒 iÒ > Ü 𝑟R .
R¢\ R¢\
The value 𝑛 for which the above expression holds is a Poisson sto-
chastic variate.
57
Simulation Techniques Harry Perros
3.5. Generating stochastic variates from an empirical distribution
Empirical probability distributions are used often in simulation, and
they can be either approximated by a known theoretical distribution
or used as is. Generating stochastic variates directly from an empiri-
cal distribution is quite easy and it is done using the inverse transfor-
mation method.
1
7/8
FX(t)
r
p(X) 4/8
3/8
1/8
1/8
0 1 2 3
0 1 2 3
X X
a) Probability density function b) Cumulative function
Figure 3.6: Generating a stochastic variate from a discrete distribution
1. If 𝑟 ≤ 1/8, then 𝑋 = 0
2. If 1/8 ≤ 𝑟 ≤ 4/8, then 𝑋 = 1
3. If 4/8 ≤ 𝑟 ≤ 7/8, then 𝑋 = 2
4. If 7/8 ≤ 𝑟 ≤ 1, then 𝑋 = 3
In figure 3.6, 𝑟 falls in between 4/8 and 7/8, and therefore we chose
𝑋 = 2.
f(x3 )
f(x4 )
f(x2 ) f(x5 )
f(x1 )
f(x6 )
f(x7 )
x1 x2 x3 x4 x5 x6 x7
Figure 3.7: A histogram of a continuous random variable
x1 x2 x3 x4 x5 x6 x7
𝑟 − 𝐹Ô (𝑥Ri\ )
𝑥 = 𝑥Ri\ + (𝑥R − 𝑥Ri\ ) .
𝐹Ô (𝑥R ) − 𝐹Ô (𝑥Ri\ )
!" #
$ %
Figure 3.9: Normalized 𝒇(𝒙)
59
Simulation Techniques Harry Perros
3.6. The rejection method
The rejection technique can be used to generate random variates if
𝑓(𝑥) is bounded and 𝑥 takes a finite range of values, i.e., 𝑎 ≤ 𝑥 ≤ 𝑏.
It consists of the following steps:
60
Harry Perros Simulation Techniques
1. Generate a pair of random numbers (𝑟\ , 𝑟f ).
2. If 𝑟f < 𝑓(𝑟\ ), where 𝑓(𝑟\ ) = Ï1 − 𝑟\f , then 𝑟f is under (or
on) the curve and hence the pair (𝑟\ , 𝑟f ) is accepted. Other-
wise, it is rejected.
Monte Carlo techniques are beyond the scope of this book, and the
interested reader is referred to the relevant litterature.
Computer projects
In this assignment you will modify the code you developed in Chapter
1 so that to introduce randomness in your simulation model.
Make sure that your clocks are defined as real variables. Run your
simulation model as before. Each time an event occurs, print out a
line of output to show the new value of the clocks and the other
relevant parameters.
62
Harry Perros Simulation Techniques
Chapter 4:
Simulation Designs
4.1. Introduction
In this Chapter, we examine three different designs for building simu-
lation models. These are: the event-advance design, the unit-time ad-
vance design and the activity-based design. The first two designs are
event-based but utilize different ways of advancing the time. The
third design is activity-based rather than event-based. The two event-
based designs are the most commonly used.
Advance
time
Take appropriate
action depending
on the type
of event
no Any
A conditional
events
?
yes
Create a new Future
event(s) event
list
64
Harry Perros Simulation Techniques
• Time of occurrence (i.e., value of the event's clock)
• Type of event
1. Locating the next future event with the smallest clock time.
2. Deleting an event from the list after it has occurred.
3. Inserting a newly scheduled event in the event list.
Below we examine two different schemes for storing an event list. In
the first scheme, the event list is stored in a sequential array, and in
the second scheme it is stored as a linked list.
minIndex ¬ 1
minValue ¬ A(1)
For i ¬ 1,n
If minValue ≤ A(i)
continue
else
minValue ¬ A(i)
minIndex ¬ i
. . . NULL
head
Figure 4.3: A linked list
A single linked list can be used to store an event list as shown in fig-
ure 4.4. Each node consists of two data elements, a clock CL showing
the future time of an event and a value i indicating the type of event.
The nodes are arranged in an ascending order of the clocks.
CL i CL j . . . CL n NULL
head
Struc node
{
Int type;
Float clock;
struct node* next;
}
. . . . . . . . .
Memory
nodePtr
68
Harry Perros Simulation Techniques
struct node* nodePtr = (struct node*) malloc
(sizeof (struct node));
We use the cast (struct node*) to explicitly tell the compiler that
the memory location returned by this call will be pointed to by a vari-
able which is a pointer to the node structure that we have defined. We
can modify the fields of the structure any time in the program as fol-
lows:
nodePtr->type = ARRIVAL;
nodePtr->next = NULL;
The linked list is initially empty and the head does not point to any
node. Thus, the head must be explicitly initialized to point to NULL
using the command:
After inserting nodes, the list can be traversed using the head and the
pointer to next node in each member node of the list.
69
Simulation Techniques Harry Perros
line returns a pointer to the created node. The function can be called
from the program as follows:
where ARRIVAL is one of the predefined event types and MCL is the
master clock. Thus, we are creating a new ARRIVAL event that will
occur 5.5 units of time in the future. This function can be visualized
as shown in figure 4.6.
newNodePtr
free(nodePtr);
As commented in the code above there are three separate cases that
we must handle.
Case 1: head is NULL
In this case, head is currently pointing to NULL, which means that the
list is empty and the node being inserted will be the first element in
the list. Thus, after the call to insertNode(), head must point to
the node that is being inserted. This can be visualized as shown in
figures 4.7 and 4.8.
NULL
head
newNodePtr
head
head
newNodePtr
ARR 6.7
72
Harry Perros Simulation Techniques
Note that in the above two cases, the pointer to the head changes.
Thus, the pointer returned by insertNode()must be assigned to
head.
head
newNodePtr
ARR 10.7
After the node is removed, all that needs to be done is to return the
node that was pointed to by head and make head to point to the next
node. This is done as follows:
74
Harry Perros Simulation Techniques
Other data structures can be used, and the interested reader can con-
sult a textbook on data structures.
i j . . . k NULL
head
75
Simulation Techniques Harry Perros
E Ei Ej
t t+1 … t1 … t3
Master clock
is increased
by a unit-time
Is
any future no
event clock A
= MC
?
yes
An event has
occurred
Take appropriate
action
Any
conditional no
event(s) A
?
yes
Schedule new
event (j)
76
Harry Perros Simulation Techniques
and that the next event 𝐸R will occur at time 𝑡\ . We increase the mas-
ter clock by a unit time and compare it to all the event clocks. Since
none of the clocks is equal to the master clock, the master clock is
increased by another unit time, and so on, until after some iterations
the master clock becomes equal to 𝑡\ . Event 𝐸R is then processed, the
event list is modified accordingly, and then the master clock is in-
creased by one unit time repeatedly until it hits the next event, and so
on.
MCL = MCL + 1
>1 ST =1
ST = ST - 1 ST = ST - 1
End of service
Take appropriate
action
>1 AT =1
AT = AT - 1 AT = AT - 1
An arrival has
occurred. Take
appropriate action
One minor issue that the reader needs to be aware of when using the
unit-time design is regarding the estimation of the duration of an en-
dogenously created variable. For instance, let us assume that in the
simulation of the single server queue, we want to estimate the resi-
dence time of a customer. This is the elapsed time from the time in-
stance the customer arrives at the single server queue to the time in-
stance that it departs. If we use clocks, then this elapsed time is the
difference between the departure clock and the arrival clock. If we
use activity durations which are integers, then we can use the corre-
sponding master clock values at which these two events occurred.
However, if the duration of the activities are real numbers, then the
best we can do is to use the master clock value before or after the
time the event occurred. For instance, if an arrival event occurs be-
tween 𝑡 and 𝑡 + 1, then we can use 𝑡. This of course introduces an
error in the estimation of the residence time which depends on the
length of the unit time.
79
Simulation Techniques Harry Perros
4.6.2. Implementation
The main operation related to the processing of the future event list is
to compare all the future event clocks against the master clock each
time the master clock is increased by a unit time. An implementation
using a sequential array as described in section 4.4 would suffice in
this case.
The worst case for the unit-time advance method is when there are
few events and they are far apart from each other. In this case, the
unit-time advance design will spend a lot of non-productive time ad-
vancing the master clock and checking if an event has occurred. In
such cases, the event-advance design is obviously preferable.
80
Harry Perros Simulation Techniques
𝑖th arrival. Also, let 𝐴𝑇R[\ be the interarrival time between the 𝑖th and
(𝑖 + 1)st arrival. Finally, we assume that the 𝑖th arrival occurs at time
𝑎R , starts its service at time 𝑠R , and ends its service at time 𝑠R + 𝑆𝑇R , as
shown in figure 4.18. Let us assume now that we know the waiting
time 𝑊𝑇R and the service time 𝑆𝑇R of the 𝑖th arrival. Then, one the
following three situations may occur.
ST i
WT i
AT
i+1
a si a i+1 s i + STi
i
1. The (𝑖 + 1)st arrival occurs during the time that the 𝑖th arrival
is waiting.
2. The (𝑖 + 1)st arrival occurs when the 𝑖th arrival is in service.
This is the case shown in figure 4.18.
3. The (𝑖 + 1)st arrival occurs after the 𝑖th arrival has departed
from the server.
For each of these three cases, the waiting time 𝑊𝑇R[\ of the (𝑖 + 1)st
arrival can be easily determined as follows:
= 𝑇𝑊R − 𝐴𝑇R[\ ,
where 𝑇𝑊R is the total waiting time of the 𝑖th customer in the
system, i.e., queueing and being served.
3. 𝑊𝑇R[\ = 0
Having calculated 𝑊𝑇R[\ , the total waiting time 𝑇𝑊R[\ in the system
of the (𝑖 + 1)st arrival is calculated by generating a service time
𝑆𝑇R[\ . We have: 𝑇𝑊R[\ = 𝑊𝑇R[\ + 𝑆𝑇R[\ . For the next arrival 𝑖 + 2,
generate an inter-arrival time 𝐴𝑇R[f , and then calculate 𝑊𝑇R[f and
𝑇𝑊R[\ as above, and so on. The basic mechanism of this activity-
based simulation model is shown in figure 4.19.
Generate
AT
Generate
Generate ST
ST
TW = ST
TW = ST + (TW - AT)
A
A
4.8. Examples
In this section, we highlight the event-based simulation designs
through two examples. The first one, deals with an inventory system,
and the second one with the round-robin queue.
83
Simulation Techniques Harry Perros
The fluctuation in the inventory level is shown in figure 4.20. At time
𝑡\ an order is placed, which arrives at time 𝑡f . The new order is added
to the existing inventory. At time 𝑡e a new order is placed, which ar-
rives at time 𝑡{ . We see that at time 𝑡{ , the inventory has become neg-
ative, meaning that there are backorders. When the new order arrives,
the backorders are satisfied immediately. We observe that the two
lead times (𝑡f − 𝑡\ ) and (𝑡{ − 𝑡e ) are not the same. Depending on the
application, lead times maybe or may not be constant.
Stock
level
Reorder
point
t t t t
1 2 3 4
time
84
Harry Perros Simulation Techniques
Initialization
Generate
MC t=t+1
AT
Print
t>T results
yes
no
End
>1
LT =1 order has
arrived
=0
LT = 0
LT = LT - 1 I=I+Q
no order
outstanding
Generate
daily demand D
I=I-D
no
I < ROP TC1 = TC1 + I*C1
?
yes
a
no
LT = 0
yes
Generate
LT
TC2 = TC2+ C2
no
I<0 TC1 = TC1 + I*C1
yes
a
TC3 = TC3 + I*C3
We assume a finite number of users, and each user has always work
to do. That is, a user never becomes idle. A user spends some time
thinking (i.e., typing a line or thinking what to do next), upon comple-
tion of which a request is created which is then executed by the CPU.
86
Harry Perros Simulation Techniques
Since a user never becomes idle, the user’s state continuously cycles
through a think state and a CPU state, as shown in figure 4.23.
...
think CPU think CPU think CPU
state state state state state state
The requests are executed by the CPU in a round robin manner. That
is, each request is allowed to use the CPU for a small quantum of
time. If the request is done at the end of this quantum or during the
quantum (not considered in this simulation model), then it departs
from the CPU. Otherwise, it is simply placed at the end of the CPU
queue. In this manner, each request in the CPU queue gets a chance to
use the CPU. Furthermore, short requests get done faster than long
ones. A request that leaves the CPU simply goes back to the originat-
ing user. At that instance the user goes into a think state. The think
time is typically significantly longer than the duration of a quantum.
For instance, the mean think time could be 30 seconds, whereas a
quantum could be less than 1 msec. A request, in general, would re-
quire many CPU quanta. If a request requires 5 seconds of CPU time,
and a quantum is 1 msec, then it would cycle through the CPU queue
5000 times!
Terminals
Users
Requests
•
•
• CPU
87
Simulation Techniques Harry Perros
We model this client-server scheme using the queueing system shown
in figure 4.24. The reader will realize that this is the same queueing
system as the machine interference problem, described in section 1.3.
In this case, the users are the machines and the CPU is the repairman.
A user in the think state is like a machine being operational. The main
difference from the machine interference problem is that the CPU
queue is served in a round robin fashion rather than in a FIF0 manner.
!1 #1 !2 #2 . . . !1 #1 NULL
head
Figure 4.25: Future event list of all new arrivals to the CPU queue
All the information regarding the requests in the CPU queue, includ-
ing the one in service, is maintained in the separate linked shown in
figure 4.26. This linked list is known as a circular singly linked list,
since it is singly linked and the last node is linked to the first node.
Each node contains the number of quanta required by a request and its
terminal identification number. Pointers H and E point to the begin-
ning and end of the list respectively. The nodes are ordered in the way
that the requests are waiting in the CPU queue. Thus, the first node
represents the request currently in service, and the last node the re-
quest currently at the end of the CPU queue. When a new request ar-
rives at the CPU queue, a new node is created and it is attached after
the node pointed by E. If a request requires further service upon com-
pletion of its quantum, then its node is simply placed at the end of the
linked list. This is achieved by simply changing H to point to the next
node in the linked list, i.e., H¬LINK(H), and setting E to H. If a re-
quest does not require further service upon completion of its quan-
tum, then its node is removed and H¬LINK(H).
H !1 #1 !2 #2 . . . !% #% E
Figure 4.26: Future event list of all new arrivals to the CPU queue
89
Simulation Techniques Harry Perros
The simulation model operates under the unit-time advance design
during the period of time that the CPU is busy, with a unit time equal
to one quantum. During the time that CPU is idle, the simulation
model switches to an event-advance design. This hybrid design is
summarized in figure 4.27. Note that 𝑡𝑎𝑟𝑟 gives the time of the next
new arrival at the CPU queue. The remaining details of this program
are left to the reader as an exercise.
Is
yes CPU no
MCL = tarr MCL = MCL + 1
queue
empty
?
A new arrival A departure
event occurs event occurs
Is no
b MCL=tarr b
?
yes
A new arrival
event occurs
Problems
Consider the following systems:
Choose any of the above systems. First, describe how the system op-
erates. (Make your own assumptions whenever necessary. Make sure
that these assumptions do not render the system trivial!) Then, set up
a simulation model to represent the operations of the system. State
clearly which are the state variables and what are the events. For each
caused event, state clearly what action the simulation model will take.
Computer projects
The objective of the projects below is to experiment linked data struc-
tures and other enhancements. These projects can be skipped without
affecting the process of the development of your simulation model.
92
Harry Perros Simulation Techniques
Chapter 5:
Estimation techniques for analyzing
endogenously created data
5.1. Introduction
So far, we have examined techniques for building a simulation model.
These techniques were centered around the topics of random number
generation and simulation design. The reason why we develop a sim-
ulation model is because we need to estimate various performance
measures of a system under study. These measures are obtained by
collecting and analyzing endogenously created data. In this Chapter,
we will examine various estimation techniques that are commonly
used in simulation. Before we proceed, we discuss briefly how one
can collect data generated by a simulation program.
93
Simulation Techniques Harry Perros
puted using this sample, such as, the mean, the standard deviation,
and percentiles.
94
Harry Perros Simulation Techniques
Node 1 Node 2 Node 3
Arr Dep Arr Dep Arr Dep
x x x x x x
x x x x x x
x x x x H3 x
x x H2 x E3 x
H1 x E2 x
x
E1 x
Figure 5.2: Data structure for the token-based access scheme simulation
Instead of keeping two columns per node, one can keep one column.
When a packet arrives, its arrival time is stored in the next available
location. Upon departure of the packet, its arrival time is substituted
by its total time in the queue.
95
Simulation Techniques Harry Perros
obtained by dividing the contents of the 𝑛th location by 𝑇, the total
simulation time.
96
Harry Perros Simulation Techniques
system might find itself in. This reduces the duration of the transient
period. However, in order to set the initial conditions properly, an a
priori knowledge of the system is required. The easiest solution is to
assume an empty system.
One should be careful about the effects of the transient period when
collecting endogenously created data. For, the data created during the
transient period are dependent on the initial condition. Two methods
are commonly used to remove the effects of the transient period. The
first one requires a very long simulation run, so that the amount of
data collected during the transient period is insignificant relative to
the amount of data collected during the steady state period. The sec-
ond method, which is preferable, simply requires that no data collec-
tion is carried out during the transient period. This can be easily im-
plemented as follows. Run the simulation model until it reaches its
steady state, and then set to zero all the statistical counts, while leav-
ing the rest of the variables in the simulation unchanged. Continue to
simulate until a sufficient number of observations have been obtained.
97
Simulation Techniques Harry Perros
90th, 95th, and 99th percentile. For instance, the 95th percentile of
the down time is the down time such that only 5% of all down times
are greater than it. Percentiles are easy to compute and they are used
in service level agreements (SLAs)
Let us assume that we estimate that the mean and the 95th percentile
of the down time is 10 and 40 hours respectively. We see that down
times can be a lot larger than 10, which is something that cannot be
determined by simply looking at the mean. If the 95th percentile of
40 hours is not acceptable to the management, then more repairmen
can be added so that to bring it (and the mean too) down. Obviously,
a 95th percentile of 20 hours is much preferable since it guarantees a
faster turnaround on the repair times. The simulation model can be
used to do what-if analysis in order to find the required number of
repairmen so that the 95th percentile is below a given threshold.
1 l
𝑥̅ = ž 𝑥R .
𝑛 R¢\
98
Harry Perros Simulation Techniques
known as the confidence level. The confidence interval of the sample
mean 𝑥̅ is given by the expression:
𝜎 𝜎
Û𝑥̅ − 1.96 , 𝑥̅ + 1.96 Ý
√𝑛 √𝑛
Now, let us fix points 𝑎 and 𝑏 in this distribution so that 95% of the
observations (that is, of the sample means 𝑥̅ ) fall in-between the two
points. Points 𝑎 and 𝑏 are symmetrical around 𝜇, and the two tails
(−∞, 𝑎) and (𝑏, +∞) account for 5% of the total distribution, as
shown in figure 5.3. Using the 𝑧-transform we have that 𝑎 is 1.96
standard deviations below 𝜇, i.e., 𝑎 = 𝜇 − 1.96𝜎/√𝑛, and 𝑏 is 1.96
standard deviations above 𝜇, i.e., 𝑏 = 𝜇 + 1.96𝜎/√𝑛. Now, if we
consider an arbitrary observation 𝑥̅ , then this observation will lie in
the interval [𝑎, 𝑏] 95% of the time. That is, its distance from 𝜇 will be
less than 1.96𝜎/√𝑛 95% of the time. Or, otherwise stated, 95% of
the time 𝜇 will be less than 1.96𝜎/√𝑛 from 𝑥̅ . That is, 𝜇 will lie in
the confidence interval Ÿ𝑥̅ − 1.96𝜎/√𝑛, 𝑥̅ + 1.96𝜎/√𝑛 95% of the
time.
99
Simulation Techniques
0.25
Harry Perros
0.2
fX(t)
0.15
fX(t)
0.1
0.05
0 2 4 6
a 8 10
µ
12
b 14 16 18 20
The above expression for 𝑠 f is correct when the observations are in-
dependent of each other. To compute the above expression, we need
to store all the observations first and then at the end of the simulation
compute the sample variance. The following equivalent expression
100
Harry Perros Simulation Techniques
allows the calculation on the fly without having to store all the obser-
vations.
f
1 l 1 l
𝑠 f = Éž 𝑥Rf − Ûž 𝑥R Ý Ê.
𝑛−1 R¢\ 𝑛 R¢\
That is, when the 𝑘th observation is obtained, we update the cumula-
tive sums ∑kR¢\ 𝑥\f and ∑kR¢\ 𝑥R , and total count of observations. At the
end of the simulation, the variance can be easily computed using the
above expression.
In the case where the standard deviation 𝜎 is not known and has to be
estimated from the sample data, the confidence interval is computed
using the 𝑡-student distribution with 𝑛 − 1 degrees of freedom. That
is:
𝑠 𝑠
Û𝑥̅ − 𝑡li\ , 𝑥̅ + 𝑡li\ Ý.
√𝑛 √𝑛
The 𝑡-student distribution is symmetric and bell-shaped, like
the normal distribution, but has heavier tails, meaning that it is more
prone to producing values that fall far from its mean. It is commonly
used instead of the 𝑧 value, which is calculated from the normal dis-
tribution, since the standard deviation 𝜎 is typically not known. The
𝑡-student distribution is also used if the sample size is small, less than
30. As the degrees of freedom increase, the 𝑡-distrubtion tends to the
normal distribution. It is calculated for different confidence level in
the same way as the 𝑧 values.
101
Simulation Techniques Harry Perros
102
Harry Perros Simulation Techniques
In the presence of correlated observations, the above expression for
the variance does not hold. The expression for the mean holds for cor-
related and uncorrelated observations. The correct procedure, there-
fore, for obtaining the confidence interval of the sample mean is to
first check if the observations are correlated. If they are not, one can
proceed as described above. If the observations are correlated, then
one has to use a special procedure to get around this problem. Below,
we discuss the following four procedures for estimating the variance
of correlated observations:
The covariance takes values in the space (−∞, +∞), and it is not di-
mensionless, which may make its interpretation difficult. In view of
this, we use the correlation 𝐶𝑜𝑟𝑟(𝑋, 𝑌), which is the normalized
𝐶𝑜𝑣(𝑋, 𝑌). This is a dimensionless metric, defined as follows:
𝐶𝑜𝑣(𝑋, 𝑌)
𝐶𝑜𝑟𝑟(𝑋, 𝑌) = .
Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌)
103
Simulation Techniques Harry Perros
∑lR¢\(𝑥R − 𝑥̅ )(𝑦R − 𝑦µ)
𝑟Ôð = .
Ï∑lñ¢\(𝑥R − 𝑥̅ )f Ï∑lñ¢\(𝑦R − 𝑦µ)f
∑li\
R¢\ (𝑥R − 𝑥̅ )(𝑥R[\ − 𝑥̅ )
𝑟Ôð = ,
Ï∑li\ f li\
ñ¢\ (𝑥R − 𝑥̅ ) Ï∑ñ¢\ (𝑥R[\ − 𝑥̅ )
f
where
li\ l
1 1
𝑥̅ = ž 𝑥R and 𝑦µ = ž 𝑥R .
𝑛−1 𝑛−1
R¢\ R¢f
∑li\
R¢\ (𝑥R − 𝑥̅ )(𝑥R[\ − 𝑥̅ )
𝑟\ = ,
∑lñ¢\(𝑥R − 𝑥̅ )f
∑lik
R¢\ (𝑥R − 𝑥̅ )(𝑥R[k − 𝑥̅ )
𝑟k = , 𝑘 ≥ 1,
∑lñ¢\(𝑥R − 𝑥̅ )f
104
Harry Perros Simulation Techniques
where 𝑥̅ is the overall sample mean. The lag 𝑘 autocorrelations, 𝑘 ≥
1, are also referred to as the autocorrelation coefficients.
+1
rk
lag k
+1
rk
lag k
105
Simulation Techniques Harry Perros
Let us now return to our estimation problem. Having obtained a sam-
ple of 𝑛 observations 𝑥\ , 𝑥f , … , 𝑥l , we calculate the autocorrelation
coefficients, and then the variance can be estimated using the expres-
sion:
l/{
𝑘
𝑠 f = 𝑠Ôf ò1 + 2 ž Û1 − 𝑟k Ýó ,
𝑛
k¢\
106
Harry Perros Simulation Techniques
|
1
𝑥̿ = ž 𝑥̅R
𝑚
R¢\
|
1
𝑠 f = ž(𝑥̅R − 𝑥̿ )f ,
𝑚−1
R¢\
The batch size and the number of batches simulated may be a lot big-
ger than what was described above. In general, they are fixed so that
the confidence interval is very small. This is an important issue and it
is discussed further in section 5.6.
Let 𝑥̅R be the sample mean of the 𝑖th replication, that is:
1 ö
𝑥̅R = ž 𝑥Ru , 𝑖 = 1,2, … , 𝑚.
𝑙 u¢\
The sample means 𝑥̅\ , 𝑥̅f , … , 𝑥̅ | are independent from each other and
therefore the confidence interval is:
𝑠 𝑠
Û𝑥̿ − 𝑡c.õ² , 𝑥̿ + 𝑡c.õ² Ý,
√𝑚 √𝑚
where
| |
1 1
𝑥̿ = ž 𝑥̅R and 𝑠 f = ž(𝑥̅R − 𝑥̿ )f .
𝑚 𝑚−1
R¢\ R¢\
108
Harry Perros Simulation Techniques
5.6. Estimation of the confidence interval of a percentile
Percentiles is an important performance metric used in service level
agreements (SLAs). They are often ignored in favor of the mean of a
random variable, which in general is not an adequate metric. We are
primarily interested in the 90th, 95th, and 99th percentile. Given a
random variable 𝑋, its 95th percentile (or any other percentile) is a
value 𝑥c.õ² such that 95% of the values of the random variable 𝑋 are
less than 𝑥c.õ² , i.e., 𝑃𝑟𝑜𝑏[𝑋 ≤ 𝑥c.õ² ] = 0.95. Correspondingly, 5%
of the values of the random variable 𝑋 are greater than 𝑥c.õ² , i.e.,
𝑃𝑟𝑜𝑏[𝑋 ≥ 𝑥c.õ² ] = 0.05.
109
Simulation Techniques Harry Perros
𝑚 𝑚
1 1 2
𝑥̅c.õ² = ž 𝑥c.õ²,R and 𝑠2 = žŸ𝑥c.õ²,R − 𝑥̅c.õ² .
𝑚 𝑚−1
𝑖=1 𝑖=1
110
Harry Perros Simulation Techniques
| |
1 1
𝑝̅ = ž 𝑝R and 𝑠 f = ž(𝑝R − 𝑝̅ )f .
𝑚 𝑚−1
R¢\ R¢\
111
Simulation Techniques Harry Perros
should be fixed so that the width is about 10% or less of the estimat-
ed statistic. The typical approach is to run the simulation with increas-
ing values of 𝑛, until we get the desired result. Then, we can proceed
with the simulation experiments with the chosen value of 𝑛.
.Finally, we note that the width 𝑡c.õ² Ÿ𝑠⁄√𝑛 can be reduced by reduc-
ing the variance 𝑠 f . For that, there are special variance reduction
techniques some of which are presented in Chapter 7.
Computer project
In this assignment you will enhance your program with a statistical
estimation procedure and then exercise it to obtain performance
curves. Use the code you developed in the computer project in Chap-
ter 3.
112
Harry Perros Simulation Techniques
regarding the amount of time each machine spends being broken
down, i.e., waiting in the queue and also being repaired.
Run your simulation for 1050 observations, i.e., repairs. (That is,
stop the simulation after the 1050th observation is generated and
ignore the customers remaining in the queues.) Discard the first
50 observations to account for the transient state. Use a statistical
package to obtain and graph the correlogram based on the above
1000 observations. Based on the correlogram, calculate the batch
size. Implement the batch means approach in your program and
run your program for 31 batches. Disregard the first batch and use
the other 30 batch means to construct a confidence interval of the
mean down time (queueing and being repaired) 𝑊.
Use the batch means method to estimate the 95th percentile of the
response time. For this, make sure that the batch size is very large,
around 1000 observatios. Below you will estimate both the mean
and the 95th percentile in a single run, and for this you should use
a large batch size of 1000 observations instead of the batch size
determined above.
b. Experimentation
You have now completed the development of the simulation
model. The reason we developed the simulation model is to use it
to obtain performance curves. In this simulation model, we have
two variables to play with, namely the mean repair time (𝑀𝑅𝑇)
and the mean operational time (𝑀𝑂𝑇).
Set 𝑀𝑅𝑇 = 10 and calculate the mean 𝑊 and the 95th percentile
𝑊c.õ² of the down time by varying 𝑀𝑂𝑇 from 10 to 50 in incre-
ments of 5. The idea is to get a good spread of values so that to
have a complete picture of how 𝑊 and 𝑊c.õ² vary as a function of
𝑀𝑂𝑇. Therefore, if needed, decrease or increase the range of val-
ues of 𝑀𝑂𝑇. You will observe that 𝑊c.õ² is much larger than 𝑊
since it is an estimate of the tail of the distribution, unless the dis-
tribution has very low variability in which case the two estimates
will be close. Graph your results and discuss the observed trends.
113
Simulation Techniques Harry Perros
Obtain similar curves by fixing 𝑀𝑂𝑇 and then vary 𝑀𝑅𝑇. Choose
values for 𝑀𝑂𝑇 and 𝑀𝑅𝑇 so that to obtain a complete picture of
the two graphs. Graph your results and discuss the observed
trends.
You can extend your simulation model to the case where the
number of machines is an input to the simulation. Then you can
repeat the above experiments by also varying the number of ma-
chines. For instance, you can get multiple graphs when you fix the
mean repair time 𝑀𝑅𝑇 and vary the mean operational time 𝑀𝑂𝑇,
each corresponding to a different number of machines. Again, you
need to select values for the 𝑀𝑅𝑇 and 𝑀𝑂𝑇 so that to get mean-
ingful curves.
114
Harry Perros Simulation Techniques
Run your simulation for a total of 3100 packet departures from all
three queues. (That is, stop the simulation after the 3100th packet
has been transmitted. Ignore the remaining packets in the queues.)
Discard the first 100 observations to account for the transient
state. Use a statistical package to calculate and graph the correlo-
gram of the response times collected in the above task. Based on
the correlogram calculate the batch size. Implement the batch
means approach in your program and run your program for 31
batches. Disregard the first batch and use the other 30 batch
means to construct a confidence interval of the mean response
time.
Use the batch means method to estimate the 95th percentile of the
response time. For this, make sure that the batch size is very large,
around 1000 observatios. Below you will estimate both the mean
and the 95th percentile in a single run, and for this you should use
a large batch size of 1000 observations instead of the batch size
determined above.
b. Experimentation
You have now finished building the simulation model and you
can exercise it in order to study the performance of the system.
Specifically, we are interested to see how the response time is af-
fected by the packet inter-arrival time and by the time out 𝑇. The
transmission time of a packet and the switch over time also affect
the response time, but they will not be considered here. Also, in
order to simplify the experiments, we will assume that the mean
inter-arrival time of packets to all three queues has the same
mean, notated as 𝑀𝐼𝐴𝑇. Obtain the following results.
116
Harry Perros Simulation Techniques
from the time it arrives to queue 1 to the time it departs from
server 2. We shall refer to this time as the production time.
Use the batch means method to estimate the 95th percentile of the
production time. For this, make sure that the batch size is very
large, around 1000 observatios. Below you will estimate both the
mean and the 95th percentile in a single run, and for this you
should use a large batch size of 1000 observations instead of the
batch size determined above.
Experimentation
You have now finished building the simulation model and you
can exercise it in order to study the performance of the system. Of
interest is how the production time of a customer is affected by
the parameters of the system. These are: the mean inter-arrival
time (𝑀𝐼𝐴𝑇) to the two-stage manufacturing system, the mean
service time (𝑀𝑆𝑇1), mean operational (𝑀𝑂𝑇1) and mean repair
time (𝑀𝑅𝑇1) of server 1, the mean service time (𝑀𝑆𝑇2), mean
operational (𝑀𝑂𝑇2) and mean repair time (𝑀𝑅𝑇2) of server 2,
and the buffer capacity 𝐵 of queue 2 (which includes the one in
service as well).
117
Simulation Techniques Harry Perros
200, 𝑀𝑅𝑇1 = 50, 𝑀𝑆𝑇2 = 10, 𝑀𝑂𝑇2 = 300, 𝑀𝑅𝑇2 = 30, and
𝐵 = 4. You will observe that 𝑃c.õ² is much larger than 𝑃 since it
is an estimate of the tail of the distribution of the production time,
unless the distribution has very low variability in which case the
two estimates will be close. Start with a large value of 𝑀𝐼𝐴𝑇,
such as 𝑀𝐼𝐴𝑇 = 200, and then decrease it by 20 each time you
run the simulation. It is important that 𝑀𝐼𝐴𝑇 does not get smaller
than a critical value 𝑀𝐼𝐴𝑇 ∗ below which the first queue becomes
unstable. That is, it continuously builds up and grows infinitely
big as time goes on. (This is an undesirable situation since the
system should operate within parameters so that the first queue is
always stable. Note that the second queue will never become un-
stable since it has a finite buffer. Also, in this case your simula-
tion program may eventually run out of memory.) For the given
values of the input parameters, the critical value 𝑀𝐼𝐴𝑇 ∗ is ap-
proximately equal to 25. In view of this, exercise caution as you
reduce 𝑀𝐼𝐴𝑇. Keep an eye on 𝑃 and 𝑃c.õ² and stop when they
start increasing rapidly. Graph your results and discuss the ob-
served trends.
119
Harry Perros Simulation Techniques
Chapter 6:
Validation of a simulation model
Validating a simulation model is often neglected despite the fact that
it is a very important part of the process of building a simulation
model. How accurately does a simulation model (or, for that matter,
any kind of model) reflect the operations of the real-life system under
study? How confident are you that the obtained simulation results are
accurate? These are important questions that need to be addressed be-
fore using the results to make decisions.
Let us consider another example where the actual system does not
exist at all. For instance, let us consider a communications equipment
manufacturer who is currently designing a new communications de-
vice, such as a router. Obviously, the manufacturer would like to
know in advance if the new router has an acceptable performance.
Also, the manufacturer would like to know the performance of vari-
ous alternative configurations of the router so that to come up with a
good portfolio. In this case, the performance of the router can be es-
timated through simulation or other mathematical modeling. The
question that arises here is how does one make sure that the model
that will be constructed is a valid representation of the system under
study.
121
Simulation Techniques Harry Perros
The two examples presented above are typical situations that a mod-
eler has to deal with. The following is a list of checks for validating a
simulation model.
122
Harry Perros Simulation Techniques
output validity tests can be carried out by obtaining graphs of
a particular measure of interest for different values of the in-
put parameters and then try to interpret these graphs intuitive-
ly.
Computer projects
Here we describe some validation tests that you can use to validate
your simulation code. Use the code developed in the previous Chap-
ter. You can also devise additional validation tests. If your validation
test fails, then you have to look into your code very carefully and try
to identify the error(s).
𝑊 = 𝐿 / 𝜆(𝛮 − 𝐿)
where
𝜆+𝜇
𝐿 = #𝑁 − + 1$ (1 − 𝑝c )
𝜆
and
%
𝑁! 𝜆 l
𝑝c = 1/ ž Û Ý .
(𝑁 − 𝑛)! 𝜇
l¢c
For each value of 1/𝜆, obtain an estimate of the mean down time
and confidence interval using your simulation model with the
123
Simulation Techniques Harry Perros
batch means method. Graph both sets of results. The theoretical
results should lie within the confidence intervals.
2. The token-based access scheme. Carry out the following two vali-
dation tests.
b. Remove the changes you made in the code in the above task
so that to restore the code in its original state. Assume that 𝑇
is very small, i.e., 𝑇 = 0.001, and that the switch over time is
very small as well, i.e., it is equal to 0.001. Then, in this case
the queues will be served in a round-robin fashion where a
124
Harry Perros Simulation Techniques
maximum of one customer is served from each queue, and the
overhead time due to switch-over time is almost zero. Use the
simulation method with the batch means method to estimate
the mean waiting time 𝑊 in the system, assuming that the
mean service time is the same for all packets and it is equal to
5, and vary the inter-arrival time to each queue from 40 to 20
in increments of 2. (Again, if needed, increase or decrease the
range of values so that to get a full picture of the trend in the
graph.)
Run your simulation model with the batch means method and es-
timate the total mean waiting time in the manufacturing system
and its confidence interval. The theoretical results obtained using
the 𝑀/𝑀/1 queue should lie within the confidence interval ob-
tained from the simulation.
126
Harry Perros Simulation Techniques
Chapter 7:
Variance reduction techniques
NC S U
Variance
Reduction
7.1. Introduction
In Chapter 6, it was mentioned that the accuracy of an estimate is
proportional to 1/√𝑛, where 𝑛 is the sample size. One way to in-
crease the accuracy of an estimate (i.e., reduce the width of its confi-
dence interval) is to increase 𝑛. For instance, the confidence interval
width can be halved if the sample size is increased to 4𝑛. That is, the
accuracy can be increased by running long simulations, which maybe
time consuming. An alternative way to increase the estimate's accura-
cy is to reduce its variance. If one can reduce the variance of an en-
dogenously created random variable without disturbing its expected
value, then the confidence interval width will be smaller, for the same
amount of simulation. Techniques aiming at reducing the variance of
a random variable are known as Variance Reduction Techniques.
Most of these techniques were originally developed in connection
with Monte Carlo Techniques (see section 3.7).
Let us assume that we want to construct the mean and confidence in-
terval of an endogenously created random variable 𝑋. Let 𝑋(1) and
𝑋(2) be the random variable 𝑋 as observed in two separate simula-
tion runs 1 and 2, and let 𝑍 be a random variable defined as follows:
𝑋(1) + 𝑋(2)
𝑍= .
2
That is, let 𝑥\,\ , 𝑥\,f , … , 𝑥\,l be 𝑛 observations of 𝑋 obtained in a sim-
ulation run, and let 𝑥f,\ , 𝑥f,f , … , 𝑥f,l be 𝑛 observations of 𝑋 obtained
in a second simulation run. Then, the 𝑖th value of 𝑍 is:
Ÿ𝑥\,R + 𝑥f,R
𝑧R = , 𝑖 = 1,2, … , 𝑛.
2
As will be seen below, we use the random variable Z to construct the
confidence interval of 𝐸(𝑋). We have
𝑋(1) + 𝑋(2)
𝐸(𝑍) = 𝐸 É Ê
2
1
= '𝐸(𝑋(1)) + 𝐸Ÿ𝑋(2) ( = 𝐸(𝑋),
2
128
Harry Perros Simulation Techniques
since the expected value of 𝑋(1) or 𝑋(2) is that of 𝑋. Thus, the ex-
pected value of this new random variable 𝑍 is identical to that of 𝑋.
Now, let us examine its variance. We have
𝑋(1) + 𝑋(2)
𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟 É Ê
2
\
= { [𝑉𝑎𝑟(𝑋(1)) + 𝑉𝑎𝑟(𝑋(2)) + 2𝐶𝑜𝑣(𝑋(1), 𝑋(2))].
1
𝑉𝑎𝑟(𝑍) = [𝑉𝑎𝑟(𝑋) + 𝐶𝑜𝑣(𝑋(1), 𝑋(2)].
2
The covariance of two random variables 𝑋 and 𝑌 can be written as
1
𝑉𝑎𝑟(𝑍) = Ÿ𝑉𝑎𝑟(𝑋) + 𝜌𝑉𝑎𝑟(𝑋)
2
1
= 𝑉𝑎𝑟(𝑋)(1 + 𝜌),
2
where 𝜌 is the correlation between 𝑋(1) and 𝑋(2).
We note that if 𝜌 < 0, then 𝑉𝑎𝑟(𝑍) < 𝑉𝑎𝑟(𝑋), and in this case we
can use 𝑍 in order to construct the mean and confidence interval of
𝑋, since 𝑍 and 𝑋 have the same mean and Z has a lower variance.
Once we have generated the two sets of observations 𝑥R,\ , 𝑥R,f , … , 𝑥R,l ,
𝑖 = 1,2, we construct the mean and confidence interval estimate of
𝐸(𝑋) using 𝑍 as described above. That is, we calculate a new set of
observations 𝑧\ , 𝑧f , … , 𝑧l where 𝑧R = (𝑥R,\ + 𝑥R,f )/2, and then calcu-
late the mean 𝑧̅ and standard deviation 𝑠* , from which we obtain the
confidence interval of the mean of 𝑍 which is the same as that of 𝑋.
130
Harry Perros Simulation Techniques
Sample size 𝒏 Confidence interval
600 13.86 ± 3.46
900 13.03 ± 2.70
1200 13.11 ± 2.30
1500 12.82 ± 1.99
1800 12.86 ± 1.84
45.0
Per Customer
45.0
Per Customer
35.0
TimeTime
35.0
Waiting
Waiting
25.0
25.0
15.0
15.0
5.0
5.0
1 5 10 15 20 25 30
Simulated Number of Customers
1 5 10 15 20 25 30
Simulated Number of Customers
$ $
1.6 1.6
$
Straight$
Straight
SimulationSimulation
#
Standard#
Standard
Antithetic Antithetic
Variate Technique
Variate Technique
1.2 1.2
Standard Error
! ⁄ " Error
# #
$ $
Standard
$ $
0.8 0.8 $ $$ $
# # #$ #$#$ $
#
0.4 0.4
Figure 7.2: A comparison between straight simulation and the antithetic vari-
ates techniques for an M/M/2 queue
133
Simulation Techniques Harry Perros
in the queue, i.e., 𝑋 is small. Therefore, we see that X and Y can be
negatively correlated.
and
l
1
𝑠* = − 1 ž(𝑧R − 𝑧̅)f .
𝑛
R¢\
𝑍 = 𝑋 − 𝑎 (𝑌 − 𝐸(𝑌)),
134
Harry Perros Simulation Techniques
We select 𝑎 so that to minimize the left-hand side in the above ex-
pression. For this, we set the derivative of the left-hand side to zero.
We have
2𝑎 𝑉𝑎𝑟(𝑌) − 2 𝐶𝑜𝑣(𝑋, 𝑌) = 0
or
𝐶𝑜𝑣(𝑋, 𝑌)
𝑎 = .
𝑉𝑎𝑟(𝑌)
(𝐶𝑜𝑣(𝑋, 𝑌))f
= 𝑉𝑎𝑟(𝑋) −
𝑉𝑎𝑟(𝑌)
Since
𝐶𝑜𝑣(𝑋, 𝑌)
𝜌Ôð = ,
Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌)
We have
f ).
𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋)(1 − 𝜌Ôð
135
Simulation Techniques Harry Perros
|
𝑍 = 𝑋 − ž 𝑎R (𝑌R − 𝐸(𝑌R ),
R¢\
Computer assignments
Consider the machine interference problem. Carry out the following
tasks:
136
Harry Perros Simulation Techniques
Chapter 8: Simulation projects
In this Chapter, we give three simulation projects to help you hone
your simulation skills. These projects are drawn from the area of
computer networks, but as you will see, you do not need to be famil-
iar with the underlying networking technology because each project is
described in a non-technical manner. These projects are:
Note that the first simulation project is broken into three tasks in or-
der to facilitate its development, similar to the way that you devel-
oped your simulation model in the previous chapters. However, this is
not the case for the other two projects, though there are sufficient in-
structions as to how to build the models. Also, good validation data
are given in the first project so you can test the accuracy of your sim-
ulation model; a limited number of validation data is given in the sec-
ond project, and none in the third project. Note that the first part of
the third project may be of interest to the reader since it is a simple
project and at the same time it is a good example of a discrete-time
simulation, something that has not been discussed in this book so far.
137
Simulation Techniques Harry Perros
collect data which they send to the server in the form of messages.
The server processes the messages and issues commands that are sent
to the actuators. There are two types of messages, namely, real-time
(RT) and non real-time (nonRT). RT messages have to be processed
by the server as fast as possible since they represent tasks that need to
be executed in real time. For instance, in the case of a driverless car,
the breaks have to be activated immediately when the radar detects a
person in front of the car. NonRT messages represent non real-time
tasks which are not time constrained.
RT Queue
Arrivals of RT
messages Server
nonRT Queue
Arrivals of nonRT
messages
138
Harry Perros Simulation Techniques
on completion of processing the RT message, the server se-
lects the next message by going back to step 1.
• When the server processes an interrupted nonRT message, it
starts processing it from where it stopped when it was inter-
rupted. (This explains the “resume” term of the name of the
scheduling policy.)
1. An RT arrival occurs
2. A nonRT arrival occurs
3. The processing time of an RT or nonRT message is completed
Note, that the occurrence of one event may trigger the occurrence of
one or more events. Below, we discuss what happens when an event
occurs.
1. An RT arrival occurs
The message joins the RT queue, that is, the number of RT messages
𝑛¶+ currently in the queue is increased by 1. No further action is re-
quired, if there are other messages waiting in the queue. However, if
this message finds the queue empty, then it may be possible that it can
start its processing immediately. Check the status of the server and
schedule a new service time if it is idle. If it is busy serving an RT
message, then no further action is required. If it is busy serving a
nonRT message, then the service is interrupted, and the server starts
processing the RT message. The interrupted nonRT message returns
to the top of the nonRT queue and it will be processed at a later time
per the scheduling algorithm. Its processing will start from where it
stopped when it was pre-empted. Before leaving this part of the logic,
generate the next arrival time of an RT message.
The message joins the nonRT queue, that is, the number of nonRT
messages 𝑛l–l¶+ currently in the queue is increased by 1. No further
action is required, if there are other messages waiting in the queue.
On the other hand, if this message finds the queue empty, then it may
be possible that it can start its processing immediately. Check the sta-
tus of the server, and if idle schedule a new service time. No further
action is required if it is busy serving an RT or a nonRT message. Be-
140
Harry Perros Simulation Techniques
fore leaving this part of the logic, generate the next arrival time of an
nonRT message.
In this case, the server runs the scheduler to determine the next mes-
sage to process. Specifically, it checks first the RT queue. If the num-
ber of messages 𝑛¶+ in the queue is greater than 0, then at least an RT
message is waiting. The server starts processing the first message at
the top of the RT queue and 𝑛¶+ is reduced by one. Schedule the
time-of-service completion. If 𝑛¶+ = 0, then it checks the nonRT
queue. If the number of nonRT messages 𝑛¶+ currently in the queue
is greater than 0, then it starts processing the first message at the top
of the nonRT queue. Schedule the time-of-service completion and
reduce 𝑛l–l¶+ by 1. If the nonRT message has been pre-empted, the
service time is the left-over service at the time when the message was
pre-empted.
Each of the above three events is associated with a clock that gives
the time of completion of the event in the future. Let 𝑅𝑇𝐶𝐿,
𝑛𝑜𝑛𝑅𝑇𝐶𝐿, and 𝑆𝐶𝐿 be the clock for the event of an RT arrival, a
nonRT arrival, and a service completion respectively. In addition, let
𝑀𝐶 be the master clock that gives the current time in the simulation.
Each time an event has been serviced, the simulation logic checks all
the event clocks scheduled to occur in the future. It then advances to
the event with the smallest clock value. Depending on the event, it
takes the following action.
1. Arrival of an RT message
• Set 𝑀𝐶 to 𝑅𝑇𝐶𝐿.
• Message joins the RT queue. Increase 𝑛¶+ by 1.
• Generate the next inter-arrival time 𝐼𝐴𝑇 of an RT message and
set 𝑅𝑇𝐶𝐿 = 𝑀𝐶 + 𝐼𝐴𝑇.
• If 𝑛¶+ = 1, then check if server is idle.
o If yes, message begins processing at the server.
141
Simulation Techniques Harry Perros
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Decrease 𝑛¶+ by 1.
o Set 𝑠 = 1. Recall that 𝑠 is the state of the server: 𝑠 = 0
(idle), 𝑠 = 1 (serving an RT message, 𝑠 = 2 (serving a
nonRT message).
• If not, check if the server is busy serving a nonRT message. If
yes, the nonRT message is preempted and the RT message
begins processing.
o If the remaining service time of the pre-empted nonRT
message is not zero, then store the remaining service time,
return the pre-empted message to the nonRT queue, and
increase 𝑛l–l¶+ by 1.
o If the remaining service time of the pre-empted nonRT
message is zero, then the nonRT message has completed
its service and it departs from the system. (Note: this con-
dition can only happen when the clocks are integers,
which is what we assume in tasks 1 and 2.1. In tasks 2.2
and 3 the clocks are defined as real variables, which
means that two event clocks can never be the same.)
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Decrease 𝑛¶+ by 1.
o Set 𝑠 = 1.
• Go back to the beginning to locate the next event.
• Set 𝑀𝐶 to 𝑛𝑜𝑛𝑅𝑇𝐶𝐿.
• Message joins the nonRT queue. Increase 𝑛l–l¶+ by 1.
• Generate the next inter-arrival time 𝐼𝐴𝑇 of a nonRT message
and set 𝑛𝑜𝑛𝑅𝑇𝐶𝐿 = 𝑀𝐶 + 𝐼𝐴𝑇.
• If 𝑛l–l¶+ = 1, then check if server is idle. If yes, message be-
gins processing at the server.
o Determine the length of the service time 𝑆𝑇 and set 𝑆𝐶𝐿 =
𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 2.
• Decrease 𝑛l–l¶+ by 1.
142
Harry Perros Simulation Techniques
• Go back to the beginning to locate the next event.
3. Service completion
• Set 𝑀𝐶 to 𝑆𝐶𝐿.
• Check the RT queue.
o If there is an RT message waiting in the queue, calculate
its service time 𝑆𝑇 and set 𝑆𝐶𝐿 = 𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 1.
o Decrease 𝑛¶+ by 1.
• If RT queue is empty, check the nonRT queue.
o If there is a nonRT message waiting in the queue calculate
its service time 𝑆𝑇 and set 𝑆𝐶𝐿 = 𝑀𝐶 + 𝑆𝑇.
o Set 𝑠 = 2.
o Decrease 𝑛l–l¶+ by 1.
• If both queues are empty, the server becomes idle. Set 𝑠 = 0.
• Go back to the beginning to locate the next event.
Note that it is possible that an arrival event may occur at the same
time as a service completion event. In this case, first execute the ser-
vice completion event and show the resulting status variables in a
new line in the table and then execute the arrival event and show the
results in the next line. Alternatively, execute both events and show
the resulting status variables in a single line in the table. The problem
of simultaneously occurring events will go away in the subsequent
two tasks because all the clocks will be defined as real variables.
143
Simulation Techniques Harry Perros
Task 1.1: Continue the hand simulation started in table 8.1 until
𝑀𝐶 = 50. (The last column in the table gives the remaining service
time 𝑅𝑆𝑇 of a pre-empted nonRT message.) Check your hand simula-
tion table against the correct solution given in section 8.1.5 to make
sure that you have not made any errors.
Task 1.2: Do the hand simulation started in table 8.2 until 𝑀𝐶 = 20.
This is the same simulation as above but now we have switched the
input parameters as follows. The inter-arrival time of RT messages is
constant equal to 5, the inter-arrival time of nonR is constant equal to
10, the service time of an RT message is constant equal to 4, and the
service time of a nonRT message is constant equal to 2. The initial
conditions are the same. Compare your hand simulation table with the
144
Harry Perros Simulation Techniques
correct solution given in section 8.1.5 to make sure that you have not
made any errors.
Print a line of output each time you handle an event, i.e., you advance
the master clock, with the same information as in the tables in task 1.
Run your simulation until 𝑀𝐶 exceeds 200.
145
Simulation Techniques Harry Perros
8.1.4. Task 3: Statistical estimation of the response time
The objective of this task is to use the simulation you developed in
task 2 in order to calculate the mean and 95th percentile and their
confidence intervals of the response time 𝑅¶+ and 𝑅l–l¶+ of the RT
and nonRT messages respectively. We recall that 𝑅¶+ is the time
elapsed from the moment an RT message arrives at the RT queue to
the moment it is fully processed and departs from the server. 𝑅l–l¶+
is defined similarly.
146
Harry Perros Simulation Techniques
• Calculate the mean, 95th percentile and their confidence in-
terval of 𝑅¶+ and 𝑅l–l¶+ .
The correct plots for the above results are given in section 8.1.5. Your
results may not match entirely these plots due to the randomness in
the simulation.
147
Simulation Techniques Task 1.1
Harry Perros
MC RTCL nonRTCL n_RT n_nonRT SCL Status (s) Pre-empted time
0 3 5 0 Task 1.1 0 4 2 0
MC 3 RTCL 13 nonRTCL5 n_RT 0 n_nonRT1 SCL 5 Status (s)1 Pre-empted time 1
50 13
3 10
5 00 02 45 21 01
53 13
13 10
5 00 11 56 12 10
65 13
13 10
10 00 20 10
5 12 10
105 13
13 15
10 00 11 10
6 22 00
106 13
13 15
10 00 00 14
10 22 00
13
10 23
13 15
15 00 11 15
10 21 01
15
10 23
13 20
15 00 02 15
14 21 01
15
13 23 20
15 0 11 16
15 12 10
16
15 23 20 0 20 20
15 12 10
20
15 23 25
20 0 11 20
16 22 00
20
16 23 25
20 0 00 24
20 22 00
23
20 33
23 25 0 11 25
20 21 01
25
20 33
23 30
25 0 02 25
24 21 01
25
23 33 30
25 0 11 26
25 12 10
26
25 33 30 0 20 30
25 12 10
30
25 33 35
30 0 11 30
26 22 00
30
26 33 35
30 0 00 34
30 22 00
33
30 43
33 35 0 11 35
30 21 01
35
30 43
33 40
35 0 02 35
34 21 01
35
33 43 40
35 0 11 36
35 12 10
36
35 43 40 0 20 40
35 12 10
40
35 43 45
40 0 11 40
36 22 00
40
36 43 45
40 0 00 44
40 22 00
43
40 53
43 45 0 11 45
40 21 01
45
40 53
43 50
45 0 02 45
44 21 01
45
43 53 50
45 0 11 46
45 12 10
45
46 53 50 0 20 45
50 12 10
45
50 53 50
55 0 11 46
50 22 00
46
50 53 50
55 0 00 50
54 22 00
50 53 55 0 1 50 2 0
50 53 55 0 Task 1.2 0 54 2 0
MC RTCL Table 8.3: Correct
nonRTCL num_RT hand simulation
num_nonRT SCL forStatus
task 1.1Pre-empted time
0 3 5 0 Task 1.2 0 4 2 0
MC 3 RTCL 8 nonRTCL 5 num_RT 0 num_nonRT 1 SCL 7 Status 1 Pre-empted time 1
50 3
8 5
15 0 02 47 21 01
73 8 5
15 0 11 78 12 10
85 8
13 15 0 21 7
12 11 10
127 8
13 15 0 10 8
14 22 00
138 13
18 15 0 11 12
17 11 01
12
15 13
18 15
25 0 02 14
17 21 01
13
17 18 15
25 0 11 17
18 12 10
15
18 18
23 25 0 21 17
22 11 10
17
22 18
23 25 0 10 18
24 22 00
18 23 25 0 1 22 1 0
22 23 25 0 0 24 2 0
!"#$%&%'(
Figure 8.2: Mean and confidence intervals for RT and nonRT messages
!"#$%&%'(
The mean for RT remains almost constant whereas for Non-RT is decreasing. This is because
in real-time jobs,Figure 8.3: 𝟗𝟓th
the interarrival percentile
time does andthe
not impact confidence intervals
response time due to the fact that
for RTstarts
once a new job arrives, it immediately andprocessing
nonRT messages
(except when the server is already
processing a real-time job) since they have higher priority. Hence, the response time remains
almost constant and nearer to the service time of a real-time job.
149
In the case of non real-time jobs, as the interarrival time increases the response time decreases
because the larger IAT allows more jobs in the queue to be processed and hence decreases the
pending queue length. The jobs do not need to wait for a long time to start processing (if there
are no jobs in RT queue) and hence are also completed quickly, thereby reducing the response
Simulation Techniques Harry Perros
8.2. A simulation of the flow of SIP messages
In order to make a voice or video call, many messages are exchanged
between the calling and the called devices prior to the moment where
we start to speak. Several specialized servers are also involved in this
message exchange to provide different functionalities, such as, au-
thentication and location of the called device. These messages are
used to setup the call, modify it, and finally tear it down when the us-
ers hang up. SIP is a very popular protocol that defines the structure
of these messages, the functionality of the various servers, and the
flow of messages between the two devices.
For those familiar with SIP, in this simulation project you will model
the flow of SIP messages through a P-CSCF, an S-CSCF, and an ap-
plication server (AS). One does not have to be familiar with the con-
tent of the SIP messages and the functionality of the SIP servers,
since in this simulation we abstract the problem into a simulation of
the flow of messages through a number of service stations, each rep-
resenting a different SIP server. Such a network of interconnected
service stations through which customers flow following different
paths is known as a queueing network. These queueing models are
technology independent and they are used to model many situations,
such as, the flow of jobs in a manufacturing process, the flow of que-
ries to a web server through the different web services that are in-
volved in the processing of a query, the flow of parcels ordered on-
line through a delivery system, and the flow of IP packets through a
computer network.
AU
!
Figure 8.4: The queueing system under study
150
Harry Perros Simulation Techniques
The queueing network under study consists of three service stations,
namely, P-CSCF, S-CSCF, and AS, as shown in figure 8.4. A cus-
tomer in this queueing network represents a SIP message, and the
flow of customers through the three service stations is depicted by the
circled numbers shown in figure 8.4. Each service station consists of
a queue where customers are waiting and a server that serves the cus-
tomers. (Here the word “server” means a service point and not a
computer server!) A customer arrives at the P-CSCF queue (step 1),
waits for its turn, gets served and then moves into the S-CSCF queue
(step 2). There it waits for its turn and gets processed by the server.
Upon service completion, the customer moves to the AS queue (step
3). After the AS processes the request, the customer goes back and
joins the S-CSCF queue (step 4), and then after it is processed it goes
back and joins the P-CSCF queue (step 5). Upon service completion,
it departs from the system (step 6). We observe that in the first two
queues we have new customers moving towards the AU server, and
recirculated ones going backwards. We need to distinguish between
these two types of customer, so we know how to route them.
The inter-arrival time of new customers at the P-CSCF and the ser-
vice times at the three queues are assumed to be exponentially dis-
tributed. Let 𝐼𝐴𝑇 be the mean inter-arrival time at the P-CSCF queue,
and let 𝑀𝑆𝑃, 𝑀𝑆𝑆, 𝑀𝑆𝐴 be the mean service at the P-CSCS, S-CSCF
and AS queues, respectively. Customers are processed in each queue
in the order in which they arrive, that is, in first in first out (FIFO)
manner.
For each event we define a clock, which gives the time when the
event will occur. Let 𝐴𝐶, 𝑆𝐶𝑃, 𝑆𝐶𝑆, and 𝑆𝐶𝐴 be the arrival clock,
service completion clock at the P-CSCF, service completion clock at
the S-CSCF, and service completion clock at the AU, respectively.
Also, let 𝑀𝐶 be the master clock.
For the simulation model, will we use the event-advance design. That
is, after completing servicing an event, we check all the future events
to see which of them will occur next. Subsequently, we advance
𝑀𝐶 to the time of occurrence of this event and take appropriate ac-
tion, and then we go back to locate the next event, and so on until a
stopping rule is satisfied. Below, we describe the actions taken when
an event occurs.
The first task is to decide when the next arrival of a new customer
will occur. For this, we generate an exponential variate 𝑡 with a mean
𝐼𝐴𝑇 and update the arrival clock, i.e., 𝐴𝐶 = 𝑀𝐶 + 𝑡. Recall that to
generate an exponential variate 𝑡, we first generate a pseudo-random
number 𝑟 and then set 𝑡 = −(𝑚𝑒𝑎𝑛)𝑙𝑜𝑔Ø (𝑟), where 𝑚𝑒𝑎𝑛 is the
mean of the exponential distribution.
If the queue is empty and the server is idle, the new customer goes
into service and for this we generate an exponentially distributed ser-
vice time 𝑡 with mean 𝑀𝑆𝑃 and update the service completion clock
𝑆𝐶𝑃, i.e., 𝑆𝐶𝑃 = 𝑀𝐶 + 𝑡. If the server is busy, then the customer
joins the queue and no further action is taken.
If the departing customer arrived from the P-CSCF, then it joins the
AS queue. Otherwise, it joins the P-CSCF queue. The rest of the logic
is the same as above. That is, if the customer finds the P-CSCF or AS
server idle (i.e., it is not busy and no one is waiting in the queue),
then it starts a new service and the appropriate service completion
clock is updated. Otherwise, it just joins the queue. In addition, if
there are customers waiting in the S-CSCF queue, then the next in
line starts service.
153
Simulation Techniques Harry Perros
The objective of this simulation is to estimate the mean and the 95th
percentile and confidence intervals of the end-to-end delay. This is
the amount of time elapsed from the moment a customer arrives at the
P-CSCS queue as a new customer to the moment it departs from the
P-CSCF queue as a recirculated customer after it has visited the other
queues.
Start your simulation assuming that all the queues are empty. In order
to get the simulation going you need to pre-generate the time that the
first arrival will occur at the P-CSCF queue. In order to eliminate the
effects of the initial condition, run the simulation for 100 departures
first, and after that, start the batch method. Simulate 30 batches of
2000 observations, i.e., departures from the queueing network. For
each batch calculate the mean and the 95th percentile of the end-to-
end delay, and at the end of the simulation construct their confidence
intervals.
Set 𝑀𝑆𝑃 = 0.1 sec, 𝑀𝑆𝑆 = 0.2 sec, and 𝑀𝑆𝐴 = 0.5 sec, and run
your simulation for different values of the 𝐼𝐴𝑇, ranging from 0.67 to
2 in increments of 0.15. Plot your mean end-to-end delay and its con-
fidence interval as a function of 𝐼𝐴𝑇. Same for the 95th percentile.
Comment on your results. Which SIP server in your opinion may be-
come a bottleneck?
You can validate your simulation model using the following two data
points:
Note that your results maybe slightly different due to the randomness
in the simulation.
154
Harry Perros Simulation Techniques
8.3. A simulation model of a multiplexer
A multiplexer is a device used in computer networks to merge many
different streams of packets originating from different locations into a
single stream that is transmitted out to a single destination. Figure 8.5
gives an example of a multiplexer with four arrival streams. The
packets, indicated by small colored orthogonals, join a common
queue from where they are transmitted out one at a time. The queue is
referred to as the buffer and its size is finite with capacity 𝐵. That is,
the number of packets waiting in the buffer plus the one being trans-
mitted out cannot exceed the value 𝐵. This means that if a packet ar-
rives at a time when the buffer is full, the packet is lost. Therefore, of
interest is how to size the buffer so that only a given percentage of
packets are lost.
Stream 1
Stream 2
Stream 3
B
Stream 4
8 7 6 5 4 3 2 1
slots
Figure 8.5: The multiplexer with four arrival streams
155
Simulation Techniques Harry Perros
The time axis is slotted, and each slot is long enough to accommodate
the transmission of a packet. That is, it takes one slot to transmit out a
packet from the buffer, and one time slot for a packet to arrive at the
buffer. For simplicity, we will assume in this model that there are four
arriving streams of packets, as shown in figure 8.5. The arrival of
packets for all four streams is synchronized. That is, in slot 𝑖, we may
have an arrival from each stream, up to a total of four arrivals, and
also during the same slot, we may have a departure from the buffer.
For instance, let us consider time slot 1 in figure 8.5, and let us as-
sume that there are 3 packets in the buffer, one of which is being
transmitted out. Then at the end of the time slot 1, a packet has de-
parted from the buffer and 4 new packets have arrived. That is, now
the buffer contains 6 packets. At the end of time slot 2, we have one
arrival and one departure, which means that the buffer still contains 6
packets. At the end of slot 3, we have two arrivals and one departure,
and the buffer contains 7 packets, and so on.
156
Harry Perros Simulation Techniques
At the end of each slot do the following:
• If the buffer has at least one packet, then one packet departs.
Update the total number of packets in the buffer.
• Draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then a packet
from stream 1 has arrived at the buffer. Else, no arrival from
stream 1.
• Repeat above step for streams 2, 3, and 4.
• Calculate how many of these arriving packets will be admitted
to the buffer, based on the current number of packets in the
buffer, and update the number of packets in the buffer.
• Keep a counter 𝑁 of the total number of arrivals and another
counter 𝐿 of the total number of lost packets.
For the initial conditions assume that the buffer is empty. There is no
need to make an assumption as to when the first packet in each stream
will arrive, as we have done in previous simulations.
8.3.2. Results
The objective of this simulation is to calculate the packet loss 𝑝ö–˜˜
for different values of 𝐵 and 𝑝. The packet loss is defined as the per-
centage of the arriving packets that do not enter the buffer, i.e.,
𝑝ö–˜˜ = 𝐿/𝑁. The estimation of the confidence interval can be done
using the batch means method as described in section 5.7, after we
allow for the warm-up period. That is, let 𝑝ö–˜˜,R be the packet loss
calculate in batch 𝑖, 𝑖 = 1,2, … 𝑚. Then, the confidence interval is:
𝑠 𝑠
Û𝑝̅ö–˜˜ − 𝑡c.õ² , 𝑝̅ö–˜˜ + 𝑡c.õ² Ý
√𝑚 √𝑚
where
| |
1 1 f
𝑝̅ö–˜˜ = ž 𝑝ö–˜˜,R and 𝑠 f = žŸ𝑝ö–˜˜,R − 𝑝̅ .
𝑚 𝑚−1
R¢\ R¢\
Stream 1
voice
Stream 2
video
Stream 3
data
Stream 4
8 7 6 5 4 3 2 1
slots
B
Figure 8.6: The multiplexer with priorities
The three queues share the total buffer space 𝐵. In order to make sure
that each queue 𝑖 does not grow too big so that it takes up most of the
buffer space 𝐵, we impose a low bound 𝐿R and an upper bound 𝑈R .
The upper bound is used to limit how big the 𝑖th queue can grow, and
it is selected so that the sum of the upper bounds of the four queues is
larger than 𝐵. The lower bound 𝐿R can be seen part of the duffer that
is dedicated permanently to the 𝑖th queue. That is, the 𝐿R buffer spac-
es are only used to store packets belonging to the 𝑖th queue. The sum
of the lower bounds of the four queues is less than 𝐵. Therefore, the
total number of buffer spaces that can be shared by the three queues is
𝐵 − 𝐿.
In order to clarify further how the upper and lower bounds work, let
us consider the 𝑖th queue and let us assume that it is empty. The first
packet that arrives to the 𝑖th queue is stored in the buffer, and the total
number of d spaces in the dedicated buffer of the 𝑖th queue is reduced
by one. This continues until all dedicated spaces for the 𝑖th queue are
used. When a packet arrives to the 𝑖th queue at a time when all dedi-
cated spaces have been used, the packet will be accepted if the fol-
lowing two conditions should be satisfied:
• The total number of packets in the 𝑖th queue is less than its
upper bound 𝑈R .
• For each queue 𝑖, calculate the number of packets 𝑥R which are
over its lower bound 𝐿R . If the number of packets in the 𝑖th
queue is less or equal to 𝐿R then 𝑥R = 0. Then, the sum 𝑥\ +
𝑥f + 𝑥e has to be less than 𝐵 − 𝐿.
The simulation logic is the same as in the simulation model described
above with the following additions. First, we have to decide which of
the three queues a new arrival will join, and whether it will be saved
in the buffer or it will be rejected, i.e., it will be lost. Also, we have to
159
Simulation Techniques Harry Perros
implement the priority scheme described above. The main logic of the
model is summarized below.
• Select the next queue to serve, and if it is not empty then one
packet departs. Update the total number of packets in the
queue. If all queues are empty, then no departure occurs.
• Draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then a packet
from stream 1 has arrived at the buffer. Else, no arrival from
stream 1. If we have an arrival, then:
o Draw a pseudo-random number r.
o If 𝑟 ≤ 0.20, then the new arrival is a voice packet.
o If 0.20 < 𝑟 ≤ 0.40, then the new arrival is a video
packet.
o If 0.40 < 𝑟 ≤ 1, then the new arrival is a data packet.
Once the type of packet has been identified determine whether
the packet is accepted or rejected.
• Repeat above step for streams 2, 3, and 4.
• Keep a counter 𝑁R of the total number of arrivals and another
counter 𝐾R of the total number of lost packets for each queue
𝑖, 𝑖 = 1,2,3.
At the end of the simulation, the packet loss per queue 𝑖 is determined
using the expression: 𝐿R /𝐾R , 𝑖 = 1,2,3. The confidence interval of the
packet loss per queue can be estimated using the batch means method
as explained above in section 8.3.2. Notice that the batch means
method has to be applied for each queue separately. As before, ignore
the first 100 arrivals so that to account for the effects of the initial
conditions. Then, run your simulation until you have generated the
required number of arrivals, i.e., observations, for each queue. As a
result, you will generate more arrivals for a queue than you need. You
can ignore the additional arrivals or simply use them to create addi-
tional batches.
161