Top Banner
Is CPU queue empty? Is MCL = tarr ? b b b b 2 1 2 4 5 6 3 3 9 9 6 7 MCL = tarr MCL = MCL + 1 Clients CPU A new arrival event occurs A new arrival event occurs A departure event occurs Yes Yes No Computer Simulation Techniques: The denitive introduction! Harry Perros
175

Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Aug 16, 2021

Download

Documents

dariahiddleston
Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

i

Is CPU queue

empty?

Is MCL = tarr

?

b

b

b

b

2

1

2

4 5 6

3

3

9 96

7

MCL = tarr MCL = MCL + 1

Clients

CPU

A new arrival event occurs

A new arrival event occurs

A departure event occurs

Yes

Yes

No

Computer Simulation Techniques:The definitive introduction!

Harry Perros

Page 2: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

ii

Page 3: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

iii

Computer Simulation Techniques: The definitive introduction!

Harry Perros

Page 4: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

iv

Copyright © 2021 by 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.

Page 5: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

v

To Isabelle

Page 6: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished
Page 7: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

vii

Table of Contents FORWARD.......................................................................................................................XIAUTHOR........................................................................................................................XIIICHAPTER1:INTRODUCTION......................................................................................11.1.INTRODUCTION.......................................................................................................................11.2.BUILDINGASIMULATIONMODEL.........................................................................................21.4.ATOKEN-BASEDACCESSSCHEME.........................................................................................91.5.ATWO-STAGEMANUFACTURINGSYSTEM........................................................................15PROBLEMS.....................................................................................................................................21COMPUTERPROJECTS..................................................................................................................22

CHAPTER2:GENERATIONOFPSEUDO-RANDOMNUMBERS........................252.1.INTRODUCTION....................................................................................................................252.2.PSEUDO-RANDOMNUMBERS..............................................................................................262.3.THECONGRUENTIALMETHOD...........................................................................................282.3.1.GENERALCONGRUENTIALMETHODS............................................................................302.3.2.COMPOSITEGENERATORS...............................................................................................302.4.TAUSWORTHEGENERATORS..............................................................................................312.5.THELAGGEDFIBONACCIGENERATORS............................................................................312.6.STATISTICALTESTSFORPSEUDO-RANDOMNUMBERGENERATORS.............................332.6.1.HYPOTHESISTESTING......................................................................................................332.6.2.FREQUENCYTEST(MONOBITTEST)..............................................................................402.6.3SERIALTEST......................................................................................................................412.6.4.RUNSTEST.........................................................................................................................442.6.5.CHI-SQUAREDTESTFORGOODNESSOFFIT..................................................................44PROBLEMS.....................................................................................................................................45COMPUTERPROJECTS..................................................................................................................45

CHAPTER3:GENERATINGSTOCHASTICVARIATES.........................................473.1.INTRODUCTION....................................................................................................................473.2.THEINVERSETRANSFORMATIONMETHOD......................................................................473.3.SAMPLINGFROMCONTINUOUS-TIMEPROBABILITYDISTRIBUTIONS...........................503.3.1.SAMPLINGFROMAUNIFORMDISTRIBUTION...............................................................503.3.2.SAMPLINGFROMANEXPONENTIALDISTRIBUTION.....................................................513.3.3.SAMPLINGFROMANERLANGDISTRIBUTION...............................................................523.3.4.SAMPLINGFROMANORMALDISTRIBUTION.................................................................533.4.SAMPLINGFROMDISCRETE-TIMEPROBABILITYDISTRIBUTIONS.................................553.4.1.GENERATINGBINOMIALDISTRIBUTEDSTOCHASTICVARIATES.................................553.4.2.GENERATINGGEOMETRICALLYDISTRIBUTEDSTOCHASTICVARIATES.....................553.4.3.GENERATINGPOISSONDISTRIBUTEDSTOCHASTICVARIATES...................................563.5.GENERATINGSTOCHASTICVARIATESFROMANEMPIRICALDISTRIBUTION................583.6.THEREJECTIONMETHOD....................................................................................................60

Page 8: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

viii

COMPUTERPROJECTS...................................................................................................................61CHAPTER4:SIMULATIONDESIGNS.......................................................................634.1.INTRODUCTION.....................................................................................................................634.2.EVENT-ADVANCEDESIGN....................................................................................................634.3.FUTUREEVENTLIST............................................................................................................644.4.EVENTLISTSTOREDINASEQUENTIALARRAY.................................................................654.5.EVENTLISTSTOREDINALINKEDLIST..............................................................................664.5.1.DEFININGALINKEDLIST.................................................................................................684.5.2.CREATIONOFANEWNODE.............................................................................................694.5.3.DELETIONOFANODE.......................................................................................................704.5.4.INSERTINGANODEINALINKEDLIST.............................................................................704.5.5.REMOVINGTHEFIRSTNODEOFALINKEDLIST............................................................734.5.6.TIMECOMPLEXITY............................................................................................................744.5.7.DOUBYLINKEDLISTS.......................................................................................................754.6.UNIT-TIMEADVANCEDESIGN.............................................................................................754.6.1.SELECTINGAUNITTIME..................................................................................................794.6.2.IMPLEMENTATION............................................................................................................804.6.3.EVENT-ADVANCEVS.UNIT-TIMEADVANCE..................................................................804.7.ACTIVITY-BASEDSIMULATIONDESIGN.............................................................................804.8.EXAMPLES.............................................................................................................................834.8.1.ANINVENTORYSYSTEM...................................................................................................834.8.2.AROUND-ROBINQUEUE..................................................................................................85PROBLEMS.....................................................................................................................................90COMPUTERPROJECTS...................................................................................................................91

CHAPTER5:ESTIMATIONTECHNIQUESFORANALYZINGENDOGENOUSLYCREATEDDATA...........................................................................935.1.INTRODUCTION.....................................................................................................................935.2.COLLECTINGENDOGENOUSLYCREATEDDATA.................................................................935.3.TRANSIENTSTATEVSSTEADY-STATESIMULATION........................................................965.3.1.TRANSIENT-STATESIMULATION....................................................................................965.3.2.STEADY-STATESIMULATION...........................................................................................965.4.ESTIMATIONTECHNIQUESFORSTEADY-STATESIMULATION........................................975.5.ESTIMATIONOFTHECONFIDENCEINTERVALOFTHEMEAN.........................................985.5.1.ESTIMATIONOFTHEAUTOCORRELATIONFUNCTION(ACF)..................................1035.5.2.THEBATCHMEANSMETHOD........................................................................................1065.5.3.THEREPLICATIONMETHOD.........................................................................................1075.6.ESTIMATIONOFTHECONFIDENCEINTERVALOFAPERCENTILE................................1095.7.ESTIMATIONOFTHECONFIDENCEINTERVALOFAPROBABILITY..............................1105.8.ESTIMATIONTECHNIQUESFORTRANSIENT-STATESIMULATION..............................111COMPUTERPROJECT..................................................................................................................112

CHAPTER6:VALIDATIONOFASIMULATIONMODEL..................................121COMPUTERPROJECTS................................................................................................................123

Page 9: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

ix

CHAPTER7:VARIANCEREDUCTIONTECHNIQUES........................................1277.1.INTRODUCTION.................................................................................................................1277.2.THEANTITHETICVARIATESTECHNIQUE.......................................................................1287.3.THECONTROLVARIATESTECHNIQUE............................................................................133COMPUTERASSIGNMENTS.......................................................................................................136

CHAPTER8:SIMULATIONPROJECTS..................................................................1378.1.ASIMULATIONOFACPUPRIORITYSCHEDULER.........................................................1378.1.1.THESIMULATIONLOGIC...............................................................................................1398.1.2.TASK1:HANDSIMULATION........................................................................................1438.1.3.TASK2:WRITETHEBASICSIMULATIONCODE.........................................................1458.1.4.TASK3:STATISTICALESTIMATIONOFTHERESPONSETIME.................................1468.1.5.CORRECTSIMULATIONRESULTS.................................................................................1478.2.ASIMULATIONOFTHEFLOWOFSIPMESSAGES..........................................................1508.2.1.THESIMULATIONLOGIC..............................................................................................1518.2.2.IMPLEMENTATIONANDRESULTS...............................................................................1538.3.ASIMULATIONMODELOFAMULTIPLEXER...................................................................1558.3.1.SIMULATIONLOGIC.......................................................................................................1568.3.2.RESULTS.........................................................................................................................1578.3.3.ANEXTENSIONOFTHESIMULATIONMODEL.............................................................158

Page 10: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished
Page 11: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

xi

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]).

Copyright: Please remember that it is illegal to reproduce parts of this book or all of the book in other publications without my consent.

Page 12: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

xii

Please acknowledge this book, if you use it in a course, or in a project, or in a publication.

Acknowledgment: My many thanks to many students at NC State University for their contributions to the writing of this book.

Enjoy!

Harry Perros, February 2021

Page 13: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

xiii

Author

HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished Graduate Professor, NC State University, and an IEEE Fellow. He received the B.Sc. degree in Mathematics in 1970 from Athens University, Greece, the M.Sc. degree in Operation-al Research with Computing from Leeds University, England, in 1971, and the Ph.D. degree in Operations Research from Trinity Col-lege Dublin, Ireland, in 1975. He has held visiting faculty positions at INRIA, Rocquencourt, France (1979), NORTEL, Research Triangle Park, North Carolina (1988-89 and 1995-96), University of Paris 6, France (1995-96, 2000, 2002, 2012), University of Paris 13, France (2005-2006), and Victoria University, Wellington, New Zealand (2006).

He has published 220 journal and conference papers in the area of performance modeling of computer and communication systems, and he has organized several international conferences. He has also pub-lished seven print books: Queueing Networks with Blocking: Exact and Approximate Solutions, Oxford University Press 1994, An Intro-duction to ATM Networks, Wiley 2001, Connection-Oriented Net-works, Wiley 2005, Networking Services: QoS, Signaling, Processes, Amazon.com 2014, VBR Video Traffic Models, Wiley-ISTE 2014, Bandwidth Allocation for Video under QoS Constraints, Wiley-ISTE 2015, and An Introduction to IoT Analytics, CSC press 2021, and a free e-book Computer Simulation Techniques – The Definitive Intro-duction, 2002, revised in 2021.

In 1995 he founded the IFIP Working Group 6.3 on the Performance of Communication Systems, and he was the chairman from 1995 to 2002. He served as an associate Editor in several Journals and edited several special issues and conference proceedings. He was also the co-founder and program coordinator of the Master of Science degree in Computer Networks at NC State University.

He is an avid sailor, and he also enjoys playing music, reading, cook-ing and traveling.

Page 14: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

xiv

Page 15: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

1

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.

Simulation is part of a collection of modeling tools used in Opera-tions Research (OR) to study the performance of systems in many diverse domains, such as, manufacturing, the internet, transportation, and health, to name a few. A model is a representation of a real-life system. In general, models can be classified into: iconic, analogue, and symbolic.

An iconic model is an exact replica of the properties of the real-life system, but smaller in scale. Examples are: model airplanes, maps, etc. An analogue model uses a set of properties to represent the prop-erties of a real-life system. For instance, a hydraulic system can be used as an analogue of electrical, traffic and economic systems. Sym-bolic models represent the properties of the real-life system through the means of symbols, such as mathematical equations and computer programs. Simulation models are symbolic models.

Symbolic models are classified into two groups: deterministic models and stochastic models. Deterministic models are models which do not contain the element of probability. These are typically optimization techniques, such as, linear programming, non-linear programming and dynamic programming. Stochastic models are models which con-tain the element of probability, such as queueing theory and stochas-

Page 16: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

2

tic processes. Simulation techniques rely heavily on the element of randomness. However, deterministic simulation techniques in which there is a no randomness, are not uncommon.

1.2. Building a simulation model Any real-life structure that is modelled using simulation, or any other OR model, is viewed as a system. A system is a collection of entities which are logically related and which are of interest to a particular application. The following features of a system are of interest:

• Environment: Each system can be seen as a subsystem of a broader system.

• Interdependency: No activity takes place in total isolation. • Sub-systems: Each system can be broken down to sub-

systems. • Organization: Virtually all systems consist of highly orga-

nized elements or components, which interact in order to carry out the mission of the system.

• Change: The present condition or state of the system usually varies over a long period of time.

Figure 1.1: Beard's managerial pyramid

When building a simulation model of a real-life system, one does not simulate the whole system. Rather, one simulates those sub-systems which are related to the problems at hand. This involves modeling parts of the system at various levels of detail. This can be graphically depicted using Beard's managerial pyramid shown in figure 1.1. The

Levels of Detail

Page 17: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

3

collection of the shaded areas form the parts of the system that are incorporated in the model.

A simulation model is, in general, used in order to study real-life sys-tems which do not currently exist. In particular, one is interested in quantifying the performance of a system under study for various val-ues of its input parameters. Such quantified measures of performance can be very useful in the managerial decision process. The basic steps involved in carrying out a simulation exercise are depicted in figure 1.2.

Figure 1.2: Basic steps involved in carrying out a simulation study

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.

Define the Problem

Analyze Data

Formulate Sub-Models

Combine Sub-Models

Collect Data

Write the Simulation Program

Debug

Validate Model

Design Simulation Experiments

Run the Simulator

Analyze the Results

Implement Results

(alternatives)

a

a

Earlier Steps

Page 18: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

4

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.

Some of the variables of the system are of paramount importance as they are used to define the state of the system. These variables are known as status variables, and they form the backbone of any simula-tion model. During a simulation run, these variables indicate the state of the system at any time instance. The selection of these variables is affected by the type of information one wants to maintain about the simulated system.

Below, we describe the basic simulation methodology through three different examples, namely, the machine interference problem, a to-ken-based access scheme, and a two-stage manufacturing system.

1.3. The machine interference problem

Let us consider a single server queue with a finite population known as the machine interference problem. This problem arose originally out of a need to model the behavior of machines. Later on, it was used extensively in computer modeling. Let us consider 𝑀 machines. Each machine is operational for a period of time and then it breaks down. We assume that there is one repairman. A machine remains broken down until it is fixed by the repairman. Broken-down machines are served in a FIFO manner, and the service is non-preemptive. The total down time of a machine is made up of the time it has to "queue" for the repairman and the time it takes for the repairman to fix it. A ma-chine becomes immediately operational after it has been fixed. Thus, each machine the cycle shown in figure 1.3, which is repeated contin-uously.

Page 19: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

5

Figure 1.3: The basic cycle of a machine

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.

Figure 1.4: The machine interference problem

Let us now look at the repairman's queue. This can be visualized as a single server queue fed by a finite population of machines, i.e., cus-tomers, as shown in figure 1.4

For simplicity, we assume that the operational time of each machine is equal to 10 units of time. Also, the repair time of each machine is equal to 5 units of time. In other words, we assume that all the ma-chines have identical constant operational times. They also have iden-tical and constant repair times.

The first and most important step in building a simulation model of the above system, is to identify the basic events whose occurrence will alter the status of the system. This brings up the problem of hav-ing to define the state variables of the above problem. The selection

Finite Population

of Machines

Repairman

Page 20: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

6

of the status variables depends mainly upon the type of performance measures that we wish to obtain.

In this problem, the most important status variable is 𝑛, the number of broken-down machines, i.e., those waiting in the queue plus the one being repaired.

• If 𝑛 = 0, then we know that the queue is empty and the re-pairman is idle.

• If 𝑛 = 1, then the queue is empty and the repairman is busy. • If 𝑛 > 1, then the repairman is busy and there are 𝑛 − 1

broken-down machines in the queue.

Now, there are two events whose occurrence will cause 𝑛 to change. These are:

1. A machine breaks down, i.e., an arrival occurs at the queue. 2. A machine is fixed, i.e., a departure occurs from the queue.

The flow-charts given in figures 1.5, and 1.6 show what happens when each of these events occur.

In order to incorporate the two basic events in the simulation model, we need a set of variables, known as clocks, which keep track of the time instants at which an arrival or departure event will occur. In par-ticular, for this specific model, we need to associate a clock for each machine. The clock will simply show the time instant at which the machine will break down, i. e., it will arrive at the repairman's queue. Obviously, at any instance, only the clocks of the operational ma-chines are of interest. In addition to these clocks, we require to have another clock which shows the time instant at which a machine cur-rently being repaired will become operational, i.e., it will cause a de-parture event to occur. Thus, in total, if we have 𝑚 machines, we need 𝑚 + 1 clocks. Each of these clocks is associated with the occur-rence of an event. In particular, 𝑚 clocks are associated with 𝑚 arri-val events and one clock is associated with the departure event. In addition to these clocks, it is useful to maintain a master clock, which simply keeps track of the simulated time.

Page 21: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

7

Figure 1.5: An arrival event

Figure 1.6: A departure event

The heart of the simulation model centers around the manipulation of these events. In particular, using the above clocks, the model decides which of all the possible events will occur next. Then the master clock is advanced to this time instant, and the model takes action as indicated in the flow-charts given in figures 1.5 and 1.6. This event manipulation approach is depicted in figure 1.7.

A machine breaks down

Repairman busy

?

yes

Repairman becomes busy

Repair starts

Join the queue

A machine is repaired

no

A new repair starts

Repairman becomes

idle

Other machines to be

repaired?

Page 22: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

8

Figure 1.7: Event manipulation

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.

We note that in order to schedule a new arrival time we simply have to set the associated clock to 𝑀𝐶 + 10. Similarly, each time a new repair service begins we set 𝐶𝐿4 = 𝑀𝐶 + 5. A very important aspect of this simulation model is that we only check the system at time in-stants at which an event takes place. We observe in the above hand simulation that the master clock in the above example gives the time instants at which something happened (i.e., an event occurred). These

Choose next event

a

A machineBreaks down

A machineis repaired

Create operation time

Generate anew service

Queueempty? ayes

no

a

Generate anew service

Queueempty?

no

yes

a

a

Page 23: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

9

times are: 0, 1, 4, 6, 9, 11, 16, . .. We note that in-between these in-stants no event occurs and, therefore, the system's status remains un-changed. In view of this, it suffices to check the system at time in-stants at which an event occurs. Furthermore, having taken care of an event, we simply advance the Master clock to the next event which has the smallest clock time. For instance, in the above hand simula-tion, after we have taken care of a departure at time 11, the simulation advances to time 16. This is because after the time instant 11, there are three events that are scheduled to take place in the future. These are: a) arrival of machine 1 at time 16; b) arrival of machine 2 at time 21; and c) a departure of machine 3 at time 16. Obviously, the next event to occur is the latter event at time 16.

𝑴𝑪 CL1 𝑪𝑳𝟐 𝑪𝑳𝟑 𝑪𝑳𝟒 𝒏 State of 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

Table 1.1: Hand simulation of the machine interference problem

1.4. A token-based access scheme The second example presented in this Chapter deals with a computer network which consists of a number of nodes connected via a shared wired or wireless transport medium, as shown in figure 1.9. The circle indicates a node and the line the shared medium. Access to the shared medium is controlled by a token. That is, a node cannot transmit on the network unless it has the token. In this example, we simulate a simplified version of such an access scheme, as described below. 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-

Page 24: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

10

Figure 1.8: Fowchart of the simulation program for the machine interference problem

Figure 1.9: Nodes interconnected by a shared medium

Initiatesimulation

A

Nextevent

MC=0,CL1=1,CL2=4CL3=9,n=0,R=0(idle)

Arrival(ithmachine)

Departure(thmachine)

MC=CLi MC=CL4

n=0?

R=1

yes

No n=n+1

CL4=MC+5

A

n=n-1

n=0? YesR=0

ACL4=MC+5

No

CLi=MC+10

A

Page 25: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

11

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.

Figure 1.10. The conceptual queueing system

Conceptually, this network can be seen as comprising of a number of queues, one per node. Only the queue that has the token can transmit packets. The token can be seen as a server which switches cyclically between the queues, as shown in figure 1.10. Once the token is switched to a queue, packets waiting in this queue can be transmitted on the network. The maximum time that a queue can keep the token is 𝑇 units of time, and the token is surrendered following the rules de-scribed above. The time it takes for the token to switch from one queue to the next is known as switch-over time. Such queueing sys-tems are referred to as polling systems, and they have been studied in the literature under a variety of assumptions.

. . .

token

Page 26: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

12

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:

• 𝑀𝐶: Master clock • 𝐴𝑇𝑖: Arrival time clock at queue 𝑖, 𝑖 = 1,2,3 • 𝐷𝑇𝑖: Departure time clock from queue 𝑖, 𝑖 = 1,2,3 • 𝑇𝑂𝑈𝑇: Time out clock for token • 𝐴𝑁𝐻: Arrival time clock of token to next queue

The logic for each of the events in this simulation is summarized in figures 1.11 to 1.14. In figure 1.11, we show what happens when an arrival occurs, figure 1.12 deals with the case where a service is com-pleted at queue 𝑖, figure 1.13 describes the action taken when a time-out occurs, and figure 1.14 summarizes the action taken when the to-ken arrives at a queue.

Page 27: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

13

Figure 1.11: Arrival event at queue 𝒊

Figure 1.12: Service completion at queue 𝒊

Arrivaloccurs

Join thequeue

Schedule Next Arrival

Return

MC=ATi

qi = qi + 1

ATi = MC + new inter-arrival time

Service is completed

Departs from queue

Return

Passtoken

MC = DT

q = q-1

H = (H+1) mod3

Schedule new service

Schedule arrival time

Queueempty?

Tokentime out

?Return

ANH = MC + switch over time

DT = MC + new service time

yes

no

no

i

i

yes

Page 28: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

14

Figure 1.13: Time-out of token

Figure 1.14: Arrival of token at next queue

Time-out occurs

Return

This means that node is still transmitting.

Raise aflag

Arrival of token at next queue

Return

Passtoken

MC = ANH

H = (H + 1) mod3

Schedule next service

completion

Schedule next arrival

Queueempty

?

ANH = MC + (switch over time)

DT4 = MC + (new service time)

yes

no

Schedule time-outTOUT = MC + T

yes

DTi =MC+(newservicetime)

Page 29: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

15

Figure 1.15: Event manipulation

The set of all pending events in a simulation is known as the event list. As will be seen later on, the event list is implemented as a data structure. Each time we want to locate the next event, we search the event list for the event with the smallest clock. An event that has just taken place is deleted from the event list, and newly generated events are inserted in the event list. This event manipulation forms the basis of the simulation, and it is summarized in figure 1.15. The hand sim-ulation is given in table 1.2.

1.5. A two-stage manufacturing system Let us consider a two-stage manufacturing system as depicted by the queueing network shown in figure 1.16. Stage 1 consists of an infinite capacity queue, referred to as queue 1, served by a single server, re-

Initializesimulation

End

i. e. search event list for smallest number

If new eventsare scheduledupdate next

event

Issimulation

over?

Takeappropriate

action

Locatenext event

no

yes

i. e. branch to the appropriate part of the program (or procedure)

i.e. search event listfor smallest clock value

i.e. jump to the appropriate part of the program

Update event list if new events are

schedule

Page 30: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

16

Queue 𝟏 Queue 𝟐 Queue 𝟑 Token 𝑴𝑪 𝑨𝑻𝟏 𝑫𝑷𝟏 Q 𝑨𝑻𝟐 𝑫𝑷𝟐 Q 𝑨𝑻𝟑 𝑫𝑷𝟑 Q Node

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

Table 1.2: Hand simulation for the token-based access scheme

Page 31: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

17

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.

Figure 1.16: A two-stage queueing network.

When queue 2 becomes full, server 1 stops working. More specifical-ly, upon service completion at server 1, the server gets blocked if queue 2 is full. That is, the server cannot serve any other customers that may be waiting in queue 1. Server 1 will remain blocked until a customer departs from queue 2. In this case, a space will become available in queue 2 and the served customer in front of server 1 will be able to move into queue 2, thus freeing the server to serve other customer in queue 1.

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.

The following events may occur.

1. Arrival of a customer to queue 1 (clock 𝐴𝑇) 2. Service completion at server 1 (clock 𝐷𝑇1) 3. Service completion at server 2 (clock 𝐷𝑇2) 4. Server 1 breaks down (clock 𝐵𝑅1) 5. Server 1 becomes operational (clock 𝑂𝑃1) 6. Server 2 breaks down (clock 𝐵𝑅2) 7. Server 2 becomes operational (clock 𝑂𝑃2)

Stage 1 Stage 2

Queue 1 Server 1 Queue 2 Server 2

Page 32: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

18

Below, we identify the events that may be triggered when each of the above events occurs.

1. Arrival to queue 1.

a. Arrival to queue 1 (new value for 𝐴𝑇 clock): This event is al-ways scheduled each time an arrival occurs.

b. Service completion at server 1 (new value for 𝐷𝑇1 clock): This event will be triggered if the new arrival to queue 1 finds the server idle and starts its service.

2. Service completion at server 1:

a. Service completion at server 1(new value for 𝐷𝑇1 clock): This event will occur if there is one or more customers waiting in queue 1.

b. Service completion at server 2 (new value for 𝐷𝑇2 clock): This event will occur if the customer who just completed its service at server 1 finds server 2 idle and starts its service.

The occurrence of a service completion event at server 1 may cause server 1 to get blocked if queue 2 is full.

3. Service completion at server 2:

a. Service completion at server 2 (new value for 𝐷𝑇1 clock): This event will occur if there is one or more customers in queue 2.

b. Service completion at server 1 (new value for 𝐷𝑇1 clock): This event will occur if server 1 was blocked.

4. Server 1 breaks down:

Server 1 becomes operational (new value for 𝑂𝑃1 clock): This event gives the time in the future when the server will be repaired and will become operational. If the server was busy when it broke down, update the clock of the service completion event at server 1 to reflect the delay due to the repair.

Page 33: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

19

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.

6. Server 2 breaks down:

Server 2 becomes operational (new value for 𝑂𝑃2 clock): This event gives the time in the future when server 2 will be repaired, and therefore it will become operational. During that time the server is broken down.

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.

7. Server 2 becomes operational:

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.

In the hand-simulation given in table 1.3, it is assumed that the buffer capacity of the second queue is 4 (this includes the customer in ser-vice). All service times, inter-arrival times, operational and repair times are constant with the following values: inter-arrival time = 40, service time at node 1 = 20, service time at node 2 = 30, operational time for server 1 = 200, operational time for server 2 = 300, re-pair time for server 1 = 50, and repair time for server 2 = 150. Initially the system is assumed to be empty. The first arrival occurs at

Page 34: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

20

time 10, server 1 will break down for the first time at time 80, and server 2 at time 90. Please note that the symbols 𝑛R and 𝑠R, 𝑖 = 1,2, used in the header of table 1.3 indicate the number of customers in queue 𝑖, and the status of server 𝑖 respectively.

Since we are dealing with integer numbers, it is possible that more than one clock may have the same value. That is, more than one event may occur at the same time. In the simulation of the two-stage manu-facturing system, simultaneous events can be taken care in any arbi-trary order. In general, depending on the nature of the problem, the order with which simultaneously occurring events are dealt with may matter, and it should be reflected in the simulation model. If the clocks can be defined as real numbers, then events will never occur at the same time.

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

Page 35: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

21

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

Table 3: Hand simulation for the two-stage manufacturing system

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.

2. Do the hand simulation of the token-based access scheme, de-

scribed in section 1.4, for the following cases: a. Packets have priority 1 or 2 (1 being the highest). The packets

in a queue are served according to their priority. Packets with the same priority are served in a FIFO manner.

b. There are five queues.

Page 36: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

22

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! 1. The machine interference problem. Write a computer program to

simulate the machine interference problem described in section 1.3. Each time an event occurs, print out a line of output to show the current values of the clocks and of the other status parameters as in the hand simulation. Run your simulation until the master clock is equal to 20. Check by hand to make sure that the simula-tion advances from event to event correctly, and whether it up-dates the clocks and the other status parameters correctly.

2. The token-based access scheme. Write a computer program to simulate the token-based access scheme as described in section 1.4. Each time an event occurs, print out a line of output to show the current values of the clocks and of the other status parameters as in the hand simulation. Run your simulation until the master clock is equal to 100. Check by hand to make sure that the simula-tion advances from event to event correctly, and whether it up-dates the clocks and the other status parameters correctly.

3. The two-stage manufacturing system. Write a computer program to simulate the two-stage manufacturing system as described in section 1.5. Each time an event occurs, print out a line of output to show the current values of the clocks and of the other status pa-rameters as in the hand simulation. Run your simulation until the master clock is equal to 500. Check by hand to make sure that the

Page 37: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

23

simulation advances from event to event correctly, and whether it updates the clocks and the other status parameters correctly.

Page 38: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished
Page 39: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

25

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.

In simulation, random numbers are used in order to introduce ran-domness in the model. For instance, let us consider the machine inter-ference simulation model discussed in the previous Chapter, section 1.3. In this model it was assumed that the operational time and the repair time of a machine are constant. It is possible that one may find a real-life system where this is true. However, in general, one will observe that the time a machine is operational varies. Also, its repair time may vary. In order to make the simulation model realistic, we can collect a representative sample of operational times by observing the machine over a long period of time. We can then fit a theoretical distribution to this sample, or simply summarize the sample data into a histogram. Then, each time we want to determine the next opera-tional time of the machine, we generate randomly a number from the theoretical distribution or the histogram. Similarly, we can do the same for the repair times.

In order to generate random numbers, one needs to be able to gener-ate first uniformly distributed random numbers in [0,1] known as pseudo-random numbers. These pseudo-random numbers are then used to generate random numbers from a theoretical or an empirical distribution. These random numbers are referred to as stochastic vari-ates or random variates. In this Chapter, we focus our discussion on pseudo-random numbers. In sections 2.2 to 2.5, we discuss how to generate pseudo-random numbers, and in section 2.6, we present a number of statistical tests that can be used to test whether a sequence

Page 40: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

26

of pseudo-random numbers is random. The generation of stochastic variates is described in Chapter 3.

2.2. Pseudo-random numbers There is no such a thing as a single random number. Rather, we speak of a sequence of random numbers that follow a specified theoretical or empirical distribution. There are two main approaches to generat-ing random numbers. In the first approach, a physical phenomenon is used as a source of randomness from where random numbers can be generated. Random numbers generated in this way are called true random numbers.

A true random number generator requires a completely unpredictable and non-reproducible source of randomness. Such sources can be found in nature, or they can be created from hardware and software. For instance, the elapsed time between emissions of particles during radioactive decay is a well-known randomized source. Also, the thermal noise from a semiconductor diode or resistor can be used as a randomized source. Finally, sampling human-computer interactions, such as keyboard or mouse activity of a user, can give rise to a ran-domized source.

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.

An advantage of generating pseudo random numbers in a determinis-tic fashion is that they are reproducible, since the same sequence of

Page 41: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

27

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.

Pseudo-random numbers and in general random numbers are typically generated on demand. That is, each time a random number is re-quired, the appropriate generator is called which then returns a ran-dom number. Consequently, there is no need to generate a large set of random numbers in advance and store them in an array for future use.

We note that the term pseudo-random number is reserved for random numbers that are uniformly distributed in the space [0,1]. All other random numbers, including those that are uniformly distributed with-in any space other than [0,1], are referred to as stochastic variates or random variates. For simplicity, we refer to pseudo-random numbers as random numbers.

In general, an acceptable method for generating random numbers must yield sequences of numbers or bits that are:

a. Uniformly distributed b. Statistically independent c. Reproducible d. Non-repeating for any desired length.

Historically, the first method for creating random numbers by com-puter was Von Neuman's mid-square method. His idea was to take the square of the previous random number and to extract the middle dig-its. For example, let us assume that we are generating 10-digit num-bers and that the previous value was 5772156649. The square of this value is 33317792380594909291 and the next number is 7923805949.

The mid-square method was relatively slow and statistically unsatis-factory, and it was abandoned in favor of other algorithms. In the fol-lowing sections, we describe the congruential method, the Taus-worthe generators, and the lagged Fibonacci generators.

Page 42: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

28

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 congruential method uses the following recursive relationship to generate random numbers:

𝑥R[\ = 𝑎𝑥R + 𝑐(𝑚𝑜𝑑𝑚),

where𝑥R, 𝑎, 𝑐 and 𝑚 are all non-negative numbers. Given that the previous random number was 𝑥R, the next random number 𝑥R[\ can be generated as follows. Multiply 𝑥Rby 𝑎 and then add 𝑐. Then, compute the modulus 𝑚 of the result. That is, divide the result by 𝑚 and set 𝑥R[\ equal to the remainder of this division. For example, for 𝑥c =0, 𝑎 = 𝑐 = 7, and 𝑚 = 10 we obtain the following sequence of numbers: 7, 6, 9, 0, 7, 6, 9, 0, . ..

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.

The numbers generated by a congruential method are between 0 and 𝑚 − 1. Uniformly distributed random numbers between 0 and 1 can be obtained by simply dividing the resulting 𝑥R by 𝑚.

The number of successively generated pseudo-random numbers after which the sequence starts repeating itself is called the period. If the period is equal to 𝑚, then the generator is said to have a full period. Theorems from number theory show that the period depends on the value of 𝑚. The larger its value, the larger is the period. In particular, the following conditions on 𝑎, 𝑐, and 𝑚 guarantee a full period:

Page 43: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

29

a. 𝑚 and 𝑐 have no common divisor. b. 𝑎 = 1(𝑚𝑜𝑑𝑟) if 𝑟 is a prime factor of 𝑚. That is, if 𝑟 is a

prime number (divisible only by itself and 1) that divides 𝑚, then it divides 𝑎 − 1.

c. 𝑎 = 1(𝑚𝑜𝑑4) if 𝑚 is a multiple of 4. It is important to note that one should not use any arbitrary values for 𝑎, 𝑐, and 𝑚. Systematic testing of various values for these parameters have led to generators which have a full period and which are statisti-cally satisfactory. A set of such values is: 𝑎 = 314, 159, 269, 𝑐 =453, 806, 245, and 𝑚 =2ef (for a 32 bit machine).

Typically, we do not write our own random number generator, but use the one available in the programming language used to write the simulation code.

In order to get a generator started, we need an initial seed value for 𝑥c. It will become obvious later on that the value of the seed does not affect the sequence of the generated random numbers after a small set of random numbers has been generated.

The implementation of a pseudo-random number generator involves a multiplication, an addition and a division. The division can be avoid-ed by setting 𝑚 equal to the size of the computer word. For, if the to-tal numerical value of the expression 𝑎𝑥R + 𝑐 is less than the word size, then it is in itself the result of the operation 𝑎𝑥R + 𝑐(𝑚𝑜𝑑𝑚), where 𝑚 is set equal to the word size. Now, let us assume that the ex-pression 𝑎𝑥R + 𝑐gives a number greater than the word size. In this case, an overflow will occur. If the overflow does not cause the exe-cution of the program to be aborted, but it simply causes the signifi-cant digits to be lost, then the remaining digits left in the register is the remainder of the division (𝑎𝑥R + 𝑐)/𝑚. This is because the lost significant digits are the multiples of the value of 𝑚, which is the quotient of the above division.

In order to demonstrate the above idea, let us consider a fictitious decimal calculator whose register can accommodate a maximum of 2

Page 44: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

30

digits. Obviously, the largest number that can be held in the register is 99, and we set 𝑚 = 100. For 𝑎 = 8, 𝑥c = 2, and 𝑐 = 10, we have that 𝑎𝑥c + 𝑐 = 26, and 26(𝑚𝑜𝑑100) = 26. However, if 𝑥c = 20, then the product 𝑎𝑥c (which is equal to 8𝑥20) will cause an overflow to occur. The first significant digit will be lost and thus the register will contain the number 60. If we now add 𝑐 (which is equal to 10) to the above result we will obtain 70, which is, the remainder of the di-vision 170/100.

2.3.1. General congruential methods The mixed congruential method described above is a special case of the following generator:

𝑥R[\ = 𝑓(𝑥R, 𝑥Ri\, . . . )(𝑚𝑜𝑑𝑚),

where 𝑓(. ) is a function of previously generated pseudo-random numbers. A special case of the general congruential method is the quadratic congruential generator, which has the form:

𝑥R[\ = 𝑎\𝑥Rf + 𝑎f𝑥Ri\(𝑚𝑜𝑑𝑚).

The special case of 𝑎\ = 𝑎f = 1, 𝑐 = 0, and 𝑚 being a power of 2 has been found to be related to the midsquare method. Another special case that has been considered is the additive congruential method, which is based on the expression:

𝑓(𝑥R, 𝑥Ri\, . . . ) = 𝑎\𝑥R + 𝑎f𝑥Ri\ + ⋯+ 𝑎k𝑥Rik.

The case of 𝑓(𝑥R, 𝑥Ri\, ) = 𝑥R + 𝑥Ri\ has received attention.

2.3.2. Composite generators Composite generators can be used by combining two generators, since this achieves a better statistical behavior than either individual generator. For instance, we can use a congruential generator to fill a vector of size 𝑛 with random numbers. A second generator is then used to generate a random integer 𝑟 uniformly distributed in [1, 𝑛]. The random number stored in the 𝑟th position of the vector is the ran-dom number returned from the composite generator. The first genera-

Page 45: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

31

tor replaces the random number in the 𝑟th position with a new ran-dom number, and the procedure repeats. It has been demonstrated that such a combined generator has good statistical properties, even if the two individual generators used are not as good.

2.4. Tausworthe generators Tausworthe generators are additive congruential generators obtained when the modulus 𝑚 is equal to 2. That is,

𝑥R = 𝑎\𝑥Ri\ + 𝑎f𝑥Rif + ⋯+ 𝑎l𝑥li\(𝑚𝑜𝑑2),

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

The generated bits can be put together sequentially to form an 𝑙 −bit binary integer between 0 and 2𝑙 − 1. Several bit selection techniques have been suggested in the literature.

In the composite generator scheme discussed in the previous section, one of the generators (but not both) could be a Tausworthe generator.

Tausworthe generators are independent of the computer used and its word size and have very long cycles. However, they are slow since they only produce bits. A fast variation of these generators is the tri-nomial-based Tausworthe generator. Two or more such generators have to be combined in order to obtain a statistically good output.

2.5. The lagged Fibonacci generators The lagged Fibonacci generators (LFG) are an important improve-

Page 46: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

32

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 𝑥c = 0 and 𝑥\ = 1. The beginning of the Fibonacci sequence is: 0, 1, 1, 2, 3, 5, 8, 13, 21. Based on this recurrence relation, the LFG can be expressed as follows:

𝑥R = 𝑥RiuO𝑥Rik(𝑚𝑜𝑑𝑚)

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:

𝑥R = 𝑥Riu + 𝑥Rik(𝑚𝑜𝑑𝑚)

where0 < 𝑗 < 𝑘. As can be seen, it is very easy to implement and also it is quite fast. A very long period, equal to 𝑚k − 1, can be ob-tained if 𝑚 is a prime number. However, using a prime number may not be very fast. Thus, typically 𝑚 is set to2efor 2z{. In this case, the maximum period of the additive LFG is (2k − 1)2|i\.

The multiplicative LFG is: 𝑥R = 𝑥Riu × 𝑥Rik(𝑚𝑜𝑑𝑚), where 𝑚 is set to2efor 2z{ and0 < 𝑗 < 𝑘. The maximum period is (2k − 1)2|ie.

In general, LFGs generate a sequence of random numbers with very good statistical properties, and they are nearly as efficient as the line-ar congruential generators. Their execution can also be parallelized.

Page 47: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

33

However, LFGs are highly sensitive to the seed. That is, the statistical properties of an output sequence of random numbers varies from seed to seed. Determining a good seed for LFGs is a difficult task.

2.6. Statistical tests for pseudo-random number generators It is a good practice to check statistically the output of a pseudo-random number generator prior to using it. In this section, we de-scribe the following four statistical tests:

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.

Statistical testing involves testing whether a particular assumption, known as a hypothesis, is correct. Before we proceed to describe the statistical tests, we review statistical hypothesis testing.

2.6.1. Hypothesis testing Statistical hypothesis is used in Statistics to test whether a hypothesis, of which the truth is not known, is correct or not. The hypothesis, no-tated as 𝐻c, is known as the null hypothesis, and the opposite to the hypothesis, notated as 𝐻~, is known as the alternative hypothesis.

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

Page 48: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

34

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

On the other hand, if we want to test whether the average population weight 𝜇 has decreased, then we formulate a one-tailed test (or one-sided test) to the left, as follows:

𝐻c:𝜇 = 170 𝐻~:𝜇 < 170

We test the two-tailed test hypothesis by drawing a random sample of weights from the 2020 population of all men in the USA of age 20 to 29, and calculate the sample mean �̅�. We note that if we sample 𝑛 observations from a population which has an arbitrary distribution with a mean 𝜇 and variance 𝜎f, then the sample mean �̅� is normally distributed with mean 𝜇 and variance 𝜎f/𝑛, for 𝑛 ≥ 30. Accordingly, if the null hypothesis is true, that is, the population mean has not changed, then the sample mean �̅� is normally distributed with mean 170 and standard deviation 40/√𝑛, where n is the sample size. As-suming that𝑛 = 64, we have that 40/√64 = 5.

Now, we compare �̅� to the population mean𝜇 = 170. If �̅� = 170, then we can argue that the null hypothesis is true, but what can we say if �̅� = 165 or �̅� = 180? In general, if �̅�is very close to 𝜇, then we accept the null hypothesis that the population mean has not changed. On the other hand, if it is far away from 𝜇, then we reject the null hypothesis, and accept the alternative hypothesis that the population mean has changed (increased or decreased).

In view of this, we need a cut-off point which will act as a threshold. In fact, since we are doing a two-tailed test, we need two symmetrical points 𝑎 and 𝑏. We choose the two points so that 95%of the popula-tion (or any other percentage, such as 90% and 99%) lies between

Page 49: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

35

them, as shown in figure 2.1. This means that 2.5% of the population lies within each tail (−∞, 𝑎) or (𝑏, +∞). If the population mean has not changed, then 95% of the sample means �̅� are between 𝑎 and 𝑏. Consequently, if �̅� lies in-between 𝑎 and 𝑏, we have a95% probabil-ity that the null hypothesis is correct. Otherwise, we reject it and ac-cept the alternative hypothesis. This probability is known as the con-fidence level.

Figure 2.1: Accept 𝑯𝟎 if 𝒙� is between 𝒂 and 𝒃

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”.

In order to carry out the test, we transform points 𝑎, 𝑏 and �̅� to the standard normal distribution using the 𝑧-transform. This is the normal distribution with 𝜇 = 0 and 𝜎f = 1. A point𝑥 in a normal distribu-tion with mean 𝜇 and variance 𝜎f is transformed to an equivalent point in the standard normal distribution using the expression 𝑧 =(𝑥 − 𝜇)/𝜎. By equivalent point we mean that the distance of 𝑧 from its mean 0expressed in standard deviations of 1, is the same as the distance of 𝑥 from its mean 𝜇 expressed in standard deviation of 𝜎. Using the table for the standard normal distribution (available in the Internet and in any introductory Statistics book), we can calculate that at 95% confidence, 𝑎 corresponds to −1.96 and 𝑏 to 1.96. Now, we transform �̅�to 𝑧, i.e., 𝑧 = (�̅� − 170)/5 and check it against the val-

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µ!"

fX(t)

a b

95% 2.5%2.5%

Page 50: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

36

ues of 𝑎 and 𝑏. For the two-tailed test, we accept the null hypothesis if|𝑧| ≤ 1.96, see figure 2.2. Otherwise, we reject it. For instance, let �̅� = 173. Then, 𝑧 = 3/5 = 0.6 which is less than 1.96, and therefore we accept the null hypothesis that the population mean of weights has not changed. Likewise, if �̅� = 165, since𝑧 = −5/5 = −1 >−1.96. However, if �̅� = 180, then we reject the null hypothesis since 𝑧 = 10/5 = 2 > 1.96.

Figure 2.2: Two-tailed test at 𝟗𝟓% confidence

Figure 2.3: One-tailed test on the right at 𝟗𝟓% confidence

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

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µz

fX(t)

-1.96

95% 2.5%2.5%

1.960

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µz

fX(t)

95% 5%

1.6450

Page 51: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

37

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.

Figure 2.4: One-tailed test on the left at 𝟗𝟓% confidence

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

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µz

-1.645

95%5%

0

fX(t)

Page 52: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

38

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.

Real situation Decision

Ho is accepted H0 is rejected

Ho is true Valid Type I error

Ho is not true Type II error Valid

Table 2.1: Type I and type II errors

We note that we use the standard normal distribution if the population variance 𝜎f in known and/or the sample size 𝑛 ≥ 30. Otherwise, we use the t-student distribution. This is a symmetric and bell-shaped dis-tribution, 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 normal distribution, since the stand-ard deviation 𝜎 is typically not known. The 𝑡-student distribution is also used if the sample size is small, less than 30.

Figure 2.5: The p-value

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µz1.6450

5%

p-value

fX(t)

Page 53: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

39

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.

An easier way to do this is to calculate the 𝑝-value of �̅�. As shown in figure 2.5, the 𝑝-value is the area under the standard normal curve (or the 𝑡-student distribution curve) from 𝑧 to +∞, where 𝑧 is the value that corresponds to �̅�.

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.

Statistical packages automatically report the 𝑝-value. For a one-sided test to the left or to the right, the 𝑝-value is reported as the area under the 𝑡-student distribution from |𝑡| to +∞, where 𝑡 is the calculated 𝑡-student statistic. For a two-tailed test, the reported 𝑝-value is two times the area of the tail |𝑡| to +∞.

The 𝑝-value allows us to decide quickly whether to accept or reject a null hypothesis. Typically, we test a hypothesis at the confidence lev-els of 90%, 95%, and 99%. For a one tailed test, the area of the tail corresponding to these three confidence levels is 0.10, 0.05, and 0.01 respectively. For a two-tailed test, the area of both tails is also 0.10, 0.05, and 0.01 respectively. So, irrespectively of whether we do a one-tailed or a two-tailed test, we simply compare the 𝑝-value to these three thresholds, 0.10, 0.05, and 0.01, and accordingly we ac-cept or 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

Page 54: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

40

we reject the null hypothesis. However, if it falls in-between these values, say it is0.07, then the conclusion is questionable. For, we re-ject the null hypothesis at 90% confidence but accept it at 95%and 99% confidence.

We now proceed to describe the statistical tests for pseudo-random number generators. In these tests, the null hypothesis is that a se-quence of numbers produced by a pseudo-random generator is ran-dom, and the alternative hypothesis is that it is not.

We note, that in many tests, the tested sequence has to be binary. In this case, if the number generator generates numbers in [0,1], you can represent each number by a binary string and then concatenate them into a sequence of bits.

2.6.2. Frequency test (monobit test) The frequency test is one of the most basic tests for pseudo-random number generators. If a pseudo-random number generators fails this test, then it is highly likely that it will also fail other more sophisticat-ed tests.

The tested sequence is a bit string. In a true random sequence, the number of 1’s and 0’s should be about the same. This test checks whether this is correct or not, and it is carried out as follows:

1. Generate 𝑚 pseudo-random numbers and concatenate them into a string of bits. Let the length of this string be 𝑛. The 0’s are converted into −1’s and then the obtained set of −1 and 1 values is added up. Let 𝑆l be the sum of the −1 and 1 values.

2. Compute the test statistic: 𝑆��� = |𝑆l|/√𝑛. 3. Compute the 𝑝-value: 𝑝-value= 𝑒𝑟𝑓𝑐(𝑆���/√2), where

𝑒𝑟𝑓𝑐(. ) is the complimentary error function available in pro-gramming languages and statistical packages.

At 99% confidence, we accept the null hypothesis that the sequence is random if p-value> 0.01.

Page 55: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

41

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 observation, that if the sequence is random, then 𝑆l will be close to zero. A large positive value of 𝑆l is indicative of too many 1’s, and a large negative value of 𝑆lis indicative of too many 0’s.

It is recommended that the sequence to be tested should be greater than a 100.

2.6.3 Serial test

The tested sequence is a bit string. For a 𝑘-bit string, there are 2k dif-ferent ways of combining. Each of these combinations has the same chance of occurring if the sequence of the k bits is random. The serial test determines whether the number of times each of these combina-tions occurs is uniformly distributed. If 𝑘 = 1, then this test becomes equivalent to the frequency test described above.

Let 𝑒 denote a sequence of 𝑛 bits created by a pseudo-random number generator. The minimum recommended value for 𝑛 is 100. The serial statistical test checks the randomness of overlapping blocks of 𝑘, 𝑘 −1, 𝑘 − 2 bits found in 𝑒, where 𝑘 < |𝑙𝑜𝑔f(𝑛)| − 2. The test is carried out as follows:

1. Augment 𝑒 by appending its first 𝑘 − 1 bits from the begin-ning of 𝑒 to the end of 𝑒,and let 𝑒\� be the resulting bit string. Likewise, augment 𝑒 by appending its first 𝑘 − 2 bits to the end, and let 𝑒f� be the resulting bit string. Finally, augment 𝑒 by appending its first 𝑘 − 2 bits to the end, and let 𝑒e� be the resulting bit string.

2. Compute the frequency of occurrence of each 𝑘, 𝑘 − 1and 𝑘 − 2 overlapping bit combination using the bit string 𝑒\� , 𝑒f� , 𝑒e� respectively. Let 𝑐\ be the total number of combina-tions of 𝑘 bits, and let 𝑓\,\,𝑓\,f, … , 𝑓\,be the frequency of oc-currence for each of these combinations in 𝑒\� . Likewise, let 𝑐f

Page 56: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

42

be the total number of combinations of 𝑘 − 1 bits, and let 𝑓f,\,𝑓f,f, … , 𝑓f,��,be the frequency of occurrence for each of these combinations in 𝑒f� . Finally, let 𝑐e be the total number of combinations of 𝑘 − 2 bits, and let 𝑓e,\,𝑓e,f, … , 𝑓e,��,be the frequency of occurrence for each of these combinations in 𝑒e� .

3. Compute the following statistics and difference statistics:

𝑆kf =2k

𝑛 ��𝑓\,R f

�¡

R¢\

− 𝑛

𝑆ki\f =2ki\

𝑛 ��𝑓f,R f − 𝑛

��

R¢\

𝑆kiff =2kif

𝑛 ��𝑓e,R f

�£

R¢\

− 𝑛

𝛥𝑆kf = 𝑆kf − 𝑆ki\f

𝛥f𝑆kf = 𝑆kf − 2𝑆ki\f + 𝑆k[\f

4. Compute the 𝑝-values:

𝑝 − value\ = IncompleteGamma(2kif, 𝛥𝑆kf/2) 𝑝 − valuef = IncompleteGamma(2kie, 𝛥f𝑆kf/2)

where the function IncompletGamma(. ) can be found in programming languages and statistical packages.

5. At 99% confidence level, we reject the null hypothesis that the sequence is random if 𝑝 − value\ or 𝑝 − valuef < 0.01. Else, we accept the null hypothesis that the sequence is ran-dom.

The test is based on the idea that if the overlapping bit combinations are not uniformly distributed, then 𝛥𝑆kf and 𝛥f𝑆kfbecome large which causes the 𝑝 − values to become small.

As an example we consider the sequence 𝑒 = 0011011101, with 𝑛 = 10, and 𝑘 = 3.

Page 57: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

43

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:

𝑆ef =2e

10(0 + 1 + 1 + 4 + 1 + 4 + 4 + 1) − 10 = 2.8

𝑆ff =2f

10(0 + 9 + 9 + 9) − 10 = 1.2

𝑆\f =210(16 + 36) − 10 = 0.4

𝛥𝑆ef = 𝑆ef − 𝑆ff = 2.8 − 1.2 = 1.6

𝛥f𝑆ef = 𝑆ef − 2𝑆ff + 𝑆\f = 0.8

4. Decision at 99% confidence level:

𝑝 − value\ = IncompleteGamma(2,0.8) = 0.9057 𝑝 − valuef = IncompleteGamma(1,0.4) = 0.8805

Since 𝑝 − value\ and 𝑝 − valuefare greater than 0.01, the sequence passes the serial test.

Page 58: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

44

2.6.4. Runs test

Given a bit string, a run is an uninterrupted sequence of 1’s or 0’s. For instance, for the bit string 001011110001111000 we have the following runs: 00, 1, 0, 1111, 000, 1111,000. Now, let 𝑅 be the total number of runs for a sample generated by a pseudo-random genera-tor. The test is carried out by computing the following test statistic:

𝑧 =𝑅 − 𝑅µ𝑠¶

,

where 𝑅µ and 𝑠¶ is the expected number of runs and the standard de-viation respectively, given by the expressions:

𝑅µ =2𝑛\𝑛f𝑛\+𝑛f

+ 1

𝑠¶ =2𝑛\𝑛f(2𝑛\𝑛f − 𝑛\ − 𝑛f)(𝑛\+𝑛f)f(𝑛\+𝑛f − 1)

where 𝑛\and 𝑛f are the number of 0�𝑠 and 1’s.

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.

2.6.5. Chi-squared test for goodness of fit This test checks whether a sequence of pseudo-random numbers in [0,1] are uniformity distributed. The chi-squared (𝜒f) test can be used to check whether an empirical distribution follows a specific theoreti-cal distribution. In our case, we are concerned about testing whether the numbers produced by a generator are uniformly distributed.

Let us consider a sequence of pseudo-random numbers between 0 and 1. We divide the interval [0,1]into 𝑘 subintervals of equal length, where 𝑘 > 100. Let 𝑓R be the number of pseudo-random numbers that fall within the 𝑖th subinterval (make sure that enough random numbers are generated so that 𝑓R > 5). The 𝑓R values are called the observed values. Now, if these generated random numbers are truly uniformly distributed, then the mean number of random numbers that

Page 59: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

45

fall within each subinterval is 𝑛/𝑘, where 𝑛 is the sample size. This value is called the theoretical value. The chi-squared test measures whether the differences between the observed and the theoretical val-ues is due to random fluctuations or due to the fact that the empirical distribution does not follow the specific theoretical distribution. For the case where the theoretical distribution is the uniform distribution, the chi-squared statistic is given by the expression:

𝜒f =𝑘𝑛�¸𝑓R −

𝑛𝑘¹

fk

R¢\

and it has 𝑘 − 1 degrees of freedom. The null hypothesis is that the generated random numbers are uniformly distributed in [0,1]. This hypothesis is rejected if the computed value of 𝜒f is greater than the one obtained from the chi-squared tables for 𝑘 − 1 degrees of free-dom and 𝑎 level of significance. The chi-squared tables can be found in any introductory statistics book, and of course, the function to gen-erate 𝜒fvalues can be found in software packages.

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.

2. This is a more involved exercise! Implement the random number generators described in this Chapter, and then test them using the statistical tests described in section 2.6. Measure the execution time for each generator for the same output size. Present your re-sults in a table that gives the execution speed of each generator and whether it has failed or passed each statistical test. Discuss your re-sults. Which generator would you chose?

Page 60: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

46

Page 61: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

47

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], andthey 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.

3.2. The inverse transformation method This method is applicable only to cases where the cumulative density function can be inversed. Assume that we wish to generate stochastic variates from a probability density function (pdf) 𝑓(𝑥). Let 𝐹(𝑥) be its cumulative density function. We note that 𝐹(𝑥) is defined in the region [0,1]. We explore this property of the cumulative density func-tion to obtain the following simple stochastic variates generator.

We generate a random number 𝑟 and set it equal to 𝐹(𝑥). That is, 𝐹(𝑥) = 𝑟. 𝑥 is then obtained by inverting 𝐹(𝑥). That is, 𝑥 =𝐹i\(𝑟), where 𝐹i\(𝑟) indicates the inverse transformation of 𝐹(𝑥). Below, we demonstrate this method through four simple examples.

Example 1: Generate stochastic variates with probability density function 𝑓(𝑥) = 2𝑥, 0 ≤ 𝑥 ≤ 1.

Page 62: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

48

A graphical representation of this probability density function is giv-en in figure 3.1a. We first calculate the cumulative density function 𝐹(𝑥). We have

𝐹(𝑥) = ¾ 2𝑡𝑑𝑡¿

c= 𝑥f, 0 ≤ 𝑥 ≤ 1.

Let 𝑟 be a random number. Then, 𝑟 = 𝑥f or 𝑥 = √𝑟. That is, a sto-chastic variate from the distribution 𝑓(𝑥) = 2𝑥 can be generated by taking the square root of a random number. This inversion is shown graphically in figure 3.1b.

a) 𝒇(𝒙) b) Inversion of 𝑭(𝒙)

Figure 3.1: Sampling from 𝒇(𝒙) = 𝟐𝒙

Example 2: Generate random variates with probability density func-tion:

𝑓(𝑥) = Â3𝑥f, 0 ≤ 𝑥 ≤ 1

0,otherwise.

Let 𝐹(𝑡) be the cdf of 𝑓(𝑡). Then

𝐹(𝑥) = ¾ 3𝑥f𝑑𝑥 = 𝑥e|Ç

c

𝑡0 = 𝑡e − 0 = 𝑡e, 0 ≤ 𝑡 ≤ 1.

Let 𝑟 be a pseudo-random number. By the method of inverse transformation, we have 𝑟 = 𝑡e. Therefore, 𝑡 = 𝑟\/e.

! " r# "

"0

2

"0

1

1 1

Page 63: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

49

Example 3: Generate random variates from

𝑓(𝑥) = Â 5𝑥,0 ≤ 𝑥 ≤ 4𝑥 − 2, 4 < 𝑥 ≤ 10.

We first normalize 𝑓(𝑡). We have:

¾ 5𝑥𝑑𝑥 + ¾ (𝑥 − 2)𝑑𝑥\c

{

{

c

= 512 𝑥

f È40 + É𝑥f

2 − 2𝑥Ê È104 = 70.

Therefore

𝐹(𝑥) =

⎩⎪⎨

⎪⎧ 1

7052𝑥f 0 ≤ 𝑥 ≤ 4

170É40 +

𝑥f

2 − 2𝑥Ê 4 < 𝑥 ≤ 10.

Procedure:

1. Draw a random number 𝑟. 2. If 𝑟 ≤0.4 then𝑟 = (5/140)𝑥f or 𝑥 = Ï(140/5)𝑟.

3. Otherwise, set r = \³c¸40 + ¿�

f− 2𝑥¹ and solve for x.

Example 4: Generate random variates from

𝑓(𝑥) = Â 𝑥,0 ≤ 𝑥 ≤ 1/21 − 𝑥, 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.

1. Draw a random number r. 2. If 𝑟 ≤ 0.5 then set 𝑟 = 𝑥/0.25 from where we obtain 𝑥 =

0.25𝑟.

Page 64: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

50

3. Otherwise, set 𝑟 = (1 − 𝑥)/0.25 from where we obtain 𝑥 =1 − 0.25𝑟.

3.3. Sampling from continuous-time probability distributions In this section, we use the inverse transformation method to generate stochastic variates from a uniform distribution, an exponential distri-bution, and an Erlang distribution. We also present a technique for generating variates from the normal distribution.

3.3.1. Sampling from a uniform distribution The probability density function of the uniform distribution is defined as follows:

𝑓(𝑥) = Ð1

𝑏 − 𝑎 𝑎 < 𝑥 < 𝑏

0 otherwise,

and it is shown graphically in figure 3.2.

Figure 3.2: The uniform distribution

The cumulative density function is:

𝐹(𝑥) = ¾1

𝑏 − 𝑎 𝑑𝑡 =𝑥 − 𝑎𝑏 − 𝑎

¿

~

The inverse transformation method for generating stochastic variates is as follows. Generate a random number 𝑟. Then

𝑟 =𝑥 − 𝑎𝑏 − 𝑎

1" − $

$ "

Page 65: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

51

or, 𝑥 = 𝑎 +(𝑏 − 𝑎)𝑟. That is, 𝑟is multiplied by (𝑏 − 𝑎) and then shifted by 𝑎.

3.3.2. Sampling from an exponential distribution The probability density function of the exponential distribution is given by the expression:

𝑓(𝑥) = 𝜆𝑒iÒ¿, 𝜆 > 0, 𝑥 ≥ 0,

The cumulative density function is:

𝐹(𝑥) = ¾ 𝜆𝑒iÒ¿¿

c= 1 − 𝑒iÒ¿.

a) Probability density function b) Cumulative function

Figure 3.3: The exponential distribution and its cumulative

The probability density function of the exponential distribution and its cumulative function are shown in figure 3.3. The inverse transfor-mation method for generating stochastic variates is as follows. Gener-ate a random number 𝑟.Then,

𝑟 = 1 − 𝑒iÒ¿

or

1 − 𝑟 = 𝑒iÒ¿

or

X

FX(t)

1

! "

X

fX(t)# "

Page 66: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

52

𝑥 = −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 4respectively. Then, the sum 𝑥 = 𝑥\ + 𝑥f + 𝑥e + 𝑥{ follows the Erlang distribution.

Figure 3.4: The Erlang distribution

When, we model an activity, such as a service time, as an Erlang dis-tribution, we do not necessarily think of this service time as consist-ing of a number of exponentially distributed service times. We use this distribution because it is a more general distribution than the ex-ponential distribution, and it has been used successfully to model the duration of an activity, such as a service time. Other mixtures of ex-ponential distributions have also been developed, but they are not dis-cussed here since they are beyond the scope of this book. The inter-ested reader can look up the phase-time distribution, which is the most general case.

1/a 1/a 1/a 1/a

Page 67: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

53

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,

𝑥 = −1𝑎�log(

k

R¢\

𝑟R).

Example: A customer in a bank may receive a service which has an Erlang distribution with three stages, each phase with a mean 10, or an Erlang distribution with four phases, each phase with a mean 5, with probability 0.4 and 0.6 respectively. Setup a procedure to gener-ate random variates of a customer's service.

Draw a random number 𝑟. If 𝑟 ≤ 0.4 then generate a stochastic vari-ate from the first Erlang distribution. Otherwise, generate a stochastic variate from the second one.

3.3.4. Sampling from a normal distribution The normal distribution (also known as the Gaussian distribution) is a very important distribution in Statistics and in many areas of science and engineering. This is because many variables are normally distrib-uted, or they can be closely approximated by a normal distribution, such as, height, intelligence, and measurement errors. Another main reason for its importance is the central limit theorem, which says that if a random variable 𝑋 is the sum of 𝑛 identical and independent ran-dom variables which may not necessarily be normally distributed, then 𝑋 follows the normal distribution as 𝑛 increases.

The normal distribution has the familiar bell-shaped symmetric curve shown in figure 3.5. Half of the points are above the mean and the other half below it. In addition, about 68% of all points fall within one standard deviation of the mean, about 96% of the points lie with-in two standard deviations of the mean, and almost all of the points lie within three standard deviations of the mean.

Page 68: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

54

Figure 3.5: The normal probability density distribution

The pdf of a normally distributed random variable 𝑋 is:

𝑓Ô(𝑥) =1

𝜎√2𝜋𝑒i

\f(¿iÖ)�×� , −∞ < 𝑥 < +∞,

where 𝜇 and 𝜎f is the mean and variance of 𝑋 respectively. If 𝜇 = 0 and 𝜎f = 1, then the normal distribution is known as the standard normal distribution and its pdf is:

𝑓Ô(𝑡) =1√2𝜋

𝑒i\fÇ

�, −∞ < 𝑡 < +∞.

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:

𝑥\ = Ï−2𝑙𝑜𝑔Ø𝑟\cos(2𝜋𝑟f)

and

𝑥f = Ï−2𝑙𝑜𝑔Ø𝑟\ sin(2𝜋𝑟f).

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µ

fX(t)

Page 69: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

55

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.

3.4.1. Generating binomial distributed stochastic variates Consider a sequence of Bernoulli trials, that is, independent trials where the outcome of each trial is either a success or a failure. Let 𝑝 be the probability of success and 1 − 𝑝 the probability of a failure in a trial. Let 𝑋 be a random variable indicating the number of successes in 𝑛 trials. Then, this random variable follows the binomial distribu-tion, and the probability that 𝑋 = 𝑖 is:

𝑝(𝑋 = 𝑖) = ¸𝑛𝑖 ¹ 𝑝R(1 − 𝑝)liR, 𝑖 = 0,1,2, …

The easiest way to generate stochastic variates is to imitate the Ber-noulli experiment. Let 𝑖be a counter set to zero. We generate a ran-dom number 𝑟\ and if 𝑟\ ≤ 𝑝,then we have a success in the first trial and 𝑖 is increased by one, otherwise we have a failure and 𝑖 is not changed. Next, we generate a second random number 𝑟f and if 𝑟f ≤𝑝, then we have a success in the second trial and 𝑖 is increased by one, otherwise we have a failure and 𝑖 is not modified. We continue in this way until we generate 𝑛 random numbers. The final value of 𝑖 is a stochastic variate from the binomial distribution.

3.4.2. Generating geometrically distributed stochastic variates Consider an infinite sequence of Bernoulli trials, where the outcome of each trial is either a success or a failure. Let 𝑝 and 1 − 𝑝 be the probability of a success and a failure respectively. The random varia-ble 𝑋that gives the number of successive failures that occur before a success occurs follows the geometric distribution. The probability density function of the geometric distribution is:

𝑝(𝑋 = 𝑖) = 𝑝R(1 − 𝑝), 𝑖 = 0,1, ….

Its cumulative probability function is:

Page 70: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

56

𝐹Ô(𝑘) = 1 − 𝑝k[\.

A stochastic variate 𝑘can be obtained using the inverse transfor-mation technique. We observe that 1 − 𝐹Ô(𝑘) = 𝑝k[\ also varies be-tween 0 and 1. Therefore, let 𝑟 be a random number, then

𝑟 = 𝑝k[\,

or

𝑙𝑜𝑔𝑟 = (𝑘 + 1)𝑙𝑜𝑔𝑝

or

𝑘 =𝑙𝑜𝑔𝑟𝑙𝑜𝑔𝑞 − 1.

The above expression can be further simplified since the quantity (1 − 𝐹Ô(𝑘))/𝑝 = 𝑝k also varies between 0 and 1. We have

𝑟 = 𝑝k,

or

𝑘 =𝑙𝑜𝑔𝑟𝑙𝑜𝑔𝑝.

3.4.3. Generating Poisson distributed stochastic variates

A Poisson random variable 𝑋 gives the number of times that an event occurs in a unit time. The probability that 𝑋 takes the value 𝑖 is given by the expression:

𝑝(𝑋 = 𝑖) = 𝑒iÒ𝜆R

𝑖! , 𝑖 = 0,1,2, …

where 𝜆 is the average number of times the event occurs in a unit time.

Page 71: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

57

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

�𝑥R

l

R¢\

≤ 1 < �𝑥R

l[\

R¢\

.

The value 𝑛 is the Poisson stochastic variate. We recall that an expo-nential variate 𝑥 is obtained using the expression: 𝑥 = −(1/𝜆)𝑙𝑜𝑔Ø(𝑟), where 1/𝜆 is the mean and 𝑟 is a random number. Conse-quently, the above expression can be re-written as:

� −1𝜆 𝑙𝑜𝑔Ø𝑟R

l

R¢\≤ 1 <� −

1𝜆 𝑙𝑜𝑔Ø𝑟R

l[\

R¢\,

or

� 𝑙𝑜𝑔Ø𝑟Rl

R¢\≥ −𝜆 >� 𝑙𝑜𝑔Ø𝑟R

l[\

R¢\

or

𝑙𝑜𝑔Ø ÛÜ 𝑟Rl

R¢\Ý ≥ −𝜆 > 𝑙𝑜𝑔Ø ÛÜ 𝑟R

l[\

R¢\Ý,

or

Ü 𝑟Rl

R¢\≥ 𝑒iÒ >Ü 𝑟R.

l[\

R¢\

The value 𝑛 for which the above expression holds is a Poisson sto-chastic variate.

Page 72: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

58

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.

a) Probability density function b) Cumulative function

Figure 3.6: Generating a stochastic variate from a discrete distribution

Consider a discrete random variable 𝑋 that takes the values 0,1,2,3 with probability 1/8,3/8,3/8,1/8 respectively, shown in figure 3.6a. Its cumulative distribution takes the values 1/8,4/8,7/8,1, and it is shown in figure 3.6b. Let 𝑟 be a random number. Then we select a stochastic variate as follows:

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.

If the random variable 𝑋 is continuous, then it is described by a histo-gram, as shown in figure 3.7. Let 𝑥R and 𝑓Ô(𝑥R), 𝑖 = 1,2,⋯ ,7, be the mid-point and height respectively of the ith rectangular. The cumula-tive distribution of 𝑋 can be approximated by the points 𝐹Ô(𝑥R) =𝑓Ô(𝑥\) + 𝑓Ô(𝑥f) + ⋯+ 𝑓Ô(𝑥R), 𝑖 = 1,2,⋯ ,7, as shown in figure 3.8.

X

p(X)

X0 1 2 3

1/8

3/8

FX(t)

1/8

4/8

7/8

1

0123

r

Page 73: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

59

Figure 3.7: A histogram of a continuous random variable

Figure 3.8: Generating a stochastic variate from a continuous distribution

Generating a stochastic variate is straightforward. Let 𝑟 be a random number and let 𝐹Ô(𝑥Ri\) ≤ 𝑟 ≤ 𝐹Ô(𝑥R). Then, we select a stochastic variate 𝑥 by interpolating between the values 𝑥Ri\ and 𝑥R, that is:

𝑥 = 𝑥Ri\ + (𝑥R − 𝑥Ri\)𝑟 − 𝐹Ô(𝑥Ri\)

𝐹Ô(𝑥R) − 𝐹Ô(𝑥Ri\).

Figure 3.9: Normalized 𝒇(𝒙)

r

x1 x2 x3 x4 x5 x6 x7

!" #

$ %

1

x1 x2 x3 x4 x5 x6 x7

f(x1)f(x2)

f(x3)f(x4)

f(x5)f(x6)

f(x7)

Page 74: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

60

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:

1. Normalize the range of 𝑓(𝑥) by a scale factor 𝑐 so that 𝑐𝑓(𝑥) < 1, 𝑎 ≤ 𝑥 ≤ 𝑏, see figure 3.9.

2. Define𝑥 as a linear function of 𝑟, i.e.𝑥 = 𝑎 +(𝑏 − 𝑎)𝑟, where 𝑟 is a random number.

3. Generate pairs of random numbers (𝑟\, 𝑟f). 4. Accept a pair and use 𝑥 = 𝑎 +(𝑏 − 𝑎)𝑟\ as a random vari-

ate whenever the pair satisfies the relationship 𝑟f ≤ 𝑐𝑓(𝑎 +(𝑏 − 𝑎)𝑟\), i.e., the pair (𝑥, 𝑟f) falls under the curve in figure 3.9.

The idea behind this approach is that the probability of 𝑟f being less than or equal to 𝑐𝑓(𝑥) is 𝑝[𝑟f ≤ 𝑐𝑓(𝑥)] = 𝑐𝑓(𝑥). Consequently, if 𝑥 is chosen at random from the range (𝑎, 𝑏) and then accepted if 𝑟f ≤𝑐𝑓(𝑥), the probability density function of the accepted 𝑥's will follow 𝑓(𝑥).

We demonstrate the rejection method by giving two examples. The first example deals with random variate generation, and the second one with numerical integration.

Example 1: Use the rejection method to generate stochastic variates with probability density function 𝑓(𝑥) = 2𝑥, 0 ≤ 𝑥 ≤ 1. We have:

1. Set 𝑐 = 1/2 such that 𝑐𝑓(𝑥) ≤ 1. 2. Generate 𝑟\, and set 𝑥 = 𝑟\. 3. Generate 𝑟f. If 𝑟f < 𝑐𝑓(𝑟\) = (1/2)2𝑟\ = 𝑟\, then accept 𝑟f,

otherwise, go back to step 2. Example 2: Use the rejection method to compute the area of the NE quadrant of a unit circle. We note that a pair of random numbers (𝑟\, 𝑟f) lies within or on the circumference of the NE quadrant of the unit circle, if 𝑟\f + 𝑟ff ≤ 1.The numerical integration is accomplished by carrying out the following two steps a large number of times:

Page 75: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

61

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.

The area under the curve is obtained as the ratio:

area =TotalnumberofacceptedpairsTotalnumberofgeneratedpairs.

The rejection method is not very efficient when 𝑐(𝑏 − 𝑎) becomes very large. In this case, the method of mixtures can be used, whereby the distribution is broken into pieces and the pieces are then sampled in proportion to the amount of distribution area each contains. This process is identical to the rejection method for each piece of the dis-tribution, plus a straightforward sampling of data.

3.7. Monte Carlo methods

Monte Carlo methods is a branch of mathematics which is concerned with computational algorithms using random numbers. Monte Carlo methods are used in physical and mathematical problems and are most useful when it is difficult or impossible to use other approaches. They are mainly used in three classes: optimization, numerical inte-gration, and generation of stochastic random numbers. The previously described rejection method for calculating the integral of a function is an example of Monte Carlo.

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. 1. The machine interference problem. Change your simulation pro-

gram so that the operational time and the repair time of a machine are exponentially distributed with the same means as before. Make sure that your clocks are defined as real variables. Run your

Page 76: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

62

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.

2. The token-based access scheme. Change your simulation program so that the inter-arrival times are exponentially distributed with the same means as before. The switch over time and the time pe-riod 𝑇 remain constant as before. The packet transmission time is calculated as follows. We assume that 80% of the transmitted packets are short and the remaining 20% are long. The time to transmit a short packet is exponentially distributed with a mean 2.5, and the time to transmit a long packet is exponentially dis-tributed with mean 20.

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.

3. The two-stage manufacturing system. Change your simulation program so that the inter-arrival, service, operational, and repair times are all exponentially distributed. Make sure that your clocks are defined as real variables. Run your simulation model as be-fore. Each time an event occurs, print out a line of output to show the new value of the clocks and the other relevant parameters.

Page 77: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

63

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.

4.2. Event-advance design This is the design employed in the three examples described in Chap-ter 1. The basic idea behind this design is that the status of the system changes each time an event occurs. During the time that elapses be-tween two successive events, the system's status remains unchanged. In view of this, it suffices to monitor only the changes in the system's status. In order to implement this idea, each event is associated with a clock. The value of this clock gives the time instance in the future that this event will occur. The simulation model, upon completion of processing an event, checks the set of events that will occur in the future, referred to as the future event list, and finds the one with the smallest clock value, say event 𝑖. Then, it advances the master clock that gives the current time in the simulation, to the timewhen event 𝑖 will occur. It takes appropriate action as dictated by the type of the event, and then repeats the process of finding the next event, and so on. The simulation model, therefore, moves through time by simply visiting the time instances at which events occur. In view of this it is known as the event-advance design.

In the machine interference problem, described in section 1.3 of Chapter 1, there are two types of events. That is, the event of an arri-val at the repairman's queue, and the event of a departure from the repairman's queue. These events are known as primary events. Quite often the occurrence of a primary event may trigger off the creation of a new event. For instance, the occurrence of an arrival at the re-pairman's queue may trigger the creation of a departure event if this

Page 78: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

64

arrival occurs at a time when the repairman is idle. Such triggered events are known as conditional events. The basic approach of the event-based design is shown in the flow chart in figure 4.1.

Figure 4.1: The event-advance simulation design

4.3. Future event list

Let us assume that a simulation model is currently at time 𝑡. The set of all the events scheduled to occur in the future, i.e., events with a clock greater than 𝑡, is known as the future event list, or simply the event list. For each event scheduled to occur in the future, the list contains the following information:

A

Find next event

Advancetime

Take appropriateaction depending

on the typeof event

Anyconditional

events?

Ano

yes

Create a newevent(s)

Futureeventlist

A

Futureeventlist

Page 79: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

65

• Time of occurrence (i.e., value of the event's clock) • Type of event

The event type is used in order to determine what action should be taken when the event occurs. For instance, using the event type the program can determine which procedure to call.

In the simulation examples described in Chapter 1, there were only a few events. For instance, in the case of the machine interference problem there were only two: an arrival to the repairman's queue and a service-ending (departure) event. However, when simulating com-plex systems, the number of events may be very large. In such cases, finding the next event might require more than a few comparisons. Naturally, it is important to have an efficient algorithm for finding the next event, since this operation may well account for a large percent-age of the total CPU time required to run a simulation program. The efficiency of this algorithm depends on how the event list is stored in a computer. An event list should be stored in such a way so as to lend itself to an efficient execution of the following operations.

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.

Figure 4.2: Event list stored in a sequential array

4.4. Event list stored in a sequential array In this scheme, all future event times are stored in a sequential array. The simplest way to implement this, is to associate each event type with a different integer number 𝑖. The clock associated with this event is always stored in the 𝑖th location of the array. For instance, in figure 4.2, clock 𝐶𝐿\for event type 1 is kept in the first location of

...!"#!"% !"& !"'

Page 80: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

66

the array, clock 𝐶𝐿ffor event type 2 is kept in the second position of the array, and so on.

Finding the next event is reduced to the problem of locating the smallest value in an array. The following simple algorithm can be used to find the smallest value in an array 𝐴.

minIndex ¬ 1 minValue ¬ A(1) For i ¬ 1,n If minValue ≤ A(i) continue else minValue ¬ A(i) minIndex ¬ i

Variable 𝑚𝑖𝑛𝐼𝑛𝑑𝑒𝑥 will eventually contain the location of the array with the smallest value. If 𝑚𝑖𝑛𝐼𝑛𝑑𝑒𝑥 = 𝑖, then the next event is of type 𝑖 and it will occur at time 𝐴(𝑖).

An event is not deleted from the array after it has occurred. If an event is not valid at a particular time, then its clock can be set to a very large value so that the above algorithm will never select it. A newly scheduled event 𝑖 is inserted in the list by simply updating its clock given by 𝐴(𝑖).

The advantage of storing an event list in a sequential array is that in-sertions of new events and deletions of executed events can be done very easily, i.e., in constant time. The time it takes to find the smallest number in the array depends on the length of the array. Locating the smallest number in an array does not take much time if the array is small. However, it becomes time consuming if the array is large. To overcome these problems, one should store the event list in a linked list, described below.

4.5. Event list stored in a linked list A linked list stores each data element in a different part of the memory. Therefore, we no longer need contiguous memory locations

Page 81: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

67

and data can be dynamically added at runtime. In order to access the data elements in their correct order, we store along with a data ele-ment the address of the next data element. This is a pointer that points to the location where the next data element is stored. This pointer is referred to as a link. A data element and the link is referred to as a node. In general, a node may consist of a number of data elements and links. Linked lists are drawn graphically as shown in figure 4.3.

Figure 4.3: A linked list

Each node is represented by a box consisting of as many compart-ments as the number of data elements and links stored in the node. In the example in figure 4.3, each node consists of two compartments, one for storing a data element and the other for storing the pointer to the next node. The pointer called head points to the first node in the list. If the linked list is empty, i.e., it contains no nodes, then head is set to a special value called NULL indicating that it does not point to any node and that the list is empty. The pointer of the last node is al-ways set to NULL indicating that this is the last node in the linked list. Due to the fact that two successive nodes are connected by a single pointer, this data structure is known as a singly 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 CLshowing 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.

Figure 4.4: Event list stored as a linked list

We now proceed to explain how to implement the basic operations on a linked list, that is, create a linked list, add a new node, and delet a node, which are also the same operations that one needs to carry out

head

... NULL

NULLhead

...CL i CL j CL n

Page 82: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

68

on an event list. We shall use the C programming language syntax to explain the implementation.

4.5.1. Defining a linked list Before we procced, let us see how the data elements and pointers are organized in a node. A simple node which saves event type, event clock, and pointer to the next node can be represented as follows:

Struc node {

Int type; Float clock; struct node* next;

}

The above structure is an example of a self-referential structure since it contains a pointer to a structure of the same type as itself which would be the next node. In addition to the data elements (i.e. type and clock) shown above the structure can contain other data which may be specific to each event.

Figure 4.5: Allocating memory for a node using malloc().

A new node is dynamically allocated each time a new event is creat-ed. Dynamic allocation of memory refers to reserving a number of bytes in the computer’s memory and setting up a pointer to the first memory location of the reserved space. In C, the system call mal-loc() is used to return such a pointer. Thus, a node can be dynami-cally allocated as follows:

sizeof(struct node) bytes

nodePtr

Memory

... ......

Page 83: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

69

struct node* nodePtr = (struct node*) malloc (sizeof (struct node));

The above call can be visualized as shown in figure 4.5.

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:

struct node* head = NULL;

After inserting nodes, the list can be traversed using the head and the pointer to next node in each member node of the list.

4.5.2. Creation of a new node

The following function creates a new node and returns a pointer.

struct node* createNode(int type, float clock) {

struct node* nodePtr = (struct node*) mal-loc (sizeof (struct node)); nodePtr->type = type; nodePtr->clock = clock; nodePtr->next = NULL; return nodePtr;

}

In the above function we allocate memory for the node and assign values to the node passed into the function as parameters. The last

Page 84: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

70

line returns a pointer to the created node. The function can be called from the program as follows:

newNodePtr = createNode(ARRIVAL, MCL+5.5);

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.

Figure 4.6: Result of calling createNode()

4.5.3. Deletion of a node When a node is deleted, the memory allocated to it must be returned to the system. This is done with the system call free() as follows:

free(nodePtr); After the above call, nodePtr points to NULL.

4.5.4. Inserting a node in a linked list In order to insert a node into the linked list, the pointer to the head of the list and the pointer to the new node must be provided. The node can then be inserted into the list in an appropriate position such that the nodes are ordered according to a specified field. For the event list, this field is the event clock. struct node* insertNode(struct node* head, struct node* newNodePtr) {

if (head == NULL) { //Case 1 return newNodePtr;

}

NULL

newNodePtr

type clock

Page 85: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

71

else if (head->clock > newNodePtr->clock) { // Case 2 newNodePtr->next = head; return newNodePtr;

} else { // Case 3 struct node* prev = NULL; struct node* curr = head; while ((curr!=NULL) && (curr->clock <= newNodePtr->clock))

{ prev = curr; curr = curr->next; }

prev->next = newNodePtr; newNodePtr->next = curr; return head;

} }

The above function can be called from a program as follows:

newNodePtr = createNode(ARRIVAL, MCL+5.5);

head = insertNode(head, newNodePtr);

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.

Case 2: head ® clock >newNodePtr ® clock

In the second case, head is not pointing to NULL and the clock value

Page 86: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

72

of head is greater than the clock value of the new node. Thus, the new node must be inserted before the head and the pointer to the new head node must be returned. This can be visualized as shown in fig-ures 4.9 and 4.10.

Figure 4.7: Head and newNodePtr before the call to insertNode()

Figure 4.8: head after the call to insertNode()

Figure 4.9: head and newNodePtr before the call to insertNode()

Figure 4.10: head after the call to insertNode()

head

NULL

newNodePtr

NULLARR 8.93

head

NULLARR 8.93

head

NULL

newNodePtr

NULL

ARR 8.93 DEP 15.8

ARR 6.7

head

NULLARR 8.93 DEP 15.8

ARR 6.7

Page 87: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

73

Note that in the above two cases, the pointer to the head changes. Thus, the pointer returned by insertNode()must be assigned to head.

Case 3: Insertion between head and last node of the list In the third case, head is not pointing to NULL and the clock value of head is less than the clock value of the new node. Thus, the new node must be inserted after the head. This can be visualized as shown in figures 4.11 and 4.12.

Figure 4.11: head and newNodePtr before the call to insertNode()

Figure 4.12: head after the call to insertNode()

4.5.5. Removing the first node of a linked list A linked list storing an event list is always maintained in an ascend-ing order of the clock value. Therefore, the next event to occur is al-ways given by the first node of the linked list. This node has to be removed after the event has been processed. This is done as follows:

struct node* removeNode(struct node* head, struct node** nextRef) {

*nextRef = head;

head

newNodePtr

NULL

ARR 8.93 DEP 15.8

ARR 10.7

head

NULLARR 8.93 DEP 15.8

ARR 10.7

Page 88: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

74

if (head != NULL) { head = head->next; }

(*nextRef)->next = NULL; return head;

}

We note that the first parameter passed in removeNode()function is the head of the linked list. The second parameter struct node** nextRef is a pointer which is used to return the memory location of the next node to the calling function.

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:

struct node* nextNodePtr = NULL; head = removeNode(head,&nextNodePtr);

4.5.6. Time complexity Let us consider the time complexity of the insert operation on a linked list, which is the most time-consuming operation on an event list. In order to insert a node in a linked list we have to traverse the linked list and compare each node until we find the correct insertion posi-tion. The maximum number of nodes compared in the worst case will be the total number of nodes in the list. Thus, the complexity of the insert operation is linear on 𝑛.

Searching a linked list might be time consuming if 𝑛 is very large. In this case, one can employ better searching procedures. For example, a simple solution is to maintain a pointer 𝐵 to a node which is in the middle of the linked list. This node logically separates the list into two sublists. By comparing clock value of the node to be inserted with the clock stored in this node, we can easily establish in which sublist the insertion is to take place. The actual insertion can then be located by sequentially searching the nodes of the sublist.

Page 89: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

75

Other data structures can be used, and the interested reader can con-sult a textbook on data structures.

4.5.7. Douby linked lists So far, we have examined singly linked lists. The main disadvantage of these lists is that they can be only traversed in one direction, name-ly from the first node to the last one. Doubly linked lists allow trav-ersing a linked list in both directions. This is possible because any two successive nodes are linked with two pointers, as shown in figure 4.13. Depending upon the application, a doubly-linked list may be more advantageous than a singly-linked list. A doubly-linked list can be processed using functions similar to those described above for sin-gly linked lists.

Figure 4.13: A doubly linked list

4.6. Unit-time advance design In the event-advance simulation, the master clock is advanced from event to event. Alternatively, the master clock can be advanced in fixed increments of time, each increment being equal to one unit of time. In view of the way of advancing the master clock, this simula-tion design is known as the unit-time advance design. Each time the master clock is advanced by a unit time, all future event clocks are compared with the current value of the master clock. If any of these clocks is equal to the current value of the master clock, then the asso-ciated event has just occurred and appropriate action has to take place. If no clock is equal to the current value of the master clock, then no event has occurred and no action has to take place. In either case, the master clock is again increased by unit time and the cycle is repeated.

The unit-time advance design is depicted in figure 4.14. Let us say that at time 𝑡 the simulation has just completed processing event 𝐸,

head

...i j k NULL

Page 90: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

76

Figure 4.14: The unit-time advance design

Figure 4.15: The unit-time advance design

E Ei Ej

t t+1 … t1 … t3

A

Master clockis increased

by a unit-time

Isany future

event clock= MC

?

An event hasoccurred

Take appropriateaction

Anyconditional

event(s)?

Schedule newevent (j)

A

A

Ano

yes

no

yes

Page 91: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

77

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 𝐸Ris 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.

The basic approach of the unit-time design is summarized in the flow-chart given in figure 4.15. In this flow-chart, as in the case of an event-advance design, a future event clock gives the time instance in the future that an event will occur. Alternatively, a future clock can simply give the duration of a particular activity. For instance, in the machine interference problem, the departure clock will contain the duration of a service, rather than the future time at which the service will be completed. In this case, the unit-time design can be modified as follows. Each time the master clock is advanced by a unit of time, the value of each future clock is decreased by a unit time. If a clock becomes equal to zero, then the associated event has occurred and appropriate action has to take place. Obviously, the way one defines the future event clock does not affect the unit-time simulation design.

Figure 4.16: A single server queue

We demonstrate the unit-time advance design by showing how to simulate a single server queue. In this simple queueing system, shown in figure 4.16, customers arrive from outside seeking to receive ser-vice at the server. A customer goes straight into service if it arrives at a time when the server is idle. A customer is forced to wait, if upon arrival it finds the server is busy or one or more customers waiting in the queue. A customer departs from the queueing system when it completes its service, and the first customer waiting at the queue starts its service immediately. The server becomes idle if there is no

Stage 1 Stage 2

Queue 1 Server 1 Queue 2 Server 2

Page 92: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

78

customer waiting at the queue when it completes a service. This queueing system is similar to the machine interference problem, with the exception that the population of customers is infinite.

Figure 4.17: A unit-time advance design of a single server queue

The simulation logic is shown in figure 4.17. Variable 𝐴𝑇 gives the inter-arrival time between two successive arrivals, 𝑆𝑇 contains the service time of the customer in service, and 𝑀𝐶𝐿 is the master clock.

We note that the variables 𝐴𝑇 and 𝑆𝑇 have been assumed to be inte-gers, multiples of the unit time. However, in many simulations, activi-ty durations, or equivalently future time instances, are real variables. This can be easily accommodated in the unit-time design by assuming that an event has occurred when its corresponding activity duration or future clock, becomes negative. For instance, if at time 𝑀𝐶 = 𝑡 vari-

A

MCL = MCL + 1

ST

End of serviceTake appropriate

action

AT

A

ST = ST - 1 ST = ST - 1

> 1 = 1

An arrival hasoccurred. Take

appropriate action

AT = AT - 1 AT = AT - 1

> 1 = 1

Page 93: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

79

able 𝐴𝑇 = 0.8,then at time 𝑀𝐶 = 𝑡 + 1, 𝐴𝑇 = −0.2. This means, that the event has occurred between 𝑡 and 𝑡 + 1, and appropriate ac-tion needs to be taken.

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.

4.6.1. Selecting a unit time The unit time is readily obtained in the case where all future event clocks are represented by integer variables. For, each event clock is simply a multiple of the unit time. Several heuristic and analytic methods have been proposed for choosing a unit time when the clocks, or equivalently the duration of the activities, are real varia-bles. Specifically, a unit time should be small enough so that at most one event occurs during the period of a unit of time. However, if it is too small, the simulation program will spend most of its time in non-productive mode, i.e., advancing the master clock and checking whether an event has occurred. A simple heuristic rule is to set the unit time equal to one-half of the smallest stochastic variate generat-ed. For this, we run the simulation and record all the generated dura-tions of the activities. Then, we set the time unit to half the value of the smallest duration.

Page 94: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

80

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.

4.6.3. Event-advance vs. unit-time advance The unit-time advance method is advantageous in cases where there are many events which occur at times close to each other. In this case, the next event can be selected fairly rapidly provided that an appro-priate value for the unit time has been selected. The best case, in fact, would occur when the events are about a unit time apart from each other.

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.

4.7. Activity-based simulation design In the event-based simulation design, the system is viewed as a col-lection of events whose occurrence trigger changes in the system. In an activity-based simulation, the system is viewed as a collection of activities or processes. For instance, for the single server queue, the activities are: a) inter arriving, b) being served, and c) waiting for ser-vice. These activities, as will be seen below, are manipulated using a set of conditions that determine when they start and stop. This is not a commonly used design, due the fact that the conditions that determine when activities start and stop can be very complex. However, it works extremely fast compared to an event-based design by several orders of magnitude.

We demonstrate this design by setting up an activity-based simulation model of the single server queue studied above in section 4.6. Let 𝑆𝑇R and 𝑊𝑇Rbe the service time and the waiting time respectively of the

Page 95: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

81

𝑖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.

Figure 4.18: Time components related to the ith arrival.

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:

1. 𝑊𝑇R[\ =(𝑊𝑇R−𝐴𝑇R[\) +𝑆𝑇R

= (𝑊𝑇R +𝑆𝑇R)−𝐴𝑇R[\

= 𝑇𝑊R − 𝐴𝑇R[\,

where 𝑇𝑊Ris the total waiting time of the 𝑖th customer in the system, i.e., queueing and being served.

2. 𝑊𝑇R[\ =(𝑊𝑇R + 𝑆𝑇R)−𝐴𝑇R[\

a s + STii s i a i+1 i

ATi+1

ST iWTi

ith arrival

ith arrivalbegins its

service

(i+1)starrival

ith arrivalends itsservice

Page 96: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

82

=𝑇𝑊R − 𝐴𝑇R[\

We observe that in both cases 1 and 2, the waiting time of the (𝑖 + 1)st arrival 𝑊𝑇R[\ is equal to 𝑇𝑊R − 𝐴𝑇R[\.

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.

Figure 4.19: An activity-based simulation design for a single server queue

One of the endogenously parameters of interest when we simulate a single server queue, is the waiting time of a customer before it starts

A

GenerateAT

CompareTW with

AT

TW = ST

A

GenerateST

TW > AT TW ≤ AT

Initialconditions

Next arrival occurs when server is idle

TW = ST + (TW - AT)

A

GenerateST

Empty systemWT = ST = 0, TW = 0

Next arrival occurswhen current arrivalis either waiting or

is in service

Page 97: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

83

receiving service. As can be seen, this is estimated without having to manipulate events and without the use of a master clock.

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.

4.8.1. An inventory system In an inventory system, one is mainly concerned with making deci-sions in order to minimize the total cost of the operation. These deci-sions are mainly related to how much inventory to order (or produce) and the frequency of ordering (or production runs). The total cost of running an inventory system consists of different types of costs. Here, we will consider the following three costs: a) holding cost, b) setup cost, and c) shortage cost. The holding cost is related to the cost of keeping one item of inventory over a unit time. One of the most im-portant components of this cost is that of the invested capital. The setup cost is related to the cost in placing a new order or setup for a production run. Finally, the shortage cost is associated with the cost of not having available a unit of inventory when demanded. This cost may be in the form of transportation charges, i.e., expediting deliver-ies, increased overtime, and loss of future business.

Let 𝐼Ç be the inventory at time 𝑡, 𝑆 be the quantity added in the system between time 𝑡 and 𝑡´, and 𝐷 be the demand between these two time instances. Then, the inventory at time 𝑡´ is: 𝐼Ç´ = 𝐼Ç + 𝑆 − 𝐷. If 𝐼Ç´ is below the re-ordering level, then an order is placed. The time it takes for the order to arrive is known as the lead time. We assume that the daily demand and the lead time follow known arbitrary distributions. The inventory level is checked at the end of each day. If it is less than or equal to the re-ordering level, an order is placed. The lead time for the order begins to count from the following day. Orders arrive in the morning before the business day begins and they can be disposed of during the same day. During stockout days, orders are backlogged. They are satisfied on the day the order arrives.

Page 98: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

84

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.

Figure 4.20: An inventory system

The simulation model is described in the flow charts given in figures 4.21 and 4.22. The model estimates the total cost of the inventory sys-tem for specific values of the reordering point and the quantity or-dered. The model keeps track of 𝐼Çon a daily basis. In view of this, the model was developed using the unit-time advance design. We note that this design arises naturally in this case. The unit of time is simply equal to one day. The lead time is expressed in the same unit time. The basic input parameters to the simulation model are the fol-lowing. a) 𝑅𝑂𝑃, reordering point, b) 𝑄, quantity ordered, c) 𝐵𝐼, the beginning inventory, d) probability distributions for variables 𝐷 and 𝐿𝑇 representing the daily demand and lead time, respectively, e) 𝑇, the total simulation time, and f) 𝐶1, 𝐶2, 𝐶3, representing the holding cost per unit, the setup cost per order, and the shortage cost per unit, respectively. The output parameters are 𝑇𝐶1, 𝑇𝐶2, 𝑇𝐶3 representing the total holding, setup and shortage costs respectively.

t t t t1 2 3 4

Reorderpoint

time

Stocklevel

Page 99: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

85

Figure 4.21: A unit-time simulation design of an inventory system

4.8.2. A round-robin queue We consider a client-server system where a number of clients send requests to a server. A client is a user-operated software that runs on a laptop or an app that runs on a mobile device. The user’s device is connected to the server over the Internet. Each time a user sends a request, the client software gets blocked, that is, the user cannot do any more processing nor can send another request. The client remains blocked until it gets an answer back from the server. This type of re-quest, whereby a client get blocked until the request is executed, is referred to as a synchronous request. The requests are executed by the

a

GenerateAT

t > T

b

Initialization

Generatedaily demand D

LT = 0I = I + Q

End

Printresults

no orderoutstanding

LT

LT = LT - 1

> 1= 1

= 0

I = I - D

order hasarrived

no

yes

MC t=t+1

Page 100: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

86

server’s CPU in a round-robin fashion, as will be explained below. For simplicity, we do not consider transmission delays between a us-er’s device and the server. Also, the server is simply modelled by a CPU, that processes the requests, and a queue in front of it where re-quests wait for their turn to be executed.

Figure 4.22: A unit time simulation design of an inventory system

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.

b

I < ROP?

LT = 0

GenerateLT

TC1 = TC1 + I*C1

TC2 = TC2+ C2

no

yesa

yes

no

I < 0no

TC1 = TC1 + I*C1

aTC3 = TC3 + I*C3

a

yes

Page 101: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

87

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.

Figure 4.23: Cycling through a think time and a CPU time

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!

Figure 4.24: A round-robin queue

think CPU think CPU think CPU

. . .

state state state state state state

• CPU

TerminalsRequestsUsers

Page 102: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

88

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.

The events associated with this system are: a) arrival of a request at the CPU queue, and b) service completion at the CPU. A request ar-riving at the CPU queue may be either a new arrival (i.e., a user sends a request, thus ending the think state), or it may be a request that has just received a quantum of CPU time and it requires further pro-cessing. A departing request from the CPU may either go back to the originating user, or it may simply join the end of the CPU queue for further execution. We observe that during the period of time that the CPU is busy, departure events occur every quantum of time. Howev-er, new arrivals of requests at the CPU queue occur at time instances which may be a few hundreds of quanta apart.

An event-advance simulation model can be developed following the same approach as in the machine interference problem. The future event list may contain an event associated with a departure from the CPU and a number of events are associated with future new arrivals of requests. For instance, let us assume that the total number of users is 10, and that at some arbitrary time there are seven users in the think state, which means that there are three requests being processed by the CPU. In this case, the event list consists of a departure from the CPU event, and seven events each associated with a future arrival of a request from a user in the think state. When a departure occurs, most likely a new departure event will be scheduled to occur in the next quantum of time. This new event will more likely be the next event to occur. In view of this, most of the time, a newly created event will be simply inserted at the top of the future event list. Such insertions can be done in 0(1) time.

We now give an alternative simulation design of the round-robin queue which utilizes both the event-advance and unit-time advance designs! Specifically, all the events related to new arrivals of requests

Page 103: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

89

at the CPU queue are kept in a linked list as shown in figure 4.25. Each node is associated with an event 𝑖and it contains a future event time 𝑡R and a client identification number 𝑐R. The event time simply shows the time at which the user will stop thinking and will access the CPU. Nodes are ordered in an ascending order of the data element that contains the future event time. Thus, the next new arrival event is given by the first node of the linked list.

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).

Figure 4.26: Future event list of all new arrivals to the CPU queue

NULLhead

...!1 #1 !2 #2 !1 #1

H ...!1 #1 !2 #2 E!% #%

Page 104: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

90

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.

Figure 4.27: Hybrid simulation design of the round-robin queue

Problems Consider the following systems:

a. Checkout stands at a supermarket b. Teller's window at a bank c. Elevators serving an office building d. Traffic lights in a configuration of 8 city blocks e. Outpatient clinic f. Pumps at a gasoline station

b

IsCPU

queueempty

?A departureevent occurs

MCL = MCL + 1no

b

b

yes

IsMCL=tarr

?

no

yes

A new arrivalevent occurs

b

A new arrivalevent occurs

MCL = tarr

Page 105: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

91

g. Parking lot h. Runways at an airport

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. 1. Implement the hybrid simulation model of the round-robin queue

discussed in section 4.6.2.

2. The machine interference problem. Modify your simulation model so that the event list is maintained in the form of a linked list. As-sume that the queue of broken-down machines can be repaired by more than one server (i.e., there are more than one repairman re-pairing machines off the same queue). Parametrize your program so that it can run for any number of machines and repairmen (maximum 20 and 10, respectively). Run your simulation model until 20 repairs have been completed. As before, each time an event occurs, print out the usual line of output and also additional information pertaining to the event list. Check by hand that your linked list implementation is correct.

3. The token-based access scheme problem. Assume that transmis-sions are not error-free. That is, when a packet arrives at the des-tination node, it may contain errors. In this case, the packet will have to be re-transmitted. The procedure is as follows.

Upon completion of the transmission of a packet, the host will wait to hear from the receiving host whether the packet has been received correctly or not. The time for the receiver to notify the sender may be assumed to be constant. If the packet has been cor-

Page 106: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

92

rectly received, the host will proceed with the next transmission. If the packet has been erroneously transmitted, the sender will re-transmit the packet. There is 0.01 probability that the packet has been transmitted erroneously. The sender will re-transmit the packet immediately. This procedure will be repeated until the packet is correctly transmitted. No more than 5 re-transmissions will be attempted. After the 5th re-transmission, the packet will be discarded, and the sender will proceed to transmit another packet. All these re-transmissions take place while the host has the token. When the token's time-out occurs, the host will carry on re-transmitting until either the packet is transmitted correctly, or the packet is discarded.

Describe how you will modify your simulation model in order to accommodate the above acknowledgement scheme. Can this re-transmission scheme be accommodated without introducing more events? If yes, how? If no, what additional events need to be in-troduced? Describe what action will be taken each time one of these additional events takes place. Also, describe how these new events will interact with the existing events (i.e., triggering-off each other).

4. The token-based access scheme problem. Modify the simulation design in order to take advantage of the structure of the system. Specifically, do not generate arrivals to each of the nodes. Store the residual inter-arrival time when the node surrenders the token. Then, when the token comes back to the station, generate arrivals (starting from where you stopped last time) until the token times-out or it is surrendered by the node. This change leads to a con-siderably simpler simulation model.

5. Two-stage manufacturing model. Modify your simulation model so that the event list is maintained in the form of a linked list. Run your simulation model until 20 customers have completed their service at queue 2 for different values of the capacity of queue 2.

Page 107: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

93

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.

5.2. Collecting endogenously created data The performance metrics that we typically want to estimate in a simu-lation are a) frequency of occurrence of a particular event, and b) du-ration of an activity. For instance, in a simulation of a parking lot, we may want to know the probability 𝑝of a car not finding a place to park upon arrival at the parking lot. This can be easily calculated by keeping track of the following two variables: 𝑋 for counting the num-ber of simulated cars that arrived at the parking lot to find it full, and 𝑌 for counting the total number of arrivals at the parking lot. At the end of the simulation, we compute 𝑝 as the ratio of these two varia-bles, that is, 𝑝 = 𝑋/𝑌.

An example of a metric expressed as the duration of an activity, is the down time of a machine in the machine interference problem. This is the time the machine spends queueing up waiting for the repairman plus its repair time. The down time of a machine can be obtained by keeping the following information: a) time of arrival at the repair-man's queue, and b) time at which the repair was completed. At the end of the simulation, the downtime for each machine that broke down is computed by taking the difference of its departure time mi-nus its arrival time. Various statistics of the down time can be com-

Page 108: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

94

puted using this sample, such as, the mean, the standard deviation, and percentiles.

Figure 5.1: The linked list for the repairman's queue

The information on the arrival and departure times of the broken-down machines can be collected by maintaining a linked list that rep-resents the machines which are in the repairman's queue. This linked list is shown in figure 5.1. Each node contains the following two data elements: a) time of arrival at the repairman's queue 𝑡R, and b) id number of the machine. The nodes are linked in FIFO manner so that to represent the way in which the machines are served. That is, the first node, pointed by H, represents the machine currently in service. If a machine arrives at the repairman's queue, a new node will be ap-pended after the last node pointed by E. The total down time of a ma-chine is calculated at the instance when the machine departs from the repairman. This is equal to the master clock's value at that instance minus its arrival time. This information is saved in a one-dimensional array, and at the end of the simulation it can be processed in order to obtain various statistics.

An alternative and simpler method to using a linked list is to use a two-dimensional array. Let us consider the simulation model of the token-based access scheme. The simulation program can be enhanced so that each node is associated with a two-dimensional array, as shown in figure 5.2. The first column contains the arrival times of packets, and the second column contains their departure time. For node 𝑖, the locations between pointers Hi and Ei give the arrival times of the packets currently in the queue, including the one in ser-vice. The locations before Hi contain the arrival and departures times of packets that already have been through the queue. When a packet arrives at queue 𝑖, its arrival time is stored at the location after the one pointed by Ei. When a packet departs from the node, its departure time is stored in the corresponding location of the second column.

EH ...!" # !$ % !& '

Page 109: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

95

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.

Another statistic of interest is the probability distribution of the num-ber of broken-down machines. In this case, the maximum number of broken-down machines does not exceed 𝑀, the total number of ma-chines. In view of this, it suffices to maintain an array with 𝑀 + 1 locations. Location 𝑖 contains the total time during which the number of broken-down machines (waiting and being served) was 𝑖, 𝑖 =0,1,2, … ,𝑀. This is computed as follows. Let us assume that at time 𝑡\ a machine breaks down and joins the repairman’s queue which brings the total number of broken-down machines to, say, 2. Later on at time 𝑡f another machine breaks down which brings the number of broken-down machines to 3. The time 𝑡f − 𝑡\ is a time period during which the number of broken-down machines is 2. This time is saved cumulatively in location 2. Likewise, if instead of an arrival, we have a departure at time 𝑡f, the time period 𝑡f − 𝑡\ is also saved cummula-tively in location 𝑖. At the end of the simulation, this location will contain the total amount of time during which the number of broken-down machines was 2. Likewise, for the other locations in the array. The probability 𝑝(𝑛) that there are 𝑛 broken-down machines is then

E3

H3

E1

H1

x x

x x

x x

x x

x

x

x

Arr DepNode 1

E2

H2

x x

x x

x x

x

x

Arr DepNode 2

x x

x x

x

x

Arr DepNode 3

Page 110: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

96

obtained by dividing the contents of the 𝑛th location by 𝑇, the total simulation time.

5.3. Transient state vs steady-state simulation In general, a simulation model can be used to estimate a parameter of interest during the transient state or the steady state. Let us assume that in the machine interference problem we are interested in obtain-ing statistics pertaining to the number of broken-down machines. The simulation starts by assuming that the system at time zero is at a giv-en state. This is known as the initial condition. Evidently, the behav-ior of the system will be affected by the particular initial condition. However, if we let the simulation run for a long period, its statistical behavior will eventually become independent of the particular initial condition. In general, the initial condition will affect the behavior of the system for an initial period of time 𝑇. Thereafter, the simulation will behave statistically in the same way whatever the initial condi-tion. During this initial period 𝑇, the simulated system is said to be in the transient state. After 𝑇, the simulated system is said to be in the steady state.

5.3.1. Transient-state simulation Transient-state simulation is used when we are interested in studying how the system behaves for a specific initial starting condition. Also, there are cases where the system does not have a steady state, and therefore one has to resort to transient-state simulation. Such a case may arise when the system under study is constantly changing.

5.3.2. Steady-state simulation In general, transient state simulation is not very common. Typically, a simulation model is used to study the steady-state behavior of a sys-tem, and for this, the simulation model has to run long enough so that to get away from the transient state which is influenced by the initial conditions. There are two basic strategies for choosing the initial con-ditions. The first strategy is to begin with an empty system. That is, we assume that there are no activities going on in the system at the beginning of the simulation. The second strategy is to make the initial condition to be as representative as possible of the typical states the

Page 111: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

97

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.

This brings us to the problem of how to determine when the simula-tion system has reached its steady state. There are various methods to do that, but in practice, we just discard the first few hundreds of the collected data. If in doubt, run your simulation multiple times, and each time increase the transient period. Select the transient period past which no change in the performance metrics collected during the steady state is observed.

5.4. Estimation techniques for steady-state simulation Most of the performance measures that one would like to estimate through simulation are related to the probability distribution of an en-dogenously created random variable. The most commonly computed measure of an endogenously created random variable is the mean. For instance, in the machine interference problem we are interested in the down time, and for this, we estimate the mean down time. However, the mean does not provide a sense of the upper bound of the down time. For this, we need to calculate a percentile which gives us a probabilistic upper bound, that is, an upper bound that holds for most of the down times but not all of them. Typical percentiles used are the

Page 112: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

98

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.

In general, it is a good practice to estimate the mean and a percentile of an endogenously created random variable. Below, we describe var-ious estimation procedures. We assume that the observations have been collected when the simulation is in steady state.

5.5. Estimation of the confidence interval of the mean Let us assume that we have collected a sample of n endogenous data for a performance metric, such as the down time in the machine inter-ference problem, when the simulation is in steady state. Let 𝑥\, 𝑥f, … , 𝑥l be the n observations. Then, the sample mean is:

�̅� =1𝑛� 𝑥R.

l

R¢\

Typically, when we estimate the mean of a performance metric, we also provide an error of the estimation. For instance, in opinion polls, we typically hear that 𝑥% of the people prefer candidate 𝑋 and that the error is 3%. This means that the true percentage of the population who prefer candidate 𝑋 is within 𝑥 ± 3%. This interval is known as the confidence interval, because the true population percentage lies in this interval with some probability, such as 95%. This probability is

Page 113: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

99

known as the confidence level. The confidence interval of the sample mean �̅� is given by the expression:

Û�̅� − 1.96𝜎√𝑛

, �̅� + 1.96𝜎√𝑛Ý

at 95% confidence level. The confidence interval provides an indica-tion of the error associated with the sample mean. It is a very useful statistic and it should always be computed. Unfortunately, quite fre-quently it is ignored. The confidence interval tells us that the true population mean lies within the interval 95% of the time. That is, if we repeat the above experiment 100 times, 95% of these times, on the average, the true population mean will be within the interval.

The derivation of the confidence interval is very simple and it is based on the Central Limit theorem. This theorem says that if a ran-dom variable 𝑋 is the sum of 𝑛 identical and independent random var-iables which may not necessarily be normally distributed, then 𝑋 fol-lows the normal distribution as 𝑛 increases. Therefore, if we sample 𝑛 observations from a population which follows an arbitrary distribu-tion with a mean 𝜇 and variance 𝜎f, then the sample mean �̅� is nor-mally distributed with mean 𝜇 and variance𝜎f/𝑛, for 𝑛 ≥ 30.

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.

Page 114: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

100

Figure 5.3: Points a and b in the normal distribution

In general, a confidence interval can be calculated for any confidence level. Most typical confidence levels are 99%, 95% and 90%. For each value, points 𝑎 and 𝑏 can be calculated from the table of the standard normal distribution. The 𝑧 value at 90% and 99% confi-dence is 1.645 and 2.56 respectively. We observe that a small confi-dence interval comes at the expense of having lower confidence about the result!

In order to construct a confidence interval of the sample mean �̅�, we need to know the variance 𝜎f. If it is not known, then we use the sample variance 𝑠f given by the familiar expression:

𝑠f =1

𝑛 − 1�(𝑥R − �̅�)f

l

R¢\

.

Note that dividing the above summation ∑ (𝑥R − �̅�)flR¢\ by 𝑛 − 1

gives an unbiased estimator of 𝜎f, whereas dividing it by 𝑛 gives the maximum likelihood estimator of 𝜎f. Either denominators can be used.

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

0

0.05

0.1

0.15

0.2

0.25

0 2 4 6 8 10 12 14 16 18 20

fX(t)

µ

fX(t)

a b

95% 2.5%2.5%

Page 115: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

101

allows the calculation on the fly without having to store all the obser-vations.

𝑠f =1

𝑛 − 1É� 𝑥Rf −1𝑛

l

R¢\Û� 𝑥R

l

R¢\ÝfÊ.

That is, when the 𝑘th observation is obtained, we update the cumula-tive sums ∑ 𝑥\fk

R¢\ and ∑ 𝑥RkR¢\ , 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.

Quite frequently, the observations 𝑥\, 𝑥f, … , 𝑥l that we obtain endog-enously from a simulation model are correlated. For instance, the down time of a machine 𝑖depends on the down time of the machine 𝑗 in front of it in the repairman's queue. If the down time of 𝑗is long, then the down time of 𝑖is also long. Likewise, if the down time of 𝑗is short, then the down time of 𝑖is also short. In general, two random variables are uncorrelated, or positively correlated or negatively cor-related, as shown in figure 5.4.

Page 116: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

102

a) Uncorrelated, i.e., 𝒓𝑿𝒀 = 0

b) Positively correlated, i.e., 𝒓𝑿𝒀 > 0

c) Negatively correlated, i.e., 𝒓𝑿𝒀 < 0

Figure 5.4: The three cases of correlation

Y

X

Y

X

Y

X

Page 117: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

103

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:

a. Estimation of the autocorrelation function b. Batch means c. Replications

5.5.1. Estimation of the autocorrelation function (ACF)

Let 𝑋 and 𝑌 be two random variables. Then, covariance of X and Y 𝐶𝑜𝑣(𝑋, 𝑌) is given by the expression:

𝐶𝑜𝑣(𝑋, 𝑌) = 𝐸(𝑋𝑌) − 𝐸(𝑋)𝐸(𝑌)].

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:

𝐶𝑜𝑟𝑟(𝑋, 𝑌) =𝐶𝑜𝑣(𝑋, 𝑌)

Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌).

Let (𝑥\, 𝑦\), (𝑥f, 𝑦f), … , (𝑥l, 𝑦l) be 𝑛 pairs of observations of (𝑋, 𝑌). Then, the sample covariance 𝜌Ôð is given by the expression:

𝜌Ôð =∑ (𝑥R − �̅�)(𝑦R − 𝑦µ)lR¢\

𝑛 − 1 ,

and the sample correlation 𝑟Ôð, obtained by normalizing the sample covariance, is:

Page 118: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

104

𝑟Ôð =∑ (𝑥R − �̅�)(𝑦R − 𝑦µ)lR¢\

Ï∑ (𝑥R − �̅�)flñ¢\ Ï∑ (𝑦R − 𝑦µ)fl

ñ¢\.

It can be shown that −1 ≤ 𝑟Ôð ≤ 1. 𝑋 and 𝑌 are positively correlated if 𝑟Ôð > 0, and they are strongly positively correlated if𝑟Ôð = 1. 𝑋 and 𝑌 are negatively correlated if 𝑟Ôð < 0, and they are strongly negatively correlated if𝑟Ôð = −1. Finally, they are uncorrelated, i.e., independent from each other if 𝑟Ôð = 0.

Now, let us now consider 𝑛 observations 𝑥\, 𝑥f, … , 𝑥lfrom a random variable 𝑋. We form the following 𝑛 − 1 pairs of observations: (𝑥\, 𝑥f), (𝑥f, 𝑥e), … , (𝑥li\, 𝑥l). We regard the first observation in each pair as coming from 𝑋 and the second observation as coming from a random variable Y. Then, 𝑟Ôðis:

𝑟Ôð =∑ (𝑥R − �̅�)(𝑥R[\ − �̅�)li\R¢\

Ï∑ (𝑥R − �̅�)fli\ñ¢\ Ï∑ (𝑥R[\ − �̅�)fli\

ñ¢\,

where

�̅� =1

𝑛 − 1�𝑥Rand𝑦µ =1

𝑛 − 1�𝑥R.l

R¢f

li\

R¢\

The above correlation 𝑟Ôð is called the lag 1 autocorrelation of a ran-dom variable and we refer to it as 𝑟\, where the subscript stands for lag 1. If 𝑛 is large, it can be approximated as follows:

𝑟\ =∑ (𝑥R − �̅�)(𝑥R[\ − �̅�)li\R¢\

∑ (𝑥R − �̅�)flñ¢\

,

where �̅� is the sample mean of the 𝑛 observations. In a similar fash-ion, we can obtain the lag 𝑘 autocorrelation, that is the correlation between observations which are 𝑘 apart. This is given by the expres-sion:

𝑟k =∑ (𝑥R − �̅�)(𝑥R[k − �̅�)likR¢\

∑ (𝑥R − �̅�)flñ¢\

, 𝑘 ≥ 1,

Page 119: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

105

where �̅� is the overall sample mean. The lag 𝑘 autocorrelations, 𝑘 ≥1, are also referred to as the autocorrelation coefficients.

The autocorrelation can also be computed by first computing the au-to-covariances 𝜌\, 𝜌f, … , 𝜌l where 𝜌k is given by the expression:

𝜌k =1𝑛�(𝑥Ri�̅�)(lik

R¢\

𝑥R[k − �̅�).

The lag 𝑘 autocorrelation is computed as the ratio:

𝑟k =𝜌k𝑠f,

where 𝑠f is the sample variance. We note that 𝑟c = 1, since 𝜌c = 𝑠f. The set of sample autocorrelations computed from a given sample, are typically presented in a graph, known as the correlogram. Two examples of a correlogram are shown in figures 5.5 and 5.6.

Figure 5.5: A correlogram with short-term correlation

Figure 5.6: A alternating correlogram

lag k

rk

+1

lag k

rk

+1

Page 120: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

106

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:

𝑠f = 𝑠Ôf ò1 + 2�Û1 −𝑘𝑛 𝑟kÝ

l/{

k¢\

ó,

where 𝑠Ôf is the sample variance given by

𝑠f = \li\

∑ (𝑥R − �̅�)f,lR¢\ where �̅� = \

l∑ 𝑥R.lR¢\

That is, in the case of correlated observations, the sample variance s2

calculated under the assumption that the observations are uncorrelat-ed, is modified by the above term in the square brackets.

Figure 5.7: The batch means method

5.5.2. The batch means method This is a popular technique and very easy to implement. It involves dividing successive observations into batches as shown in figure 5.7. Each batch contains the same number of observations. Let the batch size be equal to 𝑏. Then, batch 1 contains observations 𝑥\, 𝑥f, … , 𝑥�, batch 2 observations 𝑥�[\, 𝑥�[f, … , 𝑥f�, and so on. The total number observations n should be selected so that 𝑛 = 𝑚𝑏. The observations at the end of a batch maybe be correlated with those at the beginning of the next batch. Let �̅�R be the sample mean of the 𝑖th batch. If we chose the batch size 𝑏 to be large enough, then the sequence of the 𝑚 sample means �̅�\, �̅�f, … , �̅�|, are approximately uncorrelated. Conse-quently, we can calculate the standard deviation using the standard expression. We have:

!",!$,… , !&, !&'",!&'$,… , !$&, !()*")&'",!()*")&'$,… , !)&…

Page 121: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

107

�̿� =1𝑚��̅�R

|

R¢\

𝑠f =1

𝑚 − 1�(�̅�R − �̿�)f

|

R¢\

,

and the confidence interval of �̿� is:

Û�̿� − 𝑡c.õ²𝑠√𝑚

, �̿� + 𝑡c.õ²𝑠√𝑚

Ý.

An estimate of 𝑏 can be obtained by plotting out the correlogram of all the 𝑚𝑏 observations, which can be obtained from a preliminary simulation run. Statistical packages that plot a correlogram, also plot two symmetric lines about the 𝑥-axis, i.e., one above the 𝑥-axis and the other below it. These two lines give the confidence interval bounds for the null hypothesis that the lag 𝑘 autocorrelation is zero. In simple terms, if the lag 𝑘 autocorrelation is above the upper line or below the lower line, then we assume that it is not zero. If these con-fidence interval lines are not given, then the rule of thumb is to use a cutoff of around 0.15. That is, if the absolute value of lag 𝑘 is less than 0.15, then we assume that lag 𝑘 is zero. That is, the pairs (𝑥R, 𝑥R[k), 𝑖 ≥ 1, are not correlated. Going back to estimating 𝑏, we select the first lag 𝑘 which falls within the confidence interval bounds, and we fix 𝑏 so that it is at least 5 × 𝑘. The number of batch-es 𝑚 is typically more than 30.

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.

5.5.3. The replication method Another approach to constructing a confidence interval of a mean is to replicate the simulation several times. For each replication, we use a different seed, which gives rise to a different sequence of pseudo-random numbers, and consequently a different sequence of events. As

Page 122: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

108

a result, we get a different set of observations for each replication. The difference between two successive seeds should be large.

Suppose we make m replications each with l observations, and we get the following sets of observations:

Replication 1: 𝑥\\, 𝑥\f, … , 𝑥\ö Replication 2: 𝑥f\, 𝑥ff, … , 𝑥fö

⋮ Replication 𝑚: 𝑥|\, 𝑥|f, … , 𝑥|ö

Let �̅�R be the sample mean of the 𝑖th replication, that is:

�̅�R =1𝑙 � 𝑥Ru

ö

u¢\, 𝑖 = 1,2, … ,𝑚.

The sample means �̅�\, �̅�f, … , �̅�| are independent from each other and therefore the confidence interval is:

Û�̿� − 𝑡c.õ²𝑠√𝑚

, �̿� + 𝑡c.õ²𝑠√𝑚

Ý,

where

�̿� =1𝑚��̅�R

|

R¢\

and𝑠f =1

𝑚 − 1�(�̅�R − �̿�)f.

|

R¢\

For each replication, we allow the simulation to go through the warm up period and then run it long enough so that to collect the 𝑙 observa-tions. This procedure is repeated 𝑚 times.

The replication method appears to be similar to the batch means method. However, in the batch means method the batch size is rela-tively small and the number of batches is large. In the replications method it is the other way around. That is, each a replication is very large and the number of replications can be small, as little as 5.

Page 123: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

109

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.

These high percentiles give us an idea of the shape of the right-hand side tail of the probability distribution. For, if 𝑥c.õ² is close to the mean, then the tail is very short. On the other hand, if it is far away from the mean, then the distribution has a long tail. We are interested in the shape of the right-hand tail of the distribution as it provides a probabilistic upper bound of a random variable.

Percentiles are calculated as follows. Let 𝑥\, 𝑥f, … , 𝑥l, be a sample of an endogenously created random variable X. The sample is sorted out in an ascending order, and let 𝑦\ ≤ 𝑦f ≤ … ≤ 𝑦l be the sorted ob-servations. Then, the 95th percentile (or any other percentile) of 𝑋 is the value 𝑦k where k = ⌈0.95x𝑛⌉. (The function ⌈𝑦⌉ is the ceiling function that maps a real number 𝑥 to the smallest integer not less than 𝑥.) For instance, for a sample of 950 observations, 𝑘 =⌈0.95x950⌉ = 903, and the 95th percentile is the value 𝑦õce.

The confidence interval of a percentile can be calculated easily using the batch means method or the replication method. Let us assume that we are interested in the 95th percentile. After 𝑚 batches or replica-tions, we obtain a sample of 𝑚 independent estimates of the 95th per-centile, i.e., 𝑥c.õ²,\, 𝑥c.õ²,f, … , 𝑥c.õ²,|. Then, we have:

Û𝑥�0.95 − 𝑡c.õ²𝑠√𝑚

, 𝑥�0.95 + 𝑡c.õ²𝑠√𝑚

Ý

where

Page 124: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

110

�̅�c.õ² =1𝑚�𝑥c.õ²,R

𝑚

𝑖=1

and𝑠2 =1

𝑚 − 1��𝑥c.õ²,R − �̅�c.õ² 

2.

𝑚

𝑖=1

The estimation of high percentiles requires very long batches or repli-cation runs, because we want to collect as many as possible observa-tions in the right-hand tail of the distribution. If the batches are not long, then the estimates will be inaccurate. The calculation of a per-centile requires that we store the entire sample of observations until the end of the simulation, and that we order the sample of observa-tions in an ascending order. These two operations can be avoided by constructing a frequency histogram of the random variable on the fly. When an observation becomes available, it is immediately classified into the appropriate bin of the histogram. Thus, it suffices to keep track of how many observations fall within each bin. At the end of each batch, the percentile can be easily picked out from the histo-gram. Obviously, the accuracy of this implementation depends on the chosen width of the bins of the histogram.

5.7. Estimation of the confidence interval of a probability

The estimation of the confidence interval of the probability 𝑝 that a particular event 𝐸 occurs can be handled in the same way as the esti-mation of the confidence interval of a mean or a percentile using the batch means method or the replication method. For instance, let us say that in the machine interference model we want to calculate the probability that a machine that breaks down finds the repairman idle. We introduce the variables 𝑁 and 𝐾 in the simulation; the first one keeps count of the total number of machine that broke down, and the second one keeps count of the total number of those machine that broke down at a time that the repairman was idle. Then, 𝑝 = 𝐾/𝑁. Using the batch means method or the replication method we calculate 𝑚 independent estimates of 𝑝, i.e., 𝑝\, 𝑝f, … , 𝑝|. Then, we have:

Û�̅� − 𝑡c.õ²𝑠√𝑚

, �̅� + 𝑡c.õ²𝑠√𝑚

Ý

where

Page 125: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

111

�̅� =1𝑚�𝑝R

|

R¢\

and𝑠f =1

𝑚 − 1�(𝑝R − �̅�)f.

|

R¢\

5.8. Estimation techniques for transient-state simulation The statistical behavior of a simulation during its transient state de-pends on the initial condition. We can estimate a statistic of a random variable 𝑋 during the transient state using the replication method, see section 5.3.3. For each replication we start the simulation with the same initial condition and run the simulation for the transient period. This, of course, requires advanced knowledge of the length of the transient period. Furthermore, the pseudo-random numbers used in a replication have to be independent of those used in previous replica-tions. This can be achieved by using a different seed for each replica-tion. The difference between two seeds should be about 10,000.

5.9. Achieving a required accuracy

So far, we discussed techniques for generating confidence intervals for various statistics of an endogenously generated random variable. In general, the accuracy of an estimate of a statistic depends on the width of the confidence interval 𝑡c.õ²�𝑠 √𝑛⁄  , where 𝑛 is the number of observations used. The width of the confidence interval is also re-ferred to as the error. The smaller the width, the higher is the accura-cy. For instance, if it is concluded in an opinion poll that 30% of the population will vote for a candidate with an error of ±5%, then that means that the true population percent of people who will vote for the candidate lies in interval (25%, 35%) for a given level of confidence. However, this is not the same as concluding that 30% will vote for the candidate with an error of ±1%, i.e., the confidence interval is (29%, 31%). Obviously, in the second case the accuracy is much higher.

We note that the width 𝑡c.õ²�𝑠 √𝑛⁄  is proportional to 1 √𝑛⁄ . There-fore, the width can be reduced if we increase 𝑛. For instance, in order to halve the width, 𝑛 has to be increased four times since 1 √4𝑛⁄ =(1/2)(1 √𝑛⁄ ). That is, we need to run the simulation long enough so that to collect a total of 4𝑛 observations. A rule of thumb is that 𝑛

Page 126: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

112

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 𝑛.

An alternative approach is to determine the desired simulation length on the fly. That is, the simulation runs continuously and periodically we check the width of the confidence interval. We continue until the desired accuracy has been achieved. Let us assume that we want to estimate a statistic 𝜃 of a random variable using independent replica-tions or the batch means method, with a width of the confidence in-terval approximately equal to 0.1𝜃þ, where 𝜃þis a point estimate of 𝜃. We first run the simulation with𝑛 replications or batch means, and let 𝜃þ\ be the point estimate of 𝜃and 𝛿\ the width of its confidence inter-val. If 𝛿\ ≤ 0.1𝜃þ\, then we stop. Otherwise, we run the simulation for another 𝑛 replications or batches and calculate 𝜃þfand 𝛿f using the 2𝑛replications. If 𝛿f ≤ 0.12, then we stop, else, we repeat the above step.

.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.

1. The machine interference problem. Implement a statistical estima-tion procedure in your program and obtain performance curves as described below.

a. Statistical estimation procedure Remove the print statement that prints out a line of output each time an event occurs. Setup a data structure to collect information

Page 127: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

113

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.

Page 128: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

114

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.

You can also run a similar experiment by further extending your simulation model to the case where the number of machines is al-so an input to the simulation. Now, you can vary both the number of repairmen and machines!

Note: Draw the confidence intervals at each simulated point as a vertical line. You may find that for some values the confidence in-tervals may be longer than others. For these cases, simply in-crease the number of batches.

2. The token-based access scheme. Implement a statistical estimation procedure in your program and obtain performance curves as de-scribed below.

a. Statistical estimation procedure

Change the inter-arrival times to 20, 25, and 30 instead of 10, 15, and 20 that you used before which leads to an unstable sys-tem, that is, the queues may become infinitely big. Remove the print statement that prints out a line of output each time an event occurs. Augment your simulation to record how much time each packet spends in a queue, from the time when it joins the queue to the time it is transmitted out. We will refer to this time as the re-sponse time. Record this information in the order in which packets arrive to the token-based system.

Page 129: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

115

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.

Estimate the mean response time 𝑅and the 95th percentile 𝑅c.õ²of the response time by varying 𝑀𝐼𝐴𝑇 while 𝑇 is fixed equal to 15. You will observe that 𝑅c.õ²is much larger than 𝑅 since it is an estimate of the tail of the distribution, unless the distribution has very low variability in which case the two estimates will be close. Start with a large value of 𝑀𝐼𝐴𝑇, such as 𝑀𝐼𝐴𝑇 = 40, and then decrease it by 5 each time you run the simulation. It is im-portant that 𝑀𝐼𝐴𝑇 does not get smaller than a critical value

Page 130: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

116

𝑀𝐼𝐴𝑇∗ below which the queues become unstable. That is, they continuously build up and they grow infinitely big as time goes on. In this case you will observe your simulation program will eventually run out of memory. Also, in this case there is no need to simulate the system in order to estimate 𝑅and 𝑅c.õ² since they are both infinitely big. (In general, a system which uses queues should operate within parameters so that the queues are always stable.) The problem, however, is that it is not easy to calculate 𝑀𝐼𝐴𝑇∗. In view of this, you need to proceed with 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.

Repeat the above experiment by varying 𝑇 for each value of the 𝑀𝐼𝐴𝑇 that you used. Vary 𝑇 sufficiently so that you can obtain a complete picture of a trend. Graph your results of 𝑅and 𝑅c.õ²ver-sus 𝑇 for each value of 𝑀𝐼𝐴𝑇 and discuss the observed trends. (You can also graph your results of 𝑅and 𝑅c.õ²versus 𝑀𝐼𝐴𝑇 for each value of 𝑇.)

Modify the simulation model so that the number of queues is an input variable and obtain similar graphs as above for different number of queues.

Note: Draw the confidence intervals at each simulated point as a vertical line. You may find that for some values the confidence in-tervals may be longer than others. For these cases, simply in-crease the number of batches.

3. The two-stage manufacturing system. Implement a statistical es-timation procedure in your program and obtain performance curves as described below.

a. Statistical estimation procedure Remove the print statement that prints out a line of output each time an event occurs. Setup a data structure to collect information regarding the amount of time a customer spends in the system, i.e.

Page 131: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

117

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.

Run your simulation until you have obtained 1050 production times. (That is, stop the simulation after the 1050th customer de-parts from server 2. Ignore the remaining customers in the queues.) Discard the first 50 observations, i.e., production times, to account for the transient state and use a statistical package to calculate and graph the correlogram of the remaining 1000 obser-vations. Calculate the batch size based on the correlogram. Im-plement the batch means method 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 time a customer spends in the system.

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). Estimate the mean production time 𝑃and the 95th percentile 𝑃c.õ²by varying the mean inter-arrival time 𝑀𝐼𝐴𝑇 with the re-maining parameters fixed as follows: 𝑀𝑆𝑇1 = 20,𝑀𝑂𝑇1 =

Page 132: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

118

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.

Recall that upon a service completion at server 1, the server gets blocked if queue 2 is full. That is, the server cannot serve any other customers that may be waiting in queue 1. The server will remain blocked until a customer departs from server 2. In this case, a space will become available in queue 2 and the served cus-tomer in front of server 1 will move into queue 2 thus freeing server 1 to serve other customer in queue 1. Obviously, the block-ing of server 1 affects the production time since during that time it cannot serve other customer in queue 1. The more frequently it gets blocked, the longer is the production time. Estimate the mean production time 𝑃and the 95th percentile 𝑃c.õ²by varying the mean service time 𝑀𝑆𝑇2 of server 2 while the remaining parame-ters remain fixed as in the previous experiment, that is, 𝑀𝑆𝑇1 =20,𝑀𝑂𝑇1 = 200,𝑀𝑅𝑇1 = 50,𝑀𝑂𝑇2 = 300,𝑀𝑅𝑇2 = 30, and 𝐵 = 4. For the mean inter-arrival time 𝑀𝐼𝐴𝑇 select a value somewhere in the middle of the values you used in the previous experiment. Start with an initial 𝑀𝑆𝑇2 equal to 10 and slowly in-crease it each time you run the simulation. 𝑃and 𝑃c.õ²increase as

Page 133: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

119

the 𝑀𝑆𝑇2 gets longer. Again, keep an eye on these two values and stop when they start increasing rapidly. Vary 𝑀𝑆𝑇2suffi-ciently so that you can obtain a complete picture of how the pro-duction time changes as a function of 𝑀𝑆𝑇2. Graph your results of 𝑃and 𝑃c.õ²versus 𝑀𝑆𝑇2and discuss the observed trends. You can repeat this experiment for various values of the 𝑀𝐼𝐴𝑇 so that to get a family of graphs each corresponding to a different value of 𝑀𝐼𝐴𝑇.

Modify the simulation model so that the number of servers at each stage is an input variable and obtain similar graphs as above for different number of servers.

Note: Draw the confidence intervals for each simulated point as a vertical line. You may find that for some values the confidence in-tervals may be longer than others. For these cases, simply in-crease the number of batches.

Page 134: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished
Page 135: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

121

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 an example involving a factory operating at nearly full capacity. The management is considering various alternative con-figurations for expanding the system's capacity. The question that arises here is which of the alternatives is the best one. That is, it will achieve the expected performance at minimum cost. Now, since these alternative configurations do not exist, their performance can be evaluated by using simulation or other mathematical models. The standard method is to construct a simulation model of the existing system. Then, modify the model so that to analyze each alternative. The model of the existing system can be validated by comparing its output against actual data obtained from the existing system. Howev-er, there is no guarantee that when we modify the simulation to model an alternative configuration, that this new simulation model correctly represents the configuration under study!

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.

Page 136: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

122

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.

a. Check the pseudo-random number generator. A battery of sta-tistical tests can be carried out to make sure that the pseudo-random number generator used in the simulation program cre-ates numbers that are uniformly distributed in [0,1] and statis-tically independent.

b. Check the stochastic variate generators. Similar statistical tests can be carried out for each random variate generator used in a simulation model.

c. Check the logic of the simulation program. This is an arduous task. One way of going about it is to print out the status varia-bles, the event list, and other relevant data structures each time an event takes place, similar to what you did in the early stag-es of the development of your simulation. Then, check by hand whether the data structures are updated appropriately. This is a rather tedious task. However, using this method one can discover logical errors and also get a good feel about the simulation model.

d. Relationship validity. Verify that the assumptions you made when building the simulation model are the same as in the system under study.

e. Output validity. This is the most important validity checks. If actual data are available then this data can be compared with the output obtained from the simulation model. Otherwise, there are various ways that the validity of the output can be confirmed. A simple method is to consider special cases for which you can predict the behavior of the results. For in-stance, let us consider the machine interference model. As the mean repair time gets smaller, the mean down time of a ma-chine should also get smaller. This can be verified by running the simulation model for different mean repair times that get progressively smaller. An alternative situation can be created by assuming that the mean operational time is very close to zero. In this case, we should observe that all the machines are broken down queueing in the repairman’s queue. In general,

Page 137: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

123

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).

1. The machine interference problem. This problem can be also ana-lyzed using queueing theory. Let 1/𝜆 and 1/𝜇 be the mean opera-tional time of a machine and the mean repair time respectively. Also, let 𝑁 be the number of machines. Then, the mean down time 𝑊 of a machine (queueing and being repaired) is given by the expression:

𝑊 = 𝐿/𝜆(𝛮 − 𝐿)

where

𝐿 = #𝑁 −𝜆 + 𝜇𝜆 + 1$ (1 − 𝑝c)

and

𝑝c = 1/�𝑁!

(𝑁 − 𝑛)!

%

l¢c

Û𝜆𝜇Ý

l

.

Set 1/𝜇 = 10 and calculate 𝑊 by varying 1/𝜆 from 10 to 50 in increments of 5. (The idea is to get a good spread of values so that to observe the behavior of 𝑊 as a function of 1/𝜆. If needed de-crease or increase the range of values of 1/𝜆.)

For each value of 1/𝜆, obtain an estimate of the mean down time and confidence interval using your simulation model with the

Page 138: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

124

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.

a. Modify your code so that the token is always in queue 1 and it never visits the other queues. You can do this by setting 𝑇 to a very large number so that the time-out never occurs during the life of the simulation. Also, do not let the token change queues if queue 1 is empty. Then, queue 1 will be the only queue that will be served, while the other queues will grow in length for ever. Use the batch means method to estimate the mean wait-ing time 𝑊 and its confidence interval in queue 1. Assume a mean service time of 5 for all packets and vary the mean inter-arrival time from 20 to 8 in increments of 1. (The idea is to get a good spread of values, so you may have to de-crease/increase the range of values of the mean inter-arrival time so that to achieve this.) The only departures are from queue 1, and consequently your estimation procedure will au-tomatically give the mean waiting time of queue 1. Make sure that the data structures for the other queues do not become very large so that to cause memory problems. An easy way to do that, is to take no action when there is an arrival to the oth-er queues.

Under the conditions described above, 𝑊can also be obtained by analyzing queue 1 as an 𝑀/𝑀/1 queue using queueing theory. In this case we have 𝑊 = 1/(µ-l)where 1/𝜆 and 1/µ is the mean inter-arrival and mean service time respectively. Calculate 𝑊 for the same values as in the simulation and compare the results. The theoretical results should lie within the confidence intervals obtained from the simulation.

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

Page 139: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

125

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.)

Under the conditions described above, the entire token-based system behaves as an 𝑀/𝑀/1 queue and therefore 𝑊 =1/(µ-l�)where 1/µ is the mean service time and 1/l� is the mean inter-arrival time to all three queues, that is, it is equal to the mean inter-arrival time 1/𝜆 to a queue divided by 3, i.e., 1/l� = 1/3𝜆.Set 1/µ = 5 and calculate 𝑊 by varying the mean inter-arrival 1/𝜆 as above. The theoretical results should lie within the confidence interval obtained from the simula-tion.

3. The two-stage manufacturing system. As the buffer capacity of the second queue increases, the probability that server 1 will get blocked upon service completion due to queue 2 being full will decrease, and eventually it will become zero. At that moment, the two-stage manufacturing system becomes equivalent to two infi-nite capacity queues linked in series. Now, if we assume that the percent of time each machine is down is very small, i.e., the mean downtime divided by the sum of the mean operational time and the mean downtime is less than 2%, then the service time at each queue can be safely approximated by an exponential distribution. Therefore, each queue becomes an M/M/1 queue, and the mean waiting time in the two-stage manufacturing system is equal to the mean waiting time 𝑊\ = 1/(µ\-l) in the first queue plus the mean waiting time 𝑊f = 1/(µf-l), where 1/𝜆 is the mean inter-arrival time to the first queue and 1/µ\and 1/µf is the mean ser-vice time in the first and second queue respectively. Use the ex-pressions for 𝑊\ and 𝑊f to calculate the total mean waiting time in the manugacturing system 𝑊\ +𝑊f. Set 1/µ\ = 10and 1/µf =

Page 140: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

126

15 and vary 1/𝜆 from 100 to 20 in increments of 10. (The idea is to get a good picture of the trend in the graph, and so, if needed, decrease or increase the range of values of 1/𝜆.)

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.

Page 141: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

127

Chapter 7: Variance reduction techniques

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).

Variance reduction techniques require additional computation in or-der to be implemented. Furthermore, it is not possible to know in ad-vance whether a variance reduction technique will effectively reduce the variance in comparison with straightforward simulation. It is a good idea, therefore, to carry out pilot simulation runs in order to get a feel of the effectiveness of a variance reduction technique and of the additional computational cost required for its implementation.

VarianceReduction

NC S U

Page 142: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

128

In this Chapter, we examine two variance reduction techniques, namely, the antithetic variates technique and the control variates technique. We note that the material in this Chapter is not necessary for learning how to construct a simulation model, since in practice accuracy is achieved by simply running very long simulations. How-ever, it is worth studying it should you have the time and interest!

7.2. The antithetic variates technique This is a very simply technique to use and it only requires a few addi-tional lines of code in order to be implemented. However, there is no guarantee that it is effective. Also, it is not possible to know in ad-vance how much variance reduction can be achieved. Therefore, a small pilot study may be useful in order to decide whether or not to implement this technique.

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 =�𝑥\,R + 𝑥f,R 

2 , 𝑖 = 1,2, … , 𝑛.

As will be seen below, we use the random variable Z to construct the confidence interval of 𝐸(𝑋). We have

𝐸(𝑍) = 𝐸 É𝑋(1) + 𝑋(2)

2 Ê

=12 '𝐸(𝑋

(1)) + 𝐸�𝑋(2) ( = 𝐸(𝑋),

Page 143: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

129

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))].

Since 𝑉𝑎𝑟(𝑋(1)) = 𝑉𝑎𝑟(𝑋(2)) = 𝑉𝑎𝑟(𝑋), we have

𝑉𝑎𝑟(𝑍) = 12[𝑉𝑎𝑟(𝑋) + 𝐶𝑜𝑣(𝑋(1), 𝑋(2)].

The covariance of two random variables 𝑋 and 𝑌 can be written as

𝐶𝑜𝑣(𝑋, 𝑌) = 𝐶𝑜𝑟(𝑋, 𝑌)Ï𝑉𝑎𝑟(𝑋)𝑉𝑎𝑟(𝑌),

where 𝐶𝑜𝑟(𝑋, 𝑌)is the correlation of 𝑋 and 𝑌. Therefore,

𝑉𝑎𝑟(𝑍) = 12 �𝑉𝑎𝑟

(𝑋) + 𝜌𝑉𝑎𝑟(𝑋) 

=12𝑉𝑎𝑟

(𝑋)(1 + 𝜌),

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.

The antithetic variates technique attempts to introduce a negative cor-relation 𝜌 between the results of the two simulation runs. For in-stance, let us consider a simulation model of a single server M/M/1 queue, and let 𝑋 be an endogeneously created random variable that indicates the waiting time of a customer in the queue, i.e., the time from the moment it arrives to the moment it departs. We want to con-struct an interval estimate of 𝑋. For this, we run the simulation to col-

Page 144: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

130

lect a sample of 𝑛 observations. Then, we modify the simulation and run it again in order to collect a second sample which is negatively correlated to the first one. This is done using the following simple procedure.

We note that in order to simulate a customer 𝑖we need to genarate two pseudo-random numbers 𝑟R and 𝑣R. Using these two numbers we obtain the customer’s interarrival time 𝑡R = 𝐹i\(𝑟R)and service time 𝑠R = 𝐺i\(𝑣R), where 𝐹(𝑡) and G(s) are the cumulative distribution of the inter-arrival time and service time respectively. An indication of whether the queue is tending to increase or decrease can be obtained by examining the difference 𝑑R = 𝑡R − 𝑠R. This difference may be pos-itive or negative indicating that the queue is going through a slack or a busy period respectively. In the second run, we generate the pseudo-random numbers 𝑟R� and 𝑣R� for the 𝑖th simulated customer, so that 𝑑R� = 𝑡R� − 𝑠R�, where 𝑡R� = 𝐹i\(𝑟R�)) and 𝑠R� = 𝐺i\(𝑣R�), has the opposite sign of 𝑑R. That is, if the queue was going through a slack (busy) pe-riod in the first run at the time of the 𝑖th simulated customer, now it goes through a busy (slack) period. In order to achieve this negative correlation, we simply set 𝑟R� = 1 − 𝑟R and 𝑣R� = 1 − 𝑣R. An easy way to implement this is to store 𝑟R and 𝑣R for each customer and then use them in the second run for the same customer. A simpler way is to generate both pseudo-random numbers 𝑟R and 𝑣R when we schedule the arrival of a new customer 𝑖.In this way, we guarantee that the same pair of pseudo-random numbers are associated with the same customer.

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, … , 𝑧lwhere 𝑧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 𝑋.

Page 145: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

131

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

Table 7.1: Straight simulation of an M/M/1 queue

Figure 7.1: The antithetic variates technique applied to an M/M/1 queue

The antithetic variates technique, was implemented in a simulation of an M/M/1 queue. The random variable 𝑋 is the time a customer spends in the system. In both simulation runs the observations of 𝑋 were obtained by sampling every 10th customer, in order to avoid

15.0

25.0

35.0

45.0

55.0

5.0

5 10 15 20 25 301

Simulated Number of Customers

Wai

ting T

ime

Per

Cust

om

er

Original SampleAntithetic VariatesMean of Two Samples

15.0

25.0

35.0

45.0

55.0

5.0

5 10 15 20 25 301

Simulated Number of Customers

Wai

ting T

ime

Per

Cust

om

er

Original SampleAntithetic VariatesMean of Two Samples

First runSecond runZ values

Page 146: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

132

generating correlated observations. The results given in table 7.1 were obtained using straight simulation. Using the antithetic variates tech-nique, we obtained a confidence interval of 13.52 ± 1.76 using two sets of 300 observations, i.e., a total of 600 observations. From table 7.1, we see that a similar confidence interval width of 1.84was ob-tained using a sample size of 𝑛 = 1800. Figure 7.2 shows a sample of the values from the first run (straight simulation), the second run (where we implemented the antithetic variates technique) and the computed 𝑍 values. We observe that the samples from the first and second run are nicely negatively correlated, and the 𝑍 values are all close to the mean, indicating that their variance is small.

Figure 7.2: A comparison between straight simulation and the antithetic vari-ates techniques for an M/M/2 queue

In the above example, the antithetic variates technique worked quite well. However, this should not be construed that this method always works well. We applied the same technique to an M/M/2 queue, i.e., an M/M/1 queue but with two servers, and as can be seen in figure 7.2 there is little benefit to be gained from using the antithetic variates technique. The sample size on the axis is the total number of custom-ers simulated in the straight simulation, and it is the total number of customers simulated in both runs when implementing the antitheic variates technique.

0.4

0.8

1.2

1.6

$

#$

800 1600 2400 3200

$ $$

$ $# ##

Sample Size

Sta

ndar

d E

rror

$ Straight Simulation# Standard Antithetic Variate Technique

0.4

0.8

1.2

1.6

$

#$

800 1600 2400 3200

$ $$

$ $# ##

Sample Size

Sta

ndar

d E

rror

$ Straight Simulation# Standard Antithetic Variate Technique

!"

Page 147: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

133

7.3. The control variates technique This method is also known as the method of concomitant information. Let 𝑋 be an endogenously created random variable whose mean and confidence interval we wish to estimate. Let 𝑌 be another endoge-nously created random variable whose mean is known in advance and it is correlated with 𝑋. This random variable is referred to as the con-trol variable. We have the following two cases.

a. X and Y are negatively correlated

Define a new random variable 𝑍 = 𝑋 + 𝑌 − 𝐸(𝑌). We have

𝐸(𝑍) = 𝐸(𝑋 + 𝑌 − 𝐸(𝑌)) = 𝐸(𝑋),

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + 𝑉𝑎𝑟(𝑌) + 2𝐶𝑜𝑣(𝑋, 𝑌).

Since 𝑋 and 𝑌 are negatively correlated, we have that 𝐶𝑜𝑣(𝑋, 𝑌) < 0. Therefore, if 𝑉𝑎𝑟(𝑌) − 2|𝐶𝑜𝑣(𝑋, 𝑌)| < 0 then, a reduction in the variance of Z has been achieved.

b. X and Y are positively correlated

Define 𝑍 = 𝑋 − 𝑌 + 𝐸(𝑌). Then

𝐸(𝑍) = 𝐸(𝑋 − 𝑌 + 𝐸(𝑌)) = 𝐸(𝑋)

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) + 𝑉𝑎𝑟(𝑌) − 2𝐶𝑜𝑣(𝑋, 𝑌).

Therefore, if 𝑉𝑎𝑟(𝑌) − 2|𝐶𝑜𝑣(𝑋, 𝑌)| < 0 then a reduction in the variance of Z has been achieved.

As an example, let us consider a simulation of an M/M/1 queue, and let 𝑋 and 𝑌 indicate the waiting time in the queue and the interarrival time respectively. If 𝑌 is very small, then customers arrive faster and, therefore, the queue size gets larger, which means that customers have to wait longer, i.e. 𝑋 is large. On the other hand, if 𝑌 is large, then customers arrive slower and, hence, the queue size gets smaller. Obviously, the smaller the queue size, the less a customer has to wait

Page 148: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

134

in the queue, i.e., 𝑋 is small. Therefore, we see that X and Y can be negatively correlated.

Let 𝑥\, 𝑥f, … , 𝑥l be 𝑛 observations of 𝑋, and let 𝑦\, 𝑦f, … , 𝑦 be 𝑛 ob-servations of 𝑌, where 𝑦R is the inter-arrival time associated with the 𝑥Robservation. Let

𝑧R = 𝑥R+ 𝑦R − 𝐸(𝑌), 𝑖 = 1,2, … , 𝑛..

Then, the confidence interval of 𝐸(𝑋) is given by the expression

𝑧̅ ± 1.96𝑠*√𝑛

where

𝑧̅ =1𝑛�𝑧R

l

R¢\

and

𝑠* =1𝑛 − 1�

(𝑧R − 𝑧̅)fl

R¢\

.

More generally, the random variable 𝑍 can be be defined as

𝑍 = 𝑋 − 𝑎(𝑌 − 𝐸(𝑌)),

where 𝑎 is a constant to be estimated and 𝑌 is positively or negatively correlated to 𝑋. Again, we have that 𝐸(𝑍) = 𝐸(𝑋), and

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) +𝑎f𝑉𝑎𝑟(𝑌) − 2𝑎𝐶𝑜𝑣(𝑋, 𝑌).

𝑍 has a smaller variance than 𝑋 if

𝑎f𝑉𝑎𝑟(𝑌) − 2𝑎𝐶𝑜𝑣(𝑋, 𝑌) < 0.

Page 149: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

135

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

𝑎 = 𝐶𝑜𝑣(𝑋, 𝑌)𝑉𝑎𝑟(𝑌) .

Now, substituting into the expression for 𝑉𝑎𝑟(𝑍) we have

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋) +É𝐶𝑜𝑣(𝑋, 𝑌)𝑉𝑎𝑟(𝑌) Ê

f

𝑉𝑎𝑟(𝑌)

− 2𝐶𝑜𝑣(𝑋, 𝑌)𝑉𝑎𝑟(𝑌) 𝐶𝑜𝑣(𝑋, 𝑌)

= 𝑉𝑎𝑟(𝑋) −(𝐶𝑜𝑣(𝑋, 𝑌))f

𝑉𝑎𝑟(𝑌)

Since

𝜌Ôð =𝐶𝑜𝑣(𝑋, 𝑌)

Ï𝑉𝑎𝑟(𝑋)Ï𝑉𝑎𝑟(𝑌),

We have

𝑉𝑎𝑟(𝑍) = 𝑉𝑎𝑟(𝑋)(1 − 𝜌Ôðf ).

Thus, we always get a reduction in the variance of 𝑍 for the optimal value of 𝑎, provided that 𝑋 and 𝑌 are correlated. The determination of 𝑎 requires a priori knowledge of the 𝑉𝑎𝑟(𝑌) and 𝐶𝑜𝑣(𝑋, 𝑌). Sample estimates can be used in order to approximately obtain 𝑎.

The definition of Z can be further generalized using 𝑚 control varia-bles as follows:

Page 150: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

136

𝑍 = 𝑋 −�𝑎R(𝑌R − 𝐸(𝑌R),|

R¢\

where 𝑎R, 𝑖 = 1,2, … ,𝑚, are any real numbers.

Computer assignments Consider the machine interference problem. Carry out the following tasks:

1. Implement the antithetic variance reduction technique. 2. Implement the control variates technique. 3. Compare the two variance reduction techniques against straight

simulation for various sample sizes.

Page 151: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

137

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:

• A simulation of a CPU priority scheduler • A simulation of the flow of SIP messages • A simulation of a multiplexer

Simulation models as well as all stochastic models are either continu-ous-time or discrete-time models. That is, the underlying time is ei-ther a continuous or a discrete variable. The three simulation projects used in the previous Chapters are continuous-time models. In this Chapter, the first two projects are continuous-time models and the third one is a discrete-time model. As we have seen, only one event can take place at any time in a continuous-time model. However, in a discrete-time model it is possible that multiple events may occur at the same time. Also, in a discrete-time simulation, the unit-time ad-vance design is an obvious choice.

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.

8.1. A simulation of a CPU priority scheduler We consider a simple Internet of Things (IoT) system that consists of a large number of sensors and actuators, and a server. The sensors

Page 152: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

138

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.

Figure 8.1: The CPU priority queueing system

The server maintains two queues, an RT queue and a nonRT queue, as shown in figure 8.1. The server processes the messages using the pre-emptive resume priority policy that guarantees that RT messages always have a higher priority than nonRT messages. This policy is commonly used for scheduling processes for execution on a CPU. This scheduling policy works as follows:

• Each time the server completes a service, i.e., processing a message, it checks the RT queue to see if there are any mes-sages waiting. If there is a message waiting, then it starts pro-cessing it.

• If the RT queue is empty, the server checks the nonRT queue. If there is a message waiting, it starts processing it. If there are no messages waiting, the server becomes idle.

• If during the time the server is processing a nonRT message, an RT message arrives, the server interrupts the processing of the nonRT message and starts processing the RT message. (This is why the scheduling policy is called preemptive.) Up-

Server

RT Queue

nonRT Queue

Arrivals of RT messages

Arrivals of nonRTmessages

Page 153: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

139

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.)

In this simulation project, we are only concerned with the perfor-mance of the CPU scheduling policy. In particular, you will develop an event-based simulation model of the two-queueing system at the server, with a view to calculating the 95th percentile of the response time of RT and nonRT messages. The response time is defined as the time elapsing from the time a message (RT or nonRT) joins its queue to the moment it is fully processed by the server and departs from the server. For pedagogical purposes, the simulation project is broken into the following three tasks:

1. Task 1 - Hand simulation: The objective is to make sure that you understand the events, the event clocks, and how the sim-ulation advances from one event to another.

2. Task 2 - Code basic simulation: The objective of this task is to code the simulation logic and to make sure that the program works correctly by carefully checking the output.

3. Task 3 - Statistical estimation: The objective here is to embel-lish your simulation with additional code so that to estimate the mean and 95th percentile of the response time of the RT and nonRT messages.

8.1.1. The simulation logic The state of the simulation model can be described by the following parameters:

• Number of RT messages in the RT queue, 𝑛¶+ • Number of nonRT messages in the nonRT queue, 𝑛l�l¶+ • State of the server 𝑠: idle (𝑠 = 0), serving an RT message

(𝑠 = 1), serving a nonRT message (𝑠 = 2).

Page 154: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

140

There are other parameters too, but they are not critical to the devel-opment of the simulation model. The simulation is based on tracking the events that change the state of the system through time. These events are:

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.

2. A nonRT arrival occurs

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-

Page 155: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

141

fore leaving this part of the logic, generate the next arrival time of an nonRT message.

3. A service completion occurs

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.

We are now ready to summarize the logic of the simulation program.

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.

Page 156: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

142

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 tasks2.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.

2. Arrival of a nonRT message

• 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.

Page 157: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

143

• 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.

8.1.2. Task 1: Hand simulation We start with the hand simulation shown in table 8.1. For this, the following simplifying assumptions have been made:

• The inter-arrival time of RT messages is constant equal to 10 • The inter-arrival time of nonR is constant equal to 5 • The service time of an RT message is constant equal to 2 • The service time of a nonRT message is constant equal to 4.

The following initial conditions at 𝑀𝐶 = 0are used:𝑅𝑇𝐶𝐿 = 3, 𝑛𝑜𝑛𝑅𝑇𝐶𝐿 = 5, 𝑛¶+ = 0, 𝑛l�l¶+ = 0, 𝑠 = 2, 𝑆𝐶𝐿 = 4.

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.

Page 158: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

144

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.

𝑴𝑪 𝑹𝑻𝑪𝑳 𝒏𝒐𝒏𝑹𝑻𝑪𝑳 𝒏𝑹𝑻 𝒏𝒏𝒐𝒏𝑹𝑻 𝑺𝑪𝑳 𝒔 RST0 3 5 0 0 4 2

3 13 5 0 1 5 1 1 5 13 10 0 2 5 1 1 5 13 10 0 1 6 2 6 13 10 0 0 10 2 10 13 15 0 1 10 2 10 13 15 0 0 14 2 13 23 15 0 1 15 1 1

Table 8.1: Hand simulation for task 1.1

𝑴𝑪 𝑹𝑻𝑪𝑳 𝒏𝒐𝒏𝑹𝑻𝑪𝑳 𝒏𝑹𝑻 𝒏𝒏𝒐𝒏𝑹𝑻 𝑺𝑪𝑳 𝒔 𝑹𝑺𝑻0 3 5 0 0 4 2

3 8 5 0 1 7 1 s=1 5 8 15 0 2 7 1 s=1 7 8 15 0 1 8 2 8 13 15 0 1 12 1

12 13 15 0 0 14 2

Table 8.2: Hand simulation for task 1.2

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

Page 159: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

145

correct solution given in section 8.1.5 to make sure that you have not made any errors.

8.1.3. Task 2: Write the basic simulation code In this task, you will write the simulation code to reproduce the hand simulation. Also, you will introduce randomness in your simulation by assuming that the inter-arrival and service times are exponentially distributed.

Task 2.1: Use a programming language of your choice to code your hand simulation and reproduce the tables in tasks 1.1 and 1.2. Run your code with the same input values as in task 1.1 and print out the same table you produced in task 1.1. Check the output by hand to verify that it is the same as in task 1.1. If it is not the same, go through your code to identify the errors. Repeat the same for the input values used in task 1.2.

Task 2.2: Introduce randomness in your simulation Use the program you developed in task 2.1, with the same initial con-ditions, but assume that the inter-arrival times and service times are exponentially distributed. Make sure you define the clocks as real variables.

Let 𝑀𝐼𝐴𝑇¶+,𝑀𝐼𝐴𝑇l�l¶+,𝑀𝑆𝑇¶+ and 𝑀𝑆𝑇l�l¶+ be the mean inter-arrival time of RT messages, the mean inter-arrival time of nonRT messages, the mean service time of an RT message, and the mean service time of a nonRT message. The mean values are the same as the input values used in task 1.1, i.e., 𝑀𝐼𝐴𝑇¶+ = 10, 𝑀𝐼𝐴𝑇l�l¶+ = 5, 𝑀𝑆𝑇¶+ = 2, and 𝑀𝑆𝑇l�l¶+ = 4. Each time you want to generate an inter-arrival time or a service time, draw a pseudo-random number 𝑟 and then obtain the exponential variate using the expression: −(𝑚𝑒𝑎𝑛)𝑙𝑜𝑔Ø(𝑟).

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.

Page 160: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

146

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.

For this, you have to modify your program as follows:

• Stop printing a line of output each time you handle an event as in tasks 1 and 2. In other words, do not print the hand simula-tion table.

• Add a data structure to collect data on the response time. o For this, you will have to implement a queue structure for

the RT and another one for the nonRT queues. Arrivals are added to the end of the queue, and messages are served from the top. Each data element represents a different message and it should contain the time of arrival.

o When an RT message completes its service and departs from the server, calculate the elapsed time for the message by subtracting the current time from the time of its arrival. Save it in the array𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒.

o Same as above for nonRT messages. Save the elapsed times in the array 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒.

o After you simulate the required number of messages, use the two arrays 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 to calculate the required statistical estimates. If space is a problem, you can use the 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 arrays to store the response times from one batch, and calculate the batch mean and batch 95th percentile after you have simu-lated the total number in the batch. Save this information in another array and reuse the two arrays for the next batch.

• Implement the batch means method to construct the confi-dence intervals.

Page 161: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

147

• Calculate the mean, 95th percentile and their confidence in-terval of 𝑅¶+ and 𝑅l�l¶+ .

The length of the simulation is controlled by m, the number of batch-es and the number of observations b within a batch. Set m = 51, and b = 1,000. (A large batch size is required in order to estimate the per-centile accurately.) In order to allow for the initial conditions, ignore the results from the first batch, and calculate all your performance metrics using the remaining 50 batches. Run your simulation until you have mb observations collected in the arrays 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒. Then apply the batch means method to the 𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 and 𝑛𝑜𝑛𝑅𝑇𝑠𝑎𝑚𝑝𝑙𝑒 arrays separately. It is possible that one array may have more than mb observations. In this case use only the first mb observations.

Set 𝑀𝐼𝐴𝑇¶+ = 7, 𝑀𝑆𝑇¶+ = 2,𝑀𝑆𝑇l�l¶+ = 4, and run your simula-tion for different values of 𝑀𝐼𝐴𝑇l�l¶+ ranging from 10 to 40 in in-crements of 5. For each value of 𝑀𝐼𝐴𝑇l�l¶+ obtain the mean, 95th percentile, and confidence intervals of 𝑅¶+ and 𝑅l�l¶+. Graph all your results on the mean response times of 𝑅¶+ and 𝑅l�l¶+ including the confidence intervals (drawn as vertical bars), as a function of 𝑀𝐼𝐴𝑇l�l¶+. Likewise, graph all your results for the 95th percentile. Comment on all your results.

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.

8.1.5. Correct simulation results Tables 8.3 and 8.4 give the complete hand simulation for tasks 1.1 and 1.2 respectively, and figures 8.2 and 8.3 give the plots for tasks 3. Use these results to verify that your simulation model is correct. Re-member, however, that in practice you will not have such results available and you will have to rely on the validation checks described in Chapter 6.

Page 162: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

148

Table 8.3: Correct hand simulation for task 1.1

Table 8.4: Correct hand simulation for task 1.2

MC RTCL nonRTCL n_RT n_nonRT SCL Status (s) Pre-empted time0 3 5 0 0 4 2 03 13 5 0 1 5 1 15 13 10 0 2 5 1 15 13 10 0 1 6 2 06 13 10 0 0 10 2 0

10 13 15 0 1 10 2 010 13 15 0 0 14 2 013 23 15 0 1 15 1 115 23 20 0 2 15 1 115 23 20 0 1 16 2 016 23 20 0 0 20 2 020 23 25 0 1 20 2 020 23 25 0 0 24 2 023 33 25 0 1 25 1 125 33 30 0 2 25 1 125 33 30 0 1 26 2 026 33 30 0 0 30 2 030 33 35 0 1 30 2 030 33 35 0 0 34 2 033 43 35 0 1 35 1 135 43 40 0 2 35 1 135 43 40 0 1 36 2 036 43 40 0 0 40 2 040 43 45 0 1 40 2 040 43 45 0 0 44 2 043 53 45 0 1 45 1 145 53 50 0 2 45 1 145 53 50 0 1 46 2 046 53 50 0 0 50 2 050 53 55 0 1 50 2 050 53 55 0 0 54 2 0

MC RTCL nonRTCL num_RT num_nonRTSCL Status Pre-empted time0 3 5 0 0 4 2 03 8 5 0 1 7 1 15 8 15 0 2 7 1 17 8 15 0 1 8 2 08 13 15 0 1 12 1 0

12 13 15 0 0 14 2 013 18 15 0 1 17 1 115 18 25 0 2 17 1 117 18 25 0 1 18 2 018 23 25 0 1 22 1 022 23 25 0 0 24 2 0

Task 1.2

Task 1.1

MC RTCL nonRTCL n_RT n_nonRT SCL Status (s) Pre-empted time0 3 5 0 0 4 2 03 13 5 0 1 5 1 15 13 10 0 2 5 1 15 13 10 0 1 6 2 06 13 10 0 0 10 2 0

10 13 15 0 1 10 2 010 13 15 0 0 14 2 013 23 15 0 1 15 1 115 23 20 0 2 15 1 115 23 20 0 1 16 2 016 23 20 0 0 20 2 020 23 25 0 1 20 2 020 23 25 0 0 24 2 023 33 25 0 1 25 1 125 33 30 0 2 25 1 125 33 30 0 1 26 2 026 33 30 0 0 30 2 030 33 35 0 1 30 2 030 33 35 0 0 34 2 033 43 35 0 1 35 1 135 43 40 0 2 35 1 135 43 40 0 1 36 2 036 43 40 0 0 40 2 040 43 45 0 1 40 2 040 43 45 0 0 44 2 043 53 45 0 1 45 1 145 53 50 0 2 45 1 145 53 50 0 1 46 2 046 53 50 0 0 50 2 050 53 55 0 1 50 2 050 53 55 0 0 54 2 0

MC RTCL nonRTCL num_RT num_nonRTSCL Status Pre-empted time0 3 5 0 0 4 2 03 8 5 0 1 7 1 15 8 15 0 2 7 1 17 8 15 0 1 8 2 08 13 15 0 1 12 1 0

12 13 15 0 0 14 2 013 18 15 0 1 17 1 115 18 25 0 2 17 1 117 18 25 0 1 18 2 018 23 25 0 1 22 1 022 23 25 0 0 24 2 0

Task 1.2

Task 1.1

Page 163: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

149

Figure 8.2: Mean and confidence intervals for RT and nonRT messages

Figure 8.3: 𝟗𝟓th percentile and confidence intervals

for RT and nonRT messages

!"#$%&%'(

Graphs:

The mean for RT remains almost constant whereas for Non-RT is decreasing. This is because in real-time jobs, the interarrival time does not impact the response time due to the fact that once a new job arrives, it immediately starts processing (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. 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 time for non real-time jobs. A similar behaviour is observed for the 95th percentile but the only difference is that the numbers are larger in value and the confidence interval is correspondingly bigger as compared to the mean confidence interval.

!"#$%&%'(

Page 164: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

150

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.

Figure 8.4: The queueing system under study

!

AU

Page 165: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

151

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 objective of this simulation is to estimate the end-to-end delay, also known as the response time. This is the amount of time elapsed from the moment a message arrives at the queueing network to the moment it departs from it.

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.

8.2.1. The simulation logic The basic events that change the state of the system are:

1. Arrival of a new customer at the P-CSCF queue 2. Completion of a service time at the P-CSCF queue 3. Completion of a service time at the S-CSCF queue

Page 166: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

152

4. Completion of a service time at the AS queue. 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.

1. Arrival of a new customer to the P-CSCF queue:

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.

2. Service completion at the P-CSCF

If the departing customer is a new customer, then it joins the S-CSCF queue. If it is a recirculated customer that has come back from the S-CSCF, then it leaves the system. If it moves to the S-CSCF queue, then we follow the same logic as above. That is, if there is no one in the queue and the S-CSCF server

Page 167: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

153

is idle, then the customer goes into service and we generate a new service time and update the service completion clock 𝑆𝐶𝑆. If the serv-er is busy, then the customer joins the queue and no further action is taken. In addition, if there are customers waiting in the P-CSCF queue, then the next customer will start its service and for this we generate a new service time and update the service completion clock 𝑆𝐶𝑃.

3. Service completion at the S-CSCF

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.

4. Service completion at the AS

The departing customer joins the S-CSCF queue. If it finds the S-CSCF server idle, i.e., it is not busy and no one is waiting in the queue, then it starts a new service and the service completion clock 𝑆𝐶𝑆 is updated. Otherwise, it just joins the queue. In addition, if there are customers waiting in the AS queue, then the next in line starts service and the service completion clock 𝑆𝐶𝐴 is updated.

8.2.2. Implementation and results The easiest way to implement the event list is to use a linear sequen-tial array as described in section 4.4. Also, each queue should be im-plemented as a linked list, see section 4.5, where each data element represents a customer and contains the time it arrived at the P-CSCF queue for the first time as a new customer and whether it is a new customer or a recirculated one. When the customer departs from the queueing network, calculate the total time it spent in the system and save this information is a separate array.

Page 168: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

154

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:

• 𝐼𝐴𝑇 = 1: Confidence interval for the mean end-to-end delay: (1.89946, 1.94703). Confidence interval for the 95th per-centile end-to-end delay: (4.04669, 4.24222).

• 𝐼𝐴𝑇 = 2: Confidence interval for the mean end-to-end delay: (1.38601, 1.40044).Confidence interval for the 95th per-centiles end-to-end delay: (2.85748, 2.89641).

Note that your results maybe slightly different due to the randomness in the simulation.

Page 169: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

155

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.

Another feature of this particular multiplexer is that all the packets have the same length. That is, they all consist of the same number of bytes. This is not the case with the packets in the Internet currently which have a variable length. However, back in the 90s, a novel net-working architecture was developed, known as ATM (not related to ATM machines!), which revolutionized the way computer networks worked. This network is not in use anymore, but many of its scheme are used in the Internet so that voice, video, and data can be all car-ried simultaneously. Our interest in simulating this multiplexer is that it is a good example of a discrete-time simulation, as opposed to all the other simulation models in this book which are in continuous time.

Figure 8.5: The multiplexer with four arrival streams

Stream 1

Stream 2

Stream 3

Stream 4 B

8 7 6 5 4 3 2 1slots

Page 170: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

156

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.

In order to construct the simulation model, we also need to describe how the packets of a single stream arrive at the buffer. In the continu-ous-time simulation models we have assumed that the inter-arrival time of customers is exponentially distributed. The equivalent of the exponential distribution in discrete time is the geometric distribution, see section 3.4.2. That is, for a given stream, let 𝑝 and 1 − 𝑝 be the probability that a time slot contains a packet or it is empty. Then, the probability that the interarrival time between two successive packets is 𝑖slots is: (1 − 𝑝)R𝑝. In the simulation model, we do not have to generate geometric variates, as there is a simpler way to determine whether a packet from a given stream has arrived or not. That is, for each slot we draw a pseudo-random number 𝑟, and if 𝑟 ≤ 𝑝, then the slot contains a packet. Otherwise, it is empty.

8.3.1. Simulation logic The simulation model under the assumptions described above is ex-tremely simple. We use the unit-time design, where the unit is one slot. That is, we advance the master clock one slot each time, and take care of the events that occur at the end of the slot. Also, as will be seen below, we do not need to keep an event list.

Page 171: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

157

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𝑚�𝑝ö���,R

|

R¢\

and𝑠f =1

𝑚 − 1��𝑝ö���,R − �̅� f.

|

R¢\

Run your simulation for different values of 𝐵 and 𝑝 in order to see how the packet loss probability varies as a function of these two pa-rameters. Specifically, estimate the packet loss for 𝑝, 0 ≤ 𝑝 ≤ 1, for

Page 172: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

158

𝐵 = 1. Then repeat this process for 𝐵 = 2, and so on. Stop when you see a small difference between the results for two different values of 𝐵. Plot all your results on the same graph, a different plot for each value of 𝐵, and discuss your conclusions.

Figure 8.6: The multiplexer with priorities

8.3.3. An extension of the simulation model In this section, we describe a slightly more complicated version of the above model. So far, we have assumed that all packets in the buffer have the same priority. However, this is not the case as typically dif-ferent packets are given different priorities for transmission out of the buffer. In figure 8.6, we see that the buffer consists of three queues: one for packets carrying voice, a separate one for packets carrying video, and a third one for packets carrying data. The priority scheme is the same as in the simulation project described in section 8.1, but without pre-emption. The voice queue has priority over the video queue, which has priority over the data queue. Each time the server completes a service, i.e., it completes a transmission of a packet, it checks the voice queue, and if it is not empty it starts serving the packet at the top of the queue. Otherwise, it checks the video queue. If it is not empty, it starts serving the packet at the top of the queue. Otherwise, it checks the data queue and if it is not empty, it starts serving the packet at the top of the queue. Otherwise, and the server become idle. This procedure is repeated each time a packet completes its service.

As before, we assume four arrival streams, and a slotted time struc-ture. It takes one slot for a packet to arrive and one slot for a packet to

Stream 1

Stream 2

Stream 3

Stream 4

B8 7 6 5 4 3 2 1

slots

voice

video

data

Page 173: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

159

be served, i.e., transmitted out of the buffer. Each incoming stream consists of 20% voice packets, 20% video pckets, and 60% data packets. That is, a slot in a stream contains a packet with probability 𝑝, and this packet is a voice, video, or data packet with probability 0.20, 0.20, and 0.60 respectively.

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

Page 174: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Simulation Techniques Harry Perros Sim S

160

implement the priority scheme described above. The main logic of the model is summarized below.

At the end of a time slot, do the following:

• 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. The performance question of interest regarding this multiplexer, and any other multiplexer for that matter, is to determine the size of 𝐵 and lower and upper bounds for each queue so that the packet loss per queue is less than a predefined number for a given a traffic load. The

Page 175: Computer Simulation Techniques: The de nitive introduction! · 2021. 2. 15. · xiii Author HARRY G. PERROS is an Emeritus Professor of Computer Science and an Alumni Distinguished

Harry Perros Simulation Techniques Sim S

161

traffic load is expressed in terms of the probability 𝑝, 0 < 𝑝 ≤ 1. The higher its value, the more packets arrive. Also, the predefined packet loss varies from one queue to another. Typically, we want a very low packet loss for the voice and video queues and a slightly higher one for the data queue. In real life, the packet loss should be about 1% for the voice and video packets and about 5% for the data packets. In or-der to simplify the simulation experiments, we will assume 10%, 10% and 20% packet loss for the voice, video, and data packets respective-ly. So, the question you need to address using this simulation model is as follows. Determine the values for 𝐵, 𝐿\, 𝑈\, 𝐿f, 𝑈f, 𝐿e, 𝑈e so that the packet loss satisfies the above requirements for a given value of 𝑝. The values of the above parameters can be determined by running a grid search. That is, we create a set of different values for each of the unknown parameters, and then run a simulation for each combination of the values of the parameters. The best combination or combina-tions that satisfy the desired packet loss can then be selected. This approach will obviously result in a huge number of simulations. In order to reduce the number of simulations, we can create a range of values for each parameter using large increments, which will result to a coarse grid of different combinations of values of these parameters. Then, we run a simulation for each of these combinations with a view to locating an area of the grid, i.e., a set of combinations, where the best value(s) may lie. Subsequently, we carry out a refined grid search within that area using small increments.

For this problem, we will further simplify the experimentation by as-suming that 𝐿\ = 𝐿f = 𝐿e = 0.20𝐵,and 𝑈\ = 𝑈f = 𝑈e = 0.40𝐵. So, now the search is reduced to simply determine the value of 𝐵 for which the packet loss constraints are satisfied. Repeat this experiment for different values of 𝑝, and plot the best value of 𝐵 versus 𝑝. Dis-cuss your results.