def test_simulated_annealing_runtime(self): import simulations.turkish_vowel_harmony as current_simulation configurations.load_configurations_from_dict( current_simulation.configurations_dict) self.initialise_segment_table('turkish_segment_table.txt') initial_hmm = None initial_rule_set = None initial_hypothesis = Hypothesis.create_initial_hypothesis( current_simulation.data, initial_hmm, initial_rule_set) target_tuple = current_simulation.target_tuple data = current_simulation.data target_rule_set = RuleSet.load_from_flat_list(target_tuple[1]) target_hypothesis = Hypothesis.create_hypothesis( HMM(target_tuple[0]), target_rule_set, data) target_energy = target_hypothesis.get_energy() simulated_annealing = SimulatedAnnealing(initial_hypothesis, target_energy) simulated_annealing.before_loop() # mutate hypothesis for some time before measuring steps for i in range(500): simulated_annealing.make_step() @timeit_best_of_N def make_step_profiled(): simulated_annealing.make_step() make_step_profiled()
def get_random_hypothesis_by_mutations(cls, data, fixed_hmm=None, fixed_rules=None): if configurations["EVOLVE_RULES"]: initial_rule_set = RuleSet() elif fixed_rules: initial_rule_set = RuleSet.load_from_flat_list(fixed_rules) else: initial_rule_set = RuleSet() if configurations["EVOLVE_HMM"]: initial_hmm = None elif fixed_hmm: initial_hmm = HMM(deepcopy(fixed_hmm)) else: initial_hmm = None hypothesis = Hypothesis.create_initial_hypothesis(data, initial_hmm=initial_hmm, initial_rule_set=initial_rule_set) for _ in range(ga_config.RANDOM_INIT_WARMUP_STEPS): new_hypothesis = deepcopy(hypothesis) success = new_hypothesis.grammar.make_mutation() current_energy = hypothesis.get_energy() new_energy = new_hypothesis.get_energy() if success and not isinf(new_energy): if new_energy < current_energy or random.random() < ga_config.ACCEPT_WORSE_PROBAB: hypothesis = new_hypothesis return hypothesis
def get_random_hypothesis_randomized(cls, simulation, data, initial_hmm=None, initial_rules=None): if initial_rules: rule_set = RuleSet.load_from_flat_list(initial_rules) elif not configurations['EVOLVE_RULES']: rule_set = RuleSet.load_from_flat_list(deepcopy(simulation.target_tuple[1])) else: rule_set = RuleSet.get_random_rule_set() if initial_hmm: hmm = HMM(deepcopy(initial_hmm)) elif not configurations['EVOLVE_HMM']: hmm = HMM(deepcopy(simulation.target_tuple[0])) else: hmm = HMM.get_random_hmm(data) grammar = Grammar(hmm, rule_set) return Hypothesis(grammar)
def init_target_hypothesis(self): target_tuple = self.simulation.target_tuple target_rule_set = RuleSet.load_from_flat_list(target_tuple[1]) target_hypothesis = Hypothesis.create_hypothesis( HMM(deepcopy(target_tuple[0])), target_rule_set) target_energy = target_hypothesis.get_energy() if self.generation == 0: self.logger.info('Target hypothesis:') log_hypothesis(target_hypothesis, self.logger.info) self.logger.info('Target energy: {}'.format(target_energy)) self.logger.info('Target hypothesis energy signature: {}'.format( target_hypothesis.get_recent_energy_signature())) return target_hypothesis, target_energy
def get_target_hypo(self): target_hmm = deepcopy(self.simulation.target_hmm) target_rule_set = RuleSet.load_from_flat_list(self.simulation.target_tuple[1]) return Hypothesis(Grammar(target_hmm, target_rule_set))