Skip to content

Commit 175f798

Browse files
authored
Squashed all the changes related to densities. _pdf works with the (#241)
shrunk unit hypercube range for the Beta hyperparameter case, and tests have been rewritten to accomodate.
1 parent 0ec0e13 commit 175f798

6 files changed

Lines changed: 1761 additions & 8 deletions

File tree

ConfigSpace/configuration_space.pyx

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,31 @@ class ConfigurationSpace(collections.abc.Mapping):
13441344
"""
13451345
self.random = np.random.RandomState(seed)
13461346

1347+
def remove_hyperparameter_priors(self) -> 'ConfigurationSpace':
1348+
"""
1349+
Produces a new ConfigurationSpace where all priors on parameters are removed.
1350+
Non-uniform hyperpararmeters are replaced with uniform ones, and
1351+
CategoricalHyperparameters with weights have their weights removed.
1352+
1353+
Returns
1354+
-------
1355+
:class:`~ConfigSpace.configuration_space.ConfigurationSpace`
1356+
The resulting configuration space, without priors on the hyperparameters
1357+
"""
1358+
uniform_config_space = ConfigurationSpace()
1359+
for parameter in self.get_hyperparameters():
1360+
if hasattr(parameter, 'to_uniform'):
1361+
uniform_config_space.add_hyperparameter(parameter.to_uniform())
1362+
else:
1363+
uniform_config_space.add_hyperparameter(copy.copy(parameter))
1364+
1365+
new_conditions = self.substitute_hyperparameters_in_conditions(self.get_conditions(), uniform_config_space)
1366+
new_forbiddens = self.substitute_hyperparameters_in_forbiddens(self.get_forbiddens(), uniform_config_space)
1367+
uniform_config_space.add_conditions(new_conditions)
1368+
uniform_config_space.add_forbidden_clauses(new_forbiddens)
1369+
1370+
return uniform_config_space
1371+
13471372
def estimate_size(self) -> Union[float, int]:
13481373
"""
13491374
Estimate the size of the current configuration space (i.e. unique configurations).
@@ -1370,6 +1395,102 @@ class ConfigurationSpace(collections.abc.Mapping):
13701395
size = size * sizes[i]
13711396
return size
13721397

1398+
@staticmethod
1399+
def substitute_hyperparameters_in_conditions(conditions, new_configspace) -> List['ConditionComponent']:
1400+
"""
1401+
Takes a set of conditions and generates a new set of conditions with the same structure, where
1402+
each hyperparameter is replaced with its namesake in new_configspace. As such, the set of conditions
1403+
remain unchanged, but the included hyperparameters are changed to match those types that exist in
1404+
new_configspace.
1405+
1406+
Parameters
1407+
----------
1408+
new_configspace: ConfigurationSpace
1409+
A ConfigurationSpace containing hyperparameters with the same names as those in the conditions.
1410+
1411+
Returns
1412+
-------
1413+
List[ConditionComponent]:
1414+
The list of conditions, adjusted to fit the new ConfigurationSpace
1415+
"""
1416+
new_conditions = []
1417+
for condition in conditions:
1418+
if isinstance(condition, AbstractConjunction):
1419+
conjunction_type = type(condition)
1420+
children = condition.get_descendant_literal_conditions()
1421+
substituted_children = ConfigurationSpace.substitute_hyperparameters_in_conditions(children, new_configspace)
1422+
substituted_conjunction = conjunction_type(*substituted_children)
1423+
new_conditions.append(substituted_conjunction)
1424+
1425+
elif isinstance(condition, AbstractCondition):
1426+
condition_type = type(condition)
1427+
child_name = getattr(condition.get_children()[0], 'name')
1428+
parent_name = getattr(condition.get_parents()[0], 'name')
1429+
new_child = new_configspace[child_name]
1430+
new_parent = new_configspace[parent_name]
1431+
1432+
if hasattr(condition, 'value'):
1433+
condition_arg = getattr(condition, 'value')
1434+
substituted_condition = condition_type(child=new_child, parent=new_parent, value=condition_arg)
1435+
elif hasattr(condition, 'values'):
1436+
condition_arg = getattr(condition, 'values')
1437+
substituted_condition = condition_type(child=new_child, parent=new_parent, values=condition_arg)
1438+
else:
1439+
raise AttributeError(f'Did not find the expected attribute in condition {type(condition)}.')
1440+
1441+
new_conditions.append(substituted_condition)
1442+
else:
1443+
raise TypeError(f'Did not expect the supplied condition type {type(condition)}.')
1444+
1445+
return new_conditions
1446+
1447+
@staticmethod
1448+
def substitute_hyperparameters_in_forbiddens(forbiddens, new_configspace) -> List['ConditionComponent']:
1449+
"""
1450+
Takes a set of forbidden clauses and generates a new set of forbidden clauses with the same structure,
1451+
where each hyperparameter is replaced with its namesake in new_configspace. As such, the set of forbidden
1452+
clauses remain unchanged, but the included hyperparameters are changed to match those types that exist in
1453+
new_configspace.
1454+
1455+
Parameters
1456+
----------
1457+
new_configspace: ConfigurationSpace
1458+
A ConfigurationSpace containing hyperparameters with the same names as those in the forbidden clauses.
1459+
1460+
Returns
1461+
-------
1462+
List[AbstractForbiddenComponent]:
1463+
The list of forbidden clauses, adjusted to fit the new ConfigurationSpace
1464+
"""
1465+
new_forbiddens = []
1466+
for forbidden in forbiddens:
1467+
if isinstance(forbidden, AbstractForbiddenConjunction):
1468+
conjunction_type = type(forbidden)
1469+
children = forbidden.get_descendant_literal_clauses()
1470+
substituted_children = ConfigurationSpace.substitute_hyperparameters_in_forbiddens(children, new_configspace)
1471+
substituted_conjunction = conjunction_type(*substituted_children)
1472+
new_forbiddens.append(substituted_conjunction)
1473+
1474+
elif isinstance(forbidden, AbstractForbiddenClause):
1475+
forbidden_type = type(forbidden)
1476+
hyperparameter_name = getattr(forbidden.hyperparameter, 'name')
1477+
new_hyperparameter = new_configspace[hyperparameter_name]
1478+
1479+
if hasattr(forbidden, 'value'):
1480+
forbidden_arg = getattr(forbidden, 'value')
1481+
substituted_forbidden = forbidden_type(hyperparameter=new_hyperparameter, value=forbidden_arg)
1482+
elif hasattr(forbidden, 'values'):
1483+
forbidden_arg = getattr(forbidden, 'values')
1484+
substituted_forbidden = forbidden_type(hyperparameter=new_hyperparameter, values=forbidden_arg)
1485+
else:
1486+
raise AttributeError(f'Did not find the expected attribute in forbidden {type(forbidden)}.')
1487+
1488+
new_forbiddens.append(substituted_forbidden)
1489+
else:
1490+
raise TypeError(f'Did not expect the supplied forbidden type {type(forbidden)}.')
1491+
1492+
return new_forbiddens
1493+
13731494

13741495
class Configuration(collections.abc.Mapping):
13751496
def __init__(self, configuration_space: ConfigurationSpace,

ConfigSpace/forbidden.pyx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
# notice, this list of conditions and the following disclaimer in the
1313
# documentation and/or other materials provided with the distribution.
1414
# * Neither the name of the <organization> nor the
15-
# names of its contributors may be used to endorse or promote products
15+
# names of itConfigurationSpaces contributors may be used to endorse or promote products
1616
# derived from this software without specific prior written permission.
1717
#
1818
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND

0 commit comments

Comments
 (0)