Modeling & Simulation Lecture 2
Discrete-Event Simulation
Instructor: Eng. Ghada Al-Mashaqbeh The Hashemite University Computer Engineering Department
Outline
Introduction. Discrete even simulation (DES). Components and organization of DES. DES example: single server queue system.
The Hashemite University
What is Discrete-Event Simulation (DES)?
Modeling of a system as it evolves over time by a representation where the state variables change instantaneously at separated points in time
More precisely, state can change at only a countable number of points in time These points in time are when events occur
What is an event? Instantaneous occurrence that may change the state of the system
Arrival of a customer Service completion (and departure) of a customer End of simulation (a fake event since no change in the status of the system takes place) Take some decision (also no change in system state here takes place). The Hashemite University
DES Milestones
Real system under study. Model of this system which contains:
Inputs. Processing or simulation flow as assumed in reality. Output (indications about the system performance).
You need to consider the following during simulation:
Set of events. Simulation time schedule that determines when events occur, i.e. determine simulation flow. Performance metrics: aspects of the system that you want to study. How to report such metrics (graphically, numerically, etc.). How to carry out simulation (by hand or using a digital computer). On a computer how to implement simulation (using a general purpose programming language or a special purpose software). When to end simulation.
The Hashemite University 4
Time Advance Mechanism
DES is dynamic so we need to keep track of time. Simulation clock: Variable that keeps the current value of (simulated) time in the model
Must decide on, be consistent about, time units Most of the time the unit of the time is not specified, just a fixed quantity of time. Usually no relation between simulated time and (real) time needed to run a model on a computer
Two approaches for time advance
Fixed-increment time advance Next-event time advance
The second approach is the most dominant.
The Hashemite University 5
Fixed-Increment Time Advance
0 e0 t 2t 3t e1 4t e2 5t 6t Time
The clock starts at 0 and it is advanced by a fixed increment and no skip of empty periods at which no events occur. There is no problem with events that occur at the fixed increment, they will be handled at time. However, Events occurring between time increments must be moved to an increment boundary (at the end of the interval in which it lie). Simple to implement, but has many drawbacks:
not an accurate realization of occurrence of events The model must define a technique to handle events that occur at the same time (due to movements to the end of time intervals). Rescheduling of events and the no skipping of empty period increase the 6 The Hashemite University simulation execution time.
Next-event Time Advance
Initialize simulation clock to 0 Determine times of occurrence of future events from event list Clock advances to next most imminent (first or earliest one) event, which is executed
Event execution may involve updating event list
Continue until stopping rule is satisfied (must be explicitly stated) Clock jumps from one event time to the next, and doesnt exist for times between successive events periods of inactivity are ignored so simulation execution time is shorter than the fixed increment approach. Clock jumps, most of the time, are not equal depending on the events sequence. Problem of simultaneous( ) events still exist but may be less than the fixed time increment.
The Hashemite University 7
Want to write a next-event program?
Determine the events and understand what happens when they occur. Generate events and keep a time-ordered list of the events. Based on the time-ordered list, we will have to schedule events and so advance the simulation clock accordingly. Track information of interest during the simulation. Finally, generate a report that contains the required results when simulation ends.
The Hashemite University 8
Components of a DES Program I
Simulation clock current value of simulated time System state variables to describe state
Server status, number in queue, arrival times, etc
Event list times of future events for each event type Statistical counters to accumulate performance measures
Waiting time in queue, server utilization,
Initialization routine
Start simulation at time 0 and initialize all variables and statistical counters in the system model
The Hashemite University 9
Components of a DES Program II
Timing routine
determines next event time, type; advances clock
Event routines
carry out logic for each event type and update the system state accordingly.
Library routines
utility routines to generate random variates, etc.
Report generator
Subprogram that computes estimates of the required performance metrics from the statistical counters and write a report of them.
Main program
ties routines together, executes them in the correct order May check for termination of simulation and invoke the report generator (this task may be handled in the event routine).
The Hashemite University 10
Organization of DES Program
It is the logical relationship (flow of control) between DES components described previously. It makes the programming, debugging, and promotion of DES easier. This organization (shown in the next slide) is mainly used when writing DES program using general purpose programming language. This organization is called event-scheduling approach. Since the time of future events are coded explicitly in the program. DES written using a special purpose simulation software follow the process approach where it tracks the experience of each entity as it flows in the system. In our course we will study the event-scheduling approach for DES.
The Hashemite University 11
Start
Initialization routine
1. Set simulation 0 clock=0 2. Initialize system state and statistical counters 3. Initialize event list
Main program
0. Invoke the initialization routine 1. Invoke the timing routine Repeatedly 2. Invoke event routine 2 Event routine i 1.Update system state 2.Update statistical counters 3.Generate future events and add to event list Is simulation over? Report generator No 1
Time routine
1. Determine the next event type, say, i 2. Advance the simulation clock
Library routines Generate random variates
Yes 1. Compute estimates of interest 2. Write report
Stop
The Hashemite University 12
Our DES Example
We will focus on a simple single-server service center (or single server queuing system). Possible examples:
Convenience store: customers with single cashier Single ATM banking system.
Customer in service
Arriving customer
Customers in queue
Server
Departing customer
13
The Hashemite University
Single-Server Service Center
Customer in service
Arriving customer
Customers in queue
Server
Departing customer
Performance measures of interest:
Average customer waiting time (queuing delay). Average number of customers in queue (queue length). Average server utilization
How do we simulate this system and obtain measures of interest?
Need to simulate time
The Hashemite University 14
Next-event time-Advance in Our Example I
Consider the single-server service center example
ti = time of arrival of ith customer (t0 = 0) Ai = ti ti-1 = interarrival time between (i-1)st and ith customers Si = time spent serving the ith customer Di = delay in queue of ith customer Ci = ti + Di + Si = time ith customer completes service and departs
The Hashemite University
15
Next-event time-Advance in Our Example II
Generally, the interarrival time and the service time are generated randomly. So, we a need a probability distribution functions to model them, but which distribution function to use?
(more about this topic in later chapters in the course).
Arrows in the previous figure represent simulation clock advances based on the most imminent event principle.
The Hashemite University 16
Simulation Stopping Rule
In our example, simulation will stops when the queueing delay of n customers is computed. Is this identical to: n customers leave the system? Can you think in another terminating rule (or stopping criteria) for our system?
The Hashemite University 17
What about performance metrics computation?
Needed to be computed while simulation is running where their final values will be available when simulation ends. So, in our program we need variables to track the quantities needed to compute these metrics. Such variables are called statistical counters.
The Hashemite University 18
Examples of Statistical counters for Our Example
The number of customers has been served so far. The total of waiting times in queue so far. The number of customers that have waited in the queue so far. The longest time spent in queue (i.e. queuing delay) weve seen so far The total time spent in the system by all customers that have departed so far Server utilization: time duration in which the server is busy. etc.
The Hashemite University 19
Notes on Performance Measures
Determine whether the performance measure quantity is discrete or continuous. How?? Determine how to update the statistical counters during simulation which is affected by the measure nature, i.e. discrete or continuous, all values or just the min or max, etc. Determine how to represent this measure to the end user:
As a figure. As multiple values in a table. As one composite value, e.g. max, min, average, 20 The Hashemite University etc.
Our Example Implementation
The simulation of our example will be implemented using two approaches:
By hand: which will be figured out in details throughout the lecture. By computer using C (or C++) language: Also will be figured out in the lecture. Another solution exist in your textbook which is left for you. It will be useful for you to help in doing the homeworks and the project.
The Hashemite University 21
Hand Simulation of a Single Server Service Center
Interarrival times (all times are in minutes): service times: 2.0, 0.7, 0.2, 1.1, 3.7, 0.6, n = 6 delays in queue desired (for simulation end) Hand simulation:
Display system, state variables, clock, event list, statistical counters all after execution of each event Use above lists of interarrivals (to determine when new customers arrive), and service times (to determine when customers depart) to drive simulation Stop when number of delays hits n = 6, then compute output performance measures
The Hashemite University 22
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9,
Performance Measures I
First Metric: Expected average delay in queue (excluding service time) of the n customers completing their delays n ^
1 d (n) Di n i 1
Why expected? Since it is the average delay we expect a real customer will wait in the real system. This performance measure is discrete, since we can know the queueing delay of a customer at specified instances of time (countable) when customers enter service. And remember the simulation give us only estimates, for this purpose we put a hat (^) on the variable name to indicate that it is just an estimate of the actual value.
The Hashemite University 23
Performance Measures II
Second Metric: Expected average number of
customers in queue (excluding any in service)
A continuous-time average (at any instance of time you have a value for this quantity)
1 q ( n ) ipi iTi T ( n) i 1 i 1
^
Here q(n) is the weighted average. T(n) : The time required to observe the n delays in queue (simulation duration).
T(n) T0 T1 T2 ....
Ti : The total time during the simulation that the queue is of length i
The Hashemite University 24
Performance Measures III
Now let Q(t) to be the continuous curve that define the number of customers in queue over the simulation duration T(n). The calculated departure times of the customers are as follows: 2.4, 3.1, 3.3, 4.9, and 8.6. look we have only 5 values why??!!!? So, T(n) = 8.6 Look at the following figure
The Hashemite University 25
Q(t)
Q(t) vs. time
0
e1=0.4
2
e2=1.6 e3=2.1
4
e8=4.0 e7=3.8
Arrivals
e11=5.8 e10=5.6
e12=7.2
e6=3.3
Departures
e4=2.4 e5=3.1 e9=4.9
e13=8.6=T(6)
The Hashemite University
26
Performance Measures IV
Lets calculate q(t) from the previous figure
q(6) 9.9/8.6 1.15
The Hashemite University 27
Performance Measures V
Look deeply at the previous calculations, it is simply the area under the Q(t) curve integral. So:
1 q( n ) T ( n)
T ( n)
Q( t )
0
During simulation we will just accumulate the area under the Q(t) curve in the same manner used in the previous slide.
The Hashemite University 28
Performance Measures VI
Third Metric: Expected utilization
Another continuous-time average
(proportion of busy time) of the server
1 u ( n) T ( n)
^
T (n)
B(t )
0
1 if the server is busy at time t B(t) 0 if the server is idle at time t
The Hashemite University 29
B(t)
B(t) vs. time
0
e1=0.4
2
e2=1.6
4
e8=4.0
Arrivals
e3=2.1
e7=3.8
e11=5.8 e10=5.6
e12=7.2
e6=3.3
Departures
e4=2.4 e5=3.1 e9=4.9
e13=8.6=T(6)
(3.3 - 0.4) (8.6 - 3.8) 7.7 u(6) 0.90 8.6 8.6
The Hashemite University 30
Hand Simulation of Our Example
Now we will perform the simulation of the single server service system manually. We will see how to define the needed variables that will represent:
Entities in the system. Statistical counters to store the data needed to compute the output or the required performance measures. Variables needed for intermediate processing required to carry simulation such the event list and the simulation clock.
Also we need to define the values of the statistical counters while simulation is running.
The Hashemite University 31
Before Starting
Lets explore the used variables to represent the different entities and their attributes in our system model.
The Hashemite University
32
Time = 0
Status shown is after all changes have been made in each case
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
33
Time = 0.4
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
34
Time = 1.6
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
35
Time = 2.1
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
36
Time = 2.4
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
37
Time = 3.1
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
38
Time = 3.3
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
39
Time = 3.8
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
40
Time = 4.0
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
41
Time = 4.9
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
42
Time = 5.6
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
43
Time = 5.8
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
44
Time = 7.2
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
The Hashemite University
45
Time = 8.6
Interarrival times: Service times:
0.4, 1.2, 0.5, 1.7, 0.2, 1.6, 0.2, 1.4, 1.9, 2.0, 0.7, 0.2, 1.1, 3.7, 0.6,
Final output performance measures: Average delay in queue = 5.7/6 = 0.95 min./customers Time-average number in queue = 9.9/8.6 = 1.15 customers Server utilization = 7.7/8.6 = 0.90 (dimensionless)
The Hashemite University 46
DES Programming Issues
Program termination rules
Number of events, total time
Time breaking during event scheduling (what to do with simultaneous events?)
Choose departure before arrival Two arrivals at the same time? Randomly choose one Two departures at the same time? Suggest a solution!! Other defined rules
The Hashemite University 47
Self Reading
Subsection 1.4.7 Have a look at the C code of our simulation example. Also, have a look at sections 1.5 and 1.6 for additional examples of DES simulations.
The Hashemite University
48
Additional Notes
The lecture covers the following sections from the textbook:
Chapter 1:
Sections: 1.3, 1.4.1, 1.4.2, 1.4.3, and 1.4.7 Appendix 1A.
The Hashemite University
49