|
16 | 16 | OrdinalHyperparameter, |
17 | 17 | UniformFloatHyperparameter, |
18 | 18 | UniformIntegerHyperparameter, |
| 19 | + IntegerHyperparameter, |
| 20 | + NumericalHyperparameter, |
19 | 21 | ) |
| 22 | +from ConfigSpace.util import ForbiddenValueError, deactivate_inactive_hyperparameters |
20 | 23 | from ConfigSpace.util import get_one_exchange_neighbourhood |
21 | 24 |
|
22 | 25 | __copyright__ = "Copyright 2022, automl.org" |
@@ -182,6 +185,61 @@ def print_config_changes( |
182 | 185 | logger.debug(msg) |
183 | 186 |
|
184 | 187 |
|
| 188 | +def transform_continuous_designs( |
| 189 | + design: np.ndarray, origin: str, configspace: ConfigurationSpace |
| 190 | + ) -> list[Configuration]: |
| 191 | + """Transforms the continuous designs into a discrete list of configurations. |
| 192 | +
|
| 193 | + Parameters |
| 194 | + ---------- |
| 195 | + design : np.ndarray |
| 196 | + Array of hyperparameters originating from the initial design strategy. |
| 197 | + origin : str | None, defaults to None |
| 198 | + Label for a configuration where it originated from. |
| 199 | + configspace : ConfigurationSpace |
| 200 | +
|
| 201 | + Returns |
| 202 | + ------- |
| 203 | + configs : list[Configuration] |
| 204 | + Continuous transformed configs. |
| 205 | + """ |
| 206 | + params = configspace.get_hyperparameters() |
| 207 | + for idx, param in enumerate(params): |
| 208 | + if isinstance(param, IntegerHyperparameter): |
| 209 | + design[:, idx] = param._inverse_transform(param._transform(design[:, idx])) |
| 210 | + elif isinstance(param, NumericalHyperparameter): |
| 211 | + continue |
| 212 | + elif isinstance(param, Constant): |
| 213 | + design_ = np.zeros(np.array(design.shape) + np.array((0, 1))) |
| 214 | + design_[:, :idx] = design[:, :idx] |
| 215 | + design_[:, idx + 1 :] = design[:, idx:] |
| 216 | + design = design_ |
| 217 | + elif isinstance(param, CategoricalHyperparameter): |
| 218 | + v_design = design[:, idx] |
| 219 | + v_design[v_design == 1] = 1 - 10**-10 |
| 220 | + design[:, idx] = np.array(v_design * len(param.choices), dtype=int) |
| 221 | + elif isinstance(param, OrdinalHyperparameter): |
| 222 | + v_design = design[:, idx] |
| 223 | + v_design[v_design == 1] = 1 - 10**-10 |
| 224 | + design[:, idx] = np.array(v_design * len(param.sequence), dtype=int) |
| 225 | + else: |
| 226 | + raise ValueError("Hyperparameter not supported when transforming a continuous design.") |
| 227 | + |
| 228 | + configs = [] |
| 229 | + for vector in design: |
| 230 | + try: |
| 231 | + conf = deactivate_inactive_hyperparameters( |
| 232 | + configuration=None, configuration_space=configspace, vector=vector |
| 233 | + ) |
| 234 | + except ForbiddenValueError: |
| 235 | + continue |
| 236 | + |
| 237 | + conf.origin = origin |
| 238 | + configs.append(conf) |
| 239 | + |
| 240 | + return configs |
| 241 | + |
| 242 | + |
185 | 243 | # def check_subspace_points( |
186 | 244 | # X: np.ndarray, |
187 | 245 | # cont_dims: np.ndarray | list = [], |
|
0 commit comments