def run( self, datamanager: XYDataManager, metric: Scorer, all_scoring_functions: bool, splitter, initial_configs ): # time.sleep(random.random()) if not initial_configs: self.logger.warning("Haven't initial_configs. Return.") return if hasattr(splitter, "random_state"): setattr(splitter, "random_state", self.random_state) self.evaluator.init_data( datamanager, metric, all_scoring_functions, splitter, ) self.scenario = Scenario( { "run_obj": "quality", "runcount-limit": 1000, "cs": self.shps, # configuration space "deterministic": "true", # todo : 如果是local,存在experiment,如果是其他文件系统,不输出smac # "output_dir": self.resource_manager.smac_output_dir, }, initial_runs=0, db_type=self.resource_manager.db_type, db_params=self.resource_manager.get_runhistory_db_params(), anneal_func=self.search_method_params.get("anneal_func") ) # todo 将 file_system 传入,或者给file_system添加 runtime 参数 smac = SMAC4HPO( scenario=self.scenario, rng=np.random.RandomState(self.random_state), tae_runner=self.evaluator, initial_configurations=initial_configs ) smac.solver.initial_configurations = initial_configs smac.solver.start_() run_limit = self.get_run_limit() for i in range(run_limit): smac.solver.run_() should_continue = self.evaluator.resource_manager.delete_models() if not should_continue: self.logger.info(f"PID = {os.getpid()} is exiting.") break
def run( self, initial_configs, evaluator_params=frozendict(), instance_id="", rh_db_type="sqlite", rh_db_params=frozendict(), rh_db_table_name="runhistory" ): # time.sleep(random.random()) if not initial_configs: self.logger.warning("Haven't initial_configs. Return.") return self.evaluator.init_data(**evaluator_params) senario_dict = { "run_obj": "quality", "runcount-limit": 1000, "cs": self.shps, # configuration space "deterministic": "true", "instances": [[instance_id]], "cutoff_time": self.per_run_time_limit, "memory_limit": self.per_run_memory_limit # todo : 如果是local,存在experiment,如果是其他文件系统,不输出smac # "output_dir": self.resource_manager.smac_output_dir, } self.scenario = Scenario( senario_dict, initial_runs=0, db_type=rh_db_type, db_params=rh_db_params, db_table_name=rh_db_table_name, anneal_func=self.search_method_params.get("anneal_func"), use_pynisher=self.limit_resource ) # todo 将 file_system 传入,或者给file_system添加 runtime 参数 smac = SMAC4HPO( scenario=self.scenario, rng=np.random.RandomState(self.random_state), tae_runner=self.evaluator, initial_configurations=initial_configs ) smac.solver.initial_configurations = initial_configs smac.solver.start_() run_limit = self.get_run_limit() for i in range(run_limit): smac.solver.run_() should_continue = self.evaluator.resource_manager.delete_models() if not should_continue: self.logger.info(f"PID = {os.getpid()} is exiting.") break
def run(self, datamanager: XYDataManager, metric: Scorer, all_scoring_functions: bool, splitter, initial_configs): if hasattr(splitter, "random_state"): setattr(splitter, "random_state", self.random_state) self.set_task(datamanager.task) self.evaluator.init_data( datamanager, metric, all_scoring_functions, splitter, ) # todo: metalearn self.scenario = Scenario( { "run_obj": "quality", "runcount-limit": self.runcount_limit, "cs": self.phps, # configuration space "deterministic": "true", "output_dir": self.resource_manager.smac_output_dir, }, initial_runs=self.initial_runs, db_type=self.resource_manager.db_type, db_args=self.resource_manager.rh_db_args, db_kwargs=self.resource_manager.rh_db_kwargs, ) # todo 将 file_system 传入,或者给file_system添加 runtime 参数 smac = SMAC4HPO(scenario=self.scenario, rng=np.random.RandomState(self.random_state), tae_runner=self.evaluator, initial_configurations=initial_configs) smac.solver.start_() for i in range(self.runcount_limit): smac.solver.run_() should_continue = self.evaluator.resource_manager.delete_models() if not should_continue: print("info:exit") break
def fmin_smac(func: typing.Callable, x0: typing.List[float], bounds: typing.List[typing.Iterable[float]], maxfun: int = -1, rng: typing.Union[np.random.RandomState, int] = None, scenario_args: typing.Mapping[str, typing.Any] = None, **kwargs): """ Minimize a function func using the SMAC4HPO facade (i.e., a modified version of SMAC). This function is a convenience wrapper for the SMAC4HPO class. Parameters ---------- func : typing.Callable Function to minimize. x0 : typing.List[float] Initial guess/default configuration. bounds : typing.List[typing.List[float]] ``(min, max)`` pairs for each element in ``x``, defining the bound on that parameters. maxfun : int, optional Maximum number of function evaluations. rng : np.random.RandomState, optional Random number generator used by SMAC. scenario_args: typing.Mapping[str,typing.Any] Arguments passed to the scenario See dsmac.scenario.scenario.Scenario **kwargs: Arguments passed to the optimizer class See ~dsmac.facade.smac_facade.SMAC Returns ------- x : list Estimated position of the minimum. f : float Value of `func` at the minimum. s : :class:`smac.facade.smac_hpo_facade.SMAC4HPO` SMAC objects which enables the user to get e.g., the trajectory and runhistory. """ # create configuration space cs = ConfigurationSpace() # Adjust zero padding tmplt = 'x{0:0' + str(len(str(len(bounds)))) + 'd}' for idx, (lower_bound, upper_bound) in enumerate(bounds): parameter = UniformFloatHyperparameter(name=tmplt.format(idx + 1), lower=lower_bound, upper=upper_bound, default_value=x0[idx]) cs.add_hyperparameter(parameter) # create scenario scenario_dict = { "run_obj": "quality", "cs": cs, "deterministic": "true", "initial_incumbent": "DEFAULT", } if scenario_args is not None: scenario_dict.update(scenario_args) if maxfun > 0: scenario_dict["runcount_limit"] = maxfun scenario = Scenario(scenario_dict) smac = SMAC4HPO( scenario=scenario, tae_runner=ExecuteTAFuncArray, tae_runner_kwargs={'ta': func}, rng=rng, **kwargs ) smac.logger = logging.getLogger(smac.__module__ + "." + smac.__class__.__name__) incumbent = smac.optimize() config_id = smac.solver.runhistory.config_ids[incumbent] run_key = RunKey(config_id, None, 0) incumbent_performance = smac.solver.runhistory.data[run_key] incumbent = np.array([incumbent[tmplt.format(idx + 1)] for idx in range(len(bounds))], dtype=np.float) return incumbent, incumbent_performance.cost, smac
def main_cli(self, commandline_arguments: typing.List[str]=None): """Main function of SMAC for CLI interface""" self.logger.info("SMAC call: %s" % (" ".join(sys.argv))) cmd_reader = CMDReader() kwargs = {} if commandline_arguments: kwargs['commandline_arguments'] = commandline_arguments main_args_, smac_args_, scen_args_ = cmd_reader.read_cmd(**kwargs) root_logger = logging.getLogger() root_logger.setLevel(main_args_.verbose_level) logger_handler = logging.StreamHandler( stream=sys.stdout) if root_logger.level >= logging.INFO: formatter = logging.Formatter( "%(levelname)s:\t%(message)s") else: formatter = logging.Formatter( "%(asctime)s:%(levelname)s:%(name)s:%(message)s", "%Y-%m-%d %H:%M:%S") logger_handler.setFormatter(formatter) root_logger.addHandler(logger_handler) # remove default handler if len(root_logger.handlers) > 1: root_logger.removeHandler(root_logger.handlers[0]) # Create defaults rh = None initial_configs = None stats = None incumbent = None # Create scenario-object scenario = {} scenario.update(vars(smac_args_)) scenario.update(vars(scen_args_)) scen = Scenario(scenario=scenario) # Restore state if main_args_.restore_state: root_logger.debug("Restoring state from %s...", main_args_.restore_state) rh, stats, traj_list_aclib, traj_list_old = self.restore_state(scen, main_args_) scen.output_dir_for_this_run = create_output_directory( scen, main_args_.seed, root_logger, ) scen.write() incumbent = self.restore_state_after_output_dir(scen, stats, traj_list_aclib, traj_list_old) if main_args_.warmstart_runhistory: aggregate_func = average_cost rh = RunHistory(aggregate_func=aggregate_func) scen, rh = merge_foreign_data_from_file( scenario=scen, runhistory=rh, in_scenario_fn_list=main_args_.warmstart_scenario, in_runhistory_fn_list=main_args_.warmstart_runhistory, cs=scen.cs, aggregate_func=aggregate_func) if main_args_.warmstart_incumbent: initial_configs = [scen.cs.get_default_configuration()] for traj_fn in main_args_.warmstart_incumbent: trajectory = TrajLogger.read_traj_aclib_format( fn=traj_fn, cs=scen.cs) initial_configs.append(trajectory[-1]["incumbent"]) if main_args_.mode == "SMAC4AC": optimizer = SMAC4AC( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, stats=stats, restore_incumbent=incumbent, run_id=main_args_.seed) elif main_args_.mode == "SMAC4HPO": optimizer = SMAC4HPO( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, stats=stats, restore_incumbent=incumbent, run_id=main_args_.seed) elif main_args_.mode == "SMAC4BO": optimizer = SMAC4BO( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, stats=stats, restore_incumbent=incumbent, run_id=main_args_.seed) elif main_args_.mode == "ROAR": optimizer = ROAR( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, run_id=main_args_.seed) elif main_args_.mode == "EPILS": optimizer = EPILS( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, run_id=main_args_.seed) elif main_args_.mode == "Hydra": optimizer = Hydra( scenario=scen, rng=np.random.RandomState(main_args_.seed), runhistory=rh, initial_configurations=initial_configs, stats=stats, restore_incumbent=incumbent, run_id=main_args_.seed, random_configuration_chooser=main_args_.random_configuration_chooser, n_iterations=main_args_.hydra_iterations, val_set=main_args_.hydra_validation, incs_per_round=main_args_.hydra_incumbents_per_round, n_optimizers=main_args_.hydra_n_optimizers) elif main_args_.mode == "PSMAC": optimizer = PSMAC( scenario=scen, rng=np.random.RandomState(main_args_.seed), run_id=main_args_.seed, shared_model=smac_args_.shared_model, validate=main_args_.psmac_validate, n_optimizers=main_args_.hydra_n_optimizers, n_incs=main_args_.hydra_incumbents_per_round, ) try: optimizer.optimize() except (TAEAbortException, FirstRunCrashedException) as err: self.logger.error(err)