示例#1
0
        private static List <OperatingData> GenerateInitialPopulation()
        {
            var randomSolution = new RandomSolution();
            var localSearch    = new LocalSearch();
            var population     = new List <OperatingData>();

            for (var i = 0; i < DAL.PopulationSize; i++)
            {
                var solution = new OperatingData {
                    UnusedNodes = DAL.Instance.Nodes.CloneList()
                };
                localSearch.Optimize(randomSolution.FindRouteFromRandomStart(solution));

                while (population.Any(solution2 => solution2.Distance == solution.Distance))
                {
                    solution = new OperatingData {
                        UnusedNodes = DAL.Instance.Nodes.CloneList()
                    };
                    localSearch.Optimize(randomSolution.FindRouteFromRandomStart(solution));
                }

                population.Add(solution);
            }

            return(population);
        }
示例#2
0
        //private const int NumberOfLocalSearchResults = 1000;
        //private const int EvaluationTimeInMiliseconds = 83360;
        //private const int NumberOfHybridGeneticAlgorithmExecutions = 10;
        //private const int PopulationSize = 20;
        static void Main(string[] args)
        {
            DAL.Instance.ReadFromFile();

            var hybridAlgorithmGlobalTimer = new Stopwatch();
            var localSearchTimer           = new Stopwatch();
            var randomSolution             = new RandomSolution();
            var localSearch      = new LocalSearch();
            var random           = new Random();
            var hybridStatistics = new List <List <HybridAlgorithmStatisticsData> >(DAL.NumberOfHybridGeneticAlgorithmExecutions);

            //var population = new List<OperatingData>(20);

            for (var i = 0; i < DAL.NumberOfHybridGeneticAlgorithmExecutions; i++)
            {
                Console.WriteLine("Started iteration number:    " + (i + 1));

                var population = GenerateInitialPopulation();
                //var hybridStatisticsData = new HybridAlgorithmStatisticsData();
                var iterationNumber = 0;
                //var singleExecutionTime = long.MinValue;
                var hybridStatisticsSingleExecutionsList = new List <HybridAlgorithmStatisticsData>();

                hybridAlgorithmGlobalTimer.Restart();

                //Console.WriteLine("--- Ready to start iterating ---");
                //Console.ReadKey();

                //while (hybridAlgorithmGlobalTimer.ElapsedMilliseconds < DAL.EvaluationTimeInMiliseconds)
                while (hybridAlgorithmGlobalTimer.ElapsedMilliseconds < DAL.EvaluationTimeInMiliseconds)
                {
                    var parent1       = population[random.Next(0, population.Count)];
                    var parent2       = population[random.Next(0, population.Count)];
                    var worstSolution = FindWorstSolution(population);

                    while (parent1.Equals(parent2))
                    {
                        parent2 = population[random.Next(0, population.Count)];
                    }

                    var child = Recombine(parent1, parent2);

                    localSearchTimer.Reset();
                    localSearchTimer.Start();
                    localSearch.Optimize(child);
                    localSearchTimer.Stop();
                    //singleExecutionTime += localSearchTimer.ElapsedMilliseconds;

                    if (population.Any(solution => solution.Distance == child.Distance) == false && child.Distance < worstSolution.Distance)
                    {
                        population.Remove(worstSolution);
                        population.Add(child);
                    }

                    hybridStatisticsSingleExecutionsList.Add(new HybridAlgorithmStatisticsData
                    {
                        Distance        = FindBestDistance(population),
                        ExecutionTime   = localSearchTimer.Elapsed.Milliseconds,
                        IterationNumber = ++iterationNumber
                    });

                    if (iterationNumber % 100 == 0)
                    {
                        Console.WriteLine("--- Iteration number: " + iterationNumber + " ---");
                    }
                }

                hybridStatistics.Add(hybridStatisticsSingleExecutionsList);

                Console.WriteLine("\n--- BEST DISTANCE = " + FindBestDistance(population) + " ---\n");

                //DAL.Instance.HybridAlgorithmStatistics[i] = Tuple.Create(i+1, long.MinValue, FindBestDistance(population));
            }

            hybridAlgorithmGlobalTimer.Stop();

            DAL.Instance.PrepareFileToWrite();
            DAL.Instance.WriteToFileForHybridAlgorithm(hybridStatistics);
            DAL.Instance.CloseFileToWrite();

            Console.WriteLine("Done!");
            Console.ReadKey();
        }