Skip to content

Commit

Permalink
Merge pull request #212 from y0z/feature/benchmarks-tutorial
Browse files Browse the repository at this point in the history
Add tutorial for constrained problems
  • Loading branch information
nabenabe0928 authored Dec 18, 2024
2 parents c946779 + d620cb2 commit 4629e85
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions recipes/007_benchmarks_advanced.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

import optuna
from optunahub.benchmarks import BaseProblem
from optunahub.benchmarks import ConstrainedMixin


###################################################################################################
Expand Down Expand Up @@ -63,6 +64,37 @@ def evaluate(self, params: dict[str, float]) -> float:
study = optuna.create_study(directions=dynamic_problem.directions)
study.optimize(dynamic_problem, n_trials=20)


###################################################################################################
# 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.
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


###################################################################################################
# Then, you can optimize the problem with Optuna as usual.
# Don't forget to set the `constraints_func` argument to the sampler to use.
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)

###################################################################################################
# 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.

0 comments on commit 4629e85

Please sign in to comment.