Advertisement
Guest User

Untitled

a guest
Apr 29th, 2016
46
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.14 KB | None | 0 0
  1. public List<City> NearestInsertion(List<City> Cities)
  2.         {
  3.             List<City> Solution = new List<City>();
  4.             List<bool> IsCityAdded = new List<bool>();
  5.             IsCityAdded = InitTabWithValue(IsCityAdded, Cities.Count, false);
  6.             int firstCityIndex = rnd.Next(0, Cities.Count);
  7.             Solution.Add(Cities[firstCityIndex]);
  8.             IsCityAdded[firstCityIndex] = true;
  9.             int NumberOfCitiesInSolution = 1;
  10.  
  11.             while (!AllCitiesAdded(IsCityAdded))
  12.             {
  13.                 int bestDistance = int.MaxValue;
  14.                 int tmpDistance = 0;
  15.                 int CityToAddIndex = 0;
  16.                 for (int i = 0; i < Cities.Count; i++)
  17.                 {
  18.                     if (IsCityAdded[i] == false)
  19.                     {
  20.                         for (int j = 0; j < NumberOfCitiesInSolution; j++)
  21.                         {
  22.                             tmpDistance = City.getDestination(Cities[i], Solution[j]);
  23.                             if (tmpDistance < bestDistance)
  24.                             {
  25.                                 bestDistance = tmpDistance;
  26.                                 CityToAddIndex = i;
  27.                             }
  28.                         }
  29.                     }
  30.                 }
  31.                 int IndexToInsert = 0;
  32.                 int bestGrowth = int.MaxValue;
  33.                 int tmpBestGrowth = 0;
  34.                 for (int i = 1; i < NumberOfCitiesInSolution; i++)
  35.                 {
  36.                     tmpBestGrowth = City.getDestination(Cities[CityToAddIndex], Solution[i - 1]) + City.getDestination(Cities[CityToAddIndex], Solution[i]) -
  37.                         City.getDestination(Solution[i - 1], Solution[i]);
  38.                     if (tmpBestGrowth < bestGrowth)
  39.                     {
  40.                         IndexToInsert = i;
  41.                         bestGrowth = tmpBestGrowth;
  42.                     }
  43.                 }
  44.                 Solution.Insert(IndexToInsert, Cities[CityToAddIndex]);
  45.                 IsCityAdded[CityToAddIndex] = true;
  46.                 NumberOfCitiesInSolution++;
  47.             }
  48.             return Solution;
  49.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement