double[] TotalServingTime; // #endregion Fields #region Constructors public Simulation() { TotalOccupancyOfRouter = 0; Package ProcessedPackage = new Package(); NumberOfStreams = 0; GetData(); TimeOfSimulation = CurrentTime =AuxilaryTime= 0; occupancyOfRouter = false; eventQueue=new Heap(); NumberOfReceivedPackages = new double[NumberOfStreams + 1]; NumberOfServedPackages = new double[NumberOfStreams + 1]; ProbabilityOfFailure = new double[NumberOfStreams + 1]; AverageServingTime = new double[NumberOfStreams + 1]; TotalServingTime = new double[NumberOfStreams + 1]; AverageOccupancyOfqueue = new double[NumberOfStreams]; TotalOccupancyOfQueue = new double[NumberOfStreams]; rejected = new double[NumberOfStreams +1]; TotalOccupancyOfRouter = 0; AverageOccupancyOfRouter = 0; for (int i=0;i<=NumberOfStreams;i++) { rejected[i] = 0; TotalServingTime[i] = 0; AverageServingTime[i] = 0; NumberOfReceivedPackages[i] = 0; NumberOfServedPackages[i] = 0; ProbabilityOfFailure[i] = 0; } for (int i = 0; i < NumberOfStreams; i++) { AverageOccupancyOfqueue[i] = 0; TotalOccupancyOfQueue[i] = 0; } }
public void fSimulation() { double previoustime=0; double CurrentTime = 0; Random rnd= new Random(DateTime.Now.Millisecond); while (TimeOfSimulation==0) { try { Console.WriteLine("Prosze podac dodatni czas symulacji w ms: "); TimeOfSimulation=double.Parse(Console.ReadLine()); if (TimeOfSimulation<=0) throw new Exception(); } catch (Exception) { Console.WriteLine("Prosze podaj inna liczbe "); } for (int i=0;i<NumberOfStreams;i++) { double wait=Extractvalue(stream[i].GetNameOfWaitingDistribution(),NumberOfStreams); eventQueue.Insert(Event.Coming(i, wait)); } } while (CurrentTime<TimeOfSimulation) { Event TempEvent=eventQueue.Delete(); previoustime = CurrentTime; CurrentTime=TempEvent.getKey(); if (AuxilaryTime == 0) AuxilaryTime = CurrentTime; else AuxilaryTime = CurrentTime - previoustime; for (int i=0;i<NumberOfStreams;i++) { TotalOccupancyOfQueue[i] +=( queue[i].GetOccupancy()/queue[i].GetSize()) * AuxilaryTime; } if (occupancyOfRouter == true) TotalOccupancyOfRouter += AuxilaryTime; switch(TempEvent.GetKind()) { case KindOfEvent.Coming: { double length=Extractvalue(stream[TempEvent.getStreamNumber()].GetNameOflengthDistribution(),NumberOfStreams); Package TemporaryPackage = new Package(length,TempEvent.getKey()); NumberOfReceivedPackages[TempEvent.getStreamNumber()]++; NumberOfReceivedPackages[NumberOfStreams ]++; if (occupancyOfRouter==false) { occupancyOfRouter = true; double processing = length/flowability; eventQueue.Insert(Event.GoingOutOfRouter(TempEvent.getStreamNumber(), CurrentTime+processing)); ProcessedPackage = TemporaryPackage; } else if (queue[stream[TempEvent.getStreamNumber()].bufor].occupancy + TemporaryPackage.GetSize() < queue[stream[TempEvent.getStreamNumber()].bufor].size) { queue[stream[TempEvent.getStreamNumber()].bufor].SetPackage(TemporaryPackage); } else { rejected[TempEvent.getStreamNumber()] += 1; } double interval = Extractvalue(stream[TempEvent.getStreamNumber()].GetNameOfWaitingDistribution(),NumberOfStreams); eventQueue.Insert(Event.Coming(TempEvent.getStreamNumber(), CurrentTime + interval)); break; } case KindOfEvent.GoingOutOfRouter: { double ServingTime = CurrentTime - ProcessedPackage.GetComingTime(); TotalServingTime[TempEvent.getStreamNumber()] += ServingTime; TotalServingTime[NumberOfStreams] += ServingTime; NumberOfServedPackages[TempEvent.getStreamNumber()]++; NumberOfServedPackages[NumberOfStreams] = NumberOfServedPackages[NumberOfStreams] + 1; occupancyOfRouter = false; for (int i = 0; i < NumberOfQueues; i++) { if (queue[i].occupancy != 0) { occupancyOfRouter = true; ProcessedPackage = queue[i].MoveToStream(); double length = Extractvalue(stream[i].GetNameOflengthDistribution(), NumberOfStreams); double processing = length/ flowability; eventQueue.Insert(Event.GoingOutOfRouter(i, CurrentTime + processing)); break; } } break; } } } SumUp(); double Av1 = rejected[NumberOfStreams] / NumberOfReceivedPackages[NumberOfStreams]; Console.WriteLine("{0} oraz {1}", Av1, ProbabilityOfFailure[NumberOfStreams]); WriteResults(); Console.WriteLine("Zakonczono symulacje"); }