Beispiel #1
0
        /// <summary>
        /// Starts the TSP algorithm.
        /// To stop before all generations are calculated, set <see cref="Halt"/> to true.
        /// </summary>
        /// <param name="populationSize">Number of random tours to create before starting the algorithm.</param>
        /// <param name="maxGenerations">Number of times to perform the crossover operation before stopping.</param>
        /// <param name="groupSize">Number of tours to examine in each generation. Top 2 are chosen as the parent tours whose children replace the worst 2 tours in the group.</param>
        /// <param name="mutation">Odds that a child tour will be mutated..</param>
        /// <param name="seed">Seed for the random number generator.</param>
        /// <param name="chanceToUseCloseCity">The odds (out of 100) that a city that is known to be close will be used in any given link.</param>
        /// <param name="cityList">List of cities in the tour.</param>
        public void Begin(int populationSize, int maxGenerations, int groupSize, int mutation, int seed, int chanceToUseCloseCity, List <City> cityList)
        {
            Rand     = new Random(seed);
            CityList = cityList;

            Population1 = RouteHelper.CreateRandomPopulation(populationSize, cityList, Rand, chanceToUseCloseCity);

            DisplayRoute(RouteHelper.BestRoute, 0, false);

            int generation;

            for (generation = 0; generation < maxGenerations; generation++)
            {
                if (Halt)
                {
                    break;  // GUI has requested we exit.
                }
                var foundNewBestTour = MakeChildren(groupSize, mutation);

                if (foundNewBestTour)
                {
                    DisplayRoute(RouteHelper.BestRoute, generation, false);
                }
            }

            DisplayRoute(RouteHelper.BestRoute, generation, true);
        }