<<

Clients

CPU

Computer Techniques: The defnitive introduction!

1

2

2 3

3 7

b 4 5 6 9 6 9

Yes Is CPU No MCL = tarr queue MCL = MCL + 1 empty?

A new arrival A departure event occurs event occurs

b Is MCL = tarr b ?

Yes

A new arrival event occurs

b

i Harry Perros Simulation Techniques Harry Perros Sim S

ii Harry Perros Simulation Techniques Sim S

Computer Simulation Techniques: The definitive introduction!

Harry Perros

iii Simulation Techniques Harry Perros Sim S

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.

iv Harry Perros Simulation Techniques Sim S

To Isabelle

v

Table of Contents

FORWARD ...... XI AUTHOR ...... XIII CHAPTER 1: INTRODUCTION ...... 1 1.1. INTRODUCTION ...... 1 1.2. BUILDING A SIMULATION MODEL ...... 2 1.4. A TOKEN-BASED ACCESS SCHEME ...... 9 1.5. A TWO-STAGE SYSTEM ...... 15 PROBLEMS ...... 21 COMPUTER PROJECTS ...... 22 CHAPTER 2: GENERATION OF PSEUDO-RANDOM NUMBERS ...... 25

2.1. INTRODUCTION ...... 25 2.2. PSEUDO-RANDOM NUMBERS ...... 26 2.3. THE CONGRUENTIAL METHOD ...... 28 2.3.1. GENERAL CONGRUENTIAL METHODS ...... 30 2.3.2. COMPOSITE GENERATORS ...... 30 2.4. TAUSWORTHE GENERATORS ...... 31 2.5. THE LAGGED FIBONACCI GENERATORS ...... 31 2.6. STATISTICAL TESTS FOR PSEUDO-RANDOM NUMBER GENERATORS ...... 33 2.6.1. HYPOTHESIS TESTING ...... 33 2.6.2. FREQUENCY TEST (MONOBIT TEST) ...... 40 2.6.3 SERIAL TEST ...... 41 2.6.4. RUNS TEST ...... 44 2.6.5. CHI-SQUARED TEST FOR GOODNESS OF FIT ...... 44 PROBLEMS ...... 45 COMPUTER PROJECTS ...... 45 CHAPTER 3: GENERATING VARIATES ...... 47

3.1. INTRODUCTION ...... 47 3.2. THE INVERSE TRANSFORMATION METHOD ...... 47 3.3. SAMPLING FROM CONTINUOUS- PROBABILITY DISTRIBUTIONS ...... 50 3.3.1. SAMPLING FROM A UNIFORM DISTRIBUTION ...... 50 3.3.2. SAMPLING FROM AN EXPONENTIAL DISTRIBUTION ...... 51 3.3.3. SAMPLING FROM AN ERLANG DISTRIBUTION ...... 52 3.3.4. SAMPLING FROM A NORMAL DISTRIBUTION ...... 53 3.4. SAMPLING FROM DISCRETE-TIME PROBABILITY DISTRIBUTIONS ...... 55 3.4.1. GENERATING BINOMIAL DISTRIBUTED STOCHASTIC VARIATES ...... 55 3.4.2. GENERATING GEOMETRICALLY DISTRIBUTED STOCHASTIC VARIATES ...... 55 3.4.3. GENERATING POISSON DISTRIBUTED STOCHASTIC VARIATES ...... 56 3.5. GENERATING STOCHASTIC VARIATES FROM AN EMPIRICAL DISTRIBUTION ...... 58 3.6. THE REJECTION METHOD ...... 60 vii Simulation Techniques Harry Perros Sim S COMPUTER PROJECTS ...... 61 CHAPTER 4: SIMULATION DESIGNS ...... 63 4.1. INTRODUCTION ...... 63 4.2. EVENT-ADVANCE DESIGN ...... 63 4.3. FUTURE EVENT LIST ...... 64 4.4. EVENT LIST STORED IN A SEQUENTIAL ARRAY ...... 65 4.5. EVENT LIST STORED IN A LINKED LIST ...... 66 4.5.1. DEFINING A LINKED LIST ...... 68 4.5.2. CREATION OF A NEW NODE ...... 69 4.5.3. DELETION OF A NODE ...... 70 4.5.4. INSERTING A NODE IN A LINKED LIST ...... 70 4.5.5. REMOVING THE FIRST NODE OF A LINKED LIST ...... 73 4.5.6. TIME ...... 74 4.5.7. DOUBY LINKED LISTS ...... 75 4.6. UNIT-TIME ADVANCE DESIGN ...... 75 4.6.1. SELECTING A UNIT TIME ...... 79 4.6.2. IMPLEMENTATION ...... 80 4.6.3. EVENT-ADVANCE VS. UNIT-TIME ADVANCE ...... 80 4.7. ACTIVITY-BASED SIMULATION DESIGN ...... 80 4.8. EXAMPLES ...... 83 4.8.1. AN INVENTORY SYSTEM ...... 83 4.8.2. A ROUND-ROBIN QUEUE ...... 85 PROBLEMS ...... 90 COMPUTER PROJECTS ...... 91 CHAPTER 5: ESTIMATION TECHNIQUES FOR ANALYZING ENDOGENOUSLY CREATED DATA ...... 93

5.1. INTRODUCTION ...... 93 5.2. COLLECTING ENDOGENOUSLY CREATED DATA ...... 93 5.3. TRANSIENT STATE VS STEADY-STATE SIMULATION ...... 96 5.3.1. TRANSIENT-STATE SIMULATION ...... 96 5.3.2. STEADY-STATE SIMULATION ...... 96 5.4. ESTIMATION TECHNIQUES FOR STEADY-STATE SIMULATION ...... 97 5.5. ESTIMATION OF THE CONFIDENCE INTERVAL OF THE MEAN ...... 98 5.5.1. ESTIMATION OF THE AUTOCORRELATION FUNCTION (ACF) ...... 103 5.5.2. THE BATCH MEANS METHOD ...... 106 5.5.3. THE REPLICATION METHOD ...... 107 5.6. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PERCENTILE ...... 109 5.7. ESTIMATION OF THE CONFIDENCE INTERVAL OF A PROBABILITY ...... 110 5.8. ESTIMATION TECHNIQUES FOR TRANSIENT-STATE SIMULATION ...... 111 COMPUTER PROJECT ...... 112 CHAPTER 6: VALIDATION OF A SIMULATION MODEL ...... 121 COMPUTER PROJECTS ...... 123 viii Harry Perros Simulation Techniques Sim S CHAPTER 7: VARIANCE REDUCTION TECHNIQUES ...... 127 7.1. INTRODUCTION ...... 127 7.2. THE ANTITHETIC VARIATES TECHNIQUE ...... 128 7.3. THE CONTROL VARIATES TECHNIQUE ...... 133 COMPUTER ASSIGNMENTS ...... 136 CHAPTER 8: SIMULATION PROJECTS ...... 137

8.1. A SIMULATION OF A CPU PRIORITY SCHEDULER ...... 137 8.1.1. THE SIMULATION LOGIC ...... 139 8.1.2. TASK 1: HAND SIMULATION ...... 143 8.1.3. TASK 2: WRITE THE BASIC SIMULATION CODE ...... 145 8.1.4. TASK 3: STATISTICAL ESTIMATION OF THE RESPONSE TIME ...... 146 8.1.5. CORRECT SIMULATION RESULTS ...... 147 8.2. A SIMULATION OF THE FLOW OF SIP MESSAGES ...... 150 8.2.1. THE SIMULATION LOGIC ...... 151 8.2.2. IMPLEMENTATION AND RESULTS ...... 153 8.3. A SIMULATION MODEL OF A MULTIPLEXER ...... 155 8.3.1. SIMULATION LOGIC ...... 156 8.3.2. RESULTS ...... 157 8.3.3. AN EXTENSION OF THE SIMULATION MODEL ...... 158

ix

Forward This book is based on my lecture notes for an undergraduate course on discrete-event simulation techniques that I used to teach back in the early 80s. In the early 90s I prepared the first version of this book and made it available through my web site free of charge to anyone interested in discrete-event simulation. In a light-hearted moment over a few beers, I decided to call it the “definitive introduction!”. I am not sure how “definitive” it is, but the title stuck!

Over the years I have received many positive comments despite errors and other issues that resulted from not putting enough time into the writing of the book. In this version, I reformatted the manuscript, re- moved errors and improved the flow of the presentation in many sec- tions. I also removed some advanced topics on data structures and random number generation methods, as I want to keep the book at an introductory level.

I would like to emphasize that this book was written for people from all disciplines that use discrete-event simulation. As such, I describe topics that maybe well-known to computer science students. The knowledgeable reader can skip these sections.

How to use the book: This is a self-study book. In Chapter 1 you will find three examples. Select one of the three and do the corresponding computer project given at the end of the Chapter. Then, after you read each new Chapter, do the computer project that corresponds to the example that you have selected, described at the end of the Chapter. By the time you reach the end of the book, you will have developed a very sophisticated simulation model! You can use any high-level pro- gramming language you like. After you finish this book, I strongly recommend that you learn one or two simulation languages.

Errors: I am not responsible for any errors in the book, and if you do find any, please let me know ([email protected]).

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

xi Simulation Techniques Harry Perros Sim S 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

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

xiii Simulation Techniques Harry Perros Sim S

xiv Harry Perros Simulation Techniques Sim S

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 , transportation, and health, to 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 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- 1 Simulation Techniques Harry Perros Sim S 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.

Levels of Detail

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 2 Harry Perros Simulation Techniques Sim S 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.

Define the Problem a

Design Simulation (alternatives) Analyze Data Experiments

Run the Simulator Formulate Sub-Models

Analyze the Results Combine Sub-Models

Implement Results Collect Data

Write the Simulation Program

Debug

Validate Model Earlier Steps

a

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. 3 Simulation Techniques Harry Perros Sim S 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.

4 Harry Perros Simulation Techniques Sim S

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.

Repairman Finite Population of Machines

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

5 Simulation Techniques Harry Perros Sim S 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.

6 Harry Perros Simulation Techniques Sim S A machine breaks down

Repairman yes Join the busy queue ?

Repairman becomes busy

Repair starts

Figure 1.5: An arrival event

A machine is repaired

Other no Repairman machines to be becomes repaired? idle

A new repair starts

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. 7 Simulation Techniques Harry Perros Sim S a

A machine Choose A machine Breaks down next event is repaired

Create operation time Queue no empty? a

ye Queue yes s empty? a Generate a new service no Generate a a new service

a

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 8 Harry Perros Simulation Techniques Sim S 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.

State of �� CL1 ��� ��� ��� � server 0 1 4 9 - 0 idle 1 - 4 9 6 1 busy 4 - - 9 6 2 busy 6 16 - 9 11 1 busy 9 16 - - 11 2 busy 11 16 21 - 16 1 busy 16 - 21 26 21 1 busy

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-

9 Simulation Techniques Harry Perros Sim S

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

A

Arrival Next Departure (ith machine) event (th machine)

MC=CLi MC=CL4

No CLi=MC+10 n=0 ? n=n+1

yes n=n-1

R=1

Yes n=0 ? R=0 CL4=MC+5 No A A CL4=MC+5

A

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

Figure 1.9: Nodes interconnected by a shared medium

10 Harry Perros Simulation Techniques Sim S ume that the token never gets lost. A node cannot transmit unless it has the token. When a node receives the token from its previous logi- cal upstream node, it may keep it for a period of time up to �. During this time, the node transmits packets. A packet is assumed to consist of data and a header. The header consists of the address of the sender, the address of the destination, and various control fields. The node surrenders the token when: a) time � has run out, or b) it has trans- mitted out all the packets in its queue before � expires, or c) it re- ceives the token at a time when it has no packets in its queue to transmit. If time � runs out and the node is in the process of transmit- ting a packet, it will complete the transmission and then it will sur- render the token. Surrendering the token means, that the node will transmit it to its next downstream logical neighbor.

token .

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.

11 Simulation Techniques Harry Perros Sim S 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. 12 Harry Perros Simulation Techniques Sim S

Arrival MC=ATi occurs

Join the qi = qi + 1 queue

Schedule ATi = MC + new Next Arrival inter-arrival time

Return

Figure 1.11: Arrival event at queue �

Service is MC = DT completed i

Departs from q = q-1 queue

yes Queue Pass H = (H+1) mod3 empty? token no Schedule arrival ANH = Token time MC + switch over time time out ? yes Return no

Schedule new DT = MC + new service time service i

Return

Figure 1.12: Service completion at queue �

13 Simulation Techniques Harry Perros Sim S This means that Time-out node is still occurs transmitting.

Raise a flag

Return

Figure 1.13: Time-out of token

Arrival of token MC = ANH at next queue

Queue yes Pass H = (H + 1) mod3 empty token ?

no Schedule ANH = MC + next arrival (switch over time) Schedule TOUT = MC + T time-out yes

DT4DTi = MC += MC + Schedule next (new service time)(new service time) service completion

Return

Figure 1.14: Arrival of token at next queue

14 Harry Perros Simulation Techniques Sim S

Initialize simulation

i.e. search event list Locate i. e. search event list for next event smallestfor smallest number clock value

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

UpdateIf new event events list are scheduled if newupdate events next are scheduleevent

Is no simulation over ? yes

End

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-

15 Simulation Techniques Harry Perros Sim S Queue � Queue � Queue � Token Node �� ��� ��� Q ��� ��� Q ��� ��� Q ���� ��� No 0 2 0 4 0 6 0 1 1 1 2 0 4 0 6 0 2 2 2 12 1 4 0 6 0 3 3 3 12 9 1 4 0 6 0 1 18 4 12 9 1 19 1 6 0 1 18 6 12 9 1 19 1 26 1 1 18 9 12 0 19 1 26 1 1 10 10 12 0 19 16 1 26 1 2 25 12 22 1 19 16 1 26 1 2 25 16 22 1 19 0 26 1 2 17 17 22 1 19 0 26 23 1 3 32 19 22 1 34 1 26 23 1 3 32 22 32 2 34 1 26 23 1 3 32 23 32 2 34 1 26 0 3 24 24 32 30 2 34 1 26 0 1 39 26 32 30 2 34 1 46 1 1 39 30 32 36 1 34 1 46 1 1 39 32 42 36 2 34 1 46 1 1 39 34 42 36 2 49 2 46 1 1 39 36 42 42 1 49 2 46 1 1 39 39 42 42 1 49 2 46 1 1 * 42 52 42 2 49 2 46 1 1 * 42 52 1 49 2 46 1 1 43 43 52 1 49 49 2 46 1 2 58

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

16 Harry Perros Simulation Techniques Sim S ferred to as server 1. Stage 2 consists of a finite capacity queue, re- ferred to as queue 2, served by a single server, referred to as server 2.

Stage 1 Stage 2

Queue 1 Server 1 Queue 2 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)

17 Simulation Techniques Harry Perros Sim S 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.

18 Harry Perros Simulation Techniques Sim S 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 19 Simulation Techniques Harry Perros Sim S 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 � and �, � = 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

20 Harry Perros Simulation Techniques Sim S 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.

21 Simulation Techniques Harry Perros Sim S 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

22 Harry Perros Simulation Techniques Sim S simulation advances from event to event correctly, and whether it updates the clocks and the other status parameters correctly.

23

Harry Perros Simulation Techniques Sim S 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

25 Simulation Techniques Harry Perros Sim S 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

26 Harry Perros Simulation Techniques Sim S 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.

27 Simulation Techniques Harry Perros Sim S 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:

� = �� + � (��� �), where �, �, � and � are all non-negative numbers. Given that the previous random number was �, the next random number � can be generated as follows. Multiply � by � and then add �. Then, compute the modulus � of the result. That is, divide the result by � and set � equal to the remainder of this division. For example, for � = 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 � = �� (��� �). 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 � 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:

28 Harry Perros Simulation Techniques Sim S

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 � = 2 (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 �. 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 �� + � is less than the word size, then it is in itself the result of the operation �� + � (��� �), where � is set equal to the word size. Now, let us assume that the ex- pression �� + � 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 (�� + �)/�. 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

29 Simulation Techniques Harry Perros Sim S digits. Obviously, the largest number that can be held in the register is 99, and we set � = 100. For � = 8, � = 2, and � = 10, we have that �� + � = 26, and 26 (��� 100) = 26. However, if � = 20, then the product �� (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:

� = �(�, �, . . . ) (��� �), 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:

� = �� + ��(��� �).

The special case of � = � = 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:

�(�, �, . . . ) = �� + �� + ⋯ + ��.

The case of �(�, �, ) = � + � 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-

30 Harry Perros Simulation Techniques Sim S 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,

� = �� + �� + ⋯ + ��(��� 2), where �, � = 1,2, …, and �, � = 1,2, … , �, are all binary. That is, the generator produces a sequence of bits. � 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-

31 Simulation Techniques Harry Perros Sim S 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:

� = � + � where � = 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:

� = � O �(����) 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:

� = � + �(����) where 0 < � < �. As can be seen, it is very easy to implement and also it is quite fast. A very long period, equal to � − 1, can be ob- tained if � is a prime number. However, using a prime number may not be very fast. Thus, typically � is set to 2 or 2. In this case, the maximum period of the additive LFG is (2 − 1)2.

The multiplicative LFG is: � = � × �(����), where � is set to 2 or 2 and 0 < � < �. The maximum period is (2 − 1)2.

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. 32 Harry Perros Simulation Techniques Sim S 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 �, 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,

�: � = 170 �: � ≠ 170

33 Simulation Techniques Harry Perros Sim S 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:

�: � = 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:

�: � = 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 �, then the sample mean �̅ is normally distributed with mean � and variance �/�, 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 34 Harry Perros Simulation Techniques Sim S 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 a 95% 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.

0.25

0.2

0.15 fX(t) fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 a µ b !" 2.5% 95% 2.5%

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 � = 1. A point � in a normal distribu- tion with mean � and variance � 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 0 expressed 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- 35 Simulation Techniques Harry Perros Sim S 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.

0.25

0.2

0.15 f (t) fXX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 -1.96 0µ 1.96 z

2.5% 95% 2.5%

Figure 2.2: Two-tailed test at ��% confidence

0.25

0.2

0.15 fX(t) fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 0µ 1.645 z 95% 5%

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 36 Harry Perros Simulation Techniques Sim S distribution, as shown in figure 2.3. Now, given a sample mean �̅, we calculate its corresponding � value, i.e., � = (�̅ − 170)/5, and we accept the null hypothesis if � < 1.645. Otherwise, we reject it and accept the alternative hypothesis. For instance, if �̅ = 175, then � = 1<1.645, and we accept the null hypothesis. Likewise, if �̅ = 155, for � = −3 < 1.645. However, if �̅ = 180, then � = 2 > 1.645, and therefore we reject the null hypothesis and accept the alternative hy- pothesis.

0.25

0.2

0.15 fX(t) fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 -1.645 0µ z 5% 95%

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

37 Simulation Techniques Harry Perros Sim S assumption, whereas in fact it is correct. A type II error occurs when we accept the null hypothesis when in fact it is not correct. Since we do not know the truth, we do not know whether we have committed a type I or a type II error. The type I error is commonly known as a false positive, and the type II error is known as a false negative. Table 2.1 summarizes the type I and type II errors.

Decision Real situation Ho is accepted H0 is rejected

Ho is true Valid Type I error

Ho is not true Type II error Valid

Table 2.1: Type I and type II errors

We note that we use the standard normal distribution if the population variance � 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.

0.25

0.2

fX(t)

0.15

fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 0µ 1.645 z 5% p-value

Figure 2.5: The p-value

38 Harry Perros Simulation Techniques Sim S 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

39 Simulation Techniques Harry Perros Sim S we reject the null hypothesis. However, if it falls in-between these values, say it is 0.07, then the conclusion is questionable. For, we re- ject the null hypothesis at 90% confidence but accept it at 95% and 99% confidence.

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 � be the sum of the −1 and 1 values. 2. Compute the test statistic: � = |�|/√�. 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.

40 Harry Perros Simulation Techniques Sim S

For example, let us consider the string: 1011010101. Then, � = 1 − 1 + 1 + 1 − 1 + 1 − 1 + 1 − 1 + 1 = 2 and � = 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 � will be close to zero. A large positive value of � is indicative of too many 1’s, and a large negative value of � is 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 2 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 � < |���(�)| − 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 � be the resulting bit string. Finally, augment � by appending its first � − 2 bits to the end, and let � be the resulting bit string. 2. Compute the frequency of occurrence of each �, � − 1 and � − 2 overlapping bit combination using the bit string �, �, � respectively. Let � be the total number of combina- tions of � bits, and let �,, �,, … , �,be the frequency of oc- currence for each of these combinations in �. Likewise, let � 41 Simulation Techniques Harry Perros Sim S be the total number of combinations of � − 1 bits, and let

�,, �,, … , �,,be the frequency of occurrence for each of these combinations in �. Finally, let � be the total number of

combinations of � − 2 bits, and let �,, �,, … , �,,be the frequency of occurrence for each of these combinations in �. 3. Compute the following statistics and difference statistics: 2 � = � − � � , 2 � = � − � � , 2 � = � − � � , �� = � − �

� � = � − 2� + � 4. Compute the �-values: � − value = IncompleteGamma (2 , �� /2) � − value = IncompleteGamma (2 , � � /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 � − value < 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 �� and � � become large which causes the � − values to become small.

As an example we consider the sequence � = 0011011101, with � = 10, and � = 3.

42 Harry Perros Simulation Techniques Sim S 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 � = 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, �, = 1, �, = 1, �, = 2, �, = 1, �, = 2, �, = 2, �, = 1. b. For the 2-bit blocks we use � = 00110111010. The 2 bit combinations are: 00,01,10,11, and their corresponding frequency of occurrence is: �, = 1, �, = 3, �, = 3, �, = 3 c. For the 1-bit blocks we use � = 00110111010. The 1-bit combinations are: 0,1 and their corresponding frequency of occurrence is �, = 4, �, = 6. 3. Compute the following statistics and differences: 2 � = (0 + 1 + 1 + 4 + 1 + 4 + 4 + 1) − 10 = 2.8 10 2 � = (0 + 9 + 9 + 9) − 10 = 1.2 10 2 � = (16 + 36) − 10 = 0.4 10 �� = � − � = 2.8 − 1.2 = 1.6

� � = � − 2� + � = 0.8

4. Decision at 99% confidence level:

� − value = IncompleteGamma (2,0.8) = 0.9057 � − value = IncompleteGamma (1,0.4) = 0.8805

Since � − value and � − value are greater than 0.01, the sequence passes the serial test. 43 Simulation Techniques Harry Perros Sim S 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� � � = + 1 �+�

2��(2�� − � − �) � = (�+�) (�+� − 1)

where �and � are the number of 0 � and 1’s.

For a sample, where � > 10 and � > 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 (�) 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 � be the number of pseudo-random numbers that fall within the �th subinterval (make sure that enough random numbers are generated so that � > 5). The � values are called the observed values. Now, if these generated random numbers are truly uniformly distributed, then the mean number of random numbers that 44 Harry Perros Simulation Techniques Sim S 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:

� � � = � − � �

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 � 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 � values 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 �: � = 2, � = 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?

45 Simulation Techniques Harry Perros Sim S

46 Harry Perros Simulation Techniques Sim S Chapter 3: Generating Stochastic Variates

3.1. Introduction In the previous Chapter, we examined techniques for generating ran- dom numbers. In this Chapter, we discuss techniques for generating random numbers from a given distribution. These random numbers are called stochastic variates or random variates. Pseudo-random numbers are uniformly distributed in the space [0,1], and they are typically referred to as random numbers. As will be seen in this Chapter, random numbers are used in the generation of stochastic var- iates.

There are many techniques for generating stochastic variates. The in- verse transformation method is the most commonly used technique. This is presented in section 3.2. In sections 3.3 and 3.4, we use the inverse transformation method to generate stochastic variates from known continuous and discrete theoretical distributions. In section 3.5, we describe how to generate stochastic variates from empirical distributions. Finally, in section 3.6 we describe an alternative method for generating stochastic variates known as the rejection method.

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, � = �(�), where �(�) 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. 47 Simulation Techniques Harry Perros Sim S 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��� = �, 0 ≤ � ≤ 1.

Let � be a random number. Then, � = � 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.

2 1 # " ! " r

0 0 1 " " 1 a) �(�) b) Inversion of �(�) Figure 3.1: Sampling from �(�) = ��

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

3�, 0 ≤ � ≤ 1 �(�) = . 0, otherwise

Let �(�) be the cdf of �(�). Then

� �(�) = 3��� = �| = � − 0 = �, 0 ≤ � ≤ 1. 0

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

48 Harry Perros Simulation Techniques Sim S Example 3: Generate random variates from

5�, 0 ≤ � ≤ 4 �(�) = . � − 2, 4 < � ≤ 10

We first normalize �(�). We have:

5��� + (� − 2)��

1 4 � 10 = 5 � + − 2� = 70. 2 0 2 4

Therefore 1 5 ⎧ � 0 ≤ � ≤ 4 ⎪ 70 2 �(�) = . ⎨ 1 � ⎪ 40 + − 2� 4 < � ≤ 10 ⎩70 2 Procedure:

1. Draw a random number �. 2. If � ≤ 0.4 then � = (5/140)� or � = (140/5)�. 3. Otherwise, set r = 40 + − 2� and solve for x.

Example 4: Generate random variates from

�, 0 ≤ � ≤ 1/2 �(�) = . 1 − �, 1/2 < � ≤ 1

This is the triangle distribution, and the area under the curve is equal to 0.5 × 0.5 = 0.25. We divide �(�) by 0.25 so it becomes a pdf. The sampling procedure is as follows.

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

49 Simulation Techniques Harry Perros Sim S 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.

1 " − $

$ " 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 � − � � = � − �

50 Harry Perros Simulation Techniques Sim S 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:

�(�) = ��, � > 0, � ≥ 0,

The cumulative density function is:

�(�) = �� = 1 − �.

1

#fX("t) ! " FX(t)

X X 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 − � or

1 − � = � or

51 Simulation Techniques Harry Perros Sim S 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 � = �, 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., �(�) = �� . Let �, �, �, � be the amount of time spent in stage 1,2,3, and 4 respectively. Then, the sum � = � + � + � + � follows the Erlang distribution.

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

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.

52 Harry Perros Simulation Techniques Sim S 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 �, �, … , �, and then set � equal to their sum, i.e., � = � + � + ⋯ + �. Specifically, let �, �, … , � be � random numbers. Then,

1 � = − log ( � ) . �

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

53 Simulation Techniques Harry Perros Sim S

0.25

0.2

0.15 fX(t) fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 µ

Figure 3.5: The normal probability density distribution

The pdf of a normally distributed random variable � is:

() 1 �(�) = � , −∞ < � < +∞, �√2� where � and � is the mean and variance of � respectively. If � = 0 and � = 1, then the normal distribution is known as the standard normal distribution and its pdf is:

1 �(�) = � , −∞ < � < +∞. √2� One way to generate variates from a normal distribution is to employ the central limit theorem. An alternative popular method is to use the Box-Muller algorithm that generates two normal variates at a time. Let � and � be two random numbers. Then, we can obtain two nor- mal variates � and � as follows:

� = −2����cos (2��) and

� = −2���� sin(2��).

54 Harry Perros Simulation Techniques Sim S 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: � �(� = �) = �(1 − �), � = 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 � and if � ≤ �, 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:

�(� = �) = �(1 − �), � = 0,1, ….

Its cumulative probability function is:

55 Simulation Techniques Harry Perros Sim S �(�) = 1 − � .

A stochastic variate � can be obtained using the inverse transfor- mation technique. We observe that 1 − �(�) = � also varies be- tween 0 and 1. Therefore, let � be a random number, then

� = �, or

���� = (� + 1)���� or

���� � = − 1. ����

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

� = �, 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:

� �(� = �) = � , � = 0,1,2, … �! where � is the average number of times the event occurs in a unit time.

56 Harry Perros Simulation Techniques Sim S 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 �, �, ⋯ , �, �, so that

� ≤ 1 < �.

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 1 − ���� ≤ 1 < − ����, � � or

���� ≥ −� > ���� or

��� � ≥ −� > ��� �, or

� ≥ � > �.

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

57 Simulation Techniques Harry Perros Sim S 3.5. Generating stochastic variates from an empirical distribution Empirical probability distributions are used often in simulation, and they can be either approximated by a known theoretical distribution or used as is. Generating stochastic variates directly from an empiri- cal distribution is quite easy and it is done using the inverse transfor- mation method.

1

7/8

FX(t) r p(X) 3/8 4/8

1/8 1/8 0 1 2 3 0 1 2 3 X X a) Probability density function b) Cumulative function Figure 3.6: Generating a stochastic variate from a discrete distribution

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 � and �(�), � = 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 �(�) = �(�) + �(�) + ⋯ + �(�), � = 1,2, ⋯ ,7, as shown in figure 3.8. 58 Harry Perros Simulation Techniques Sim S

f(x3) f(x4) f(x2) f(x5) f(x1) f(x6) f(x7)

x1 x2 x3 x4 x5 x6 x7 Figure 3.7: A histogram of a continuous random variable

r

x x x x x x x 1 2 3 4 5 6 7 Figure 3.8: Generating a stochastic variate from a continuous distribution

Generating a stochastic variate is straightforward. Let � be a random number and let �(�) ≤ � ≤ �(�). Then, we select a stochastic variate � by interpolating between the values � and �, that is:

� − �(�) � = � + (� − �) . �(�) − �(�)

1

!" #

$ % Figure 3.9: Normalized �(�)

59 Simulation Techniques Harry Perros Sim S 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 (�, �). 4. Accept a pair and use � = � + (� − �)� as a random vari- ate whenever the pair satisfies the relationship � ≤ ��(� + (� − �)�), i.e., the pair (�, �) falls under the curve in figure 3.9.

The idea behind this approach is that the probability of � being less than or equal to ��(�) is �[� ≤ ��(�)] = ��(�). Consequently, if � is chosen at random from the range (�, �) and then accepted if � ≤ ��(�), 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 �. If � < ��(�) = (1/2)2� = �, then accept �, 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 (�, �) lies within or on the circumference of the NE quadrant of the unit circle, if � + � ≤ 1. The numerical integration is accomplished by carrying out the following two steps a large number of times:

60 Harry Perros Simulation Techniques Sim S

1. Generate a pair of random numbers (�, �). 2. If � < �(�), where �(�) = 1 − � , then � is under (or on) the curve and hence the pair (�, �) is accepted. Other- wise, it is rejected.

The area under the curve is obtained as the ratio:

Total number of accepted pairs area = . Total number of generated pairs

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 61 Simulation Techniques Harry Perros Sim S 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.

62 Harry Perros Simulation Techniques Sim S 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 time when 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 63 Simulation Techniques Harry Perros Sim S 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.

A

Find next Future event event list

Advance time

Take appropriate action depending on the type of event

no Any A conditional events ? yes Create a new Future event(s) event list

A

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:

64 Harry Perros Simulation Techniques Sim S • 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 65 Simulation Techniques Harry Perros Sim S the array, clock �� for 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 66 Harry Perros Simulation Techniques Sim S 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.

. . . NULL head 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 CL showing the future time of an event and a value i indicating the type of event. The nodes are arranged in an ascending order of the clocks.

CL i CL j . . . CL n NULL head

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 67 Simulation Techniques Harry Perros Sim S 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.

sizeof(struct node) bytes

......

Memory

nodePtr

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:

68 Harry Perros Simulation Techniques Sim S 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

69 Simulation Techniques Harry Perros Sim S 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.

type clock NULL

newNodePtr

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; } 70 Harry Perros Simulation Techniques Sim S 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 71 Simulation Techniques Harry Perros Sim S 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.

NULL head

ARR 8.93 NULL

newNodePtr

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

ARR 8.93 NULL

head

Figure 4.8: head after the call to insertNode()

ARR 8.93 DEP 15.8 NULL

head

ARR 6.7 NULL newNodePtr

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

ARR 8.93 DEP 15.8 NULL head

ARR 6.7

Figure 4.10: head after the call to insertNode()

72 Harry Perros Simulation Techniques Sim S 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.

ARR 8.93 DEP 15.8

head

ARR 10.7 NULL

newNodePtr

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

ARR 8.93 DEP 15.8 NULL head

ARR 10.7

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;

73 Simulation Techniques Harry Perros Sim S 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.

74 Harry Perros Simulation Techniques Sim S 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.

i j . . . k NULL head

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 �,

75 Simulation Techniques Harry Perros Sim S

E Ei Ej

t t+1 … t1 … t3

Figure 4.14: The unit-time advance design

A

Master clock is increased by a unit-time

Is any future no event clock A = MC ? yes An event has occurred

Take appropriate action

Any conditional no event(s) A ?

yes Schedule new event (j)

A

Figure 4.15: The unit-time advance design

76 Harry Perros Simulation Techniques Sim S and that the next event � 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 � is then processed, the event list is modified accordingly, and then the master clock is in- creased by one unit time repeatedly until it hits the next event, and so on.

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. Stage 1 Stage 2

Queue 1 Server 1 Queue 2 Server 2

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

77 Simulation Techniques Harry Perros Sim S 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.

A

MCL = MCL + 1

> 1 ST = 1

ST = ST - 1 ST = ST - 1

End of service Take appropriate action

> 1 AT = 1

AT = AT - 1 AT = AT - 1

An arrival has occurred. Take appropriate action

A

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 , 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- 78 Harry Perros Simulation Techniques Sim S 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.

79 Simulation Techniques Harry Perros Sim S 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 �� and �� be the service time and the waiting time respectively of the

80 Harry Perros Simulation Techniques Sim S

�th arrival. Also, let �� be the interarrival time between the �th and (� + 1)st arrival. Finally, we assume that the �th arrival occurs at time �, starts its service at time �, and ends its service at time � + ��, as shown in figure 4.18. Let us assume now that we know the waiting time �� and the service time �� of the �th arrival. Then, one the following three situations may occur.

ST i WTi AT i+1

a s a s + ST i i i+1 i i

ith ith arrival (i+1)st ith arrival arrival begins its arrival ends its service service

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 �� of the (� + 1)st arrival can be easily determined as follows:

1. �� = (��−��) +��

= (�� +��)−��

= �� − ��,

where �� is the total waiting time of the �th customer in the system, i.e., queueing and being served.

2. �� =(�� + ��)−��

81 Simulation Techniques Harry Perros Sim S

= �� − �� We observe that in both cases 1 and 2, the waiting time of the (� + 1)st arrival �� is equal to �� − ��.

3. �� = 0

Having calculated ��, the total waiting time �� in the system of the (� + 1)st arrival is calculated by generating a service time ��. We have: �� = �� + ��. For the next arrival � + 2, generate an inter-arrival time ��, and then calculate �� and �� as above, and so on. The basic mechanism of this activity- based simulation model is shown in figure 4.19.

Initial Empty system conditions WT = ST = 0, TW = 0

A

Generate AT

Next arrival occurs TW > AT TW ≤ AT Next arrival when current arrival Compare occurs when is either waiting or TW with server is idle is in service AT

Generate Generate ST ST

TW = ST TW = ST + (TW - AT)

A A

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

82 Harry Perros Simulation Techniques Sim S 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.

83 Simulation Techniques Harry Perros Sim S The fluctuation in the inventory level is shown in figure 4.20. At time � an order is placed, which arrives at time �. The new order is added to the existing inventory. At time � 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 (� − �) and (� − �) are not the same. Depending on the application, lead times maybe or may not be constant.

Stock level

Reorder point

t t t t 1 2 3 4 time

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.

84 Harry Perros Simulation Techniques Sim S

Initialization

a

MCGenerate t=t+1 AT

Print t > T yes results no End > 1 LT = 1 order has arrived = 0 LT = 0 LT = LT - 1 I = I + Q no order outstanding

Generate daily demand D

I = I - D

b

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 85 Simulation Techniques Harry Perros Sim S 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.

b

no I < ROP TC1 = TC1 + I*C1 ?

yes a no LT = 0

yes

Generate LT

TC2 = TC2+ C2

no I < 0 TC1 = TC1 + I*C1

yes

TC3 = TC3 + I*C3 a

a

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. 86 Harry Perros Simulation Techniques Sim S Since a user never becomes idle, the user’s state continuously cycles through a think state and a CPU state, as shown in figure 4.23.

. . . think CPU think CPU think CPU state state state state state state

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!

TerminalsUsers Requests

• • • CPU

Figure 4.24: A round-robin queue

87 Simulation Techniques Harry Perros Sim S 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 88 Harry Perros Simulation Techniques Sim S 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 � and a client identification number �. 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.

. . . !1 #1 !2 #2 !1 #1 NULL head

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

All the information regarding the requests in the CPU queue, includ- ing the one in service, is maintained in the separate linked shown in figure 4.26. This linked list is known as a circular singly linked list, since it is singly linked and the last node is linked to the first node. Each node contains the number of quanta required by a request and its terminal identification number. Pointers H and E point to the begin- ning and end of the list respectively. The nodes are ordered in the way that the requests are waiting in the CPU queue. Thus, the first node represents the request currently in service, and the last node the re- quest currently at the end of the CPU queue. When a new request ar- rives at the CPU queue, a new node is created and it is attached after the node pointed by E. If a request requires further service upon com- pletion of its quantum, then its node is simply placed at the end of the linked list. This is achieved by simply changing H to point to the next node in the linked list, i.e., H¬LINK(H), and setting E to H. If a re- quest does not require further service upon completion of its quan- tum, then its node is removed and H¬LINK(H).

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

Figure 4.26: Future event list of all new arrivals to the CPU queue 89 Simulation Techniques Harry Perros Sim S 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.

b

Is yes no CPU MCL = MCL + 1 MCL = tarr queue empty ? A new arrival A departure event occurs event occurs

Is no b MCL=tarr b ?

yes A new arrival event occurs

b

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

90 Harry Perros Simulation Techniques Sim S 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- 91 Simulation Techniques Harry Perros Sim S 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.

92 Harry Perros Simulation Techniques Sim S 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-

93 Simulation Techniques Harry Perros Sim S puted using this sample, such as, the mean, the standard deviation, and percentiles.

. . . H !" # !$ % !& ' E

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

94 Harry Perros Simulation Techniques Sim S Node 1 Node 2 Node 3 Arr Dep Arr Dep Arr Dep x x x x x x x x x x x x x x x x H3 x

x x H2 x E3 x H1 x E2 x x E1 x

Figure 5.2: Data structure for the token-based access scheme simulation

Instead of keeping two columns per node, one can keep one column. When a packet arrives, its arrival time is stored in the next available location. Upon departure of the packet, its arrival time is substituted by its total time in the queue.

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 � another machine breaks down which brings the number of broken-down machines to 3. The time � − � 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 �, the time period � − � 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

95 Simulation Techniques Harry Perros Sim S 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

96 Harry Perros Simulation Techniques Sim S 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

97 Simulation Techniques Harry Perros Sim S 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 �, �, … , � be the n observations. Then, the sample mean is: 1 �̅ = � . � 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

98 Harry Perros Simulation Techniques Sim S 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 �, then the sample mean �̅ is nor- mally distributed with mean � and variance �/ �, 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.

99 Simulation Techniques0.25 Harry Perros Sim S

0.2

0.15 fX(t)

fX(t) 0.1

0.05

0 0 2 4 6 8 10 12 14 16 18 20 a µ b

2.5% 95% 2.5%

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 �. If it is not known, then we use the sample variance � given by the familiar expression:

1 � = (� − �̅). � − 1

Note that dividing the above summation ∑(� − �̅) by � − 1 gives an unbiased estimator of �, whereas dividing it by � gives the maximum likelihood estimator of �. Either denominators can be used.

The above expression for � is correct when the observations are in- dependent of each other. To compute the above expression, we need to store all the observations first and then at the end of the simulation compute the sample variance. The following equivalent expression

100 Harry Perros Simulation Techniques Sim S allows the calculation on the fly without having to store all the obser- vations.

1 1 � = � − � . � − 1 �

That is, when the �th observation is obtained, we update the cumula- tive sums ∑ � and ∑ �, 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: � � �̅ − � , �̅ + � . √� √� 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 �, �, … , � 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.

101 Simulation Techniques Harry Perros Sim S

Y

X

a) Uncorrelated, i.e., ��� = 0

Y

X

b) Positively correlated, i.e., ��� > 0

Y

X

c) Negatively correlated, i.e., ��� < 0

Figure 5.4: The three cases of correlation

102 Harry Perros Simulation Techniques Sim S 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 (�, � ), (�, � ), … , (�, �) be � pairs of observations of (�, �). Then, the sample covariance � is given by the expression:

∑ (� − �̅)(� − �) � = , � − 1 and the sample correlation �, obtained by normalizing the sample covariance, is:

103 Simulation Techniques Harry Perros Sim S ∑(� − �̅)(� − �) � = . ∑(� − �̅) ∑(� − �)

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 �, �, … , � from a random variable �. We form the following � − 1 pairs of observations: (�, � ), (�, � ), … , (�, �). We regard the first observation in each pair as coming from � and the second observation as coming from a random variable Y. Then, � is:

∑ (� − �̅)(� − �̅) � = , ∑ (� − �̅) ∑ (� − �̅) where

1 1 �̅ = � and � = � . � − 1 � − 1

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:

∑ (� − �̅)(� − �̅) � = , ∑(� − �̅) 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:

∑ (� − �̅)(� − �̅) � = , � ≥ 1, ∑(� − �̅) 104 Harry Perros Simulation Techniques Sim S 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 �, �, … , � where � is given by the expression:

1 � = (� �̅)( � − �̅). �

The lag � autocorrelation is computed as the ratio:

� � = , �

where � is the sample variance. We note that � = 1, since � = � . 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.

+1

rk

lag k

Figure 5.5: A correlogram with short-term correlation

+1

rk

lag k

Figure 5.6: A alternating correlogram

105 Simulation Techniques Harry Perros Sim S Let us now return to our estimation problem. Having obtained a sam- ple of � observations �, �, … , �, we calculate the autocorrelation coefficients, and then the variance can be estimated using the expres- sion:

/ � � = � 1 + 2 1 − � , �

where � is the sample variance given by

� = ∑ (� − �̅) , where �̅ = ∑ � . 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 �, �, … , �, batch 2 observations �, �, … , �, 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 �̅ 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 �̅, �̅, … , �̅, are approximately uncorrelated. Conse- quently, we can calculate the standard deviation using the standard expression. We have:

106 Harry Perros Simulation Techniques Sim S 1 �̿ = �̅ �

1 � = (�̅ − �̿), � − 1 and the confidence interval of �̿ is: � � �̿ − �. , �̿ + �. . √� √� 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 (�, � ), � ≥ 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 107 Simulation Techniques Harry Perros Sim S 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: �, �, … , � Replication 2: �, �, … , � ⋮ Replication �: �, �, … , �

Let �̅ be the sample mean of the �th replication, that is: 1 �̅ = � , � = 1,2, … , �. �

The sample means �̅, �̅, … , �̅ are independent from each other and therefore the confidence interval is: � � �̿ − �. , �̿ + �. , √� √� where

1 1 �̿ = �̅ and � = (�̅ − �̿). � � − 1 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.

108 Harry Perros Simulation Techniques Sim S 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 �. such that 95% of the values of the random variable � are less than �., i.e., ����[� ≤ �.] = 0.95. Correspondingly, 5% of the values of the random variable � are greater than �., i.e., ����[� ≥ �.] = 0.05.

These high percentiles give us an idea of the shape of the right-hand side tail of the probability distribution. For, if �. 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 �, �, … , �, be a sample of an endogenously created random variable X. The sample is sorted out in an ascending order, and let � ≤ � ≤ … ≤ � be the sorted ob- servations. Then, the 95th percentile (or any other percentile) of � is the value � 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 �. 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., �.,, �.,, … , �.,. Then, we have: � � �0.95 − �. , �0.95 + �. √� √� where

109 Simulation Techniques Harry Perros Sim S � � 1 1 2 �̅ = � and �2 = � − �̅ . . � ., � − 1 ., . �=1 �=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., �, �, … , �. Then, we have: � � �̅ − �. , �̅ + �. √� √� where

110 Harry Perros Simulation Techniques Sim S 1 1 �̅ = � and � = (� − �̅). � � − 1 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 �.�⁄√�, 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 �.�⁄√� 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 �

111 Simulation Techniques Harry Perros Sim S 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 � and � using the 2� replications. If � ≤ 0.12, then we stop, else, we repeat the above step.

.Finally, we note that the width �.�⁄√� can be reduced by reduc- ing the variance �. 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

112 Harry Perros Simulation Techniques Sim S 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 �. 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 �. vary as a function of ���. Therefore, if needed, decrease or increase the range of val- ues of ���. You will observe that �. is much larger than � since it is an estimate of the tail of the distribution, unless the dis- tribution has very low variability in which case the two estimates will be close. Graph your results and discuss the observed trends.

113 Simulation Techniques Harry Perros Sim S 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.

114 Harry Perros Simulation Techniques Sim S 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 �. of the response time by varying ���� while � is fixed equal to 15. You will observe that �. 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 115 Simulation Techniques Harry Perros Sim S ����∗ 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 �. 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 �. 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 �. ver- sus � for each value of ���� and discuss the observed trends. (You can also graph your results of � and �. 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.

116 Harry Perros Simulation Techniques Sim S 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 �. by varying the mean inter-arrival time ���� with the re- maining parameters fixed as follows: ���1 = 20, ���1 =

117 Simulation Techniques Harry Perros Sim S 200, ���1 = 50, ���2 = 10, ���2 = 300, ���2 = 30, and � = 4. You will observe that �. 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 �. 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 �. 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 �. increase as 118 Harry Perros Simulation Techniques Sim S the ���2 gets longer. Again, keep an eye on these two values and stop when they start increasing rapidly. Vary ���2 suffi- 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 �. versus ���2 and 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.

119

Harry Perros Simulation Techniques Sim S 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. 121 Simulation Techniques Harry Perros Sim S 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,

122 Harry Perros Simulation Techniques Sim S 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 − � ) � and �! � � = 1/ . (� − �)! �

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

123 Simulation Techniques Harry Perros Sim S 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 124 Harry Perros Simulation Techniques Sim S 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 � = 1/(µ-l), where 1/� is the mean inter- arrival time to the first queue and 1/µ and 1/µ is the mean ser- vice time in the first and second queue respectively. Use the ex- pressions for � and � to calculate the total mean waiting time in the manugacturing system � + �. Set 1/µ = 10 and 1/µ = 125 Simulation Techniques Harry Perros Sim S 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.

126 Harry Perros Simulation Techniques Sim S Chapter 7: Variance reduction techniques

N C S U Variance Reduction

7.1. Introduction In Chapter 6, it was mentioned that the accuracy of an estimate is proportional to 1/√�, where � is the sample size. One way to in- crease the accuracy of an estimate (i.e., reduce the width of its confi- dence interval) is to increase �. For instance, the confidence interval width can be halved if the sample size is increased to 4�. That is, the accuracy can be increased by running long simulations, which maybe time consuming. An alternative way to increase the estimate's accura- cy is to reduce its variance. If one can reduce the variance of an en- dogenously created random variable without disturbing its expected value, then the confidence interval width will be smaller, for the same amount of simulation. Techniques aiming at reducing the variance of a random variable are known as Variance Reduction Techniques. Most of these techniques were originally developed in connection with Monte Carlo Techniques (see section 3.7).

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.

127 Simulation Techniques Harry Perros Sim S 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 �,, �,, … , �, be � observations of � obtained in a sim- ulation run, and let �,, �,, … , �, be � observations of � obtained in a second simulation run. Then, the �th value of � is:

� + � � = , , , � = 1,2, … , �. 2 As will be seen below, we use the random variable Z to construct the confidence interval of �(�). We have

�(1) + �(2) �(�) = � 2

1 = �(�(1)) + ��(2) = �(�), 2 128 Harry Perros Simulation Techniques Sim S 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

1 ���(�) = [���(�) + ���(�(1), �(2)]. 2 The covariance of two random variables � and � can be written as

���(�, �) = ���(�, �)���(�)���(�) , where ���(�, �)is the correlation of � and �. Therefore,

1 ���(�) = ���(�) + ����(�) 2 1 = ���(�)(1 + �), 2 where � is the correlation between �(1) and �(2).

We note that if � < 0, then ���(�) < ���(�), and in this case we can use � in order to construct the mean and confidence interval of �, since � and � have the same mean and Z has a lower variance.

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- 129 Simulation Techniques Harry Perros Sim S 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 � and �. Using these two numbers we obtain the customer’s interarrival time � = � (�) and service time � = � (�), 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 � = � − �. 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 � and � for the �th simulated customer, so that � = � − � , where � = � (� )) and � = � (� ), has the opposite sign of �. 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 � = 1 − � and � = 1 − �. An easy way to implement this is to store � and � 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 � and � 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 �,, �,, … , �,, � = 1,2, we construct the mean and confidence interval estimate of �(�) using � as described above. That is, we calculate a new set of observations �, �, … , � where � = (�, + �,)/2, and then calcu- late the mean � ̅ and standard deviation �, from which we obtain the confidence interval of the mean of � which is the same as that of �.

130 Harry Perros Simulation Techniques Sim S 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

FirstOriginal run Sample SecondAntitheticOriginalrun Sample Variates Mean of Two Samples 55.0 ZAntithetic values Variates 55.0 Mean of Two Samples

45.0 45.0

35.0 35.0

25.0 Waiting Time Per Customer 25.0 Waiting Time Per Customer

15.0 15.0

5.0

5.0

1 5 10 15 20 25 30 Simulated Number of Customers 1 5 10 15 20 25 30 Simulated Number of Customers

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 131 Simulation Techniques Harry Perros Sim S 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.84 was 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.

$ $ 1.6 1.6 $ Straight$ Simulation Straight Simulation # Standard# AntitheticStandard AntitheticVariate Technique Variate Technique

1.2 1.2 # # $ $ " $ $ ⁄ $ 0.8 ! 0.8 $ $ $ Standard Error

Standard Error # $ # # #$$ $ # # 0.4 0.4

800 800 1600 16002400 24003200 3200 Sample SizeSample Size

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. 132 Harry Perros Simulation Techniques Sim S 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

133 Simulation Techniques Harry Perros Sim S in the queue, i.e., � is small. Therefore, we see that X and Y can be negatively correlated.

Let �, �, … , � be � observations of �, and let �, �, … , � be � ob- servations of �, where � is the inter-arrival time associated with the � observation. Let

� = �+ � − �(�), � = 1,2, … , �..

Then, the confidence interval of �(�) is given by the expression � �̅ ± 1.96 √� where

1 �̅ = � � and

1 � = − 1 (� − �̅). �

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

���(�) = ���(�) + � ���(�) − 2� ���(�, �).

� has a smaller variance than � if

� ���(�) − 2� ���(�, �) < 0.

134 Harry Perros Simulation Techniques Sim S 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

���(�, �) ���(�) = ���(�) + ���(�) ���(�) ���(�, �) − 2 ���(�, �) ���(�)

(���(�, �)) = ���(�) − ���(�)

Since

���(�, �) � = , ���(�)���(�)

We have

���(�) = ���(�)(1 − �).

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:

135 Simulation Techniques Harry Perros Sim S

� = � − �(� − �(�), where �, � = 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.

136 Harry Perros Simulation Techniques Sim S 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 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

137 Simulation Techniques Harry Perros Sim S collect data which they send to the server in the form of messages. The server processes the messages and issues commands that are sent to the actuators. There are two types of messages, namely, real-time (RT) and non real-time (nonRT). RT messages have to be processed by the server as fast as possible since they represent tasks that need to be executed in real time. For instance, in the case of a driverless car, the breaks have to be activated immediately when the radar detects a person in front of the car. NonRT messages represent non real-time tasks which are not time constrained.

RT Queue Arrivals of RT messages Server

nonRT Queue Arrivals of nonRT messages

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-

138 Harry Perros Simulation Techniques Sim S 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, � • State of the server �: idle (� = 0), serving an RT message (� = 1), serving a nonRT message (� = 2). 139 Simulation Techniques Harry Perros Sim S 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 � currently in the queue is increased by 1. No further action is required, if there are other messages waiting in the queue. On the other hand, if this message finds the queue empty, then it may be possible that it can start its processing immediately. Check the sta- tus of the server, and if idle schedule a new service time. No further action is required if it is busy serving an RT or a nonRT message. Be-

140 Harry Perros Simulation Techniques Sim S 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 � 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. 141 Simulation Techniques Harry Perros Sim S 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 � by 1. o If the remaining service time of the pre-empted nonRT message is zero, then the nonRT message has completed its service and it departs from the system. (Note: this con- dition can only happen when the clocks are integers, which is what we assume in tasks 1 and 2.1. In tasks 2.2 and 3 the clocks are defined as real variables, which means that two event clocks can never be the same.) o Determine the length of the service time �� and set ��� = �� + ��. o Decrease � by 1. o Set � = 1. • Go back to the beginning to locate the next event.

2. Arrival of a nonRT message

• Set �� to �������. • Message joins the nonRT queue. Increase � by 1. • Generate the next inter-arrival time ��� of a nonRT message and set ������� = �� + ���. • If � = 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 � by 1. 142 Harry Perros Simulation Techniques Sim S • 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 � 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 �� = 0 are used: ���� = 3, ������� = 5, � = 0, � = 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. 143 Simulation Techniques Harry Perros Sim S 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.

�� ���� ������� � � ��� � RST �� ����� 0 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

144 Harry Perros Simulation Techniques Sim S 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 ����, ����, ��� and ��� 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, ���� = 5, ��� = 2, and ��� = 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.

145 Simulation Techniques Harry Perros Sim S 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 � 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. � 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.

146 Harry Perros Simulation Techniques Sim S • Calculate the mean, 95th percentile and their confidence in- terval of � and �.

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, ��� = 4, and run your simula- tion for different values of ���� ranging from 10 to 40 in in- crements of 5. For each value of ���� obtain the mean, 95th percentile, and confidence intervals of � and �. Graph all your results on the mean response times of � and � including the confidence intervals (drawn as vertical bars), as a function of th ����. Likewise, graph all your results for the 95 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.

147 Simulation Techniques Task 1.1 Harry Perros Sim S MC RTCL nonRTCL n_RT n_nonRT SCL Status (s) Pre-empted time 0 3 5 0 Task 1.1 0 4 2 0 MC 3 RTCL13 nonRTCL5 n_RT 0 n_nonRT1 SCL 5 Status (s)1 Pre-empted time 1 50 133 105 0 02 45 21 01 53 13 105 0 1 56 12 10 65 13 10 0 20 105 12 10 105 13 1015 0 1 106 22 00 106 13 1015 0 0 1014 22 00 1310 1323 15 0 1 1015 21 01 1510 1323 1520 0 02 1415 21 01 1513 23 1520 0 1 1516 12 10 1615 23 20 0 20 1520 12 10 2015 23 2025 0 1 1620 22 00 2016 23 2025 0 0 2024 22 00 2320 2333 25 0 1 2025 21 01 2520 2333 2530 0 02 2425 21 01 2523 33 2530 0 1 2526 12 10 2625 33 30 0 20 2530 12 10 3025 33 3035 0 1 2630 22 00 3026 33 3035 0 0 3034 22 00 3330 3343 35 0 1 3035 21 01 3530 3343 3540 0 02 3435 21 01 3533 43 3540 0 1 3536 12 10 3635 43 40 0 20 3540 12 10 4035 43 4045 0 1 3640 22 00 4036 43 4045 0 0 4044 22 00 4340 4353 45 0 1 4045 21 01 4540 4353 4550 0 02 4445 21 01 4543 53 4550 0 1 4546 12 10 4645 53 50 0 20 4550 12 10 5045 53 5055 0 1 4650 22 00 5046 53 5055 0 0 5054 22 00 50 53 55 0 1 50 2 0 50 53 55 0 Task 1.2 0 54 2 0 MC RTCL TablenonRTCL 8.3: Correctnum_RT handnum_nonRT simulationSCL forStatus task 1.1Pre-empted time 0 3 5 0 Task 1.2 0 4 2 0 MC 3 RTCL 8 nonRTCL 5 num_RT 0 num_nonRT1 SCL 7 Status 1 Pre-empted time 1 50 38 155 0 02 47 21 01 73 8 155 0 1 78 12 10 85 138 15 0 21 127 11 10 127 138 15 0 10 148 22 00 138 1318 15 0 1 1217 11 01 1512 1318 1525 0 02 1417 21 01 1713 18 1525 0 1 1718 12 10 1815 1823 25 0 21 1722 11 10 2217 1823 25 0 10 1824 22 00 18 23 25 0 1 22 1 0 22 23 25 0 0 24 2 0

Table 8.4: Correct hand simulation for task 1.2 148

GHarryraphs: Perros Simulation Techniques Sim S

!"#$%&%'( Figure 8.2: Mean and confidence intervals for RT and nonRT messages

!"#$%&%'( The mean for RT remains almost constant whereas for Non-RT is decreasing. This is beca use in real-time jobs, Figurethe inter a8.rr3iv:a ��l timthe dpercentileoes not imp aandct th confidencee response ti mintervalse due to t h e fact that once a new job arrives, it immefordiat eRTly s tandarts pnonRTrocessin gmessages (except w hen 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 interarriva149l tim e 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. Simulation Techniques Harry Perros Sim S 8.2. A simulation of the flow of SIP messages In order to make a voice or video call, many messages are exchanged between the calling and the called devices prior to the moment where we start to speak. Several specialized servers are also involved in this message exchange to provide different functionalities, such as, au- thentication and location of the called device. These messages are used to setup the call, modify it, and finally tear it down when the us- ers hang up. SIP is a very popular protocol that defines the structure of these messages, the functionality of the various servers, and the flow of messages between the two devices.

For those familiar with SIP, in this simulation project you will model the flow of SIP messages through a P-CSCF, an S-CSCF, and an ap- plication server (AS). One does not have to be familiar with the con- tent of the SIP messages and the functionality of the SIP servers, since in this simulation we abstract the problem into a simulation of the flow of messages through a number of service stations, each rep- resenting a different SIP server. Such a network of interconnected service stations through which customers flow following different paths is known as a queueing network. These queueing models are technology independent and they are used to model many situations, such as, the flow of jobs in a manufacturing process, the flow of que- ries to a web server through the different web services that are in- volved in the processing of a query, the flow of parcels ordered on- line through a delivery system, and the flow of IP packets through a computer network.

AU

! Figure 8.4: The queueing system under study

150 Harry Perros Simulation Techniques Sim S 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

151 Simulation Techniques Harry Perros Sim S 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 152 Harry Perros Simulation Techniques Sim S 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.

153 Simulation Techniques Harry Perros Sim S 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.

154 Harry Perros Simulation Techniques Sim S 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.

Stream 1

Stream 2

Stream 3

B Stream 4 8 7 6 5 4 3 2 1 slots

Figure 8.5: The multiplexer with four arrival streams

155 Simulation Techniques Harry Perros Sim S 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 − �)�. 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.

156 Harry Perros Simulation Techniques Sim S 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 �, be the packet loss calculate in batch �, � = 1,2, … �. Then, the confidence interval is: � � �̅ − �. , �̅ + �. √� √� where

1 1 �̅ = � and � = � − �̅ . � , � − 1 ,

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 157 Simulation Techniques Harry Perros Sim S � = 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.

Stream 1 voice

Stream 2 video

Stream 3 data Stream 4 8 7 6 5 4 3 2 1 slots B

Figure 8.6: The multiplexer with priorities

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 158 Harry Perros Simulation Techniques Sim S 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 � and an upper bound �. 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 � can be seen part of the duffer that is dedicated permanently to the �th queue. That is, the � 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 �. • For each queue �, calculate the number of packets � which are over its lower bound �. If the number of packets in the �th queue is less or equal to � then � = 0. Then, the sum � + � + � has to be less than � − �. The simulation logic is the same as in the simulation model described above with the following additions. First, we have to decide which of the three queues a new arrival will join, and whether it will be saved in the buffer or it will be rejected, i.e., it will be lost. Also, we have to

159 Simulation Techniques Harry Perros Sim S 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 � of the total number of arrivals and another counter � 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: �/�, � = 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 160 Harry Perros Simulation Techniques Sim S 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 �, �, �, �, �, �, � 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 � = � = � = 0.20�, and � = � = � = 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.

161