/// <summary> /// Randomly select a group of tours from the population. /// The top 2 are chosen as the parent tours. /// Crossover is performed on these 2 tours. /// The childred tours from this process replace the worst 2 tours in the group. /// </summary> /// <param name="groupSize">Number of tours in this group.</param> /// <param name="mutation">Odds that a child will be mutated.</param> bool makeChildren(int groupSize, int mutation) { int[] tourGroup = new int[groupSize]; int tourCount, i, topTour, childPosition, tempTour; // pick random tours to be in the neighborhood city group // we allow for the same tour to be included twice for (tourCount = 0; tourCount < groupSize; tourCount++) { tourGroup[tourCount] = rand.Next(population.Count); } // bubble sort on the neighborhood city group for (tourCount = 0; tourCount < groupSize - 1; tourCount++) { topTour = tourCount; for (i = topTour + 1; i < groupSize; i++) { if (population[tourGroup[i]].Fitness < population[tourGroup[topTour]].Fitness) { topTour = i; } } if (topTour != tourCount) { tempTour = tourGroup[tourCount]; tourGroup[tourCount] = tourGroup[topTour]; tourGroup[topTour] = tempTour; } } bool foundNewBestTour = false; // take the best 2 tours, do crossover, and replace the worst tour with it childPosition = tourGroup[groupSize - 1]; population[childPosition] = Tour.Crossover(population[tourGroup[0]], population[tourGroup[1]], cityList, rand); if (rand.Next(100) < mutation) { population[childPosition].Mutate(rand); } population[childPosition].DetermineFitness(cityList); // now see if the first new tour has the best fitness if (population[childPosition].Fitness < population.BestTour.Fitness) { population.BestTour = population[childPosition]; foundNewBestTour = true; } // take the best 2 tours (opposite order), do crossover, and replace the 2nd worst tour with it childPosition = tourGroup[groupSize - 2]; population[childPosition] = Tour.Crossover(population[tourGroup[1]], population[tourGroup[0]], cityList, rand); if (rand.Next(100) < mutation) { population[childPosition].Mutate(rand); } population[childPosition].DetermineFitness(cityList); // now see if the second new tour has the best fitness if (population[childPosition].Fitness < population.BestTour.Fitness) { population.BestTour = population[childPosition]; foundNewBestTour = true; } return(foundNewBestTour); }
/// <summary> /// Random select a group /// </summary> bool makeChildren(int groupSize, int mutation) { int[] tourGroup = new int[groupSize]; int tourCount, i, topTour, childPosition, tempTour; for (tourCount = 0; tourCount < groupSize; tourCount++) { tourGroup[tourCount] = rand.Next(population.Count); } for (tourCount = 0; tourCount < groupSize - 1; tourCount++) { topTour = tourCount; for (i = topTour + 1; i < groupSize; i++) { if (population[tourGroup[i]].Fitness < population[tourGroup[topTour]].Fitness) { topTour = i; } } if (topTour != tourCount) { tempTour = tourGroup[tourCount]; tourGroup[tourCount] = tourGroup[topTour]; tourGroup[topTour] = tempTour; } } bool foundNewBestTour = false; childPosition = tourGroup[groupSize - 1]; population[childPosition] = Tour.Crossover(population[tourGroup[0]], population[tourGroup[1]], cityList, rand); if (rand.Next(100) < mutation) { population[childPosition].Mutate(rand); } population[childPosition].DetermineFitness(cityList); if (population[childPosition].Fitness < population.BestTour.Fitness) { population.BestTour = population[childPosition]; foundNewBestTour = true; } childPosition = tourGroup[groupSize - 2]; population[childPosition] = Tour.Crossover(population[tourGroup[1]], population[tourGroup[0]], cityList, rand); if (rand.Next(100) < mutation) { population[childPosition].Mutate(rand); } population[childPosition].DetermineFitness(cityList); if (population[childPosition].Fitness < population.BestTour.Fitness) { population.BestTour = population[childPosition]; foundNewBestTour = true; } return(foundNewBestTour); }