optunahub.benchmarks.ConstrainedMixin

class optunahub.benchmarks.ConstrainedMixin[源码]

用于约束优化问题的 Mixin 类。

示例

您可以通过继承此类并如下实现 evaluate_constraints() 方法来定义一个约束优化问题。

import optuna
import optunahub

class BinAndKorn(optunahub.benchmarks.ConstrainedMixin, optunahub.benchmarks.BaseProblem):
    def evaluate(self, params: dict[str, float]) -> tuple[float]:
        x = params["x"]
        y = params["y"]

        v0 = 4 * x**2 + 4 * y**2
        v1 = (x - 5)**2 + (y - 5)**2

        return v0, v1

    def evaluate_constraints(self, params: dict[str, float]) -> tuple[float]:
        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

        return c0, c1

    @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)
constraints_func(trial: FrozenTrial) Sequence[float][源码]

评估约束函数。

参数:

trial – Optuna trial 对象。

返回:

约束值的列表。

evaluate_constraints(params: dict[str, Any]) Sequence[float][源码]

评估约束函数。

参数:

params – 输入参数的字典。

返回:

约束值的列表。