SLIDE 1
THE SIMULATOR DEFINITIONS In the requirements I have stated that - - PowerPoint PPT Presentation
THE SIMULATOR DEFINITIONS In the requirements I have stated that - - PowerPoint PPT Presentation
THE SIMULATOR DEFINITIONS In the requirements I have stated that your simulator will be a: stochastic, discrete event, discrete time simulator. Let's see what each of these terms means. STOCHASTICITY A stochastic process is one whose state
SLIDE 2
SLIDE 3
STOCHASTICITY
A stochastic process is one whose state evolves “nondeterministically”, i.e. the next state is determined according to a probability distribution. This means a stochastic simulator may produce slightly different results when run repeatedly with the same input. Therefore it is appropriate to compute certain statistics to characterise the behaviour of the simulated system. Remember, these are statistics about the model: You hope that the real system exhibits behaviour with similar statistics.
SLIDE 4
DISCRETE EVENTS
Discrete events happen at a particular time and mark a change of state in the system. This means discreteevent simulators do not track system dynamics continuously, i.e. an event either takes place or it does not. There is no finegrained time slicing of the states, i.e. Generally a state could be encoded as an integer. Usually it is encoded as a set of integers, possibly coded as different data types. Discreteevent simulations run faster than continuous ones.
SLIDE 5
DISCRETE VS CONTINUOUS STATES
When working with discrete events, it is common to consider that states are also discrete. Example:
SLIDE 6
DISCRETE TIME
Discrete time simulations operate with a discrete number of points: Seconds, Minutes, Hours, Days, etc. These can also be logical time points: Moves in a board game, Communications in a protocol. Your task is to write a discrete time simulator. Events will occur with second level granularity.
SLIDE 7
THE EXPONENTIAL DISTRIBUTION
Remember that the probability distribution gives the probability of the different possible values of a random variable. The exponential distribution describes the time between events in a Poisson process, i.e. Events' interarrival times are independent (memoryless), Events occur with a constant average rate λ.
SLIDE 8
THE EXPONENTIAL DISTRIBUTION
Roughly speaking, the time X we need to wait before an event occurs has an exponential distribution if the probability that the event occurs during a certain time interval is proportional to the length of that time interval. Applications: Call arrivals at a telephone exchange, Radioactive particle decay, Air plane arrivals at a large hub.
SLIDE 9
THE EXPONENTIAL DISTRIBUTION
The probability density function (PDF) is given by: f(x,λ) = λeλx, ∀ x > 0 Describes the relative likelihood that an event with rate λ occurs at time x.
SLIDE 10
THE EXPONENTIAL DISTRIBUTION
The probability density function (PDF) is given by: f(x,λ) = λeλx, ∀ x > 0 The integral of this gives the probability that it occurs within 2 time bounds (but you can largely ignore this).
SLIDE 11
THE EXPONENTIAL DISTRIBUTION
The cumulative distribution function (CDF) is given by: F(x,λ) = 1 eλx, ∀ x > 0
SLIDE 12
THE EXPONENTIAL DISTRIBUTION
So if something happens at a rate of 0.5 per unit of time, then the probability that we will observe it occurring within 1 time unit is: F(1, 0.5) = 1 e0.5*1 = 0.393
SLIDE 13
THE EXPONENTIAL DISTRIBUTION
The mean or expected value is given by the reciprocal of the rate parameter. In plain English this means that if something occurs at rate r then we can expect to wait 1/r time units on average to see each occurrence. If something occurs 7 times per week, you can expect to wait 1/7 of a week (or a full 24 hours) on average between each occurrence.
SLIDE 14
EXERCISE
What is the probability that a random variable X is less than its expected value, if X has an exponential distribution with rate λ? The expected value of an exponential random variable with parameter λ is: E[X] = 1/λ
SLIDE 15
EXERCISE
We need to compute P(X ≤ E[X]) using the distribution function: P(X ≤ E[X]) = P(X ≤ 1/λ) = F(x,λ) = 1 e λ*1/λ = 1 1/e
SLIDE 16
THE MEMORYLESS PROPERTY
Formally: P(X > s + t | X > s) = P(X > t), ∀ s, t > 0 Less formally: The time that we can expect to wait for the next occurrence of some (exponentially distributed) event, is unaffected by how long we have already been waiting for it. In the 7 times a week example, if it has been 24 hours since the last
- ccurrence, the expected additional time I have to wait is still 24 hours.
SLIDE 17
THE MEMORYLESS PROPERTY
A quick note, don't confuse these two properties: Correct P(X > 100 | X > 80) = P(X > 20) Incorrect P(X > 100 | X > 80) = P(X > 100) The latter would be a strange kind of predetermined system.
SLIDE 18
THE MEMORYLESS PROPERTY
In your simulation, users place new requests at exponentially distributed time intervals. That means the next request event does not depend on the previous ones. As a result of firing that event, the global state of the simulation changes. However local states may not have changed, e.g. a minibus may still be at a certain stop.
SLIDE 19
HOW DO WE SAMPLE FROM A DISTRIBUTION? INVERSE TRANSFORM METHOD
Let X be a RV with continuous and increasing distribution function F. Denote the inverse by F −1. Let U be a random variable uniformly distributed on the unit interval (0, 1). Then X can be generated by X = F −1(U). If we use an exponential CDF for F, then we effectively sample from that distribution by X = ln(U)/λ
SLIDE 20
SAMPLING EXPONENTIAL DISTRIBUTIONS IN PRACTICE
Straightforward, right? (denoting mean = 1/λ) Well... not quite... rand() is known to be implemented poorly. You want to draw a RV uniformly distributed on (0,1). Seeding properly a pseudorandom generator is tricky. OK, so what should we do?
int r = (int) (-log(rand()/RAND_MAX)*mean);
SLIDE 21
XKCD?
SLIDE 22
DRAWING UNIFORMLY DISTRIBUTED RANDOM NUMBERS
Use a uniform deviate and discard the zero.
double uniform_deviate ( int seed ) { return seed * ( 1.0 / ( RAND_MAX + 1.0 ) ); } .... int r; do r = uniform_deviate ( rand() ); while (r == 0); r = (int) (-log(r)*mean);
SLIDE 23
SEEDING RAND()
The usual solution is to get the system time. Note there may be some portability issues with the above. Julienne Walker argues that hashing the system time first is a good solution. For a longer discussion about this and random numbers you can check his .
srand ( (unsigned int) time ( NULL ) );
web page
SLIDE 24
FURTHER READING
Donald E. Knuth (1998). The Art of Computer Programming, volume 2: Seminumerical Algorithms, 3rd Edition, AddisonWesley. William H. Press et al. (2007). Numerical Recipes: The Art of Scientific Computing, 3rd Edition, Cambridge University Press.
SLIDE 25
YOUR SIMULATORS
- 1. Will be Discrete event simulators;
- 2. Will be Discrete time simulators;
- 3. Will make use of the exponential distribution to model user behaviour.
SLIDE 26
CLARIFICATIONS
Boarding time The first version of the coursework contained conflicting statements about the time to board/disembark a passenger. This parameter should be given in seconds –the coursework handout has been updated accordingly.
SLIDE 27
CLARIFICATIONS
Question: What file format should I expect for the input scripts? Answer: Input scripts will be given in plain text format (but not necessarily with a .txt extension).
SLIDE 28
REMINDERS
Read the handout carefully. These slides are not a substitute. You are encouraged to submit an early version of your simulator for Part 1. This is not marked, but it gives you the chance to receive feedback which can help in completing Part 2.
SLIDE 29