Computer Science, Informatik 4 Communication and Distributed Systems
Simulation Simulation
Modeling and Performance Analysis with Discrete-Event Simulation g y
- Dr. Mesut Güneş
Simulation Simulation Modeling and Performance Analysis with - - PowerPoint PPT Presentation
Computer Science, Informatik 4 Communication and Distributed Systems Simulation Simulation Modeling and Performance Analysis with Discrete-Event Simulation g y Dr. Mesut Gne Computer Science, Informatik 4 Communication and Distributed
Computer Science, Informatik 4 Communication and Distributed Systems
Modeling and Performance Analysis with Discrete-Event Simulation g y
Computer Science, Informatik 4 Communication and Distributed Systems
General Principles
Computer Science, Informatik 4 Communication and Distributed Systems
Chapter 3. General Principles 3
Computer Science, Informatik 4 Communication and Distributed Systems
Concepts of Discrete-Event Simulation Concepts of Discrete Event Simulation
Chapter 3. General Principles 4
Computer Science, Informatik 4 Communication and Distributed Systems
discrete points in time
System state System state
Chapter 3. General Principles 5
time
Computer Science, Informatik 4 Communication and Distributed Systems
Concepts of dynamic, stochastic systems that change in a discrete manner
System A collection of entities that interact together over time to accomplish one or more goals, e.g., bank, network, production system. M d l A b i f ll i i l l i l Model An abstract representation of a system, usually containing structural, logical, or mathematical relationships that describe the system. System state A collection of variables that contain all the information necessary to describe the system at any time. syste at a y t e Entity An object in the system that requires explicit representation in the model, e.g., people, machines, nodes, packets, server, customer. Attributes The properties of a given entity, e.g. length of a packet, capacity of machine. p p g y g g p p y List, Set A collection of associated entities ordered in some logical fashion in a waiting line. Holds entities and event notices Entities on a list are always ordered by some rule, e.g. FIFO, LIFO, or ranked by some attribute, e.g. priority, due date Event An instantaneous occurrence that changes the state of a system. Event notice A record of an event to occur at the current or some future time, along with any
Chapter 3. General Principles 6
associated data necessary to execute the event.
Computer Science, Informatik 4 Communication and Distributed Systems
Event list A list of event notices for future events, ordered by time of occurrence; known as the future event list (FEL) or future event set (FES) future event list (FEL) or future event set (FES). Always ranked by the event time. Activity A duration of time of specified length, which is known when it begins. Represents a service time interarrival time or any other processing time whose Represents a service time, interarrival time, or any other processing time whose duration has been characterized by the modeler. The duration of an activity can be specified as:
St ti ti l R d d f {2 5 7}
The duration of an activity is computable when it begins The duration is not affected by other events The duration is not affected by other events To track activities, an event notice is created for the completion time, e.g., let clock=100 and service with duration 5 time units is starting
Delay A duration of time of unspecified indefinite length, which is not known until it ends. Customer’s delay in waiting line depends on the number and service times of other customers. Typically a desired output of the simulation run
Chapter 3. General Principles 7
Typically a desired output of the simulation run. Clock A variable representing the simulated time.
Computer Science, Informatik 4 Communication and Distributed Systems
the future event list
Delay is known as conditional wait
representing a waiting line
placed in the future event list
Chapter 3. General Principles 8
Computer Science, Informatik 4 Communication and Distributed Systems
Delay Delay Delay Activity1 Activity2 t A1 A2 A3 D1 D2
Chapter 3. General Principles 9
Computer Science, Informatik 4 Communication and Distributed Systems
A( )
g y
p y p
Service completion by Able
Interarrival time of callers
Chapter 3. General Principles
10
Computer Science, Informatik 4 Communication and Distributed Systems
system being in a certain state?
D l
simulation started?
Chapter 3. General Principles 11
Computer Science, Informatik 4 Communication and Distributed Systems
S stem state
performance metrics
( )
Clock System state Entities and Set 1 Set 2 Future event list (FEL) Statistics Clock System state Entities and attributes Set 1 Set 2 ... Future event list (FEL) Statistics t (x, y, z, ...) (3,t1) – Type 3 event to occur at t1
Chapter 3. General Principles 12
... ... ... ... ... ... ... ...
Computer Science, Informatik 4 Communication and Distributed Systems
The Event Scheduling / Time Advance Algorithm The Event Scheduling / Time Advance Algorithm
Chapter 3. General Principles 13
Computer Science, Informatik 4 Communication and Distributed Systems
event to occur event to occur
Efficient management of the FEL has a major impact on the performance of a simulation run
– Array List Tree Heap etc
Chapter 3. General Principles 14
– Array, List, Tree, Heap etc.
Computer Science, Informatik 4 Communication and Distributed Systems
Clock State Future event list
Old system snapshot at time t
Clock State … Future event list
t (5,1,6) (3,t1) – Type 3 event to occur at t1 (1,t2) – Type 1 event to occur at t2 (1 t3) – Type 1 event to occur at t3
Event-scheduling/Time-advance algorithm Step 1: Remove the event notice for the imminent event from FEL
(1,t3) Type 1 event to occur at t3 ... (2,tn) – Type 2 event to occur at tn
imminent event from FEL
Step 2: Advance Clock to imminent event time
New system snapshot at time t
Step 3: Execute imminent event
Clock State … Future event list
t1 (5,1,5) (1,t2) – Type 1 event to occur at t2 (4 t*) T 4 t t t t*
New system snapshot at time t1
g y
Step 4: Generate future events and place their event notices on FEL
(4,t*) – Type 4 event to occur at t* (1,t3) – Type 1 event to occur at t3 ...
Event (4, t*) Step 5: Update statistics and counters
Chapter 3. General Principles 15
(2,tn) – Type 2 event to occur at tn
Computer Science, Informatik 4 Communication and Distributed Systems
but impinges on the system, e.g., arrival of a customer.
System
Chapter 3. General Principles 16
Computer Science, Informatik 4 Communication and Distributed Systems
first arrival, a second arrival is generated
Bootstrapping
Chapter 3. General Principles 17
Computer Science, Informatik 4 Communication and Distributed Systems
p
arrival time, when there is an idle server
– Conditions: Customer is present and server is idle
Chapter 3. General Principles 18
Computer Science, Informatik 4 Communication and Distributed Systems
t ill b h d l d event will be scheduled
generated, and an end-of-downtime (eod) event will be scheduled
Time runtime downtime runtime
Chapter 3. General Principles 19
Time Time 0 eor eor eod
Computer Science, Informatik 4 Communication and Distributed Systems
time TE Simulation will run over [0, TE]
TE is not known ahead.
Chapter 3. General Principles 20
Computer Science, Informatik 4 Communication and Distributed Systems
World Views World Views
Chapter 3. General Principles 21
Computer Science, Informatik 4 Communication and Distributed Systems
d l developer
typically support some
Discrete Simulation
yp y pp world views
discrete simulations
Event-scheduling Process-interaction Activity-scanning
discrete simulations
Chapter 3. General Principles 22
Computer Science, Informatik 4 Communication and Distributed Systems
Start
g
attributes
Initialization Select next event
attributes
system D fi h t h
in system state
Event routine 1 Event routine 2 Event routine n
each event
Terminate?
No
Output
Yes
Chapter 3. General Principles 23
End
Computer Science, Informatik 4 Communication and Distributed Systems
M d l thi k i t f
through the system, demanding resources and queueing to wait for resources
busy with another process
resource, that define the life cycle of one entity as it moves through a system
Chapter 3. General Principles 24
Computer Science, Informatik 4 Communication and Distributed Systems
Start
y g
that allow an activity to begin
Initialization Ph 2 A ti it S Phase 1: Time Scan
conditions for each activity are checked, and, if the conditions are true then the corresponding
Phase 2: Activity Scan
Activity 1 Condition Activity 2 Condition Activity n Condition
true, then the corresponding activity begins
Actions Other condition Actions Actions
Yes Disadvantage: The repeated scanning to discover whether an activity can begin results in slow runtime
satisfied?
No No Improvement: Three-phase approach
with activity scanning Output Terminate?
Yes No
Chapter 3. General Principles 25
with activity scanning p End
Computer Science, Informatik 4 Communication and Distributed Systems
Start I iti li ti
zero time units
Initialization Phase A: Time Scan Phase B: Execute B activities due now
unconditional activities
that are conditional upon certain conditions being true Phase C: Scan all C activities Phase B: Execute B activities due now co d o s be g ue
scheduled ahead of time, just as in the event-scheduling approach
Activity 1 Condition Actions Activity 2 Condition Actions Activity n Condition Actions
type activities can begin or C-type events occur happen only at the
Other condition satisfied?
Yes No pp y end of each time advance, after all B-type events have completed
Terminate?
Yes No No
Chapter 3. General Principles 26
Output End
Computer Science, Informatik 4 Communication and Distributed Systems
E3 E4 P2
3 4
A3 A4 E7 E8 A A P4 E1 E2 P1 E5 E6 A5 A6 P3 A7 A8 Time E1 E2 A1 A2
5 6
Chapter 3. General Principles 27
Computer Science, Informatik 4 Communication and Distributed Systems
Examples
Examples
Chapter 3. General Principles 28
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
I Ch t 2 W d d h th d t i l t th
( )
(A, t) arrival event at future time t
Chapter 3. General Principles 29
Server Waiting line Calling population
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
Chapter 3. General Principles 30
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
Server Busy time Maximum Queue Length
Initial conditions First customer arrives at t=0 and gets service An arrival and a departure event is on FEL event is on FEL Server was busy for 21 of 23 time units Maximum queue length Maximum queue length was 2
Chapter 3. General Principles 31
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
continue the simulation! continue the simulation!
Chapter 3. General Principles 32
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
(A t Ci) i l f t Ci t t
Checkout Line set of customers currently at the checkout counter ordered by time of arrival
the current time the current time
Chapter 3. General Principles 33
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Grocery Manual Simulation Using Event Scheduling – Grocery
System State Statistics Extended version of the simulation table from Slide 31 System State Statistics Clock LQ(t) LS(t) Checkout Line Future Event List S ND F
1 (C1,0) (A,1,C2) (D,4,C1)(E,60) 1 1 1 (C1,0)(C2,1) (A,2,C3)(D,4,C1)(E,60) 2 2 1 (C1,0)(C2,1)(C3,2) (D,4,C1)(A,8,C4)(E,60) 4 1 1 (C2,1)(C3,2) (D,6,C2)(A,8,C4)(E,60) 4 1 6 1 (C3,2) (A,8,C4)(D,11,C3)(E,60) 9 2 1 8 1 1 (C3,2)(C4,8) (D,11,C3)(A,11,C5)(E,60) 9 2 1 11 1 1 (C4,8)(C5,11) (D,15,C4)(A,18,C6)(E,60) 18 3 2 15 1 (C5,11) (D,16,C4)(A,18,C6)(E,60) 25 4 3 16 (A,18,C6)(E,60) 30 5 4 18 1 (C6,18) (D,23,C6)(A,23,C7)(E,60) 30 5 4
83 . 5 35 time response = = = S
( , ) ( , , )( , , )( , ) 23 1 (C7,23) (A,25,C8)(D,27,C7)(E,60) 35 6 5
83 . 5
5
= = =
≥
F N
Chapter 3. General Principles 34
83 . 5 6 time response
D
N 83 . 6
5 ≥ D
N N
Computer Science, Informatik 4 Communication and Distributed Systems
Examples
Examples
Chapter 3. General Principles 35
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Dump Truck Manual Simulation Using Event Scheduling – Dump Truck
p
to the railroad
the coal and returns to the loader queue
Chapter 3. General Principles 36
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Dump Truck Manual Simulation Using Event Scheduling – Dump Truck
Loading Time Distribution Loading Time PDF CDF
Loading Time PDF CDF 5 0.30 0.30 10 0.50 0.80
15 0.20 1.00 Weighing Time Distribution W i hi Ti PDF CDF
( , , ) du p uc e ds e g g ( ) a e
L d T k iti t b i l di FCFS
Weighing Time PDF CDF 12 0.70 0.70 16 0.30 1.00
Travel Time Distribution Travel Time PDF CDF
40 0.40 0.40 60 0.30 0.70 80 0 20 0 90
Chapter 3. General Principles 37
y q
80 0.20 0.90 100 0.10 1.00
Computer Science, Informatik 4 Communication and Distributed Systems
Manual Simulation Using Event Scheduling – Dump Truck Manual Simulation Using Event Scheduling – Dump Truck
It i d th t fi t k t th l d d i t th l t ti
Both loaders are busy! y
System State Lists Statistics Clock LQ(t) L(t) WQ(t) W(t) Loader Queue Weigh Queue Future Event List BL BS
3 2 1 DT4, DT5, DT6 (EL,5,DT3) (EL,10,DT2) (EW,12,DT1) 5 2 2 1 1 DT5, DT6 DT3 (EL,10,DT2) (EL,5+5,DT4) (EW,12,DT1) 10 5 10 1 2 2 1 DT6 DT3, DT2 (EL,10,DT4) (EW,12,DT1) (EL,10+10,DT5) 20 10 10 2 3 1 DT3, DT2, DT4 (EW,12,DT1) (EL,20,DT5) (EL,10+15,DT6) 20 10 12 2 2 1 DT2, DT4 (EL,20,DT5) (EW,12+12,DT3) (EL,25,DT6) (ALQ,12+60,DT1) 24 12
Chapter 3. General Principles 38
20 1 3 1 DT2, DT4, DT5 (EW,24,DT3) (EL,25,DT6) (ALQ,72,DT1) 40 20 24 1 2 1 DT4, DT5 (EL,25,DT6) (EW,24+12,DT2) (ALQ,72,DT1) (ALQ,24+100,DT3) 44 24
Computer Science, Informatik 4 Communication and Distributed Systems
Implementation of Simulations
Implementation of Simulations
Chapter 3. General Principles
Computer Science, Informatik 4 Communication and Distributed Systems
g p p programming language
First simple specific simulation g g y p
I t i l ti
implementation
exponentially distributed with mean 4.5 S i ti l
framework for discrete event simulation
exponentially distributed with mean 3.2
system
Calling population
ti ti+1
Arrivals
Chapter 3. General Principles 40
Server Waiting line
Arrivals
Computer Science, Informatik 4 Communication and Distributed Systems
l k
y
R Ti
Entity attributes
f t E tLi t
more than 4 minutes
M th d
Chapter 3. General Principles 41
Computer Science, Informatik 4 Communication and Distributed Systems
Overall structure of an event-scheduling simulation program Overall structure of the Java program simulation program
Chapter 3. General Principles 42
Computer Science, Informatik 4 Communication and Distributed Systems
A very simple realization of an event y p
class Event { class Event { public double time; public double time; private int type; private int type; public Event(int _type, double _time) { public Event(int _type, double _time) { type = type = _type; _type; time = time = _time; _time; } public int getType() { public int getType() { return type; return type; } public double getTime() { public double getTime() { return time; return time; }
Chapter 3. General Principles 43
} }
Computer Science, Informatik 4 Communication and Distributed Systems
class Sim { class Sim { // Class Sim variables // Class Sim variables // Class Sim variables // Class Sim variables public static double clock, public static double clock, meanInterArrivalTime, meanInterArrivalTime, meanServiceTime, meanServiceTime, lastEventTime lastEventTime lastEventTime lastEventTime, totalBusy, totalBusy, maxQueueLength, maxQueueLength, sumResponseTime; sumResponseTime; public static long numberOfCustomers, public static long numberOfCustomers, queueLength, queueLength, numberInService, numberInService, totalCustomers, totalCustomers, numberOfDepartures, numberOfDepartures, longService; longService; public final static int arrival = 1; public final static int arrival = 1; // Event type for an arrival // Event type for an arrival public final static int departure = 2; public final static int departure = 2; // Event type for a departure // Event type for a departure public final static int departure = 2; public final static int departure = 2; // Event type for a departure // Event type for a departure public static EventList futureEventList; public static EventList futureEventList; // We use these data structures as // We use these data structures as public static Queue customers; public static Queue customers; // given by the system. // given by the system. public static Random stream; public static Random stream; // A random number generator // A random number generator
Chapter 3. General Principles 44
public static Random stream; public static Random stream; // A random number generator // A random number generator
Computer Science, Informatik 4 Communication and Distributed Systems
public static void main(String argv[]) { public static void main(String argv[]) { meanInterArrivalTime = meanInterArrivalTime = 4 5; 5; meanInterArrivalTime = meanInterArrivalTime = 4.5; 5; meanServiceTime meanServiceTime = 3.2; = 3.2; totalCustomers totalCustomers = 1000; = 1000; long long seed = Long.parseLong(argv[0]); seed = Long.parseLong(argv[0]); t R R d ( d) d) // I // I iti iti li li RNG RNG t stream ream = new new R Ran andom
seed) d); // I // Initi itiali lize ze RNG RNG stream ream futureEventList futureEventList = new EventList(); = new EventList(); // Initialize the FEL // Initialize the FEL customers = customers = new new Queue(); Queue(); // Set which holds the customers // Set which holds the customers initialization(); initialization(); // Loop until first “totalCustomers" // Loop until first “totalCustomers" have departed have departed while( numberOfDepartures < totalCustomers ) while( numberOfDepartures < totalCustomers ) { Event event = (Event)futureEventList.getMin(); Event event = (Event)futureEventList.getMin(); // Get imminent event // Get imminent event futureEventList futureEventList dequeue(); dequeue(); // Be rid of it // Be rid of it futureEventList futureEventList.dequeue(); dequeue(); // Be rid of it // Be rid of it clock = event.getTime(); clock = event.getTime(); // Advance simulation time // Advance simulation time if( event.getType() if( event.getType() == arrival == arrival ) { ) { processArrival(event); processArrival(event); } l { l { else se { { processDeparture(event); processDeparture(event); } }
Chapter 3. General Principles 45
reportGeneration(); reportGeneration(); }
Computer Science, Informatik 4 Communication and Distributed Systems
// Seed the event list with TotalCustomers arrivals // Seed the event list with TotalCustomers arrivals public static void initialization() public static void initialization() { { public static void initialization() public static void initialization() { { clock = clock = 0.0; 0.0; queueLength = queueLength = 0; 0; numberInService = numberInService = 0; 0; lastEventTime = lastEventTime = 0.0; 0.0; t t l t t lB totalB lBusy usy = 0 0 ; maxQueueLength = maxQueueLength = 0; 0; sumResponseTime = sumResponseTime = 0; 0; numberOfDepartures = numberOfDepartures = 0; 0; lon longService = Service = 0; 0; g // Create first arrival event // Create first arrival event double eventTime = double eventTime = exponential(stream, MeanInterArrivalTime); exponential(stream, MeanInterArrivalTime); Event event = new Event(arrival, eventTime); Event event = new Event(arrival, eventTime); futureEventList futureEventList enqueue(event); enqueue(event); futureEventList futureEventList.enqueue(event); enqueue(event); }
Chapter 3. General Principles 46
Computer Science, Informatik 4 Communication and Distributed Systems
public static void processArrival(Event public static void processArrival(Event event) { event) { customers customers enqueue(event); enqueue(event); customers customers.enqueue(event); enqueue(event); queueLength++; queueLength++; // If the server is idle, fetch the event, do statistics and put into service // If the server is idle, fetch the event, do statistics and put into service if( numberInService == 0 if( numberInService == 0 ) ) { scheduleDeparture(); scheduleDeparture(); } else { else { totalBusy += totalBusy += (clock (clock - lastEventTime); lastEventTime); // server is busy // server is busy } // Adjust max queue length statistics // Adjust max queue length statistics if(maxQueueLength < if(maxQueueLength < queueLength) queueLength) { maxQueueLength = maxQueueLength = queueLength; queueLength; } // Schedule the next arrival // Schedule the next arrival Double eventTime = Double eventTime = clock + clock + exponential(stream, exponential(stream, meanInterArrivalTime); meanInterArrivalTime); Event nextArrival = Event nextArrival = new Event(arrival, eventTime); new Event(arrival, eventTime); futureEventList.enqueue( nextArrival futureEventList.enqueue( nextArrival ); ); l t l tE tTi l k l k las astE tEven ventTi tTime me = cloc
}
Chapter 3. General Principles 47
Computer Science, Informatik 4 Communication and Distributed Systems
public static void scheduleDeparture() public static void scheduleDeparture() { double serviceTime = double serviceTime = exponential(stream exponential(stream meanServiceTime); meanServiceTime); double serviceTime = double serviceTime = exponential(stream exponential(stream, meanServiceTime); meanServiceTime); Event depart = new Event(departure, Event depart = new Event(departure, clock + serviceTime); clock + serviceTime); futureEventList.enqueue(depart); futureEventList.enqueue(depart); numberInService = numberInService = 1; 1; queueLength--; queueLength--; } public static void processDeparture(Event public static void processDeparture(Event e) { e) { // Get the customer description // Get the customer description Event finished = (Event) customers.de Event finished = (Event) customers.dequeue(); ueue(); q // If there are customers in the queue then schedule the departure of the next one // If there are customers in the queue then schedule the departure of the next one if( queueLength > if( queueLength > 0 0 ) { ) { scheduleDeparture(); scheduleDeparture(); } else { else { else { else { numberInService = numberInService = 0; 0; } // Measure the // Measure the response time and response time and update cumulative statistics update cumulative statistics double response = double response = clock - clock - finished.getTime(); finished.getTime(); R T R Ti + + sum sumResponse esponseTi Time me + += response; response; if( response > 4.0 ) if( response > 4.0 ) longService++; longService++; // record long service // record long service totalBusy += totalBusy += (clock (clock - lastEventTime); lastEventTime); numberOfDepartures++; numberOfDepartures++;
Chapter 3. General Principles 48
lastEventTime = lastEventTime = clock; clock; }
Computer Science, Informatik 4 Communication and Distributed Systems
public public static void static void reportGe reportGene nera ration() { { d bl h l h t t l t t lB / l k dou
ble rho = t totalB lBusy usy/cloc
double avgr double avgr = = sumRespon sumResponseTime/totalCustomers; double double pc4 pc4 = = ((double) ((double)longService)/totalCustomers; System System out println( "SINGLE SERVER QUEUE SIMULATI SINGLE SERVER QUEUE SIMULATION ON - GROCERY GROCERY STORE CHECKOUT STORE CHECKOUT COUNTER COUNTER "); ); System System.ou
SINGLE SERVER QUEUE SIMULATION ON GROCERY GROCERY STORE CHECKOUT STORE CHECKOUT COUNTER COUNTER ); ); System. System.out.println( "\tMEAN "\tMEAN INTERARR INTERARRIVAL TIME TIME " " + + meanInter meanInterAr ArrivalTime ); ); System. System.out.println( "\tMEAN "\tMEAN SERVICE SERVICE TIME TIME " " + + meanServi meanService ceTime ); ); System. System.out.println( "\tNUMBER "\tNUMBER OF OF CUSTOMER CUSTOMERS S SERVED SERVED " " + + totalCust totalCustom
); System. stem.out.pri rint ntln ln(); (); y p y p System. System.out.println( "\tSERVER "\tSERVER UTILIZA UTILIZATI TION ON " " + + rho rho ); ); System. System.out.println( "\tMAXIMU "\tMAXIMUM M LINE LINE LENGTH LENGTH " " + + maxQueueL maxQueueLen ength ); ); System. System.out.println( "\tAVERAG "\tAVERAGE E RESPONSE RESPONSE TIME TIME " " + + avgr avgr + + " " Time Time Units"); Units"); System. System.out.println( "\tPROPOR "\tPROPORTI TION WHO WHO SPEND SPEND FOUR FOUR "); "); System. System.out.println( "\t "\t MINUTES MINUTES OR MORE OR MORE IN IN SYSTEM SYSTEM " + " + pc4 pc4 ); ); System. System.out.println( "\tSIMULA "\tSIMULATI TION RUNLENGTH RUNLENGTH " + " + clock clock + " Time + " Time Units"); Units"); System. System.out.println( "\tNUMBER "\tNUMBER OF OF DEPARTUR DEPARTURES ES " " + + totalCust totalCustom
); }
Chapter 3. General Principles 49
Computer Science, Informatik 4 Communication and Distributed Systems
SINGLE SERVER QUEUE SIMULATION - SINGLE SERVER QUEUE SIMULATION - GROCERY STORE CHECKOUT COUNTER GROCERY STORE CHECKOUT COUNTER MEAN INTERARRIVAL MEAN INTERARRIVAL TIME TIME 4.5 4.5 MEAN SERVICE TIME MEAN SERVICE TIME 3.2 3.2 NUMBER OF CUSTOMERS NUMBER OF CUSTOMERS SERVED SERVED 1000 1000 SERVER UTILIZATION SERVER UTILIZATION 0.718 0.718 MAXIMUM MAXIMUM LINE LENGTH LINE LENGTH 13.0 13.0 AVERAGE AVERAGE RESPONSE TIME RESPONSE TIME 9.563 9.563 PROPORTION WHO SPEND FOUR PROPORTION WHO SPEND FOUR MINUTES MINUTES OR MORE IN SYSTEM OR MORE IN SYSTEM 0.713 0.713 SIMULATION SIMULATION RUNLENGTH RUNLENGTH 4485.635 4485.635 NUMBER OF DEPARTURES NUMBER OF DEPARTURES 1000 1000
Chapter 3. General Principles 50
Computer Science, Informatik 4 Communication and Distributed Systems
Implementation of Simulations
Implementation of Simulations
Chapter 3. General Principles
Computer Science, Informatik 4 Communication and Distributed Systems
Package core Package rng
Chapter 3. General Principles 52
Computer Science, Informatik 4 Communication and Distributed Systems
Chapter 3. General Principles 53
Computer Science, Informatik 4 Communication and Distributed Systems
public class SimEvent { public class SimEvent { double time; double time; double time; double time; int type; int type; SimEntity src; SimEntity src; SimEntity dst; SimEntity dst; public long id; public long id; public SimEvent(SimEntity _dst) { public SimEvent(SimEntity _dst) { type = 0; type = 0; time = 0; time = 0; src = null; src = null; dst = _dst; dst = _dst; } public SimEvent(double _time, SimEntity _dst) { public SimEvent(double _time, SimEntity _dst) { type type = 0; 0; type type 0; 0; time = _time; time = _time; src = null; src = null; dst = _dst; dst = _dst; } public SimEvent(double public SimEvent(double _time, SimEntity _src, SimEntity _dst) { _time, SimEntity _src, SimEntity _dst) { type = 0; type = 0; time = _time; time = _time; src = _src; src = _src;
Chapter 3. General Principles 54
dst = _dst; dst = _dst; }
Computer Science, Informatik 4 Communication and Distributed Systems
public abstract class SimEntity { public abstract class SimEntity { protected SimControl protected SimControl simControl; simControl; protected SimControl protected SimControl simControl; simControl; /** /** * An entity has to know the current instance of the simulator. * An entity has to know the current instance of the simulator. * @param * @param _simControl _simControl * @ * @ Si Si C t l * @ * @see see Si SimCon
rol */ */ public SimEntity(SimControl public SimEntity(SimControl _simControl) { _simControl) { simControl = simControl = _simControl; _simControl; } /** /** * This method handles the events destined to this entity. * This method handles the events destined to this entity. * @param * @param event event * @see SimEvent @see SimEvent @see SimEvent @see SimEvent */ */ abstract public void abstract public void handleEvent(SimEvent handleEvent(SimEvent event); event); }
Chapter 3. General Principles 55
Computer Science, Informatik 4 Communication and Distributed Systems
public abstract class SimQueue { public abstract class SimQueue { /** /** * Schedule the given event according to the event time. * Schedule the given event according to the event time. * @param * @param event event * @see SimEvent * @see SimEvent */ */ */ */ abstract public void abstract public void schedule(SimEvent event); schedule(SimEvent event); /** /** * Return the next event in the * Return the next event in the queue. ueue. q * @return imminent event in queue. * @return imminent event in queue. * @see SimEvent * @see SimEvent */ */ abstract public SimEvent getNextEvent(); abstract public SimEvent getNextEvent(); /** /** * This method dumps the content of the queue. * This method dumps the content of the queue. * It is for debugging * It is for debugging purposes. purposes. */ */ b t t bli t bli id id d () abstrac ract t pu publi blic vo void d id dump ump() (); abstract public boolean isEmpty(); abstract public boolean isEmpty(); }
Chapter 3. General Principles 56
Computer Science, Informatik 4 Communication and Distributed Systems
public class SimControl { public class SimControl { private SimQueue queue; private SimQueue queue; private double time; private double time; private double endTime; private double endTime; public SimControl(SimQueue _queue) { public SimControl(SimQueue _queue) { queue = queue = _queue; _queue; } public void run() { public void run() { SimEvent event; SimEvent event; while( queue.isEmpty() while( queue.isEmpty() == false ) { == false ) { // If there is an event in FEL and the sim-end is not reached ... // If there is an event in FEL and the sim-end is not reached ... event = queue.getNextEvent(); event = queue.getNextEvent(); time = event.getTime(); time = event.getTime(); if( event.getTime() if( event.getTime() <= endTime ) <= endTime ) dispatch(event); dispatch(event); // ... call the destination object of this event // ... call the destination object of this event else else break; break; } }
Chapter 3. General Principles 57
private void dispatch(SimEvent private void dispatch(SimEvent event) { event) { event.getDestination().handleEvent(event); event.getDestination().handleEvent(event); }
Computer Science, Informatik 4 Communication and Distributed Systems
... public class SimControl ... ... public class SimControl ... public void setRunTime(double _runTime) { public void setRunTime(double _runTime) { endTime = _runTime; endTime = _runTime; } bli bli id h id h d l (Si (Si E t t) t) { pu publi blic vo void id sc schedule(Si (SimEven vent t even event) { t) { queue.schedule(event); queue.schedule(event); } public void schedule(SimEvent ublic void schedule(SimEvent event, double event, double _delta) { delta) { p _ p _ event.setTime(time event.setTime(time +_delta); +_delta); schedule(event); schedule(event); }
Chapter 3. General Principles 58
Computer Science, Informatik 4 Communication and Distributed Systems
public abstract class RNG public abstract class RNG { abstract public double getNext(); abstract public double getNext(); abstract public double getNext(); abstract public double getNext(); } bli bli l E E ti l t d RNG { RNG { pu publi blic class ass E Exponen xponenti tial l ex exten ends RNG { RNG { double lambda; double lambda; Random uniform; Random uniform; public Ex ublic Exponential(double
lambda) { p p p p _ lambda = _lambda; lambda = _lambda; uniform = uniform = new Random(System. new Random(System.currentTimeMillis currentTimeMillis()); ()); } /* / / * @see rng.RNG#getNext() * @see rng.RNG#getNext() */ */ public double getNext() { public double getNext() { return -Math. return -Math.log log(uniform.nextDouble())/lambda; (uniform.nextDouble())/lambda; } }
Chapter 3. General Principles 59
Computer Science, Informatik 4 Communication and Distributed Systems
i t d i l ti
framework
customer
Chapter 3. General Principles 60
Computer Science, Informatik 4 Communication and Distributed Systems
Chapter 3. General Principles 61
Computer Science, Informatik 4 Communication and Distributed Systems
8 9 Simulation Theory 8 9 Simulation Theory 4 5 6 7 ystem Time 4 5 6 7 Queueing Time
System Time Queueing Time
1 2 3 4 Sy 1 2 3 Q
System Time Queueing Time
0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 rho 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 rho 0.9 Simulation 0 5 0.6 0.7 0.8 y System Simulation Theory
p0 – Probability that a customer finds the system idle p0
0.2 0.3 0.4 0.5 Probability of Empt
Chapter 3. General Principles 62
0.1 0.1 0.2 0.3 0.4 0.5 0.6 0.7 0.8 0.9 rho
Computer Science, Informatik 4 Communication and Distributed Systems
How to model discrete-event simulations?
How to model discrete event simulations?
Chapter 3. General Principles 63
Computer Science, Informatik 4 Communication and Distributed Systems
Chapter 3. General Principles 64
Computer Science, Informatik 4 Communication and Distributed Systems
providing condition (i) is true
A condition is omitted unconditional edge A, condition is omitted unconditional edge
A B
(i)
Chapter 3. General Principles 65
A B
t
Computer Science, Informatik 4 Communication and Distributed Systems
Arrival
tA
{N++}
Chapter 3. General Principles 66
tA1 tA2 tA3 tA4 tA5
Computer Science, Informatik 4 Communication and Distributed Systems
tS Arrival Start service tA (B<MaxB) End service
S
{Q++} (Q>0) {B--} {Q--, B++}
Chapter 3. General Principles 67
Computer Science, Informatik 4 Communication and Distributed Systems
must be scheduled at the beginning of the run. g g
Run
Chapter 3. General Principles 68
Computer Science, Informatik 4 Communication and Distributed Systems
Arrival Start tA (B<MaxB) End tS Arrival service service {Q++} (Q>0) {B--} {Q--, B++} Run tA {Set value of MaxB, Q=0, B=0}
Special event used as convention to place initial events in FEL events in FEL.
Chapter 3. General Principles 69
Computer Science, Informatik 4 Communication and Distributed Systems
providing condition (i) is true. The parameter j is set to k.
A B(j)
(i)
k
A B(j)
t k
Chapter 3. General Principles 70
Computer Science, Informatik 4 Communication and Distributed Systems
Calling population
Service Group 1 Service Group 2 Service Group N
Chapter 3. General Principles 71
Computer Science, Informatik 4 Communication and Distributed Systems
Arrival Start tA (B1<MaxB1) End tS1 Arrival 1 service 1 service 1 {Q1++} (Q1>0) {B1--} {Q1--, B1++} (Q1>0) tS2 Arrival 2 Start service 2 (B2<MaxB2) End service 2
S2
Chapter 3. General Principles 72
{Q2++} (Q2>0) {B2--} {Q2--, B2++}
Computer Science, Informatik 4 Communication and Distributed Systems
t (j)
1
(j=1) Arrival (j) Start service (j) tA (B(j)<MaxB(j)) End service (j) tS(j) j j (j) (j) {Q(j)++} (Q(j)>0) {B(j)--} {Q(j)--, B(j)++} j j+1 (j<N)
Chapter 3. General Principles 73
(j<N)
Computer Science, Informatik 4 Communication and Distributed Systems
A B
(i)
A B
Chapter 3. General Principles 74
Computer Science, Informatik 4 Communication and Distributed Systems
simple ones simple ones
Chapter 3. General Principles 75
Computer Science, Informatik 4 Communication and Distributed Systems
Chapter 3. General Principles 76