Ejemplo n.º 1
0
def add_component_deepcopy(config_space, prefix, component_space):
    # We have to retrieve the configuration space every time because
    # we change the objects it returns. If we reused it, we could not
    # retrieve the conditions further down
    # TODO implement copy for hyperparameters and forbidden and
    # conditions!
    component = config_space.get_hyperparameter("__choice__")
    for parameter in component_space.get_hyperparameters():
        new_parameter = copy.deepcopy(parameter)
        new_parameter.name = "%s:%s" % (prefix, new_parameter.name)
        config_space.add_hyperparameter(new_parameter)
        # We must only add a condition if the hyperparameter is not
        # conditional on something else
        if len(component_space.get_parents_of(parameter)) == 0:
            condition = EqualsCondition(new_parameter, component, prefix)
            config_space.add_condition(condition)
    space_copy = copy.deepcopy(component_space)
    for condition in space_copy.get_conditions():
        dlcs = condition.get_descendant_literal_conditions()
        for dlc in dlcs:
            if not dlc.child.name.startswith(prefix):
                dlc.child.name = "%s:%s" % (prefix, dlc.child.name)
            if not dlc.parent.name.startswith(prefix):
                dlc.parent.name = "%s:%s" % (prefix, dlc.parent.name)
        config_space.add_condition(condition)
    space_copy = copy.deepcopy(component_space)
    for forbidden_clause in space_copy.forbidden_clauses:
        dlcs = forbidden_clause.get_descendant_literal_clauses()
        for dlc in dlcs:
            if not dlc.hyperparameter.name.startswith(prefix):
                dlc.hyperparameter.name = "%s:%s" % (prefix,
                                                     dlc.hyperparameter.name)
        config_space.add_forbidden_clause(forbidden_clause)

    return config_space
Ejemplo n.º 2
0
def add_component_deepcopy(config_space, prefix, component_space):
    # We have to retrieve the configuration space every time because
    # we change the objects it returns. If we reused it, we could not
    # retrieve the conditions further down
    # TODO implement copy for hyperparameters and forbidden and
    # conditions!
    component = config_space.get_hyperparameter("__choice__")
    for parameter in component_space.get_hyperparameters():
        new_parameter = copy.deepcopy(parameter)
        new_parameter.name = "%s:%s" % (prefix,
                                        new_parameter.name)
        config_space.add_hyperparameter(new_parameter)
        # We must only add a condition if the hyperparameter is not
        # conditional on something else
        if len(component_space.get_parents_of(parameter)) == 0:
            condition = EqualsCondition(new_parameter, component,
                                        prefix)
            config_space.add_condition(condition)
    space_copy = copy.deepcopy(component_space)
    for condition in space_copy.get_conditions():
        dlcs = condition.get_descendant_literal_conditions()
        for dlc in dlcs:
            if not dlc.child.name.startswith(prefix):
                dlc.child.name = "%s:%s" % (
                    prefix, dlc.child.name)
            if not dlc.parent.name.startswith(prefix):
                dlc.parent.name = "%s:%s" % (
                    prefix, dlc.parent.name)
        config_space.add_condition(condition)
    space_copy = copy.deepcopy(component_space)
    for forbidden_clause in space_copy.forbidden_clauses:
        dlcs = forbidden_clause.get_descendant_literal_clauses()
        for dlc in dlcs:
            if not dlc.hyperparameter.name.startswith(prefix):
                dlc.hyperparameter.name = "%s:%s" % (prefix,
                                                     dlc.hyperparameter.name)
        config_space.add_forbidden_clause(forbidden_clause)

    return config_space
Ejemplo n.º 3
0
    def get_hyperparameter_search_space(cls, dataset_properties,
                                        default=None,
                                        include=None,
                                        exclude=None):
        if include is not None and exclude is not None:
            raise ValueError("The argument include and exclude cannot be used together.")

        cs = ConfigurationSpace()

        # Compile a list of all estimator objects for this problem
        available_estimators = cls.get_available_components(
            data_prop=dataset_properties,
            include=include,
            exclude=exclude)

        if len(available_estimators) == 0:
            raise ValueError("No regressors found")

        if default is None:
            defaults = ['random_forest', 'support_vector_regression'] + \
                list(available_estimators.keys())
            for default_ in defaults:
                if default_ in available_estimators:
                    if include is not None and default_ not in include:
                        continue
                    if exclude is not None and default_ in exclude:
                        continue
                    default = default_
                    break

        estimator = CategoricalHyperparameter('__choice__',
                                              list(available_estimators.keys()),
                                              default=default)
        cs.add_hyperparameter(estimator)
        for estimator_name in available_estimators.keys():

            # We have to retrieve the configuration space every time because
            # we change the objects it returns. If we reused it, we could not
            # retrieve the conditions further down
            # TODO implement copy for hyperparameters and forbidden and
            # conditions!

            estimator_configuration_space = available_estimators[
                estimator_name]. \
                get_hyperparameter_search_space(dataset_properties)
            for parameter in estimator_configuration_space.get_hyperparameters():
                new_parameter = copy.deepcopy(parameter)
                new_parameter.name = "%s:%s" % (
                    estimator_name, new_parameter.name)
                cs.add_hyperparameter(new_parameter)
                # We must only add a condition if the hyperparameter is not
                # conditional on something else
                if len(estimator_configuration_space.
                        get_parents_of(parameter)) == 0:
                    condition = EqualsCondition(new_parameter, estimator,
                                                estimator_name)
                    cs.add_condition(condition)

            for condition in available_estimators[estimator_name]. \
                    get_hyperparameter_search_space(
                    dataset_properties).get_conditions():
                dlcs = condition.get_descendant_literal_conditions()
                for dlc in dlcs:
                    if not dlc.child.name.startswith(estimator_name):
                        dlc.child.name = "%s:%s" % (
                            estimator_name, dlc.child.name)
                    if not dlc.parent.name.startswith(estimator_name):
                        dlc.parent.name = "%s:%s" % (
                            estimator_name, dlc.parent.name)
                cs.add_condition(condition)

            for forbidden_clause in available_estimators[estimator_name]. \
                    get_hyperparameter_search_space(
                    dataset_properties).forbidden_clauses:
                dlcs = forbidden_clause.get_descendant_literal_clauses()
                for dlc in dlcs:
                    if not dlc.hyperparameter.name.startswith(estimator_name):
                        dlc.hyperparameter.name = "%s:%s" % (estimator_name,
                                                             dlc.hyperparameter.name)
                cs.add_forbidden_clause(forbidden_clause)

        return cs
Ejemplo n.º 4
0
    def get_hyperparameter_search_space(cls,
                                        dataset_properties,
                                        default=None,
                                        include=None,
                                        exclude=None):
        if include is not None and exclude is not None:
            raise ValueError(
                "The argument include and exclude cannot be used together.")

        cs = ConfigurationSpace()

        # Compile a list of all estimator objects for this problem
        available_estimators = cls.get_available_components(
            data_prop=dataset_properties, include=include, exclude=exclude)

        if len(available_estimators) == 0:
            raise ValueError("No regressors found")

        if default is None:
            defaults = ['random_forest', 'support_vector_regression'] + \
                list(available_estimators.keys())
            for default_ in defaults:
                if default_ in available_estimators:
                    if include is not None and default_ not in include:
                        continue
                    if exclude is not None and default_ in exclude:
                        continue
                    default = default_
                    break

        estimator = CategoricalHyperparameter('__choice__',
                                              list(
                                                  available_estimators.keys()),
                                              default=default)
        cs.add_hyperparameter(estimator)
        for estimator_name in available_estimators.keys():

            # We have to retrieve the configuration space every time because
            # we change the objects it returns. If we reused it, we could not
            # retrieve the conditions further down
            # TODO implement copy for hyperparameters and forbidden and
            # conditions!

            estimator_configuration_space = available_estimators[
                estimator_name]. \
                get_hyperparameter_search_space(dataset_properties)
            for parameter in estimator_configuration_space.get_hyperparameters(
            ):
                new_parameter = copy.deepcopy(parameter)
                new_parameter.name = "%s:%s" % (estimator_name,
                                                new_parameter.name)
                cs.add_hyperparameter(new_parameter)
                # We must only add a condition if the hyperparameter is not
                # conditional on something else
                if len(estimator_configuration_space.get_parents_of(
                        parameter)) == 0:
                    condition = EqualsCondition(new_parameter, estimator,
                                                estimator_name)
                    cs.add_condition(condition)

            for condition in available_estimators[estimator_name]. \
                    get_hyperparameter_search_space(
                    dataset_properties).get_conditions():
                dlcs = condition.get_descendant_literal_conditions()
                for dlc in dlcs:
                    if not dlc.child.name.startswith(estimator_name):
                        dlc.child.name = "%s:%s" % (estimator_name,
                                                    dlc.child.name)
                    if not dlc.parent.name.startswith(estimator_name):
                        dlc.parent.name = "%s:%s" % (estimator_name,
                                                     dlc.parent.name)
                cs.add_condition(condition)

            for forbidden_clause in available_estimators[estimator_name]. \
                    get_hyperparameter_search_space(
                    dataset_properties).forbidden_clauses:
                dlcs = forbidden_clause.get_descendant_literal_clauses()
                for dlc in dlcs:
                    if not dlc.hyperparameter.name.startswith(estimator_name):
                        dlc.hyperparameter.name = "%s:%s" % (
                            estimator_name, dlc.hyperparameter.name)
                cs.add_forbidden_clause(forbidden_clause)

        return cs