-
Notifications
You must be signed in to change notification settings - Fork 14
Introduce ConstrainedMixin
#75
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 4 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
ba7cccb
Add ConstrainedMixIn
y0z 61b44f6
Add test
y0z 654a9ab
Update
y0z d7b5554
Rename MixIn to Mixin
y0z ab5db62
Update tests/test_benchmarks.py
y0z eb71a14
Update tests/test_benchmarks.py
y0z 127e345
Update optunahub/benchmarks/_constrained_mixin.py
y0z 1c1eede
Update optunahub/benchmarks/_constrained_mixin.py
y0z 92cb4a6
Update optunahub/benchmarks/_constrained_mixin.py
y0z 1e0905f
Update optunahub/benchmarks/_constrained_mixin.py
y0z 822cec4
Update optunahub/benchmarks/_constrained_mixin.py
y0z cd0a37c
Update optunahub/benchmarks/_constrained_mixin.py
y0z da2c017
Apply ruff
y0z 322c0d5
Apply ruff
y0z 1cbaf7f
Update optunahub/benchmarks/_constrained_mixin.py
nabenabe0928 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
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,6 +1,8 @@ | ||
| from ._base_problem import BaseProblem | ||
| from ._constrained_mixin import ConstrainedMixin | ||
|
|
||
|
|
||
| __all__ = [ | ||
| "BaseProblem", | ||
| "ConstrainedMixin", | ||
| ] |
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,78 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from typing import Any | ||
| from typing import Sequence | ||
|
|
||
| import optuna | ||
|
|
||
|
|
||
| class ConstrainedMixin: | ||
| """Mixin class for constrained optimization problems. | ||
| Example: | ||
| You can define a constrained optimization problem by inheriting this class and implementing | ||
| the :meth:`evaluate_constraints` method as follows. | ||
| :: | ||
| import optuna | ||
| import optunahub | ||
| class BinAndKorn(optunahub.benchmarks.ConstrainedMixin, optunahub.benchmarks.BaseProblem): | ||
| def evaluate(self, params: dict[str, float]) -> float: | ||
y0z marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| x = params["x"] | ||
| y = params["y"] | ||
| v0 = 4 * x ** 2 + 4 * y ** 2 | ||
| v1 = (x - 5) ** 2 + (y - 5) ** 2 | ||
y0z marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return v0, v1 | ||
| def evaluate_constraints(self, params: dict[str, float]) -> list[float]: | ||
y0z marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| x = params["x"] | ||
| y = params["y"] | ||
| # Constraints which are considered feasible if less than or equal to zero. | ||
| # The feasible region is basically the intersection of a circle centered at (x=5, y=0) | ||
| # and the complement to a circle centered at (x=8, y=-3). | ||
| c0 = (x - 5) ** 2 + y ** 2 - 25 | ||
| c1 = -((x - 8) ** 2) - (y + 3) ** 2 + 7.7 | ||
y0z marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| return [c0, c1] | ||
y0z marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| @property | ||
| def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: | ||
| return { | ||
| "x": optuna.distributions.FloatDistribution(low=-15, high=30), | ||
| "y": optuna.distributions.FloatDistribution(low=-15, high=30) | ||
| } | ||
| @property | ||
| def directions(self) -> list[optuna.study.StudyDirection]: | ||
| return [optuna.study.StudyDirection.MINIMIZE, optuna.study.StudyDirection.MINIMIZE] | ||
| problem = BinAndKorn() | ||
| sampler = optuna.samplers.TPESampler(constraints_func=problem.constraints_func) | ||
| study = optuna.create_study(sampler=sampler, directions=problem.directions) | ||
| study.optimize(problem, n_trials=20) | ||
| """ | ||
|
|
||
| def constraints_func(self, trial: optuna.trial.FrozenTrial) -> Sequence[float]: | ||
| """Evaluate the constraint functions. | ||
| Args: | ||
| trial: Optuna trial object. | ||
| Returns: | ||
| List of the constraint values. | ||
| """ | ||
| return self.evaluate_constraints(trial.params) | ||
nabenabe0928 marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| def evaluate_constraints(self, params: dict[str, Any]) -> Sequence[float]: | ||
| """Evaluate the constraint functions. | ||
| Args: | ||
| params: Dictionary of input parameters. | ||
| Returns: | ||
| List of the constraint values. | ||
| """ | ||
| raise NotImplementedError | ||
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
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.
Uh oh!
There was an error while loading. Please reload this page.