Matlab Simulation
Matlab Simulation
Dr. Dessouky
Description
Simulation is a very powerful and widely used management
science technique for the analysis and study of complex systems.
Simulation may be defined as a technique that imitates the
operation of a real-world system as it evolves over time. This is
normally done by developing a simulation model. A simulation
model usually takes the form of a set of assumptions about the
operation of the system, expressed as mathematical or logical
relations between the objects of interest in the system.
Simulation has its advantages and disadvantages. We will focus
our attention on simulation models and the simulation technique.
Simulation
What is simulation:
The process of designing a mathematical
or logical model of a real-system and then
conducting computer-based experiments
with the model to describe, explain, and
predict the behavior of the real system.
Simulation
Where simulation fits in
Programming
Simulation
Analysis
Modeling
Probability &
Statistics
Basic Terminology
In most simulation studies, we are concerned with
the simulation of some system.
Thus, in order to model a system, we must
understand the concept of a system.
Definition: A system is a collection of entities that
act and interact toward the accomplishment of some
logical end.
Systems generally tend to be dynamic their status
changes over time. To describe this status, we use
the concept of the state of a system.
Value of Simulation
Empirical Method verses mathematical
model
Allow you to calculate the extreme values
not just the expected value
Simulation
What is simulation
Simulationistheactualrunningofthe
modelsystemtogaininsightintoits
performance.
Simulation
Why use simulation
Simulationisusedtobetterunderstandthe
expectedperformanceoftherealsystem
andtotesttheeffectivenessofthesystem
design.
Simulation
Why use simulation
Without building them
experimental system
new concepts
Queuing systems
Performancemeasures(output)
Datarequirements(input)
Usesofmodel
Kendallsnotation
Queuing systems
SystemPerformancemeasures(outputs)
Expectednumberofcustomersinsystem
Expectednumberofcustomersinqueue
Expectedtimeinsystem
Expectedtimeinqueue
Serverutilization
Probabilityofncustomersinsystem
Throughput
Queuing systems
Datarequirements(Inputs)
Interarrivaltimedistribution
Servicetimedistribution
Numberofservers
Queuediscipline
Systemcapacity
Sizeofinputpopulation
Kendallsnotation(M/M/s/FCFS/K/M)
Alternativetosimulation
Simulation
Analytic models
Physical experimentation
Visit other sites
Disadvantage:
May not provide you with the optimal solution
time to construct model will be longer.
High Speed
Not disruptive
Replication easy
Control variations
Generally less costly
Disadvantage:
Realism
Validity
Cost
V
Representing system
System:
a collection of mutually interacting objects
designed to accomplish a goal (machines
repair system)
Entities:
denotes an element/object within boundary of
system (machines, operators, repairman)
Entity work being performed on object
Resource performing the work
Representing system
Attribute:
Characteristic or property or an entity
(machine ID, Type of breakdown, time that
machine went down)
Activity:
transforms the state of an object usually over
some time (repairman service time, machine
run time)
Representing system
State of the system:
Numeric values that contain all the
information necessary to describe the system
at any time.
Delays:
Processes that take a conditional length of
time in the system
Representing system
Events:
Change the state of the system(end of service
of machine,machine breaks down)
Queue:
it is set, used to model waiting
Sets
People waiting at each floor
Attributes
Elevators capacity, speed, destination,
current location of each elevator
People inter-arrival time at each floor,
destination of each people
Activities
Load/Unloading passenger
Travel to next floor (speed and distance)
Persons travel to elevator
Events:
Elevator arrival
End unloading
End Loading
Person Arrival
Programming
Look at system only when events occur; time is
advanced from event to event.
Programming:
Advances time in small intervals. Use differential
equations to represent flows.
An Example of a Discrete-Event
Simulation
To simulate a queuing system, we first have to
describe it.
We assume arrivals are drawn from an infinite
calling population.
There is unlimited waiting room capacity, and
customers will be serve in the order of their arrival
(FCFS).
Arrivals occur one at a time in a random fashion.
All arrivals are eventually served with the
distribution of service teams as shown in the book.
Event scheduling
Time is advanced from event to event
Future events list ordered list of
upcoming events
As events are scheduled, they are added to the
list
As events occur they are removed from list
Event scheduling
List is required to keep track of entities in
a set
Statistics Two types
Sample statistics average of some values
(W)
W = (W1 +W2 + +Wn)/n = Total Wait / # of wait
Activity scanning
Activity scanning
Time is modeled in fixed time increments to
check if activity occurred
Small time increments is inefficient
Large time increments may miss activity
describes the activities in which the entities in
the system engage.
Process Oriented
Process oriented:
Many simulation models include elements
which occur in defined patterns
The logic associated with such a system or
events can be generalized and defined by a
single statement
A simulation language could then translate
such statement into the appropriate sequence
of events
describes the processes through which the
entities in the system flow.
Process Oriented
Process oriented:
These statements, define a sequence of events
which are automatically executed by the
simulation language as the entities move
through the process
Create arrival entities every t time units
However, since we are normally restricted to a
set of standardized statement, provided by the
simulation language, our model flexibility is
not as great as with the event condition
Simulation Languages
One of the most important aspects of a simulation
study is the computer programming.
Several special-purpose computer simulation
languages have been developed to simplify
programming.
The best known and most readily available
simulation languages, including GPSS, GASP IV
and SLAM.
Most simulation languages use one of two different
modeling approaches or orientations; event
scheduling or process interaction.
Problem/Model Formulation
State the objective of the study.
Identify the Problem. Determine any underlying
causes if possible.
Determine the input variables.
Controllable Variables.
Uncontrollable Variables.
Data collection/acquisition
Determine the Data Collection System or
Estimates to be used.
Model Construction or
Development
Identify The Real System
Determine Conceptual Model -Activities
and Events
Develop the Logical Model.
Identify the Programming Language used.
Computer Implementation (Promodel,
Arena, Slam Systems).
Model Construction or
Development
Modeling Tips
1, 3, 5, 9,13,15,17
Data Collection
Activities may be represented as
Constants
Random variables
Collection of data
Design a data collection form
Record more than single attribute in case you
need to use data in a different way.
Use several session to get representative data
Use control charts
Data Collection
Machine
Time
Elapsed
Data Collection
Testing data
Independence
Randomness
Homogeneity
Data Collection
Test of Independence
Ho: Measure A is independent of measure B
H1: Measure A is not independent of measure
B.
Inventory and day of week
Data Collection
Test of Randomness
Ho: f(xi/xj) = f(xi) =Independent
Hi: f(xi/xj) f(xi) : Dependent
For example, when simulation a production
process in which the items can be defective or
good, it would be important to know if
successive items are randomly distributed with
reputation good items followed by some of
defective items.
Data Collection
Test of Homogeneity
Tests for whether multiple sets of data can be
considered as coming from statistical
population are generally referred to as tests of
homogeneity distribution free.
Ho : G(x) =H(x)
H1 : G(x) H(x)
Two different workers working on the same
machine.
Random Variable
Two types
Discrete
Continuous
Random Variable
Probability mass function
Discrete
P(X = xi) = p(xi)
p(xi) = 1
Random Variable
Random Variable
Cumulative distribution function (CDF)
F(X) = P(X <= x)
X<x
p(xi)
- x f(x) dx
Random Variable
Expected value
= E(x)
= xi p (xi)
= x f(x) dx
Random Variable
Variance
V ( x ) E[( x ) 2 ]
E[ x 2 2 x 2 ]
E
x ( E ( x))
2
i
p ( xi ) (
xi p( xi))
Random Variable
Standard deviation
SD ( X )
Sums of R.V.
V (X )
Y a1 x1 a2 x2
E ( y ) a1 E ( x1 ) a2 E ( x2 )
2
V (Y ) a1 V ( x1 ) a2 V ( x2 )
Random Variable
SampleMean X
SampleVari ance S
(X X )
n 1
2
i
nx
n 1
Exponential Distribution
Previously, we discussed the Poisson random variable,
which was the number of events occurring in a given
interval. This number was a discrete r.v. and the
probabilities associated with it could be described by the
Poisson Probability Distribution.
Not only is the number of events a r.v., but the waiting
time between event is also a random variable. This r.v. is a
continuous r.v. for it can assume any positive value.
This r.v. is an exponential r.v. which can be described by
the exponential distribution.
Exponential Distribution
e x
x 0& 0
i. Pdf: f ( x)
otherwise
0
where = rate at which events occur
ii. Correspondingly,
x
F ( x) P ( X x) e x dx 1 e x , x 0
0
1
V (X ) 2
E[ X ]
a xb
f ( x) b a
0
otherwise
F ( X ) P ( X x)
f ( x)dx
x x
x
a
xa
f ( x)dx
ba a ba ba ba
1
ba
E[ X ] xf ( x)dx x(
)dx
ba
2
(b a ) 2
V (X )
12
bxc
(c b)(c a )
0
elsewhere
xa
( x a) 2
F ( x)
(b a )(c a )
(c x ) 2
1
(c b)(c a )
1
xc
a xb
bxc
Normal Distribution
It is a fact that measurements on many random variables will follow a bellshaped distribution.
Random variable of this type are closely approximated by a Normal
Probability Distribution.
A continuous r.v. X is said to have a normal distribution if the pdf of X is
f ( x)
1
2
( x )2
2 2
, 0, x ,
Normal Distribution
The Standard Normal Distribution
To compute P(a x b) when X ~ N(, 2), we must evaluate
b
f ( x)dx
a
1
2
( x )2
2 2
dx
Normal Distribution
The normal distribution for parameters values
2
=
0,
and
1
2
z2
Normal Distribution
The cumulative distribution of Z is
z
P( Z z )
f ( y)dy
Normal Distribution
Non-standard Normal Distribution
The table only provides probabilities for r.v.
following the N(0,1) distribution. Thus, when X
2
2
~ N(, ), (i.e. not = 0, = 1), probabilities
involving X are computed by standardizing
the r.v. to N(0,1) scale.
Selecting a Distribution
Theoretical prior knowledge
Random arrival => exponential IAT
Sum of large manufactures => Normal CLT
Data Collection
Little variability model as a constant.
Variability model as a random variable.
Empirical vs. Theoretical, Select a
Distribution, Estimate Parameter of
distribution, goodness or fit test.
i
i
TS
k
i 1
K-S test
Compare observed with theoretical CDF
Limited to continuous distribution, known
parameters
H0: Xi are IID r.v. with CDF F(x)
H1: Xi are not IID r.v. with CDF F(x)
Test statistic From table
K-S test
Critical value
A large T.S would cause rejection
Critical value 0.01
1.63 / n
0.05
1.36 / n
0.10
1.22 / n
i 1
i ^
TS max max( F ( Xi )
), max( F ( Xi ))
n
n
Parameter estimation
Set of data
x
xi
n
x1, x2, xm
s
2
2
2
x
n
x
i
n 1
Parameter estimation
Maximum likelihood => find parameter
that max the likelihood of obtaining the
given sample
Produces efficient and consistent estimates
Not always unbiased
Superior properties to methods of moments
Common sense.
Output analysis
Need multiple observations to estimate
variability
Y1, Y2, Y3, . Yn
Estimate a confidence interval for the
measure of performance
Estimate the number of observations
required to obtain the desired precision
Output analysis
What is an observation?
Is observation a sample statistic or time
average statistic?
Is this a steady state simulation or
terminating simulation?
Are the observations independent or
correlated?
Time Based
Average Number in System
Average Number in Queue
Machine Utilization
Terminating simulation
Simulation in which the output measure of
performance is defined over a specific
interval of time with a specific starting
condition and a specific ending condition
Terminating simulation
Has a specified starting and ending
condition.
Each observation must have the same
starting and ending.
Observations are obtained by replication.
Use a different seed for random number
generation.
Y t r 1 ,
(Y
) RY
R 1
1
2
1
R
YR )
R
RS 2
S2
2
R
R
Y t
,
1
R0 1
2
S
R0
,
1
R 1
2
S2
R
If R is large,
r 1
R
R
*S
(Y1 Y2 ) 0
V (Y1 Y2 )
Y1 , s12
Y2 , s 22
R1 R2 2
R1 R2 2
3. Var (Y1 Y2 ) Var (Y1 ) Var (Y2 )
S p2
R1
4. (1 )confident : Y1 Y2 t / 2, R1 R2 2
5. t-test: t
t-crit = t
S p2
S p2
R1
R2
S p2
R2
( y1 y 2 )
Sp
1 1
R1 R2
R1 R2 2 ,
Test Statistic: Z 0
2
2
2
2
1
2
m
n
1 2
m
n
R1 R2
3. (1 )confident : Y1 Y2 t / 2,
S2 S2
1
2
R R
1
2
S2
1
R
1
R1 1
S2
2
R
2
2 1
S12 S 22
R1 R2
d
S d2
R
S d2
V (d )
R
2. Test statistics = F =
3. Critical Value =
S12
S 22
R1 1, R2 1,
4. Example
F =5.4/2.55 = 2.12
= .10, Fcritical = F9,9,.05 = 3.18, can not reject Ho
Observation
Confident Interval
(Y1 Y2 ) t R 1, / 2
V (Y1 Y2 )
Random Numbers
Generation of U(0,1) random number
algorithm used by the RND function
Generation of random variates from
various distributions algorithm used by
EXPONENTIAL, UNIFORM, and so on
(these algorithms use U(0,1) random
numbers.
Multiplicative Generators
Zi = a * Zi-1 mod m
Z0 = seed
Saves an addition, more popular
Multiplicative Generators
C=0
Multiplicative Generators
2b is not a good choice for m
only possible numbers
Let m = 2b - 1
Uniform
Triangular
Exponential
Normal
Poisson
Disadvantage
CDF may not always exist
( x a )2
,a x b
( x ) ( b a )( c a )
ui
( c x )2
1
,b x c
( c b )( c a )
No
Yes
u
i
x a
i
ba
ca
(b a)(c a) ui
ba
ui c a
ba
ui ca
x c
i
(c b)(c a)(1 u i)
Convolution Method
Applicable to situation where the random
variable of interest can be expressed as a
sum of other random variables that are IID
(independent identical distributed)
X=Y1+Y2+Y3. +Yn
Idea: Generate Y1. Yn and add these up
to calculate X
Convolution Method
Normal distribution
Focus: Generating Zi ~ N(0, 1)
xi
Zi
xi Z i ~ N ( , )
Generating Zi
1
f (Z )
e
2
1 2
z
2
Convolution Method
Normal distribution
Generate Ui
Generate Zi
Then Xi Zi
Zi~N(0,1)
Acceptance\Rejection Method
Applicable to distribution functions that
are hard to integrate
Idea
Find a majoring function t(x) where t(x) > f(x)
Sample values of x from t(x) call it x*
Sample Ui < f(x*) / t(x*), accept x*
0x2
otherwise
Step 5: If
r2
f ( x*)
M
1
2
Z1 (2 ln r1 ) sin 2r2
1
2
Z2 (2 ln r1 ) cos 2r2
N
(
t
)
dt
0
ELn EL
i 1
we compute
n
T
where
T = length of simulation
N(t) = number of terminals on-line at time t (0tT)
Ai = area of rectangle under N(t) between ei-1 and ei
(where ei is the time of the ith event)
m = number of events that occur in the interval [0,T]
Between time 0 and time e1, the time of the first
event, the total on-line time for all the terminals is
given by 10ei, since each terminal is on-line for a
period of e1 time units.
and
x 0.125 0.005(1 r ) (0.5 r 1.0)
as the process generators.