Simulation
Simulation
By
Banks Corson II & Nelson Nicle
Fifth Edition 2014
Pearson Edition Limited 2014
Elaborated by
1
:Syllabus
2
3 Estimation and statistical tests
•Empirical distributions
•Estimation
•Tests of hypotheses
•The Chi-Squire goodness-of-fit test
•The Kolmogorive-Smirnov test
Generation of Random Numbers 4
•Pseudo random numbers
•Congruential generators
•Testing and validating Pseudo random numbers
5 Introduction to queuing theory
•Review of the Poisson and Exponential
distributions
•The M/M/1/∞/FIFO system
•Summary measures for the M/M/1/∞/FIFO
system
•The M/M/1/k/FIFO system
•M/M/C/∞/FIFO system
3
6 Discrete system simulation
•Examples,
•Time management methods,
•Collecting and recording simulation data
•Analysis of simulation results
•Evaluation of the simulation model
7 Languages for discrete system
simulation
• Language characteristics
• Use multipurpose languages
• Special-purpose languages:
1.GPSS
2.SIMSCRIPT II.5
3.SLAM II
4.GASP IV
8 Introduction to continuous system
simulation.
•Models of continuous systems
•Solution of linear differential equations
•Analog computing
•Digital simulation of continuous systems
•Continuous system simulation languages
4
Simulation Principles and Methods
1. Introduction
The basic idea of Simulation is to build an
experimental device (model) that act like
(simulate) the real system in certain important
aspects. The purpose is to understand or
evaluate the behavior of a complex real-world
system over extended period without risk to the
real system performance.
5
• Too expensive, (not feasible)
• Too risky (atomic problems),(not possible)
• Too complex,
• Not existing system,
• Intractable.
7
b) State of a system: It is the minimal
collection of information with which the system future
behavior can be predicted. For the Jet aircraft, the
state of the system can be determined by Speed,
Direction of travel, weather conditions, number of
passengers, and amount of remaining fuel.
8
d) System Classification:
o Natural and Man-Made,
o Deterministic
o Stochastic (Probabilistic)
9
5. System methodology
When simulation is used to solve a problem, the
following time-tested steps, or stages are
applied:
10
The system being studied may impose constraints
on certain steps of this scientific method. For
example, consider the simulation of a system that
does not yet exist. Obviously the observation of
such system is not possible, but the simulation of
such system may still be possible if the analysis is
carefully conducted and if the ultimate
requirements are known. The problem solving
process is divided into four phases:
. Planning
. Modeling
. Validation
. Application.
11
a) Planning or premodeling includes the initial
encounter with the system, the problem to be
solved, and the factors pertaining to the system
and its environment that are likely to affect the
solution of the problem. This means the problem
must be well defined. Obviously the more accurate
and precise the problem statement is, the more
smoothly the solution process can proceed.
Resources must be considered and estimated such
as money, time, personal, and special equipment. If
crucial resources are not available, solution of the
problem can be judged infeasible before a
significant amount of time or money is spent.
12
b) Modeling in this phase the analyst constructs the
system model, which is a representation of the real
system. The characteristics of this model should be
Representative (not identical) of the characteristics of the
real system. You must select some minimal set of the
system's characteristics so that the model approximates
the real system to be cost-effective and manageable.
There are many types of simulation models:
14
d) Application Once the model has been properly
validated, it can be applied to solve the problem at
hand. A simulator may work error-free for a long period
until it encounters a new and perhaps unique
combination of program parameters that generates the
next error.
15
6.Advantages and disadvantages of
simulation
16
They also list 4 disadvantages:
a) A simulation model may become expensive in terms of
manpower and computer time,
b) Extensive development time may be encountered,
c) Hidden critical assumption may cause the model to
diverge from reality,
d) Model parameters may be difficult to initialize. These
may require extensive time in collection, analysis, and
interpretation.
17
Examples
Example1
The first example we are going to see is the
simulation of a tossing of a fair coin. First step is
analyzing the problem. The fair coin means that
when tossing that coin the probability of head equal
the probability of tail equal 50%. So, using a digital
computer to simulate this phenomenon we are going
to use a uniform Random number generated by the
package you are using or you can write its code.
Uniform random number means that you have a set
of random number between 0 and 1 all with the
same probability. But most languages generate
uniform random number integer from a to b with
equal probability.
18
See the following program written in c++
#include <iostream>
#include <stdlib.h>
void main(void)
{
int x,nuber_or_trials, head=0, tail=0;
float phead,ptail,error_head,error_tail;
cout<<"enter number of trials"<<endl;
cin>>nuber_or_trials;
for(int i=0;i<nuber_or_trials;i++)
{
x=random(2);
if (x==1) head++;
else ++tail;
}
phead=head*1.0/nuber_or_trials;
ptail=tail*1.0/nuber_or_trials;
error_head = abs(((0.5 - phead)/0.5)*100);
error_tail = abs(((0.5 - ptail)/0.5)*100);
cout<<"probability of head= " <<phead<<"
with error ="<<error_head<<"%"<<endl;
cout<<"probability of tai = "<<ptail<<" with
error ="<<error_tail<<"%"<<endl;
cin>>x;
}
19
The output results are:
20
Example2
Get the average daily demand for a small grocery store
selling a fresh bread according to the following table:
Device to generate
Daily demand D Probability Of chance outcomes
demand P(D) Number and color
of balls
100 0.20 20 Red
110 0.50 50 Blue
120 0.30 30 Yellow
21
To simulate the daily demand (5 days)
Draw one ball at a time, notice its color and then place
it back in the bowl. Then translate the outcomes into
unique values of demand.
22
Example3
Use the one-digit Random Number (6, 3, 5, 0, 8)
to generate random observations for :
a)Throwing an unbiased coin,
b)Throwing a die,
c)The color of a traffic found by a randomly arriving car
when it is green 40% of the time, yellow 10% of the
time, and red 50% of the time.
23
Solution
R.Observations
(6, 3, 5, 0, 8) (6,3,5,reject,reject)
24
c) Divide 0 – 9 into 3 classes according to the given
probability:
o Green 40% 0, 1, 2, 3
o Yellow 10% 4
o Red 50% 5, 6, 7, 8, 9
R.Observations
So (6,3,5,0,8) (Red,Green,Red,Green,Red)
25
Probability and Distribution Theory
Definition1 A random variable is a quantity whose value
is determined by the outcome of a Random
Experiment ,i.e., the Random Variable X is a function
whose domain is the event space (set of all possible
outcomes of a random experiment) and whose range is
some subset of the real numbers.
X : E R
Set Theory and compound Events
Definition2
An event is some subset of the event space of a
random experiment.
Ω : Event space or the collection of all the possible outcomes of
the experiment.
Example
Consider the experiment of tossing two dice and noting
the sum of the faces. The event space is Ω = {2, 3, 4,
5, 6, 7, 8, 9, 10, 11, 12} and some events are
E1 = {2} E2 = { 3, 4, 5} E3 = {2,4,6,8,10.12} (an even
number appears.
Definition3
The Complement of an event E is Ē which contains the
set of elements that are in Ω but not in E.
Note that: E + Ē = Ω , E * Ē = Φ (null set )
26
Example
Ē1 = {3,4,5,6,7,8,9,10,11,12}
Ē2 ={2,6,7,8,9,10,11,12}
Ē3 = {3,5,7,9,11}
Definition4
Let a random experiment has N equally likely
outcomes. Let some event E is composed of n
outcomes, so the probability of E is denoted by
P(E) = n/N , P(Ē ) = 1 – P( E ) = (N – n )/N
Note : P(Ω ) = 1 , P( Φ ) = 0.
Definition5
The Union of two events E1 and E2 is denoted
by E1 + E2 and it is defined as the outcomes in
either E1 OR E2 OR both.
The intersection of two events E1 & E2 is denoted by
E1 . E2 and is defined as the outcomes that the
events have in common .Two events that have no
outcomes in common are said to be
mutually exclusive.
27
Discrete Distribution
P(x)
6/21
5/21
4/21
3/21
2/21
1/21
x
1 2 3 4 5 6
28
The properties that every discrete probability
function must have :
1) P(xi ) ≤ 0 for all x It is called probability density
function (p.d.f.)
2)
P(x ) 1
i 1
i
F(x)
3/4
1/2
1/4
1 2 3 4 5 6
29
The CDF has the following properties:
1) 0 ≤ F ( X ) ≤ 1 - ∞ <x<∞
2) If X1 ≤ X2 F ( X 1) ≤ F ( X 2) that is F is
monotonically increasing.
3) Lim F ( X ) =F(∞) = 1
x ∞
Lim F ( X ) =F(-∞) = 0
x -∞
E(X) = X P( X
i 0
i i
)
30
Variance
V ( X ) [ xi E ( X ) ] 2 P ( x i )
i 0
E [ ( X E ( X ) )2 ]
E ( X 2 ) ( E ( X ) )2
1/ 2
Standard Deviation of X is given by S(X) =
V( X )
For the above example
S(X)=[2.222]^1/2 b= 1.4907
31
Continuous Distribution
• 1- f(x) =0 if x is not in the range of X,
• 2- f(x) ≥0,
• 3- f ( x)dx 1
X1
• 4- F(x1) = f ( x)dx
• 5- E(X) = xf ( x)dx
2
( x E ( X )) f ( x)dx
• 6- V(X ) =
2 2
x f ( x ) dx ( E ( X ))
• =
32
Function of a Random Variable
• E(Y) = E(aX+b)
• =
(ax b) f ( x) dx a xf ( x)dx b f ( x)dx
• So E(Y) = aE(X) + b
• V(X) = V(aX + b)
33
a x f ( x)dx 2ab xf ( x)dx b
2 2 2
f ( x ) dx ( E ( aX b ) 2
a 2 x 2 f ( x)dx 2ab E ( X ) b 2 ( E (aX b) 2
a 2 x 2 f ( x)dx a 2 ( E ( X )) 2
a 2 [ x 2 f ( x)dx ( E ( X )) 2 ]
a2 V ( X )
34
Some Common Distribution
1) Bernoulli Distribution
It is a discrete distribution having only two
outcomes. Let X has been defined and it takes
the value o with probability p and 1 with
probability q=1-p i.e.,
X p(x)
0 p
1 q
CDF = F(x) = 0 x< 0
p 0≤x
<1
1 x≥1
μ = E(X) = 0.p + 1.q = q
2 V ( X ) E ( X 2 ) ( E ( X ) ) 2
0. p 1.q q 2 q (1 q ) pq
( pq )1 / 2
35
p(x) F(x)
q 1
p p
x x
0 1 0 1
Example
Consider the experiment of tossing a fair coin. Let X
assume the value o if a head appears and 1 for tail.
Then p = q = ½ and μ(mean) = p = ½ &
2 (V ( X )) pq 1 / 4
36
Uniform Distribution
f(x)
Let variable X has a uniform distribution
between a and b X ≈ U(a,b)
f(x) = 1/(b-a) a≤x≤b a b
= 0 otherwise
•Rectangular distribution
•The interval [a,b] is called the range of F(x)
the distribution 1
•CDF is given by:
x
F(X ) f (t )dt 0 xa
0
xa a
a xb b
ba
1 xb
ab 2(b a ) 2
Mean Var .
2 12
37
Poisson Distribution
It is a discrete distribution.
( t ) k e t
P( X = k ) = PX ( k )
k!
k e
k 0 k!
1
Mean λ Variance = λ
38
Exponential Distribution
y
1
e
f x ( y)
1
fy(x)
0
y
39
y y
1
x
f ( y ) dy 0 e
dy e
1
0
1 1
E( X ) , 2 V (X )
2
CDF Fx (b) f
x ( y )dy
0 b 0
y b
1
0 e dy 1 e
b 0
40
Normal Distribution
( y )2
1 2 2
f x ( y) e y
2
it is called a N ( , ) i., e., it has two parameters.
fy(x)
x
μ
∞- ∞
41
( y )2
1
Fx (b)
2
e 2 2
dy
z2
1
f ( z) e 2
( 0 , 1)
2
42
Variance Reduction Techniques
1 Increase sample size N where
V = E(x2) – (E(X))2 V 1/N
Examples
use f(x) = e –x it is exponential distribution with mean
λ=1 i.,e.,F(X)= 1 – e –x
i R.N. Xi = - ln ( 1 - ri )
1 0.495 0.684
2 0.335 0.408
3 0.791 1.568
4 0.469 0.633
5 0.279 0.326
6 0.698 1.199
7 0.013 0.014
8 0.761 1.433
9 0.290 0.343
10 0.693 1.183
F(X) = X2 = r So Xi = + ri0.5
Ri Xi = ri0.5
0.628 0.792
0.207 0.455
0.662 0.814
0.774 0.880
0.360 0.600
0.951 0.975
TOTAL= 4.516 Estimated mean = 4.516/6 = 0.753 with
error = 13%
45
Using Complement Random Variables:
ri ŕ=1-ri xi= ri , (1 ri )
0.628 0.792
0.372 0.61
0.207 0.455
0.793 0.891
0.662 0.814
0.338 0.581
46
Introduction to Queuing Theory
47
:The items of interest concerning any queuing model
48
n
n 0
p n 1 ( ) p0
n0
1 1
p0
n
n 0
(
)
n 0
n
1
p0 1
1
1
pn (1 ) n for n 0,1,2,3,...
L 1,
1
2
Lq 1
1
49
• Expected number in the system (L)
• L = E(X)
xp n x (1 ) x
x 0 x 0
L (1 ) x x
x 0
(1 ) x x 1
d x
(1 )
d x 0
d 1
(1 )
d 1
1
(1 )
(1 ) 2
L 1 i., e.,
1
50
• Expected Number in the queue Lq
Lq op0 ( x 1) p x
x 1
xp x p x
x 1 x 1
L (1 p0 )
L 1 p0
11
1
2 2
Lq 1, i.e.,
1 ( )
51
Using Little’s formula
L
W
1
1
Wq W
( )
52
Random Number Generation (Pseudo R.N.)
Ri+1 = ( a Ri + c ) modulo T
where
53
Example
For a = 1, c = 4, T = 5, R0= 2(seed); generate 10 random
numbers.
Solution
R0 = 2
x
1
F(x)
R1
R2
R3
0
x1 x2 x3 x
56
Illustrative Example:
57
Insurance Example
time(10000)
time(20000)
1/3 of
1/6 of
Probability of No sale=0.75
Prob. of sale=0.25
Theoretical Solution:
Probability of No Sale = 0.75
Probability of Sale = 0.25
Pro(10000 policy | sale) =2/3 = 0.667
Pro(20000 policy | sale) =1/3 = 0.333
Expected value = 0.667x10000 + 0.333x20000
= 13333 L.E.
58
Simulation of 20 insurance calls
Trial Flip Interest Not Roll 0 10 20 Value
Interest
1 H X 4 X 10,000
2 H X 4 X 10,000
3 T X
4 H X 1 X
5 H X 2 X
6 T X
7 H X 3 X
8 H X 6 X 20,000
9 T X
10 T X
11 H X 2 X
12 T X
13 H X 3 X
14 T X
15 H X 6 X 20,000
16 H X 4 X 10,000
17 H X 2 X
18 H X 4 X 10,000
19 T X
20 H X 6 X 20,000
59
From the table, 13 of the visits will result in a “No sale”.
P (No sale) = 13/20 = 0.65
Given Sale, we have 4x10,000 and 3x20,000 policies
soled so
P(10000) = 4/7 = 0.5714
P(20000) = 3/7 = 0.4286
The Expected Vale = 0.5714 x 10000 + 0.4286 x 20000
= 14,286 L.E.
60
Time Management Methods
There are two time references used in simulation:
1) Simulation Time: The period of time simulated by
the model – 30min., 5years, 100 years whatever
interval the researcher is interested in. This time is
reset to zero at the beginning of the simulation run
and acts as a counter to the number of simulation
time units.
2) Run time: The time that the computer takes to
simulate the period of interest.
61
The simulation clock is then advanced another unit,
and the process is repeated. In this technique, the
exact time of the occurrence of particular event is
largely ignored. All events that occur during a given
interval are treated as if these events occurred at the
end of that interval.
Example :
Consider the simulation of a single-bay-
service station for a period of ten minutes. Assume
that the system is empty at the beginning and at the
end of the period. Assume also that four customers
are serviced, their arrivals occur at simulation times
1.8, 3.2, 6.1 and 7.4, and that service completions
occur at simulation times 2.6, 4.8, 7.3 and 8.1.
If a time interval of one minute is used, arrival events
are denoted by A1, A2, A3 and A4. While completion
events are denoted by C1, C2, C3 and C4(see the
following figure.).
62
A1 C1 A2 C2 A3 C3 A4 C4
1.8 2.6 3.2 4.8 6.1 7.3 7.4 8.1
S0 S1 S2 S3 S4 S5 S6 S7 S8 S9 S10
C3 and A4 are both considered to have occurred at clock
time 8.0; although C3 actually occurred before A4.
63
Thus the actual run time of the simulation and hence
the cost of simulation is increased. So there is a trade-
off between the precision and maximum run time.
ii ( The Event Scan
In this technique the clock is advanced by
the amount necessary to trigger the occurrence of the
next event. Thus the time advance intervals are of
variable lengths.
While some information can be lost in the
periodic scan, the event scan avoids this problem;
because the clock is advanced only to the next
occurrence time.
In periodic scan, the exact occurrence time
of events is not known, because each event is treated
as if occurred at the end of the time interval in which
it occurred.. This problem is overcome in Event Scan
approach.
64
Example
A bank of machines in a manufacturing shop breaks down according to the
following inter arrival time distribution. The time it takes one repair person
to complete the repair of a machine is given in the service time distribution.
Inter arrival P(X) Mapping Service P(X) Mapping
time (hours) of R.N. Time (hours) of R.N.
0.5 0.30 00 – 29 0.5 0.25 00 – 24
1.0 0.22 30 – 51 1.0 0.20 25 – 44
1.5 0.16 52 – 67 2.0 0.25 45 – 69
2.0 0.10 68 – 77 3.0 0.15 70 – 84
3.0 0.14 78 – 91 4.0 0.10 85 – 94
4.0 0.08 92 – 99 5.0 0.05 95 – 99
1.00 1.00
65
Solution
RN Interarrival Time of RN Service Repair Man1 Repair Man2 Waiting
Down
Time Arrival Time On Off On Off Time Time
+
30 1.0 1.0 81 3 1 4 0 3
+
02 0.5 1.5 91 4 1.5 5.5 0 4
+
51 1.0 2.5 08 0.5 4 4.5 1.5 2
+
28 0.5 3.0 44 1 4.5 5.5 1.5 2.5
86 3.0 6.0 84 3 6 9 0 3
66
Example for a continuous distribution:
Simulate a system M/M/1 with arrival rate λ = 20/h
and service rate μ = 25/h. Assuming system
empty at time zero.
•Using time scan (periodic scan)
•Event scan
Solution:
67
Time Customer Random number Arrival Departure
in min. In system Arrival Departure Yes/No Yes/No
0 0 0.096 - Yes -
1 1 0.569 0.665 No No
2 1 0.764 0.842 No No
3 1 0.492 0.224 No Yes
4 0 0.950 - No -
5 0 0.610 - No -
6 0 0.145 - Yes -
7 1 0.484 0.552 No No
8 1 0.350 0.59 No No
9 1 0.450 0.410 No Yes
10 0 0.802 - No -
68
2. Using Event Scan (Event Driven):
The time until next arrival is given by
ln(1 R ) 1
X arr . min .
3
3 ln(1 R )
12 ln(1 R ) 5
X dep. min .
5 12
69
Time in Event Customer Random Number Min. until next Next
min. In system Arrival Departure Arr. Dep. Event
0 0 0.096 - 0.303 - A
0.303 A 1 0.569 0.665 2.525 2.625 A
2.828 A 2 0.764 - 4.332 0.1 C
2.928 C 1 - 0.842 4.232 4.428 A
7.160 A 2 0.492 - 2.032 0.196 C
7.356 C 1 - 0.224 1.836 0.609 C
70
Example for discrete distribution:
Two-stage assembly line are producing a large product. There
is no room in between the two line ; so the output of the first
line is fed to the second line directly else the first line must
wait until second line to be free. The service distribution for
both line are given below:
Service Time Freq. R.N. Time Freq. R.N
seconds for 1st line Allocation for 2nd line Allocation
10 4 00 – 03 4 00 - 07
20 6 04 - 09 5 08 - 17
30 10 10 – 19 6 18 - 29
40 20 20 – 39 7 30 - 43
50 40 40 – 79 10 44 - 63
60 11 80 – 90 8 64 - 79
70 5 91 – 95 6 80 - 91
80 4 96 - 99 450 92 - 99
100
71
Item First Line Waiting Second Line
Time
No. R.N. Start Service Finish R.N. Start Service Finish
Time Time Time Time Time Time
1 56 00 50 50 83 50 70 120