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
 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()
예제 #3
0
 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 __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 __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.runner_dummy = RunnerDummy()
        self.task_no = 0
        self.config_path = \
            r"C:\Users\Michał\PycharmProjects\mgr\sgcs\sgcs\data\experimental\unold0.parconf"
class LongTestSimulationExecutor(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.runner_dummy = RunnerDummy()
        self.task_no = 0
        self.config_path = \
            r"C:\Users\Michał\PycharmProjects\mgr\sgcs\sgcs\data\experimental\unold0.parconf"

    def test_tomita_1(self):
        input_data = r"C:\Users\Michał\PycharmProjects\mgr\sgcs\sgcs\data\experimental\t1.inconf"
        run_func, conf = self.sut.prepare_simulation(
            self.runner_dummy, self.task_no, input_data, self.config_path)
        run_func(conf)
예제 #7
0
 def load_config_action(self):
     selected_filename = QtGui.QFileDialog.getOpenFileName(
         self.widget, 'Load config...', self.last_directory, self.DATA_CONFIG_EXT)
     if selected_filename:
         self.selected_learning_filename, self.selected_testing_filename = \
             SimulationExecutor.load_input_config(selected_filename)
예제 #8
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)
예제 #9
0
def main():
    logging.basicConfig(level=logging.INFO, format="%(asctime)s %(message)s")
    usage = "usage: %prog -o -OUTPUT_DIR [options] [input, config [,starting_rules]]..."
    parser = OptionParser(usage=usage)
    parser.add_option(
        "-p",
        "--print_only",
        action="store_false",
        dest="run",
        default=True,
        help="Only shows configuration, does not run it",
    )
    parser.add_option("-o", "--output", dest="output", default=None, help="Destination")
    parser.add_option(
        "-s",
        "--starting_rules",
        action="store_true",
        dest="starting_rules",
        default=False,
        help="Script expects triples of data(input, config, starting_rules) "
        + "instead of doubles (input, config), if used.",
    )
    options, args = parser.parse_args()

    if options.output is None:
        print("No output dir specified!")
        return

    print("artifact directory:", options.output)

    chunk_size = 3 if options.starting_rules else 2

    if not args or len(args) % chunk_size != 0:
        print("Invalid number of arguments!")
        return

    i = 0
    tasks = []
    for input_file, config_file, *rest in chunk(args, chunk_size):
        print("Task", i)
        task = TaskModel()

        print("\tinput:", input_file)
        task.data_configuration = input_file

        print("\tconfig:", config_file)
        task.params_configuration = config_file

        starting_population = None
        if rest:
            starting_population = rest[0]
            print("\tstarting population:", starting_population)

        task.population_configuration = starting_population

        tasks.append(task)

    if options.run:
        print("Starting run")
        executor = SimulationExecutor()
        runner = Runner()
        for i, x in enumerate(tasks):
            run_func, configuration, population_printer = executor.prepare_simulation(
                runner, i, x.data_configuration, x.params_configuration, x.population_configuration
            )

            result = run_func(configuration)

            collected = False
            while not collected:
                try:
                    _collect_task(x, result, i, configuration, population_printer, executor, options.output)
                except PermissionError:
                    collected = False
                    print("not collected!")
                else:
                    collected = True
예제 #10
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