Discrete-Event System Simulation in Java
Discrete Event Systems New dynamic systems New dynamic systems Computer and communication networks Computer and communication networks Automated manufacturing systems Automated manufacturing systems Air traffic control systems Air traffic control systems … Operational rules designed by humans Operational rules designed by humans Laws of nature are inappropriate Laws of nature are inappropriate Dynamics characterized by asynchronous occurrences of discrete events Dynamics characterized by asynchronous occurrences of discrete events Hitting a keyboard key Hitting a keyboard key Sending a message packet Sending a message packet Equipment failure Equipment failure
Modeling Frameworks for Discrete Event Systems Automata Automata Petri Nets Petri Nets Markov Chains Markov Chains Queues Queues
Automata q r f (q, e ) = r Given Event sequence {e 1, e 2, …} Initial State q 0 We can determine the state sequence {q 0, f (q 0, e 1 ), f (f (q 0, e 1 ), e 2 ), …}
Stochastic Timed Automata
Example M/M/1/K M/M/1/K 0 1K d aa d … a aaa ddd STATE EVENTS V a,3 V d,2 V d,3 V d,1 V a,
Discrete Event System Simulation Concerns with modeling of a DES as it evolves over time by a representation in which the system variables change at discrete points on time Characteristics: time is a part of simulation (Dynamic) time is a part of simulation (Dynamic) contains random elements (Stochastic) contains random elements (Stochastic)
World Views for Model Development Event-scheduling approach Event-scheduling approach Concentrates of events and their effects on system state Concentrates of events and their effects on system state e.g. simulation in Java, hand-simulation e.g. simulation in Java, hand-simulation Process-interaction approach Process-interaction approach Concentrates on entities, and their life cycles Concentrates on entities, and their life cycles Life cycles consist of events, activities and delays Life cycles consist of events, activities and delays Activities might require resources that are limited. Activities might require resources that are limited. e.g. Arena e.g. Arena Activity-scanning approach Activity-scanning approach Concentrates on activities and conditions that allow them to begin Concentrates on activities and conditions that allow them to begin e.g. some simulation packages popular in UK and Europe e.g. some simulation packages popular in UK and Europe
Time Advance Advancing simulation time Fixed time advanceVariable time advance Advances the clock by one unit time & examines the conditions to decide whether any activities can begin (NOT EFFICIENT) Activity-scanning approach Advances the clock to the most imminent event time & examines the system state Event-scheduling Approach Process-interaction Approach
Event Scheduling Approach Step 0: Initialize STATE to q 0, simulation TIME to zero Populate the Scheduled Event List (SEL) with feasible events for q 0 while setting their scheduled event times as t e = v e,1 Reorder SEL based on a smallest-scheduled-time-first scheme. Step 1: Remove the first entry (e 1, t 1 ) from the SEL Step 2: Update simulation TIME by advancing it to the new event time t 1 Step 3: Update STATE according to the state transition function q ’=f (q, e 1 ) Step 4: Delete from the SEL any entries corresponding to infeasible events in the new state Step 5: Add to the SEL any feasible event which is not already scheduled (possibly including the triggering event removed in the first step). The scheduled event times are calculated by t e = TIME + v e Step 6: Reorder the updated SEL. Go to Step 1.
INITIALIZE STATE q TIME t SCHEDULED EVENT LIST e1e1 t1t1 e2e2 t2t2 :: UPDATE STATE q’ = f (q, e 1 ) UPDATE TIME t’ = t 1 q’ q t’ DELETE INFEASIBLE (e k, t k ) ADD NEW FEASIBLE (e k, t’ + v k ) AND REORDER q’t’ RANDOM VARIATE GENERATOR New Event Lifetime v k
Simulation in Java Common Components Clock : A variable defining simulated time Initialization Method: A method to define the system state at time 0 Min-time event method: A method that identifies the imminent event, the element of the SEL that has the smallest scheduled event time Event methods: For each event type, a method to update system state (and cumulative statistics) when that event occurs Random-variate generators: Methods to generate samples from desired probability distributions Main program: To maintain overall control of the event-scheduling algorithm Report generator: A method that computes summary statistics from cumulative statistics and prints a report at the end of the simulation
Event Scheduling Flow Chart
Stopping Criteria Simulation Run Length Fixed time Variable time Example: 2 weeks 1 year 1 shift Example: 1000 job completions 50 departures 4000 arrivals
Java Simulation of M/M/1
Main Program in Java public static void main(String argv[]) { Initialization(); //Loop until first "TotalCustomers" have departed while (NumberofDepartures < TotalCustomers) { Event evt = FutureEventList[0]; //get imminent event removefromFEL(); //be rid of it Clock = evt.get_time(); //advance in time if (evt.get_type() == arrival) ProcessArrival(); else ProcessDeparture(); } ReportGeneration(); }
Arrival Event Arrival event Update Statistics Increase number in the system Set service time & schedule departure Increase entity number in queue Make server busy Is server busy? NOYES L(t)=L(t)+1 Q(t)=Q(t)+1 B(t)=1 Schedule next arrival
Arrival Event public static void ProcessArrival() { //Update Statistical Accumulators updateStats(arrival); Entity newEntity = new Entity(Clock); //create a new entity if (NumberInService == 0)//if the server is idle { custinService = newEntity;//fetch the entity ScheduleDeparture(newEntity); //schedule its departure } else custQueue.Enqueue(newEntity); //add the new entity to the queue //schedule the next arrival Event next_arrival=new Event(arrival,Clock+exponential(stream,MeanInterArrivalTime)); addtoFEL(next_arrival); LastEventTime=Clock; }
Departure Event Departure event Decrease number in system Set service time & schedule departure for entity in service Make server idle Decrease number in queue Is queue empty? NOYES L(t)=L(t)-1 Q(t)=Q(t)-1 B(t)=0 Update Statistics
Departure Event public static void ProcessDeparture() { updateStats(departure); //update statistical accumulators //if there are customers in the queue then schedule // the departure of the next one QueueLength = custQueue.get_Count(); if (QueueLength > 0) { Entity newEntity = (Entity)custQueue.Dequeue(); ScheduleDeparture(newEntity); custinService = newEntity; } else { NumberInService = 0; custinService = null; } LastEventTime = Clock; }