CS 1538 Introduction To Simulation: Course Notes For
CS 1538 Introduction To Simulation: Course Notes For
CS 1538
Introduction to Simulation
By
John C. Ramirez
Department of Computer Science
University of Pittsburgh
• These notes are intended for use by students in CS1538 at
the University of Pittsburgh and no one else
• These notes are provided free of charge and may not be sold
in any shape or form
• These notes are NOT a substitute for material covered during
course lectures. If you miss a lecture, you should definitely
obtain both these notes and notes written by a student who
attended the lecture.
• Material from these notes is obtained from various sources,
including, but not limited to, the following:
Discrete-Event System Simulation, Fourth Edition by Banks,
Carson, Nelson and Nicol (Prentice Hall)
• Also (same title and authors) Third Edition
Object-Oriented Discrete-Event Simulation with Java by Garrido
(Kluwer Academic/Plenum Publishers)
Simulation Modeling and Analysis, Third Edition by Law and
Kelton (McGraw Hill)
A First Course in Monte Carlo
2
by George S. Fishman (Thomson &
Brooks/ Cole
Goals of Course
3
Goals of Course
4
Goals of Course
6
Introduction to Simulation
• What is simulation?
Banks, et al:
• "A simulation is the imitation of the operation
of a real-world process or system over time".
It "involves the generation of an artificial
history of a system, and the observation of
that artificial history to draw inferences … "
Law & Kelton:
• "In a simulation we use a computer to
evaluate a model (of a system) numerically,
and data are gathered in order to estimate the
desired true characteristics of the model"
7
Introduction to Simulation
8
Introduction to Simulation
9
Introduction to Simulation
12
Introduction to Simulation
15
Some Definitions
• System
"A group of objects that are joined
together in some regular interaction or
interdependence toward the
accomplishment of some purpose"
(Banks et al)
• Note that this is a very general definition
• We will represent this system in our simulation
using variables (objects) and operations
The state of a system is the variables
(and their values) at one instance in time
16
Some Definitions
Continuous System
• State variables change continuously over time
– Ex: Volume of CO2 in the atmosphere
> CO2 is being generated via people (breathing),
industries and natural events and is being
consumed by plants
17
Some Definitions
18
Some Definitions
• System Components
Entities
• Objects of interest within a system
– Typically "active" in some way
– Ex: Customers, Employees, Devices, Machines,
etc
• Contain attributes to store information about
them
– Ex: For Customer: items purchased, total bill
• May perform activities while in the system
– Ex: For Customer: shopping, paying bill
– In normal cases it is really just the period of
time required to perform the activity
• Note how nicely this
19
meshes with object-
oriented programming
Some Definitions
Events
• Instantaneous occurrences that may change
the state of a system
– Note that the event itself does not take any
time
– Ex: A customer arrives at a store
– Note that they "may" change the state of the
system
> Example of when they would not?
• Endogenous event
– Events occurring within the system
– Ex: Customer moves from shopping to the
check-out
• Exogenous event
– Events relating 20
/ connecting the system to the
outside
Some Definitions
• System Model
A representation of the system to be used /
studied in place of the actual system
• Allows us to study a system without actually
building it (which, as we discussed previously,
could be very expensive and time-consuming to
do)
Physical Model
• A physical representation of the system (often
scaled down) that is actually constructed
– Tests are then run on the model and the results
used to make decisions about the system
– Ex: Development of the "bouncing bomb" in WWII
> http://www.bbc.co.uk/dna/ww2/A2163827
> http://www.computing.dundee.ac.uk/staff/irmurray/bigbounc.asp
21
Some Definitions
Mathematical Model
• Representing the system using logical and
mathematical relationships
• Simulations are run using the mathematical
model, and, assuming it is valid, the results
are interpreted for the system in question
• Simple ex: d = vot + ½ at2
– This equation can be used to predict the
distance traveled by an object at time t
– However, will acceleration always be the same?
• More often this model is fairly complex and
defined by the entities and events
• So this is the model we will be using
22
Some Definitions
• Analytical evaluation
– If the model is not too complex we can
sometimes solve it in a closed form using
analytical methods
– One type of analytical evaluation is the Markov
process (or Markov chain)
– Nice simple example at:
http://en.wikipedia.org/wiki/Examples_of_Markov_chains
Deterministic Model
• Inputs to the simulation are known values
– No random variables are used
– Ex: Customer arrivals to a store are monitored
over a period of days and the arrival times are
used as input to the simulation
Stochastic Model
• One or more random variables are used in the
simulation
– Results can only be interpreted as estimates (or
educated guesses) of the true behavior of the
system
– Quality of the simulation depends heavily on
the correctness of the random data distribution
> Different situations may require different
24
distributions
Some Definitions
25
Some Definitions
Static Model
• Models a system at a single point in time,
rather than over a period of time
• Sometimes called Monte Carlo simulations
– We'll briefly discuss these shortly
Dynamic Model
• Models a system over time
• Our simulations will typically use this model
• In summary our models will typically
be: discrete, mathematical,
stochastic and dynamic
26
The Clock
31
Simple Example
32
Simple Example
35
Priority Queue to Represent the FEL
– Idea of a Heap:
> Store data in a partially ordered complete binary
tree such that the following rule holds for EACH
node, V:
Priority(V) betterthan Priority(LChild(V))
Priority(V) betterthan Priority(RChild(V))
> This is called the HEAP PROPERTY
> Note that betterthan here often means smaller
> Note also that there is no ordering of siblings –
this is why the overall ordering is only a partial
ordering 10
– ex: 30 20
40 70 85 35
90 45 80
38
Heap Implementation of a Priority Queue
39
Heap Implementation of a Priority Queue
– remove()
> Clearly, the min node is the root
> However, removing it will disrupt the tree greatly
> How can we solve this problem?
40
Heap Implementation of a Priority Queue
– Run-Time?
> Since our tree is complete, it is balanced and
thus for N nodes has a height of ~ lgN
> Thus upHeapand downHeap require no more
than ~lgN time to complete
> Thus, if we have N adds and N removeMins, our
total run-time will be NlgN
> This is a SIGNIFICANT improvement of the
simpler implementations, especially for a long
simulation
> Ex: Compare N2 with NlgN for N = 1M (= 220)
– Note:
> For our simple example, a heap is probably not
necessary, since we have few items in our FEL at
any given time
> However, for more complex simulations, with
many different event types, a heap is definitely
preferable 41
Implementing a Heap
Parent(i) = floor((i-1)/2)
LChild(i) = 2i+1
RChild(i) = 2i+2
44
Programming a Simple Example
Stock Profit
40 2.71 Expected profit
values for given
50 6.11 stock anoumts
60 9.51 Note that this table
shows that 60 is
70 9.2 the best choice
(more or less
80 6.4
agreeing with the
90 3.6 simulation results)
100 0.82
53
Newspaper Seller's Problem
55
Simulation Software
56
Simulation Software
57
Probability and Statistics in Simulation
58
Experiments and Sample Space
• Experiment
A process which could result in several
different outcomes
• Sample Space
The set of possible outcomes of a given
experiment
• Example:
Experiment: Rolling a single die
Sample Space: {1, 2, 3, 4, 5, 6}
• Another example?
59
Random Variables
• Random Variable
A function that assigns a real number to
each point in a sample space
Example 5.2:
• Let X be the value that results when a single
die is rolled
• Possible values of X are 1, 2, 3, 4, 5, 6
• Discrete Random Variable
A random variable for which the number
of possible values is finite or countably
infinite
• Example 5.2 above is discrete – 6 possible
values 60
Random Variables and Probability Distribution
• Probability Distribution
For each possible value, xi, for discrete
random variable X, there is a probability
of occurrence, P(X = xi) = p(xi)
p(xi) is the probability mass function
(pmf) of X, and obeys the following
∑
rules:
all i
p( xi )
63
Cumulative Distribution
Properties of cdf, F:
1) F is non-decreasing
2) lim ( F ( x) ) = 1
x →∞
3) lim ( F ( x)) = 0
x → −∞
and
P(a < X ≤ b) = F(b) – F(a) for all a < b
Ex: Probability that a roll of two dice will
result in a value > 7?
• Discuss
Ex: Probability that 10 flips of a fair coin
will yield between 6 and 8 (inclusive)
heads?
• Discuss 64
Expected Value
65
Expected Value and Variance
66
Expected Value and Variance
• Expected Value
– E(X) = (0)(q) + (1)(p) = p
• Variance
– V(X) = [02q + 12p] – p2 = p(1 – p)
A single Bernoulli trial is not that
interesting
• Typically, multiple trials are performed, from
which we can derive other distributions:
– Binomial Distribution
– Geometric Distribution
70
Binomial Distribution
Binomial Distribution
• Given n Bernoulli trials, let random variable X
denote the number of successes in those trials
• Note that the order of the successes is not
important, just the number of successes
– Thus, we can achieve the same number of
successes in various different ways
n x n − x
p q , x = 0,1, , n
p ( x) = x
0, otherwise
• E(X) = np
– Discuss
• V(X) = npq
• Consider an example:
– Exercise 5.1
– Read
– Do solution on board
72
Binomial Distribution
73
Geometric Distribution
Geometric Distribution
• Given a sequence of Bernoulli trials, let X
represent the number of trials required until
the first success
q p , x = 1,2,
x −1
p( x) =
0 , otherwise
P( X > s + t )
P–( X > s + tif| XX>>s+t,
Clearly, s ) =then X > t, so we get
P( X > s)
77
Geometric Distribution
∑ (q )
∞ ∞ ∞
∑q
j = s +1
j −1
p = ∑q
j = s +1
j −1
(1 − q ) =
j = s +1
j −1
−qj =
(q s
) ( ) (
− q s +1 + q s +1 − q s + 2 + q s + 2 − q s +3 + = q s )
– We can use similar logic to determine that
P(X > s + t) = qs+t
– Now our conditional probability becomes
q s +t
P( X > s + t | X > s) = s = q t = P( X > t )
q
– and thus we have shown that the geometric
distribution is memoryless
> We will see shortly that the exponential
distribution is also memoryless
78
Poisson Distribution
Poisson Distribution
• Often used to model arrival processes with
constant arrival rates
• Gives (probability of) the number of events
that occur in a given period
• Formula looks quite complicated (and NOT
discrete), but it is discrete and using it is not
e −αα x
that difficult
, x = 0,1,
p ( x) = x!
0 , otherwise
x
e −α α i
F ( x) = ∑
i =0 i!
• Example
– Number of people logging onto a computer per
minute is Poisson Distributed with mean 0.7
– What is the probability that 5 or more people
will log onto the computer in the next 10
minutes?
– Solution?
>Must first convert the mean to the 10 minute
period – if mean is 0.7 in 1 minute, it will be
(0.7)(10) = 7 in a ten minute period
>Now we can plug in the formula
>P(X >= 5) = 1 – P(0) – P(1) – P(2) – P(3) – P(4)
= 1 – F(4) (where F is the cdf)
= 1 – 0.173 (from Table A.4 in the text)
= 0.827
81
Continuous Random Variables
83
Continuous Random Variables
84
Continuous Random Variables
V ( X ) = E ( X 2 ) − [ E ( X )]
2
( Eq 5.10)
x2 b x3
dx − [ E ( X )] =
b
=∫
2
− [ E ( X )]2
a b−a a 3(b − a )
85
Continuous Random Variables
2
b x 3
b −a3 3
b −a
2 2
V (X ) = − [ E ( X )] =
2
−
a 3(b − a ) 3(b − a ) 2(b − a )
2
b −a
3 3
(b + a )(b − a ) b 3 − a 3 (b + a ) 2 (b − a ) 2
= − = −
3(b − a ) 2(b − a ) 3(b − a ) 4(b − a ) 2
4(b 3 − a 3 ) 3(b + a ) 2 (b − a )
= −
12(b − a ) 12(b − a )
4b 3 − 4a 3 − 3[b 2 + 2ab + a 2 ](b − a )
=
12(b − a )
4b 3 − 4a 3 − 3b 3 − 6ab 2 − 3a 2b + 3ab 2 + 6a 2b + 3a 3
=
12(b − a )
b 3 − a 3 − 3ab 2 + 3a 2b (b − a ) 3 (b − a ) 2
= = =
12(b − a ) 12(b − a ) 12
86
Uniform Distribution
91
Exponential Distribution
92
Gamma Distribution
Gamma Distribution
• More general than exponential
• Based on the gamma function, which is a
continuous generalization of the the factorial
Γ( β ) = ( β − 1)Γ( β − 1)
function
= ( β − 1)! when β ∈ Ζ
94
Erlang Distribution
96
Erlang Distribution
97
Erlang Distribution
2 −1
e − (120 / 50 ) (120 / 50) i
F (120) = 1 − ∑
i =0 i!
= 1 − (0.0907) − (0.2177) = 0.6916
– Note that these results are fairly intuitive
> Requiring both to be < 1 minute is more
restrictive a condition than requiring the sum to
be < 2 minutes, and would seem to have a lower
probability
– How about if we add another part: Probability
that the next 3 customers will have a
cumulative time of more than 2.5 minutes?
> Now we want P(X > 150) = 1 – F(150)
> But F has changed since we now have 3 events
> Let's do this one on the board
98
Normal Distribution
101
Normal Distribution
– Let Z = (X – 2.4)/0.8
– We want the area under the normal curve with
mean 2.4 and standard deviation 0.8 where x >
3.0
This will be 1 – F(3.0)
F(3.0) = Φ [(3.0 – 2.4)/0.8] = Φ
(0.75)
– Looking up Φ (0.75) in Table A.3 we find 0.77337
– Recall that we want 1 – F(3.0), which gives us
our final answer of 1 – 0.77337 = 0.2266
• The idea in general is that we are moving from
the mean in units of standard deviations
– The relationship of the mean to standard
deviation is the same for all normal
distributions, which is why we can use the
method indicated
102
Other Distributions
104
Poisson Arrival Process
108
Brief Intro. to Monte Carlo Simulation
110
Brief Intro. to Monte Carlo Simulation
112
Brief Intro. to Monte Carlo Simulation
Axiomatic probability
• Set theoretic approach that determines
probabilities of events based on the number of
ways they can occur out of the total number of
possible outcomes
• Gives the "true" probability of a given event,
whereas empirical probability only gives an
estimate (since we cannot actually have N be
infinity)
• However, for complex situations this could be
quite difficult to do
When axiomatic probability is not
practical, empirical probability (via Monte
Carlo sims) can often be a good
substitute 113
Let's Make a Deal
116
Monte Carlo Integration
117
Monte Carlo Integration
b
∫a
f ( x)dx = (b − a ) f (c)
121
Simple Queueing Theory
122
Simple Queueing Theory
123
Simple Queueing Theory
124
Simple Queueing Theory
125
Simple Queueing Theory
127
Simple Queueing Theory
131
Time-Average Number in System
T i =0 T ∫0
L(t ) dt
– See on board
∧
• For many stable systems, asLT ∞ (or,
practically speaking, as T gets very large)
approaches L, the long-run time-average
number of customers in the system
– However, initial conditions may determine how
∧
large T must be before the long-run average
LQ is
reached
The same principles can be applied to ,
the time-average133number in the queue,
Average Time in System Per Customer
136
Arrival Rates, Service Rates and Stability
138
Server Utilization
Server Utilization
• What fraction of the time is the server busy?
• Clearly it is related to the other measures we
have discussed (we will see the relationship
shortly)
• As with our other measures we can calculate
∧
this 1 ∞a given system (G/G/1/∞/∞)
for
ρ=
T
∑ T
i =1
i
G/G/1/∞/∞ systems
• Consider again the arrival rate λ and the
service rate µ
• Consider only the server (without the queue)
• With a single server, it can be either busy or
idle
• If it is busy, there is 1 customer in the "server
system", otherwise there are 0 customers in
the "server system" (excluding the queue)
– Thus we can define Ls = ρ = average number of
customers in the "server system"
• Using the conservation equation this gives us
– L s = λs w s
> where λs is the rate of customers coming into the
server and w140
s is the average time spent in the
Server Utilization
G/G/c/∞/∞ systems
• Applying the same techniques we did for the
single server, recalling that for a stable system
with c servers:
λ < cµ
• we end up with the the final result
ρ = λ/cµ
142
Markov Systems in Steady-State
147
Markov Systems in Steady-State
149
Markov Systems in Steady-State
µ ρ 2
LQ = =
2(1 − ρ ) (1 − ρ )
150
Markov Systems in Steady-State
∞
L = ∑ i Pi –= 0Note:
[1 − ρ We
]ρ 0 +can
1[1 −use
ρ ]ρthis
1
+ 2value
[1 − ρ ]to
ρ 2show
+ 3[1 −Lρfor
]ρ 3this
+
i =0 system
= 0 + ρ − ρ 2 + 2 ρ 2 − 2 ρ 3 + 3ρ 3 − 3ρ 4 +
1
= ρ (1 + ρ + ρ + ρ + ) = ρ (
2 3
)
1− ρ
Exercise 6.6
– Patients arrive for a physical exam according to
a Poisson process at the rate of 1/hr
– The physical exam requires 3 stages, each one
independently and exponentially distributed
with a service time of 15min
– A patient must go through all 3 stages before
the next patient is admitted to the facility
– Determine the average number of delayed
patients, LQ, for this system
– Hint: The variance of the sum of independent
random variables is the sum of the variance
> Discuss and develop the solution
153
Markov Systems in Steady-State
Exercise 6.23
– Copy shop has a self-service copier
– Room in the shop for only 4 people (including
the person using the machine)
> Others must line up outside the shop
> This is not desirable to the owners
– Customers arrive at rate of 24/hr
– Average use time is 2 minutes
– What impact will adding another copier have on
this system
• How to solve this?
– Determine the systems in question
– Calculate for each system the probability that 5
or more people will be in the system at steady-
state
154
Markov Systems in Steady State
Exercise 6.21:
– A large consumer shopping mall is to be
constructed
– During busy periods the expected rate of car
arrivals is 1000 per hour
– Customers spend 3 hrs on average shopping
– Designers would like there to be enough spots
in the lot 99.9% of the time
– How many spaces should they have?
• We will model this as an M/G/∞ queue, where
the spaces are the servers
– We want to know how many servers, c, are
necessary so that the probability of having c+1
or more customers in the system is < 0.001
155
Markov Systems in Steady State
156
Markov Systems in Steady State
∑P
k =0
k =1
∑P
k =0
k =1 •All that is needed for this
derivation is fairly simple
k
∞
λ algebra
∑ P0 = 1 •Before completing the
k =0 µ derivation, we must note an
∞
λ
k
important requirement: to be
P0 1 + ∑ = 1 stable, the system utilization
k =1 µ λ/µ < 1
must be true
1 •This allows the series to
P0 = k converge
∞
λ
1 + ∑
k =1 µ
159
Markov Systems in Steady State
1 1
P0 = =
λ / µ 1 − (λ / µ ) λ/µ
1 + +
1 − (λ / µ ) 1 − (λ / µ ) 1 − (λ / µ )
1 λ
P0 = = 1−
1 µ
1 − (λ / µ )
• Which is the solution for P0 from the M/G/1
Queue in Table 6.3
Utilizing Eq. 138.1, we can substitute back
k k
to get λ λ λ
Pk = P0 = 1 − = (1 − ρ ) ρ
k
µ µ µ
166
Linear Congruential Generators
Standard Equation:
X0 = seed value
Xi+1 = (aXi + c) mod mfor i = 1, 2, …
– where
a = multiplier
c = increment
m = modulus
• For c == 0 it is called a multiplicative
congruential generator
• For c != 0 it is called a mixed congruential
generator
– Both can achieve good results
• Initially proposed by Lehmer and studied
extensively by Knuth
167
– See references at end of the chapter
Linear Congruential Generators
168
Linear Congruential Generators
• Idea:
Consider discrete random variable X,
which has possible values x1, x2, …, xk
and probabilities p1, p2, …, pk (which sum
to 1)
Assume n random values for X are
chosen
Then the expected number of times each
value will occur Ei = npi
Now assume n random values of
distribution Y thought to be the same as
X are chosen
173
• How can we tell if distribution Y is the same as
Chi Square Testing for Uniformity
V =∑
k
( Yi − Ei )
2
i =1 Ei
• Where Yi is the observed number of
occurrences of value xi and Ei is the expected
number of occurrences of value xi
• This is showing the square of the differences
the observed values and the expected values
(divided by the expected values to normalize
it)
• Now consider two hypotheses:
– NULL Hypothesis, H0 : Y matches distribution of
X
– Hypothesis H1: 175
Y does not match distribution of
Chi Square Test
184
Kolmogorov-Smirnov Test
More specifically:
• Sort the empirical data
• Calculate the maximum "SN(x) above F(x)"
value, D+ for the empirical data
• Calculate the maximum "SN(x) below F(x)"
value, D- for the empirical data
– Idea is that if the empirical data matches the
uniform distribution, it should not ever be too
much above or below the cdf – See Figure 7.2
• Test max(D+, D-) for the desired level of
significance against the table of critical values
for the given degree of freedom (see Table
A.8)
– If it is too high, reject the null hypothesis
186
Kolmogorov-Smirnov Test
188
Tests for Independence
194
Other Tests
• Serial Test
– Let's start with an example here:
– Given a sequence of random values in U[0,1):
X1, X2, X3, X4, …
– Consider the values in non-overlapping pairs
(X1, X2), (X3, X4), …
– Now let's consider the distribution of the pairs
of values in a plane
> We can approach this in a similar way to what
we did with the Chi-Square frequency test
> Now, however, rather than dividing U[0,1) into
(for example) 10 bins we instead divide U[0,1)2
into (for example) 100 bins, one for each pair
> We then check the empirical results with the
expected values for each bin (using Chi-Square)
196
Other Tests
197
Other Tests
200
Other Random Number Generators
Combined LCGs
• In this case multiple LCGs are combined (in a
specific way) to produce generators with
longer periods
– See Section 7.3.2
• These must be handled very carefully
– Oddly enough, the best random generators
tend to be those that follow strict mathematical
formulas
Additive Generators
• These generators add previously generated
values to generate new values
• They typically differ on how many values are
added and which previous values are chosen
to add 202
Additive Generators
205
Inverse Transform Technique
209
Uniform Distributions on Different Ranges
1 1 2 n − m n − m +1
0, n − m + 1 , n − m + 1 , n − m + 1 n − m + 1 , n − m + 1
• Each sub-interval is equal in range and thus
has equal probability
• Given R in each sub-interval, a distinct value in
the range [m,n] will result
– Ex: Assume R is in the second sub-interval
above
– We then have
m + (n – m + 1)(1/(n – m + 1)) = m + 1 ≤ X <
m + (n – m + 1)(2/(n – m + 1)) = m + 2
– Since X is an integer, and is strictly less than m
+ 2, the entire sub-interval maps into m + 1
211
Inverse Transform Technique
212
Convolution Technique
213
Erlang Distribution
214
Erlang Distribution
217
Variate Generation Summary
• In a professional simulation
Software used will have predefined
functions for all of these variates
• However, it is good to know some of
the theory for how they are derived
You may perhaps have to derive one
You may need to examine the
implementation to perhaps improve the
efficiency
218
Input Modeling
221
Input Modeling
223
Panera Example
225
Panera Example
> The degrees of freedom will be n – s – 1, where n is
the number of intervals, and s is the number of
estimated parameters (in this case s = 1)
> Groups with fewer than 5 expected values will be
merged into a single group
– Note that the p-values described in the Banks text
(section 9.4.4) can be easily calculated in Excel
using the CHITEST function
> However there is an issue with degrees of freedom –
see panera.xls
– As indicated in the Banks text (section 9.4.2) we
should also consider an equal probability Chi
Square test
> Note that for our data this test does not perform
nearly as well as the “uneven” probability test (in fact
it fails fairly badly)
• We could also use a K-S test to test the
distribution
– See Example 9.16
226in Banks text
Panera Example