Exemple #1
0
 def restore_state(
     self,
     scen: Scenario,
     restore_state: str,
 ) -> typing.Tuple[RunHistory, Stats, typing.List, typing.List]:
     """Read in files for state-restoration: runhistory, stats, trajectory.
     """
     # Check for folder and files
     rh_path = os.path.join(restore_state, "runhistory.json")
     stats_path = os.path.join(restore_state, "stats.json")
     traj_path_aclib = os.path.join(restore_state, "traj_aclib2.json")
     traj_path_old = os.path.join(restore_state, "traj_old.csv")
     _ = os.path.join(restore_state, "scenario.txt")
     if not os.path.isdir(restore_state):
         raise FileNotFoundError(
             "Could not find folder from which to restore.")
     # Load runhistory and stats
     rh = RunHistory()
     rh.load_json(rh_path,
                  scen.cs)  # type: ignore[attr-defined] # noqa F821
     self.logger.debug("Restored runhistory from %s", rh_path)
     stats = Stats(scen)
     stats.load(stats_path)
     self.logger.debug("Restored stats from %s", stats_path)
     with open(traj_path_aclib, 'r') as traj_fn:
         traj_list_aclib = traj_fn.readlines()
     with open(traj_path_old, 'r') as traj_fn:
         traj_list_old = traj_fn.readlines()
     return rh, stats, traj_list_aclib, traj_list_old
Exemple #2
0
def restore_state(scenario: typing.Union[Scenario, ScenarioProperties]):
    r"""Read in files for state-restoration: runhistory, stats, trajectory.

    :param scenario: Scenario whose state shall be loaded.
    :return: (RunHistory, Stats, dict)-tuple
    """
    # Check for folder and files
    rh_path = os.path.join(scenario.output_dir_for_this_run, 'runhistory.json')
    stats_path = os.path.join(scenario.output_dir_for_this_run, 'stats.json')
    traj_path_aclib = os.path.join(scenario.output_dir_for_this_run, 'traj_aclib2.json')
    if not os.path.isdir(scenario.output_dir_for_this_run):
        raise FileNotFoundError('Could not find folder from which to restore.')

    # Load runhistory and stats
    rh = RunHistory(aggregate_func=None)
    rh.load_json(rh_path, scenario.cs)
    log.debug('Restored runhistory from %s', rh_path)

    stats = Stats(scenario)
    stats.load(stats_path)
    log.debug('Restored stats from %s', stats_path)

    trajectory = TrajLogger.read_traj_aclib_format(fn=traj_path_aclib, cs=scenario.cs)
    incumbent = trajectory[-1]['incumbent']
    log.debug('Restored incumbent %s from %s', incumbent, traj_path_aclib)
    return rh, stats, incumbent
Exemple #3
0
 def restore_state(self, args_, scen, root_logger):
     # Check for folder and files
     rh_path = os.path.join(args_.restore_state, "runhistory.json")
     stats_path = os.path.join(args_.restore_state, "stats.json")
     traj_path = os.path.join(args_.restore_state, "traj_aclib2.json")
     scen_path = os.path.join(args_.restore_state, "scenario.txt")
     if not os.path.isdir(args_.restore_state):
         raise FileNotFoundError(
             "Could not find folder from which to restore.")
     # Load runhistory and stats
     rh = RunHistory(aggregate_func=None)
     rh.load_json(rh_path, scen.cs)
     root_logger.debug("Restored runhistory from %s", rh_path)
     stats = Stats(scen)
     stats.load(stats_path)
     root_logger.debug("Restored stats from %s", stats_path)
     trajectory = TrajLogger.read_traj_aclib_format(fn=traj_path,
                                                    cs=scen.cs)
     incumbent = trajectory[-1]["incumbent"]
     root_logger.debug("Restored incumbent %s from %s", incumbent,
                       traj_path)
     # Copy traj if output_dir of specified scenario-file is different than
     # the output_dir of the scenario-file in the folder from which to restore.
     if scen.output_dir != InputReader().read_scenario_file(
             scen_path)['output_dir']:
         new_traj_path = os.path.join(scen.output_dir, "traj_aclib2.json")
         shutil.copy(traj_path, new_traj_path)
         root_logger.debug("Copied traj %s", rh_path)
     return rh, stats, incumbent
 def restore_state_before_scen(self, args_):
     """Read in files for state-restoration: runhistory, stats, trajectory.
     """
     # Construct dummy-scenario for object-creation (mainly cs is needed)
     tmp_scen = InputReader().read_scenario_file(args_.scenario_file)
     tmp_scen = Scenario(tmp_scen, cmd_args={'output_dir': ''})
     # Check for folder and files
     rh_path = os.path.join(args_.restore_state, "runhistory.json")
     stats_path = os.path.join(args_.restore_state, "stats.json")
     traj_path_aclib = os.path.join(args_.restore_state, "traj_aclib2.json")
     traj_path_old = os.path.join(args_.restore_state, "traj_old.csv")
     scen_path = os.path.join(args_.restore_state, "scenario.txt")
     if not os.path.isdir(args_.restore_state):
         raise FileNotFoundError(
             "Could not find folder from which to restore.")
     # Load runhistory and stats
     rh = RunHistory(aggregate_func=None)
     rh.load_json(rh_path, tmp_scen.cs)
     self.logger.debug("Restored runhistory from %s", rh_path)
     stats = Stats(
         tmp_scen)  # Need to inject actual scenario later for output_dir!
     stats.load(stats_path)
     self.logger.debug("Restored stats from %s", stats_path)
     with open(traj_path_aclib, 'r') as traj_fn:
         traj_list_aclib = traj_fn.readlines()
     with open(traj_path_old, 'r') as traj_fn:
         traj_list_old = traj_fn.readlines()
     return rh, stats, traj_list_aclib, traj_list_old
def main():
    # Initialize scenario, using runcount_limit as budget.
    orig_scen_dict = {
        'algo': 'python cmdline_wrapper.py',
        'paramfile': 'param_config_space.pcs',
        'run_obj': 'quality',
        'runcount_limit': 25,
        'deterministic': True,
        'output_dir': 'restore_me'
    }
    original_scenario = Scenario(orig_scen_dict)
    smac = SMAC(scenario=original_scenario)
    smac.optimize()

    print(
        "\n########## BUDGET EXHAUSTED! Restoring optimization: ##########\n")

    # Now the output is in the folder 'restore_me'
    #
    # We could simply modify the scenario-object, stored in
    # 'smac.solver.scenario' and start optimization again:

    #smac.solver.scenario.ta_run_limit = 50
    #smac.optimize()

    # Or, to show the whole process of recovering a SMAC-run from the output
    # directory, create a new scenario with an extended budget:
    new_scenario = Scenario(
        orig_scen_dict,
        cmd_args={
            'runcount_limit': 50,  # overwrite these args
            'output_dir': 'restored'
        })

    # We load the runhistory, ...
    rh_path = os.path.join(original_scenario.output_dir, "runhistory.json")
    runhistory = RunHistory(aggregate_func=None)
    runhistory.load_json(rh_path, new_scenario.cs)
    # ... stats, ...
    stats_path = os.path.join(original_scenario.output_dir, "stats.json")
    stats = Stats(new_scenario)
    stats.load(stats_path)
    # ... and trajectory.
    traj_path = os.path.join(original_scenario.output_dir, "traj_aclib2.json")
    trajectory = TrajLogger.read_traj_aclib_format(fn=traj_path,
                                                   cs=new_scenario.cs)
    incumbent = trajectory[-1]["incumbent"]
    # Because we changed the output_dir, we might want to copy the old
    # trajectory-file (runhistory and stats will be complete)
    new_traj_path = os.path.join(new_scenario.output_dir, "traj_aclib2.json")
    shutil.copy(traj_path, new_traj_path)

    # Now we can initialize SMAC with the recovered objects and restore the
    # state where we left off. By providing stats and a restore_incumbent, SMAC
    # automatically detects the intention of restoring a state.
    smac = SMAC(scenario=new_scenario,
                runhistory=runhistory,
                stats=stats,
                restore_incumbent=incumbent)
    smac.optimize()
Exemple #6
0
 def restore_state(self, scen, args_):
     """Read in files for state-restoration: runhistory, stats, trajectory.
     """
     # Check for folder and files
     rh_path = os.path.join(args_.restore_state, "runhistory.json")
     stats_path = os.path.join(args_.restore_state, "stats.json")
     traj_path_aclib = os.path.join(args_.restore_state, "traj_aclib2.json")
     traj_path_old = os.path.join(args_.restore_state, "traj_old.csv")
     scen_path = os.path.join(args_.restore_state, "scenario.txt")
     if not os.path.isdir(args_.restore_state):
         raise FileNotFoundError(
             "Could not find folder from which to restore.")
     # Load runhistory and stats
     rh = RunHistory(aggregate_func=None)
     rh.load_json(rh_path, scen.cs)
     self.logger.debug("Restored runhistory from %s", rh_path)
     stats = Stats(scen)
     stats.load(stats_path)
     self.logger.debug("Restored stats from %s", stats_path)
     with open(traj_path_aclib, 'r') as traj_fn:
         traj_list_aclib = traj_fn.readlines()
     with open(traj_path_old, 'r') as traj_fn:
         traj_list_old = traj_fn.readlines()
     return rh, stats, traj_list_aclib, traj_list_old
Exemple #7
0
def create_or_restore_smac(scenario_dict, rng, tae):
    out_dir = path.join(scenario_dict['output_dir'], 'run_1')
    if True or not isfile(path.join(out_dir, "traj_aclib2.json")):
        # if some incomplete data lays arround, delete it completely
        shutil.rmtree(out_dir, ignore_errors=True)
        scenario = Scenario(scenario_dict)
        smac = SMAC4HPO(scenario=scenario,
                        rng=rng,
                        tae_runner=tae,
                        initial_design=SobolDesign,
                        run_id=1)
    else:
        new_scenario = Scenario(scenario_dict)
        rh_path = path.join(out_dir, "runhistory.json")
        runhistory = RunHistory(aggregate_func=None)
        runhistory.load_json(rh_path, new_scenario.cs)
        # ... stats, ...
        stats_path = path.join(out_dir, "stats.json")
        stats = Stats(new_scenario)
        stats.load(stats_path)
        # ... and trajectory.
        traj_path = path.join(out_dir, "traj_aclib2.json")
        trajectory = TrajLogger.read_traj_aclib_format(fn=traj_path,
                                                       cs=new_scenario.cs)
        incumbent = trajectory[-1]["incumbent"]

        # Now we can initialize SMAC with the recovered objects and restore the
        # state where we left off. By providing stats and a restore_incumbent, SMAC
        # automatically detects the intention of restoring a state.
        smac = SMAC4HPO(scenario=new_scenario,
                        runhistory=runhistory,
                        stats=stats,
                        restore_incumbent=incumbent,
                        run_id=1)
        print('restored smac from:', out_dir)
    return smac
Exemple #8
0
        original_scenario_dict,
        cmd_options={
            "runcount_limit": 50,  # overwrite these args
            "output_dir": "restored",
        },
    )

    # We load the runhistory
    rh_path = os.path.join(old_output_dir, "runhistory.json")
    runhistory = RunHistory()
    runhistory.load_json(rh_path, new_scenario.cs)

    # And the stats
    stats_path = os.path.join(old_output_dir, "stats.json")
    stats = Stats(new_scenario)
    stats.load(stats_path)

    # And the trajectory
    traj_path = os.path.join(old_output_dir, "traj_aclib2.json")
    trajectory = TrajLogger.read_traj_aclib_format(fn=traj_path,
                                                   cs=new_scenario.cs)
    incumbent = trajectory[-1]["incumbent"]

    # Now we can initialize SMAC with the recovered objects and restore the
    # state where we left off. By providing stats and a restore_incumbent, SMAC
    # automatically detects the intention of restoring a state.
    smac = SMAC4AC(
        scenario=new_scenario,
        runhistory=runhistory,
        stats=stats,
        restore_incumbent=incumbent,