A simulation creates a model of a real-world situation so that we can better understand it. The simulation uses computer objects with methods that model real-world objects and their behaviors. One very popular type of simulation is an event-driven simulation that uses a minimum priority queue to process events that occur over time. The priority queue consists of different types of event objects that are time-stamped to indicate when they are to occur. The simulation identifies an event, notes its time and circumstances, and adds it to the priority queue. Often, the occurrence of an event triggers other related and subsequent events which are added to the queue. The simulation runs by repeated removing and analyzing the next (earliest time) event from the priority queue until the queue is empty of a specified length of time elapses. The simulation provides a progression of discrete events that highlight the key activities in the real world situation.
In the following sections, we will present design principles for all event-driven simulations and use a bank simulation as the example. Code segments that declare variables and implement key methods will aid our discussion.
A Bank Simulation :
In this section, we will describe a bank simulation that looks at the flow of customers through a bank as they are served by a group of tellers. In the process, we will measure the efficiency of service by computing the average waiting time of each customer and the percentage of time each teller is busy. Instead of collecting actual customer data, we introduce probabilistic values that describe different expected arrival rates for customers and different expected service times for a teller to handle a customer. We use a random number generator to mirror the arrival and departure of customers during a bank day. The simulation allows us to introduce new parameters and thus measure the relative effect on service if we change customer or teller behavior. For instance, suppose the bank estimates that a gift promotion would increase customer traffic by 20%. A simulation study would increase the expected arrival rate and measure the effect on waiting times and teller utilization. The result may indicate that customers experience unacceptable wait times, creating dissatisfaction that would diminish the effect of the promotion. The bank could repeat the simulation with additional tellers until reasonable wait times are established.
The bank simulation produces arrival, service and departure events for each customer. Associated variables maintain an ongoing record of service time that each teller is committed to provide customers that are already in the bank. When a customer enters, the simulation creates an arrival event(A) that marks the time. This event spawns a service event(S) indicating when the customer reaches a teller. The time for the service event depends on prior teller commitments. If a teller is free, service begins immediately. Otherwise, the simulation must evaluate the backlogged service times for each teller and select the minimal value. In effect, the value represents the first time a teller is free. The interval between arrival time and service time denotes waiting time for the customer. The service event spawns a departure event that takes into account the time required by the teller to handle the customer. The service time becomes a further commitment for the teller and is used to update the backlogged service time for the teller, which could affect waiting times for subsequent customers.
Below figure looks at four customers who enter a bank that employs two tellers. A time line lays out times for events from the start of the simulation, measured in minutes. The line segments above the timeline detail the events for each customer. The line segments below the timeline track service provided by each teller.
Simulation Design Pattern :
The key components of a simulation are events. These objects are instances of different types of activities and behaviors that occur in the simulation. The object type for the different events are part of an inheritance hierarchy that defines Event as a superclass. The Event class includes a single integer time, which denotes when the event occurs. An abstract method doEvent() is included for polymorphism. The method is overwritten in each subclass to carry out our tasks each time a specific event occurs. The Event superclass implements the Comparable interface by having compareTo() compare the relative times when two events occur. This allows the simulation to store events in a minimum priority queue. The simulation runs by extracting events from the priority queue in order of their times. The simulation is thus like a compressed video recording of a day's activities in which successive frames in the video are the events :
In our example, the inheritance hierarchy includes the Event superclass and subclasses for ArrivalEvent, ServiceEvent and DepartureEvent.
The actual running of the simulation is handled by an EventDrivenSimulation class, which provides a priority queue of Event references. The class features the method pushEvent(), which inserts a new event in the priority queue and the method run() which proceeds to empty the queue in a progressive time sequence of events. Each event that is popped from the queue is processed using doEvent().
BankSimulation Class :
The BankSimulation class drives the bank simulation. By extending EventDrivenSimulation, it has access to the priority queue and the method run() in the superclass that processes events in a time sequence. BankSimulation defines user-supplied parameters that specify the length of the simulation, the number of tellers and the range (high-low) for expected arrival times and service times. The class defines variables that are updated during execution; for instance, number of customers and total wait time. It also supplies supporting variables such as a random number generator and an array of Teller objects. The method startSimulation() inputs parameters for the simulation and creates and stores events in the priority queue(Here is hard code). It then calls run() to execute the simulation and concludes by displaying a summary of the results :
For whole source code, please refer to attachment(Including three event class). The execution result just like below :
作者已經移除這則留言。
回覆刪除作者已經移除這則留言。
回覆刪除