|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +from abc import ABCMeta |
| 4 | +from abc import abstractmethod |
| 5 | +from typing import Any |
| 6 | +from typing import Sequence |
| 7 | + |
| 8 | +import optuna |
| 9 | + |
| 10 | + |
| 11 | +class BaseProblem(metaclass=ABCMeta): |
| 12 | + """Base class for optimization problems.""" |
| 13 | + |
| 14 | + def __call__(self, trial: optuna.Trial) -> float | Sequence[float]: |
| 15 | + """Objective function for Optuna. By default, this method calls :meth:`evaluate` with the parameters defined in :attr:`search_space`. |
| 16 | +
|
| 17 | + Args: |
| 18 | + trial: Optuna trial object. |
| 19 | + Returns: |
| 20 | + The objective value or a sequence of the objective values for multi-objective optimization. |
| 21 | + """ |
| 22 | + params = {} |
| 23 | + for name, dist in self.search_space.items(): |
| 24 | + params[name] = trial._suggest(name, dist) |
| 25 | + trial._check_distribution(name, dist) |
| 26 | + return self.evaluate(params) |
| 27 | + |
| 28 | + def evaluate(self, params: dict[str, Any]) -> float | Sequence[float]: |
| 29 | + """Evaluate the objective function. |
| 30 | +
|
| 31 | + Args: |
| 32 | + params: Dictionary of input parameters. |
| 33 | +
|
| 34 | + Returns: |
| 35 | + The objective value or a sequence of the objective values for multi-objective optimization. |
| 36 | +
|
| 37 | + Example: |
| 38 | + :: |
| 39 | +
|
| 40 | + def evaluate(self, params: dict[str, Any]) -> float: |
| 41 | + x = params["x"] |
| 42 | + y = params["y"] |
| 43 | + return x ** 2 + y |
| 44 | + """ |
| 45 | + raise NotImplementedError |
| 46 | + |
| 47 | + @property |
| 48 | + def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: |
| 49 | + """Return the search space. |
| 50 | +
|
| 51 | + Returns: |
| 52 | + 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>`__). |
| 53 | +
|
| 54 | + Example: |
| 55 | + :: |
| 56 | +
|
| 57 | + @property |
| 58 | + def search_space(self) -> dict[str, optuna.distributions.BaseDistribution]: |
| 59 | + return { |
| 60 | + "x": optuna.distributions.FloatDistribution(low=0, high=1), |
| 61 | + "y": optuna.distributions.CategoricalDistribution(choices=[0, 1, 2]), |
| 62 | + } |
| 63 | + """ |
| 64 | + raise NotImplementedError |
| 65 | + |
| 66 | + @property |
| 67 | + @abstractmethod |
| 68 | + def directions(self) -> list[optuna.study.StudyDirection]: |
| 69 | + """Return the optimization directions. |
| 70 | +
|
| 71 | + Returns: |
| 72 | + List of `optuna.study.direction <https://optuna.readthedocs.io/en/stable/reference/generated/optuna.study.StudyDirection.html>`__. |
| 73 | +
|
| 74 | + Example: |
| 75 | + :: |
| 76 | +
|
| 77 | + @property |
| 78 | + def directions(self) -> list[optuna.study.StudyDirection]: |
| 79 | + return [optuna.study.StudyDirection.MINIMIZE] |
| 80 | + """ |
| 81 | + ... |
0 commit comments