.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "recipes/007_benchmarks_advanced.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_recipes_007_benchmarks_advanced.py: .. _benchmarks_advanced: How to Implement Your Benchmark Problems with OptunaHub (Advanced) ================================================================== OptunaHub provides the ``optunahub.benchmarks`` module for implementing benchmark problems. In this tutorial, we will explain how to implement complex benchmark problems such as a problem with dynamic search space using ``optunahub.benchmarks``. For the implementation of simple benchmark problems, please refer to :ref:`benchmarks_basic`. .. GENERATED FROM PYTHON SOURCE LINES 14-20 Implementing a Problem with Dynamic Search Space ------------------------------------------------- Here, let's implement a problem with a dynamic search space. First of all, import `optuna` and other required modules. .. GENERATED FROM PYTHON SOURCE LINES 20-27 .. code-block:: Python from __future__ import annotations import optuna from optunahub.benchmarks import BaseProblem from optunahub.benchmarks import ConstrainedMixin .. GENERATED FROM PYTHON SOURCE LINES 28-31 Next, define your own problem class by inheriting ``BaseProblem`` class. To implement a problem with a dynamic search space, ``__call__(self, trial: optuna.Trial)`` method must be overridden so that we can define a dynamic search space in the define-by-run manner. Please note that ``direcitons`` property must also be implemented. .. GENERATED FROM PYTHON SOURCE LINES 31-55 .. code-block:: Python class DynamicProblem(BaseProblem): def __call__(self, trial: optuna.Trial) -> float: x = trial.suggest_float("x", -5, 5) if x < 0: # Parameter `y` exists only when `x` is negative. y = trial.suggest_float("y", -5, 5) return x**2 + y else: return x**2 @property def directions(self) -> list[optuna.study.StudyDirection]: return [optuna.study.StudyDirection.MINIMIZE] @property def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: # You can implement this property as you like, or leave it unimplemented (``BaseProblem`` provides this default behavior). raise NotImplementedError def evaluate(self, params: dict[str, float]) -> float: # You can implement this method as you like, or leave it unimplemented (``BaseProblem`` provides this default behavior). raise NotImplementedError .. GENERATED FROM PYTHON SOURCE LINES 56-60 The implementations of the ``search_space`` and ``evaluate`` are non-trivial when the search space is dynamic. However, since ``__call__(self, trial: optuna.Trial)`` does not have to depend on both the ``evaluate`` method and the ``search_space`` attribute internally, their implementations are up to users. If possible, you could provide their implementations, but this is not necessary to make your benchmark problem work. Please note that calling them will result in ``NotImplementedError`` if you leave them unimplemented. .. GENERATED FROM PYTHON SOURCE LINES 62-63 Then, you can optimize the problem with Optuna as usual. .. GENERATED FROM PYTHON SOURCE LINES 63-68 .. code-block:: Python dynamic_problem = DynamicProblem() study = optuna.create_study(directions=dynamic_problem.directions) study.optimize(dynamic_problem, n_trials=20) .. GENERATED FROM PYTHON SOURCE LINES 69-75 Implementing a problem with constraints ------------------------------------------------- Here, let's implement a problem with constraints. To implement a problem with constraints, you need to inherit ``ConstrainedMixin`` class in addition to ``BaseProblem`` and implement the ``evaluate_constraints`` method. The ``evaluate_constraints`` method evaluates the constraint functions given a dictionary of input parameters and returns a list of constraint values. Then, ``ConstrainedMixin`` internally defines the ``constraints_func`` method for Optuna samplers. .. GENERATED FROM PYTHON SOURCE LINES 75-88 .. code-block:: Python class ConstrainedProblem(ConstrainedMixin, DynamicProblem): def evaluate_constraints(self, params: dict[str, float]) -> tuple[float, float]: x = params["x"] c0 = x - 2 if "y" not in params: c1 = 0.0 # c1 <= 0, so c1 is satisfied in this case. return c0, c1 else: y = params["y"] c1 = x + y - 3 return c0, c1 .. GENERATED FROM PYTHON SOURCE LINES 89-91 Then, you can optimize the problem with Optuna as usual. Don't forget to set the `constraints_func` argument to the sampler to use. .. GENERATED FROM PYTHON SOURCE LINES 91-98 .. code-block:: Python problem = ConstrainedProblem() sampler = optuna.samplers.TPESampler( constraints_func=problem.constraints_func ) # Pass the constraints_func to the sampler. study = optuna.create_study(sampler=sampler, directions=problem.directions) study.optimize(problem, n_trials=20) .. rst-class:: sphx-glr-script-out .. code-block:: none /opt/hostedtoolcache/Python/3.11.11/x64/lib/python3.11/site-packages/optuna/_experimental.py:31: ExperimentalWarning: Argument ``constraints_func`` is an experimental feature. The interface can change in the future. .. GENERATED FROM PYTHON SOURCE LINES 99-101 After implementing your own benchmark problem, you can register it with OptunaHub. See :doc:`002_registration` for how to register your benchmark problem with OptunaHub. .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 0.095 seconds) .. _sphx_glr_download_recipes_007_benchmarks_advanced.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 007_benchmarks_advanced.ipynb <007_benchmarks_advanced.ipynb>` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 007_benchmarks_advanced.py <007_benchmarks_advanced.py>` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: 007_benchmarks_advanced.zip <007_benchmarks_advanced.zip>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_