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

check type of parent of conditions before writting to PCS files #247

Merged
merged 3 commits into from
Jun 7, 2022
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
16 changes: 11 additions & 5 deletions ConfigSpace/read_and_write/pcs_new.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,28 +169,34 @@ def build_condition(condition):
notequal_template = "%s | %s != %s"
equal_template = "%s | %s == %s"

parent = condition.parent
if isinstance(parent, NumericalHyperparameter):
cond_values = str(condition.value)
else:
cond_values = [str(value) for value in condition.value]
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved

if isinstance(condition, NotEqualsCondition):
return notequal_template % (condition.child.name,
condition.parent.name,
condition.value)
cond_values)

elif isinstance(condition, InCondition):
return in_template % (condition.child.name,
condition.parent.name,
", ".join(condition.values))
", ".join(cond_values))

elif isinstance(condition, EqualsCondition):
return equal_template % (condition.child.name,
condition.parent.name,
condition.value)
cond_values)
elif isinstance(condition, LessThanCondition):
return less_template % (condition.child.name,
condition.parent.name,
condition.value)
cond_values)
elif isinstance(condition, GreaterThanCondition):
return greater_template % (condition.child.name,
condition.parent.name,
condition.value)
cond_values)


def build_conjunction(conjunction):
Expand Down
26 changes: 26 additions & 0 deletions test/read_and_write/test_pcs_converter.py
Original file line number Diff line number Diff line change
Expand Up @@ -561,3 +561,29 @@ def test_write_categorical_with_weights(self):
pcs.write(cs)
with self.assertRaisesRegex(ValueError, 'The pcs format does not support'):
pcs.write(cs)

def test_write_numerical_cond(self):
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved
cs = ConfigurationSpace(seed=12345)

hc1 = CategoricalHyperparameter(name="hc1", choices=[True, False], default_value=True)
hc2 = CategoricalHyperparameter(name="hc2", choices=[True, False], default_value=True)

hf1 = UniformFloatHyperparameter(name="hf1", lower=1.0, upper=10., default_value=5.0)
hi1 = UniformIntegerHyperparameter(name="hi1", lower=1, upper=10, default_value=5)
cs.add_hyperparameters([hc1, hc2, hf1, hi1])
c1 = InCondition(child=hc1, parent=hc2, values=[True])
c2 = GreaterThanCondition(hi1, hf1, 6.0)
c3 = EqualsCondition(hi1, hf1, 8.0)
c4 = AndConjunction(c2, c3)

cs.add_conditions([c1, c4])

f1 = ForbiddenEqualsClause(hc1, False)
f2 = ForbiddenEqualsClause(hf1, 2.0)
f3 = ForbiddenEqualsClause(hi1, 3)
cs.add_forbidden_clauses([ForbiddenAndConjunction(f2, f3), f1])
tf = tempfile.NamedTemporaryFile()
name = tf.name
tf.close()
with open(name, 'w') as fh:
fh.write(pcs_new.write(cs))
eddiebergman marked this conversation as resolved.
Show resolved Hide resolved