Пример #1
0
def test_reproduce():
    conf = Conf()

    innovs = Innovations()

    genome = Genome.minimal_fully_connected(0, (2, 3))

    species = Species()

    ranks = random.sample(xrange(20), 10)

    for x in xrange(10):
        new_genome = genome.duplicate(innovs.next_genome())

        org = Organism(new_genome)

        org.rank = ranks[x]

        species.organisms.append(org)

    species.offspring = 10

    children = species.epoch(conf, innovs)

    assert len(children) == 10
Пример #2
0
def test_mutate_neuron():
    genome = Genome.minimal_fully_connected(0, (3, 2))

    innovations = Innovations()

    innovations.innov = max(map(lambda x: x.innov, genome.genes))

    genome.mutate_neuron(innovations)

    assert len(genome.genes) == 8
    assert sum(genome.neurons) == 6
    assert len(innovations.neuron_innov) == 1
Пример #3
0
def test_compatible_diff_struct():
    conf = Conf()
    innovations = Innovations()

    g1 = Genome.minimal_fully_connected(0, (3, 2))

    innovations.innov = max(map(lambda x: x.innov, g1.genes))

    g2 = g1.duplicate(1)

    g2.mutate_neuron(innovations)

    assert not g1.compatible(conf, g2)
Пример #4
0
def test_crossover_unequal_dad_mut_mom_fit():
    innovs = Innovations()

    mom = Genome.minimal_fully_connected(0, (3, 2))

    innovs.innov = max(map(lambda x: x.innov, mom.genes))

    dad = mom.duplicate(1)

    dad.mutate_neuron(innovs)

    baby = mom.crossover(dad, 2.0, 1.0, innovs)

    assert len(baby.genes) == 6
Пример #5
0
def test_mutate_gene_none_free():
    genome = Genome.minimal_fully_connected(0, (3, 2))

    innovations = Innovations()

    innovations.innov = max(map(lambda x: x.innov, genome.genes))

    old = random.choice

    random.choice = mock.MagicMock(side_effect=[0, Genome.MAX_HIDDEN])

    genome.mutate_gene(innovations)

    random.choice = old

    assert len(genome.genes) == 6
    assert len(innovations.gene_innov) == 0
Пример #6
0
def test_mutate_gene():
    genome = Genome.minimal_fully_connected(0, (3, 2))

    genome.neurons[1] += 1

    innovations = Innovations()

    innovations.innov = max(map(lambda x: x.innov, genome.genes))

    old = random.choice

    random.choice = mock.MagicMock(side_effect=[0, 3])

    genome.mutate_gene(innovations)

    random.choice = old

    assert len(genome.genes) == 7
    assert len(innovations.gene_innov) == 1