Пример #1
0
        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 BuildSimulationTable()
        {
            var rand = new Random();

            for (int i = 0; i < NumOfRecords; i++)
            {
                var simulationCase = new SimulationCase
                {
                    DayNo             = i + 1,
                    RandomNewsDayType = rand.Next(1, 101),
                    RandomDemand      = rand.Next(1, 101)
                };

                simulationCase.NewsDayType = GetDayType(simulationCase.RandomNewsDayType);
                simulationCase.Demand      = GetDemand(simulationCase.RandomDemand, simulationCase.NewsDayType);
                simulationCase.DailyCost   = NumOfNewspapers * PurchasePrice;
                simulationCase.SalesProfit = Math.Min(NumOfNewspapers, simulationCase.Demand) * SellingPrice;
                var diffrence = simulationCase.Demand - NumOfNewspapers;
                simulationCase.LostProfit     = ((diffrence > 0) ? diffrence * UnitProfit : 0);
                simulationCase.ScrapProfit    = ((diffrence < 0) ? Math.Abs(diffrence) * ScrapPrice : 0);
                simulationCase.DailyNetProfit = simulationCase.SalesProfit - simulationCase.DailyCost - simulationCase.LostProfit + simulationCase.ScrapProfit;

                SimulationTable.Add(simulationCase);
            }
        }
        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(string fnam)
        {
            readfile(fnam);
            Random day    = new Random();
            Random demand = new Random();

            for (int i = 1; i <= NumOfRecords; i++)
            {
                SimulationCase sc = new SimulationCase();
                sc.DayNo             = i;
                sc.RandomNewsDayType = day.Next(1, 100);
                for (int h = 0; h < DayTypeDistributions.Count; h++)
                {
                    if (DayTypeDistributions[h].MinRange <= sc.RandomNewsDayType && DayTypeDistributions[h].MaxRange >= sc.RandomNewsDayType)
                    {
                        sc.NewsDayType = DayTypeDistributions[h].DayType;
                        break;
                    }
                }
                sc.RandomDemand = demand.Next(1, 100);
                sc.Demand       = get_demand(sc.RandomDemand, sc.NewsDayType);
                sc.DailyCost    = NumOfNewspapers * PurchasePrice;
                sc.SalesProfit  = Math.Min(sc.Demand, NumOfNewspapers) * SellingPrice;

                if (sc.Demand < NumOfNewspapers)
                {
                    sc.ScrapProfit    = (NumOfNewspapers - sc.Demand) * ScrapPrice;
                    sc.DailyNetProfit = sc.SalesProfit + sc.ScrapProfit - sc.DailyCost;
                }
                else if (sc.Demand > NumOfNewspapers)
                {
                    sc.LostProfit     = (sc.Demand - NumOfNewspapers) * UnitProfit;
                    sc.DailyNetProfit = sc.SalesProfit - sc.LostProfit - sc.DailyCost;
                }
                else
                {
                    sc.DailyNetProfit = sc.SalesProfit - sc.DailyCost;
                }

                if (sc.ScrapProfit != 0)
                {
                    PerformanceMeasures.DaysWithUnsoldPapers += 1;
                }
                if (sc.LostProfit != 0)
                {
                    PerformanceMeasures.DaysWithMoreDemand += 1;
                }
                PerformanceMeasures.TotalLostProfit  += sc.LostProfit;
                PerformanceMeasures.TotalScrapProfit += sc.ScrapProfit;
                PerformanceMeasures.TotalCost        += sc.DailyCost;
                PerformanceMeasures.TotalNetProfit   += sc.DailyNetProfit;
                PerformanceMeasures.TotalSalesProfit += sc.SalesProfit;

                SimulationTable.Add(sc);
            }
        }
        public void sim()
        {
            int    ncycle      = NumberOfDays / ReviewPeriod;
            int    da          = 1;
            Random rnd         = new Random();
            Random rndd        = new Random();
            int    startshortq = 0;

            for (int i = 1; i <= ncycle; i++)
            {
                for (int j = 1; j <= ReviewPeriod; j++)
                {
                    if (j == (StartLeadDays + 1))
                    {
                        StartInventoryQuantity += StartOrderQuantity;
                    }
                    SimulationCase c = new SimulationCase();
                    c.Day = da;
                    da++;
                    c.Cycle              = i;
                    c.DayWithinCycle     = j;
                    c.BeginningInventory = StartInventoryQuantity;
                    c.RandomDemand       = rnd.Next(1, 101);
                    c.Demand             = get_dist(c.RandomDemand, DemandDistribution);
                    if (c.Demand + startshortq <= c.BeginningInventory)
                    {
                        c.EndingInventory      = c.BeginningInventory - c.Demand - startshortq;
                        startshortq            = 0;
                        StartInventoryQuantity = c.EndingInventory;
                    }
                    else if (c.Demand + startshortq > c.BeginningInventory)
                    {
                        c.EndingInventory      = 0;
                        StartInventoryQuantity = 0;
                        c.ShortageQuantity     = c.Demand - c.BeginningInventory + startshortq;
                        startshortq            = c.ShortageQuantity;
                    }
                    if (j == ReviewPeriod)
                    {
                        c.OrderQuantity    = OrderUpTo - c.EndingInventory + c.ShortageQuantity;
                        StartOrderQuantity = c.OrderQuantity;
                        c.RandomLeadDays   = rndd.Next(1, 101);
                        c.LeadDays         = get_dist(c.RandomLeadDays, LeadDaysDistribution);
                        StartLeadDays      = c.LeadDays;
                    }
                    PerformanceMeasures.EndingInventoryAverage  += c.EndingInventory;
                    PerformanceMeasures.ShortageQuantityAverage += c.ShortageQuantity;
                    SimulationTable.Add(c);
                }
            }
            PerformanceMeasures.EndingInventoryAverage  = (decimal)(PerformanceMeasures.EndingInventoryAverage / NumberOfDays);
            PerformanceMeasures.ShortageQuantityAverage = (decimal)(PerformanceMeasures.ShortageQuantityAverage / NumberOfDays);
        }
 public void fillSimulationTable()
 {
     for (int i = 1; i <= NumOfRecords; i++)
     {
         SimulationCase tempCase = new SimulationCase();
         tempCase.DayNo = i;
         tempCase.setNewsDayType(DayTypeDistributions);
         tempCase.setDemand(DemandDistributions);
         tempCase.fill(NumOfNewspapers, PurchasePrice, SellingPrice, ScrapPrice, UnitProfit);
         SimulationTable.Add(tempCase);
     }
 }
        public void Simulate()
        {
            PerformanceMeasures.DaysWithMoreDemand   = 0;
            PerformanceMeasures.DaysWithUnsoldPapers = 0;
            PerformanceMeasures.TotalCost            = 0;
            PerformanceMeasures.TotalLostProfit      = 0;
            PerformanceMeasures.TotalNetProfit       = 0;
            PerformanceMeasures.TotalSalesProfit     = 0;
            PerformanceMeasures.TotalScrapProfit     = 0;

            for (int i = 1; i <= NumOfRecords; i++)
            {
                SimulationCase sc = new SimulationCase();
                sc.DayNo = i;

                sc.RandomNewsDayType = rnd.Next(1, 101);
                foreach (DayTypeDistribution DTD in DayTypeDistributions)
                {
                    if (sc.RandomNewsDayType >= DTD.MinRange && sc.RandomNewsDayType <= DTD.MaxRange)
                    {
                        sc.NewsDayType = DTD.DayType;
                    }
                }

                sc.RandomDemand = rnd.Next(1, 101);
                foreach (DemandDistribution DD in DemandDistributions)
                {
                    if (sc.RandomDemand >= DD.DayTypeDistributions[(int)sc.NewsDayType].MinRange && sc.RandomDemand <= DD.DayTypeDistributions[(int)sc.NewsDayType].MaxRange)
                    {
                        sc.Demand = DD.Demand;
                    }
                }

                sc.SalesProfit = Math.Min(NumOfNewspapers, sc.Demand) * SellingPrice;
                PerformanceMeasures.TotalSalesProfit += sc.SalesProfit;

                sc.ScrapProfit = Math.Max(0, NumOfNewspapers - sc.Demand) * ScrapPrice;
                PerformanceMeasures.TotalScrapProfit     += sc.ScrapProfit;
                PerformanceMeasures.DaysWithUnsoldPapers += (sc.ScrapProfit > 0)? 1: 0;

                sc.LostProfit = Math.Max(0, sc.Demand - NumOfNewspapers) * UnitProfit;
                PerformanceMeasures.TotalLostProfit    += sc.LostProfit;
                PerformanceMeasures.DaysWithMoreDemand += (sc.LostProfit > 0) ? 1 : 0;

                sc.DailyCost = NumOfNewspapers * PurchasePrice;
                PerformanceMeasures.TotalCost += sc.DailyCost;

                sc.DailyNetProfit = sc.SalesProfit - sc.DailyCost - sc.LostProfit + sc.ScrapProfit;
                PerformanceMeasures.TotalNetProfit += sc.DailyNetProfit;

                SimulationTable.Add(sc);
            }
        }
        ////////////////////////////////

        void runSimulation()
        {
            Random random = new Random();

            for (int i = 0; i < NumOfRecords; i++)
            {
                SimulationCase simCase = new SimulationCase();
                simCase.DayNo             = i + 1;
                simCase.RandomNewsDayType = random.Next(1, 100);
                simCase.NewsDayType       = getDayType(simCase.RandomNewsDayType);
                simCase.RandomDemand      = random.Next(1, 100);
                simCase.Demand            = getDemand(simCase.NewsDayType, simCase.RandomDemand);
                simCase.DailyCost         = CalcDaiyCost();
                simCase.SalesProfit       = calcDailyProfit(simCase.Demand);
                simCase.LostProfit        = calcLostProfit(simCase.Demand);
                simCase.ScrapProfit       = calcSalvageFromSaleOfScrap(simCase.Demand);
                simCase.DailyNetProfit    = calcDailyNetProfit(simCase.SalesProfit, simCase.DailyCost, simCase.LostProfit, simCase.ScrapProfit);
                SimulationTable.Add(simCase);
            }
        }
        public void Start_Process()
        {
            int            c    = 0;
            Random         rand = new Random();
            SimulationCase row;

            while (c < NumOfRecords)
            {
                row = new SimulationCase();

                row.DayNo             = c + 1;
                row.RandomNewsDayType = rand.Next(1, 101);
                row.NewsDayType       = Get_Day_Type(DayTypeDistributions, row.RandomNewsDayType);
                row.RandomDemand      = rand.Next(1, 101);
                row.Demand            = Get_Demand(DemandDistributions, row.RandomDemand, row.NewsDayType.ToString());
                row.DailyCost         = Cost;
                row.SalesProfit       = row.Demand * SellingPrice;
                if (row.Demand > NumOfNewspapers)
                {
                    row.SalesProfit = NumOfNewspapers * SellingPrice;
                    row.LostProfit  = (row.Demand - NumOfNewspapers) * (SellingPrice - PurchasePrice);
                    PerformanceMeasures.DaysWithMoreDemand++;
                }
                if (row.Demand < NumOfNewspapers)
                {
                    row.ScrapProfit = (NumOfNewspapers - row.Demand) * ScrapPrice;
                    PerformanceMeasures.DaysWithUnsoldPapers++;
                }
                row.DailyNetProfit = row.SalesProfit - row.DailyCost - row.LostProfit + row.ScrapProfit;

                // rest of performance calculations
                PerformanceMeasures.TotalCost        += row.DailyCost;
                PerformanceMeasures.TotalSalesProfit += row.SalesProfit;
                PerformanceMeasures.TotalScrapProfit += row.ScrapProfit;
                PerformanceMeasures.TotalLostProfit  += row.LostProfit;
                PerformanceMeasures.TotalNetProfit   += row.DailyNetProfit;

                SimulationTable.Add(row);
                c++;
            }
        }
Пример #10
0
        public void BeginSimulation()
        {
            Random R = new Random();

            for (int i = 0; i < NumOfRecords; i++)
            {
                SimulationCase SC = new SimulationCase();
                SC.DayNo             = i + 1;
                SC.RandomNewsDayType = R.Next(1, 100);
                SC.NewsDayType       = GetDayType(SC.RandomNewsDayType);
                SC.RandomDemand      = R.Next(1, 100);
                SC.Demand            = GetDemand(SC.RandomDemand, SC.NewsDayType);


                SC.DailyCost   = NumOfNewspapers * PurchasePrice;
                SC.SalesProfit = NumOfNewspapers * SellingPrice;
                decimal ProfitPerPiece = SellingPrice - PurchasePrice;

                if (SC.Demand > NumOfNewspapers)
                {
                    SC.LostProfit  = (SC.Demand - NumOfNewspapers) * ProfitPerPiece;
                    SC.ScrapProfit = 0;
                }
                else if (SC.Demand < NumOfNewspapers)
                {
                    SC.ScrapProfit     = (NumOfNewspapers - SC.Demand) * ScrapPrice;
                    SC.SalesProfit    -= (NumOfNewspapers - SC.Demand) * SellingPrice;
                    SC.DailyNetProfit += SC.ScrapProfit;
                    SC.LostProfit      = 0;
                }
                else
                {
                    SC.ScrapProfit = 0;
                    SC.LostProfit  = 0;
                }
                SC.DailyNetProfit = SC.SalesProfit - SC.DailyCost - SC.LostProfit + SC.ScrapProfit;
                SimulationTable.Add(SC);
            }
            Resas();
        }
Пример #11
0
        public void set_simulation_case()
        {
            Random rd         = new Random();
            bool   setdaytype = false;

            for (int i = 0; i < NumOfRecords; i++)
            {
                SimulationTable.Add(new SimulationCase());
                SimulationTable[i].DayNo             = i + 1;
                SimulationTable[i].RandomNewsDayType = rd.Next(1, 100);
                foreach (DayTypeDistribution item in DayTypeDistributions)
                {
                    if (SimulationTable[i].RandomNewsDayType >= item.MinRange && SimulationTable[i].RandomNewsDayType <= item.MaxRange)
                    {
                        SimulationTable[i].NewsDayType = item.DayType;
                        setdaytype = true;
                        break;
                    }
                }

                if (setdaytype == true)
                {
                    SimulationTable[i].RandomDemand = rd.Next(1, 100);

                    foreach (DemandDistribution item2 in DemandDistributions)
                    {
                        foreach (DayTypeDistribution item3 in item2.DayTypeDistributions)
                        {
                            if (SimulationTable[i].NewsDayType == item3.DayType)
                            {
                                if (SimulationTable[i].RandomDemand >= item3.MinRange && SimulationTable[i].RandomDemand <= item3.MaxRange)
                                {
                                    SimulationTable[i].Demand = item2.Demand;
                                }
                            }
                        }
                    }
                }
            }
        }
        private void Prepare_Customers()
        {
            int lastArrival = 0;

            for (int i = 1; i <= maxCustomers; i++)
            {
                MultiQueueModels.SimulationCase simCase = new MultiQueueModels.SimulationCase();
                simCase.CustomerNumber = i;

                //Arrival
                if (i == 1)
                {
                    simCase.RandomInterArrival = 1;
                    simCase.InterArrival       = 1;
                    simCase.ArrivalTime        = 0;
                }
                else
                {
                    simCase.RandomInterArrival = rnd.Next(1, 101);
                    for (int j = 0; j < InterarrivalDistribution.Count; j++)
                    {
                        if (simCase.RandomInterArrival >= InterarrivalDistribution[j].MinRange &&
                            simCase.RandomInterArrival <= InterarrivalDistribution[j].MaxRange)
                        {
                            simCase.InterArrival = InterarrivalDistribution[j].Time;
                            simCase.ArrivalTime  = lastArrival + simCase.InterArrival;
                            break;
                        }
                    }
                    lastArrival = simCase.ArrivalTime;
                }

                //Service
                simCase.RandomService = rnd.Next(1, 101);

                simCase.TimeInQueue = 0;
                SimulationTable.Add(simCase);
            }
        }
        public Tuple <decimal, decimal> FillSimulationTable()
        {
            int     orderArrival = StartLeadDays + 1, orderQuantity = StartOrderQuantity;
            int     lastInventory = StartInventoryQuantity, lastShortage = 0;
            decimal totalEnding = 0, totalShortage = 0;

            for (int i = 0; i < NumberOfDays; i++)
            {
                SimulationCase Case = new SimulationCase();

                Case.Day = i + 1;

                decimal cycle = decimal.Divide(Case.Day, ReviewPeriod);
                Case.Cycle = Convert.ToInt32(Math.Ceiling(cycle));

                Case.DayWithinCycle = Case.Day % 5;
                if (Case.DayWithinCycle == 0)
                {
                    Case.DayWithinCycle = 5;
                }

                Case.BeginningInventory = lastInventory;
                if (Case.Day == orderArrival)
                {
                    Case.BeginningInventory += orderQuantity;
                }

                Tuple <int, int> demand = HelperFunctions.GetNumber("Demand", DemandDistribution);
                Case.RandomDemand = demand.Item1;
                Case.Demand       = demand.Item2;

                Case.EndingInventory = Case.BeginningInventory - Case.Demand;

                if (Case.Day == orderArrival)
                {
                    Case.EndingInventory -= lastShortage;
                    lastShortage          = 0;
                }

                Case.ShortageQuantity = lastShortage;
                if (Case.EndingInventory < 0)
                {
                    Case.ShortageQuantity -= Case.EndingInventory;
                    Case.EndingInventory   = 0;
                }

                if (Case.DayWithinCycle == 5)   // Review
                {
                    Tuple <int, int> leadDays = HelperFunctions.GetNumber("LeadDays", LeadDaysDistribution);
                    Case.RandomLeadDays = leadDays.Item1;
                    Case.LeadDays       = leadDays.Item2;
                    Case.OrderQuantity  = OrderUpTo - Case.EndingInventory + Case.ShortageQuantity;
                    if (Case.OrderQuantity > 0)   // Place order
                    {
                        orderArrival  = Case.Day + Case.LeadDays + 1;
                        orderQuantity = Case.OrderQuantity;
                    }
                }

                if (orderArrival >= Case.Day + 1)
                {
                    Case.DaysUntilOrderArrives = orderArrival - (Case.Day + 1);
                }

                SimulationTable.Add(Case);

                // Update data
                lastInventory  = Case.EndingInventory;
                lastShortage   = Case.ShortageQuantity;
                totalEnding   += Case.EndingInventory;
                totalShortage += Case.ShortageQuantity;
            }

            return(new Tuple <decimal, decimal>(totalEnding, totalShortage));
        }
Пример #14
0
        public void StartSimulation(string filepath)
        {
            int maxEndtime = 0;

            ReadInput(filepath);
            generate_cumulative_range(InterarrivalDistribution);
            for (int i = 0; i < NumberOfServers; i++)
            {
                generate_cumulative_range(Servers[i].TimeDistribution);
            }
            //first customer
            SimulationTable.Add(new SimulationCase());
            SimulationTable[0].CustomerNumber     = 1;
            SimulationTable[0].ArrivalTime        = 0;
            SimulationTable[0].RandomInterArrival = 1;
            ServerSelection(0);
            SimulationTable[0].fill_service_values();
            if (maxEndtime < SimulationTable[0].EndTime)
            {
                maxEndtime = SimulationTable[0].EndTime;
            }
            if (StoppingCriteria == Enums.StoppingCriteria.NumberOfCustomers)
            {
                for (int i = 1; i < StoppingNumber; i++)
                {
                    SimulationTable.Add(new SimulationCase());
                    SimulationTable[i].fill_arrival_values(SimulationTable[i - 1], InterarrivalDistribution);
                    ServerSelection(i);
                    SimulationTable[i].fill_service_values();
                    if (maxEndtime < SimulationTable[i].EndTime)
                    {
                        maxEndtime = SimulationTable[i].EndTime;
                    }
                }
                finishtime = maxEndtime;
            }
            else
            {
                int i = 0;
                while (SimulationTable[i].StartTime <= StoppingNumber)//change to endtime <= if this is required
                {
                    i++;
                    SimulationTable.Add(new SimulationCase());
                    SimulationTable[i].fill_arrival_values(SimulationTable[i - 1], InterarrivalDistribution);
                    ServerSelection(i);
                    SimulationTable[i].fill_service_values();
                }
                if (SimulationTable[i].StartTime <= StoppingNumber)
                {
                    if (SimulationTable[i].TimeInQueue > 0)
                    {
                        waiting_Costumers_count.RemoveRange(SimulationTable[i].ArrivalTime, waiting_Costumers_count.Count - SimulationTable[i].ArrivalTime);
                    }
                    SimulationTable[i].AssignedServer.FinishTime -= SimulationTable[i].ServiceTime;
                    SimulationTable.RemoveAt(i);
                }
                finishtime = SimulationTable[SimulationTable.Count - 1].EndTime;
            }

            //servers calculation
            for (int i = 0; i < NumberOfServers; i++)
            {
                int numofCustomersperserver = 0;
                int working_time            = 0;
                for (int j = 0; j < SimulationTable.Count; j++)
                {
                    if (SimulationTable[j].AssignedServer.ID == Servers[i].ID)
                    {
                        numofCustomersperserver++;
                    }
                    if ((SimulationTable[j].AssignedServerID - 1) == Servers[i].ID)
                    {
                        working_time += SimulationTable[j].ServiceTime;
                    }
                }
                Servers[i].IdleTime  = finishtime - working_time;
                Servers[i].IdleTime += finishtime - Servers[i].FinishTime;
                Servers[i].calculate(finishtime, numofCustomersperserver);
            }

            calculate_total_measures();
        }
        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;
        }
Пример #16
0
        public void runSystem()
        {
            int  currentQuantity   = StartInventoryQuantity;
            int  currentShortage   = 0;
            int  nextOrderLeadDays = StartLeadDays - 1;
            int  nextOrderQuantity = StartOrderQuantity;
            bool flag = true;

            for (int i = 0; i < NumberOfDays; ++i)
            {
                SimulationCase nCase = new SimulationCase();
                nCase.Day = i + 1;

                if (nextOrderLeadDays == 0 && flag)
                {
                    currentQuantity         += nextOrderQuantity;
                    nCase.BeginningInventory = currentQuantity;
                    currentQuantity         -= currentShortage;
                    currentShortage          = 0;
                    flag = false;
                }
                else
                {
                    nCase.BeginningInventory = currentQuantity;
                }

                nCase.Cycle = nCase.Day / ReviewPeriod;
                if (nCase.Day % ReviewPeriod != 0)
                {
                    ++nCase.Cycle;
                }

                nCase.DayWithinCycle = nCase.Day % ReviewPeriod;
                if (nCase.DayWithinCycle == 0)
                {
                    nCase.DayWithinCycle = ReviewPeriod;
                }


                nCase.RandomDemand = randomNumber();
                for (int j = 0; j < DemandDistribution.Count; ++j)
                {
                    if (nCase.RandomDemand >= DemandDistribution[j].MinRange && nCase.RandomDemand <= DemandDistribution[j].MaxRange)
                    {
                        nCase.Demand = DemandDistribution[j].Value;
                    }
                }

                if (nCase.Demand <= currentQuantity)
                {
                    currentQuantity -= nCase.Demand;
                }
                else
                {
                    currentShortage += nCase.Demand - currentQuantity;
                    currentQuantity  = 0;
                }

                nCase.EndingInventory  = currentQuantity;
                nCase.ShortageQuantity = currentShortage;

                if (nCase.Day % ReviewPeriod == 0)
                {
                    nCase.OrderQuantity  = (OrderUpTo - currentQuantity) + currentShortage;
                    nextOrderQuantity    = nCase.OrderQuantity;
                    nCase.RandomLeadDays = randomNumber();
                    for (int j = 0; j < LeadDaysDistribution.Count; ++j)
                    {
                        if (nCase.RandomLeadDays >= LeadDaysDistribution[j].MinRange && nCase.RandomLeadDays <= LeadDaysDistribution[j].MaxRange)
                        {
                            nCase.LeadDays = LeadDaysDistribution[j].Value;
                        }
                    }
                    nextOrderLeadDays = nCase.LeadDays + 1;
                    flag = true;
                }
                else
                {
                    nCase.OrderQuantity = nCase.RandomLeadDays = nCase.LeadDays = 0;
                }


                if (nextOrderLeadDays > 0 && i != 0)
                {
                    --nextOrderLeadDays;
                }
                nCase.DaysUntilOrderArrives = nextOrderLeadDays;

                SimulationTable.Add(nCase);
            }
            PerformanceMeasures.calculatePerformanceMeasures(SimulationTable);
        }
Пример #17
0
        ///////////// SimulationTable ////////////////
        public void fillTable()
        {
            Random rand = new Random();

            for (int i = 1; i <= NumOfRecords; i++)
            {
                SimulationCase sc = new SimulationCase();
                sc.DayNo = i;

                sc.RandomNewsDayType = rand.Next(1, 101);
                sc.RandomDemand      = rand.Next(1, 101);

                if (sc.RandomNewsDayType >= DayTypeDistributions.ElementAt(0).MinRange&& sc.RandomNewsDayType <= DayTypeDistributions.ElementAt(0).MaxRange)
                {
                    sc.NewsDayType = Enums.DayType.Good;
                }
                else if (sc.RandomNewsDayType >= DayTypeDistributions.ElementAt(1).MinRange&& sc.RandomNewsDayType <= DayTypeDistributions.ElementAt(1).MaxRange)
                {
                    sc.NewsDayType = Enums.DayType.Fair;
                }
                else
                {
                    sc.NewsDayType = Enums.DayType.Poor;
                }


                if (sc.NewsDayType == Enums.DayType.Good)
                {
                    for (int j = 0; j < DemandDistributions.Count; j++)
                    {
                        if (sc.RandomDemand >= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(0).MinRange&& sc.RandomDemand <= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(0).MaxRange)
                        {
                            sc.Demand = DemandDistributions.ElementAt(j).Demand;
                        }
                    }
                }



                else if (sc.NewsDayType == Enums.DayType.Fair)
                {
                    for (int j = 0; j < DemandDistributions.Count; j++)
                    {
                        if (sc.RandomDemand >= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(1).MinRange&& sc.RandomDemand <= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(1).MaxRange)
                        {
                            sc.Demand = DemandDistributions.ElementAt(j).Demand;
                        }
                    }
                }
                else
                {
                    for (int j = 0; j < DemandDistributions.Count; j++)
                    {
                        if (sc.RandomDemand >= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(2).MinRange&& sc.RandomDemand <= DemandDistributions.ElementAt(j).DayTypeDistributions.ElementAt(2).MaxRange)
                        {
                            sc.Demand = DemandDistributions.ElementAt(j).Demand;
                        }
                    }
                }



                sc.DailyCost = NumOfNewspapers * PurchasePrice;
                if (sc.Demand >= NumOfNewspapers)
                {
                    sc.SalesProfit = NumOfNewspapers * SellingPrice;
                    sc.LostProfit  = (sc.Demand - NumOfNewspapers) * (SellingPrice - PurchasePrice);
                    sc.ScrapProfit = 0;
                }

                else if (NumOfNewspapers > sc.Demand)
                {
                    sc.SalesProfit = sc.Demand * SellingPrice;
                    sc.ScrapProfit = (NumOfNewspapers - sc.Demand) * ScrapPrice;
                    sc.LostProfit  = 0;
                }
                sc.DailyNetProfit = sc.SalesProfit - sc.DailyCost - sc.LostProfit + sc.ScrapProfit;
                SimulationTable.Add(sc);
            }
        }
        //===================================================================================================================
        //===============================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;
            }
        }
Пример #19
0
        public void simulate()
        {
            int            O_Quantity    = 0;
            int            temp_shortage = 0;
            SimulationCase temp          = new SimulationCase();

            temp.Day                = 1;
            temp.Cycle              = 1;
            temp.DayWithinCycle     = 1;
            temp.BeginningInventory = StartInventoryQuantity;
            temp.demand(DemandDistribution);
            temp.EndingInventory = temp.BeginningInventory - temp.Demand;
            temp.Daytillarr      = StartLeadDays - 1;
            if (temp.EndingInventory < 0)
            {
                temp.ShortageQuantity = -1 * temp.EndingInventory;
                temp.EndingInventory  = 0;
            }
            SimulationTable.Add(temp);
            O_Quantity = StartOrderQuantity;

            temp = new SimulationCase();
            SimulationTable.Add(temp);
            for (int i = 1; i < NumberOfDays; i++)
            {
                temp_shortage          = SimulationTable[i - 1].ShortageQuantity;
                SimulationTable[i].Day = SimulationTable[i - 1].Day + 1;
                SimulationTable[i].BeginningInventory = SimulationTable[i - 1].EndingInventory;

                if (SimulationTable[i - 1].DayWithinCycle == 5)
                {
                    SimulationTable[i].Cycle          = SimulationTable[i - 1].Cycle + 1;
                    SimulationTable[i].DayWithinCycle = 1;
                }
                else
                {
                    SimulationTable[i].Cycle          = SimulationTable[i - 1].Cycle;
                    SimulationTable[i].DayWithinCycle = SimulationTable[i - 1].DayWithinCycle + 1;
                }

                SimulationTable[i].demand(DemandDistribution);
                SimulationTable[i].EndingInventory = SimulationTable[i].BeginningInventory - SimulationTable[i].Demand;

                if (SimulationTable[i - 1].Daytillarr > 0)
                {
                    SimulationTable[i].Daytillarr = SimulationTable[i - 1].Daytillarr - 1;
                }
                else
                {
                    if (SimulationTable[i - 1].Daytillarr == 0 && SimulationTable[i - 2].Daytillarr == 1)
                    {
                        SimulationTable[i].BeginningInventory += O_Quantity;
                        SimulationTable[i].demand(DemandDistribution);
                        SimulationTable[i].EndingInventory = SimulationTable[i].BeginningInventory - SimulationTable[i].Demand - SimulationTable[i - 1].ShortageQuantity;
                        temp_shortage = 0;
                    }
                }

                if (SimulationTable[i].EndingInventory < 0)
                {
                    SimulationTable[i].ShortageQuantity = -1 * SimulationTable[i].EndingInventory + temp_shortage;
                    SimulationTable[i].EndingInventory  = 0;
                }
                else if (SimulationTable[i].EndingInventory == 0)
                {
                    SimulationTable[i].ShortageQuantity = SimulationTable[i - 1].ShortageQuantity;
                }

                if (SimulationTable[i].DayWithinCycle == ReviewPeriod)
                {
                    SimulationTable[i].order(LeadDaysDistribution, OrderUpTo);
                    O_Quantity = SimulationTable[i].OrderQuantity;
                }

                if (i != NumberOfDays - 1)
                {
                    temp = new SimulationCase();
                    SimulationTable.Add(temp);
                }
            }
        }
        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");
        }
Пример #21
0
        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;
        }