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)
Exemplo n.º 2
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
Exemplo n.º 3
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