diff --git a/package/samplers/nsgaii_with_tpe_warmup/LICENSE b/package/samplers/nsgaii_with_tpe_warmup/LICENSE new file mode 100644 index 00000000..f6547d35 --- /dev/null +++ b/package/samplers/nsgaii_with_tpe_warmup/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Shuhei Watanabe + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/package/samplers/nsgaii_with_tpe_warmup/README.md b/package/samplers/nsgaii_with_tpe_warmup/README.md new file mode 100644 index 00000000..fe66e995 --- /dev/null +++ b/package/samplers/nsgaii_with_tpe_warmup/README.md @@ -0,0 +1,40 @@ +--- +author: Shuhei Watanabe +title: NSGAIISampler Using TPESampler for the Initialization +description: This sampler uses TPESampler for the initialization to warm start. +tags: [sampler, tpe, nsgaii, warmstart] +optuna_versions: [4.1.0] +license: MIT License +--- + +## Abstract + +This sampler uses `TPESampler` instead of `RandomSampler` for the initialization of `NSGAIISampler`. + +## APIs + +- NSGAIIWithTPEWarmupSampler + +This class takes the identical interface as the Optuna [`NSGAIISampler`](https://optuna.readthedocs.io/en/stable/reference/samplers/generated/optuna.samplers.NSGAIISampler.html). + +## Example + +```python +from __future__ import annotations + +import optuna +import optunahub + + +def objective(trial: optuna.Trial) -> tuple[float, float]: + x = trial.suggest_float("x", -5, 5) + y = trial.suggest_float("y", -5, 5) + return x**2 + y**2, (x - 2) ** 2 + (y - 2) ** 2 + + +package_name = "samplers/nsgaii_with_tpe_warmup" +sampler = optunahub.load_module(package=package_name).NSGAIIWithTPEWarmupSampler() +study = optuna.create_study(sampler=sampler, directions=["minimize"]*2) +study.optimize(objective, n_trials=60) + +``` diff --git a/package/samplers/nsgaii_with_tpe_warmup/__init__.py b/package/samplers/nsgaii_with_tpe_warmup/__init__.py new file mode 100644 index 00000000..444f6f81 --- /dev/null +++ b/package/samplers/nsgaii_with_tpe_warmup/__init__.py @@ -0,0 +1,35 @@ +from __future__ import annotations + +from typing import Any +from typing import TYPE_CHECKING + +from optuna.samplers import NSGAIISampler +from optuna.samplers import TPESampler +from optuna.samplers.nsgaii._sampler import _GENERATION_KEY + + +if TYPE_CHECKING: + from optuna import Study + from optuna.distributions import BaseDistribution + from optuna.trial import FrozenTrial + + +MAX_INT32 = (1 << 31) - 1 + + +class NSGAIIWithTPEWarmupSampler(NSGAIISampler): + def sample_relative( + self, + study: Study, + trial: FrozenTrial, + search_space: dict[str, BaseDistribution], + ) -> dict[str, Any]: + if trial.number < self._population_size: + seed = self._rng.rng.randint(MAX_INT32) + sampler = TPESampler( + multivariate=True, constraints_func=self._constraints_func, seed=seed + ) + study._storage.set_trial_system_attr(trial._trial_id, _GENERATION_KEY, 0) + return sampler.sample_relative(study, trial, search_space) + + return super().sample_relative(study, trial, search_space) diff --git a/package/samplers/nsgaii_with_tpe_warmup/example.py b/package/samplers/nsgaii_with_tpe_warmup/example.py new file mode 100644 index 00000000..07d7a3fe --- /dev/null +++ b/package/samplers/nsgaii_with_tpe_warmup/example.py @@ -0,0 +1,16 @@ +from __future__ import annotations + +import optuna +import optunahub + + +def objective(trial: optuna.Trial) -> tuple[float, float]: + x = trial.suggest_float("x", -5, 5) + y = trial.suggest_float("y", -5, 5) + return x**2 + y**2, (x - 2) ** 2 + (y - 2) ** 2 + + +package_name = "samplers/nsgaii_with_tpe_warmup" +sampler = optunahub.load_module(package=package_name).NSGAIIWithTPEWarmupSampler() +study = optuna.create_study(sampler=sampler, directions=["minimize"] * 2) +study.optimize(objective, n_trials=60)