示例#1
0
文件: Population.cs 项目: afalz/NEAT
        public void Speciate()
        {
            Genomes.ToList().ForEach(genome => Speciate(genome));

            // Clean up any Species with no Genomes
            Species = Species.Where(s => s.Genomes.Any()).ToList();
        }
示例#2
0
文件: Population.cs 项目: afalz/NEAT
        public void Speciate(IGenome genome)
        {
            bool newSpecies = true;

            // Remove from current species
            if (genome.SpeciesId.HasValue)
            {
                Species.First(s => s.Id == genome.SpeciesId).Genomes.Remove(genome);
            }

            foreach (ISpecies species in Species.Where(s => s.Genomes.Any()))
            {
                IGenome representative = species.Genomes.First();

                if (genome.IsCompatible(representative))
                {
                    species.Genomes.Add(genome);
                    genome.SpeciesId = species.Id;

                    newSpecies = false;

                    break;
                }
            }
            ;

            if (newSpecies)
            {
                ISpecies species = new Species();

                species.Genomes.Add(genome);
                genome.SpeciesId = species.Id;

                Species.Add(species);
            }
        }
示例#3
0
        /// <summary>
        /// Separates the current organisms into the correct species. Makes new species and deletes extinct species as needed. See <see cref="NEAT.NEATClient.Evolve"/> before using!
        /// </summary>
        public void Speciate()
        {
            foreach (Organism organism in Organisms)
            {
                bool found_species = false;

                foreach (Species species in Species)
                {
                    Organism random_organism = species.GetRandomOrganism(Pedigree.Random, organism);


                    if (random_organism == null)    //The organism we're at is the only organism in the species we're at. See if we should leave it in this species.
                    {
                        bool kill_species = false;

                        IEnumerable <Species> all_other_species = Species.Where(x => x != species);

                        foreach (Species other_species in all_other_species)
                        {
                            Organism other_random_organism = other_species.GetRandomOrganism(Pedigree.Random);  //No need to exclude the organism because it can't be here.

                            if (organism.Genome.Distance(other_random_organism.Genome) < CompatibilityDistance) //The organism can fit in another species, put it there.
                            {
                                organism.Species.RemoveOrganism(organism);

                                other_species.AddOrganism(organism);


                                kill_species = true;
                                break;
                            }
                        }


                        if (kill_species)   //This species no longer has any organisms, it is now extinct.
                        {
                            Species.Remove(species);
                        }


                        found_species = true;
                        break;
                    }


                    if (organism.Genome.Distance(random_organism.Genome) < CompatibilityDistance)
                    {
                        if (organism.Species != random_organism.Species)    //If the species to set is actually different.
                        {
                            Species original_species = organism.Species;


                            original_species?.RemoveOrganism(organism);

                            species.AddOrganism(organism);


                            if (original_species?.Size == 0) //This species just went extinct.
                            {
                                Species.Remove(original_species);
                            }
                        }

                        found_species = true;
                        break;
                    }
                }

                if (!found_species)
                {
                    Species original_species = organism.Species;


                    original_species?.RemoveOrganism(organism);


                    Species new_species = new Species();

                    Species.Add(new_species);

                    new_species.AddOrganism(organism);


                    if (original_species?.Size == 0) //This species just went extinct.
                    {
                        Species.Remove(original_species);
                    }
                }
            }
        }