废弃的hdl递归解析代码


    def recursion_origin(self, hdl: Dict, path=()) -> ConfigurationSpace:
        cs = ConfigurationSpace()
        # 检测一下这个dict是否在直接描述超参
        key_list = list(hdl.keys())
        if len(key_list) == 0:
            cs.add_hyperparameter(Constant("placeholder", "placeholder"))
            return cs
        else:
            sample_key = key_list[0]
            sample_value = hdl[sample_key]
            if is_hdl_bottom(sample_key, sample_value):
                store = {}
                conditions_dict = {}
                for key, value in hdl.items():
                    if purify_key(key).startswith("__"):
                        conditions_dict[key] = value
                    else:
                        hp = self.__parse_dict_to_config(key, value)
                        cs.add_hyperparameter(hp)
                        store[key] = hp
                for key, value in conditions_dict.items():
                    if SERIES_CONNECT_LEADER_TOKEN in key:
                        leader_model, condition_indicator = key.split(SERIES_CONNECT_LEADER_TOKEN)
                    else:
                        leader_model, condition_indicator = None, key

                    if condition_indicator == "__condition":
                        assert isinstance(value, list)
                        for item in value:
                            cond = self.__condition(item, store, leader_model)
                            cs.add_condition(cond)
                    elif condition_indicator == "__activate":
                        self.__activate(value, store, cs, leader_model)
                    elif condition_indicator == "__forbidden":
                        self.__forbidden(value, store, cs, leader_model)
                    elif condition_indicator == "__rely_model":
                        RelyModels.info.append([
                            value,
                            deepcopy(path)
                        ])

                return cs
        pattern = re.compile(r"(.*)\((.*)\)")
        for key, value in hdl.items():
            mat = pattern.match(key)
            if mat:
                groups = mat.groups()
                assert len(groups) == 2
                prefix_name, method = groups
                value_list = list(value.keys())
                assert len(value_list) >= 1
                if method == "choice":
                    pass
                else:
                    raise NotImplementedError()
                cur_cs = ConfigurationSpace()
                assert isinstance(value, dict)
                # 不能用constant,会报错
                choice2proba = {}
                not_specific_proba_choices = []
                sum_proba = 0
                for k in value_list:
                    v = value[k]
                    if isinstance(v, dict) and "__proba" in v:
                        proba = v.pop("__proba")
                        choice2proba[k] = proba
                        sum_proba += proba
                    else:
                        not_specific_proba_choices.append(k)
                if sum_proba <= 1:
                    if len(not_specific_proba_choices) > 0:
                        p_rest = (1 - sum_proba) / len(not_specific_proba_choices)
                        for not_specific_proba_choice in not_specific_proba_choices:
                            choice2proba[not_specific_proba_choice] = p_rest
                else:
                    choice2proba = {k: 1 / len(value_list) for k in value_list}
                proba_list = [choice2proba[k] for k in value_list]
                value_list = list(map(smac_hdl._encode, value_list))  # choices must be str

                option_param = CategoricalHyperparameter('__choice__', value_list, weights=proba_list)  # todo : default
                cur_cs.add_hyperparameter(option_param)
                for sub_key, sub_value in value.items():
                    assert isinstance(sub_value, dict)
                    sub_cs = self.recursion_origin(sub_value, path=list(path) + [prefix_name, sub_key])
                    parent_hyperparameter = {'parent': option_param, 'value': sub_key}
                    cur_cs.add_configuration_space(sub_key, sub_cs, parent_hyperparameter=parent_hyperparameter)
                cs.add_configuration_space(prefix_name, cur_cs)
            elif isinstance(value, dict):
                sub_cs = self.recursion_origin(value, path=list(path) + [key])
                cs.add_configuration_space(key, sub_cs)
            else:
                raise NotImplementedError()

        return cs

你可能感兴趣的:(automl)