Skip to content

Conditions

ConfigSpace.conditions #

AndConjunction #

AndConjunction(*args: Condition | Conjunction)

Bases: Conjunction

The following example shows how two constraints with an AndConjunction can be combined.

from ConfigSpace import (
    ConfigurationSpace,
    LessThanCondition,
    GreaterThanCondition,
    AndConjunction
)

cs = ConfigurationSpace({ "a": (5, 15), "b": (0, 10), "c": (0.0, 1.0) })

less_cond = LessThanCondition(cs['c'], cs['a'], 10)
greater_cond = GreaterThanCondition(cs['c'], cs['b'], 5)
cs.add(AndConjunction(less_cond, greater_cond))

print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: UniformInteger, Range: [5, 15], Default: 10
    b, Type: UniformInteger, Range: [0, 10], Default: 5
    c, Type: UniformFloat, Range: [0.0, 1.0], Default: 0.5
  Conditions:
    (c | a < 10 && c | b > 5)
PARAMETER DESCRIPTION
*args

conditions, which will be combined with an AndConjunction

TYPE: Condition | Conjunction DEFAULT: ()

Source code in src/ConfigSpace/conditions.py
def __init__(self, *args: Condition | Conjunction) -> None:
    """By using the *AndConjunction*, constraints can easily be connected.

    The following example shows how two constraints with an *AndConjunction*
    can be combined.

    ```python exec="true", source="material-block" result="python"
    from ConfigSpace import (
        ConfigurationSpace,
        LessThanCondition,
        GreaterThanCondition,
        AndConjunction
    )

    cs = ConfigurationSpace({ "a": (5, 15), "b": (0, 10), "c": (0.0, 1.0) })

    less_cond = LessThanCondition(cs['c'], cs['a'], 10)
    greater_cond = GreaterThanCondition(cs['c'], cs['b'], 5)
    cs.add(AndConjunction(less_cond, greater_cond))

    print(cs)
    ```

    Args:
        *args: conditions, which will be combined with an *AndConjunction*
    """
    if len(args) < 2:
        raise ValueError("AndConjunction must at least have two Conditions.")
    super().__init__(*args)

BinaryOpCondition #

BinaryOpCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
    *,
    check_condition_legality: bool = True
)

Bases: Condition

This is a base class for conditions that are based on a binary operations.

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,  # HACK: Typing here is to allow in conditional
    *,
    check_condition_legality: bool = True,
) -> None:
    super().__init__(child, parent, value)

    if self._REQUIRES_ORDERABLE_PARENT and not parent.ORDERABLE:
        _clsname = self.__class__.__name__
        raise ValueError(
            f"The parent hyperparameter must be orderable to use "
            f"{_clsname}, however {self.parent} is not.",
        )
    if check_condition_legality and not parent.legal_value(value):
        raise ValueError(
            f"Hyperparameter '{child.name}' is "
            f"conditional on the illegal value '{value}' of "
            f"its parent hyperparameter '{parent.name}'",
        )

    self.vector_value = f64(self.parent.to_vector(value))

    # HACK: For now, the only kind of hyperparameter that can **not** be ordered
    # as a value type but can be ordered as a vector type is an
    # OrdinalHyperparameter. This is an explicit hack for that, but if the
    # needs arise, we can make this more generic
    from ConfigSpace.hyperparameters.ordinal import OrdinalHyperparameter

    self.need_compare_as_vector = isinstance(self.parent, OrdinalHyperparameter)

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

Condition #

Condition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
)

Bases: ABC

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
) -> None:
    if child == parent:
        raise ValueError(
            "The child and parent hyperparameter must be different "
            "hyperparameters.",
        )
    self.child = child
    self.parent = parent

    self.child_vector_id: np.intp | None = None
    self.parent_vector_id: np.intp | None = None

    self.value = value

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

EqualsCondition #

EqualsCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
    *,
    check_condition_legality: bool = True
)

Bases: BinaryOpCondition

Hyperparameter child is conditional on the parent hyperparameter being equal to value.

Make b an active hyperparameter if a has the value 1

from ConfigSpace import ConfigurationSpace, EqualsCondition
cs = ConfigurationSpace({ "a": [1, 2, 3], "b": (1.0, 8.0)})
cond = EqualsCondition(cs['b'], cs['a'], 1)
cs.add(cond)
print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: Categorical, Choices: {1, 2, 3}, Default: 1
    b, Type: UniformFloat, Range: [1.0, 8.0], Default: 4.5
  Conditions:
    b | a == 1
PARAMETER DESCRIPTION
child

This hyperparameter will be sampled in the configspace if the equal condition is satisfied

TYPE: Hyperparameter

parent

The hyperparameter, which has to satisfy the equal condition

TYPE: Hyperparameter

value

Value, which the parent is compared to

TYPE: Any

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,  # HACK: Typing here is to allow in conditional
    *,
    check_condition_legality: bool = True,
) -> None:
    super().__init__(child, parent, value)

    if self._REQUIRES_ORDERABLE_PARENT and not parent.ORDERABLE:
        _clsname = self.__class__.__name__
        raise ValueError(
            f"The parent hyperparameter must be orderable to use "
            f"{_clsname}, however {self.parent} is not.",
        )
    if check_condition_legality and not parent.legal_value(value):
        raise ValueError(
            f"Hyperparameter '{child.name}' is "
            f"conditional on the illegal value '{value}' of "
            f"its parent hyperparameter '{parent.name}'",
        )

    self.vector_value = f64(self.parent.to_vector(value))

    # HACK: For now, the only kind of hyperparameter that can **not** be ordered
    # as a value type but can be ordered as a vector type is an
    # OrdinalHyperparameter. This is an explicit hack for that, but if the
    # needs arise, we can make this more generic
    from ConfigSpace.hyperparameters.ordinal import OrdinalHyperparameter

    self.need_compare_as_vector = isinstance(self.parent, OrdinalHyperparameter)

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

GreaterThanCondition #

GreaterThanCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
    *,
    check_condition_legality: bool = True
)

Bases: BinaryOpCondition

Hyperparameter child is conditional on the parent hyperparameter being greater than value.

Make b an active hyperparameter if a is greater than 5

from ConfigSpace import ConfigurationSpace, GreaterThanCondition

cs = ConfigurationSpace({ "a": (0, 10), "b": (1.0, 8.0) }) cond = GreaterThanCondition(cs['b'], cs['a'], 5) cs.add(cond) print(cs) ```

Args: child: This hyperparameter will be sampled in the configspace, if the GreaterThanCondition is satisfied parent: The hyperparameter, which has to satisfy the GreaterThanCondition value: Value, which the parent is compared to

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,  # HACK: Typing here is to allow in conditional
    *,
    check_condition_legality: bool = True,
) -> None:
    super().__init__(child, parent, value)

    if self._REQUIRES_ORDERABLE_PARENT and not parent.ORDERABLE:
        _clsname = self.__class__.__name__
        raise ValueError(
            f"The parent hyperparameter must be orderable to use "
            f"{_clsname}, however {self.parent} is not.",
        )
    if check_condition_legality and not parent.legal_value(value):
        raise ValueError(
            f"Hyperparameter '{child.name}' is "
            f"conditional on the illegal value '{value}' of "
            f"its parent hyperparameter '{parent.name}'",
        )

    self.vector_value = f64(self.parent.to_vector(value))

    # HACK: For now, the only kind of hyperparameter that can **not** be ordered
    # as a value type but can be ordered as a vector type is an
    # OrdinalHyperparameter. This is an explicit hack for that, but if the
    # needs arise, we can make this more generic
    from ConfigSpace.hyperparameters.ordinal import OrdinalHyperparameter

    self.need_compare_as_vector = isinstance(self.parent, OrdinalHyperparameter)

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

InCondition #

InCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    values: list[Any],
)

Bases: Condition

Hyperparameter child is conditional on the parent hyperparameter being in a set of values.

make b an active hyperparameter if a is in the set [1, 2, 3, 4]

from ConfigSpace import ConfigurationSpace, InCondition
cs = ConfigurationSpace({ "a": (0, 10), "b": (1.0, 8.0) })
cond = InCondition(cs['b'], cs['a'], [1, 2, 3, 4])
cs.add(cond)
print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: UniformInteger, Range: [0, 10], Default: 5
    b, Type: UniformFloat, Range: [1.0, 8.0], Default: 4.5
  Conditions:
    b | a in {1, 2, 3, 4}
PARAMETER DESCRIPTION
child

This hyperparameter will be sampled in the configspace, if the InCondition is satisfied

TYPE: Hyperparameter

parent

The hyperparameter, which has to satisfy the InCondition

TYPE: Hyperparameter

values

Collection of values, which the parent is compared to

TYPE: list[Any]

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    values: list[Any],
) -> None:
    super().__init__(child, parent, values)
    for value in values:
        if not parent.legal_value(value):
            raise ValueError(
                f"Hyperparameter '{child.name}' is "
                f"conditional on the illegal value '{value}' of "
                f"its parent hyperparameter '{parent.name}'",
            )

    self.values = values
    self.vector_values = [self.parent.to_vector(value) for value in self.values]

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

LessThanCondition #

LessThanCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
    *,
    check_condition_legality: bool = True
)

Bases: BinaryOpCondition

Hyperparameter child is conditional on the parent hyperparameter being less than value.

Make b an active hyperparameter if a is less than 5

from ConfigSpace import ConfigurationSpace, LessThanCondition

cs = ConfigurationSpace({ "a": (0, 10), "b": (1.0, 8.0) })
cond = LessThanCondition(cs['b'], cs['a'], 5)
cs.add(cond)
print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: UniformInteger, Range: [0, 10], Default: 5
    b, Type: UniformFloat, Range: [1.0, 8.0], Default: 4.5
  Conditions:
    b | a < 5
PARAMETER DESCRIPTION
child

This hyperparameter will be sampled in the configspace, if the LessThanCondition is satisfied

TYPE: Hyperparameter

parent

The hyperparameter, which has to satisfy the LessThanCondition

TYPE: Hyperparameter

value

Value, which the parent is compared to

TYPE: Any

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,  # HACK: Typing here is to allow in conditional
    *,
    check_condition_legality: bool = True,
) -> None:
    super().__init__(child, parent, value)

    if self._REQUIRES_ORDERABLE_PARENT and not parent.ORDERABLE:
        _clsname = self.__class__.__name__
        raise ValueError(
            f"The parent hyperparameter must be orderable to use "
            f"{_clsname}, however {self.parent} is not.",
        )
    if check_condition_legality and not parent.legal_value(value):
        raise ValueError(
            f"Hyperparameter '{child.name}' is "
            f"conditional on the illegal value '{value}' of "
            f"its parent hyperparameter '{parent.name}'",
        )

    self.vector_value = f64(self.parent.to_vector(value))

    # HACK: For now, the only kind of hyperparameter that can **not** be ordered
    # as a value type but can be ordered as a vector type is an
    # OrdinalHyperparameter. This is an explicit hack for that, but if the
    # needs arise, we can make this more generic
    from ConfigSpace.hyperparameters.ordinal import OrdinalHyperparameter

    self.need_compare_as_vector = isinstance(self.parent, OrdinalHyperparameter)

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

NotEqualsCondition #

NotEqualsCondition(
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,
    *,
    check_condition_legality: bool = True
)

Bases: BinaryOpCondition

Hyperparameter child is conditional on the parent hyperparameter being not equal to value.

Make b an active hyperparameter if a has not the value 1

from ConfigSpace import ConfigurationSpace, NotEqualsCondition

cs = ConfigurationSpace({ "a": [1, 2, 3], "b": (1.0, 8.0) })
cond = NotEqualsCondition(cs['b'], cs['a'], 1)
cs.add(cond)
print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: Categorical, Choices: {1, 2, 3}, Default: 1
    b, Type: UniformFloat, Range: [1.0, 8.0], Default: 4.5
  Conditions:
    b | a != 1
PARAMETER DESCRIPTION
child

This hyperparameter will be sampled in the configspace if the not-equals condition is satisfied

TYPE: Hyperparameter

parent

The hyperparameter, which has to satisfy the not equal condition

TYPE: Hyperparameter

value

Value, which the parent is compared to

TYPE: Any

Source code in src/ConfigSpace/conditions.py
def __init__(
    self,
    child: Hyperparameter,
    parent: Hyperparameter,
    value: Any,  # HACK: Typing here is to allow in conditional
    *,
    check_condition_legality: bool = True,
) -> None:
    super().__init__(child, parent, value)

    if self._REQUIRES_ORDERABLE_PARENT and not parent.ORDERABLE:
        _clsname = self.__class__.__name__
        raise ValueError(
            f"The parent hyperparameter must be orderable to use "
            f"{_clsname}, however {self.parent} is not.",
        )
    if check_condition_legality and not parent.legal_value(value):
        raise ValueError(
            f"Hyperparameter '{child.name}' is "
            f"conditional on the illegal value '{value}' of "
            f"its parent hyperparameter '{parent.name}'",
        )

    self.vector_value = f64(self.parent.to_vector(value))

    # HACK: For now, the only kind of hyperparameter that can **not** be ordered
    # as a value type but can be ordered as a vector type is an
    # OrdinalHyperparameter. This is an explicit hack for that, but if the
    # needs arise, we can make this more generic
    from ConfigSpace.hyperparameters.ordinal import OrdinalHyperparameter

    self.need_compare_as_vector = isinstance(self.parent, OrdinalHyperparameter)

set_vector_idx #

set_vector_idx(
    hyperparameter_to_idx: Mapping[str, int]
) -> None

Sets the index of the hyperparameter for the vectorized form.

This is sort of a second-stage init that is called when a condition is added to the search space.

Source code in src/ConfigSpace/conditions.py
def set_vector_idx(self, hyperparameter_to_idx: Mapping[str, int]) -> None:
    """Sets the index of the hyperparameter for the vectorized form.

    This is sort of a second-stage init that is called when a condition is
    added to the search space.
    """
    self.child_vector_id = np.intp(hyperparameter_to_idx[self.child.name])
    self.parent_vector_id = np.intp(hyperparameter_to_idx[self.parent.name])

OrConjunction #

OrConjunction(*args: ConditionLike)

Bases: Conjunction

Similar to the AndConjunction, constraints can be combined by using the OrConjunction.

from ConfigSpace import (
    ConfigurationSpace,
    LessThanCondition,
    GreaterThanCondition,
    OrConjunction
)

cs = ConfigurationSpace({ "a": (5, 15), "b": (0, 10), "c": (0.0, 1.0) })

less_cond = LessThanCondition(cs['c'], cs['a'], 10)
greater_cond = GreaterThanCondition(cs['c'], cs['b'], 5)
cs.add(OrConjunction(less_cond, greater_cond))

print(cs)
Configuration space object:
  Hyperparameters:
    a, Type: UniformInteger, Range: [5, 15], Default: 10
    b, Type: UniformInteger, Range: [0, 10], Default: 5
    c, Type: UniformFloat, Range: [0.0, 1.0], Default: 0.5
  Conditions:
    (c | a < 10 || c | b > 5)
PARAMETER DESCRIPTION
*args

conditions, which will be combined with an OrConjunction.

TYPE: ConditionLike DEFAULT: ()

Source code in src/ConfigSpace/conditions.py
def __init__(self, *args: ConditionLike) -> None:
    """Initialize the *OrConjunction*.

    Args:
        *args: conditions, which will be combined with an *OrConjunction*.
    """
    if len(args) < 2:
        raise ValueError("OrConjunction must at least have two Conditions.")
    super().__init__(*args)