From ea70e5f17bfb19c168aaafebf720c4804ee5663c Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Fri, 22 Nov 2024 04:19:05 +0100 Subject: [PATCH 1/2] Add NSGAII with TPE init --- .../samplers/nsgaii_with_tpe_warmup/LICENSE | 21 ++++++++++ .../samplers/nsgaii_with_tpe_warmup/README.md | 40 +++++++++++++++++++ .../nsgaii_with_tpe_warmup/__init__.py | 35 ++++++++++++++++ .../nsgaii_with_tpe_warmup/example.py | 16 ++++++++ 4 files changed, 112 insertions(+) create mode 100644 package/samplers/nsgaii_with_tpe_warmup/LICENSE create mode 100644 package/samplers/nsgaii_with_tpe_warmup/README.md create mode 100644 package/samplers/nsgaii_with_tpe_warmup/__init__.py create mode 100644 package/samplers/nsgaii_with_tpe_warmup/example.py 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..c03445c6 --- /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) +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..6a98ef1f --- /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) +study.optimize(objective, n_trials=60) From e75c251bc2fb11e76a961fae86860a6eb08f3377 Mon Sep 17 00:00:00 2001 From: nabenabe0928 Date: Fri, 22 Nov 2024 04:24:11 +0100 Subject: [PATCH 2/2] Fix example --- package/samplers/nsgaii_with_tpe_warmup/README.md | 2 +- package/samplers/nsgaii_with_tpe_warmup/example.py | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/package/samplers/nsgaii_with_tpe_warmup/README.md b/package/samplers/nsgaii_with_tpe_warmup/README.md index c03445c6..fe66e995 100644 --- a/package/samplers/nsgaii_with_tpe_warmup/README.md +++ b/package/samplers/nsgaii_with_tpe_warmup/README.md @@ -34,7 +34,7 @@ def objective(trial: optuna.Trial) -> tuple[float, float]: package_name = "samplers/nsgaii_with_tpe_warmup" sampler = optunahub.load_module(package=package_name).NSGAIIWithTPEWarmupSampler() -study = optuna.create_study(sampler=sampler) +study = optuna.create_study(sampler=sampler, directions=["minimize"]*2) study.optimize(objective, n_trials=60) ``` diff --git a/package/samplers/nsgaii_with_tpe_warmup/example.py b/package/samplers/nsgaii_with_tpe_warmup/example.py index 6a98ef1f..07d7a3fe 100644 --- a/package/samplers/nsgaii_with_tpe_warmup/example.py +++ b/package/samplers/nsgaii_with_tpe_warmup/example.py @@ -12,5 +12,5 @@ def objective(trial: optuna.Trial) -> tuple[float, float]: package_name = "samplers/nsgaii_with_tpe_warmup" sampler = optunahub.load_module(package=package_name).NSGAIIWithTPEWarmupSampler() -study = optuna.create_study(sampler=sampler) +study = optuna.create_study(sampler=sampler, directions=["minimize"] * 2) study.optimize(objective, n_trials=60)