Example #1
0
    def produce(self,
                parents: Sequence[Genome],
                spawner: GeneSpawner = None) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        new_genome = Genome()
        for atom in parents[0]:
            if isinstance(
                    atom, Literal
            ) and self.push_type == atom.push_type and random() < self.rate:
                new_atom = self._mutate_literal(atom)
            else:
                new_atom = atom
            new_genome = new_genome.append(new_atom)
        return new_genome
Example #2
0
    def produce(self, parents: Sequence[Genome],
                spawner: GeneSpawner) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        self.checknum_parents(parents)
        new_genome = Genome()
        for gene in parents[0]:
            if random() < self.rate:
                new_genome.append(spawner.spawn_atom())
            new_genome.append(gene)
        return new_genome
Example #3
0
    def produce(self,
                parents: Sequence[Genome],
                spawner: GeneSpawner = None) -> Genome:
        """Produce a child Genome from parent Genomes and optional GenomeSpawner.

        Parameters
        ----------
        parents
            A list of parent Genomes given to the operator.
        spawner
            A GeneSpawner that can be used to produce new genes (aka Atoms).

        """
        super().produce(parents, spawner)
        self.checknum_parents(parents)
        gn1 = parents[0]
        gn2 = parents[1]
        new_genome = Genome()
        # Random pick which parent to start from
        use_parent_1 = choice([True, False])
        loop_times = len(gn1)
        if not use_parent_1:
            loop_times = len(gn2)
        i = 0
        while i < loop_times:
            if random() < self.rate:
                # Switch which parent we are pulling genes from
                i += round(self.alignment_deviation * _gaussian_noise_factor())
                i = int(max(0, i))
                use_parent_1 = not use_parent_1
            else:
                # Pull gene from parent
                if use_parent_1:
                    new_genome = new_genome.append(gn1[i])
                else:
                    new_genome = new_genome.append(gn2[i])
                i = int(i + 1)
            # Change loop stop condition
            loop_times = len(gn1)
            if not use_parent_1:
                loop_times = len(gn2)
        return new_genome