Skip to content

Commit c8337cc

Browse files
Merge pull request #415 from automl/condition_bugfix
Fix suggestion, needs discussion
2 parents 94d3ad2 + 3400af6 commit c8337cc

File tree

4 files changed

+54
-20
lines changed

4 files changed

+54
-20
lines changed

src/ConfigSpace/configuration_space.py

Lines changed: 5 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -508,25 +508,11 @@ def get_active_hyperparameters(
508508
)
509509
active_hyperparameters = set()
510510
for hp_name in self.keys():
511-
conditions = self.parent_conditions_of[hp_name]
512-
513-
active = True
514-
for condition in conditions:
515-
parent_vector_idx: np.intp | Array[np.intp]
516-
if isinstance(condition, Conjunction):
517-
assert condition.parent_vector_ids is not None
518-
parent_vector_idx = condition.parent_vector_ids
519-
else:
520-
parent_vector_idx = np.asarray(condition.parent_vector_id)
521-
522-
if np.isnan(vector[parent_vector_idx]).any():
523-
active = False
524-
break
525-
526-
if not condition.satisfied_by_vector(vector):
527-
active = False
528-
break
529-
511+
# NOTE: There could be a performance improvement possible here;
512+
# We currently check all conditions, however as a condition with only AND (no OR) can never evaluate to be True
513+
# if the vector contains any NaNs. This would have to be a property of the condition, pre-calculated
514+
# and could then be used here to reduce the number of checks.
515+
active = all(condition.satisfied_by_vector(vector) for condition in self.parent_conditions_of[hp_name])
530516
if active:
531517
active_hyperparameters.add(hp_name)
532518

test/test_conditions.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@
3030
import numpy as np
3131
import pytest
3232

33+
from ConfigSpace import ConfigurationSpace
3334
from ConfigSpace.conditions import (
3435
AndConjunction,
3536
EqualsCondition,
@@ -378,3 +379,43 @@ def test_get_parents() -> None:
378379
condition2 = InCondition(counter, _1_0_restarts, ["F", "D", "L", "x", "+"])
379380
conjunction = AndConjunction(condition, condition2)
380381
assert [_1_S_countercond, _1_0_restarts] == conjunction.parents
382+
383+
384+
def test_active_hyperparameter():
385+
cs = ConfigurationSpace(
386+
[
387+
UniformFloatHyperparameter("age_weight_ratio:log_ratio", -10.0, 3.0, 0.0),
388+
CategoricalHyperparameter(
389+
"saturation_algorithm",
390+
["discount", "fmb", "inst_gen", "lrs", "otter", "z3"],
391+
"lrs",
392+
),
393+
CategoricalHyperparameter("inst_gen_with_resolution", ["off", "on"], "off"),
394+
],
395+
)
396+
cs.add(
397+
OrConjunction(
398+
NotEqualsCondition(
399+
cs["age_weight_ratio:log_ratio"],
400+
cs["saturation_algorithm"],
401+
"inst_gen",
402+
),
403+
EqualsCondition(
404+
cs["age_weight_ratio:log_ratio"],
405+
cs["inst_gen_with_resolution"],
406+
"on",
407+
),
408+
),
409+
)
410+
cs.add(
411+
EqualsCondition(
412+
cs["inst_gen_with_resolution"],
413+
cs["saturation_algorithm"],
414+
"inst_gen",
415+
),
416+
)
417+
418+
# Check that parameter age_weight_ratio:log_ratio is active according to the default configuration
419+
# This should be the case, as saturation_algorithm is set to "lrs" (which is NOT "inst_gen") in default.
420+
default = cs.get_default_configuration()
421+
cs._check_configuration_rigorous(default)

test/test_converters_and_test_searchspaces/test_sample_configuration_spaces.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,10 @@
4747
(this_directory.parent / "test_searchspaces").absolute().resolve()
4848
)
4949
pcs_files = list(Path(configuration_space_path).glob("*.pcs"))
50+
pcs_file_ids = [path.name for path in pcs_files]
5051

5152

52-
@pytest.mark.parametrize("pcs_file", pcs_files)
53+
@pytest.mark.parametrize("pcs_file", pcs_files, ids=pcs_file_ids)
5354
def test_autosklearn_space(pcs_file: Path):
5455
with warnings.catch_warnings():
5556
warnings.simplefilter("ignore")

test/test_searchspaces/vampire.pcs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
age_weight_ratio:log_ratio real [-10.0, 3.0] [0.0]
2+
saturation_algorithm categorical {discount, fmb, inst_gen, lrs, otter, z3} [lrs]
3+
inst_gen_with_resolution categorical {off, on} [off]
4+
5+
age_weight_ratio:log_ratio | saturation_algorithm != inst_gen || inst_gen_with_resolution == on
6+
inst_gen_with_resolution | saturation_algorithm == inst_gen

0 commit comments

Comments
 (0)