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 HEBO sampler #78

Merged
merged 4 commits into from
Jun 21, 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/hebo/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Hideaki Imamura

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.
42 changes: 42 additions & 0 deletions package/samplers/hebo/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
---
author: HideakiImamura
title: HEBO (Heteroscedastic and Evolutionary Bayesian Optimisation)
description: HEBO addresses the problem of noisy and heterogeneous objective functions by using a heteroscedastic Gaussian process and an evolutionary algorithm.
tags: ["sampler", "Bayesian optimization", "Heteroscedastic Gaussian process", "Evolutionary algorithm"]
optuna_versions: ["3.6.1"]
license: "MIT License"
---

## Class or Function Names
- HEBOSampler

## Installation
```bash
pip install -r requirements.txt
git clone [email protected]:huawei-noah/HEBO.git
cd HEBO/HEBO
pip install -e .
```

## Example
```python
search_space = {
"x": FloatDistribution(-10, 10),
"y": IntDistribution(0, 10),

}
sampler = HEBOSampler(search_space)
study = optuna.create_study(sampler=sampler)
```
See [`example.py`](https://github.com/optuna/optunahub-registry/blob/main/package/samplers/hebo/example.py) for a full example.
![History Plot](images/hebo_optimization_history.png "History Plot")


## Others

HEBO is the winning submission to the [NeurIPS 2020 Black-Box Optimisation Challenge](https://bbochallenge.com/leaderboard).
Please refer to [the official repository of HEBO](https://github.com/huawei-noah/HEBO/tree/master/HEBO) for more details.

### Reference

Cowen-Rivers, Alexander I., et al. "An Empirical Study of Assumptions in Bayesian Optimisation." arXiv preprint arXiv:2012.03826 (2021).
4 changes: 4 additions & 0 deletions package/samplers/hebo/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from .sampler import HEBOSampler


__all__ = ["HEBOSampler"]
27 changes: 27 additions & 0 deletions package/samplers/hebo/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import optuna
import optunahub


module = optunahub.load_module("samplers/hebo")
HEBOSampler = module.HEBOSampler


def objective(trial: optuna.trial.Trial) -> float:
x = trial.suggest_float("x", -10, 10)
y = trial.suggest_int("y", -10, 10)
return x**2 + y**2


if __name__ == "__main__":
sampler = HEBOSampler(
{
"x": optuna.distributions.FloatDistribution(-10, 10),
"y": optuna.distributions.IntDistribution(-10, 10),
}
)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=100)
print(study.best_trial.params)

fig = optuna.visualization.plot_optimization_history(study)
fig.write_image("hebo_optimization_history.png")
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions package/samplers/hebo/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
optuna
optunahub
hebo@git+https://github.com/huawei-noah/HEBO.git#subdirectory=HEBO
94 changes: 94 additions & 0 deletions package/samplers/hebo/sampler.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
from __future__ import annotations

from optuna.distributions import BaseDistribution
from optuna.distributions import CategoricalDistribution
from optuna.distributions import FloatDistribution
from optuna.distributions import IntDistribution
from optuna.study import Study
from optuna.trial import FrozenTrial
import optunahub

from hebo.design_space.design_space import DesignSpace
from hebo.optimizers.hebo import HEBO


SimpleSampler = optunahub.load_module("samplers/simple").SimpleSampler


class HEBOSampler(SimpleSampler): # type: ignore
def __init__(self, search_space: dict[str, BaseDistribution]) -> None:
super().__init__(search_space)
self._hebo = HEBO(self._convert_to_hebo_design_space(search_space))

def sample_relative(
self, study: Study, trial: FrozenTrial, search_space: dict[str, BaseDistribution]
) -> dict[str, float]:
params_pd = self._hebo.suggest()

params = {}
for name in search_space.keys():
params[name] = params_pd[name].to_numpy()[0]
return params

def _convert_to_hebo_design_space(
self, search_space: dict[str, BaseDistribution]
) -> DesignSpace:
design_space = []
for name, distribution in search_space.items():
if isinstance(distribution, FloatDistribution) and not distribution.log:
design_space.append(
{
"name": name,
"type": "num",
"lb": distribution.low,
"ub": distribution.high,
}
)
elif isinstance(distribution, FloatDistribution) and distribution.log:
design_space.append(
{
"name": name,
"type": "pow",
"lb": distribution.low,
"ub": distribution.high,
}
)
elif isinstance(distribution, IntDistribution) and distribution.log:
design_space.append(
{
"name": name,
"type": "pow_int",
"lb": distribution.low,
"ub": distribution.high,
}
)
elif isinstance(distribution, IntDistribution) and distribution.step:
design_space.append(
{
"name": name,
"type": "step_int",
"lb": distribution.low,
"ub": distribution.high,
"step": distribution.step,
}
)
elif isinstance(distribution, IntDistribution):
design_space.append(
{
"name": name,
"type": "int",
"lb": distribution.low,
"ub": distribution.high,
}
)
elif isinstance(distribution, CategoricalDistribution):
design_space.append(
{
"name": name,
"type": "cat",
"categories": distribution.choices,
}
)
else:
raise NotImplementedError(f"Unsupported distribution: {distribution}")
return DesignSpace().parse(design_space)