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