private Solution GenerateRandomSolution() { double discriminator = random.NextDouble(); IntegerPermutation result; if (discriminator > this.NNProbability) { var availableNumbers = IntegerRandomAccessSet.CreateFullSet(nodePositions.Length); int index = 0; result = new IntegerPermutation(nodePositions.Length); while (availableNumbers.ItemsCount > 0) { int randomValue = random.Next(0, availableNumbers.ItemsCount); result[index++] = availableNumbers[randomValue]; availableNumbers.RemoveAt(randomValue); } } else { int random1 = random.Next(0, nodePositions.Length); int random2 = random.NextDifferent(0, nodePositions.Length, random1); var startEdge = new Edge(random1, random2); var nnTourFinder = new NNTourFinder(nodePositions); result = new IntegerPermutation(nnTourFinder.FindTourStartingFrom(startEdge)); } return this.CreateSolution(result); }
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(); }
static void InitializeBestTour() { TaskLogger.TaskName = "Initializing first tour"; if (TourExists(BestTourFileName)) { bestTour = LoadTour(BestTourFileName); } else { Edge shortestEdge; if (TourExists(OneEdgeTourFileName)) { shortestEdge = LoadOneEdgeTour(); } else { var shortestEdgeFinder = new ShortestEdgeFinder(InitializationThreadCount); shortestEdge = shortestEdgeFinder.FindInCompleteGraph(cities); SaveOneEdgeTour(shortestEdge); } var nnTourFinder = new NNTourFinder(cities); bestTour = new IntegerPermutation(nnTourFinder.FindTourStartingFrom(shortestEdge)); SaveTour(bestTour, BestTourFileName); } bestTourDistance = cities.GetDistance(bestTour); }
static void InitializeSecondBestTour() { TaskLogger.TaskName = "Initializing second disjoint tour"; if (TourExists(SecondBestTourFileName)) { secondBestTour = LoadTour(SecondBestTourFileName); } else { var nnTourFinder = new NNTourFinder(cities); secondBestTour = new IntegerPermutation(nnTourFinder.FindRandomDisjointTour(bestTour)); SaveTour(secondBestTour, SecondBestTourFileName); } secondBestTourDistance = cities.GetDistance(secondBestTour); }