Skip to content

Commit

Permalink
Aligh package's readme to the template one
Browse files Browse the repository at this point in the history
  • Loading branch information
HideakiImamura committed May 17, 2024
1 parent 07f0e8a commit 2ae95fb
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 197 deletions.
34 changes: 7 additions & 27 deletions package/samplers/demo/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,36 +3,16 @@ author: 'Optuna team'
title: 'Demo Sampler'
description: 'Demo Sampler of OptunaHub'
tags: ['sampler']
optuna_versions: [3.6.1]
license: 'MIT'
optuna_versions: ['3.6.1']
license: 'MIT License'
---

This package provides a demo sampler of OptunaHub.

## class DemoSampler(seed)

### Parameters
- `seed: int` - A random seed

## Class or Function Names
- DemoSampler

## Example

```python
import optuna
import optunahub


def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x


if __name__ == "__main__":
module = optunahub.load_module("samplers/demo")
sampler = module.DemoSampler(seed=42)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=5)

print(study.best_trial)
module = optunahub.load_module("samplers/demo")
sampler = module.DemoSampler(seed=42)
```
See `example.py <example.py>`_ for more details.
17 changes: 17 additions & 0 deletions package/samplers/demo/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import optuna
import optunahub


def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x


if __name__ == "__main__":
module = optunahub.load_module("samplers/demo")
sampler = module.DemoSampler(seed=42)
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=5)

print(study.best_trial)
78 changes: 9 additions & 69 deletions package/samplers/simple/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,82 +3,22 @@ author: 'Optuna team'
title: 'Simple Sampler'
description: 'An easy sampler base class to implement custom samplers.'
tags: ['sampler', 'development']
optuna_versions: [3.6.1]
license: 'MIT'
optuna_versions: ['3.6.1']
license: 'MIT License'
---

Simple Sampler
===

This package provides an easy sampler base class to implement custom samplers.
You can make your own sampler easily by inheriting `SimpleSampler` and by implementing necessary methods.


# SimpleSampler

```python
class SimpleSampler(search_space)
```


## Parameters
- `search_space (dict[str, BaseDistribution])` - A search space for the objective function.

## Class or Function Names
- SimpleSampler

## Example

```python
from typing import Any

import numpy as np
import optuna
import optunahub
from optuna import Study
from optuna.distributions import BaseDistribution
from optuna.distributions import FloatDistribution
from optuna.distributions import IntDistribution
from optuna.samplers import BaseSampler
from optuna.trial import FrozenTrial


class UserDefinedSampler(
optunahub.load_module("samplers/simple").SimpleSampler
):
def __init__(self, search_space: dict[str, BaseDistribution]) -> None:
super().__init__(search_space)
self._rng = np.random.RandomState()

def sample_relative(
self,
study: Study,
trial: FrozenTrial,
search_space: dict[str, BaseDistribution],
) -> dict[str, Any]:
params = {}
for n, d in search_space.items():
if isinstance(d, FloatDistribution):
params[n] = self._rng.uniform(d.low, d.high)
elif isinstance(d, IntDistribution):
params[n] = self._rng.randint(d.low, d.high)
else:
raise ValueError("Unsupported distribution")
return params


if __name__ == "__main__":

def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x

sampler = UserDefinedSampler({"x": FloatDistribution(0, 1)})
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=20)

print(study.best_trial.value, study.best_trial.params)
...
```
See `example.py <example.py>`_ for more details.

# Author Information

This package is contributed by [Optuna team](https://github.com/orgs/optuna/people).
## Others
This package provides an easy sampler base class to implement custom samplers.
You can make your own sampler easily by inheriting `SimpleSampler` and by implementing necessary methods.
46 changes: 46 additions & 0 deletions package/samplers/simple/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
from typing import Any

import numpy as np
import optuna
from optuna import Study
from optuna.distributions import BaseDistribution
from optuna.distributions import FloatDistribution
from optuna.distributions import IntDistribution
from optuna.trial import FrozenTrial
import optunahub


class UserDefinedSampler(optunahub.load_module("samplers/simple").SimpleSampler): # type: ignore
def __init__(self, search_space: dict[str, BaseDistribution]) -> None:
super().__init__(search_space)
self._rng = np.random.RandomState()

def sample_relative(
self,
study: Study,
trial: FrozenTrial,
search_space: dict[str, BaseDistribution],
) -> dict[str, Any]:
params = {}
for n, d in search_space.items():
if isinstance(d, FloatDistribution):
params[n] = self._rng.uniform(d.low, d.high)
elif isinstance(d, IntDistribution):
params[n] = self._rng.randint(d.low, d.high)
else:
raise ValueError("Unsupported distribution")
return params


if __name__ == "__main__":

def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x

sampler = UserDefinedSampler({"x": FloatDistribution(0, 1)})
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=20)

print(study.best_trial.value, study.best_trial.params)
52 changes: 10 additions & 42 deletions package/samplers/simulated_annealing/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,52 +3,20 @@ author: 'Optuna team'
title: 'Simulated Annealing Sampler'
description: 'Sampler based on simulated annealing algorithm.'
tags: ['sampler', 'simulated annealing']
optuna_versions: [3.5, 3.6]
license: 'MIT'
optuna_versions: ['3.5.0', '3.6.0']
license: 'MIT License'
---

Simulated Annealing Sampler
===

This package provides a sampler based on Simulated Annealing algorithm.
For more details, see [the documentation](https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/005_user_defined_sampler.html).


# SimulatedAnnealingSampler

```python
class SimulatedAnnealingSampler(temperature=100)
```

Sampler based on Simulated Annealing algorithm.

## Parameters
- `temperature=100 (int)` - A temperature parameter for simulated annealing.

## Class or Function Names
- SimulatedAnnealingSampler

## Example

```python
import optunahub
import optuna


def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x


if __name__ == "__main__":
mod = optunahub.load_module("samplers/simulated_annealing")

sampler = mod.SimulatedAnnealingSampler()
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=20)

print(study.best_trial.value, study.best_trial.params)
mod = optunahub.load_module("samplers/simulated_annealing")
sampler = mod.SimulatedAnnealingSampler()
```
See `example.py <example.py>`_ for more details.

# Author Information

This package is contributed by [Optuna team](https://github.com/orgs/optuna/people).
## Others
This package provides a sampler based on Simulated Annealing algorithm.
For more details, see [the documentation](https://optuna.readthedocs.io/en/stable/tutorial/20_recipes/005_user_defined_sampler.html).
18 changes: 18 additions & 0 deletions package/samplers/simulated_annealing/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import optuna
import optunahub


def objective(trial: optuna.Trial) -> float:
x = trial.suggest_float("x", 0, 1)

return x


if __name__ == "__main__":
mod = optunahub.load_module("samplers/simulated_annealing")

sampler = mod.SimulatedAnnealingSampler()
study = optuna.create_study(sampler=sampler)
study.optimize(objective, n_trials=20)

print(study.best_trial.value, study.best_trial.params)
68 changes: 9 additions & 59 deletions package/visualization/plot_hypervolume_history_with_rp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,69 +3,19 @@ author: 'Optuna team'
title: 'Plot Hypervolume History with Reference Point'
description: 'Plot hypervolume history with the reference point information.'
tags: ['visualization', 'hypervolume', 'multi-objective optimization']
optuna_versions: ['3.6']
license: MIT
optuna_versions: ['3.6.0']
license: 'MIT License'
---

Plot Hypervolume History with Reference Point
===
## Class or Function Names
- plot_hypervolume_history

This package provides a function to plot hypervolume history with the reference point information.

# plot_hypervolume_history
## Example
```python
def plot_hypervolume_history(study, reference_point) -> "go.Figure"
```

Plot hypervolume history of all trials in a study.


## Parameters
- `study (Study)` – A Study object whose trials are plotted for their hypervolumes. The number of objectives must be 2 or more.
- `reference_point (Sequence[float])` – A reference point to use for hypervolume computation. The dimension of the reference point must be the same as the number of objectives.


## Returns
- A `plotly.graph_objects.Figure` object.


## Return type
- `Figure`


# Example

The following code snippet shows how to plot optimization history.


```python
import optuna
import optunahub


def objective(trial):
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)

v0 = 4 * x ** 2 + 4 * y ** 2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1


if __name__ == "__main__":
mod = optunahub.load_module("visualization/plot_hypervolume_history_with_rp")

study = optuna.create_study(directions=["minimize", "minimize"])
study.optimize(objective, n_trials=50)

reference_point=[100., 50.]
fig = mod.plot_hypervolume_history(study, reference_point)
fig.show()
mod = optunahub.load_module("visualization/plot_hypervolume_history_with_rp")
mod.plot_hypervolume_history(study, reference_point)
```
See `example.py <example.py>`_ for more details.
The example of generated image is as follows.

![Example](images/example.png "Example")


# Author Information

This package is contributed by [Optuna team](https://github.com/orgs/optuna/people).
22 changes: 22 additions & 0 deletions package/visualization/plot_hypervolume_history_with_rp/example.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import optuna
import optunahub


def objective(trial: optuna.trial.Trial) -> tuple[float, float]:
x = trial.suggest_float("x", 0, 5)
y = trial.suggest_float("y", 0, 3)

v0 = 4 * x**2 + 4 * y**2
v1 = (x - 5) ** 2 + (y - 5) ** 2
return v0, v1


if __name__ == "__main__":
mod = optunahub.load_module("visualization/plot_hypervolume_history_with_rp")

study = optuna.create_study(directions=["minimize", "minimize"])
study.optimize(objective, n_trials=50)

reference_point = [100.0, 50.0]
fig = mod.plot_hypervolume_history(study, reference_point)
fig.show()

0 comments on commit 2ae95fb

Please sign in to comment.