public int GetServerUtilizationMethod(SimulationSystem simulationSystem) { List <Tuple <decimal, int> > list = new List <Tuple <decimal, int> >(); for (int i = 0; i < simulationSystem.Servers.Count; i++) { var tuple = new Tuple <decimal, int>(simulationSystem.Servers[i].TotalWorkingTime, i); list.Add(tuple); } list.Sort(); int idx = list[0].Item2, mn = 100000000; for (int i = 0; i < simulationSystem.Servers.Count; i++) { if (simulationSystem.Servers[i].TotalWorkingTime == list[0].Item1) { if (simulationSystem.Servers[i].Customer < mn) { mn = (int)simulationSystem.Servers[i].Customer; idx = i; } } } // return idx; return(list[0].Item2); }
public void ArrivalTimeCalc(SimulationSystem s) { s.SimulationTable[0].ArrivalTime = 0; for (int i = 1; i < s.SimulationTable.Count(); i++) { s.SimulationTable[i].ArrivalTime = s.SimulationTable[i - 1].ArrivalTime + s.SimulationTable[i].InterArrival; } }
public int mxQueueLength(SimulationSystem s) { SimulationSystem ss = new SimulationSystem(); int length = 0; int max = 0; for (int i = 0; i < s.SimulationTable.Count(); i++) { if (s.SimulationTable[i].TimeInQueue > 0) { QSC.Enqueue(s.SimulationTable[i]); } } if (QSC.Count() > 0) { max = 1; } for (int x = 0; x < QSC.Count(); x++) { ss.SimulationTable.Add(new SimulationCase()); } for (int z = 0; z < ss.SimulationTable.Count(); z++) { ss.SimulationTable[z] = QSC.Dequeue(); } for (int q = 0; q < ss.SimulationTable.Count(); q++) { int counter = 0; if (q == 0) { length++; } else { for (int j = q - 1; j >= 0; j--) { if (ss.SimulationTable[q].ArrivalTime < ss.SimulationTable[j].StartTime) { counter++; } } if (counter >= length) { length++; if (length > max) { max = length; } } } } s.PerformanceMeasures.MaxQueueLength = max; return(max); }
public void CalcServerPerformance(SimulationSystem system) { system.CalcTotalSimulationTime(); for (int i = 0; i < system.Servers.Count; i++) { system.Servers[i].CalcTotalWorkingTime(system); system.Servers[i].CalcAvgServiceTime(); system.Servers[i].CalcIdleProb(system); system.Servers[i].CalcUtilization(system); } }
public void CalcUtilization(SimulationSystem system) { if (system.TotalSimulationTime != 0) { Utilization = (decimal)TotalWorkingTime / system.TotalSimulationTime; } else { Utilization = 0; } }
public void CalcTotalWorkingTime(SimulationSystem system) { for (int i = 0; i < system.SimulationTable.Count; i++) { if (system.SimulationTable[i].ServerIndex == ID) { TotalWorkingTime += system.SimulationTable[i].ServiceTime; TotalServedCustomers++; } } }
public TaskSimulation() { system = new SimulationSystem(); queue = new Queue <Customer>(); customers = new List <Customer>(); trakeServers = new int[200][]; for (int i = 0; i < 200; i++) { trakeServers[i] = new int[10000]; } }
public void CalcIdleProb(SimulationSystem system) { TotalIdleTime = system.TotalSimulationTime - TotalWorkingTime; if (system.TotalSimulationTime != 0) { IdleProbability = (decimal)TotalIdleTime / system.TotalSimulationTime; } else { IdleProbability = 0; } }
public decimal average_waiting_time(SimulationSystem s) { decimal totalInQueue = 0; for (int i = 0; i < s.SimulationTable.Count(); i++) { totalInQueue += s.SimulationTable[i].TimeInQueue; } if (s.SimulationTable.Count() != 0) { s.PerformanceMeasures.AverageWaitingTime = totalInQueue / s.SimulationTable.Count(); } return(s.PerformanceMeasures.AverageWaitingTime); }
public void Graph_Handler(int ID, SimulationSystem s) { ssss = new SimulationSystem(); int j = 0; for (int i = 0; i < s.SimulationTable.Count(); i++) { if (s.SimulationTable[i].AssignedServer.ID == ID) { ssss.SimulationTable.Add(new SimulationCase()); ssss.SimulationTable[j] = s.SimulationTable[i]; j++; } } }
public void CalcSysPerformance(SimulationSystem system) { system.waitingInfo = system.WaitingCustomers(); if (system.SimulationTable.Count != 0) { AverageWaitingTime = (decimal)system.waitingInfo.WaitingTime / system.SimulationTable.Count; WaitingProbability = (decimal)system.waitingInfo.Count / system.SimulationTable.Count; } else { AverageWaitingTime = 0; WaitingProbability = 0; } CalcMaxQueue(system); //MessageBox.Show("da fo2 " + MaxQueueLength.ToString()); }
public decimal Utilization(int ID, SimulationSystem s) { decimal avg = 0; decimal total = s.SimulationTable[s.SimulationTable.Count() - 1].EndTime; for (int i = 0; i < sepcserver.SimulationTable.Count(); i++) { avg += sepcserver.SimulationTable[i].EndTime - sepcserver.SimulationTable[i].StartTime; } if (total != 0) { s.Servers[ID - 1].Utilization = avg / total; } T = 0; return(s.Servers[ID - 1].Utilization); }
public decimal serverAVGtime(int ID, SimulationSystem s) { decimal avg = 0; decimal totalcust = sepcserver.SimulationTable.Count(); for (int i = 0; i < sepcserver.SimulationTable.Count(); i++) { avg += sepcserver.SimulationTable[i].EndTime - sepcserver.SimulationTable[i].StartTime; } if (totalcust != 0) { s.Servers[ID - 1].AverageServiceTime = avg / totalcust; } return(s.Servers[ID - 1].AverageServiceTime); }
public decimal probOfWaitInSystem(SimulationSystem s) { decimal counter = 0; for (int i = 0; i < s.SimulationTable.Count(); i++) { if (s.SimulationTable[i].TimeInQueue > 0) { counter++; } } if (s.SimulationTable.Count() != 0) { s.PerformanceMeasures.WaitingProbability = counter / s.SimulationTable.Count(); } return(s.PerformanceMeasures.WaitingProbability); }
public void CalcMaxQueue(SimulationSystem system) { List <int> PartialSum = new List <int>(); List <PAIR> Ranges = new List <PAIR>(); int mx = -100000; for (int i = 0; i < system.SimulationTable.Count; i++) { int cur = system.SimulationTable[i].EndTime; if (mx < cur) { mx = cur; } PAIR tmp; tmp.first = system.SimulationTable[i].ArrivalTime; tmp.second = system.SimulationTable[i].StartTime; if (tmp.first != tmp.second) { Ranges.Add(tmp); } } for (int i = 0; i <= mx; i++) { PartialSum.Add(0); } for (int i = 0; i < Ranges.Count; i++) { PartialSum[Ranges[i].first]++; PartialSum[Ranges[i].second + 1]--; } MaxQueueLength = -100000; for (int i = 1; i < PartialSum.Count; i++) { PartialSum[i] += PartialSum[i - 1]; if (MaxQueueLength < PartialSum[i]) { MaxQueueLength = PartialSum[i]; } } //MessageBox.Show("da ta7t " + MaxQueueLength.ToString()); }
public decimal serveridlePROB(int ID, SimulationSystem s) { sepcserver = new SimulationSystem(); decimal prob = 0; decimal total = s.SimulationTable[s.SimulationTable.Count() - 1].EndTime; for (int q = 0; q < s.SimulationTable.Count(); q++) { if (ID == s.SimulationTable[q].AssignedServer.ID) { sepcserver.SimulationTable.Add(new SimulationCase()); sepcserver.SimulationTable[T] = s.SimulationTable[q]; T++; } } int last = sepcserver.SimulationTable.Count(); for (int i = 1; i < sepcserver.SimulationTable.Count(); i++) { prob += sepcserver.SimulationTable[i].StartTime - sepcserver.SimulationTable[i - 1].EndTime; } if (last == 0) { prob = total; } else { prob += sepcserver.SimulationTable[0].StartTime - s.SimulationTable[0].StartTime; prob += total - sepcserver.SimulationTable[last - 1].EndTime; } if (total != 0) { s.Servers[ID - 1].IdleProbability = prob / total; } return(s.Servers[ID - 1].IdleProbability); }
public int GetServerPriorityMethod(SimulationSystem simulationSystem, int ArrivalTime) { if (simulationSystem.Servers[0].FinishTime <= ArrivalTime) { return(0); } int min = 1000000000, idx = -1; for (int i = 0; i < simulationSystem.Servers.Count; i++) { if (simulationSystem.Servers[i].FinishTime <= ArrivalTime) { return(i); } if (simulationSystem.Servers[i].FinishTime < min) { min = simulationSystem.Servers[i].FinishTime; idx = i; } } return(idx); }
public int GetServerRandomMethod(SimulationSystem simulationSystem, int ArrivalTime) { List <int> idleServers = new List <int>(); for (int i = 0; i < simulationSystem.Servers.Count; i++) { if (simulationSystem.Servers[i].FinishTime <= ArrivalTime) { idleServers.Add(i); } } if (idleServers.Count != 0) { Random rand = new Random(); return(idleServers[rand.Next(0, idleServers.Count)]); } int min = 1000000000; for (int i = 0; i < simulationSystem.Servers.Count; i++) { if (simulationSystem.Servers[i].FinishTime < min) { min = simulationSystem.Servers[i].FinishTime; } } idleServers.Clear(); for (int i = 0; i < simulationSystem.Servers.Count; i++) { if (simulationSystem.Servers[i].FinishTime == min) { idleServers.Add(i); } } Random rand1 = new Random(); return(idleServers[rand1.Next(0, idleServers.Count)]); }
//=================================================================================================================== //===============================CASE HANDLER (FOR NUMBER OF CUS OR TIME)========================================= public void GenerateRand(SimulationSystem s) { int RINER, RSERVICE_T; //=================FOR CEREATION NUMBER OF CUSTOMERS========================== if (s.StoppingCriteria == Enums.StoppingCriteria.NumberOfCustomers) { for (int i = 0; i < s.StoppingNumber; i++) { RINER = Randomize(); SimulationTable.Add(new SimulationCase()); SimulationTable[i].RandomInterArrival = RINER; countofcustomer++; } SimulationTable[0].InterArrival = 0; for (int x = 1; x < countofcustomer; x++) { for (int i = 0; i < InterarrivalDistribution.Count(); i++) { if (SimulationTable[x].RandomInterArrival >= InterarrivalDistribution[i].MinRange && SimulationTable[x].RandomInterArrival <= InterarrivalDistribution[i].MaxRange) { SimulationTable[x].InterArrival = InterarrivalDistribution[i].Time; break; } } } for (int i = 0; i < countofcustomer; i++) { RSERVICE_T = Randomize(); SimulationTable[i].RandomService = RSERVICE_T; } } //=========================FOR CEREATION TIME============================================ else if (s.StoppingCriteria == Enums.StoppingCriteria.SimulationEndTime) { while (shrouk <= s.StoppingNumber) { RINER = Randomize(); SimulationTable.Add(new SimulationCase()); SimulationTable[countofcustomer].RandomInterArrival = RINER; for (int i = 0; i < InterarrivalDistribution.Count(); i++) { if (SimulationTable[countofcustomer].RandomInterArrival >= InterarrivalDistribution[i].MinRange && SimulationTable[countofcustomer].RandomInterArrival <= InterarrivalDistribution[i].MaxRange) { SimulationTable[countofcustomer].InterArrival = InterarrivalDistribution[i].Time; shrouk += SimulationTable[countofcustomer].InterArrival; break; } } if (shrouk == s.StoppingNumber) { countofcustomer++; break; } countofcustomer++; } } for (int i = 0; i < countofcustomer; i++) { RSERVICE_T = Randomize(); SimulationTable[i].RandomService = RSERVICE_T; } }
public List <SimulationCase> CalculateMethod(SimulationSystem simulationSystem) { List <SimulationCase> Output = new List <SimulationCase>(); SimulationCase simulationCase = new SimulationCase(); int NumberOfCustomer = simulationSystem.StoppingNumber; int number = 1; Random rand = new Random(); Queue <int> q = new Queue <int>(); while (true) { simulationCase = new SimulationCase(); simulationCase.CustomerNumber = number; if (simulationCase.CustomerNumber == 1) { simulationCase.RandomInterArrival = rand.Next(1, 101); simulationCase.InterArrival = 0; simulationCase.ArrivalTime = 0; } else { simulationCase.RandomInterArrival = rand.Next(1, 101); simulationCase.InterArrival = simulationSystem.getInterArrivalTimeByRandomRange(simulationCase.RandomInterArrival); simulationCase.ArrivalTime = simulationCase.InterArrival + Output[Output.Count - 1].ArrivalTime; } int idx; if (simulationSystem.SelectionMethod == Enums.SelectionMethod.HighestPriority) { idx = GetServerPriorityMethod(simulationSystem, simulationCase.ArrivalTime); } else if (simulationSystem.SelectionMethod == Enums.SelectionMethod.Random) { idx = GetServerRandomMethod(simulationSystem, simulationCase.ArrivalTime); } else { idx = GetServerUtilizationMethod(simulationSystem); } simulationSystem.PerformanceMeasures.MaxQueueLength = Math.Max(simulationSystem.PerformanceMeasures.MaxQueueLength, q.Count()); while (q.Count > 0) { if (q.Peek() <= simulationCase.ArrivalTime) { q.Dequeue(); } else { break; } } if (simulationSystem.Servers[idx].FinishTime == 0) { simulationCase.StartTime = simulationCase.ArrivalTime; } else { simulationCase.StartTime = Math.Max(simulationSystem.Servers[idx].FinishTime, simulationCase.ArrivalTime); } simulationCase.RandomService = rand.Next(1, 101); simulationCase.ServiceTime = simulationSystem.Servers[idx].getServiceTimeByRandomRange(simulationCase.RandomService); simulationCase.EndTime = simulationCase.StartTime + simulationCase.ServiceTime; simulationCase.AssignedServer = simulationSystem.Servers[idx]; if (simulationSystem.StoppingCriteria == Enums.StoppingCriteria.SimulationEndTime) { if (simulationCase.ArrivalTime >= simulationSystem.StoppingNumber) { break; } } simulationSystem.Servers[idx].FinishTime = simulationCase.EndTime; simulationSystem.Servers[idx].TotalWorkingTime += simulationCase.ServiceTime; simulationSystem.Servers[idx].Customer++; simulationCase.TimeInQueue = Math.Abs(simulationCase.StartTime - simulationCase.ArrivalTime); simulationSystem.Servers[idx].BusyTime.Add(new Tuple <int, int>(simulationCase.StartTime, simulationCase.EndTime)); if (simulationCase.TimeInQueue > 0) { q.Enqueue(simulationCase.StartTime); } if (number == NumberOfCustomer) { Output.Add(simulationCase); break; } number++; Output.Add(simulationCase); } return(Output); }
//==================================PIRIORITY===================================================================================== public void piriority(int customer, SimulationSystem s) { for (int i = 0; i < Servers.Count(); i++) { if (Servers[i].FinishTime <= s.SimulationTable[customer - 1].ArrivalTime) { Servers[i].IsIdle = true; } } for (int x = 0; x < Servers.Count(); x++) { if (s.Servers[x].IsIdle == true) { s.SimulationTable[customer - 1].AssignedServer.ID = x + 1; s.SimulationTable[customer - 1].AssignedServer.IsIdle = false; s.Servers[x].IsIdle = false; for (int j = 0; j < Servers[x].TimeDistribution.Count(); j++) { if (s.SimulationTable[customer - 1].RandomService <= Servers[x].TimeDistribution[j].MaxRange && s.SimulationTable[customer - 1].RandomService >= Servers[x].TimeDistribution[j].MinRange) { s.SimulationTable[customer - 1].ServiceTime = Servers[x].TimeDistribution[j].Time; s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime; s.SimulationTable[customer - 1].TimeInQueue = s.SimulationTable[customer - 1].StartTime - s.SimulationTable[customer - 1].ArrivalTime; s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime + s.SimulationTable[customer - 1].TimeInQueue; s.SimulationTable[customer - 1].EndTime = s.SimulationTable[customer - 1].ServiceTime + s.SimulationTable[customer - 1].StartTime; Servers[x].FinishTime = s.SimulationTable[customer - 1].EndTime; counterToAddInQueue = 0; break; } } if (counterToAddInQueue == 0) { break; } } counterToAddInQueue++; } if (counterToAddInQueue == Servers.Count()) { int min = 9999999; int id = 0; int index = 0; for (int i = 0; i < Servers.Count(); i++) { int z = Servers[i].FinishTime; if (z < min) { min = z; id = i + 1; index = i; } s.SimulationTable[customer - 1].AssignedServer.ID = id; } for (int j = 0; j < Servers[id - 1].TimeDistribution.Count(); j++) { if (s.SimulationTable[customer - 1].RandomService <= Servers[id - 1].TimeDistribution[j].MaxRange && s.SimulationTable[customer - 1].RandomService >= Servers[id - 1].TimeDistribution[j].MinRange) { s.SimulationTable[customer - 1].ServiceTime = Servers[id - 1].TimeDistribution[j].Time; s.SimulationTable[customer - 1].StartTime = min; s.SimulationTable[customer - 1].TimeInQueue = min - s.SimulationTable[customer - 1].ArrivalTime; if (s.SimulationTable[customer - 1].TimeInQueue < 0) { s.SimulationTable[customer - 1].TimeInQueue = 0; } s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime + s.SimulationTable[customer - 1].TimeInQueue; if (min > s.SimulationTable[customer - 1].ArrivalTime) { s.SimulationTable[customer - 1].StartTime = min; } s.SimulationTable[customer - 1].EndTime = s.SimulationTable[customer - 1].ServiceTime + s.SimulationTable[customer - 1].StartTime; Servers[id - 1].FinishTime = s.SimulationTable[customer - 1].EndTime; counterToAddInQueue = 0; break; } } } }
//==================================RANDOOOOOM========================================================================== public void Random(int customer, SimulationSystem s) { List <int> FreeServers = new List <int>(); int counter = 0; for (int i = 0; i < Servers.Count(); i++) { FreeServers.Add(new int()); } for (int i = 0; i < Servers.Count(); i++) { if (Servers[i].FinishTime <= s.SimulationTable[customer - 1].ArrivalTime) { Servers[i].IsIdle = true; counter++; FreeServers[i] = s.Servers[i].ID; } } for (int x = 0; x < Servers.Count(); x++) { if (s.Servers[x].IsIdle == true) { if (counter > 1) { int y = 0; y = r.Next(0, counter - 1); x = FreeServers[y]; x--; counter = 0; } s.SimulationTable[customer - 1].AssignedServer.ID = x + 1; s.SimulationTable[customer - 1].AssignedServer.IsIdle = false; s.Servers[x].IsIdle = false; for (int j = 0; j < Servers[x].TimeDistribution.Count(); j++) { if (s.SimulationTable[customer - 1].RandomService <= Servers[x].TimeDistribution[j].MaxRange && s.SimulationTable[customer - 1].RandomService >= Servers[x].TimeDistribution[j].MinRange) { s.SimulationTable[customer - 1].ServiceTime = Servers[x].TimeDistribution[j].Time; s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime; s.SimulationTable[customer - 1].TimeInQueue = s.SimulationTable[customer - 1].StartTime - s.SimulationTable[customer - 1].ArrivalTime; s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime + s.SimulationTable[customer - 1].TimeInQueue; s.SimulationTable[customer - 1].EndTime = s.SimulationTable[customer - 1].ServiceTime + s.SimulationTable[customer - 1].StartTime; Servers[x].FinishTime = s.SimulationTable[customer - 1].EndTime; counterToAddInQueue = 0; break; } } if (counterToAddInQueue == 0) { break; } } counterToAddInQueue++; } if (counterToAddInQueue == s.Servers.Count()) { int counterr = -1; int jj = 0; int min = 9999999; int id = 0; int index = 0; for (int i = 0; i < Servers.Count(); i++) { int z = Servers[i].FinishTime; if (z < min) { min = z; id = i + 1; index = i; } } FreeServers[jj] = id; jj++; for (int i = 0; i < Servers.Count(); i++) { if (min == Servers[i].FinishTime) { counterr++; } if (counterr > 0) { FreeServers[jj] = i + 1; jj++; } } id = FreeServers[r.Next(0, jj - 1)]; s.SimulationTable[customer - 1].AssignedServer.ID = id; for (int j = 0; j < Servers[id - 1].TimeDistribution.Count(); j++) { if (s.SimulationTable[customer - 1].RandomService <= Servers[id - 1].TimeDistribution[j].MaxRange && s.SimulationTable[customer - 1].RandomService >= Servers[id - 1].TimeDistribution[j].MinRange) { s.SimulationTable[customer - 1].ServiceTime = Servers[id - 1].TimeDistribution[j].Time; s.SimulationTable[customer - 1].StartTime = min; s.SimulationTable[customer - 1].TimeInQueue = min - s.SimulationTable[customer - 1].ArrivalTime; if (s.SimulationTable[customer - 1].TimeInQueue < 0) { s.SimulationTable[customer - 1].TimeInQueue = 0; } s.SimulationTable[customer - 1].StartTime = s.SimulationTable[customer - 1].ArrivalTime + s.SimulationTable[customer - 1].TimeInQueue; if (min > s.SimulationTable[customer - 1].ArrivalTime) { s.SimulationTable[customer - 1].StartTime = min; } s.SimulationTable[customer - 1].EndTime = s.SimulationTable[customer - 1].ServiceTime + s.SimulationTable[customer - 1].StartTime; Servers[id - 1].FinishTime = s.SimulationTable[customer - 1].EndTime; counterToAddInQueue = 0; break; } } } }