Title: Discrete Event Simulation
1Discrete Event Simulation
- Dr Shamala Subramaniam
- Lecture Notes
- Week 3 - 6
2Simulation
- Definition A computer program which executes
the model developed for a particular system
3Simulation Base System
- Focus of this course is on modeling and
simulating queuing systems - What do you understand from the above sentence?
- This means that we WONT be simulating continuous
process such as a flight trajectory - We wont be simulating systems such as cars and
engines, bridges - We wont be simulating a food cycle system
- We WILL be simulating systems which have DISCRETE
processes- clear differences between one entity /
event to another
4Developing the skills to write a discrete event
simulation
- Steps and Descriptions
- Identify the system under study
- Develop the model of the project under study
- Define an event
- Derive the logical sequence of the events, which
comes first and second and etc - Integrate / Correlate time with the events
- Design the scheduler
- Assign the continuous start times for the events
- Define each event Arrival
- Define each event Departure
- Develop the main program
- Colleting the simulation results
5Link between Discrete and Queuing Systems
- Why?
- The simulation technique you will learn is called
Discrete Event Simulation - The link it has with queuing system is every
queuing system has discrete events which
influence waiting time (delay ) in a high
percentage. - Event ? An occurrence which changes the
statistical composition of a system
6Event queuing system
- Step 1 Identify your system (Project)
- Step 2 define Statistical composition of a
system - Before you define an event, kindly define what is
your statistical composition
- Example of Statistical Composition of Systems
- Carre Four Customer
- Bank Customer
- Computer System data/instruction
- Network - packets
7Event queuing system (cont.)
- Step 3 Define an event (Please do remember this
is actually a skill and not a fixed method) - For example
- In a simple queuing system in a payment counter
we will be able to define two events, the arrival
and departure of a customer - In a Medium Access Control (MAC) IEEE 802.11
protocol, we could define either 8 or 12 events
8Example Carre Four Payment Counter System
- 1 (one) payment counter
- Model the payment counter system
- Source of customers
- Service Counter / Cashier
- Queue
- Statistical composition
- Customers
Source of Load
System
Service Counter
Queue
9Example Carre Four Payment Counter System
(cont.) defining an event
- Event an occurrence that changes the
statistical composition of a system - Strategy to derive an event
- Define the statistical composition in this case
its the number of customers denoted as noc - Number of customer (noc) 0 , t 0
- When will this increase (noc) ?
- When a new customer comes in (arrival event) ?
Yes - When a customer leaves the system noc will
decrease (departure event) Yes - Can we consider queuing to be an event? No,
because queuing does not change the noc
(statistical composition)
10Why are we focused on only Queuing Systems?
- Queuing is a fundamental issue in issues related
to delay (waiting time), loss (customers leaving
without being served), throughput (number of
customers served) and etc. - Queuing Theory defined by Leonard Kleinrock (he
published two (2) books on Queuing Theory vol 1
(1975) and vol. 2 (1976)) analytical
explanation for the queuing phenomenon. - In this class, we complement this analytical
models with discrete event simulation.
11Types of Queuing Systems
- Simple Queuing System (Base Model)
- M/M/1 queuing model
- Notation of a queuing system is done by
- Kendalls Law
12Kendalls Law Notation for Queuing System
- Single (1) Data Sink (i.e. server, CPU, Service
Counter) - Single Queue (1 Queue)
- Single Source (i.e. Traffic, Customers Arriving
from a door , data / instructions from memory)
13Inter-relate the components in single
source/queue/data sink system
Data Sink represents the processing capacity
Source represents the load placed onto the
system
Load lt processing capacity (lightly loaded) Q
0 Load processing capacity (heavy load) Q 0
Load gt processing capacity (over loaded) Q gt 0
. Delay x
14- At present most queue sizes are fixed .
- However, the Queue Occupancy changes.
15General observations from reference books on
simulation
- Journal / Conference References contain heavy
load analysis - Whereas generally most text books contain
lightly loaded analysis - Congestion (over loaded)
- Delay
- Queue size
- Multiple queues
- Packet / customer loss
16Algorithm to develop a Discrete Event Simulation
- Identify your system under study (Step 1)
- Develop the model (Step 2)
- Develop the events (arrival departure) (Step 3)
- Derive the sequence of events (Step 4)
- T0 arrival of customer 1
- T1 if (load lt processing capacity) delay 0
, q 0 departure ? arrival of c 2 - T1 (case 2 ) if (load gt processing capacity)
delay x , qy arrival of customer 2 ?
departure of customer 1
17Simulating the sequence of events
- Integrate TIME (Step 5)
- Difference between time and rate
- Load (customers / second)
- Processing Capacity (customers / second)
- From the events of arrival and departure assign
time to each event. The assignment of time
indicates Arrival time , departure time
(departure time arrival time delay) - Simulate time
- Can we use the in-built clock? NO, why because
its time consuming. - Have you ever used any other types of clock?
18Try This Exercise !!!
- Assume that today is Monday and you forgot to use
your watch. Without having to ask anyone What
time is it ?, manage the day and suggest how you
would attend all activities in the day. - Would any other day be easy to do this for you ?
19An example of My Daily Time Advancing Mechanism
- EVENTS TIME
- Mum opens door 5.30am
- Mum leaves for work 7.10am
- UPM traffic Controls 7.45 8.00 am
- Cars more than 10 at the Faculty - gt 8.30 am
- Students entering the class 9.00
- Change of students in the class at Bilik Seminar
1300 - Neighbor room leaves in the afternoon for class
1600 - UPM traffic Controls 1700 - 1730
20Analyze the events and time
- The observation is that the virtual clock is
ticking but not in an interval of 1s. It moves
based on the difference of time between two
events
530am
0710 am
21Structure of a Simulation
Main
Scheduler
Main
Functions is an event
Functions
Functions
Programmer
Simulation
Functions
Functions
Functions
Application
Scheduler A scheduler is a list of events and
its respective START time , where it finds the
smallest start time
22Scheduler for M/M/1 Queue
- Event Start Time
- Arrival x
- Departure y
How would we code this ? Declaration Part
Double arrival Double departure
23S6.1 Simulating Arrival and Departure for n
number of customers
- Arrival gt 1
- Departure gt 1
- Double arrival number of sources of customers
- Double departure
24S6.2 initialize starting time for all events
- Initialize
- sim_clock 0.0
- for(i0 ilt number of sources of customers
i) - Arrivali random
- Departure 0.0 or random or ????
- Whatever value we select must be larger than
arrival thus its would be safe to place a large
value such as (1.0e30) // The key concept is
to ensure that departure never gets selected
until an arrival has happened
25Initialization
- sim_clock 0.0
- Servicetime 0.5
- Service_counter 0 // 0 idle 1 busy
- for(i0 ilt numberofdoors i)
- Arrivali random
- Departure 0.0 or random or ???? Whatever value
26S6.3 Scheduler find the smallest start time
- Write a function to find the smallest event among
all the n-arrivals and 1- departure events. - Is this a simulation question or is it a data
structure question? Finding the smallest start
time is a data structure question ? SEARCHING for
the smallest item among n arrivals and 1
departure
27S6.4 Code of Scheduler find the smallest start
time in C language
- Void scheduler (void)
-
- Smallest 1.0e30
- For (p0 p lt numberofcustomers p)
-
- if (arrivalp lt smallest)
-
- smallest arrivalp
- doornumber p
-
Main
Scheduler
Arrival
Departure
- if (departure lt smallest)
-
- Smallest departure
- Event_type 1 // 1- departure 0 - arrival
-
- Else if (departure gt smallest)
-
- Event_type 0
- Doornumber p
-
Simulation
28S6.5 Advance The Clock Call the event
- void adv_clock()
-
- sim_clock smallest
-
- Void activate_event (void)
- if (event_type 0)
- arrival()
- Else if(event_type 1)
- departure()
-
29S7 Arrival Event
- Void arrival (void)
-
- nca // nca number of customers arrived
- arrivaldoornumber sim_clock iat()
- if (service_counter 0) // idle / available
-
- service_counter 1
- departure sim_clock servicetime
-
Random pattern distribution (for customers gt
Poisson distribution)
30S7.1 Function arrival
Load Source e.g noc/m one at a time
Load 10/s
Service 10c/s
- Arrival number of customers x
- Can we know for definite the number of customers
? No - The operating time 930 1600
317.2 Inter arrival times (IAT)
T2 t1 iat
T19
Iat depends on the system which is under
study Text/ Customers- Poisson
distribution Video On/Off Model Iat random
pattern (acquired from the system under study)
32arrivaldoornumber sim_clock iat()
(1) Arrivalnoc sim_clock iat() (2)
10 c/s
Total Load load 1 load 2 load 3 30 c/ s
(Poisson Theory)
10 c/s
10 c/ s
10 c/ s
33- Void arrival (void)
-
- nca // nca number of customers arrived
- arrivaldoornumber sim_clock iat()
- if (service_counter 0) // idle / available
-
- service_counter 1
- departure sim_clock servicetime
-
- Else if (service_counter 1)
-
- if (qsize MAXQSIZE) // Q is full
- totalcloss
-
34- Else if (service_counter 1)
-
- if (qsize MAXQSIZE) // Q is full
- totalcloss
-
- Else if (qsize lt MAXQSIZE) // q is not full
-
- qsize // number of customers in the q
- Etimeqqsize sim_clock
-
35- Void fdeparture (void)
- // customer leaves the bank
-
- nocd
- If (qsize gt 0) // there are customers in the q
- // integration of scheduling algorithms
- Delay sim_clock etimeq1 // fifo/lifo-
delay per customer - // Which customer is being selected from the
queue - Tdelay delay // average delay tdelay/nocd
- Departure simclock servicetime
- Mforward()
- --qsize
-
36S8 Define the Departure Event
- Void departure (void) // cont. case when q is
empty - // customer leaves the bank
-
- If (qsize 0) // there are no customers in the
q - // please refer handout of complete source code
-
37- Void mforward (void)
- // function to move the customers to the front of
the queue - // once the customer in the first position of the
queue has moved to the service counter -
- Int t
- for (t0 t lt qsizet)
- etimeqt etimeqt1
-
38S9 Main () - Simulation Architecture
- Main()
-
- load 0
- For (i0 ilt 10 i)
-
- Initialization()
- While (nocd lt TOS)
-
- Scheduler()
- Advance_clock()
- activate_event()
- Buff_mgt()
- // end for
-
- Initialization()
- Sim_clock 0.0
- Arrival0 0.3
- Departure 1.0e30
- Load 10
- Scheduler ()
- Smallest 0.3
- Event_type 0
- Advance Clock
- Sim_clock smallest 0.3
39Simulation Architecture
- Main()
-
- Initialization()
- While (nocd lt 30000)
-
- Scheduler()
- Advance_clock()
- activate_event()
-
-
- Initialization()
- Sim_clock 0.0
- Arrival0 0.3
- Departure 1.0e30
- Arrival
- Arrival0 (0.3 iat)iat
- Departure 0.3 st
40S10 Simulation Results
- Average delay (y axis)
- (load (customers/s) and service
time(customers/s)) - Customer Loss Ratio (y axis)
- Average Queue Size (y-axis)
- X-Axis ????
- Time
- Number of service Counters
41Average Delay
- Load (customers/m)
- random pattern (Poisson Distribution)
- Poisson Distribution
- Largely used for simulation of human queuing
patterns - Input interval (load increase) the delay will
increase exponentially. The input interval is of
fixed size (i.e. all intervals are of 1ms - Relationship between interarrival time (iat)
and load
42Average Delay
- Load (customers/m)
- Inter arrival time (iat) is inversely related
to load
Random
Load 6 Customers/s Iat 1/6s Delay a
0
1s
Time Interval 1230pm
Load 3 Customers/s Iat 1/3 , delay b lt a
0
1s
Time Interval 0900am
Pattern load increases, the delay increases as
the inter arrival time decreases.
43Poisson Distribution
- Pattern ? Load increases iat decreases
- Iat 1/ load (/) iat load (X)
- Iat 1/ load (customers/time frame) ? Pattern
- RANDOM
- Iat 1/load (DETERMINISTIC)
44Poisson Distribution
- Iat 1/load
- Option 1 iat ? random 1/ load
- Option 2 iat 1? random / load
- Option 3 iat 1/ load ? random
- Iat 1/ 10
- Iat ½
- iat rand()/load O1
delay
Load
45Poisson Distribution
- iat rand()/load Op.1
- Iat (rand() but it must close to value 1)/load
- Eg.
- Iat 10/100 0.1
- Iat 200/2001
delay
Load
46How can u make it close to 1?
- X rand()
- How can we make x as close to 1
- To maintain the 1/load pattern
- 1/x solution
- 1-1/x
- x/very big number (gt x)
- Iat -log(x/very large number)/load
Average delay (s)
Load (pkts/s)
47Termination of Simulation
- TOS
- Collect Results - beautiful average delay graph
- Question ? What is the average delay of a
customer if the load is 10pkt/s and the service
capacity is 5 pkt/s ? delay x - If we were to run this for different TOS
- 15 nocd
- 5,000 nocd
- 10,000 nocd
- 100,000 nocd
- 500,000 nocd
48Delay ? load service capacity
Average delay (s)
Transient Period
Steady State
x
Nocd (person)
5k
10K
100K
500K
49Average Delay ? load service capacity
Average Queue Size
Average delay (s)
100K
Load (customer/s)
10
20
30
100
50Average Delay ? load service capacity
Service capacity 90 customers/s
Capacity lt 10c/s
Average delay (s)
Capacity gt 100c/s
100K
Load (customer/s)
10
20
30
100
900am
1200noon
51Average Queue Size
- Queue Size Utilization
- Average Queue Size ( x ) / (y )
- Common understanding
- Average qsize totalqsize/nocd
- x total queue size
- Y number of queues (ans.1)
- Y number of customers arrived (ans.2)
- Average qsize totalqsize/time
- Time sim_clock
52Calculating Total Queue Size
Total delay
A3
Queue Size
Arrival / Departure
q1
Arrival / Departure
A1
A2
A4
0
time
0
5
t2
time
t1
5
Total queue size a1a2a3a4 A1 q1t1 A2
q2t2
T1 t2 ? No Why? Because the time difference
between two events are not the same
53Calculating Total Queue Size (cont.)
A3
Queue Size
Void buff_mgt() Totalqsize totalqsize (area
of each time interval)
Arrival / Departure
q1
Arrival / Departure
A1
A2
A4
time
0
5
t2
time
t1
Total queue size a1a2a3a4 A1 q1t1 A2
q2t2
Aoeti qsizeinterval (e.g t1)
54Calculating Total Queue Size (cont.)
A3
Queue Size
Arrival / Departure
q1
Arrival / Departure
A1
A2
A4
0
5
t2
t1
2.3
5.3
Sim_clock 0.3 (arrival)
Sim_clock 1.3(departure)
Sim_clock
time
55- Interval calculation
- Initialization ? last_check 0.0
- Void buff_mgt(void)
-
- Totalqsize qsize (sim_clock last_check)
- Last_check sim_clock
56- Main()
-
- load 0
- For (i0 ilt 10 i)
-
- Initialization()
- While (nocd lt TOS)
-
- Scheduler()
- Advance_clock()
- activate_event()
- Buff_mgt() (19/8/2005)
-
- Results() (19/8/2005)
- // end for
-
57Results
- Total delay ? departure
- Total qsize ? buff_mgt
- Total customer loss ? arrival ? q is full
- Void results(void)
-
- Averagedelay totaldelay/nocd
- AverageQsize totalqsize/sim_clock
- Clossratio totalcustomerloss/noca
58Assignment 1
- Write a complete program for the following
scenario - Giant Hypermarket has the problem of deciding how
many counters to place on - A) Monday Friday
- B) Saturday and Sunday
- If the ideal average delay for per customer
should be between 5 10 minutes decide the
number of service counters which are needed for
both the set of cases
59Case Studies
- Multiple sources Single Queue Multiple
Servers (Service Counters) - Example
- Bank Systems
- TGV
- Multiprocessor Systems
60Case Studies Networking
Store and Forward Share Resources Bandwidth
Switch/Routers
Intermediate Nodes
E
C
D
Source
Z
Destination
A
B
- Routing algorithm
- Shortest Path
- Scheduling Algorithm
- 1) FIFO, LIFO, WFQ,RR
End-to-end Source to destination
61- Routing Algorithms
- Source to destination
- Scheduling Algorithms
- One node
62Routing Algorithm
Q1
Q1
Q1
Q1
Z
E
B
C
A
A
63Incoming Buffer
Outgoing Buffer
Switching Fabric Multiplexing
Queue
p
Queue
p
Queue
p
Queue
Queue
p
Queue