|
| 1 | +How to Use Benchmarks |
| 2 | +===================== |
| 3 | + |
| 4 | +OptunaHub provides various benchmarks, and you can utilize them through a unified interface. |
| 5 | +In this tutorial, we will explain how to use benchmarks in OptunaHub. |
| 6 | +If you are interested in registering your own benchmark problems, please check `Basic <https://optuna.github.io/optunahub/recipes/006_benchmarks_basic.html>`_ and `Advanced <https://optuna.github.io/optunahub/recipes/007_benchmarks_advanced.html>`_ tutorials. |
| 7 | + |
| 8 | +The following blog post also provides an overview of this feature: |
| 9 | + |
| 10 | +- `OptunaHub Benchmarks: A New Feature to Use/Register Various Benchmark Problems <https://medium.com/optuna/optunahub-benchmarks-a-new-feature-to-use-register-various-benchmark-problems-694401524ce0>`__ |
| 11 | + |
| 12 | +Preparation |
| 13 | +----------- |
| 14 | + |
| 15 | +First, ensure the necessary packages are installed by executing the following command: |
| 16 | + |
| 17 | +.. code-block:: console |
| 18 | +
|
| 19 | + $ pip install optuna optunahub |
| 20 | +
|
| 21 | +Examples |
| 22 | +-------- |
| 23 | + |
| 24 | +We will use the `black-box optimization benchmarking (bbob) test suite <https://hub.optuna.org/benchmarks/bbob/>`__ in this tutorial. |
| 25 | +This is a wrapper of `COCO (COmparing Continuous Optimizers) experiment <https://github.com/numbbo/coco-experiment>`__ library. |
| 26 | +So you need to install COCO first. |
| 27 | + |
| 28 | +.. code-block:: console |
| 29 | +
|
| 30 | + $ pip install coco-experiment |
| 31 | +
|
| 32 | +
|
| 33 | +Test code is as follows: |
| 34 | + |
| 35 | +.. code-block:: python |
| 36 | +
|
| 37 | + import optuna |
| 38 | + import optunahub |
| 39 | +
|
| 40 | +
|
| 41 | + bbob = optunahub.load_module("benchmarks/bbob") |
| 42 | + sphere2d = bbob.Problem(function_id=1, dimension=2, instance_id=1) |
| 43 | +
|
| 44 | + study = optuna.create_study(directions=sphere2d.directions, sampler=optuna.samplers.TPESampler(seed=42)) |
| 45 | + study.optimize(sphere2d, n_trials=100) |
| 46 | +
|
| 47 | + optuna.visualization.plot_optimization_history(study).show() |
| 48 | +
|
| 49 | +.. figure:: ./images/optimization_history.png |
| 50 | + :alt: Optimization History |
| 51 | + :align: center |
| 52 | + :width: 800px |
| 53 | + |
| 54 | + |
| 55 | +You can also use other optimizing frameworks to optimize the problem. |
| 56 | +:class:`~optunahub.benchmarks.BaseProblem` provides :meth:`~optunahub.benchmarks.BaseProblem.__call__` and :meth:`~optunahub.benchmarks.BaseProblem.evaluate` methods, which is used to evaluate the objective function. |
| 57 | +:meth:`~optunahub.benchmarks.BaseProblem.__call__` takes an :class:`optuna.Trial` object, while :meth:`~optunahub.benchmarks.BaseProblem.evaluate` takes a dictionary of input parameters. |
| 58 | +Therefore, you can use :meth:`~optunahub.benchmarks.BaseProblem.evaluate` to optimize the problem with other optimizing frameworks. |
| 59 | +Here, we use `scipy.optimize.minimize <https://docs.scipy.org/doc/scipy/reference/generated/scipy.optimize.minimize.html>`__ as an example. |
| 60 | +The properties ``initial_solution``, ``lower_bounds``, and ``upper_bounds`` are provided by `the bbob package <https://hub.optuna.org/benchmarks/bbob/>`__. |
| 61 | + |
| 62 | +.. code-block:: python |
| 63 | + |
| 64 | + import optunahub |
| 65 | + import scipy |
| 66 | +
|
| 67 | +
|
| 68 | + bbob = optunahub.load_module("benchmarks/bbob") |
| 69 | + sphere2d = bbob.Problem(function_id=1, dimension=2, instance_id=1) |
| 70 | + result = scipy.optimize.minimize( |
| 71 | + fun=lambda x: sphere2d.evaluate({f"x{d}": x[d] for d in range(sphere2d.dimension)}), |
| 72 | + x0=sphere2d.initial_solution, |
| 73 | + bounds=scipy.optimize.Bounds( |
| 74 | + lb=sphere2d.lower_bounds, ub=sphere2d.upper_bounds |
| 75 | + ) |
| 76 | + ) |
| 77 | +
|
| 78 | +
|
| 79 | +Constrained Problem |
| 80 | +^^^^^^^^^^^^^^^^^^^ |
| 81 | + |
| 82 | +Some benchmarks also include constraints. |
| 83 | +These problems are implemented by inheriting :class:`~optunahub.benchmarks.ConstrainedMixin` class. |
| 84 | +:class:`~optunahub.benchmarks.ConstrainedMixin` provides :meth:`~optunahub.benchmarks.ConstrainedMixin.evaluate_constraints` and :meth:`~optunahub.benchmarks.ConstrainedMixin.constraints_func` methods. |
| 85 | +As same as objective functions, :meth:`~optunahub.benchmarks.ConstrainedMixin.constraints_func` takes an :class:`optuna.Trial` object, while :meth:`~optunahub.benchmarks.ConstrainedMixin.evaluate_constraints` takes a dictionary of input parameters. |
| 86 | +Those methods are used to evaluate the constraint functions. |
| 87 | +You can optimize these problems in the same way as usual, but you need to set the ``constraints_func`` argument in the sampler. |
| 88 | + |
| 89 | +.. code-block:: python |
| 90 | +
|
| 91 | + import optuna |
| 92 | + import optunahub |
| 93 | + import matplotlib.pyplot as plt |
| 94 | +
|
| 95 | + bbob_constrained = optunahub.load_module("benchmarks/bbob_constrained") |
| 96 | + constrained_sphere2d = bbob_constrained.Problem(function_id=1, dimension=2, instance_id=1) |
| 97 | +
|
| 98 | + study = optuna.create_study( |
| 99 | + sampler=optuna.samplers.TPESampler( |
| 100 | + constraints_func=constrained_sphere2d.constraints_func, |
| 101 | + seed=42 |
| 102 | + ), |
| 103 | + directions=constrained_sphere2d.directions |
| 104 | + ) |
| 105 | + study.optimize(constrained_sphere2d, n_trials=100) |
| 106 | + optuna.visualization.plot_optimization_history(study).show() |
| 107 | + plt.show() |
| 108 | +
|
| 109 | +.. figure:: ./images/optimization_history_constrained.png |
| 110 | + :alt: Optimization History |
| 111 | + :align: center |
| 112 | + :width: 800px |
| 113 | + |
| 114 | + |
| 115 | +Multi-Objective Problem |
| 116 | +^^^^^^^^^^^^^^^^^^^^^^^ |
| 117 | + |
| 118 | +You can also try multi-objective optimization. |
| 119 | +Here, we use the `the WFG Problem Collection <https://hub.optuna.org/benchmarks/wfg/>`__ as an example. |
| 120 | +In order to use this module, you need to install `optproblems <https://pypi.org/project/optproblems/>`__ and `diversipy <https://pypi.org/project/diversipy/>`__ packages. |
| 121 | + |
| 122 | +.. code-block:: console |
| 123 | +
|
| 124 | + $ pip install -U optproblems diversipy |
| 125 | +
|
| 126 | +Example is as follows: |
| 127 | + |
| 128 | +.. code-block:: python |
| 129 | +
|
| 130 | + import optuna |
| 131 | + import optunahub |
| 132 | +
|
| 133 | +
|
| 134 | + wfg = optunahub.load_module("benchmarks/wfg") |
| 135 | + wfg4 = wfg.Problem(function_id=4, n_objectives=2, dimension=3, k=1) |
| 136 | +
|
| 137 | + study = optuna.create_study( |
| 138 | + study_name="TPESampler", |
| 139 | + sampler=optuna.samplers.TPESampler(seed=42), directions=wfg4.directions |
| 140 | + ) |
| 141 | + study.optimize(wfg4, n_trials=1000) |
| 142 | +
|
| 143 | + optuna.visualization.plot_pareto_front(study).show() |
| 144 | +
|
| 145 | +.. figure:: ./images/pareto_front.png |
| 146 | + :alt: Pareto Front |
| 147 | + :align: center |
| 148 | + :width: 800px |
| 149 | + |
| 150 | +Keep Exploring! |
| 151 | +--------------- |
| 152 | + |
| 153 | +There are many kinds of benchmarks in OptunaHub. |
| 154 | +You can find them in the `OptunaHub Benchmarks <https://hub.optuna.org/?q=Benchmark>`__ page. |
0 commit comments