示例#1
0
def _weights_differences_avg(genome_1: Genome, genome_2: Genome) -> float:
    diff = 0

    for con_1 in genome_1.connections():
        for con_2 in genome_2.connections():
            if con_1 == con_2:
                diff += np.abs(con_1.weight() - con_2.weight())
    return diff
示例#2
0
def construct(genome: Genome, file_name):
    dot = Digraph()
    for node in genome.nodes():
        dot.node(str(node.id()), str(node.id()))
    for con in genome.connections():
        if con.status() == Status.ENABLED:
            dot.edge(str(con.input_node().id()), str(con.output_node().id()))
    dot.render(f'/Users/max/IdeaProjects/neat/network_pictures/{file_name}',
               view=True)
示例#3
0
def _excesses_disjoints(genome_1: Genome, genome_2: Genome):
    E = 0
    D = 0
    last_disjoint = _pick_last_disjoint_connection(genome_1, genome_2)

    for con in genome_1.connections() + genome_2.connections():
        if con not in genome_1.connections() or con not in genome_2.connections():
            if con.innovation_number() <= last_disjoint:
                D += 1
            else:
                E += 1
    return E, D
示例#4
0
    def test_init_1(self):
        print("\n\ntest_init_1.1")
        generation = Generation()
        genome = Genome(generation=generation, input_nodes=2, output_nodes=2)
        for con in genome.connections():
            print(con)

        for node in genome.nodes():
            print(node)

        print('-' * 40)

        for node in generation.nodes():
            print(node)

        print('-' * 60)
        viz.construct(genome, 'first')
        print("\n\ntest_init_1.2")
        genome_1 = Genome(generation, 2, 2)
        for con in genome_1.connections():
            print(con)

        for node in genome_1.nodes():
            print(node)

        print('-' * 40)

        for node in generation.nodes():
            print(node)
        viz.construct(genome_1, 'second')

        print('-' * 20 + 'Mutations')
        for mutation in generation.mutations():
            print(mutation)
示例#5
0
    def test_add_connection(self):
        print('\n\ntest_add_connection\nBEFORE 0')
        generation = Generation()
        genome = Genome(generation, 2, 1)
        for con in genome.connections():
            print(con)
        viz.construct(genome, 'before_0')
        genome._add_node()
        genome._add_connection()
        genome._add_connection()
        genome._add_connection()

        print('\nAFTER 0\n')
        for con in genome.connections():
            print(con)
        viz.construct(genome, 'after_0')
示例#6
0
 def spawn(self, first=True, input_neurons=0, output_neurons=1):
     if first:
         for i in range(population_size):
             # Spawn new Genome with number of input/output neurons
             genome = Genome(self, input_neurons, output_neurons)
             self.add_organism(genome)
     else:
         pass
示例#7
0
    def test_excesses_and_disjoints(self):

        generation = Generation()
        genome_1 = Genome(generation, 1, 2)
        genome_2 = genome_1.copy_genome()
        viz.construct(genome_1, 'First Before')
        genome_1._add_node()
        genome_1._add_connection()
        for con in genome_1.connections():
            print(con)
        viz.construct(genome_1, 'First')
        genome_2._add_node()
        viz.construct(genome_2, 'Second')
        E, D = _excesses_disjoints(genome_1, genome_2)
        print('-' * 20)
        for con in genome_2.connections():
            print(con)
        print(f'Excesses: {E}')
        print(f'Disjoints: {D}')
示例#8
0
    def test_predict(self):
        generation = Generation()
        genome = Genome(generation=generation, input_nodes=2, output_nodes=2)
        nn = NeuralNetwork(genome=genome)
        construct(nn.genome(), 'Before')
        nn.add_node()
        construct(nn.genome(), '1 Node Added')
        nn.add_node()
        construct(nn.genome(), '2 Nodes Added')
        nn.add_connection()
        construct(nn.genome(), 'Connection Added')
        print("CONNECTIONS")
        for con in nn.genome().connections():
            print(con)
        print("CONNECTIONS")

        for pred in nn.predict([2, 3]):
            print(pred)
示例#9
0
    def test_init_2(self):

        generation = Generation()
        genome = Genome(generation=generation, input_nodes=1, output_nodes=1)
        genome._add_node()

        new_generation = Generation(_copy=True, prev_generation=generation)
        genome_copied = Genome(generation=new_generation,
                               _copy=True,
                               genome_to_copy=genome)

        for node in generation.nodes():
            print(node)
        print('-' * 20)

        viz.construct(genome, 'first_1')
        genome_copied._add_node()

        for node in new_generation.nodes():
            print(node)
        viz.construct(genome_copied, 'first_copied_1')
示例#10
0
    def test_sigma(self):

        generation = Generation()

        genome_1 = Genome(generation, 1, 1)
        genome_2 = genome_1.copy_genome()

        # genome_1._add_node()
        genome_1._mutate_weights()
        genome_2._mutate_weights()

        for con in genome_1.connections():
            print(con)

        print('-' * 20)
        for con in genome_2.connections():
            print(con)
        print(sigma(genome_1, genome_2))
示例#11
0
    def test_offspring(self):

        generation = Generation()
        genome_1 = Genome(generation, 2, 2)
        genome_2 = Genome(generation, 2, 2)
        # for i in range(2):
        #     genome_1._add_connection()
        #     genome_2._add_connection()
        # for i in range(1):
        #     genome_1._add_node()
        #     genome_2._add_node()

        viz.construct(genome_1, 'Genome 1')
        viz.construct(genome_2, 'Genome 2')

        for con in genome_1.connections():
            print(con)
        print('-' * 40)
        for con in genome_2.connections():
            print(con)

        genome = produce_offspring(generation, genome_1, genome_2)
        print('-' * 40)
        for node in genome.nodes():
            print(node)

        print('\n' + '-' * 40)

        for con in genome.connections():
            print(con)

        viz.construct(genome, 'offspring')

        E, D = _excesses_disjoints(genome_1, genome_2)
        print('-' * 20)
        print(f'Excesses: {E}')
        print(f'Disjoints: {D}')
示例#12
0
 def spawn(self):
     for i in range(population_size):
         genome = Genome(self, number_input_neurons, number_output_neurons)
         nn = NeuralNetwork(genome)
         self.add_organism(nn)
示例#13
0
def produce_offspring(generation: Generation, genome_1: Genome, genome_2: Genome) -> Genome:

    offspring = Genome(generation, genome_1.input_nodes(), genome_1.output_nodes(), connections=False)

    for node in genome_1.nodes() + genome_2.nodes():
        if node not in offspring.nodes():
            offspring.nodes().append(node.copy_without_connections())

    for con in genome_1.connections() + genome_2.connections():
        if (con in offspring.connections()) and (con.status() == Status.DISABLED):
            for i, in_con in enumerate(offspring.connections()):
                if in_con == con and in_con.status() == Status.ENABLED:
                    if _disable_chance():
                        in_con.disable()
                    break
        elif con not in offspring.connections():
            if (con.status() == Status.DISABLED) and (not _disable_chance()):
                con.enable()
            offspring.connections().append(con)


    return offspring
示例#14
0
    def test_add_node(self):

        print('-' * 20 + 'test_add_node\nBEFORE 0')
        generation = Generation()
        genome = Genome(generation, 2, 1)

        for con in genome.connections():
            print(con)

        for node in genome.nodes():
            print(node)

        viz.construct(genome, 'before_0')

        genome._add_node()
        genome._add_node()
        genome._add_node()
        for node in generation.nodes():
            print(node)
        print('\nAFTER 0 \n')
        for con in genome.connections():
            print(con)

        print('-' * 40)
        for node in genome.nodes():
            print(node)
        viz.construct(genome, 'after_0')

        print('\n\ntest_add_node\nBEFORE 1')

        genome_1 = Genome(generation, 1, 1)
        viz.construct(genome_1, 'before_1')
        for con in genome_1.connections():
            print(con)

        for node in genome_1.nodes():
            print(node)

        genome_1._add_node()
        genome_1._add_node()
        genome_1._add_node()
        genome_1._add_node()

        print('\nAFTER 0 \n')
        for con in genome_1.connections():
            print(con)

        for node in genome_1.nodes():
            print(node)
        viz.construct(genome_1, 'after_1')
示例#15
0
def produce_offspring(generation, genome_1: Genome,
                      genome_2: Genome) -> Genome:
    # TODO: exclude excess genes of parent 2 if parent 1 is fitter
    offspring = Genome(generation,
                       genome_1.input_nodes(),
                       genome_1.output_nodes(),
                       connections=False)

    for node in genome_1.nodes() + genome_2.nodes():
        if node not in offspring.nodes():
            offspring.nodes().append(node.copy_without_connections())

    # all common genes are appended randomly from either parent
    for con in genome_1.connections():
        if con in genome_2.connections():
            index = genome_2.connections().index(con)
            gene = _pick_random_gene(con, genome_2.connections()[index])
            _append_con(gene, offspring)

    for con in genome_1.connections() + genome_2.connections():
        if (con in offspring.connections()) and (con.status()
                                                 == Status.DISABLED):
            for in_con in offspring.connections():
                if in_con == con and in_con.status() == Status.ENABLED:
                    if _disable_chance():
                        in_con.disable()
                    break
        elif con not in offspring.connections():
            if (con.status() == Status.DISABLED) and (not _disable_chance()):
                con.enable()

            _append_con(con, offspring)

    return offspring