Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Event Simulation

Similar presentations


Presentation on theme: "Discrete Event Simulation"— Presentation transcript:

1 Discrete Event Simulation

2 Discrete Event Simulation
Purpose Used to study quantitative system behaviour when analytic queueing theory is difficult to apply or when analytic modelling assumptions are not valid Relies on Queues and Lists

3 Queue ADT Revisited ADT Review Methods Lists were positional
Stacks were last in first out Queues Queues are first-in-first-out Can have variants, priority queues etc.. Methods queue() // constructor, create an empty queue queueIsEmpty() // Determine whether queue is empty queueAdd(newItem, Success) // Add newItem to Queue queueRemove(success) // Remove item from front of queue getQueueFront(success) // Return front item but leave it in the queue (Peek)

4 Queue ADT Implementations Array, or list
Like stack, you can use a List ADT Why distinguish between these ADTs at all? If you only use the properties of a stack, then calling it a stack helps others understand the program better Same with lists and queues in general The ADTs are building blocks

5 Discrete Event Simulation
Develop an abstract model of a system that includes: Entities: Customers (for ex: people that need to fill their cars with gas) Resources (queues and service centres) (for ex: gas pump) Events Events cause changes in system state, usually things that cause the movement of customers between queues Arrival, completions Attributes of entities Time between customer arrivals Resource demands of customers at resources (for ex: the time needed for fill-up: pump gas and pay) Use the model to answer questions What is the mean response time of customers that stop for gas? What percentage of the time is the pump in use for fill-up?

6 Types of models Open models Closed models
Infinite number of customers, they arrive with a certain rate regardless of how long it takes to get service Gas station on the 401 Closed models Fixed number of customers, rate of arrivals is affected by customer response times Gas station for a taxi fleet with 10 taxis

7 Open single server queue (Simulation terminology)
Entity: building block that describe, customers, queues, and servers Events: cause movement of customers between queues, or other changes to system state Server Entity: Attributes: Scheduling Discipline (Fifo, could be others) Service Rate (Different pumps can serve a different number of litres per second and different systems for paying) Customer Arrival Event (Car arrives for gas) Server Customer Entity: Attributes: Mean Service time (for fill-up, could be expressed in litres) Mean interarrival time (Time between arrivals of cars) Departures Arrivals Device Completion Event (Customer has filled-up and leaves) Queue Entity: Attributes: Max Size (Is there a limit on how many cars can queue?)

8 Discrete event simulation process
Events are maintained in a future event list ordered by time Discrete event simulation only considers time instants at which events occur System state remains unchanged between events Simulation clock used to keep track of simulated time To process the next event Advance clock to time of event Change the system state to reflect the impact of the event For example if a customer arrives it must be enqueued Generate future events caused by this event If the server is idle , the server starts to serve the customer so a device completion event must be enqueued at the appropriate place in the future event list

9 Event scheduling approach
Keep track of all known future events in an event list Simulation divided into three parts 1. Initialization Data structures reflect system state at time zero 2. Main loop Take next event from event list Event includes time, type, and optional parameters Set clock to time of the event Call a function to handle event (and cause future events) Enqueue a customer or update the event list further Quit when termination condition is met 3. Output routine Output metrics collected in main loop

10 Flow chart for events of open single server queue
Get next event Advance clock to the time of the event ? Arrival event Departure event N=N+1 Number of customers in system N=N-1 Set server idle Schedule arrival event for future customer Enqueue this customer Queue Not Empty ? Queue Empty Instructor’s Note: N is the number of customers in the system (ie queued or in service). Customers in service are not in the queue. Server Idle ? Server Busy Start service event Deque this customer, set server busy Schedule departure event for this customer

11 Single server queue pseudo-code
Initialization: Let N be the number of customers in the system, set N=0 Set queue to empty Set server to idle Set event list to empty Set clock to zero Schedule first arrival event at time zero Enter main loop

12 Event subroutine pseudo-code
Arrival event Increment N by 1 (N is the number of customers queued or in service) Schedule next arrival event: event time = clock + interarrival time Enqueue arriving customer If server is idle, call subroutine "start service" event Start service event Dequeue customer Set server to busy Schedule a departure event: event time = clock + service time Departure event Decrement N by 1 Set server to idle If queue is non-empty, call subroutine for "start service event"

13 Options for terminating the simulation
To leave the main loop and go to step 3 Call output routine when N exceeds some pre-specified quantity Schedule "end of simulation" event during initialization Event time equal to desired termination time Call output routine when event occurs Stop scheduling arrivals after some pre-specified time Call output routine if no events remain in event list

14 Closed model exercise Consider a closed model with a single server
Customers think for an average of Z time units between visits to the server How do we adapt the open model simulation pseudo-code for this purpose? Solution: Initialization, start with N customers instead of zero, generate an arrival event for each with a mean inter-arrival time of Z time units (put them in the future event list) Arrival subroutine: don’t generate a future arrival Departure subroutine: generate an arrival event with mean value Z for the customer Solution: Initialization, start with N customers instead of zero, generate an arrival event for each with a mean interarrival time of Z time units (put them in the future event list) Arrival subroutine: don’t generate a future arrival Departure subroutine: generate an arrival event with mean value Z for the customer

15 Using distributions for generating psuedo-random variates (numbers) for service and inter-arrival times Using distributions Service times, sleeping times, inter-arrival times etc Can come from many possible distributions Deterministic Uniform Normal Exponential 1 Cummulative Density Function (CDF) 1 Value of variate

16 Inverse transformation method
Use a pseudo-random number generator to generate number between 0 and 1, then compute random variate using a known inverse function for the CDF y = Math.random() returns a psuedo-random double number betwewn 0.0 and 1.0, the numbers are pseudo-random because they eventually repeat themselves Inverse CDFs are well known for many distributions Deterministic Uniform Normal Exponential 1 Value of variate x = x x = y x = x = - ln y

17 Examples For the exponential distribution: x = - mean ln(y)
Say random returns the values y = 0.1, 0.5, and 0.3 Suppose we want an exponentially distributed random variable with a mean of 4 then: x = - 4 * ln(0.1) = 9.21 x = -4 * ln(0.5) = 2.77 x = -4 * ln(0.3) = 4.81 After a very large number of independent uniformly distributed values for y, the mean of x will approach 4

18 Histogram method Observe inter-arrival times and service times
Record them in a table such as follows: Chose a uniformly distributed pseudo random variable between 0 and 1, match with appropriate value Better for multi-modal distributions or for values of unknown distribution Value Probability

19 Collection of metrics Once we are able to step through a simulation we can collect metrics Suppose we are interested in the following The average number of customers waiting for service The standard deviation of the number of customers waiting for service The utilization of the server We now consider how to modify the simulator for this purpose Define the average and sample variance as

20 Collection of metrics for open single server queue
Get next event Advance clock ? Arrival event Departure event N=N+1 busytime = busytime + (clock - busystart) N=N-1 Set server idle Schedule arrival event for future customer Enqueue this customer save time of arrival Queue Not Empty Queue Empty Server Idle Server Busy busystart = clock waiting time = clock - time of arrival NWAIT = NWAIT + 1 SWAIT = SWAIT + waiting time SQWAIT = SQWAIT + sqr(waiting time) Start service event Deque customer, set server busy Schedule departure event for this customer

21 Collection of metrics Mean and variance of customer waiting time
Initialization NWAIT = 0 (Total number of customers that have waited) SWAIT = 0 (Sum of waiting times) SQWAIT = 0 (Sum of squares of waiting times) Waiting time computed when customer begins service Remove first customer from queue and get its time of arrival waiting time = clock - time of arrival NWAIT = NWAIT + 1 SWAIT = SWAIT + waiting time SQWAIT = SQWAIT + sqr(waiting time) At end of simulation Mean waiting time = SWAIT/NWAIT Population variance in waiting time = (SQWAIT - sqr(SWAIT)/NWAIT)/NWAIT Standard deviation of waiting time is therefore sqrt(population variance) Visualize whats going on in the collection of mean waiting time

22 Utilization Start service event Departure event Output Results
Busy start = clock time Departure event Busy time = Busy time + (clock time - Busy start) Output Results Utilization = Busy time / Total time Where Total time is the final value on the simulation clock Visualize the collection of the utilization measure

23 Instrumenting to collect metrics
Tedious and error prone Need to verify your instrumentation code One of the advantages of simulation packages is that they provide automatic support for many metrics

24 Example of Results Suppose we have an open model with arrival rate  = 1, 2, 3, 4 arrivals per second The time between arrivals (interarrival times are exponentially distributed) The service times have a mean of S = 0.18 seconds and are exponentially distributed  Rclient = S/(1-U), Userver =  D (from queueing theory)

25 Notes For this simple system we can use queueing theory
Simulation will only approximate the exact results of the queueing theory (because of pseudo random numbers and finite number of events) As U increases we will have to simulate longer and longer We always repeat simulations many times with different initial seeds Math.setSeed(long seed) to start the sequence of numbers at a specific value The mean of the mean results of each run is normally distributed (central limit theorem), therefore we can take a confidence interval to assess the statistical significance of the results As we vary our assumptions about distributions and sequences requests for queues -- simulation can give more accurate results

26 How can OO support simulation?
Various types of queues can be subclasses of a Queue class First come first served Multiple server queues Processor sharing Each of n queued customers gets 1/nth of the server Various types of events can be subclasses of an Event class Arrival events, departure events, other events if they are needed Customer objects can keep track of their own state as they flow through the system

27 Simulating Program Behaviour
Customer starts Consider the following graph that describes a program’s logic The edges are annotated with branching probabilities for going from one vertex to the next The nodes are labeled with a service (S) demand at a processor 1 1 If 2 0.3 0.7 Loop 13 3 1 1 4 1 5 0.9 0.1 6 Customer ends


Download ppt "Discrete Event Simulation"

Similar presentations


Ads by Google