Download as pdf or txt
Download as pdf or txt
You are on page 1of 37

Books to Consult

- System Simulation with Digital Computer by Narsingh Deo


- System Simulation by Geoffrey Gordon
- Simulation Modeling and Analysis by Law & Kelton
- Discrete Event Simulation by Jerry Banks

Types of Models

Simulation
Simulation is a dynamic mathematical model that can be solved using numerical techniques. Simulation
problems are only solved by numerical methods. The solution of simulation problems is either not present
or is very hard to find.

Ex: Finding the square root, cube root, integral, differentiation. Since all these things don’t have any
pre-specified formula and these can be solved using numerical techniques and have an analytical solution
that is either not present or is hard to find (common method is iterations) therefore they can be simulated.
Important Definitions

System
A system is defined as an aggregation or assemblage of objects joined in some regular interaction or
interdependence.

Entity
It is used to denote an object of interest in a system.

Activity
Any process that causes changes in the system will be called an activity.

Attributes
It is the properties (characteristics) of an entity.

Examples

System Entity Attributes Activity

Traffic Cars {Speed, Distance} Driving

Bank Customer {Name, Credit, Balance} Deposit / Transfer

Communication Message {Length, Priority} Transmitting

System
In any simulation we have a system whose behavior we are studying. A system is a collection of distinct
objects which interact with each other.

Model
A collection of pertinent information about a system is called a model of the system.

Problem: Simulation of a Simple Inventory System


Suppose you work in a retail store and your responsibility is to replenish certain items like automobile tyres
in the store by ordering it from the supplier. When your stock goes down to P, you have to order Q where:

P - Reorder Point

Q - Reorder Quantity
These are the models/policies that you got to test:

Sr. No. Policy Name P Q


Reorder Point Reorder Qty

1 Policy 1 125 150

2 Policy 2 125 250

3 Policy 3 150 250

4 Policy 4 175 250

5 Policy 5 175 300

Objective: To decide which model/policy is the best.

How To Decide: Based on minimum cost

If the demand of any day exceeds the amount of inventory on hand, the excess represents lost sales and
lost goodwill. On the other hand, over-stocking implies an increase of carrying cost (storage, interest,
insurance, deterioration etc.)

Ordering too frequently will result in excessive reorder cost (transportation, load cost etc.)

Assume the following conditions:

- There is a three day lag between order and arrival. The merchandise is ordered at the end of the
day and is received at the beginning of the fourth day.

- For each unit of inventory the carrying cost for each night is Rs. 0.75

- Each unit out of stock when ordered results in a loss of goodwill worth Rs. 2.00 per unit + loss of Rs
16.00 net income. Lost sales are lost forever.

- Placement of each order costs Rs. 75 regardless of the no. of units ordered.

- Demand in a day can be for any number of units between 0 and 99. Each equally probable.

- There is never more than 1 number of replenishment orders outstanding.

- Initially we have 115 units in hand and no orders outstanding.


Flowchart:

Solution: Here
Problem: Pure Pursuit Problem

A fighter aircraft sights an enemy bomber and


flies directly toward it, in order to catch up
and destroy it. We have to determine the
attack, the attack course of the fighter and
how long does the fighter take to catch up
with the bomber. If the target flies along a
straight line, the problem can be solved
directly with analytic techniques. But if the
path of the bomber is curved, the problem
can not be solved directly. We can solve such
problems using simulation.

We are given following conditions:

- The target & the pursuer are flying on the same horizontal plane. That is why it is a two dimensional
model.
- The fighter speed VF (Velocity of Fighter) is given and is a constant, i.e. VF = 20 Km/minute
- The target path (i.e. its position as a function of time) is specified.
- After a fixed time spent. ƍt (every minute), the fighter changes it direction in order to point itself towards
the bomber. It is given YF(0) = 50 (Y-axis of fighter) and XF(0) = 0 (X-Axis of fighter). Our purpose is to
compute the positions of the pursuer namely XF(t) & YF(t) for t = 1,2,3,...,upto 12 or until the fighter catches
up with the bomber.
- We will assume that once the fighter is within 10 Km of the bomber, the fighter shoots down its target by
fixing a missile and the pursuit is over.
- In case the target is not caught up within 12 minutes, the pursuit is abandoned and the target is
considered escaped.
- From the time t = 0 till the target is shot down, the attack course is determined as follows:
The fighter uses the following simple strategy:
The fighter looks at the target at instant t, aligns its velocity vector with line of sight (i.e. points itself towards
the target). It continues to fly in that direction for one unit of time (up to t+1). At (t+1) it looks at the target
again and realigns itself.

The distance between the two (dist(t)) at a given instant of time can be calculated by using distance
formula.

dist(t) = sqrt((YB[t]-YF[t])² + (XB[t]-XF[t])²)

The angle θ of the line from fighter to target at a given time t is given by:

Sin θ = (YB[t]-YF[t])/dist(t)
Cos θ = (XB[t]-XF[t])/dist(t)

Using these values, next position of fighter at time (t+1) is

XF[t+1]= XF[t] + VF Sin θ


YF[t+1]= YF[t] + VF Cos θ

The following tables illustrate the movements of the bomber for the 12 minutes given:
t 0 1 2 3 4 5 6 7 8 9 10 11 12

XB(t) 80 90 99 108 116 125 133 141 151 160 169 179 180

YB(t) 0 -2 -5 -9 -15 -18 -23 -29 -28 -25 -21 -20 -17

Solution: Here

Exponential Growth Model


𝑘𝑡
Many systems exhibit exponential growth. These systems follow a model of the form 𝑦 = 𝑦𝑜𝑒 where
𝑦𝑜 = 𝑖𝑛𝑖𝑡𝑖𝑎𝑙 𝑠𝑡𝑎𝑡𝑒 of the system. 𝑘 is the positive constant also called growth constant and 𝑡 is known. This
equation can be written as:

𝑑𝑦 𝑑 𝑘𝑡
𝑅𝑎𝑡𝑒 𝑜𝑓 𝑡ℎ𝑒 𝐺𝑟𝑜𝑤𝑡ℎ = 𝑑𝑥
= 𝑦' = 𝑦𝑜 𝑑𝑡
𝑒

𝑘𝑡
𝑦' = 𝑘𝑦𝑜𝑒

𝑦' = 𝑘𝑦

That is, the rate of growth is proportional to the current function value. This is a key feature of exponential
growth.

Population growth is a common example of exponential growth. Consider a population of bacteria, for
instance. It seems plausible that the rate of population growth would be proportional to the size of the
population. After all, the more bacteria there are to reproduce, the faster the population grows.
Table 6.8.1: Exponential Growth of a Bacterial Population

Time(min) Population Size (no. of bacteria)

10 244

20 298

30 364

40 445

50 544

60 664

70 811

80 991

90 1210

100 1478

110 1805

120 2205

Problem 1:
Problem 2:

Problem 3:

More Problem Examples:


Link: 6.8: Exponential Growth and Decay - Mathematics LibreTexts

Monte Carlo Method


A particular numerical computation method called the monte carlo method consists of experimental
sampling with random numbers. For example: in this figure:
In the following figure, a square (side = 1) unit is there/is given. Under the square a quadrant is present
where:

Area of square = 1 x 1 = 1
2
π𝑟 π
Area of quadrant = =
4 4

Then according to the Monte Carlo principle, if we pick points at random within the square and determine
whether they lie beneath the curve (quadrant) or not, it is apparent that providing the distribution of selected
points is uniformly spread over the square, the fraction of the points falling on or below the curve should be
approximately the ratio of the area under the curve to the area of the square.

Where N = Overall Points


And n = Random no. of points that are falling under the curve / quadrant

𝑁 𝐴𝑟𝑒𝑎 𝑜𝑓 𝑠𝑞𝑢𝑎𝑟𝑒 1 4
𝑛
= 𝐴𝑟𝑒𝑎 𝑜𝑓 𝑞𝑢𝑎𝑑𝑟𝑎𝑛𝑡
= π = π
4

4𝑛
π = 𝑁

In this way (by the use of monte carlo method) one can find the value of a deterministic variable "π" using
non-deterministic means.

The accuracy increases as 𝑁 increases.

Monte Carlo applications are sometimes classified as being simulation in addition, simulation is sometimes
described as being as an application of Monte Carlo method. It is because so many simulations involve the
use of random numbers. Simulation & Monte Carlo are both numerical computation techniques.

Simulation applies to dynamic models, the Monte Carlo is a computational technique applied to static
models.

Calculating Value of π using Monte Carlo Method


import random

INTERVAL = 1000

circle_points = 0
square_points = 0

# Total Random numbers generated= possible x


# values* possible y values
for i in range(INTERVAL**2):

# Randomly generated x and y values from a


# uniform distribution
# Range of x and y values is -1 to 1
rand_x = random.uniform(-1, 1)
rand_y = random.uniform(-1, 1)

# Distance between (x, y) from the origin


origin_dist = rand_x**2 + rand_y**2

# Checking if (x, y) lies inside the circle


if origin_dist <= 1:
circle_points += 1

square_points += 1

# Estimating value of pi,


# pi= 4*(no. of points generated inside the
# circle)/ (no. of points generated inside the square)
pi = 4 * circle_points / square_points

## print(rand_x, rand_y, circle_points, square_points, "-", pi)


# print("\n")

print("Final Estimation of Pi=", pi)

Calculating SQRT(2) using Monte Carlo Method


import random

def monte_carlo_sqrt2(N):
n = 0 # Initialize a counter to keep track of the number of points inside the
region

# Loop N times to generate random points and check if they fall within the
desired region
for _ in range(N):
x = random.uniform(0, 1) # Generate a random x-coordinate between 0 and 1
x += 1.0 # Shift x by 1 to cover the interval [1, 2]

# Check if the squared value of x is less than or equal to 2


if x**2 <= 2:
n += 1 # Increment the counter if the point falls within the region

# Calculate the estimate of the square root of 2 using the ratio of points
inside the region to total points
sqrt2_estimate = 1.0 + (n / float(N))
return sqrt2_estimate

def main():
N = 1000000 # Number of random points to generate
sqrt2_estimate = monte_carlo_sqrt2(N) # Estimate the square root of 2 using
Monte Carlo method
print("Estimated square root of 2 using Monte Carlo method:", sqrt2_estimate)

if __name__ == "__main__":
main()

Calculating SQRT(7) using Monte Carlo Method


import random

def monte_carlo_sqrt7(N):
n = 0 # Initialize a counter to keep track of the number of points inside the
region

# Loop N times to generate random points and check if they fall within the
desired region
for _ in range(N):
x = random.uniform(0, 1) # Generate a random x-coordinate between 0 and 1
x += 2.0 # Shift x by 1 to cover the interval [1, 2]

# Check if the squared value of x is less than or equal to 7


if x**2 <= 7:
n += 1 # Increment the counter if the point falls within the region

# Calculate the estimate of the square root of 7 using the ratio of points
inside the region to total points
sqrt7_estimate = 2.0 + (n / float(N))
return sqrt7_estimate

def main():
N = 1000000 # Number of random points to generate
sqrt7_estimate = monte_carlo_sqrt7(N) # Estimate the square root of 7 using
Monte Carlo method
print("Estimated square root of 7 using Monte Carlo method:", sqrt7_estimate)

if __name__ == "__main__":
main()
Process of Simulation Study:
Exponential Decay Model
Exponential functions can also be used to model populations that shrink (from disease, for example), or
chemical compounds that break down over time. We say that such systems exhibit exponential decay,
rather than exponential growth. The model is nearly the same, except there is a negative sign in the
exponent. Thus, for some positive constant k, we have

As with exponential growth, there is a differential equation associated with exponential decay. We have

Systems that exhibit exponential decay behave according to the model

where y0 represents the initial state of the system and k > 0 is a constant, called the decay constant.
Problem 1:
Let’s look at a physical application of exponential decay. Newton’s law of cooling says that an object cools
at a rate proportional to the difference between the temperature of the object and the temperature of the
surroundings. In other words, if T represents the temperature of the object and Ta represents the ambient
temperature in a room, then

Note that this is not quite the right model for exponential decay. We want the derivative to be proportional to
the function, and this expression has the additional Ta term. Fortunately, we can make a change of
variables that resolves this issue. Let y(t)=T(t)−Ta . Then y'(t)=T'(t)−0=T'(t) , and our equation becomes

From our previous work, we know this relationship between y and its derivative leads to exponential
decay. Thus,

where T0 represents the initial temperature.


More Problem Examples:
Link: 6.8: Exponential Growth and Decay - Mathematics LibreTexts

Cobweb Model
Cobweb theory is the idea that price fluctuation can lead to fluctuations in supply which cause a cycle of
raising and falling prices. In a simple cobweb model, we assume there is an agricultural market where
supply can vary due to variable factors,such as the weather.

Assumptions:
● In an agricultural market, farmers have to decide how much to produce a year in advance - before
they know what the market price will be ( supply is price inelastic in the short run).
● A key determinant of supply will be the price from the previous year.
● A low price will mean some farmers go out of business. Also,a low price will discourage Farmers
from growing that crop in the next year.
● Demand for agricultural goods is usually price inelastic ( a fall in price only causes a smaller
percentage increase in demand).
References:
● Cobweb-Model.pdf (magadhmahilacollege.org)
● Cobweb model - Wikipedia

Single Server Queuing System


These are the most fundamental type of queuing systems, and they are frequently used in simple
applications such as retail stores or fast-food restaurants. Customers arrive and queue to be served by a
single server in these systems.

Example: Consider a service facility with a single server—e.g., a one-operator barbershop or an information
desk at an airport—for which we would like to estimate the (expected) average delay in queue (line) of
arriving customers, where the delay in queue of a customer is the length of the time interval from the instant
of his arrival at the facility to the instant he begins being served. For the objective of estimating the average
delay of a customer, the state variables for a discrete-event simulation model of the facility would be the
status of the server, i.e., either idle or busy, the number of customers waiting in queue to be served (if any),
and the time of arrival of each person waiting in queue. The status of the server is needed to determine,
upon a customer’s arrival, whether the customer can be served immediately or must join the end of the
queue. When the server completes serving a customer, the number of customers in the queue is used to
determine whether the server will become idle or begin serving the fi rst customer in the queue. The time of
arrival of a customer is needed to compute his delay in queue, which is the time he begins being served
(which will be known) minus his time of arrival. There are two types of events for this system: the arrival of a
customer and the completion of service for a customer, which results in the customer’s departure. An arrival
is an event since it causes the (state variable) server status to change from idle to busy or the (state
variable) number of customers in the queue to increase by 1. Correspondingly, a departure is an event
because it causes the server status to change from busy to idle or the number of customers in the queue to
decrease by 1. We show in detail how to build a discrete-event simulation model of this single-server
queueing system.
In the above example both types of events actually changed the state of the system, but in some
discrete-event simulation models events are used for purposes that do not actually effect such a change.
For example, an event might be used to schedule the end of a simulation run at a particular time or to
schedule a decision about a system’s operation at a particular time and might not actually result in a
change in the state of the system. This is why we originally said that an event may change the state of a
system.

Time Advance Mechanisms


Because of the dynamic nature of discrete-event simulation models, we must keep track of the current
value of simulated time as the simulation proceeds, and we also need a mechanism to advance simulated
time from one value to another. We call the variable in a simulation model that gives the current value of
simulated time the simulation clock. The unit of time for the simulation clock is never stated explicitly when
a model is written in a general-purpose language such as C, and it is assumed to be in the same units as
the input parameters. Also, there is generally no relationship between simulated time and the time needed
to run a simulation on the computer. Historically, two principal approaches have been suggested for
advancing the simulation clock: next-event time advance and fixed-increment time advance. Since the fi rst
approach is used by all major simulation software and by most people programming their model in a
general-purpose language, and since the second is a special case of the first, we shall use the next-event
time-advance approach for all discrete-event simulation models discussed in this book.

With the next-event time-advance approach, the simulation clock is initialized to zero and the times of
occurrence of future events are determined. The simulation clock is then advanced to the time of
occurrence of the most imminent (first) of these future events, at which point the state of the system is
updated to account for the fact that an event has occurred, and our knowledge of the times of occurrence of
future events is also updated. Then the simulation clock is advanced to the time of the (new) most imminent
event, the state of the system is updated, and future event times are determined, etc. This process of
advancing the simulation clock from one event time to another is continued until eventually some specified
stopping condition is satisfied. Since all state changes occur only at event times for a discrete event
simulation model, periods of inactivity are skipped over by jumping the clock from event time to event time.
(Fixed-increment time advance does not skip over these inactive periods, which can eat up a lot of
computer time)It should be noted that the successive jumps of the simulation clock are generally variable
(or unequal) in size.

Next-Event Time-Advance Approach

Each of these defined quantities will generally be a random variable. Assume that the probability
distributions of the interarrival times A1, A2, . . . and the service times S1, S2, . . . are known.
Fixed-Increment Time Advance Approach

Components & Organization of Discrete Event Simulation


The following components will be found in the most discrete event simulation models using the next-event
time advance approach, programmed in a general purpose language:

● System state - The collection of state variables necessary to described the system at a particular
time
● Simulation clock - A variable giving the current value of simulated time
● Event list - A list containing the next time when each type of event will occur
● Statistical counters - Variables used for storing statistical information about system information
● Initialization routine - A subprogram to initialize the simulation model at time 0
● Timing routine - A subprogram that determines the next event from the event list and then
advances the simulation clock to the time when that event is to occur
● Report generator - A subprogram that computes estimates (from the statistical counters) of the
desired measures of performance and produces a report when the simulation ends
● Event routine - A subprogram that updates the system state when a particular type of event occur.
There is one event routine for each event type
● Library routines - A set of subprogram used to generate random observations from probability
distributions that were determined as part of the simulation model
● Main program -
○ A subprogram that invokes the timing routine
■ determine the next event and
○ transfer control to the corresponding event routine
■ update the system state appropriately
○ check for termination
■ invoke the report generator when the simulation is over.

The logical relationships (flow of control) among these components are shown below:

The simulation begins at time 0 with the main program invoking the initialization routine, where the
simulation clock is set to 0. The simulation begins at time 0 with the main program invoking the initialization
routine, where the simulation clock is set to zero, the system state and the statistical counters are
initialized, and the event list is initialized. After control has been returned to the main program, it invokes the
timing routine to determine which type of event is most imminent. If an event of type i is the next to occur,
the simulation clock is advanced to the time that event type i will occur and control is returned to the main
program
Random Numbers Generator
- Random Numbers
- Pseudo Random Numbers

Pseudo because generating numbers using a known method removes the potential for true randomness.

The important properties of a good random number routine is:


- Fast
- Portable to different computers
- Have sufficient long cycle
- Replicable
- Closely approximates the idea of statistical properties of uniformity and independence.
Properties of Random Numbers
A sequence of random numbers, R1, R2... must have two important statistical properties, uniformity and
independence. Each random number Ri, is an independent sample drawn from a continuous uniform
distribution between zero and 1. That is, the pdf is given by:

The density function is shown below:

Some consequences of the uniformity and independence properties are the following:

1. If the interval (0, 1) is divided into n classes, or subintervals of equal length, the expected number of
observations m each interval ii N/n where N is the total number of observations. Suppose there are N = 100
observations and the (0,1) class is divided into n = 10 sub-intervals. For example: 0-0.1, 0.1-0.2 and so
on… then each sub-class should contain equal number of observations means N/n = 100/10 that is 10
observations in each sub-class. This will lead to uniformity.

2. The probability of observing a value in a particular interval is of the previous values drawn. This will lead
to independence.
Linear Congruential Method
Question: Write a program in any GPL to find the period of this generator.

It has been extensively tested that for a,c, and m have been selected to assure that the characteristics
desired in a generator are most likely to be achieved for ex:

Let a = 7^5 = 16807, m = 2^31 - 1 = 2147483647

These choices satisfy the condition and ensure a period of p = m - 1 (well over 2 billion)

Generate random numbers using the above values of constants and their values should apply in linear
congruential method and finally you have to verify the period of this generator.

Findings:
Case 1: When x_o = 1, the length of period p = 16
Case 2: When x_o = 2, the length of period p = 8
Case 3: p = 16, Case 4: p = 4
- When the seed is even, the period is smaller, otherwise it's big.
- Value of m if it is smaller, the P will be smaller
- If it is big number, the period may be big
- IF it is a prime big number, the period may be very big.

Lab Que: Generate Random Numbers using LC method and find period and change and analyse values of
seed and constants.

References:
lec07.pdf (uludag.edu.tr)
unit5.pdf (weebly.com)
Frequency Tests for Uniformity
- The frequency test is a test of uniformity.
- Two different methods available, Kolmogorov-Smirnov test and the chi-square test. Both tests
measure the agreement between the distribution of a sample of generated random numbers and the
theoretical uniform distribution.
- Both tests are based on the null hypothesis of no significant difference between the sample
distribution and the theoretical distribution

References:
06 Random Number Generation.pptm (fu-berlin.de)
Chapter-7.pdf (emu.edu.tr)
Tutorial 3.pdf (guc.edu.eg).
Kolmogorov Smirnov Test
This test compares the uniform distribution F(x) to the empirical cdf SN(x) of the sample of N observations.

If the sample from the random number generator is R1, R2, R3 … RN, the the empirical cdf SN(x) is:

As N becomes larger, SN(x) should be close to F(x) i.e. it’ll become a better approximation to F(x), provided
that null hypothesis is true.

The Kolmogorov Smirnov test is based on the largest absolute deviation between F(x) and SN(x) over the
range of the random variable, that is based on the statistic

That is the absolute value of the differences.

Here D is a random variable, The sampling distribution of D is known.


If the calculated D value is greater than the ones listed in the Table, the hypothesis (no disagreement
between the samples and the theoretical value) should be rejected; otherwise, we don't have enough
information to reject it.

For testing against a uniform CDF, the test procedure follows the following steps:

Step 1: Rank the data from smallest to largest

Step 2: Compute

Step 3: Compute

Step 4: Determine the critical value Dtab (which will be given) and compare it with D.

Step 5: If D is less than or equal to Dtab then your null hypothesis is accepted otherwise it is rejected.

Or in other words: If the sample statistic D is greater than the critical value Dtab , the null hypothesis that the
sample data is from a uniform distribution is rejected; if D less than or equal to Dtab , then there is no
evidence to reject it.
Example:
Suppose that five numbers 0.44, 0.81, 0.14, 0.05, 0.93 were generated, and it is desired to perform a test
for uniformity using the Kolmogorov Smirnov test with a level of significance alpha = 0.05 (It is given that
Dtab = 0.565)
Chi-Square Test
Example:

Solution:
Auto-Correlation Test (Independence Test)
Example:
Mid Square Method (Generating Random Numbers)

Note: Append zeroes where necessary.


Random Variates Generator

Inverse Transform Technique


The inverse-transform technique can be used to sample from the exponential, the uniform, the Weibull, the
triangular distributions and from empirical distributions. Additionally, it is the underlying principle for
sampling from a wide variety of discrete distributions. The technique will be explained in detail for the
exponential distribution and then applied to other distributions. Computationally, it is the most
straightforward, but not always the most efficient, technique.

Exponential Distribution
The exponential distribution, has the probability density function (pdf)

and the cumulative distribution function (cdf)


Uniform Distribution

Drawbacks of ITT

Acceptance Rejection Technique

In uniform distribution, if no. of total generations is 100, 75 will be accepted since they’re uniformly
distributed.

Simulation Languages
A computer simulation language is used to describe the operation of a simulation on a computer. An
important part of discrete-event languages is the ability to generate pseudo-random numbers and variants
from different probability distributions. It is basically a special purpose programming language which is
designed for simulation studies. A simulation language statement can do more than n lines of codes of any
3 GPL.
For example: Suppose in GPSSL (General Purpose System Simulation Language), if we want to write an
equivalent statement for performing the same action in a 3GL, it might require more than n lines.

Normally there are two types of Simulation Languages:


- Continuous
- Discrete
- Combination of Both

Types Of Simulation Languages

More Notes:
- unit-4-notes.pdf
- unit-4-question-notes.pdf
- terminating-notterminating-simulation.pdf

You might also like