Design of discrete-event simulations Lecturer: Dmitri A. Moltchanov E-mail: moltchan@cs.tut.fi http://www.cs.tut.fi/kurssit/tlt-2707/
OUTLINE: Discrete event simulation; Event advance design; Unit-time advance design; Lecture: Design of discrete-event simulations 2
1. Discrete event simulation The basic idea: only events change the state of the system; here is not need to track state of the system between these events. The whole system consists of the following components: system under consideration: stochastic process under considerations: product of other processes and RVs. system state: state of the stochastic process. simulation clock: tells the occurrence time of the next event. event list: holder for events. You may consider it as a two dimensional vector: time and event. Lecture: Design of discrete-event simulations 3
The idea of the event list is illustrated in the following figure: T i, i = 1, 2,..., are times; E i, i = 1, 2,..., are corresponding events. E 1 T 1 E 2 T 2 E 3 T 3... E i T i... Figure 1: The idea of the event list. Events are identified by event time and event type. There are two general types of events: basic events: these events modify the state of the system: arrivals/departures of customers. additional events: these are events needed by additional tasks of simulation: run, stoppage, collection of data. Lecture: Design of discrete-event simulations 4
Algorithm for discrete-event simulation: Initialization procedure: system clock should be set to zero; system state is assigned an initial value; generate list of the event and place the next event to this list. Handling of events during a simulation: system clock should be set to the occurrence time of the first (next) event in event list; handle the event making all appropriate actions associated with the event; update the system state. Stop of simulation. Note that the following is not included: storing of statistical data; statistical analysis of obtained data. Lecture: Design of discrete-event simulations 5
1.1. Time advance methods Time advance methods in discrete-event simulations: event-advance method; unit-advance method. unit-time advance: t 0 t 1 t 2 t 3 t 4 t 5 event advance: t 0 t 1 t 3 Figure 2: Time advance techniques in discrete-event simulations. Lecture: Design of discrete-event simulations 6
2. Event advance design This is the most popular design of discrete-event simulations. The idea: observe: state of the system changes each time an event occurs; therefore: during time between events state does not change; idea: we can just skip times between events! To implement this idea: each event is associated with a certain clock value; this value gives the time in the future when this event occurs. Treatment of the event: do everything what is needed for treatment of the event; regroup all events that will occur in the future according to values of their clocks; find the one with the smallest clock value; advance the master clock to the time of that event and treat it. Lecture: Design of discrete-event simulations 7
Machine interference problem: M unreliable machines; 1 repairmen. Figure 3: A full cycle of the machine. Figure 4: A model for downtime of the machine (queue is of D/D/1/ /M type). Lecture: Design of discrete-event simulations 8
Two types of events: arrival to the repairmen s queue; departure after the machine has been fixed. Lecture: Design of discrete-event simulations 9
2.1. Future event list Future event list: collection of events that occur in the future. Future event list contains: time of occurrence: determines when the events occurs. type of the event: determines what actions should be taken to process the events; these actions must be associated with all events. The problem: how to effectively determine the next event? we considered examples with only few events; there can be a lot of events; Efficiency of the finding algorithm depends on: amount information kept in the future list; the way how this information is organized. Lecture: Design of discrete-event simulations 10
What type of operation are performed in a list: locating the next event and its type: required when advancing the time. deleting the event: required when the events has already been treated. inserting the event in a list: required when generating new event; required when basic event generates conditional events. Basically, there are two ways of organizing a list: using sequential array; using linked list. Lecture: Design of discrete-event simulations 11
2.2. Sequential arrays The approach: all future event times are stored sequentially in array. How to implement: associate each event type with a certain integer i; clock associated with this event is stored in the ith position in array. Example: we need N positions in array: clock value of the type 1 event is stored in 1st position; clock value of the type 2 event is stored in 2nd position;... clock value of the type N event is store in Nth position; Figure 5: Using sequential array as a future event list. Lecture: Design of discrete-event simulations 12
How to find the next event: locate the smallest value in array of N elements. Example: find the smallest element and its index in array E[i], i = 0, 1,..., N 1: variable smallest returns the smallest element; variable index returns the index of the smallest element. smallest = E[0]; index = 0; for (i=1; i<n; i++) { if (E[i] < smallest) { smallest = E[i]; index = i; } } Lecture: Design of discrete-event simulations 13
How to deal with other functions: deletion: set the value of the clock associated with event of type i to very big one; not deleted physically! insertion: update the value of the clock associated with event of type i. not inserted physically! Advantages: insertion and deletion are made very easily; location of the next event depend on the number of event types: complexity is linear in time. Shortcoming: if the number of event types is large location is time consuming; in this case different organization of the future event list should be considered. Lecture: Design of discrete-event simulations 14
2.3. Linked list The idea: to access data elements in correct order we store data element; we store pointer to the next element. Definitions: pointer is referred to as link; data element and the link(s) are referred to as node. Example: node consists of two elements: one is data elements, the next one is link; link F points to the first element; link of the last node is set to zero. Figure 6: Using linked list as a future event list. Lecture: Design of discrete-event simulations 15
Linked lists: singly linked: nodes are connected by a single link; multiply linked: nodes can be connected by multiple links. Example: singly linked list: node consists of: two data elements: clock value CL i ; type of the event indicating by i. one link to the next element. Note: nodes are arranged in ascending order: CL i < CL j < < CL n. Figure 7: Usage of singly linked list in event advance discrete-event simulation. Lecture: Design of discrete-event simulations 16
Implementation example: arrange integer numbers in T in ascending order: create new array P of the same dimension; the content of P (i) is the link to location in T containing element larger than T (i); example: P (1) = 7, next larger after T (1) is T (7)... Notes: F (= 5) is the first element; node: location in T and corresponding location in P. Figure 8: Locating values in a singly linked list. Lecture: Design of discrete-event simulations 17
Locating element: we want to locate number 10 in T : using pointer F we check the value stored in the first node: (T (5), P (5)); using pointer P (5) we locate the second node: (T (3), P (3)); using pointer P (3) we locate the third node: (T (1), P (1)); pointer P (1) contains the address we are looking for; general: if we know address of the first node we can locate any element. Note: we can only move forward using singly linked link! Figure 9: Deletion of elements in a singly linked list. Lecture: Design of discrete-event simulations 18
Deletion: we want to delete number 10: change the value of the pointer of the previous node; previous node: (T (1), P (1)) = (5, 4); node containing 10 has the pointer P (7) = 4; to delete: set P (1) = P (7) = 4. Note: information is not physically deleted, just not accessible! Figure 10: Deletion of elements in a singly linked list. Lecture: Design of discrete-event simulations 19
Insertion: we want to insert number 6: locate two nodes between which we should put 6; starting from the first node we find them as: (5, 7) and (10, 4) (see previous slide); get unused location in T : T (2); set P (1) = 2 to go from T (1) = 5 to T (2) = 6; set T (2) = 6, P (2) = 7 to go from T (2) = 6 to T (7) = 10. Figure 11: Insertion of elements in a singly linked list. Lecture: Design of discrete-event simulations 20
2.4. Implementation of linked lists We have to be able: organize data elements and pointers into nodes; access node using pointer; create and delete nodes. There are two ways: use built-in commands to carry these operations (if any); set-up your own storage scheme. Notes: must be long enough to accommodate all events that might be generated; all unused nodes must be linked together to form a pool of free nodes. Lecture: Design of discrete-event simulations 21
2.5. Future event list using linked list General notes: nodes are organized such that CL i < CL j < < CL n ; the next event is given by pointer F (first node); when the event occurred and processed it should be deleted; if conditional events are generated they are place in linked list. Advantages: location and deletion is made easily: using linked lists in simulation we have to delete only the first node! Shortcomings we have to use location procedure to insert an event in a linked list; search of the linked lists might be time consuming if the number of event types is large; solution: use better location procedure (e.g. use pointer pointing to the middle of the list)! Lecture: Design of discrete-event simulations 22
2.6. Doubly linked lists The main problem of the singly linked lists: we can go only forward! Doubly linked lists: link two successive node with two pointers; we can go forward and backward! Note: there are some advantages for specific applications. Figure 12: Example of the doubly linked list. Lecture: Design of discrete-event simulations 23
3. Unit-time advance The basic idea: master clock is advanced in fixed increments of time. How it works: each time clock is advanced all future events clocks is compared to it; if any of these clocks is equal to the master clock this event is processed; if not, clock is advanced again. Note: this is less popular design. t 0 t 1 t 2 t 3 t 4 t 5 Figure 13: Unit-time advance design of discrete-event simulations. Lecture: Design of discrete-event simulations 24
Lecture: Design of discrete-event simulations 25
Notes of the event clocks: may contain future time with respect to the origin; may reflect the duration of the particular activity: e.g. departure clock may contain the duration of the service. If related clocks are used in simulation: when master clock is advanced by unit time: future clock is reduced by unit times; if any is zero we process the associated event. Lecture: Design of discrete-event simulations 26
Example: simulate a single server queue with finite population of customers: AT: interarrival times; ST: service time; MCL: master clock. Lecture: Design of discrete-event simulations 27
3.1. Selecting unit-time If all future events clocks are integer: event clocks must be a multiple of unit-time; unit-time must be a smallest common divisor. If some of future events are real we have a problem: some events may occur between two successive time instant of the master clock; occurrence of the events is not known to the master clock. Another problem: some events may occur at the same time instant of the master clock. Figure 14: Certain events may occur in between two successive times of the master clock. Lecture: Design of discrete-event simulations 28
Trade-off: time increment must be small enough to ensure that one event occurs in between; if it is too small, it is ineffective. Some rules for choosing unit-time: 1. set the time-unit to the half of the smallest variate generated: we know only probabilistically what will be the smallest variate generated. 2. run multiple pilot simulation with different unit-times to observe their effect: start with very small time-unit; increase it slightly; if there is no effect on results you may increase it again. Lecture: Design of discrete-event simulations 29
3.2. Notes on implementation of unit-time advance One can use for future event lists: sequential arrays; linked lists. Advantages: when events are approximately evenly separated from each other; the best case when events are about a unit-time from each other. Shortcomings: when there are few events and they are far apart from each other. Note: practically, no advantages comparing to event advance design. Lecture: Design of discrete-event simulations 30