You are on page 1of 44

Introduction to Simulation

What is Simulation?

Simulation is the process of designing a model of a real


system and conducting experiments with this model for the
purpose either of understanding the behavior of the system
or of evaluating various strategies … for the operation of
the system” (Shannon, 1975).

 Study the dynamic behavior of a system


 System is represented by a model
 Usually implemented as a computer program

2
System

Collection of entities that interact with each other within


some boundary
‒ Physical
‒ Real

Entities
‒ Individuals, companies, machines, vehicles

Boundaries
‒ Physical, temporal or other
3
Model

 A mathematical abstraction which describes the behavior of a


system

 Purpose
‒Evaluate impact of
• Changes in input values
• Manipulations of the system
‒Decision support, control

 Physical model - building, bridges, etc.

 Mathematical model - Analytical queueing models, linear


programs, simulation
4
Need for Modeling

Alternative to experimentation with real system


Evaluation of alternatives
Hypothetical and conceptual systems
Expensive
Uncontrolled variables
Extreme and rare events
Safety issues

5
Modeling a System

Imitate
Input Input

Imitate
System Model

Evaluate
Output Output

6
Role of Simulation

Simple models
‒ Analytic solution

Large scale complex systems


‒ Analytical solution not tractable
‒ Physical model infeasible
• Human behavior
• Conceptual systems
‒ Solution via simulation

7
System Modeling Approaches

System

Experiment with Experiment with a


actual system model of the system

Physical Mathematical
model model

Analytical Simulation

8
Characteristics of simulation models

Role of time

Representation of system

Deterministic or stochastic

9
Role of time

Static
‒ Time plays no role or one point in time
‒ Steady state or equilibrium conditions

Dynamic
‒ System state changes over time

10
Representation of time

 Continuous
‒ Variables change states continuously with time
‒ e.g., movement (speed) of a vehicle
‒ Must be discretized in computer implementation

 Discrete event
‒ Variables change states instantaneously at discrete points
in time
‒ e.g., model of the length of a supermarket queue,
operations of a traffic light

11
Deterministic or stochastic
 Deterministic
‒All inputs and processes deterministic

 Stochastic (Monte Carlo)


‒Introduce randomness in
• Inputs
• Models

 Implications
‒Output is random variable
‒Each run produces different results
• Interpretation of the results requires multiple replications
12
Traffic Simulation

 Traffic simulation or the simulation of transportation systems is


the mathematical modeling of transportation systems (e.g.,
freeway junctions, arterial routes, roundabouts, downtown grid
systems, etc.) through the application of computer software to
better help plan, design, and operate transportation systems
(Leonard 2008)

 https://www.traffic-simulation.de/

13
Random Number

 Random numbers are numbers that occur in a sequence that


cannot be reasonably predicted better than by a random chance

 Random number tables are used

14
True Random Number (TRN)

 True random number are generated from a physical process,


rather than by means of an algorithm

 These random numbers are generated by devices and are


often based on microscopic phenomena
 statistically random "noise" signals, such as thermal noise,
the photoelectric effect, involving a beam splitter, and
other quantum phenomena

 These stochastic processes are completely unpredictable

15
Pseudo Random Number (PRN)

 Random numbers are very difficult to generate, especially on


computers which are designed to be deterministic devices

 Pseudo-random numbers
‒ Random” numbers produced on a computer or calculator are
not random at all
‒ Generated using a formula, so that the sequence produced is
actually predetermined
‒ Generated based on some initial value known as “SEED”
number

16
Properties of Random Number

 Long period : Generator should be of long period (the period of a


random number generator is the number of times we can call it
before the random sequence begins to repeat)

 Fast computation: Generator should be reasonably fast and low cost

 Unbiased: The output of the generator has good statistical


characteristics

 Unpredictable: Given a few first bits, it should not be easy to predict,


or compute, the rest of the bits.

 Uncorrelated sequences - The sequences of random numbers should


be serially uncorrelated 17
Random Number Generation
 Random Number, Ri, must be independently drawn from a
uniform distribution with pdf:

1 , 0 x 1
f(x) = 0 , otherwise
1
E ( R )   xdx  [ x 2 / 2]10  1 / 2
0
1
V ( R )   x 2 dx  [ E ( R )]2
0

 [ x 3 / 3]  (1 / 2) 2  1 / 3  1 / 4
1
0
 1 / 12 18
Generation of Pseudo Random Numbers

 Pseudo”, because generating numbers using a known method


removes the potential for true randomness

 Goal: To produce a sequence of numbers in [0,1] that simulates,


or imitates, the ideal properties of random numbers (RN).

19
Techniques for generating PRN

Multiplicative Congruential Method

Xi+1 = a Xi (mod m), where a  0 and m  0

 Most natural choice for m is one that equals to the capacity of a


computer word

 m = 2b (binary machine), where b is the number of bits in the computer


word

 m = 10d (decimal machine), where d is the number of digits in the


computer word
20
Techniques for generating PRN

Linear congruential Method


 To produce a sequence of integers, X1, X2, … between 0 and m-1 by
following a recursive relationship:

 The selection of the values for a, c, m, and X0 drastically affects the


statistical properties and the cycle length.

 The random integers are being generated [0,m-1], and to convert the
integers to random numbers:
21
Example 1

 Use X0 = 27, a = 17, c = 43, and m = 100

 The Xi and Ri values are:


• X1 = (17*27+43) mod 100 = 502 mod 100 = 2, R1 = 0.02;
• X2 = (17*2+43) mod 100 = 77, R2 = 0.77;
• X3 = (17*77+43) mod 100 = 52, R3 = 0.52;
• X4=(17*52+43) mod 100 = 27, R4= 0.27;

22
Example 2

 Let us consider a simple example with a = 5, c = 1, m = 16, and X0 = 1

 The period (the number of integers before the sequence repeats) P is


16 - exactly equal to the modulus, m

 Note that the period is exactly equal to 24, i.e. 2m, where m is the
base 2 log of the modulus
23
Example 3
 Consider a = 5, c = 0, m = 37, and X0 = 1

 We obtain the sequence

1,5,25,14,33,17,11,18,16,6,30,2,10,13,28,29,34,22,36,32,12,23,4,
20,26,19,21,31,7,35,27,24,9,8,3,15, 1, 5, 25, ...

 we use a prime number as the divisor for the modulus operation


and c = 0, we obtain a period one less than modulus 37 (i.e. 36)

 Period is m-1, even if c is not equal to zero

 Thus, for linear congruential generators with a prime modulus,


using a non-zero c does not increase the period 24
Techniques for generating PRN (contd..)

The max period(P) is:

 For m a power of 2, say m = 2b, and c  0, the longest possible period


is P = m = 2b , which is achieved provided that c is relatively prime to
m (that is, the greatest common factor of c and m is 1), and a = 1 + 4k,
where k is an integer.

 For m a power of 2, say m = 2b, and c = 0, the longest possible period


is P = m / 4 = 2b-2 , which is achieved provided that the seed X0 is odd
and the multiplier, a, is given by a = 3 + 8k or a = 5 + 8k, for some k = 0,
1,...

 For m a prime number and c = 0, the longest possible period is P = m -


1, which is achieved provided that the multiplier, a, has the property
that the smallest integer k such that ak - 1 is divisible by m is k = m 25- 1,
Random Number Generation

 Commonly used parameters

m a c
Borland C++ 232 22695477 1
Visual C++ 232 214013 2531011
Visual basic 224 1140671485 12820163
Java 248 25214903917 11
Random variables

 A variable that can take alternative values


‒ Discrete, continuous

 Probability (mass) density function (pdf)


b

PX  x   p  X  x  P  a  X  b    f X  x  dx
a

 Cumulative distribution function (cdf)

FX  x   p  X  x  p  X  x   1  FX  x 
p  a  X  b   FX  b   FX  a 
27
Mean and variance

 The mean

  E  X    xi p ( xi ) or  xf ( x)dx
i -

 The variance
‒ Measure of the dispersion of a RV about its mean

  Var ( X )  E  X  E  X    E  X 2    2
2 2

28
Example

• Number of vehicles at a parking lot

Vehicles 1 2 3 4

Probability 1/6 1/3 1/3 1/6

1 1 1 1
   xi p ( xi )   1   2 +  3   4  2 12
i 6 3 3 6

1 2 1 2 1 2 1 2  3
  E  X    =  1   2 +  3   4    2 2  
2
 2
 2 1 2

6 3 3 6  4
29
Probability mass function

p(x)
0.35

0.30

0.25

0.20

0.15

0.10

0.05

x
0.00
1 2 3 4

30
Cumulative distribution function

F(x)
1.0
0.9

0.8

0.7

0.6
0.5

0.4

0.3

0.2

0.1
0.0 x
0 1 2 3 4 5

31
Discrete Distribution: Poisson

 Discrete RV
 k e
pK (k ) 
k!
E(K )  
Var ( K )  

 μ - rate of events in time interval


 Modeling the number of random events that occur during a given
time interval
‒ Cars arriving at an intersection
‒ Passenger arrivals at bus stops
32
Continuous Distribution: Exponential

e  x  x0 1  e  x x0


f X ( x)   
FX ( x)  Pr ob( X  x)  
0 otherwise 0
 otherwise

E( X )  1/ 
Var ( X )  1 / 2
f(x)
f F(x)
λ(
1
x
)

PDF CDF
0

34
x 0 x
Continuous Distribution: Normal

Parameters μ and σ, - <μ <  and 0 < σ


1
1  ( x   ) 2

f ( x)  2
  x  
2
e
2
f (x )

0.35

0.30

0.25

0.20

0.15 Area =
0.10
0.68
0.05

0.00 x
-3.0 
-2.0  -1.0 
0.0 1.0   2.0 3.0
35
Drawing random variables

 Critical for realization of


‒Inputs
‒Models

 Two-step process
‒Random numbers generator
‒Random variables from a distribution

36
 How does one transform a sample of the uniform[0,1] random
variable into a sample of a given distribution ?

 Methods
‒Inverse transformation
‒Relationships
‒Acceptance -rejection

37
Inverse Transformation Method: Discrete variables

 Want to generate a discrete random variable X with pmf

 Consider the following algorithm


Inverse Transformation Method: Discrete variables

• Suppose x1<x2------------xm, then CFDF of X, FX is

39
Discrete Distribution: Example

 Want to simulate the random variable with the pmf

 Algorithm

40
Inverse Transformation Method: Continuous variables

 Generate a continuous random variable X ~ 𝐹 as follows:

 Algorithm
‒Get the CDF of random variable, F(X)
‒If only PDF is given, get CDF
‒Generate a uniform random number, U
‒Set F(X)=U
‒Solve for X in terms of U, X = 𝐹 −1 (𝑈)
Continuous Distribution: Example (Exponential)

 PDF = f(x)= λ𝑒 −λ𝑥

 CDF, F(x)=1-𝑒 −λ𝑥

 Generate a random number, U

 Set F(x)=U -> 1-𝑒 −λ𝑥 =U


-> 𝑒 −λ𝑥 = 1-U
-> - λ𝑥 =ln(1-U)
-> 𝑥 =-ln(1-U)/ λ

 If U is uniform, 1-U is also uniform

1
 Therefore, 𝑥 = − λ ln(𝑈)
Continuous Distribution: Example (Exponential)

 PDF = f(x)= λ𝑒 −λ𝑥

 CDF, F(x)=1-𝑒 −λ𝑥

 Generate a random number, U

 Set F(x)=U -> 1-𝑒 −λ𝑥 =U


-> 𝑒 −λ𝑥 = 1-U
-> - λ𝑥 =ln(1-U)
-> 𝑥 =-ln(1-U)/ λ

 If U is uniform, 1-U is also uniform

1
 Therefore, 𝑥 = − λ ln(𝑈)
Empirical Continuous Distribution

 When theoretical distributions are not applicable, empirical distribution is used


 Steps:
1) Collect empirical data from field
2) Arrange the data from smallest to largest
x1≤x2 ≤ x3-------- ≤xn , xi = data
3) Group the data into different intervals
4) Assign probability value of 1/n to each interval , where n=no. of intervals
xi-1≤X ≤ xi , where X is a random variable follows empirical distribution
5) Find X using the following formula:
X=F-1(R), Where R is a random number (0-1)-. We are using inverse transformation
technique
𝑖−1 𝑖−1 𝑖
X=F-1(R) X= 𝑥𝑖−1 + 𝑎𝑖 𝑅 − when ≤R≤
𝑛 𝑛 𝑛
𝑥𝑖 −𝑥𝑖−1
Where 𝑎𝑖 =
1/𝑛
44
Example

45

You might also like