예제 #1
0
        private static Results[] DoSimulation(Options options, Data data)
        {
            int limit = DetermineLimit(data);
            ThreadLocal <Random> rng = new ThreadLocal <Random>(() => new Random());

            Results[] allResults = new Results[limit];
            //for (int rho = 1; rho < limit; rho++)
            //{
            Parallel.For(1, limit, new ParallelOptions {
                MaxDegreeOfParallelism = data.Threads
            }, rho =>                                                                                    // MaxDegreeOfParallelism n + 1, where n is the number of cores
            {
                // Initialize servers
                Distribution[] serviceDistributions = CreateServiceDistributions(data, rng.Value);

                // Exponential arrival rate
                double lambda = data.ServerParameters.Sum(x => 1 / x.Average) * ((rho - 1) * data.Stepsize + 0.05);
                //double lambda = data.ServerParameters.Sum(x => 1 / x.Average) * rho * data.Stepsize;

                // Initialize customers
                Distribution[] arrivalDistributions = CreateArrivalDistributions(data, rng.Value, lambda);

                // Main simulation loop
                //Results results = new Results(data, rho * data.Stepsize, arrivalDistributions, serviceDistributions);
                Results results = new Results(data, ((rho - 1) * data.Stepsize + 0.05), arrivalDistributions, serviceDistributions);
                for (int r = 0; r < data.Runs; r++)
                {
                    double t = 0;

                    FutureEvents futureEvents = new FutureEvents();
                    for (int i = 0; i < data.nrCustomers; i++)
                    {
                        Customer customer = new Customer(i);
                        futureEvents.Add(new Event(Event.ARRIVAL, arrivalDistributions[i].Next(), customer));
                    }

                    CustomerQueue customerQueue = new CustomerQueue();

                    ServerQueue idleServerQueue = new ServerQueue();
                    for (int i = 0; i < data.nrServers; i++)
                    {
                        Server server = new Server(i, data.Eligibility[i]);
                        idleServerQueue.Add(server);
                    }

                    while (results.DepartureCounter(r) < options.DeparturesToSimulate)
                    {
                        Event e = futureEvents.Next();
                        t       = e.time;
                        results.Register(r, e, customerQueue, idleServerQueue);

                        if (e.type == Event.ARRIVAL)
                        {
                            customerQueue.CustomerCheckIn(e.customer, t);
                            Server server = idleServerQueue.FindServer(e.customer.cID);
                            if (server != null) // Eligible server available
                            {
                                idleServerQueue.Remove(server);
                                e.customer.server        = server;
                                e.customer.serviceTime   = t;
                                e.customer.departureTime = t + serviceDistributions[server.sID].Next();
                                futureEvents.Add(new Event(Event.DEPARTURE, e.customer.departureTime, e.customer));
                            }
                            futureEvents.Add(new Event(Event.ARRIVAL, t + arrivalDistributions[e.customer.cID].Next(), new Customer(e.customer.cID)));
                        }
                        else if (e.type == Event.DEPARTURE)
                        {
                            Customer customer = customerQueue.FindCustomer(e.customer.server);
                            if (customer != null)
                            {
                                customer.serviceTime   = t;
                                customer.departureTime = t + serviceDistributions[customer.server.sID].Next();
                                futureEvents.Add(new Event(Event.DEPARTURE, customer.departureTime, customer));
                            }
                            else
                            {
                                idleServerQueue.Add(e.customer.server);
                            }
                            customerQueue.CustomerCheckOut(e.customer);
                        }
                        else
                        {
                            Console.WriteLine("Invalid event type.");
                        }
                    }
                }
                allResults[rho] = results;
            });
            //}
            return(allResults);
        }
예제 #2
0
        static void Main(string[] args)
        {
            Console.WriteLine("QUEUING SIMULATION", new String('\u2500', 80));

            Console.WriteLine("Simulation started...");
            Stopwatch stopwatch = new Stopwatch();

            stopwatch.Start();

            // Create a directory and file to print the results
            string path = string.Concat(Environment.CurrentDirectory, "/Results/");

            System.IO.Directory.CreateDirectory(path);
            string filename = string.Concat(path, "Results_U.csv");

            using (System.IO.StreamWriter file = new System.IO.StreamWriter(filename, false))
            {
                file.WriteLine("utilization,meanW,meanS,meanLq,meanLs,meanPW,theoreticPWMMC,meanWEstimation");
            }

            // Runs (Number and time span resp.)
            int    R = 6;
            double T = 1E6;

            // Pseudo-random number generator
            //Random rng = new Random();
            ThreadLocal <Random> rng = new ThreadLocal <Random>(() => new Random());

            int counter = new int();

            Parallel.For(1, 20, new ParallelOptions {
                MaxDegreeOfParallelism = 3
            }, rho =>                                                                      // MaxDegreeOfParallelism n + 1, where n is the number of cores
            {
                // Diagnosis
                //bool repeat = rng.IsValueCreated;
                //Console.WriteLine("ThreadName = {0} {1}", rng.Value, repeat ? "(repeat)" : "");

                // Initialize servers
                //double[] serviceDistributionParameters = new double[] { 1, 1, 1, 1 };
                double[,] serviceDistributionParameters = new double[, ] {
                    { 0, 2 }, { 0, 2 }, { 0, 2 }, { 0, 2 }
                };
                //double[] serviceDistributionParameters = new double[] { 1, 1, 1, 1 };
                //double[,] serviceDistributionParameters = new double[,] { { 1, 2 }, { 1, 2 }, { 1, 2 }, { 1, 2 } };
                int nrServers = serviceDistributionParameters.GetLength(0);
                Distribution[] serviceDistributions = new Distribution[nrServers];
                for (int i = 0; i < nrServers; i++)
                {
                    //serviceDistributions[i] = new DeterministicDistribution(rng.Value, serviceDistributionParameters[i]);
                    serviceDistributions[i] = new UniformDistribution(rng.Value, serviceDistributionParameters[i, 0], serviceDistributionParameters[i, 1]);
                    //serviceDistributions[i] = new ExponentialDistribution(rng.Value, serviceDistributionParameters[i]);
                    //serviceDistributions[i] = new Hyper2ExponentialDistribution(rng.Value, serviceDistributionParameters[i, 0], serviceDistributionParameters[i, 1]);
                }

                // Exponential arrival rate
                double lambda = nrServers * (rho / 20.0);

                // Initialize customers
                double[] arrivalDistributionParameters = new double[] { lambda };
                int nrCustomers = arrivalDistributionParameters.Length;
                Distribution[] arrivalDistributions = new Distribution[nrCustomers];
                for (int i = 0; i < nrCustomers; i++)
                {
                    arrivalDistributions[i] = new ExponentialDistribution(rng.Value, arrivalDistributionParameters[i]);
                }

                // Server-Customer Eligibility
                bool[][] eligibility = new bool[nrServers][];
                for (int i = 0; i < nrServers; i++)
                {
                    bool[] tmp = new bool[nrCustomers];
                    for (int j = 0; j < nrCustomers; j++)
                    {
                        tmp[j] = true;
                    }
                    eligibility[i] = tmp;
                }

                // Main simulation loop
                Results results = new Results(R, nrServers, nrCustomers, filename);
                for (int r = 0; r < R; r++)
                {
                    double t = 0;

                    FutureEvents futureEvents = new FutureEvents();
                    for (int i = 0; i < nrCustomers; i++)
                    {
                        Customer customer = new Customer(i);
                        futureEvents.Add(new Event(Event.ARRIVAL, arrivalDistributions[i].Next(), customer));
                    }

                    CustomerQueue customerQueue = new CustomerQueue();

                    ServerQueue idleServerQueue = new ServerQueue();
                    for (int i = 0; i < nrServers; i++)
                    {
                        Server server = new Server(i, eligibility[i]);
                        idleServerQueue.Add(server);
                    }

                    while (t < T)
                    {
                        Event e = futureEvents.Next();
                        t       = e.time;
                        results.Register(r, e, customerQueue, idleServerQueue);

                        if (e.type == Event.ARRIVAL)
                        {
                            customerQueue.CustomerCheckIn(e.customer, t);
                            Server server = idleServerQueue.FindServer(e.customer.cID);
                            if (server != null) // Eligible server available
                            {
                                idleServerQueue.Remove(server);
                                e.customer.server        = server;
                                e.customer.serviceTime   = t;
                                e.customer.departureTime = t + serviceDistributions[server.sID].Next();
                                futureEvents.Add(new Event(Event.DEPARTURE, e.customer.departureTime, e.customer));
                            }
                            futureEvents.Add(new Event(Event.ARRIVAL, t + arrivalDistributions[e.customer.cID].Next(), new Customer(e.customer.cID)));
                        }
                        else if (e.type == Event.DEPARTURE)
                        {
                            Customer customer = customerQueue.FindCustomer(e.customer.server);
                            if (customer != null)
                            {
                                customer.serviceTime   = t;
                                customer.departureTime = t + serviceDistributions[customer.server.sID].Next();
                                futureEvents.Add(new Event(Event.DEPARTURE, customer.departureTime, customer));
                            }
                            else
                            {
                                idleServerQueue.Add(e.customer.server);
                            }
                            customerQueue.CustomerCheckOut(e.customer);
                        }
                        else
                        {
                            Console.WriteLine("Invalid event type.");
                        }
                    }
                }
                results.GetMeans(rho / 20.0, serviceDistributions[0].residual);
                counter++;
                Console.WriteLine("\r{0}", counter);
            });
            Console.WriteLine("\nSimulation complete.\nTotal elapsed time {0:0.00} minutes", stopwatch.ElapsedMilliseconds / 1000 / 60);
            //Console.ReadLine();
        }