You are on page 1of 80

Basic

Discrete-Event Simulation
Modeling
(PART I)

Jurusan Teknik Industri – Institut Teknologi Nasional


BANDUNG – 2017
Reference

MAIN
 Law (2015), Simulation Modeling and
Analysis, Chapter 1

SUPPORTING
 Banks, et al. (2014), Discrete-Event System
Simulation, Chapter 3

1
Classification of Simulation

 Based on time role in the model: Static vs.


Dynamic
 Based on probabilistic (i.e., random) contents:
Deterministic vs. Stochastic (Probabilistic)
 Based on the state of the system: Discrete vs.
Continuous

2
Types of Simulation Modeling

 Monte Carlo simulation: static, probabilistic


 Discrete-event simulation: dynamic,
probabilistic (stochastic), discrete
 System dynamics (Continuous simulation):
dynamic, deterministic or probabilistic
(stochastic), continuous
 Agent-based simulation: dynamic,
deterministic or probabilistic (stochastic),
discrete or continuous
3
Discrete-Event Simulation

Discrete-event simulation: concerns the modeling of


a system as it evolves over time by a representation
in which the state variables change
instantaneously at separate points in time.

Discrete-event simulation is mostly used in the field of industrial


engineering, e.g. for modeling manufacturing systems, service
systems, logistic systems, etc. Although in some cases, hybrid
approach is required, e.g. combined discrete and continuous
simulation for process-industry, bottling company, oil-tankers
logistic, etc.

4
State of a System

 State of a system: collection of variables necessary


to describe a system at a particular time, relative to
the objectives of a study.
 E.g. in a study of a bank, possible state variables:
– the number of busy tellers
– the number of customers in the bank

5
Fundamental Terms
 System: a collection of entities (e.g.: people and
machines) that interact together over time to
accomplish one or more goals.
 Measures of performance (or Performance
Indicator): output response of the system, as the
objective of the study for the system. It can be used
to compare one system to another.
 System state: the collection of variables necessary
to describe the system at a particular point of time.
 Event: an instantaneous occurrence that changes
the state of a system.
 Parameter: an intrinsic character of a system’s
component

6
Fundamental Terms
 Entity: any object or component in the system which
requires explicit representation in the model (e.g., a
customer, a machine)
 Attributes: the properties of a given entity (e.g., the
priority of a waiting customer, the routing of a job
through a shop, the arrival time of a customer)
 List: a collection of (permanently or temporarily)
associated entities ordered in some logical fashion
(e.g., FCFS, by-priority)
 Event list: a list of future events, ordered by time of
occurrence.

7
Example of Fundamental Terms
Example: notice a single server queuing system!
 State variables of the system state: number of
customer in system, number of customer in queue
(queue length), server status (busy or idle).
 Events: arrival of customer, customer entering
service, departure of customer from service.
 Measures of performance: average queue length,
average waiting time, average time in system,
maximum queue length, maximum waiting time,
server utilization.
 Parameter: (distribution of) service times,
(distribution of) arrival rate [inter-arrival times]

8
Example of Fundamental Terms

Exercise:
Identify state variables, events, and measures
of performance in the following systems:
A teaching-learning classroom
An automatic-teller-machine (ATM) center
An inventory warehouse
A field of battle-tank combat

9
Time-Advance Mechanism

 Simulation clock: a program variable in a


simulation model representing the current
value of simulated time.
 Approaches for advancing the simulation
clock:
1. Next-event time advance (NETA)
2. Fixed-increment time advance (FITA)
 Mostly, discrete-event simulation models
use next-event time advance

10
Next-event Time Advance

Example: In single-server queuing system,


suppose the successive events are:
t = 0: System starts
t = 3: First customer arrives, and then immediately enters
service
t = 4: Second customer arrives (then wait in the queue)
t = 6.5: First customer ends service, and then the second
customer enters service
t = 7.5: Third customer arrives
t = 9: Fourth customer arrives
t = 14: Second customer ends service, and then third customer
enters service
11 ….
Next-event Time Advance

 The simulation clock is advanced to the each event:

Time
0 3 4 6.5 7.5 9 14

12
Fixed Increment Time Advance

 Simulation clock is advanced by a fixed Dt.


 Every change in the system in the time period of
ti < Time ≤ (ti + Dt) is assumed to occur all at once in
the period (ti + Dt)

Time
0 Dt 2Dt 3Dt 4Dt 5Dt

 FITA sometimes is also used for animation in a NETA


model

13
General Logic for Discrete-Event Simulation
Initialization:
- Initialize random generator (seed)
- Initialize system state
- Initialize event list

Stopping criteria:
- Time-based or condition-based

Timing: (NETA approach)


- Determine the next most-imminent
event from event list
- Advance simulation clock to that event
- Remove that event from event list

Continuous-time statistic: (will be


explained later)

Event Logic (1, 2, …., k; k = number of


event-type):
- Update related system state
- Update related statistical counters
- Generate related future events and add
to event list

Report:
- Compute and write measures of
performance of interest
14
First simulation example

SINGLE-SERVER QUEUEING-SYSTEM (SSQS)

 This is considered the simplest-but-representative system for


starting to learn discrete-event simulation modeling.
 Remember that analytical models in queueing theory are available
for single-server queueing-system, so actually no-need to use
simulation for analyzing this system. But this will be a good
example for starting to learn simulation
15
SSQS: Intuitive explanation example

Consider a single-server queuing system.


 Interarrival times A1, A2, … of the customers are
independent and identically distributed random
variables. (Interarrival time is the time-interval
between a customer arrival to the next-customer
arrival)
 Service times S1, S2, … of the customers are
independent and identically distributed random
variables, independent of the interarrival times.
 A customer who arrives and finds the server busy
joins the end of a single queue.

16
SSQS: Intuitive explanation example

 Customers enter service in first-in, first-out (FIFO)


manner.
 Time to move in the queue and time to travel from
the queue-exit to the server, are assumed to be zero
 The simulation will begin in the “empty-and-idle”
state.
 We will simulate this system until a fixed number (n)
of customers have completed their waitings in queue
(stop simulation when the nth customer enters
service); for this example we will simulate until 6
customers has entered service (this terminating condition is
somewhat chosen arbitraryly for this example)

17
SSQS: Intuitive explanation example
Interarrivaltimes are from Exponential Distribution with b = 2
Service times are from Exponential Distribution with b = 1.5
(Time unit in minute)

Random numbers used for generating inter-arrival times of


customers are:
0.181, 0.451, 0.221, 0.573, 0.095, 0.550, 0.096, 0.503, 0.613, …

Random numbers used for generating service times are:


0.736, 0.373, 0.125, 0.520, 0.915, 0.330, …

18
SSQS: Intuitive explanation example

Recall:
inverse transform method to generate Exponential random
variates X with parameter b is:
- Generate U as IID U(0,1)
- Return X = - b * ln(1 – U)

By using the method, we obtain:

Interarrival times:
A1 = 0.4, A2 = 1.2, A3 = 0.5, A4 = 1.7, A5 = 0.2, A6 = 1.6, A7 = 0.2,
A8 = 1.4, A9 = 1.9, …

Service times:
S1 = 2.0, S2 = 0.7, S3 = 0.2, S4 = 1.1, S5 = 3.7, S6 = 0.6, …
19
SSQS: Intuitive explanation example

 Identified Measures of Performance:


• Average Waiting-Time
• Average Queue-Length
• Server Utilization
• Optional (additional) measures, e.g:
• Maximum queue-length
• Percentage of customers who is served less
than m minutes
• Etc.

20
SSQS: Intuitive explanation example

 Identified System’s State Variables:


• Server Status (IDLE or BUSY)
• Customers (Number of customers) in Queue

Optional:
• Customers in System = Customers in Queue +
Customer in Service

21
SSQS: Intuitive explanation example

 Identified events:
– Event Customer Arrival
 This event will increase the number of customers in
queue
– Event Customer Entering Service
 This event will decrease the number of customers in
queue, and change the server status from idle to busy
– Event Customer Departure
 This event will change the server status from busy to
idle

22
SSQS: Intuitive explanation example
Arrival event generates next Arrival event Departure event may generate EnterService event immediately

Arrival event is generated


in initialization

Arrival event may EnterService event


generate generates
EnterService event Departure event
immediately

23
SSQS: Intuitive explanation example

24
SSQS: Intuitive explanation example

25
SSQS: Intuitive explanation example

26
SSQS: Intuitive explanation example

27
SSQS: Intuitive explanation example

28
SSQS: Intuitive explanation example

29
SSQS: Intuitive explanation example

BUT WHAT IS IT FOR???

WE HAVE NOT COMPUTED


THE SYSTEM’S MEASURES
OF PERFORMANCE
30
SSQS: Intuitive explanation example

 Measures of Performance of Single Server


Queuing System:
– Expected average waiting-time (delay) in
queue, dAVG
– Expected average queue-length (number
of customers in the queue), qAVG
– Expected server utilization, u

31
SSQS: Intuitive explanation example

Estimating expected average waiting time (delay in queue)


dAVG: “average” of individual waiting times (delays in queue)
of n customers
n

D i
dˆ AVG  i 1
n

where Di is the observed waiting time of customeri

Di = Time when customeri enter service – Time when customeri enter queue

32
SSQS: Intuitive explanation example

Estimating expected average waiting time (delay in queue)


Di = Time when customeri enter service – Time when customeri enter queue

Customer-1 enter queue at 0.4, enter service at 0.4  D1 = 0.4 – 0.4 = 0


Customer-2 enter queue at 1.6, enter service at 2.4  D2 = 2.4 – 1.6 = 0.8
Customer-3 enter queue at 2.1, enter service at 3.1  D3 = 3.1 – 2.1 = 1.0
Customer-4 enter queue at 3.8, enter service at 3.8  D4 = 3.8 – 3.8 = 0
Customer-5 enter queue at 4.0, enter service at 4.9  D5 = 4.9 – 4.0 = 0.9
Customer-6 enter queue at 5.6, enter service at 8.6  D6 = 8.6 – 5.6 = 3.0

S Di = 5.7

Average delay in queue = 5.7 / 6 = 0.95 (minutes)


33
SSQS: Intuitive explanation example

Estimating average queue length (number of customers in queue)


qAVG: How to estimate qAVG?

Let’s see an intutitive concept as follow:

Suppose we observed from time-0 to time-10

Let Q(t) denotes the number of customers in queue.

Consider the following cases:

34
SSQS: Intuitive explanation example

Estimating qAVG: How to estimate qAVG?

35
SSQS: Intuitive explanation example

Estimating qAVG: How to estimate qAVG?

36
SSQS: Intuitive explanation example

Estimating qAVG: How to estimate qAVG?

37
SSQS: Intuitive explanation example

Estimating qAVG:

qAVG is a “time-average” of the number of customers in queue

Time average: the average depends on the magnitudes of the dimension


being averaged and the length-of-time spent in each of magnitudes.

Magnitude ~ vertical axis; Time-length ~ horizontal axis


 Magnitude x Time-length = Area

𝑇
‫׬‬0 𝑄 𝑡 𝑑𝑡 [Time average of Q(t)]
𝑞𝐴𝑉𝐺 = =
𝑇
[Total Area under Q(t) graph]
/ (divided-by)
[Total obeservation time]
38
SSQS: Intuitive explanation example
Estimating qAVG:

Total Area = 0x(0.4-0) + 1x(0.4-0.4) + 0x(1.6-0.4) + 1x(2.1-1.6) + 2x(2.4-2.1)


+ 1x(3.1-2.4) + 0x(3.3-3.1) + 0x(3.8-3.3) + 1x(3.8-3.8) + 0x(4.0-3.8)
+ 1x(4.9-4.0) + 0x(5.6-4.9) + 1x(5.8-5.6) + 2x(7.2-5.8) + 3x(8.6-7.2) = 9.9
Average queue-length = 9.9 / 8.6 = 1.15 (customers in queue)
39
SSQS: Intuitive explanation example
Estimating expected server utilization
u = proportion of time that the server busy
u = “time-average” value of busy function, where busy function B(t) is:

1 if the server is busy at time t


Bt   
0 if the server is idle at time t

Using the same logic to compute “time-average” statistic, the estimator for u
can be derived as follow

𝑇
‫׬‬0 𝐵 𝑡 𝑑𝑡
𝑢=
𝑇

[Time average of B(t)] = [Total Area under B(t) graph] / [Total obeservation time]

40
SSQS: Intuitive explanation example
Estimating expected server utilization

Total Area = 0x(0.4-0) + 1x(0.4-0.4) + 1x(1.6-0.4) + 1x(2.1-1.6) + 1x(2.4-2.1)


+ 1x(3.1-2.4) + 1x(3.3-3.1) + 0x(3.8-3.3) + 1x(3.8-3.8) + 1x(4.0-3.8)
+ 1x(4.9-4.0) + 1x(5.6-4.9) + 1x(5.8-5.6) + 1x(7.2-5.8) + 1x(8.6-7.2) = 7.7
Server utilization = 7.7 / 8.6 = 0.895 = 89.5%

41
Statistical Counter in Simulation

1. Discrete-time statistics or Tally statistics:


collected every change occurrence, e.g.
average waiting-time, percentage of
customers who is served < 1 minutes, etc.

2. Continuous-time statistics: collected


continuously every time advance, e.g. time-
average number-in-queue, time-average
server utilization, proportion of time when
the queue-length < 2, etc.

42
43

SSQS: Intuitive explanation example


Now let’s do the statistical computation in each step of our simulation:

43
44

SSQS: Intuitive explanation example

44
45

SSQS: Intuitive explanation example

45
46

SSQS: Intuitive explanation example

46
SSQS: Intuitive explanation example

47
SSQS: Stopping the simulation run

 Stopping rule alternatives:


– Condition based:
 Run simulation until a specified number of entities starts
service
 Run simulation until a specified number of entities exit
the system

– Time based:
 Run simulation for some fixed amount of simulated-time
(simulation-run length)

48
49

SSQS: Programming the model

 We may develop the simulation model using:


– General purpose programming language, e.g.
Visual Basic, C++, C#, Java, Python, etc.
– Simulation package, e.g. Arena, Promodel,
FlexSim, Anylogic, JaamSim, Netlogo, etc.

 The decision about what development tool should


we use depends on: software availability, financial
budget, flexibility requirement, and time availability.
50

SSQS: Programming the model


 For this course we will use Visual Basic for Appplication (VBA)
inside Microsoft Excel.
The advantage of Excel’s VBA:
– Highly available and affordable in most computers.
– Many built-in function in Excel for probability distribution.
– Easy to understand; very much resembling pseudo-code.
– Powerful feature for basic list-processing (using Collection
object), flexibility of Variant data-type, and dynamic array.
– Easy input-output interface for data reading and saving.
The disadvantage of VBA for simulation:
– Relatively slower compared to C++, Java, and other
compiled stand-alone programming language.
– Not a specific language for simulation.
51

SSQS: Programming the model


In Pseudo-code / VBA-like:
START
Call Initialization
Do While SimTime < Run-Length
Call Timing
Call ContinuousTimeStat
Select Case EventType:
Case Arrival: Call ArrivalLogic
Case EnterService: Call EnterServiceLogic
Case Departure: Call DepartureLogic
End Select
Loop
Call Report
STOP
52

SSQS: Programming the model


Design main user interface for input-output. For example:
53

SSQS: Programming the model

Import module RNGLawKelton.bas for


random number generator

Insert a new module, then name it


conveniently, e.g. SSQueueSim
Our program will be developed inside this
module, not in the Sheet’s VBA
54

SSQS: Programming the model


The CommandButton in sheet Main IO SSQS will only call the main program
subroutine in SSQueueSim module
55

SSQS: Programming the model


For convenient, we will divide the VBA programming editor to 4 main areas.
This is not mandatory, but to keep our program editing more structured

Area 1 (Top area) Global declaration (variables, objects, constants)

Area 2 Simulation main program

Area 3 Common simulation subroutines (GetInput,


Initialization, ContinuousTimeStat, Report)

Simulation events logic


Area 4
subroutines

Simulation library and math subroutines (random


variates generators, timing and event-list related
Area 5
subroutines)

Area 6 (Bottom area) Additional subroutines (e.g. animation, etc.)


56

SSQS: Programming the model


First, add global declaration in SSQueueSim module (at Area 1 / Top-area):

Use Collection object as a user-friendly


dynamic linked-list that is more powerful
than dynamic array
57

SSQS: Programming the model


….continued (at Area 1 / Top-area): Use constants (optional) to make the
programming more intuitive!
For example:

Rather than to write: If EventType = 1,


it will be more intuitive to write:
If EventType = ARRIVAL

Rather than to write:


CustInService = ServerQueue(1),
It will be more intutitive to write:
CustInService = ServerQueue(FRONT)

Rather than to write: If ServerStatus = 0,


It will be more intuitive to write:
If ServerStatus = IDLE

Etc.
58

SSQS: Programming the model


Idea of using Collection object for event-list

At first, Event List is empty Event List (Collection)

(EMPTY)

To add new event, create the new event data in EventToAdd, then Add the EventToAdd data to EventList

EventToAdd (array of variant) Event List (Collection)


ADD
1 2 3 1 2 3
(EvTime) (EvType) (EntityID) (EvTime) (EvType) (EntityID)
0.40 ARRIVAL “C1” 0.40 ARRIVAL “C1”

Timing subroutine pick the top-most event in


EventList, then transfer it to CurrentEvent, and
remove it from EventList

Event List (Collection) CurrentEvent (array of variant)


1 2 3
(EMPTY) (EvTime) (EvType) (EntityID)
0.40 ARRIVAL “C1”
59

SSQS: Programming the model

Event List (Collection) CurrentEvent (array of variant)


1 2 3
(EMPTY) (EvTime) (EvType) (EntityID)
0.40 ARRIVAL “C1”

When CurrentEvent is executed (in this case ARRIVAL), it generate EnterService (because the server is
IDLE and C1 is the front-most in the queue. Arrival-event also generate next arrival-event

EventToAdd (array of variant) Event List (Collection)


ADD
1 2 3 1 2 3
(EvTime) (EvType) (EntityID) (EvTime) (EvType) (EntityID)
0.40 ENTSRV “C1” 0.40 ENTSRV “C1”

Event List (Collection)


1 2 3
(EvTime) (EvType) (EntityID)
EventToAdd (array of variant) 0.40 ENTSRV “C1”
1 2 3 ADD 1 2 3
(EvTime) (EvType) (EntityID) (EvTime) (EvType) (EntityID)
1.60 ARRIVAL “C2” 1.60 ARRIVAL “C2”
60

SSQS: Programming the model


Event List (Collection)
Next Timing subroutine pick the 1 2 3
top-most event in EventList, then (EvTime) (EvType) (EntityID)
transfer it to CurrentEvent, and
0.40 ENTSRV “C1”
remove it from EventList
1 2 3
(EvTime) (EvType) (EntityID)
1.60 ARRIVAL “C2”

Event List (Collection) CurrentEvent (array of variant)


1 2 3
1 2 3
(EvTime) (EvType) (EntityID)
(EvTime) (EvType) (EntityID)
1.60 ARRIVAL “C2” 0.40 ENTSRV “C1”

When CurrentEvent is executed (in this case ENTSRV), it generate DEPARTURE event

Event List (Collection)


EventToAdd (array of variant) 1 2 3
(EvTime) (EvType) (EntityID)
1 2 3 ADD 1.60 ARRIVAL “C2”
(EvTime) (EvType) (EntityID)
1 2 3
2.40 DEPART “C1”
(EvTime) (EvType) (EntityID)
2.40 DEPART “C1” …and so on, so forth
61

SSQS: Programming the model


Because the EventList is always ordered (sorted) in ascending order, we
need to write a subroutine to add a new generated event to EventList at the
correct position in the list. Write this subroutine at Area 5
62

SSQS: Programming the model


Next, write this Timing subroutine at Area 5
63

SSQS: Programming the model


Next, write all required random variates generators at Area 5
For our example, we only need exponential random variates generator
64

SSQS: Programming the model


Next, write this (at Area 2) as the simulation main program algorithm
65

SSQS: Programming the model

Next, add this subroutine (at Area 3) to get input from worksheet and to
clear every output from previous simulation run.

Adjust the ranges (or cells’ positions) to suit your I/O interface design!
66

SSQS: Programming the model


Next, write this initialization subroutines at Area 3
67

SSQS: Programming the model


Next, write this ArrivalLogic at Area 4
68

SSQS: Programming the model


Next, write this EnterSrvLogic at Area 4
69

SSQS: Programming the model


Next, write this DepartureLogic at Area 4
70

SSQS: Programming the model


Next, write suboutine for updating continuous-time statistics (at Area 3)
71

SSQS: Programming the model


Next, write this subroutine for simulation report (at Area 3)

Please adjust the range (or cells’ locations) to suit your worksheet design
72

SSQS: Programming the model


It is highly reccommended that you always clean-up memory used by
dynamically allocated object, like Collection object.
You can write this clearing subroutine at Area 3
73

SSQS: Programming the model


Animation, even at a very simple form, is useful for helping to verify the model
(program). You can create your own custom-made simple animation, at Area 6

Adjust the range cells’


locations to suit your
animation design
74

SSQS: Programming the model

Congratulation! You’ve just develop your first,


simple simulation model. Try it, run it!
SSQS: Discussion

 Example for simulation-run length = 480


Input:
– Inter-arrival times:
Exponential with b = 1.0 (minutes)
– Inter-arrival times:
Exponential with b = 0.5 (minutes)
Output:
– Average waiting time = 0.483 (minutes)
– Average number in queue = 0.480 (customers)
– Average utilization = 0.483 (48.3%)

75
SSQS: Discussion
 From the result of simulation run examples, we can compare
with analytical result from M/M/1 queuing model:
If the inter-arrival times follow Exponential distribution with
mean 1.0, and the service times follow Exponential distribution
with mean 0.5, then from Queuing Theory we get:
– steady-state average waiting time in-queue: 0.5 minutes
– steady-state average number in queue: 0.5 customers
– steady-state server utilization: 50%

76
SSQS: Discussion
 If the inter-arrival times follow Exponential
distribution with mean 1.0, and the service times
follow Exponential distribution with mean 0.7, then
from Queuing Theory we get:
– steady-state average waiting time in queue: 1.633 minutes
– steady-state average number in queue: 1.633 customers
– steady-state server utilization: 70%

 By 480 minutes simulation we have sample output


as follow
– Average waiting time in queue: 1.527 minutes
– Average number in queue: 1.593 customers
– Average utilization: 66.1%

77
SSQS: Discussion

 POINTS TO NOTIFIY
– Simulation result is only estimate; but result from
analytical model is exact.
– The choice of stopping rule cannot be chosen
arbitrarily.
– Interpretation of simulation output must be
analyzed using statistical procedures for
estimation.

78
79

Now Let’s have


Tea Time and
relax for a
moment…

We’ll continue our


discussion in PART II

You might also like