Modelling & Simulation Class Notes
Modelling & Simulation Class Notes
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
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.
P - Reorder Point
Q - Reorder Quantity
These are the models/policies that you got to test:
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.)
- 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.
Solution: Here
Problem: Pure Pursuit Problem
- 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.
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)
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
𝑑𝑦 𝑑 𝑘𝑡
𝑅𝑎𝑡𝑒 𝑜𝑓 𝑡ℎ𝑒 𝐺𝑟𝑜𝑤𝑡ℎ = 𝑑𝑥
= 𝑦' = 𝑦𝑜 𝑑𝑡
𝑒
𝑘𝑡
𝑦' = 𝑘𝑦𝑜𝑒
𝑦' = 𝑘𝑦
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
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:
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.
𝑁 𝐴𝑟𝑒𝑎 𝑜𝑓 𝑠𝑞𝑢𝑎𝑟𝑒 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.
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.
INTERVAL = 1000
circle_points = 0
square_points = 0
square_points += 1
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]
# 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()
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]
# 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
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,
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
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.
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.
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
● 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.
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:
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
For testing against a uniform CDF, the test procedure follows the following steps:
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)
Exponential Distribution
The exponential distribution, has the probability density function (pdf)
Drawbacks of ITT
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.
More Notes:
- unit-4-notes.pdf
- unit-4-question-notes.pdf
- terminating-notterminating-simulation.pdf