示例#1
0
        public static WarehouseResult Optimize(WarehouseParameters warehouseParameters, CancellationToken ct, Random random)
        {
            WarehouseManager warehouseManager = new WarehouseManager();

            double[][] distancesMatrix = warehouseManager.CreateWarehouseDistancesMatrix(warehouseParameters.WarehousePath);
            Distances.Create(distancesMatrix);
            Orders orders = new Orders(warehouseParameters.OrdersPath, warehouseManager.WarehouseSize);

            GeneticWarehouse geneticWarehouse = new GeneticWarehouse(warehouseParameters.WarehouseGeneticAlgorithmParameters,
                                                                     warehouseManager.WarehouseSize,
                                                                     (population) =>
            {
                double[] fitness = new double[population.Length];
                warehouseParameters.FitnessGeneticAlgorithmParameters.WriteCsv = false;
                Parallel.For(0, population.Length, i =>
                {
                    var results = Fitness.CalculateAllOrdersFitness(orders, population[i],
                                                                    warehouseParameters.FitnessGeneticAlgorithmParameters, random);
                    fitness[i] = results.Sum(x => x.Fitness);
                });

                return(fitness);
            }, ct, random);

            var z      = geneticWarehouse.Run();
            var result = Fitness.CalculateAllOrdersFitness(orders, z.BestChromosome, warehouseParameters.FitnessGeneticAlgorithmParameters, random);

            z.FinalFitness    = result.Sum(x => x.Fitness);
            z.FinalOrderPaths = result.Select(x => x.Path).ToArray();
            return(z);
        }
示例#2
0
        public static double FindShortestPath(OptimizationParameters optimizationParameters, CancellationToken ct, int seed = 0)
        {
            seed = GetSeed(seed);
            var random = new Random(seed);
            var matrix = Files.ReadArray(optimizationParameters.DataPath);

            Distances.Create(matrix);
            return(PathFinding.ShortestPath.Find(PointsArrayGenerator.GeneratePointsToVisit(matrix.Length), optimizationParameters, ct, random));
        }
示例#3
0
        public static void FindShortestPath(OptimizationParameters optimizationParameters,
                                            DelegateFitness.CalcFitness calcFitness, int seed = 0)
        {
            seed = GetSeed(seed);
            var random = new Random(seed);
            var matrix = Files.ReadArray(optimizationParameters.DataPath);

            Distances.Create(matrix);
            PathFinding.ShortestPath.Find(PointsArrayGenerator.GeneratePointsToVisit(matrix.Length), optimizationParameters, calcFitness, CancellationToken.None, random);
        }
示例#4
0
        public static TSPResult TSP(OptimizationParameters optimizationParameters, CancellationToken ct, int seed = 0)
        {
            seed = GetSeed(seed);
            var random = new Random(seed);

            var matrix = Files.ReadArray(optimizationParameters.DataPath);

            Distances.Create(matrix);
            var tsp = new GeneticTSP(PointsArrayGenerator.GeneratePointsToVisit(matrix.Length), optimizationParameters,
                                     (population) =>
            {
                double[] fitness = new double[population.Length];
                for (int i = 0; i < population.Length; i++)
                {
                    fitness[i] = Fitness.CalculateFitness(population[i]);
                }
                return(fitness);
            }, ct, random);
            var result = tsp.Run();

            result.Seed = seed;
            return(result);
        }
示例#5
0
 /// <summary>Returns an empty distance map.</summary>
 protected Distance[,] GetEmptyDistances()
 {
     return(Distances.Create(this.Map));
 }