Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add NSGAIISampler with TPESampler warmup #188

Merged
merged 2 commits into from
Nov 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions package/samplers/nsgaii_with_tpe_warmup/LICENSE
Original file line number Diff line number Diff line change
@@ -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.
40 changes: 40 additions & 0 deletions package/samplers/nsgaii_with_tpe_warmup/README.md
Original file line number Diff line number Diff line change
@@ -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)

```
35 changes: 35 additions & 0 deletions package/samplers/nsgaii_with_tpe_warmup/__init__.py
Original file line number Diff line number Diff line change
@@ -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)
16 changes: 16 additions & 0 deletions package/samplers/nsgaii_with_tpe_warmup/example.py
Original file line number Diff line number Diff line change
@@ -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)