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 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); }