Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forbidden Relations of Disabled Hyperparameters #397

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions docs/scripts/api_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

# https://mkdocstrings.github.io/recipes/
"""

from __future__ import annotations

import logging
Expand Down
2 changes: 2 additions & 0 deletions docs/scripts/debug_which_page_is_being_rendered.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

This makes it easier to identify which file is being rendered when an error happens.
"""

from __future__ import annotations

import logging
Expand All @@ -16,6 +17,7 @@

log = logging.getLogger("mkdocs")


def on_pre_page(
page: mkdocs.structure.pages.Page,
config: Any,
Expand Down
22 changes: 19 additions & 3 deletions src/ConfigSpace/forbidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,18 @@ def is_forbidden_vector(self, vector: Array[f64]) -> bool:
# Relation is always evaluated against actual value and not vector rep
left: f64 = vector[self.vector_ids[0]] # type: ignore
right: f64 = vector[self.vector_ids[1]] # type: ignore
if np.isnan(left) or np.isnan(right):
return False
return self.left.to_value(left) < self.right.to_value(right) # type: ignore

@override
def is_forbidden_vector_array(self, arr: Array[f64]) -> Mask:
left = arr[self.vector_ids[0]]
right = arr[self.vector_ids[1]]
return self.left.to_value(left) < self.right.to_value(right)
valid = ~(np.isnan(left) | np.isnan(right))
out = np.zeros_like(valid)
out[valid] = self.left.to_value(left[valid]) < self.right.to_value(right[valid])
return out


class ForbiddenEqualsRelation(ForbiddenRelation):
Expand Down Expand Up @@ -680,13 +685,19 @@ def is_forbidden_vector(self, vector: Array[f64]) -> bool:
# Relation is always evaluated against actual value and not vector rep
left = vector[self.vector_ids[0]]
right = vector[self.vector_ids[1]]
if np.isnan(left) or np.isnan(right):
return False
return self.left.to_value(left) == self.right.to_value(right) # type: ignore

@override
def is_forbidden_vector_array(self, arr: Array[f64]) -> Mask:
left = arr[self.vector_ids[0]]
right = arr[self.vector_ids[1]]
return self.left.to_value(left) == self.right.to_value(right) # type: ignore
valid = ~(np.isnan(left) | np.isnan(right))
tmp = self.left.to_value(left[valid]) == self.right.to_value(right[valid])
out = np.zeros_like(valid)
out[valid] = tmp
return out # type: ignore


class ForbiddenGreaterThanRelation(ForbiddenRelation):
Expand Down Expand Up @@ -745,13 +756,18 @@ def is_forbidden_vector(self, vector: Array[f64]) -> bool:
# Relation is always evaluated against actual value and not vector rep
left: f64 = vector[self.vector_ids[0]] # type: ignore
right: f64 = vector[self.vector_ids[1]] # type: ignore
if np.isnan(left) or np.isnan(right):
return False
return self.left.to_value(left) > self.right.to_value(right) # type: ignore

@override
def is_forbidden_vector_array(self, arr: Array[f64]) -> Mask:
left = arr[self.vector_ids[0]]
right = arr[self.vector_ids[1]]
return self.left.to_value(left) > self.right.to_value(right)
valid = ~(np.isnan(left) | np.isnan(right))
out = np.zeros_like(valid)
out[valid] = self.left.to_value(left[valid]) > self.right.to_value(right[valid])
return out


ForbiddenLike = Union[
Expand Down
20 changes: 20 additions & 0 deletions test/test_forbidden.py
Original file line number Diff line number Diff line change
Expand Up @@ -291,3 +291,23 @@ def test_relation():
assert forb.is_forbidden_value(
{"water_temperature": "hot", "water_temperature2": "cold"},
)


def test_relation_conditioned():
from ConfigSpace import EqualsCondition, ConfigurationSpace

a = OrdinalHyperparameter("a", [2, 5, 10])
enable_a = CategoricalHyperparameter("enable_a", [False, True], weights=[99999, 1])
cond_a = EqualsCondition(a, enable_a, True)

b = OrdinalHyperparameter("b", [5, 10, 15])
for forbid in (
ForbiddenEqualsRelation,
ForbiddenGreaterThanRelation,
ForbiddenLessThanRelation,
):
forbid_a_b = forbid(a, b)

cs = ConfigurationSpace()
cs.add([a, enable_a, cond_a, b, forbid_a_b])
cs.sample_configuration(100)
Loading