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 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 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 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 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