Ejemplo n.º 1
0
        private static Distribution[] CreateServiceDistributions(Data data, Random rng)
        {
            Distribution[] serviceDistributions = new Distribution[data.nrServers];
            for (int i = 0; i < data.nrServers; i++)
            {
                switch (data.ServerParameters[i].Type)
                {
                case "Deterministic":
                    serviceDistributions[i] = new DeterministicDistribution(rng, data.ServerParameters[i].Average, data.ServerParameters[i].CV);
                    break;

                case "Uniform":
                    serviceDistributions[i] = new UniformDistribution(rng, data.ServerParameters[i].Average, data.ServerParameters[i].CV);
                    break;

                case "Exponential":
                    serviceDistributions[i] = new ExponentialDistribution(rng, data.ServerParameters[i].Average, data.ServerParameters[i].CV);
                    break;

                case "HyperExponential2":
                    serviceDistributions[i] = new Hyper2ExponentialDistribution(rng, data.ServerParameters[i].Average, data.ServerParameters[i].CV);
                    break;
                }
            }
            return(serviceDistributions);
        }
Ejemplo n.º 2
0
 private static Distribution[] CreateArrivalDistributions(Data data, Random rng, double lambda)
 {
     Distribution[] arrivalDistributions = new Distribution[data.nrCustomers];
     for (int i = 0; i < data.nrCustomers; i++)
     {
         arrivalDistributions[i] = new ExponentialDistribution(rng, 1 / (data.CustomerParameters[i].Fraction * lambda), 1);
     }
     return(arrivalDistributions);
 }
Ejemplo n.º 3
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();
        }