Ejemplo n.º 1
0
    def test_reproduce_differently_fit_genomes_with_overlapping_connections(self):
        genome1 = Genome([[1, 3, 0, True], [2, 4, 0, True], [1, 4, 1, True]], 2, 2)
        # genome2 becomes independent copy of genome 1 (same invocation numbers)
        genome2 = Genome._reproduce_equal_genomes(genome1, genome1)
        # add different connection
        genome2._create_connection_genes([[2, 3, 0, True]])
        # change genome 2 (1,4) connection weight
        genome2.connection_genes[(1, 4)].weight = 5
        genome1.fitness = 2.0
        genome2.fitness = 1.0
        child = Genome.reproduce(genome1, genome2)
        self.assertEqual([(1, 3), (1, 4), (2, 4)], child.get_connections_ids())

        overlapping_connection = child.connection_genes[(1, 4)]
        weight = overlapping_connection.weight
        self.assertTrue(weight == 1 or weight == 5)

        genome1.fitness = 1.0
        genome2.fitness = 2.0
        child = Genome.reproduce(genome1, genome2)
        self.assertEqual([(1, 3), (1, 4), (2, 3), (2, 4)], child.get_connections_ids())

        overlapping_connection = child.connection_genes[(1, 4)]
        weight = overlapping_connection.weight
        self.assertTrue(weight == 1 or weight == 5)
Ejemplo n.º 2
0
    def test_reproduce_differently_fit_genomes(self):
        genome1 = Genome([[1, 3, 0, True], [2, 4, 0, True]], 2, 2)
        genome2 = Genome([[1, 4, 0, True], [2, 3, 0, True]], 2, 2)
        genome1.fitness = 2.0
        genome2.fitness = 1.0
        child = Genome.reproduce(genome1, genome2)
        self.assertEqual([(1, 3), (2, 4)], child.get_connections_ids())

        genome1.fitness = 1.0
        genome2.fitness = 2.0
        child = Genome.reproduce(genome1, genome2)
        self.assertEqual([(1, 4), (2, 3)], child.get_connections_ids())
Ejemplo n.º 3
0
    def get_offsprings_from_group(self, group_key, group_offspring_amount,
                                  left_genomes):
        group_to_reproduce = self.groups[group_key]
        parents = group_to_reproduce.get_parents(self.r_factor)
        offsprings = []

        while (len(offsprings) != group_offspring_amount):
            first_parent = random.choice(parents)
            second_parent = random.choice(parents)
            # Make a child
            offspring = Genome.reproduce(first_parent, second_parent)
            # Mutate it
            offspring.mutate(self.mutation_coefficients)
            # Now try to fit it into this group
            if self._is_group_fitting_for_offspring(
                    group_to_reproduce.get_representative(), offspring):
                offsprings.append(offspring)
            else:
                left_genomes.append(offspring)

        return offsprings