Skip to content
Open
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
22 changes: 2 additions & 20 deletions src/ConfigSpace/configuration_space.py
Original file line number Diff line number Diff line change
Expand Up @@ -508,25 +508,7 @@ def get_active_hyperparameters(
)
active_hyperparameters = set()
for hp_name in self.keys():
conditions = self.parent_conditions_of[hp_name]

active = True
for condition in conditions:
parent_vector_idx: np.intp | Array[np.intp]
if isinstance(condition, Conjunction):
assert condition.parent_vector_ids is not None
parent_vector_idx = condition.parent_vector_ids
else:
parent_vector_idx = np.asarray(condition.parent_vector_id)

if np.isnan(vector[parent_vector_idx]).any():
active = False
break

Comment on lines -515 to -525
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You could be less aggressive with the deletion and keep the else: branch, i.e. when it's not a Conjunction. Otherwise, I guess the fix is to see what kind of conjunction it is to know whether to use .any() [AND] or .all() [OR].

That being said, I don't know if this code is needed here in the first place.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But the else branch no longer serves a purpose due to the if statement of 522 disappearing (condition.statisfied_by_vector is not using it)

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Turned it into a one liner for readability

if not condition.satisfied_by_vector(vector):
active = False
break

active = all(condition.satisfied_by_vector(vector) for condition in self.parent_conditions_of[hp_name])
if active:
active_hyperparameters.add(hp_name)

Expand Down Expand Up @@ -921,7 +903,7 @@ def _check_configuration_rigorous(
) -> None:
vector = configuration.get_array()
active_hyperparameters = self.get_active_hyperparameters(configuration)

print(active_hyperparameters)
for hp_name, node in self._dag.nodes.items():
hyperparameter = node.hp
hp_vector = vector[self.index_of[hp_name]]
Expand Down
41 changes: 41 additions & 0 deletions test/test_conditions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import numpy as np
import pytest

from ConfigSpace import ConfigurationSpace
from ConfigSpace.conditions import (
AndConjunction,
EqualsCondition,
Expand Down Expand Up @@ -378,3 +379,43 @@ def test_get_parents() -> None:
condition2 = InCondition(counter, _1_0_restarts, ["F", "D", "L", "x", "+"])
conjunction = AndConjunction(condition, condition2)
assert [_1_S_countercond, _1_0_restarts] == conjunction.parents


def test_active_hyperparameter():
cs = ConfigurationSpace(
[
UniformFloatHyperparameter("age_weight_ratio:log_ratio", -10.0, 3.0, 0.0),
CategoricalHyperparameter(
"saturation_algorithm",
["discount", "fmb", "inst_gen", "lrs", "otter", "z3"],
"lrs",
),
CategoricalHyperparameter("inst_gen_with_resolution", ["off", "on"], "off"),
],
)
cs.add(
OrConjunction(
NotEqualsCondition(
cs["age_weight_ratio:log_ratio"],
cs["saturation_algorithm"],
"inst_gen",
),
EqualsCondition(
cs["age_weight_ratio:log_ratio"],
cs["inst_gen_with_resolution"],
"on",
),
),
)
cs.add(
EqualsCondition(
cs["inst_gen_with_resolution"],
cs["saturation_algorithm"],
"inst_gen",
),
)

# Check that parameter age_weight_ratio:log_ratio is active according to the default configuration
# This should be the case, as saturation_algorithm is set to "lrs" (which is NOT "inst_gen") in default.
default = cs.get_default_configuration()
cs._check_configuration_rigorous(default)
Loading