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))
Exemplo n.º 2
0
class SimulationWorker(QtCore.QThread):
    TASK_CONFIRMED_FINISHED_SIGNAL = 'TASK_CONFIRMED_FINISHED_SIGNAL'
    ALL_TASKS_FINISHED_SIGNAL = 'ALL_TASKS_FINISHED_SIGNAL'

    def __init__(self, runner):
        super().__init__(runner.widget)
        self.runner = runner
        self.is_running = False
        self.simulation_executor = SimulationExecutor()
        self.current_data = RunnerGuiModel()
        self.root_dir = self.runner.scheduler.ui.outputDirectorylineEdit.text()

    def run(self):
        for i, task in enumerate(self.runner.scheduler.tasks):
            run_func, configuration, population_printer = self._setup_task(i, task)
            result = self._run_task(run_func, configuration)

            collected = False
            while not collected:
                try:
                    self._collect_task(result, i, configuration, population_printer)
                except PermissionError:
                    collected = False
                    self.current_data.current_phase = SimulationPhases.PERMISSION_ERROR
                else:
                    collected = True
                    self.current_data.current_phase = SimulationPhases.COLLECTING

        self.current_data.current_phase = SimulationPhases.DONE
        self.emit(QtCore.SIGNAL(self.ALL_TASKS_FINISHED_SIGNAL))

    def _setup_task(self, task_no, task):
        new_data = RunnerGuiModel()
        new_data.tasks_progress = task_no
        new_data.current_input = task.data_configuration
        new_data.current_config = task.params_configuration
        new_data.current_phase = SimulationPhases.SETUP
        self.current_data = new_data

        run_func, configuration, pop_printer = self.simulation_executor.prepare_simulation(
            self.runner, task_no, new_data.current_input, new_data.current_config,
            population_path=task.population_configuration)

        run_post_mortem_data = []
        for _ in range(configuration.max_algorithm_runs):
            task_progress_model = RunPostMortemModel()
            task_progress_model.max_steps = configuration.max_algorithm_steps
            task_progress_model.task_no = task_no
            run_post_mortem_data.append(task_progress_model)

        self.runner.run_progress_data = run_post_mortem_data

        return run_func, configuration, pop_printer

    def _run_task(self, run_func, configuration):
        self.current_data.current_phase = SimulationPhases.LEARNING
        return run_func(configuration)

    def _collect_task(self, result, task_id, configuration, population_printer):
        self.current_data.current_phase = SimulationPhases.COLLECTING
        run_estimator, ngen, grammar_estimator, population, generalisation_data = result

        path = self._prepare_artifact_dir(task_id)

        input_data_name = os.path.basename(self.current_data.current_input)
        config_data_name = os.path.basename(self.current_data.current_config)

        shutil.copy(self.current_data.current_input, os.path.join(path, input_data_name))
        shutil.copy(self.current_data.current_config, os.path.join(path, config_data_name))

        self.simulation_executor.save_population(
            population, population_printer, path, 'final_population'
        )
        self.simulation_executor.save_grammar_estimator(
            grammar_estimator, path, 'grammar_estimator'
        )
        self.simulation_executor.save_execution_summary(
            run_estimator, ngen, generalisation_data, path, 'run_summary'
        )
        self.simulation_executor.generate_grammar_estimation_diagrams(
            grammar_estimator, path, configuration
        )

    def _prepare_artifact_dir(self, task_id):
        path = os.path.join(self.root_dir, 'task_{0}'.format(task_id))
        if os.path.exists(path):
            rmdir_forced(path)

        os.mkdir(path)
        return path