public static Solution RandomFeasibleSolution(VrptwProblem problem)
        {
            Solution solution          = new Solution(problem);
            var      customersToAssign = new List <Customer>(problem.Customers);

            while (customersToAssign.Count != 0)
            {
                Route route = new Route(problem);
                route.AddCustomer(problem.Depot);
                while (true)
                {
                    float    totalTime         = route.TotalTime();
                    Customer lastOneInRoute    = route.Customers.Last();
                    var      feasibleCustomers = customersToAssign.Where(
                        c =>
                        c.ReadyTime >= totalTime + problem.GetDistance(c.Id, lastOneInRoute.Id) ||
                        (c.ReadyTime < totalTime + problem.GetDistance(c.Id, lastOneInRoute.Id) &&
                         c.DueDate >= totalTime + problem.GetDistance(c.Id, lastOneInRoute.Id))).ToList();
                    if (feasibleCustomers.Count > 0)
                    {
                        Customer randomCustomer = feasibleCustomers.ElementAt(RandomGenerator.NextInt(feasibleCustomers.Count()));
                        customersToAssign.Remove(randomCustomer);
                        route.AddCustomer(randomCustomer);
                    }
                    else
                    {
                        route.AddCustomer(problem.Depot);
                        solution.Routes.Add(route);
                        break;
                    }
                }
            }
            return(solution);
        }
コード例 #2
0
        public JsonResult LoadProblemInstance(string instanceId)
        {
            string       filePath = Server.MapPath("~/Data/Homberger/" + instanceId + @".txt");
            VrptwProblem problem  = reader.ReadFromFile(filePath);

            return(new JsonResult
            {
                Data = problem,
                JsonRequestBehavior = JsonRequestBehavior.AllowGet
            });
        }
        public override Chromosome RandomSolution(int geneNumber, IProblem problem)
        {
            VrptwProblem vrptwProblem = problem as VrptwProblem;
            List <int>   customers    = Enumerable.Range(0, geneNumber).ToList();

            customers.Shuffle();
            IGene[] genes = new IGene[geneNumber];
            for (int i = 0; i < geneNumber; ++i)
            {
                genes[i] = new CustomerGene(vrptwProblem.Customers[customers[i]]);
            }
            Solution solution = new Solution(vrptwProblem, genes);

            return(solution);
        }
コード例 #4
0
        public void Run(ResearchParameters parameters, string inputPath, string outputPath, Action <string, float> callback, string connectionId)
        {
            int distanceWeight = 1;
            Dictionary <string, int> customerNumbers = new Dictionary <string, int>()
            {
                { "1", parameters.RoadWeight200 },
                { "2", parameters.RoadWeight200 },
                //{ "4", parameters.RoadWeight400 },
                //{ "6", parameters.RoadWeight600 }, { "8", parameters.RoadWeight800 },
                //{ "10", parameters.RoadWeight1000 }
            };

            float step = 1.0f /
                         (CUSTOMER_TYPES.Count() * customerNumbers.Count * CUSTOMER_INSTANCES.Count() *
                          (parameters.CrossoverOperators.Count + parameters.MutationOperators.Count) * parameters.RandomWalkNumber);

            foreach (var type in CUSTOMER_TYPES)
            {
                foreach (var number in customerNumbers)
                {
                    for (int index = 0; index < CUSTOMER_INSTANCES.Count(); ++index)
                    {
                        string          instanceId = type + '_' + number.Key + '_' + CUSTOMER_INSTANCES[index];
                        VrptwProblem    problem    = reader.ReadFromFile(inputPath + @"\" + instanceId + ".txt");
                        FitnessFunction ff         = new FitnessFunction(number.Value, distanceWeight);
                        Landscape       landscape  = new Landscape(problem, factory, ff);
                        foreach (var op in parameters.CrossoverOperators)
                        {
                            RunSingleResearch(landscape, parameters, op, instanceId, inputPath, outputPath,
                                              callback, connectionId, step);
                        }
                        foreach (var op in parameters.MutationOperators)
                        {
                            RunSingleResearch(landscape, parameters, op, instanceId, inputPath, outputPath,
                                              callback, connectionId, step);
                        }
                    }
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Reads the VRPTW problem instance from a text file.
        /// </summary>
        /// <param name="filePath">The file path.</param>
        /// <returns>A VRPTW problem instance.</returns>
        public VrptwProblem ReadFromFile(string filePath)
        {
            string           name;
            uint             vehicleNumber, vehicleCapacity;
            IList <Customer> customers = new List <Customer>();
            Customer         depot;

            using (StreamReader sr = new StreamReader(filePath))
            {
                name = sr.ReadLine();
                sr.ReadLine(); sr.ReadLine(); sr.ReadLine();
                string vehicleData = sr.ReadLine();
                ReadVehicleNumberAndCapacity(vehicleData, out vehicleNumber, out vehicleCapacity);
                sr.ReadLine(); sr.ReadLine(); sr.ReadLine(); sr.ReadLine();
                // reading the customers data
                string          customerData;
                MatchCollection data;
                Customer        customer;
                Regex           customerRegex = new Regex(ROW_PATTERN);
                customerData = sr.ReadLine();
                data         = customerRegex.Matches(customerData);
                depot        = ReadCustomer(data);
                while ((customerData = sr.ReadLine()) != null)
                {
                    if (string.IsNullOrWhiteSpace(customerData))
                    {
                        continue;
                    }
                    data     = customerRegex.Matches(customerData);
                    customer = ReadCustomer(data);
                    customers.Add(customer);
                }
            }
            VrptwProblem problem = new VrptwProblem(name, vehicleNumber, vehicleCapacity, depot, customers);

            return(problem);
        }
コード例 #6
0
 public void SetUp()
 {
     reader  = new VrptwProblemReader();
     problem = reader.ReadFromFile(FILE_PATH);
 }
        public void Test1()
        {
            AbstractChromosomeFactory factory = new SolutionFactory();

            int[] routeWeights = new int[]
            {
                20000, 50000, 120000, 200000, 350000
            };
            int distanceWeight = 1;

            string[] customerTypes = new string[] { "C1", "C2", "R1", "R2", "RC1", "RC2" };
            Dictionary <string, int> customerNumbers = new Dictionary <string, int>()
            {
                { "2", 20000 }, { "4", 50000 }, { "6", 120000 }, { "8", 200000 }, { "10", 350000 }
            };

            string[] customerInstances = new string[] { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10" };

            CrossoverOperator[] crossoverOps = new CrossoverOperator[]
            {
                new OrderCrossover(), new PartiallyMatchedCrossover(), new CycleCrossover(), new UniformBasedOrderCrossover()
            };

            MutationOperator[] mutationOps = new MutationOperator[]
            {
                new SwapOperator(), new InsertionOperator(), new InversionOperator(), new DisplacementOperator()
            };

            int randomWalkNumber = 2000, randomWalkSteps = 5000;

            string floatPattern = "0.000", separator = ",";
            float  epsilon = 0.05f;

            foreach (var type in customerTypes)
            {
                foreach (var number in customerNumbers)
                {
                    foreach (var instance in customerInstances)
                    {
                        string          instanceId = type + '_' + number.Key + '_' + instance;
                        VrptwProblem    problem    = reader.ReadFromFile(FILE_PATH + @"\" + instanceId + ".txt");
                        FitnessFunction ff         = new FitnessFunction(number.Value, distanceWeight);
                        Landscape       landscape  = new Landscape(problem, factory, ff);
                        foreach (var op in crossoverOps)
                        {
                            string path = RESULT_PATH + @"\" + instanceId + "_" + op.GetId() + ".csv";
                            if (!File.Exists(path))
                            {
                                File.Create(path).Close();
                                File.ReadAllText(path);
                                using (TextWriter tw = new StreamWriter(path))
                                {
                                    tw.WriteLine("AC, IC, PIC, DBI");
                                    for (int i = 0; i < randomWalkNumber; ++i)
                                    {
                                        var    rwResult = landscape.RandomWalk(randomWalkSteps, op);
                                        float  ac       = Autocorrelation.Run(rwResult);
                                        float  ic       = InformationContent.Run(rwResult, epsilon);
                                        float  pic      = PartialInformationContent.Run(rwResult, epsilon);
                                        float  dbi      = DensityBasinInformation.Run(rwResult, epsilon);
                                        string line     =
                                            ac.ToString(floatPattern) + separator +
                                            ic.ToString(floatPattern) + separator +
                                            pic.ToString(floatPattern) + separator +
                                            dbi.ToString(floatPattern);
                                        tw.WriteLine(line);
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }