Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
add check for pcs_new read
  • Loading branch information
dengdifan committed Dec 8, 2021
commit 92d89cebbf4c8646fbe1bf770947f7963a1fa9fe
28 changes: 25 additions & 3 deletions ConfigSpace/read_and_write/pcs_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,11 +414,33 @@ def read(pcs_string, debug=False):
else:
# So far, only equals is supported by SMAC
if tmp_list[1] == '=':
# TODO maybe add a check if the hyperparameter is
# actually in the configuration space
hp = configuration_space.get_hyperparameter(tmp_list[0])
if isinstance(hp, NumericalHyperparameter):
if isinstance(hp, IntegerHyperparameter):
forbidden_value = int(tmp_list[2])
elif isinstance(hp, FloatHyperparameter):
forbidden_value = float(tmp_list[2])
if forbidden_value < hp.lower or forbidden_value > hp.upper:
# TODO alternatively we could raise an warning and ignore this forbidden
raise ValueError(f'forbidden_value is set out of the bound, it needs to be set between'
f'[{hp.lower}, {hp.upper}] but its value is {forbidden_value}')
elif isinstance(hp, (CategoricalHyperparameter, OrdinalHyperparameter)):
hp_values = hp.choices if isinstance(hp, CategoricalHyperparameter) else hp.sequence
forbidden_value_in_hp_values = False
for hp_value in hp_values:
if str(hp_value) == tmp_list[2]:
forbidden_value = hp_value
forbidden_value_in_hp_values = True
break
if not forbidden_value_in_hp_values:
raise ValueError(f'forbidden_value is set out of the bound, it needs to be set as'
f'one member of {hp_values} but its value is {forbidden_value}')
else:
raise ValueError('Unsupported Hyperparamter sorts')

clause_list.append(ForbiddenEqualsClause(
configuration_space.get_hyperparameter(tmp_list[0]),
tmp_list[2]))
forbidden_value))
else:
raise NotImplementedError()
tmp_list = []
Expand Down
36 changes: 35 additions & 1 deletion test/read_and_write/test_pcs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
from ConfigSpace.conditions import EqualsCondition, InCondition, \
AndConjunction, OrConjunction, NotEqualsCondition, \
GreaterThanCondition
from ConfigSpace.forbidden import ForbiddenInClause, ForbiddenAndConjunction
from ConfigSpace.forbidden import ForbiddenInClause, ForbiddenAndConjunction, ForbiddenEqualsClause

# More complex search space
classifier = CategoricalHyperparameter("classifier", ["svm", "nn"])
Expand Down Expand Up @@ -398,6 +398,40 @@ def test_build_new_GreaterThanIntCondition(self):
value = pcs_new.write(cs)
self.assertEqual(expected, value)

def test_read_new_configuration_space_forbidden(self):
cs_with_forbidden = ConfigurationSpace()
int_hp = UniformIntegerHyperparameter('int_hp', 0, 50, 30)
float_hp = UniformFloatHyperparameter('float_hp', 0., 50., 30.)
cat_hp_str = CategoricalHyperparameter('cat_hp_str', ['a', 'b', 'c'], 'b')
ord_hp_str = OrdinalHyperparameter('ord_hp_str', ['a', 'b', 'c'], 'b')

cs_with_forbidden.add_hyperparameters([int_hp, float_hp, cat_hp_str, ord_hp_str])

int_hp_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(int_hp, 1))

float_hp_forbidden_1 = ForbiddenEqualsClause(float_hp, 1.0)
float_hp_forbidden_2 = ForbiddenEqualsClause(float_hp, 2.0)
float_hp_forbidden = ForbiddenAndConjunction(float_hp_forbidden_1, float_hp_forbidden_2)

cat_hp_str_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(cat_hp_str, 'a'))
ord_hp_float_forbidden = ForbiddenAndConjunction(ForbiddenEqualsClause(ord_hp_str, 'a'))
cs_with_forbidden.add_forbidden_clauses([int_hp_forbidden, float_hp_forbidden, cat_hp_str_forbidden,
ord_hp_float_forbidden])

complex_cs = list()
complex_cs.append("int_hp integer [0,50] [30]")
complex_cs.append("float_hp real [0.0, 50.0] [30.0]")
complex_cs.append("cat_hp_str categorical {a, b, c} [b]")
complex_cs.append("ord_hp_str ordinal {a, b, c} [b]")
complex_cs.append("# Forbiddens:")
complex_cs.append("{int_hp=1}")
complex_cs.append("{float_hp=1.0, float_hp=2.0}")
complex_cs.append("{cat_hp_str=a}")
complex_cs.append("{ord_hp_str=a}")
cs_new = pcs_new.read(complex_cs)

self.assertEqual(cs_new, cs_with_forbidden)

def test_read_new_configuration_space_complex_conditionals(self):
classi = OrdinalHyperparameter(
"classi",
Expand Down