コード例 #1
0
        public static Population EvolvePopulation(Population p)
        {
            Population newPopulation = new Population(p.Size(), false);

            if (elitism)
            {
                newPopulation.SaveIndividual(0, p.GetFittest());
            }

            int elitismOffset;

            if (elitism)
            {
                elitismOffset = 1;
            }
            else
            {
                elitismOffset = 0;
            }

            for (int i = elitismOffset; i < p.Size(); i++)
            {
                Individual indiv1   = TournamentSelection(p);
                Individual indiv2   = TournamentSelection(p);
                Individual newIndiv = Crossover(indiv1, indiv2);
                newPopulation.SaveIndividual(i, newIndiv);
            }

            for (int i = elitismOffset; i < newPopulation.Size(); i++)
            {
                Mutate(newPopulation.GetIndividual(i));
            }

            return(newPopulation);
        }
コード例 #2
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="p"></param>
        /// <returns></returns>
        private static Individual TournamentSelection(Population p)
        {
            Population tournament = new Population(tournamentSize, false);
            Random     rndgen     = new Random();

            for (int i = 0; i < tournamentSize; i++)
            {
                int randomID = (int)(rndgen.NextDouble() * p.Size());
                tournament.SaveIndividual(i, p.GetIndividual(randomID));
            }
            Individual fittest = tournament.GetFittest();

            return(fittest);
        }