public void Initialize() { int _totalBusStops = TotalNumberOfBusStops; int _busNumber = 1; for (int _nIndex = 1; _nIndex <= _totalBusStops; _nIndex++) { int _currentPassangersAtStop = 0; if (_dicNumberOfPersonsByStopNumber.Keys.Contains(_nIndex)) { _dicNumberOfPersonsByStopNumber.TryGetValue(_nIndex, out _currentPassangersAtStop); } if ((_nIndex % 3).Equals(0)) { //Bus arrival event BusArrivalEvent _busArrivalEvent = new BusArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, BusNumber = _busNumber, ClockTime = _nIndex }; _busArrivalEvent = _busArrivalEvent.CreateEvent(_nIndex) as BusArrivalEvent; AddToQueue(_busArrivalEvent); BoardingEvent _boardingEvent = new BoardingEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, BusNumber = _busNumber, TimeofExecution = _busArrivalEvent.TimeofExecution }; _boardingEvent = _boardingEvent.CreateEvent(_nIndex) as BoardingEvent; AddToQueue(_boardingEvent); _busNumber += 1; } //Person Arrival event. PersonArrivalEvent _personArrival = new PersonArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, ClockTime = _nIndex }; _personArrival = _personArrival.CreateEvent(_nIndex) as PersonArrivalEvent; AddToQueue(_personArrival); _dicNumberOfPersonsByStopNumber.Add(_nIndex, _personArrival.NumberofPersonInQueueAtStop); } }
public void Initialize() { int _totalBusStops = _busTerminalConfiguration.TotalNumberofBusStops; int _busNumber = 1; for (int _nIndex = 1; _nIndex <= _totalBusStops; _nIndex++) { _lblPassengerCount = Controls.Find(string.Format("lblPassengerCount{0}", _nIndex), true).FirstOrDefault() as Label; int _currentPassangersAtStop = 0; int.TryParse(_lblPassengerCount.Text, out _currentPassangersAtStop); if ((_nIndex % 3).Equals(0)) { //Bus arrival event BusArrivalEvent _busArrivalEvent = new BusArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, BusNumber = _busNumber, ClockTime = _nIndex }; _busArrivalEvent = _busArrivalEvent.CreateEvent(_nIndex) as BusArrivalEvent; AddToQueue(_busArrivalEvent); BoardingEvent _boardingEvent = new BoardingEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, BusNumber = _busNumber, TimeofExecution = _busArrivalEvent.TimeofExecution }; _boardingEvent = _boardingEvent.CreateEvent(_nIndex) as BoardingEvent; AddToQueue(_boardingEvent); _busNumber += 1; } //Person Arrival event. PersonArrivalEvent _personArrival = new PersonArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, ClockTime = _nIndex }; _personArrival = _personArrival.CreateEvent(_nIndex) as PersonArrivalEvent; AddToQueue(_personArrival); _lblPassengerCount.Text = _personArrival.NumberofPersonInQueueAtStop.ToString(); } _ntotalBuses = _busNumber; }
public override BusStopEvents ExecuteEvent() { PersonArrivalEvent _event = null; try { _event = this; //increase number of NumberofPersonInQueueAtStop randomly between 1 - 5 and and same has to be updated in dicNumberOfPersonsByStopNumber upon returning _event.NumberofPersonInQueueAtStop = NumberofPersonInQueueAtStop + NextTimeIntervalGenerator.GetPersons(); //TODO::Randomize this ApplicationLog.Instance.WriteInfo(string.Format("New person(s) have arrived at stop#: {0} and total number of passnegers are {1}.", _event.BusStopNumber, _event.NumberofPersonInQueueAtStop)); } catch (Exception ex) { ApplicationLog.Instance.WriteException(ex); } return(_event); }
/// <summary> /// Creates event of type BusArrival and assigns properties to it which then needs to be added in a Queue. /// </summary> /// <param name="BusStopNumber"></param> /// <param name="typeOfEvent"></param> /// <returns></returns> public override BusStopEvents CreateEvent(int BusStopNumber) { PersonArrivalEvent evnt = null; try { evnt = new PersonArrivalEvent() { BusStopNumber = BusStopNumber, TypeofEvent = EventType.PersonArrival }; //Calcuate next execution time. evnt.TimeofExecution = ClockTime + (_averageArrivalTime * NextTimeIntervalGenerator.GetTimeInterval()); } catch (Exception ex) { ApplicationLog.Instance.WriteException(ex); } return(evnt); }
/// <summary> /// Simulation will begin here. /// </summary> public void SimulateEvents() { int _simulationTime = 7200; ApplicationLog.Instance.WriteInfo(string.Format("Simulation started for {0} seconds.", _simulationTime)); if (_lstBusStopevents != null && _lstBusStopevents.Count > 0) { int _clockTime = 1; double _boardinTimeExecution = 0; do { BusStopEvents _event = _lstBusStopevents[0]; BusStopEvents _nextEvent = null; if (_event != null) { int _currentPassangersAtStop = 0; if (_dicNumberOfPersonsByStopNumber.Keys.Contains(_event.BusStopNumber)) { _dicNumberOfPersonsByStopNumber.TryGetValue(_event.BusStopNumber, out _currentPassangersAtStop); } int _eventBusStopNumber = _event.BusStopNumber; _event.NumberofPersonInQueueAtStop = _currentPassangersAtStop; _lstBusStopevents.Remove(_event); switch (_event.TypeofEvent) { #region EventType.PersonArrival case EventType.PersonArrival: //Execute the current event _event = _event.ExecuteEvent(); _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop; //Create Person Arrival event for next person. _nextEvent = new PersonArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, ClockTime = _clockTime }; _nextEvent = _nextEvent.CreateEvent(_eventBusStopNumber) as PersonArrivalEvent; break; #endregion #region EventType.BusArrial case EventType.BusArrial: //TODO:: While creating a new Bus event, read the lstEventsToExecute. IEnumerable <BusArrivalEvent> _lstSameTimeBusses = _lstBusStopevents.Where(x => x.TypeofEvent.Equals(EventType.BusArrial) && x.TimeofExecution.Equals(_event.TimeofExecution) && x.BusStopNumber.Equals(_event.BusStopNumber)) as IEnumerable <BusArrivalEvent>; if (_lstSameTimeBusses != null && _lstSameTimeBusses.Any()) { IEnumerable <int> _lstWaitingBusNumbers = _lstSameTimeBusses.Select(x => x.BusNumber); string sBusNumbers = string.Empty; if (_lstWaitingBusNumbers != null && _lstWaitingBusNumbers.Any()) { foreach (int nbusnum in _lstWaitingBusNumbers) { sBusNumbers = string.Format("{0},{1}", sBusNumbers, nbusnum); } } if (!string.IsNullOrEmpty(sBusNumbers)) { ApplicationLog.Instance.WriteInfo(string.Format("Busesses {0} are waiting at {1} becuase another bus is already boarding passengers.", sBusNumbers, _event.BusStopNumber)); } } _event = _event.ExecuteEvent(); _nextEvent = new BusArrivalEvent(); //Bus arrival for next bus stop. _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1); _nextEvent.NumberofPersonInQueueAtStop = _currentPassangersAtStop; _nextEvent.ClockTime = _clockTime; _boardinTimeExecution = _nextEvent.TimeofExecution; break; #endregion #region EventType.PersonBoarding case EventType.PersonBoarding: _event = _event.ExecuteEvent(); //Make passengers at bus stop 0 since they are all boarded. _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = 0; _event.TimeofExecution = _boardinTimeExecution; _boardinTimeExecution = 0; //Boarding event for next bus stop _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1); break; #endregion } AddToQueue(_nextEvent); } //Console.WriteLine(_simulationTime); _simulationTime -= 1; _clockTime += 1; }while (_simulationTime > 0); ApplicationLog.Instance.WriteInfo(string.Format("Simulation ended after {0} seconds.", _simulationTime)); } else { ApplicationLog.Instance.WriteError("Stopping simulation due to in appropriate initialization"); } }
/// <summary> /// Simulation will begin here. /// </summary> public void SimulateEvents() { if (_lstBusStopevents != null && _lstBusStopevents.Count > 0) { BusStopEvents _event = _lstBusStopevents[0]; BusStopEvents _nextEvent = null; if (_event != null) { int _eventBusStopNumber = _event.BusStopNumber; _txtStop = Controls.Find(string.Format("txtStop{0}", _event.BusStopNumber), true).FirstOrDefault() as TextBox; _lblPassengerCount = Controls.Find(string.Format("lblPassengerCount{0}", _event.BusStopNumber), true).FirstOrDefault() as Label; int _currentPassangersAtStop = 0; int.TryParse(_lblPassengerCount.Text, out _currentPassangersAtStop); _txtStop.Text = _event.TypeofEvent.ToString(); _event.NumberofPersonInQueueAtStop = _currentPassangersAtStop; _lstBusStopevents.Remove(_event); switch (_event.TypeofEvent) { #region EventType.PersonArrival case EventType.PersonArrival: //Execute the current event _event = _event.ExecuteEvent(); _lblPassengerCount.Text = _event.NumberofPersonInQueueAtStop.ToString(); //Create Person Arrival event for next person. _nextEvent = new PersonArrivalEvent() { NumberofPersonInQueueAtStop = _currentPassangersAtStop, ClockTime = _timerCounter }; _nextEvent = _nextEvent.CreateEvent(_eventBusStopNumber) as PersonArrivalEvent; _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop; break; #endregion #region EventType.BusArrial case EventType.BusArrial: //TODO:: While creating a new Bus event, read the lstEventsToExecute. IEnumerable <BusArrivalEvent> _lstSameTimeBusses = _lstBusStopevents.Where(x => x.TypeofEvent.Equals(EventType.BusArrial) && x.TimeofExecution.Equals(_event.TimeofExecution) && x.BusStopNumber.Equals(_event.BusStopNumber)) as IEnumerable <BusArrivalEvent>; if (_lstSameTimeBusses != null && _lstSameTimeBusses.Any()) { IEnumerable <int> _lstWaitingBusNumbers = _lstSameTimeBusses.Select(x => x.BusNumber); string sBusNumbers = string.Empty; if (_lstWaitingBusNumbers != null && _lstWaitingBusNumbers.Any()) { foreach (int nbusnum in _lstWaitingBusNumbers) { sBusNumbers = string.Format("{0},{1}", sBusNumbers, nbusnum); } } if (!string.IsNullOrEmpty(sBusNumbers)) { ApplicationLog.Instance.WriteInfo(string.Format("Busesses {0} are waiting at {1} becuase another bus is already boarding passengers.", sBusNumbers, _event.BusStopNumber)); } } _event = _event.ExecuteEvent(); _nextEvent = new BusArrivalEvent(); //Bus arrival for next bus stop. _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1); _nextEvent.NumberofPersonInQueueAtStop = _currentPassangersAtStop; _nextEvent.ClockTime = _timerCounter; _boardinTimeExecution = _nextEvent.TimeofExecution; BusArrivalEvent _event1 = _event as BusArrivalEvent; chartBusArrival.Series[string.Format("BusNumber{0}", _event1.BusNumber)].Points.AddXY(_timerCounter, _event.BusStopNumber); break; #endregion #region EventType.PersonBoarding case EventType.PersonBoarding: timer1.Stop(); //int _nStoredPassngers = 0; //_dicNumberOfPersonsByStopNumber.TryGetValue(_eventBusStopNumber, out _nStoredPassngers); //if (_nStoredPassngers < _event.NumberofPersonInQueueAtStop) // _dicNumberOfPersonsByStopNumber[_eventBusStopNumber] = _event.NumberofPersonInQueueAtStop; ApplicationLog.Instance.WriteInfo(string.Format("There are {0} passengers at stop# {1} and hence bus will be at stop for {2}.", _event.NumberofPersonInQueueAtStop, _event.BusStopNumber, _event.BoardingTime * _event.NumberofPersonInQueueAtStop)); timer2.Tick += (sender, e) => timer2_Tick(sender, e, _event, _lblPassengerCount); timer2.Interval = _busTerminalConfiguration.TimerInterval; timer2.Start(); //Make passengers at bus stop 0 since they are all boarded. _event.TimeofExecution = _boardinTimeExecution; _boardinTimeExecution = 0; //Boarding event for next bus stop _nextEvent = _event.CreateEvent(_eventBusStopNumber + 1); break; #endregion } AddToQueue(_nextEvent); } CheckMainCounterStopCondition(); } else { ApplicationLog.Instance.WriteError("Stopping simulation due to in appropriate initialization"); } }