Esempio n. 1
0
        private void NextGeneration()
        {
            AverageFitnessInPreviousGeneration = Species.Average(s => s.AverageFitness);
            foreach (var specie in Species)
            {
                specie.Genomes = specie.GetMostFitGenomes();
            }

            Species = RemoveStaleSpecies(Species);

            SetGenomeRanks(Species);
            Species = Species.Where(s => s.AverageGlobalRank * Config.Population >= 1).ToList();
            var totalAverageRanks = Species.Sum(s => s.AverageGlobalRank);

            var nextGenerationChildrenGenomes = new List <Genome>();

            foreach (var specie in Species)
            {
                var childrenGenomesTobreed = (int)((specie.AverageGlobalRank / totalAverageRanks) * Config.Population - 1);
                for (int i = 0; i < childrenGenomesTobreed; i++)
                {
                    nextGenerationChildrenGenomes.Add(specie.Breed());
                }
            }

            Species = CullSpecies(Species);

            while (Species.Count + nextGenerationChildrenGenomes.Count < Config.Population)
            {
                var randomSpecie = Species.GetRandomElement();
                nextGenerationChildrenGenomes.Add(randomSpecie.Breed());
            }

            foreach (var childrenGenome in nextGenerationChildrenGenomes)
            {
                AssignSpecie(Species, childrenGenome);
            }

            Generation++;
            TopFitnessInPreviousGeneration = TopFitnessInGeneration;
            TopFitnessInGeneration         = 0;
        }