Exemplo n.º 1
0
        /// <summary>
        /// Breeds a Population of Genomes
        /// </summary>
        public void BreedNewPopulation()
        {
            if (population.Count == 0)
            {
                CreateInitialPopulation();
                return;
            }
            if (species.Count == 0)                       // No Species from previous Generation
            {
                Functions.FisherYatesShuffle(population); // Shuffle Previous Generation (so that initial Species-Mascots are randomized)
            }
            Speciate();
            // Clear old population to make room for new population (references exist through species)
            population.Clear();
            // Create new Population
            float sum = 0;

            // Find population-size per species
            for (int i = 0; i < species.Count; i++)
            {
                sum += species[i].GetFitness();     // Total Fitness
            }
            for (int i = 0; i < species.Count; i++) // For Each Species
            {
                Species s       = species[i];
                int     popSize = (int)Math.Ceiling(s.GetFitness() / sum * PopulationSize);
                // Darwin multikill:
                s.KillOffPercentage(breedingPercentage);
                // Breed within Species
                s.BreedPopulation(popSize, mutationChance);
                // Add to population
                population.AddRange(s.Genomes);
            }
            while (population.Count > PopulationSize)                      // Remove Excess from Math.Ceil
            {
                population.Remove(Functions.GetRandomElement(population)); // Darwin Sniper
            }
            MutationFactory.EndGeneration();
        }