def alternative_configuration_recovery(config_list: typing.List[str], cs: ConfigurationSpace): """ Used to recover ints and bools as categoricals or constants from trajectory """ config_dict = {} for param in config_list: k,v = param.split("=") v = v.strip("'") hp = cs.get_hyperparameter(k) if isinstance(hp, FloatHyperparameter): v = float(v) elif isinstance(hp, IntegerHyperparameter): v = int(v) ################# DIFFERENCE: ################ elif isinstance(hp, CategoricalHyperparameter) or isinstance(hp, Constant): if isinstance(hp.default_value, bool): v = True if v == 'True' else False elif isinstance(hp.default_value, int): v = int(v) elif isinstance(hp.default_value, float): v = float(v) else: v = v ############################################## config_dict[k] = v config = Configuration(configuration_space=cs, values=config_dict) config.origin = "External Trajectory" return config
def _convert_dict_to_config(config_list: typing.List[str], cs: ConfigurationSpace): # CAN BE DONE IN CONFIGSPACE """Since we save a configurations in a dictionary str->str we have to try to figure out the type (int, float, str) of each parameter value Parameters ---------- config_list: typing.List[str] Configuration as a list of "str='str'" cs: ConfigurationSpace Configuration Space to translate dict object into Confiuration object """ config_dict = {} for param in config_list: k, v = param.split("=") v = v.strip("'") hp = cs.get_hyperparameter(k) if isinstance(hp, FloatHyperparameter): v = float(v) elif isinstance(hp, IntegerHyperparameter): v = int(v) config_dict[k] = v config = Configuration(configuration_space=cs, values=config_dict) config.origin = "External Trajectory" return config
def _convert_dict_to_config(self, config_dict, config_space): # Method come from SMAC3 config = Configuration(configuration_space=config_space, values=config_dict) config.origin = "External Trajectory" return config
def _get_config(self, config_id, id2config, cs): config = Configuration(cs, id2config[config_id]['config']) try: model_based_pick = id2config[config_id]['config_info']['model_based_pick'] config.origin = 'Model based pick' if model_based_pick else 'Random' except KeyError: self.logger.debug("No origin for config!", exc_info=True) return config
def _convert_dict_to_config(config_list: typing.List[str], cs: ConfigurationSpace) -> Configuration: """Since we save a configurations in a dictionary str->str we have to try to figure out the type (int, float, str) of each parameter value Parameters ---------- config_list: typing.List[str] Configuration as a list of "str='str'" cs: ConfigurationSpace Configuration Space to translate dict object into Confiuration object """ config_dict = {} v = '' # type: typing.Union[str, float, int, bool] for param in config_list: k, v = param.split("=") v = v.strip("'") hp = cs.get_hyperparameter(k) if isinstance(hp, FloatHyperparameter): v = float(v) elif isinstance(hp, IntegerHyperparameter): v = int(v) elif isinstance(hp, (CategoricalHyperparameter, Constant)): # Checking for the correct type requires jumping some hoops # First, we gather possible interpretations of our string interpretations = [v] # type: typing.List[typing.Union[str, bool, int, float]] if v in ["True", "False"]: # Special Case for booleans (assuming we support them) # This is important to avoid false positive warnings triggered by 1 == True or "False" == True interpretations.append(True if v == 'True' else False) else: for t in [int, float]: try: interpretations.append(t(v)) except ValueError: continue # Second, check if it's in the choices / the correct type. legal = {interpretation for interpretation in interpretations if hp.is_legal(interpretation)} # Third, issue warnings if the interpretation is ambigious if len(legal) != 1: logging.getLogger("smac.trajlogger").warning( "Ambigous or no interpretation of value {} for hp {} found ({} possible interpretations). " "Passing string, but this will likely result in an error".format(v, hp.name, len(legal))) else: v = legal.pop() config_dict[k] = v config = Configuration(configuration_space=cs, values=config_dict) config.origin = "External Trajectory" return config
def from_dict(raw: Dict, origin: str) -> 'PartialConfig': # meta data are deserialized via pickle config = Configuration(config_json.read(raw['configspace']), vector=np.array(raw['config'])) config.origin = origin # noinspection PyTypeChecker return PartialConfig(raw['cfg_key'], config, raw['name'], np.array(raw['mf']))
def from_dict(raw: Dict, cs: ConfigurationSpace) -> 'Result': config = Configuration(cs, raw['config']) config.origin = raw['origin'] return Result(CandidateId.parse(raw['id']), StatusType[raw['status']], config, raw['loss'], raw['structure_loss'], Runtime.from_dict(raw['runtime']))