-
Notifications
You must be signed in to change notification settings - Fork 14
Introduce the optunahub.benchmarks module
#73
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
b2f0823
Add optunahub.benchmarks docs
y0z 40f1312
Add test_benchmarks
y0z 141d018
Update
y0z 12b6ebd
Add __future__ annotations
y0z 65e24c5
Update optunahub/benchmarks/_base_problem.py
y0z 8f88418
Update optunahub/benchmarks/_base_problem.py
y0z acec775
Update tests/test_benchmarks.py
y0z File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| {{ fullname | escape | underline }} | ||
|
|
||
| .. autoclass:: {{ fullname }} | ||
| :members: | ||
| :special-members: __init__, __call__ | ||
| :inherited-members: | ||
| :undoc-members: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| .. module:: optunahub.benchmarks | ||
|
|
||
| optunahub.benchmarks | ||
| ==================== | ||
|
|
||
| .. autosummary:: | ||
| :toctree: generated/ | ||
| :nosignatures: | ||
| :template: custom_summary.rst | ||
|
|
||
| optunahub.benchmarks.BaseProblem |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,4 +5,5 @@ Reference | |
| :maxdepth: 1 | ||
|
|
||
| optunahub | ||
| samplers | ||
| samplers | ||
| benchmarks | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,8 @@ | ||
| from optunahub import benchmarks | ||
| from optunahub import samplers | ||
| from optunahub.hub import load_local_module | ||
| from optunahub.hub import load_module | ||
| from optunahub.version import __version__ | ||
|
|
||
|
|
||
| __all__ = ["load_module", "load_local_module", "__version__", "samplers"] | ||
| __all__ = ["__version__", "benchmarks", "load_local_module", "load_module", "samplers"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| from ._base_problem import BaseProblem | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "BaseProblem", | ||
| ] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from abc import ABCMeta | ||
| from abc import abstractmethod | ||
| from typing import Any | ||
| from typing import Sequence | ||
|
|
||
| import optuna | ||
|
|
||
|
|
||
| class BaseProblem(metaclass=ABCMeta): | ||
| """Base class for optimization problems.""" | ||
|
|
||
| def __call__(self, trial: optuna.Trial) -> float | Sequence[float]: | ||
| """Objective function for Optuna. By default, this method calls :meth:`evaluate` with the parameters defined in :attr:`search_space`. | ||
|
|
||
| Args: | ||
| trial: Optuna trial object. | ||
| Returns: | ||
| The objective value or a sequence of the objective values for multi-objective optimization. | ||
| """ | ||
| params = {} | ||
| for name, dist in self.search_space.items(): | ||
| params[name] = trial._suggest(name, dist) | ||
| trial._check_distribution(name, dist) | ||
| return self.evaluate(params) | ||
|
|
||
| def evaluate(self, params: dict[str, Any]) -> float | Sequence[float]: | ||
| """Evaluate the objective function. | ||
|
|
||
| Args: | ||
| params: Dictionary of input parameters. | ||
|
|
||
| Returns: | ||
| The objective value or a sequence of the objective values for multi-objective optimization. | ||
|
|
||
| Example: | ||
| :: | ||
|
|
||
| def evaluate(self, params: dict[str, Any]) -> float: | ||
| x = params["x"] | ||
| y = params["y"] | ||
| return x ** 2 + y | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @property | ||
| def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there any reason why this method is not an abstract method?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ditto (cf. #73 (comment)) |
||
| """Return the search space. | ||
|
|
||
| Returns: | ||
| Dictionary of search space. Each dictionary element consists of the parameter name and distribution (see `optuna.distributions <https://optuna.readthedocs.io/en/stable/reference/distributions.html>`__). | ||
|
|
||
| Example: | ||
| :: | ||
|
|
||
| @property | ||
| def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: | ||
| return { | ||
| "x": optuna.distributions.FloatDistribution(low=0, high=1), | ||
| "y": optuna.distributions.CategoricalDistribution(choices=[0, 1, 2]), | ||
| } | ||
| """ | ||
| raise NotImplementedError | ||
|
|
||
| @property | ||
| @abstractmethod | ||
| def directions(self) -> list[optuna.study.StudyDirection]: | ||
| """Return the optimization directions. | ||
|
|
||
| Returns: | ||
| List of `optuna.study.direction <https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.StudyDirection.html>`__. | ||
|
|
||
| Example: | ||
| :: | ||
|
|
||
| @property | ||
| def directions(self) -> list[optuna.study.StudyDirection]: | ||
| return [optuna.study.StudyDirection.MINIMIZE] | ||
| """ | ||
| ... | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import optuna | ||
|
|
||
| import optunahub | ||
|
|
||
|
|
||
| def test_base_problem() -> None: | ||
| class TestProblem(optunahub.benchmarks.BaseProblem): | ||
| def evaluate(self, params: dict[str, float]) -> float: | ||
| x = params["x"] | ||
| return x**2 | ||
|
|
||
| @property | ||
| def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: | ||
| return {"x": optuna.distributions.FloatDistribution(low=-1, high=1)} | ||
|
|
||
| @property | ||
| def directions(self) -> list[optuna.study.StudyDirection]: | ||
| return [optuna.study.StudyDirection.MINIMIZE] | ||
|
|
||
| problem = TestProblem() | ||
| study = optuna.create_study(directions=problem.directions) | ||
| study.optimize(problem, n_trials=20) # verify no error occurs |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any reason why this method is not an abstract method?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If you only want to support Optuna (e.g., implementing define-by-run objective), it is acceptable to override
__call__and not implementevaluateandsearch_space.