From 2ae95fb89f1acf74402a62df0b24c7008a594ed5 Mon Sep 17 00:00:00 2001 From: mamu Date: Fri, 17 May 2024 10:36:19 +0900 Subject: [PATCH] Aligh package's readme to the template one --- package/samplers/demo/README.md | 34 ++------ package/samplers/demo/example.py | 17 ++++ package/samplers/simple/README.md | 78 +++---------------- package/samplers/simple/example.py | 46 +++++++++++ .../samplers/simulated_annealing/README.md | 52 +++---------- .../samplers/simulated_annealing/example.py | 18 +++++ .../README.md | 68 +++------------- .../example.py | 22 ++++++ 8 files changed, 138 insertions(+), 197 deletions(-) create mode 100644 package/samplers/demo/example.py create mode 100644 package/samplers/simple/example.py create mode 100644 package/samplers/simulated_annealing/example.py create mode 100644 package/visualization/plot_hypervolume_history_with_rp/example.py diff --git a/package/samplers/demo/README.md b/package/samplers/demo/README.md index ad82762f..996db37b 100644 --- a/package/samplers/demo/README.md +++ b/package/samplers/demo/README.md @@ -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 `_ for more details. diff --git a/package/samplers/demo/example.py b/package/samplers/demo/example.py new file mode 100644 index 00000000..78a42905 --- /dev/null +++ b/package/samplers/demo/example.py @@ -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) diff --git a/package/samplers/simple/README.md b/package/samplers/simple/README.md index c9dce0bf..96aecdc1 100644 --- a/package/samplers/simple/README.md +++ b/package/samplers/simple/README.md @@ -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 `_ 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. diff --git a/package/samplers/simple/example.py b/package/samplers/simple/example.py new file mode 100644 index 00000000..a4b9e565 --- /dev/null +++ b/package/samplers/simple/example.py @@ -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) diff --git a/package/samplers/simulated_annealing/README.md b/package/samplers/simulated_annealing/README.md index d32c0718..e76b9477 100644 --- a/package/samplers/simulated_annealing/README.md +++ b/package/samplers/simulated_annealing/README.md @@ -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 `_ 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). diff --git a/package/samplers/simulated_annealing/example.py b/package/samplers/simulated_annealing/example.py new file mode 100644 index 00000000..5022c027 --- /dev/null +++ b/package/samplers/simulated_annealing/example.py @@ -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) diff --git a/package/visualization/plot_hypervolume_history_with_rp/README.md b/package/visualization/plot_hypervolume_history_with_rp/README.md index 03f2adbf..649459a4 100644 --- a/package/visualization/plot_hypervolume_history_with_rp/README.md +++ b/package/visualization/plot_hypervolume_history_with_rp/README.md @@ -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 `_ 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). diff --git a/package/visualization/plot_hypervolume_history_with_rp/example.py b/package/visualization/plot_hypervolume_history_with_rp/example.py new file mode 100644 index 00000000..7bf1d314 --- /dev/null +++ b/package/visualization/plot_hypervolume_history_with_rp/example.py @@ -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()