void addToServer(Customer customer, int selectedServer) { KeyValuePair <int, int> serviceTime = generateServiceTime(selectedServer); int start = Math.Max(customer.ArrivalTime, system.Servers[selectedServer].FinishTime); int end = start + serviceTime.Key; for (int i = start; i <= end; i++) { trakeServers[selectedServer][i] = 1; } totalSimulation = Math.Max(totalSimulation, end); int timeInQueue = Math.Max(0, system.Servers[selectedServer].FinishTime - customer.ArrivalTime); system.Servers[selectedServer].TotalServerCustomer++; system.Servers[selectedServer].TotalWorkingTime += serviceTime.Key; system.Servers[selectedServer].FinishTime = end; SimulationCase simcase = new SimulationCase(); simcase.CustomerNumber = customer.CustomerNumber; simcase.RandomInterArrival = customer.RandomInterArrival; simcase.InterArrival = customer.InterArrivalTime; simcase.ArrivalTime = customer.ArrivalTime; simcase.RandomService = serviceTime.Value; simcase.ServiceTime = serviceTime.Key; simcase.AssignedServer = system.Servers[selectedServer]; simcase.StartTime = start; simcase.EndTime = end; simcase.TimeInQueue = timeInQueue; system.SimulationTable.Add(simcase); }
void AddUser() { if (SimulationTable.Count == 100) { return; } SimulationCase tmp = new SimulationCase(); tmp.CustomerNumber = SimulationTable.Count + 1; //A if (tmp.CustomerNumber == 1) { tmp.RandomInterArrival = 1; //B tmp.InterArrival = 0; //C tmp.ArrivalTime = 0; //D } else { tmp.RandomInterArrival = rnd.Next(100) + 1; //B tmp.InterArrival = getTime(InterarrivalDistribution, tmp.RandomInterArrival); // C tmp.ArrivalTime = SimulationTable[SimulationTable.Count - 1].ArrivalTime + tmp.InterArrival; //D } tmp.RandomService = rnd.Next(100) + 1; //E simulationQueue.Enqueue(tmp.CustomerNumber - 1); SimulationTable.Add(tmp); }
public void Run() { int LastCustomer = 0; for (int CustomerID = 1; CustomerID <= this.StoppingNumber; CustomerID++) { SimulationCase CurrentCase = new SimulationCase(); CurrentCase.CustomerNumber = CustomerID; if (CustomerID != 1) { CurrentCase.RandomInterArrival = Generator.Next(1, 100); CurrentCase.InterArrival = GetInterarrivalTime(CurrentCase.RandomInterArrival); } else { CurrentCase.RandomInterArrival = 1; CurrentCase.InterArrival = 0; } CurrentCase.ArrivalTime = LastCustomer + CurrentCase.InterArrival; LastCustomer = CurrentCase.ArrivalTime; CurrentCase.RandomService = Generator.Next(1, 100); SimulationTable.Add(CurrentCase); } Serve(); }
public void Simulate() { int CurrentCustomer = 1; SimulationCase OldCase = new SimulationCase(); Random random = new Random(); Random ServiceRandom = new Random(); Random ServerRandom = new Random(); OldCase.ArrivalTime = 0; OldCase.InterArrival = 0; OldCase.EndTime = 0; bool cond = true; while (CurrentCustomer <= StoppingNumber) { //Our Main code! if (StoppingCriteria == (MultiQueueModels.Enums.StoppingCriteria.NumberOfCustomers)) { cond = (CurrentCustomer <= StoppingNumber); } else { cond = (OldCase.EndTime <= StoppingNumber); } SimulationCase NewCase = new SimulationCase(); NewCase.CustomerNumber = CurrentCustomer; NewCase.RandomInterArrival = random.Next(1, 101); NewCase.InterArrival = GetWithinRange(InterarrivalDistribution, NewCase.RandomInterArrival); if (CurrentCustomer == 1) { NewCase.ArrivalTime = OldCase.ArrivalTime; } else { NewCase.ArrivalTime = OldCase.ArrivalTime + NewCase.InterArrival; } //Server int ServerIndex = 0; if (CheckIdle(NewCase.ArrivalTime)) { NewCase.TimeInQueue = 0; if (SelectionMethod == Enums.SelectionMethod.HighestPriority) { for (int i = 0; i < Servers.Count; i++) { if (Servers[i].LastFinishTime <= NewCase.ArrivalTime) { NewCase.AssignedServer = Servers[i]; ServerIndex = i; break; } } } else if (SelectionMethod == Enums.SelectionMethod.Random) { int RandomServer = ServerRandom.Next(0, AvailableList.Count); NewCase.AssignedServer = AvailableList[RandomServer]; ServerIndex = AvailableList[RandomServer].ID - 1; } NewCase.ServerIndex = ServerIndex + 1; if (NewCase.AssignedServer.LastFinishTime > NewCase.ArrivalTime) { NewCase.StartTime = NewCase.AssignedServer.LastFinishTime; } else { NewCase.StartTime = NewCase.ArrivalTime; } NewCase.RandomService = ServiceRandom.Next(1, 101); if (NewCase.RandomService == 0) { NewCase.RandomService = 1; } NewCase.ServiceTime = GetWithinRange(NewCase.AssignedServer.TimeDistribution, NewCase.RandomService); NewCase.EndTime = NewCase.StartTime + NewCase.ServiceTime; Servers[ServerIndex].LastFinishTime = NewCase.EndTime; } else { //Time in queue ServerIndex = GetFirstFinishServer(); NewCase.AssignedServer = Servers[ServerIndex]; NewCase.StartTime = NewCase.AssignedServer.LastFinishTime; NewCase.TimeInQueue = NewCase.StartTime - NewCase.ArrivalTime; NewCase.RandomService = ServiceRandom.Next(1, 101); NewCase.ServiceTime = GetWithinRange(NewCase.AssignedServer.TimeDistribution, NewCase.RandomService); NewCase.EndTime = NewCase.StartTime + NewCase.ServiceTime; NewCase.ServerIndex = ServerIndex + 1; Servers[ServerIndex].LastFinishTime = NewCase.EndTime; } SimulationTable.Add(NewCase); OldCase = NewCase; CurrentCustomer++; } //Don't forget PerformanceMeasures PerformanceMeasures.CalcServerPerformance(this); PerformanceMeasures.CalcSysPerformance(this); //System.PerformanceMeasures MessageBox.Show("End of Simulation"); }
public void updateServer(SimulationCase customer) { busyTill = Math.Max(busyTill, customer.ArrivalTime) + customer.ServiceTime; }
public void fillSimulationTable() { total_serv_time = new Decimal[Servers.Count + 1]; customers_in_serv = new Decimal[Servers.Count + 1]; total_run_time = -1; int customerID = 1; while (true) { if (StoppingCriteria == Enums.StoppingCriteria.NumberOfCustomers) { if (customerID > StoppingNumber) { break; } } SimulationCase currentCustomer = new SimulationCase(); currentCustomer.CustomerNumber = customerID; currentCustomer.generateRandomDigits(); if (customerID != 1) { currentCustomer.setInterArrivalTime(InterarrivalDistribution); } if (customerID == 1) { currentCustomer.ArrivalTime = 0; } else { currentCustomer.setArrivalTime(SimulationTable[customerID - 2].ArrivalTime); } if (StoppingCriteria == Enums.StoppingCriteria.SimulationEndTime) { if (currentCustomer.ArrivalTime > StoppingNumber) { break; } } List <Tuple <int, int> > IdleServers = new List <Tuple <int, int> >(); for (int i = 0; i < NumberOfServers; ++i) { IdleServers.Add(new Tuple <int, int>(Servers[i].busyTill, i)); } IdleServers = IdleServers.OrderBy(t => t.Item1).ToList(); List <Tuple <int, int> > IdleEqualServers = new List <Tuple <int, int> >(); IdleEqualServers.Add(IdleServers[0]); for (int i = 1; i < IdleServers.Count; ++i) { if (IdleServers[i].Item1 == IdleServers[0].Item1 || IdleServers[i].Item1 <= currentCustomer.ArrivalTime) { IdleEqualServers.Add(IdleServers[i]); } } if (IdleEqualServers.Count == 1) { int serverIndex = IdleEqualServers[0].Item2; currentCustomer.fillRemainingInfo(Servers[serverIndex]); Servers[serverIndex].updateServer(currentCustomer); total_serv_time[currentCustomer.AssignedServer.ID] += currentCustomer.ServiceTime; customers_in_serv[currentCustomer.AssignedServer.ID]++; total_run_time = Math.Max(total_run_time, currentCustomer.EndTime); } else { List <int> freeServerIndexes = new List <int>(); int highestServerIndex = 1000; if (SelectionMethod == Enums.SelectionMethod.HighestPriority) { for (int i = 0; i < IdleEqualServers.Count; ++i) { freeServerIndexes.Add(IdleEqualServers[i].Item2); if (IdleEqualServers[i].Item2 < highestServerIndex) { highestServerIndex = IdleEqualServers[i].Item2; } } int serverIndex = highestServerIndex; currentCustomer.fillRemainingInfo(Servers[serverIndex]); Servers[serverIndex].updateServer(currentCustomer); total_serv_time[currentCustomer.AssignedServer.ID] += currentCustomer.ServiceTime; customers_in_serv[currentCustomer.AssignedServer.ID]++; total_run_time = Math.Max(total_run_time, currentCustomer.EndTime); } else if (SelectionMethod == Enums.SelectionMethod.Random) { int selectedServer = new Random().Next(0, IdleEqualServers.Count - 1); int serverIndex = IdleEqualServers[selectedServer].Item2; currentCustomer.fillRemainingInfo(Servers[serverIndex]); Servers[serverIndex].updateServer(currentCustomer); total_serv_time[currentCustomer.AssignedServer.ID] += currentCustomer.ServiceTime; customers_in_serv[currentCustomer.AssignedServer.ID]++; total_run_time = Math.Max(total_run_time, currentCustomer.EndTime); } else if (SelectionMethod == Enums.SelectionMethod.LeastUtilization) { ///to get newest run time new_updates_inserv(); int min_index = 0; decimal min_utilization = 100000; for (int i = 0; i < IdleEqualServers.Count; ++i) { int serv_ind = IdleEqualServers[i].Item2; decimal val = decimal.Compare(Servers[serv_ind].Utilization, min_utilization); if (Convert.ToInt32(val) < 0) { min_index = serv_ind; min_utilization = Servers[serv_ind].Utilization; } else if (Convert.ToInt32(val) == 0) { if (Servers[serv_ind].ID < Servers[min_index].ID) { min_index = serv_ind; } min_utilization = Servers[serv_ind].Utilization; } } currentCustomer.fillRemainingInfo(Servers[min_index]); Servers[min_index].updateServer(currentCustomer); total_serv_time[currentCustomer.AssignedServer.ID] += currentCustomer.ServiceTime; customers_in_serv[currentCustomer.AssignedServer.ID]++; total_run_time = Math.Max(total_run_time, currentCustomer.EndTime); } } ++customerID; SimulationTable.Add(currentCustomer); } numberofCustomers = customerID - 1; }
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); }
public void fill_arrival_values(SimulationCase prev_case, List <TimeDistribution> distro_table) { this.CustomerNumber = prev_case.CustomerNumber + 1; get_time(distro_table, 1);//RandomInterArrival&InterArrival filled this.ArrivalTime = prev_case.ArrivalTime + this.InterArrival; }