-
Notifications
You must be signed in to change notification settings - Fork 33
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #215 from y0z/feature/benchmarks-bbob-constrained
Add bbob-constrained
- Loading branch information
Showing
5 changed files
with
197 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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,21 @@ | ||
MIT License | ||
|
||
Copyright (c) 2024 Preferred Networks, Inc. | ||
|
||
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. |
This file contains 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,85 @@ | ||
--- | ||
author: Optuna team | ||
title: The blackbox optimization benchmarking-constrained (bbob-constrained) test suite | ||
description: The bbob-constrained test suite is a suite of 54 non-linearly constrained test functions with varying number of (active and inactive) constraints. This package is a wrapper of the COCO (COmparing Continuous Optimizers) experiments library. | ||
tags: [benchmarks, continuous optimization, constrained optimization, BBOB, COCO] | ||
optuna_versions: [4.1.0] | ||
license: MIT License | ||
--- | ||
|
||
## Abstract | ||
|
||
This package provides a wrapper of the COCO experiments libarary's bbob-constrained test suite. | ||
|
||
## APIs | ||
|
||
### class `Problem(function_id: int, dimension: int, instance_id: int = 1)` | ||
|
||
- `function_id`: [ID of the bbob-constrained benchmark function](https://numbbo.github.io/coco/testsuites/bbob-constrained) to use. It must be in the range of `[1, 54]`. | ||
- `dimension`: Dimension of the benchmark function. It must be in `[2, 3, 5, 10, 20, 40]`. | ||
- `instance_id`: ID of the instance of the benchmark function. It must be in the range of `[1, 15]`. | ||
|
||
#### Methods and Properties | ||
|
||
- `search_space`: Return the search space. | ||
- Returns: `dict[str, optuna.distributions.BaseDistribution]` | ||
- `directions`: Return the optimization directions. | ||
- Returns: `list[optuna.study.StudyDirection]` | ||
- `__call__(trial: optuna.Trial)`: Evaluate the objective function and return the objective value. | ||
- Args: | ||
- `trial`: Optuna trial object. | ||
- Returns: `float` | ||
- `evaluate(params: dict[str, float])`: Evaluate the objective function and return the objective value. | ||
- Args: | ||
- `params`: Decision variable like `{"x0": x1_value, "x1": x1_value, ..., "xn": xn_value}`. The number of parameters must be equal to `dimension`. | ||
- Returns: `float` | ||
- `constraints_func(trial: optuna.Trial.FrozenTrial)`: Evaluate the constraint functions and return the list of constraint functions values. | ||
- Args: | ||
- `trial`: Optuna trial object. | ||
- Returns: `list[float]` | ||
- `evaluate_constraints(params: dict[str, float])`: Evaluate the constraint functions and return the list of constraint functions values. | ||
- Args: | ||
- `params`: Decision variable like `{"x0": x1_value, "x1": x1_value, ..., "xn": xn_value}`. The number of parameters must be equal to `dimension`. | ||
- Returns: `list[float]` | ||
|
||
The properties defined by [cocoex.Problem](https://numbbo.github.io/coco-doc/apidocs/cocoex/cocoex.Problem.html) are also available such as `number_of_objectives`. | ||
|
||
## Installation | ||
|
||
Please install the [coco-experiment](https://github.com/numbbo/coco-experiment/tree/main/build/python) package. | ||
|
||
```shell | ||
pip install -U coco-experiment | ||
``` | ||
|
||
## Example | ||
|
||
```python | ||
import optuna | ||
import optunahub | ||
|
||
|
||
bbob_constrained = optunahub.load_module("benchmarks/bbob_constrained") | ||
constrained_sphere2d = bbob_constrained.Problem(function_id=1, dimension=2, instance_id=1) | ||
|
||
study = optuna.create_study( | ||
sampler=optuna.samplers.TPESampler( | ||
constraints_func=constrained_sphere2d.constraints_func | ||
), | ||
directions=constrained_sphere2d.directions | ||
) | ||
study.optimize(constrained_sphere2d, n_trials=20) | ||
|
||
try: | ||
print(study.best_trial.params, study.best_trial.value) | ||
except Exception as e: | ||
print(e) | ||
``` | ||
|
||
## Details of Benchmark Functions | ||
|
||
Please refer to [the paper](https://numbbo.github.io/coco-doc/bbob-constrained/functions.pdf) for details about each benchmark function. | ||
|
||
## Reference | ||
|
||
Paul Dufossé, Nikolaus Hansen, Dimo Brockhoff, Phillipe R. Sampaio, Asma Atamna, and Anne Auger. [Building scalable test problems for benchmarking constrained optimizers. 2022. To be submitted to the SIAM Journal of Optimization](https://numbbo.github.io/coco-doc/bbob-constrained/functions.pdf). |
This file contains 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,4 @@ | ||
from ._bbob_constrained import Problem | ||
|
||
|
||
__all__ = ["Problem"] |
This file contains 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,86 @@ | ||
from __future__ import annotations | ||
|
||
from typing import Any | ||
|
||
import cocoex as ex | ||
import optuna | ||
import optunahub | ||
|
||
|
||
class Problem(optunahub.benchmarks.ConstrainedMixin, optunahub.benchmarks.BaseProblem): | ||
"""Wrapper class for COCO bbob-constrained test suite. | ||
https://coco-platform.org/ | ||
The detail is described in the following paper. | ||
Paul Dufossé, Nikolaus Hansen, Dimo Brockhoff, Phillipe R. Sampaio, Asma Atamna, and Anne Auger. | ||
Building scalable test problems for benchmarking constrained optimizers. 2022. To be submitted to the SIAM Journal of Optimization. | ||
https://numbbo.github.io/coco-doc/bbob-constrained/functions.pdf | ||
""" | ||
|
||
def __init__(self, function_id: int, dimension: int, instance_id: int = 1): | ||
"""Initialize the problem. | ||
Args: | ||
function_id: Function index in [1, 54]. | ||
dimension: Dimension of the problem in [2, 3, 5, 10, 20, 40]. | ||
instance_id: Instance index in [1, 15]. | ||
Please refer to the COCO documentation for the details of the available properties. | ||
https://numbbo.github.io/coco-doc/apidocs/cocoex/cocoex.Problem.html | ||
""" | ||
|
||
assert 1 <= function_id <= 54, "function_id must be in [1, 54]" | ||
assert dimension in [2, 3, 5, 10, 20, 40], "dimension must be in [2, 3, 5, 10, 20, 40]" | ||
assert 1 <= instance_id <= 15, "instance_id must be in [1, 15]" | ||
|
||
self._problem = ex.Suite( | ||
"bbob-constrained", "", "" | ||
).get_problem_by_function_dimension_instance( | ||
function=function_id, dimension=dimension, instance=instance_id | ||
) | ||
self._search_space = { | ||
f"x{i}": optuna.distributions.FloatDistribution( | ||
low=self._problem.lower_bounds[i], | ||
high=self._problem.upper_bounds[i], | ||
) | ||
for i in range(self._problem.dimension) | ||
} | ||
|
||
@property | ||
def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: | ||
"""Return the search space.""" | ||
return self._search_space.copy() | ||
|
||
@property | ||
def directions(self) -> list[optuna.study.StudyDirection]: | ||
"""Return the optimization directions.""" | ||
return [optuna.study.StudyDirection.MINIMIZE] | ||
|
||
def evaluate(self, params: dict[str, float]) -> float: | ||
"""Evaluate the objective function. | ||
Args: | ||
params: | ||
Decision variable, e.g., evaluate({"x0": 1.0, "x1": 2.0}). | ||
The number of parameters must be equal to the dimension of the problem. | ||
Returns: | ||
The objective value. | ||
""" | ||
return self._problem([params[name] for name in self._search_space]) | ||
|
||
def evaluate_constraints(self, params: dict[str, float]) -> list[float]: | ||
"""Evaluate the constraint functions. | ||
Args: | ||
params: | ||
Decision variable, e.g., evaluate_constraints({"x0": 1.0, "x1": 2.0}). | ||
The number of parameters must be equal to the dimension of the problem. | ||
Returns: | ||
The constraint functions values. | ||
""" | ||
return self._problem.constraint([params[name] for name in self._search_space]) | ||
|
||
def __getattr__(self, name: str) -> Any: | ||
return getattr(self._problem, name) | ||
|
||
def __del__(self) -> None: | ||
self._problem.free() |
This file contains 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 @@ | ||
coco-experiment |