def crossover(self, h1, h2): if GeneticAlgorithm._is_incest(h1, h2): return h1, h2, crossover_rules = False crossover_hmm = False if ga_config.CROSSOVER_BOTH_HMM_AND_RULES: if configurations["EVOLVE_RULES"]: crossover_rules = True if configurations["EVOLVE_HMM"]: crossover_hmm = True else: loci = [] if configurations["EVOLVE_RULES"]: loci.append('rules') if configurations["EVOLVE_HMM"]: loci.append('hmm') locus = random.choice(loci) if locus == 'rules': crossover_rules = True elif locus == 'hmm': crossover_hmm = True offspring_1 = deepcopy(h1) offspring_2 = deepcopy(h2) if crossover_rules: offspring_1_rule_set, offspring_2_rule_set = RuleSet.crossover( offspring_1.grammar.rule_set, offspring_2.grammar.rule_set) else: offspring_1_rule_set, offspring_2_rule_set = offspring_1.grammar.rule_set, offspring_2.grammar.rule_set if crossover_hmm: offspring_1_hmm, offspring_2_hmm = HMM.crossover( offspring_1.grammar.hmm, offspring_2.grammar.hmm) else: offspring_1_hmm, offspring_2_hmm = offspring_1.grammar.hmm, offspring_2.grammar.hmm offspring_1.grammar = Grammar(offspring_1_hmm, offspring_1_rule_set) offspring_2.grammar = Grammar(offspring_2_hmm, offspring_2_rule_set) # Invalidate mutated offspring fitness value (i.e. mark for re-evaluation) offspring_1.invalidate_fitness() offspring_1.invalidate_energy() offspring_2.invalidate_fitness() offspring_2.invalidate_energy() return offspring_1, offspring_2,
def test_crossover_connected_components(self): self.initialise_segment_table("plural_english_segment_table.txt") hmm_1 = HMM({INITIAL_STATE: ['q1'], 'q1': (['q2'], ['dogo', 'koko']), 'q2': (['q1', FINAL_STATE], ['z'])}) hmm_2 = HMM({INITIAL_STATE: ['q1'], 'q1': (['q2'], ['dag', 'kat']), 'q2': (['q3', FINAL_STATE], ['k']), 'q3': (['q1'], ['z'])}) offspring_1, offspring_2 = HMM.crossover(hmm_1, hmm_2) self.write_to_dot_to_file(hmm_1, 'component_parent_1') self.write_to_dot_to_file(hmm_2, 'component_parent_2') self.write_to_dot_to_file(offspring_1, 'component_offspring_1') self.write_to_dot_to_file(offspring_2, 'component_offspring_2') offspring_1.get_transducer() offspring_2.get_transducer()
def test_crossover(self): hmm_1 = HMM({INITIAL_STATE: ['q1'], 'q1': (['q2', FINAL_STATE], ['koko', 'dogo']), 'q2': ([FINAL_STATE], ['zz'])}) hmm_2 = HMM({INITIAL_STATE: ['q1'], 'q1': (['q2'], ['dog', 'kat']), 'q2': (['q3'], ['s']), 'q3': ([FINAL_STATE], ['z'])}) print("Parent 1") log_hmm(hmm_1) print() print("Parent 2") log_hmm(hmm_2) offspring_1, offspring_2 = HMM.crossover(hmm_1, hmm_2) print() print("Offspring 1") log_hmm(offspring_1) print() print("Offspring 2") log_hmm(offspring_2)
def test_random_hmms_crossover(self): self.initialise_segment_table("plural_english_segment_table.txt") for _ in range(1): hmm_1 = HMM.get_random_hmm(data=['dag', 'zook', 'kook', 'kooz']) hmm_2 = HMM.get_random_hmm(data=['dag', 'zook', 'kook', 'kooz']) print('HMM 1') log_hmm(hmm_1) print('HMM 2') log_hmm(hmm_2) offspring_1, offspring_2 = HMM.crossover(hmm_1, hmm_2) print('Offspring 1') print('HMM', offspring_1.inner_states, 'TRANSITIONS', offspring_1.transitions, 'EMISSIONS', offspring_1.emissions) print('Offspring 2') print('HMM', offspring_2.inner_states, 'TRANSITIONS', offspring_2.transitions, 'EMISSIONS', offspring_2.emissions) self.write_to_dot_to_file(hmm_1, 'random_parent_1') self.write_to_dot_to_file(hmm_2, 'random_parent_2') self.write_to_dot_to_file(offspring_1, 'random_offspring_1') self.write_to_dot_to_file(offspring_2, 'random_offspring_2') offspring_1.get_transducer() offspring_2.get_transducer()