private static void CreateCityListBasedOnNearestInsertionHeuristic(List <CityInfo> cityList, int startCityID, List <CityInfo> sample) { //firstly, find the first two cities taht salesman will visit //the fisrt city is based on the user choice, the second city is the city which is most closest to first city CityInfo startCity = sample.Find(city => city.ID == startCityID); cityList.Add(startCity); CityInfo secondCity = sample[0]; float distance = float.MaxValue; foreach (CityInfo city in sample) { if (city.ID == startCity.ID) { continue; } float temp = DistanceHelper.DistanceBetweenTwoPoints(startCity, city); if (distance > temp) { secondCity = city; distance = temp; } } cityList.Add(secondCity); //Closest Edge Insertion Heuristic while (cityList.Count != sample.Count) { //Determine whether adding this city to the partial tour based on Closest Edge Insertion Heuristic distance = float.MaxValue; CityInfo nextCity = null; int insertIndex = 0; foreach (CityInfo potentialNextCity in sample) { if (cityList.Exists(c => c.ID == potentialNextCity.ID)) { //if the city is already visited, skip it continue; } //compute the distance between potentialNextCity with all edge in the partial tour for (int i = 0; i < cityList.Count - 1; i++) { float temp = DistanceHelper.DistanceFromPointToLine(cityList[i], cityList[i + 1], potentialNextCity); if (temp < distance) { distance = temp; nextCity = potentialNextCity; insertIndex = i + 1; } } } cityList.Insert(insertIndex, nextCity); } }
static bool AreaCheck(CityInfo cityOne, CityInfo cityTwo, CityInfo cityThree, CityInfo cityFour) { float areaOne, areaTwo; areaOne = 0.5f * DistanceHelper.DistanceBetweenTwoPoints(cityOne, cityThree) * (DistanceHelper.DistanceFromPointToLine(cityOne, cityThree, cityTwo) + DistanceHelper.DistanceFromPointToLine(cityOne, cityThree, cityFour)); areaTwo = 0.5f * DistanceHelper.DistanceBetweenTwoPoints(cityTwo, cityFour) * (DistanceHelper.DistanceFromPointToLine(cityTwo, cityFour, cityOne) + DistanceHelper.DistanceFromPointToLine(cityTwo, cityFour, cityThree)); return(areaOne == areaTwo ? true : false); }