/// <summary> /// Find the cities that are closest to this one. /// </summary> /// <param name="numberOfCloseCities">When creating the initial population of tours, this is a greater chance /// that a nearby city will be chosen for a link. This is the number of nearby cities that will be considered close.</param> public void FindClosestCities(int numberOfCloseCities) { double shortestDistance; int shortestCity = 0; double[] dist = new double[Distances.Count]; Distances.CopyTo(dist); if (numberOfCloseCities > Distances.Count - 1) { numberOfCloseCities = Distances.Count - 1; } closeCities.Clear(); for (int i = 0; i < numberOfCloseCities; i++) { shortestDistance = Double.MaxValue; for (int cityNum = 0; cityNum < Distances.Count; cityNum++) { if (dist[cityNum] < shortestDistance) { shortestDistance = dist[cityNum]; shortestCity = cityNum; } } closeCities.Add(shortestCity); dist[shortestCity] = Double.MaxValue; } }
//Метод для нахождения ближайшего города public void NearestTownDistance(int nearTownCount) { double closestDistance; int nearestTown = 0; double[] way = new double[Distances.Count]; Distances.CopyTo(way); if (nearTownCount > Distances.Count - 1) { nearTownCount = Distances.Count - 1; } nearTown.Clear(); for (int i = 0; i < nearTownCount; i++) { closestDistance = Double.MaxValue; for (int townNum = 0; townNum < Distances.Count; townNum++) { if (way[townNum] < nearestTown) { closestDistance = way[townNum]; nearestTown = townNum; } nearTown.Add(nearestTown); way[nearestTown] = Double.MaxValue; } } }