private static void CreateCityListBasedOnNearestFirst(List <CityInfo> cityList, int startCityID, List <CityInfo> sample)
        {
            cityList.Add(sample.Find(city => city.ID == startCityID));

            while (cityList.Count < sample.Count)
            {
                float    distance = float.MaxValue;
                CityInfo nextCity = cityList.First();

                foreach (CityInfo city in sample)
                {
                    if (cityList.Find(c => c.ID == city.ID) != null)
                    {
                        continue;
                    }

                    float temp = DistanceHelper.DistanceBetweenTwoPoints(city, cityList.Last());
                    if (temp < distance)
                    {
                        distance = temp;
                        nextCity = city;
                    }
                }

                cityList.Add(nextCity);
            }
        }
        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);
            }
        }
Exemple #3
0
        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);
        }
Exemple #4
0
 private void RefreshList()
 {
     currentPopulation[0] = initialCityListOne;
     currentPopulation[1] = initialCityListTwo;
     currentPopulation[2] = initialCityListThree;
     currentPopulation[3] = initialCityListFour;
     currentDistances[0]  = DistanceHelper.TotalDistance(initialCityListOne);
     currentDistances[1]  = DistanceHelper.TotalDistance(initialCityListTwo);
     currentDistances[2]  = DistanceHelper.TotalDistance(initialCityListThree);
     currentDistances[3]  = DistanceHelper.TotalDistance(initialCityListFour);
     RefreshPicBox(initialCityListOne);
 }
Exemple #5
0
        public static void Evolution(ref List <CityInfo>[] previousPopulation, ref float[] previousDistances, int crossoverSelection, float mutationPossibility)
        {
            float[]           fitnesses      = Fitness(previousDistances);
            List <CityInfo>[] selected       = Selection(previousPopulation, fitnesses);
            List <CityInfo>[] nextPopulation = Crossover(selected, crossoverSelection);
            Mutation(ref nextPopulation, mutationPossibility);

            float[] nextDistances = new float[4];
            for (int i = 0; i < nextPopulation.Count(); i++)
            {
                nextDistances[i] = DistanceHelper.TotalDistance(nextPopulation[i]);
            }

            Elimination(ref previousDistances, ref nextDistances, ref previousPopulation, ref nextPopulation);
            previousPopulation = nextPopulation;
            previousDistances  = nextDistances;
        }