コード例 #1
0
class TestSimulationExecutor(unittest.TestCase):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        logging.basicConfig(level=logging.INFO,
                            filename=r"C:\Users\Michał\PycharmProjects\mgr\sgcs\log.log",
                            format='%(asctime)s %(message)s')

        self.sut = SimulationExecutor()
        self.starting_symbol = Symbol(1)

    def test_population_saving_and_loading(self):
        # Given:
        path = r"C:\Users\Michał\PycharmProjects\mgr\sgcs\sgcs\data\experimental"
        name = 'tmp'
        terminal_rule = Rule(Symbol(101), Symbol(-101))
        non_terminal_rule = Rule(Symbol(101), Symbol(101), Symbol(101))

        population = StochasticRulePopulation(self.starting_symbol)
        population.add_rule(terminal_rule, self.sut.randomizer)
        population.add_rule(non_terminal_rule, self.sut.randomizer)

        # When:
        self.sut.save_population(population, lambda _: '', path, name)
        loaded_pop = self.sut.load_population(path, name, starting_symbol=self.starting_symbol)

        # Then:
        assert_that(loaded_pop.get_all_non_terminal_rules(), only_contains(non_terminal_rule))
        assert_that(loaded_pop.get_terminal_rules(), only_contains(terminal_rule))
コード例 #2
0
class LoadPopulationWorker(QtCore.QThread):
    TRANSLATOR_READY_SIGNAL = 'TRANSLATOR_READY_SIGNAL'
    POPULATION_LOADED_SIGNAL = 'POPULATION_LOADED_SIGNAL'
    STARTING_SYMBOL = Symbol(1)

    def __init__(self, population_editor):
        super().__init__(population_editor.widget)
        self.population_editor = population_editor
        self.translator = None
        self.population_executor = PopulationExecutor()
        self.simulation_executor = SimulationExecutor()
        self.operation = None
        self.randomizer = Randomizer(Random())

    def run(self):
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.CHANGE_STEP_EVENT), 'Operation in progress...')
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.SET_PROGRESS_EVENT), -1)
        if self.operation is not None:
            # noinspection PyCallingNonCallable
            self.operation()
        self.emit(QtCore.SIGNAL(AsyncProgressDialog.RESET_EVENT))

    def load_learning_set_operation(self):
        try:
            self.translator = self.population_executor.get_learned_translator(
                self.population_editor.population_path)
        except:
            dialog = QtGui.QMessageBox()
            dialog.critical(
                self.population_editor.widget, 'Invalid file',
                'File "{0}" has invalid format!'.format(self.population_editor.population_path))
        finally:
            self.emit(QtCore.SIGNAL(self.TRANSLATOR_READY_SIGNAL))

    def load_population_operation(self):
        name = os.path.basename(self.population_editor.population_path).split('.')[0]
        dir_path = os.path.dirname(self.population_editor.population_path)
        rule_population = self.simulation_executor.load_population(
            dir_path, name, starting_symbol=self.STARTING_SYMBOL)

        self.population_editor.rules.clear()

        for rule in rule_population.get_terminal_rules():
            translated = rule.human_friendly_representation(rule_population.symbol_shift(),
                                                            rule_population.starting_symbol,
                                                            rule_population.universal_symbol)
            rule_model = TerminalRuleModel()
            rule_model.parent = translated[0]
            rule_model.left_child = self.translator.symbol_to_word(rule.left_child)
            self.population_editor.rules.append(rule_model)

        for rule in rule_population.get_all_non_terminal_rules():
            translated = rule.human_friendly_representation(rule_population.symbol_shift(),
                                                            rule_population.starting_symbol,
                                                            rule_population.universal_symbol)
            rule_model = NonTerminalRuleModel()
            rule_model.parent, rule_model.left_child, rule_model.right_child = translated
            self.population_editor.rules.append(rule_model)

        self.emit(QtCore.SIGNAL(self.POPULATION_LOADED_SIGNAL))

    def save_population_operation(self):
        population = RulePopulation(self.STARTING_SYMBOL)
        for rule_model in self.population_editor.rules:
            if rule_model.is_terminal:
                parent, terminal_word = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )
                terminal_symbol = self.translator.word_to_symbol(terminal_word)

                population.add_rule(Rule(parent, terminal_symbol), self.randomizer)
            else:
                parent, left, right = Rule.from_human_friendly_representation(
                    population.symbol_shift(),
                    population.starting_symbol,
                    population.universal_symbol,
                    rule_model.parent,
                    rule_model.left_child,
                    rule_model.right_child
                )

                population.add_rule(Rule(parent, left, right), self.randomizer)

        name = os.path.basename(self.population_editor.population_path).split('.')[0]
        path = os.path.dirname(self.population_editor.population_path)
        self.simulation_executor.save_population_data(population, path, name)