Example #1
0
        static void CrossOptimize()
        {
            var optimizer = new TwoOptimizer(cities);

            TaskLogger.TaskName = "Cross-optimization";

            optimizer.ClusterSet = nnCluster;
            optimizer.CrossOptimize(secondBestTour, bestTour);

            var newDistance = cities.GetDistance(secondBestTour);

            if (!AreTourValid())
            {
                TaskLogger.Stop();
                Console.WriteLine("Some error in the algorithm!!");
                Console.ReadKey();
                return;
            }

            if (newDistance < secondBestTourDistance && newDistance > bestTourDistance)
            {
                SaveTour(secondBestTour, SecondBestTourFileName);
                SaveTour(bestTour, BestTourFileName);
            }

            UpdateLoggerFooter();
        }
Example #2
0
        static void Main(string[] args)
        {
            TaskLogger.Start();

            ParseData();

            InitializeClusterSet();
            InitializeBestTour();
            InitializeSecondBestTour();

            UpdateLoggerFooter();

            CrossOptimize();

            Console.WriteLine("Genetic?");
            Console.ReadKey();

            var optimizer = new TwoOptimizer(cities);
            var geneticEngine = new GeneticTspEngine(cities);
            var nnTourFinder = new NNTourFinder(cities);

            while (secondBestTourDistance > TargetDistance)
            {
                TaskLogger.TaskName = "Genetic algorithm on first tour";

                geneticEngine.StartFromKnownSolutions(bestTour);
                while (geneticEngine.CurrentGeneration < 1000)
                {
                    geneticEngine.NextGeneration();

                    var currentSolution = geneticEngine.CurrentBestSolution;
                    if (currentSolution.Distance < bestTourDistance)
                    {
                        bestTour = currentSolution;
                        bestTourDistance = currentSolution.Distance;
                        SaveTour(bestTour, BestTourFileName);
                        UpdateLoggerFooter();
                    }
                }

                TaskLogger.TaskName = "Optimizationg of first tour";
                TaskLogger.ShowProgress();
                var gain = 0.0;
                do
                {
                    var oldDistance = bestTourDistance;

                    optimizer.Optimize(bestTour);
                    bestTourDistance = cities.GetDistance(bestTour);
                    UpdateLoggerFooter();

                    gain = oldDistance - bestTourDistance;

                    if (gain > 0)
                        SaveTour(bestTour, BestTourFileName);
                } while (gain > 1000.0);

                TaskLogger.TaskName = "Genetic algorithm on second disjoint tour";
                geneticEngine.Reset();
                geneticEngine.StartFromKnownSolutions(
                    secondBestTour,
                    new IntegerPermutation(nnTourFinder.FindRandomDisjointTour(bestTour)));

                while (geneticEngine.CurrentGeneration < 1000)
                {
                    geneticEngine.NextGeneration();

                    foreach (var currentSolution in geneticEngine.CurrentSolutionPool)
                    {
                        if (currentSolution.Distance > secondBestTourDistance)
                            break;

                        if (!CheckDisjointness(bestTour, secondBestTour))
                            continue;

                        if (currentSolution.Distance < secondBestTourDistance)
                        {
                            secondBestTour = currentSolution;
                            secondBestTourDistance = currentSolution.Distance;
                            SaveTour(secondBestTour, SecondBestTourFileName);
                            UpdateLoggerFooter();
                        }
                    }
                }

                TaskLogger.TaskName = "Optimization of second disjoint tour";
                TaskLogger.ShowProgress();
                do
                {
                    var oldDistance = secondBestTourDistance;

                    optimizer.OptimizeDisjoint(secondBestTour, bestTour);
                    secondBestTourDistance = cities.GetDistance(secondBestTour);
                    UpdateLoggerFooter();

                    gain = oldDistance - secondBestTourDistance;

                    if (gain > 0)
                        SaveTour(secondBestTour, SecondBestTourFileName);
                } while (gain > 1000.0);
            }

            TaskLogger.Stop();
        }