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)
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
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
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
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')
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')
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)
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}')
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))
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}')
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