Exemplo n.º 1
0
    def speciate(self):
        curgenome: Genome  # For stepping through Population
        curspecies: Species  # Steps through species
        comgenome: Genome  # Organism for comparison
        newspecies: Species  # For adding a new species

        counter: int = 0  # Species counter

        # Step through all existing organisms
        for curgenome in self.genomes:
            # search a species for each genome
            if len(self.species) == 0:
                # Create the first species
                counter += 1
                newspecies = Species().new_species(counter)
                self.species.append(newspecies)
                newspecies.add_genome(curgenome)  # add current genome
                curgenome.species = newspecies
            else:
                for curspecies in self.species:
                    if len(curspecies.genomes) > 0:
                        for comgenome in curspecies.genomes:
                            if curgenome.compatibility(
                                    comgenome) < Neat.compat_thresh:
                                # Found compatible species, so add this organism to it
                                curspecies.add_genome(curgenome)
                                curgenome.species = curspecies
                                comgenome = 0  # mark search over
                                break
                            else:
                                # Keep searching for a matching species
                                break

                        if comgenome == 0:
                            break
                # If we didn't find a match, create a new species
                if comgenome != 0:
                    counter += 1
                    newspecies = Species().new_species(counter)
                    self.species.append(newspecies)
                    newspecies.add_genome(curgenome)
                    curgenome.species = newspecies

        self.last_species = counter  # Keep track of highest species
        return True