|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import math |
| 4 | +from typing import Any |
| 5 | +from typing import Union |
| 6 | + |
| 7 | +import cmaes |
| 8 | +import numpy as np |
| 9 | +from optuna import Study |
| 10 | +from optuna._transform import _SearchSpaceTransform |
| 11 | +from optuna.distributions import BaseDistribution |
| 12 | +from optuna.distributions import FloatDistribution |
| 13 | +from optuna.distributions import IntDistribution |
| 14 | +from optuna.samplers import BaseSampler |
| 15 | +from optuna.samplers import CmaEsSampler |
| 16 | +from optuna.study import StudyDirection |
| 17 | +from optuna.trial import FrozenTrial |
| 18 | + |
| 19 | + |
| 20 | +CmaClass = Union[cmaes.CMA, cmaes.SepCMA, cmaes.CMAwM] |
| 21 | + |
| 22 | + |
| 23 | +class UserPriorCmaEsSampler(CmaEsSampler): |
| 24 | + """A sampler using `cmaes <https://github.com/CyberAgentAILab/cmaes>`__ as the backend with user prior. |
| 25 | +
|
| 26 | + Please check ``CmaEsSampler`` in Optuna for more details of each argument. |
| 27 | + This class modified the arguments ``x0`` and ``sigma0`` in ``CmaEsSampler`` of Optuna. |
| 28 | + Furthermore, due to the incompatibility, |
| 29 | + This class does not support ``source_trials`` and ``use_separable_cma``. |
| 30 | +
|
| 31 | + Args: |
| 32 | + param_names: |
| 33 | + The list of the parameter names to be tuned. This list must be a unique list. |
| 34 | + mu0: |
| 35 | + The mean vector used for the initialization of CMA-ES. |
| 36 | + cov0: |
| 37 | + The covariance matrix used for the initialization of CMA-ES. |
| 38 | + """ # NOQA: E501 |
| 39 | + |
| 40 | + def __init__( |
| 41 | + self, |
| 42 | + param_names: list[str], |
| 43 | + mu0: np.ndarray, |
| 44 | + cov0: np.ndarray, |
| 45 | + n_startup_trials: int = 1, |
| 46 | + independent_sampler: BaseSampler | None = None, |
| 47 | + warn_independent_sampling: bool = True, |
| 48 | + seed: int | None = None, |
| 49 | + *, |
| 50 | + consider_pruned_trials: bool = False, |
| 51 | + restart_strategy: str | None = None, |
| 52 | + popsize: int | None = None, |
| 53 | + inc_popsize: int = 2, |
| 54 | + with_margin: bool = False, |
| 55 | + lr_adapt: bool = False, |
| 56 | + ) -> None: |
| 57 | + super().__init__( |
| 58 | + x0=None, |
| 59 | + sigma0=None, |
| 60 | + n_startup_trials=n_startup_trials, |
| 61 | + independent_sampler=independent_sampler, |
| 62 | + warn_independent_sampling=warn_independent_sampling, |
| 63 | + seed=seed, |
| 64 | + consider_pruned_trials=consider_pruned_trials, |
| 65 | + restart_strategy=restart_strategy, |
| 66 | + popsize=popsize, |
| 67 | + inc_popsize=inc_popsize, |
| 68 | + use_separable_cma=False, |
| 69 | + with_margin=with_margin, |
| 70 | + lr_adapt=lr_adapt, |
| 71 | + source_trials=None, |
| 72 | + ) |
| 73 | + self._validate_user_prior(param_names, mu0, cov0) |
| 74 | + self._param_names = param_names[:] |
| 75 | + self._mu0 = mu0.astype(float) |
| 76 | + self._cov0 = cov0.astype(float) |
| 77 | + |
| 78 | + def _validate_user_prior( |
| 79 | + self, param_names: list[str], mu0: np.ndarray, cov0: np.ndarray |
| 80 | + ) -> None: |
| 81 | + dim = len(param_names) |
| 82 | + if dim != len(set(param_names)): |
| 83 | + raise ValueError( |
| 84 | + "Some elements in param_names are duplicated. Please make it a unique list." |
| 85 | + ) |
| 86 | + if mu0.shape != (dim,) or cov0.shape != (dim, dim): |
| 87 | + raise ValueError( |
| 88 | + f"The shape of mu0 and cov0 must be (len(param_names)={dim}, ) and " |
| 89 | + f"(len(param_names)={dim}, len(param_names)={dim}), but got {mu0.shape} and " |
| 90 | + f"{cov0.shape}." |
| 91 | + ) |
| 92 | + if not np.allclose(cov0, cov0.T): |
| 93 | + raise ValueError("cov0 must be a symmetric matrix.") |
| 94 | + if np.any(cov0 < 0.0): |
| 95 | + raise ValueError("All elements in cov0 must be non-negative.") |
| 96 | + if np.any(np.linalg.eigvals(cov0) < 0.0): |
| 97 | + raise ValueError("cov0 must be a semi-positive definite matrix.") |
| 98 | + |
| 99 | + def sample_relative( |
| 100 | + self, |
| 101 | + study: Study, |
| 102 | + trial: FrozenTrial, |
| 103 | + search_space: dict[str, BaseDistribution], |
| 104 | + ) -> dict[str, Any]: |
| 105 | + if len(search_space) != 0 and set(search_space.keys()) != set(self._param_names): |
| 106 | + raise ValueError( |
| 107 | + "The keys in search_space and param_names did not match. " |
| 108 | + "The most probable reason is duplicated names in param_names." |
| 109 | + ) |
| 110 | + elif len(search_space) != 0: |
| 111 | + # Ensure the parameter order is identical to that in param_names. |
| 112 | + search_space = { |
| 113 | + param_name: search_space[param_name] for param_name in self._param_names |
| 114 | + } |
| 115 | + |
| 116 | + return super().sample_relative(study=study, trial=trial, search_space=search_space) |
| 117 | + |
| 118 | + def _calculate_initial_params( |
| 119 | + self, trans: _SearchSpaceTransform |
| 120 | + ) -> tuple[np.ndarray, float, np.ndarray]: |
| 121 | + # NOTE(nabenabe): Except this method, everything is basically based on Optuna v4.0.0. |
| 122 | + # As this class does not support some cases supported by Optuna, I simply added validation |
| 123 | + # to each method, but otherwise, nothing changed. In principle, if users find a bug, it is |
| 124 | + # likely that the bug exists in this method. |
| 125 | + search_space = trans._search_space.copy() |
| 126 | + if any( |
| 127 | + not isinstance(d, (IntDistribution, FloatDistribution)) for d in search_space.values() |
| 128 | + ): |
| 129 | + raise ValueError("search_space cannot include categorical parameters.") |
| 130 | + if any( |
| 131 | + d.log |
| 132 | + for d in search_space.values() |
| 133 | + if isinstance(d, (FloatDistribution, IntDistribution)) |
| 134 | + ): |
| 135 | + src_url = "https://hub.optuna.org/samplers/user_prior_cmaes/" |
| 136 | + raise ValueError( |
| 137 | + "search_space for user_prior cannot include log scale. " |
| 138 | + f"Please use the workaround described in {src_url}." |
| 139 | + ) |
| 140 | + |
| 141 | + dim = len(self._param_names) |
| 142 | + raw_bounds = trans._raw_bounds |
| 143 | + domain_sizes = raw_bounds[:, 1] - raw_bounds[:, 0] |
| 144 | + is_single = domain_sizes == 0.0 |
| 145 | + |
| 146 | + mu0 = self._mu0.copy() |
| 147 | + mu0[is_single] = 0.5 |
| 148 | + # Clip into [0, 1]. |
| 149 | + mu0[~is_single] = (mu0[~is_single] - raw_bounds[~is_single, 0]) / domain_sizes[~is_single] |
| 150 | + |
| 151 | + # We also need to transform the covariance matrix accordingly to adapt to the [0, 1] scale. |
| 152 | + cov0 = self._cov0 / (domain_sizes * domain_sizes[:, np.newaxis]) |
| 153 | + |
| 154 | + # Make the determinant of cov0 1 so that it agrees with the CMA-ES convention. |
| 155 | + sigma0 = math.pow(np.linalg.det(cov0), 1.0 / 2.0 / dim) |
| 156 | + # Avoid ZeroDivisionError in cmaes. |
| 157 | + sigma0 = max(sigma0, 1e-10) |
| 158 | + cov0 /= sigma0**2 |
| 159 | + |
| 160 | + return mu0, sigma0, cov0 |
| 161 | + |
| 162 | + def _init_optimizer( |
| 163 | + self, |
| 164 | + trans: _SearchSpaceTransform, |
| 165 | + direction: StudyDirection, |
| 166 | + population_size: int | None = None, |
| 167 | + randomize_start_point: bool = False, |
| 168 | + ) -> CmaClass: |
| 169 | + n_dimension = len(trans.bounds) |
| 170 | + mu0, sigma0, cov0 = self._calculate_initial_params(trans) |
| 171 | + |
| 172 | + if self._with_margin: |
| 173 | + steps = np.empty(len(trans._search_space), dtype=float) |
| 174 | + for i, dist in enumerate(trans._search_space.values()): |
| 175 | + assert isinstance(dist, (IntDistribution, FloatDistribution)) |
| 176 | + # Set step 0.0 for continuous search space. |
| 177 | + if dist.step is None or dist.log: |
| 178 | + steps[i] = 0.0 |
| 179 | + elif dist.low == dist.high: |
| 180 | + steps[i] = 1.0 |
| 181 | + else: |
| 182 | + steps[i] = dist.step / (dist.high - dist.low) |
| 183 | + |
| 184 | + return cmaes.CMAwM( |
| 185 | + mean=mu0, |
| 186 | + sigma=sigma0, |
| 187 | + bounds=trans.bounds, |
| 188 | + steps=steps, |
| 189 | + cov=cov0, |
| 190 | + seed=self._cma_rng.rng.randint(1, 2**31 - 2), |
| 191 | + n_max_resampling=10 * n_dimension, |
| 192 | + population_size=population_size, |
| 193 | + ) |
| 194 | + |
| 195 | + return cmaes.CMA( |
| 196 | + mean=mu0, |
| 197 | + sigma=sigma0, |
| 198 | + cov=cov0, |
| 199 | + bounds=trans.bounds, |
| 200 | + seed=self._cma_rng.rng.randint(1, 2**31 - 2), |
| 201 | + n_max_resampling=10 * n_dimension, |
| 202 | + population_size=population_size, |
| 203 | + lr_adapt=self._lr_adapt, |
| 204 | + ) |
0 commit comments