} // end GenerateArrivalEvents private void GenerateDepartureEvent(Registrant registrant) { // generates a random interval for how long the person takes, with a minimum of 1 minute // and 30 seconds TimeSpan wait = new TimeSpan(0, 0, (int)(90 + NegExp(AvgRegistrationTime.TotalSeconds - 90))); // Creates departure event based on the time interval plus the time we are currently // at in the Priority Queue Event newDepartureEvent = new Event(EventType.DEPARTURE, ((EventPQ.Peek( ).Time + wait))); //Associates departure event with registrant newDepartureEvent.Registrant = registrant; registrant.Departure = newDepartureEvent; // Adds departure event to the queue EventPQ.Enqueue(newDepartureEvent); // Update wait times for statistic counters TotalWait += wait; if (wait > LongestWait) { LongestWait = wait; } if (wait < ShortestWait) { ShortestWait = wait; } } // end GenerateDepartureEvents
} // end SetupSimulation #region Generate Arrival and Departure Events /// <summary> /// Generates the arrival events for registrants and adds them to event Priority Queue /// </summary> private void GenerateArrivalEvents( ) { TimeSpan start; TimeSpan timeOpen = EndTime - StartTime; for (int registrant = 1; registrant <= NumRegistrants; registrant++) { // Generate random amount of time between 0 and registration open period start = new TimeSpan(0, 0, R.Next((int)timeOpen.TotalSeconds)); // Create arrival event Event newEvent = new Event(EventType.ARRIVAL, (StartTime + start)); // Create registrant Registrant newRegistrant = new Registrant(registrant, newEvent); // Associate registrant with event newEvent.Registrant = newRegistrant; newRegistrant.Arrival = newEvent; // Add arrival event to Priority Queue of events EventPQ.Enqueue(newEvent); } } // end GenerateArrivalEvents
} // end GenerateDepartureEvents #endregion public void RunSimulation( ) { Queue <Registrant> shortestLine = null; Queue <Registrant> currentLine = null; if (EventPQ.IsEmpty( )) // Indicates simulation is finished running { Complete = true; return; } if (EventPQ.Peek( ).Type == EventType.ARRIVAL) // If the event is an arrival { // Find shortest line and add registrant shortestLine = FindShortestLine( ); shortestLine.Enqueue(EventPQ.Peek( ).Registrant); // Increment if new largest queue found if (shortestLine.Count > LargestQueue) { LargestQueue++; } // Generate departure event if this customer is at the front of the line if (shortestLine.Count == 1) { GenerateDepartureEvent(shortestLine.Peek( )); } // Event completely handled; remove the event and add to counter EventPQ.Dequeue( ); TotalArrivals++; TotalEvents++; } else // If the event is a departure { // Get the line that registrant is about to leave for (int i = 0; i < WaitLines.Count; i++) { if (WaitLines[i].Count > 0) { if (WaitLines[i].Peek( ).ID == EventPQ.Peek( ).Registrant.ID) { currentLine = WaitLines[i]; break; } } } // Remove registrant from wait line currentLine.Dequeue( ); TotalDepartures++; TotalEvents++; // If the wait line is not empty, generate a departure event for the next registrant if (currentLine.Count > 0) { GenerateDepartureEvent(currentLine.Peek( )); } // Departure event handled; remove from Priority Queue EventPQ.Dequeue( ); } } // end RunSimulation