|
| 1 | +import typing |
| 2 | + |
| 3 | +import numpy as np |
| 4 | +from ConfigSpace import ConfigurationSpace |
| 5 | +from ConfigSpace.hyperparameters import ( |
| 6 | + BetaFloatHyperparameter, |
| 7 | + BetaIntegerHyperparameter, |
| 8 | + CategoricalHyperparameter, |
| 9 | + Constant, |
| 10 | + NormalFloatHyperparameter, |
| 11 | + NormalIntegerHyperparameter, |
| 12 | + OrdinalHyperparameter, |
| 13 | + UniformFloatHyperparameter, |
| 14 | + UniformIntegerHyperparameter, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +def get_types( |
| 19 | + config_space: ConfigurationSpace, |
| 20 | + instance_features: typing.Optional[np.ndarray] = None, |
| 21 | +) -> typing.Tuple[typing.List[int], typing.List[typing.Tuple[float, float]]]: |
| 22 | + """Return the types of the hyperparameters and the bounds of the |
| 23 | + hyperparameters and instance features. |
| 24 | + """ |
| 25 | + # Extract types vector for rf from config space and the bounds |
| 26 | + types = [0] * len(config_space.get_hyperparameters()) |
| 27 | + bounds = [(np.nan, np.nan)] * len(types) |
| 28 | + |
| 29 | + for i, param in enumerate(config_space.get_hyperparameters()): |
| 30 | + parents = config_space.get_parents_of(param.name) |
| 31 | + if len(parents) == 0: |
| 32 | + can_be_inactive = False |
| 33 | + else: |
| 34 | + can_be_inactive = True |
| 35 | + |
| 36 | + if isinstance(param, (CategoricalHyperparameter)): |
| 37 | + n_cats = len(param.choices) |
| 38 | + if can_be_inactive: |
| 39 | + n_cats = len(param.choices) + 1 |
| 40 | + types[i] = n_cats |
| 41 | + bounds[i] = (int(n_cats), np.nan) |
| 42 | + elif isinstance(param, (OrdinalHyperparameter)): |
| 43 | + n_cats = len(param.sequence) |
| 44 | + types[i] = 0 |
| 45 | + if can_be_inactive: |
| 46 | + bounds[i] = (0, int(n_cats)) |
| 47 | + else: |
| 48 | + bounds[i] = (0, int(n_cats) - 1) |
| 49 | + elif isinstance(param, Constant): |
| 50 | + # for constants we simply set types to 0 which makes it a numerical |
| 51 | + # parameter |
| 52 | + if can_be_inactive: |
| 53 | + bounds[i] = (2, np.nan) |
| 54 | + types[i] = 2 |
| 55 | + else: |
| 56 | + bounds[i] = (0, np.nan) |
| 57 | + types[i] = 0 |
| 58 | + # and we leave the bounds to be 0 for now |
| 59 | + elif isinstance(param, UniformFloatHyperparameter): |
| 60 | + # Are sampled on the unit hypercube thus the bounds |
| 61 | + # are always 0.0, 1.0 |
| 62 | + if can_be_inactive: |
| 63 | + bounds[i] = (-1.0, 1.0) |
| 64 | + else: |
| 65 | + bounds[i] = (0, 1.0) |
| 66 | + elif isinstance(param, UniformIntegerHyperparameter): |
| 67 | + if can_be_inactive: |
| 68 | + bounds[i] = (-1.0, 1.0) |
| 69 | + else: |
| 70 | + bounds[i] = (0, 1.0) |
| 71 | + elif isinstance(param, NormalFloatHyperparameter): |
| 72 | + if can_be_inactive: |
| 73 | + raise ValueError( |
| 74 | + "Inactive parameters not supported for Beta and Normal Hyperparameters" |
| 75 | + ) |
| 76 | + |
| 77 | + bounds[i] = (param._lower, param._upper) |
| 78 | + elif isinstance(param, NormalIntegerHyperparameter): |
| 79 | + if can_be_inactive: |
| 80 | + raise ValueError( |
| 81 | + "Inactive parameters not supported for Beta and Normal Hyperparameters" |
| 82 | + ) |
| 83 | + |
| 84 | + bounds[i] = (param.nfhp._lower, param.nfhp._upper) |
| 85 | + elif isinstance(param, BetaFloatHyperparameter): |
| 86 | + if can_be_inactive: |
| 87 | + raise ValueError( |
| 88 | + "Inactive parameters not supported for Beta and Normal Hyperparameters" |
| 89 | + ) |
| 90 | + |
| 91 | + bounds[i] = (param._lower, param._upper) |
| 92 | + elif isinstance(param, BetaIntegerHyperparameter): |
| 93 | + if can_be_inactive: |
| 94 | + raise ValueError( |
| 95 | + "Inactive parameters not supported for Beta and Normal Hyperparameters" |
| 96 | + ) |
| 97 | + |
| 98 | + bounds[i] = (param.bfhp._lower, param.bfhp._upper) |
| 99 | + elif not isinstance( |
| 100 | + param, |
| 101 | + ( |
| 102 | + UniformFloatHyperparameter, |
| 103 | + UniformIntegerHyperparameter, |
| 104 | + OrdinalHyperparameter, |
| 105 | + CategoricalHyperparameter, |
| 106 | + NormalFloatHyperparameter, |
| 107 | + NormalIntegerHyperparameter, |
| 108 | + BetaFloatHyperparameter, |
| 109 | + BetaIntegerHyperparameter, |
| 110 | + ), |
| 111 | + ): |
| 112 | + raise TypeError("Unknown hyperparameter type %s" % type(param)) |
| 113 | + |
| 114 | + if instance_features is not None: |
| 115 | + types = types + [0] * instance_features.shape[1] |
| 116 | + |
| 117 | + return types, bounds |
0 commit comments