public PerformanceMeasures CalculatePerformance() { var performanceMeasures = new PerformanceMeasures { TotalCost = NumOfRecords * NumOfNewspapers * PurchasePrice, TotalLostProfit = SimulationTable.Sum(itm => itm.LostProfit), TotalSalesProfit = SimulationTable.Sum(itm => itm.SalesProfit), TotalScrapProfit = SimulationTable.Sum(itm => itm.ScrapProfit), TotalNetProfit = SimulationTable.Sum(itm => itm.DailyNetProfit), DaysWithMoreDemand = SimulationTable.Count(itm => itm.LostProfit != 0), DaysWithUnsoldPapers = SimulationTable.Count(itm => itm.ScrapProfit != 0) }; return(PerformanceMeasures = performanceMeasures); }
public void ServerSelection(int currentCaseIndex) { List <int> available_server_index = new List <int>(); bool wait = false; for (int i = 0; i < NumberOfServers; i++) //fill available_server_id { if (SimulationTable[currentCaseIndex].ArrivalTime >= Servers[i].FinishTime) { available_server_index.Add(i); } } //get the servers that will finish first if all are working if (available_server_index.Count == 0) { wait = true; int min_finish_time = Servers[0].FinishTime; available_server_index.Add(0); //search for minimum finish time for (int i = 1; i < NumberOfServers; i++) //fill available_server_id { if (min_finish_time > Servers[i].FinishTime) //current server will finish earlier { min_finish_time = Servers[i].FinishTime; available_server_index.Clear(); available_server_index.Add(i); } else if (min_finish_time == Servers[i].FinishTime) { available_server_index.Add(i); } } } //based on the selection methode chose a server from the available servers if (SelectionMethod == Enums.SelectionMethod.HighestPriority) { SimulationTable[currentCaseIndex].AssignedServer = Servers[available_server_index[0]]; if (!wait) { Servers[available_server_index[0]].IdleTime += SimulationTable[currentCaseIndex].ArrivalTime - Servers[available_server_index[0]].FinishTime; } else { for (int i = SimulationTable[currentCaseIndex].ArrivalTime; i < SimulationTable[currentCaseIndex].AssignedServer.FinishTime; i++) { while (i >= (waiting_Costumers_count.Count())) { waiting_Costumers_count.Add(0); } waiting_Costumers_count[i]++; } } } else if (SelectionMethod == Enums.SelectionMethod.Random) { int randomnumber = rand.Next(0, available_server_index.Count); // System.Threading.Thread.Sleep(randomnumber); SimulationTable[currentCaseIndex].AssignedServer = Servers[available_server_index[randomnumber]]; if (!wait) { Servers[available_server_index[0]].IdleTime += SimulationTable[currentCaseIndex].ArrivalTime - Servers[available_server_index[0]].FinishTime; } else { for (int i = SimulationTable[currentCaseIndex].ArrivalTime; i < SimulationTable[currentCaseIndex].AssignedServer.FinishTime; i++) { while (i >= (waiting_Costumers_count.Count())) { waiting_Costumers_count.Add(0); } waiting_Costumers_count[i]++; } } } else if (SelectionMethod == Enums.SelectionMethod.LeastUtilization) { workingime = new Dictionary <int, int>(); int max_idel_server_index = 0; int max_val = 1000000; //search for the server of max idel time (LeastUtilization) for (int i = 0; i < available_server_index.Count; i++) //fill available_server_id { /*if (Servers[available_server_index[max_idel_server_index]].IdleTime < Servers[available_server_index[i]].IdleTime) * { * max_idel_server_index = i; * }*/ for (int k = 0; k < SimulationTable.Count() - 1; k++) { if ((SimulationTable[k].AssignedServerID - 1) == available_server_index[i]) { if (workingime.ContainsKey(available_server_index[i])) { workingime[available_server_index[i]] += SimulationTable[k].ServiceTime; } else { workingime.Add(available_server_index[i], SimulationTable[k].ServiceTime); } }//unused servers is not compaired } } for (int e = 0; e < Servers.Count; e++) { if (Servers[e].FinishTime == 0) { workingime.Add(e, 0); } } foreach (KeyValuePair <int, int> entry in workingime) { if (entry.Value < max_val) { max_val = entry.Value; max_idel_server_index = entry.Key; } } SimulationTable[currentCaseIndex].AssignedServer = Servers[max_idel_server_index]; if (!wait) { Servers[available_server_index[0]].IdleTime += SimulationTable[currentCaseIndex].ArrivalTime - Servers[available_server_index[0]].FinishTime; } else { for (int i = SimulationTable[currentCaseIndex].ArrivalTime; i < SimulationTable[currentCaseIndex].AssignedServer.FinishTime; i++) { while (i >= (waiting_Costumers_count.Count())) { waiting_Costumers_count.Add(0); } waiting_Costumers_count[i]++; } } } }
public void Run(string path) { random = new Random(); int cycle = 1, counter_day_in_cycle = 1, days_until_order_arrives = -10, next_order = 0; ReadFromFile(path); CompleteDemandAndLeadDaysDistribution(DemandDistribution); CompleteDemandAndLeadDaysDistribution(LeadDaysDistribution); SimulationCase simulationCase; for (int i = 0; i < NumberOfDays; ++i) { simulationCase = new SimulationCase(); simulationCase.Day = i + 1; simulationCase.Cycle = cycle; simulationCase.DayWithinCycle = counter_day_in_cycle; if (i == 0) { simulationCase.BeginningInventory = StartInventoryQuantity; } else { simulationCase.BeginningInventory = SimulationTable[i - 1].EndingInventory; } if (i == StartLeadDays) { simulationCase.BeginningInventory += StartOrderQuantity; } if (days_until_order_arrives == -1) { simulationCase.BeginningInventory += next_order; } simulationCase.RandomDemand = random.Next(1, 100); simulationCase.Demand = getDistributionValueByRandomNumber(DemandDistribution, simulationCase.RandomDemand); int last_sortage = 0; if (i > 0) { last_sortage = SimulationTable[i - 1].ShortageQuantity; } if (simulationCase.BeginningInventory >= simulationCase.Demand + last_sortage) { simulationCase.EndingInventory = simulationCase.BeginningInventory - (simulationCase.Demand + last_sortage); simulationCase.ShortageQuantity = 0; } else { simulationCase.EndingInventory = 0; simulationCase.ShortageQuantity = (simulationCase.Demand + last_sortage) - simulationCase.BeginningInventory; } if (ReviewPeriod == counter_day_in_cycle) { cycle++; counter_day_in_cycle = 0; simulationCase.OrderQuantity = OrderUpTo - simulationCase.EndingInventory + simulationCase.ShortageQuantity; next_order = simulationCase.OrderQuantity; simulationCase.RandomLeadDays = random.Next(1, 100); simulationCase.LeadDays = getDistributionValueByRandomNumber(LeadDaysDistribution, simulationCase.RandomLeadDays); days_until_order_arrives = simulationCase.LeadDays; } else { simulationCase.OrderQuantity = 0; simulationCase.RandomLeadDays = 0; simulationCase.LeadDays = 0; } counter_day_in_cycle++; days_until_order_arrives--; SimulationTable.Add(simulationCase); } decimal count_end = 0, count_shortage = 0; for (int i = 0; i < SimulationTable.Count(); ++i) { count_end += SimulationTable[i].EndingInventory; count_shortage += SimulationTable[i].ShortageQuantity; } PerformanceMeasures.EndingInventoryAverage = count_end / NumberOfDays; PerformanceMeasures.ShortageQuantityAverage = count_shortage / NumberOfDays; }