Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public List<City> NearestInsertion(List<City> Cities)
- {
- List<City> Solution = new List<City>();
- List<bool> IsCityAdded = new List<bool>();
- IsCityAdded = InitTabWithValue(IsCityAdded, Cities.Count, false);
- int firstCityIndex = rnd.Next(0, Cities.Count);
- Solution.Add(Cities[firstCityIndex]);
- IsCityAdded[firstCityIndex] = true;
- int NumberOfCitiesInSolution = 1;
- while (!AllCitiesAdded(IsCityAdded))
- {
- int bestDistance = int.MaxValue;
- int tmpDistance = 0;
- int CityToAddIndex = 0;
- for (int i = 0; i < Cities.Count; i++)
- {
- if (IsCityAdded[i] == false)
- {
- for (int j = 0; j < NumberOfCitiesInSolution; j++)
- {
- tmpDistance = City.getDestination(Cities[i], Solution[j]);
- if (tmpDistance < bestDistance)
- {
- bestDistance = tmpDistance;
- CityToAddIndex = i;
- }
- }
- }
- }
- int IndexToInsert = 0;
- int bestGrowth = int.MaxValue;
- int tmpBestGrowth = 0;
- for (int i = 1; i < NumberOfCitiesInSolution; i++)
- {
- tmpBestGrowth = City.getDestination(Cities[CityToAddIndex], Solution[i - 1]) + City.getDestination(Cities[CityToAddIndex], Solution[i]) -
- City.getDestination(Solution[i - 1], Solution[i]);
- if (tmpBestGrowth < bestGrowth)
- {
- IndexToInsert = i;
- bestGrowth = tmpBestGrowth;
- }
- }
- Solution.Insert(IndexToInsert, Cities[CityToAddIndex]);
- IsCityAdded[CityToAddIndex] = true;
- NumberOfCitiesInSolution++;
- }
- return Solution;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement